refactor: merge blocks-editor package with web (#2217)

This commit is contained in:
Aman Harwara
2023-02-17 18:35:17 +05:30
committed by GitHub
parent 135956ce73
commit 7bf76b51c5
158 changed files with 95 additions and 668 deletions

View File

@@ -0,0 +1,37 @@
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}`,
)
}
}