Files
standardnotes-app-web/packages/web/src/javascripts/Components/SuperEditor/Utils.ts
2023-02-17 18:35:17 +05:30

38 lines
1.0 KiB
TypeScript

import { $getRoot, EditorState } from 'lexical'
export function truncateString(string: string, limit: number) {
if (string.length <= limit) {
return string
} else {
return string.substring(0, limit) + '...'
}
}
export function handleEditorChange(
editorState: EditorState,
previewLength?: number,
onChange?: (value: string, previewText: string) => void,
) {
const childrenNodes = $getRoot().getAllTextNodes().slice(0, 2)
let previewText = ''
childrenNodes.forEach((node, index) => {
previewText += node.getTextContent()
if (index !== childrenNodes.length - 1) {
previewText += '\n'
}
})
if (previewLength) {
previewText = truncateString(previewText, previewLength)
}
try {
const stringifiedEditorState = JSON.stringify(editorState.toJSON())
onChange?.(stringifiedEditorState, previewText)
} catch (error) {
window.alert(
`An invalid change was made inside the Super editor. Your change was not saved. Please report this error to the team: ${error}`,
)
}
}