refactor: merge blocks-editor package with web (#2217)
This commit is contained in:
37
packages/web/src/javascripts/Components/SuperEditor/Utils.ts
Normal file
37
packages/web/src/javascripts/Components/SuperEditor/Utils.ts
Normal 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}`,
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user