Files
standardnotes-app-web/packages/web/src/javascripts/Components/SuperEditor/Utils.ts
Mo 4a29e2a24c chore: upgrade eslint and prettier (#2376)
* chore: upgrade eslint and prettier

* chore: add restrict-template-expressions
2023-07-27 14:36:05 -05:00

40 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: ${JSON.stringify(
error,
)}`,
)
}
}