fix: Fixed issue where converting to Super would result in empty note

This commit is contained in:
Aman Harwara
2023-02-01 21:25:12 +05:30
parent 0a72e8f609
commit cf4e34858f
5 changed files with 55 additions and 27 deletions

View File

@@ -11,7 +11,7 @@ import { HashtagPlugin } from '@lexical/react/LexicalHashtagPlugin'
import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin'
import { LinkPlugin } from '@lexical/react/LexicalLinkPlugin'
import { ListPlugin } from '@lexical/react/LexicalListPlugin'
import { $getRoot, EditorState, LexicalEditor } from 'lexical'
import { EditorState, LexicalEditor } from 'lexical'
import HorizontalRulePlugin from '../Lexical/Plugins/HorizontalRulePlugin'
import TwitterPlugin from '../Lexical/Plugins/TwitterPlugin'
import YouTubePlugin from '../Lexical/Plugins/YouTubePlugin'
@@ -22,7 +22,7 @@ import CodeHighlightPlugin from '../Lexical/Plugins/CodeHighlightPlugin'
import FloatingTextFormatToolbarPlugin from '../Lexical/Plugins/FloatingTextFormatToolbarPlugin'
import FloatingLinkEditorPlugin from '../Lexical/Plugins/FloatingLinkEditorPlugin'
import { TabIndentationPlugin } from '../Lexical/Plugins/TabIndentationPlugin'
import { truncateString } from './Utils'
import { handleEditorChange } from './Utils'
import { SuperEditorContentId } from './Constants'
import { classNames } from '@standardnotes/utils'
import { MarkdownTransformers } from './MarkdownTransformers'
@@ -55,27 +55,7 @@ export const BlocksEditor: FunctionComponent<BlocksEditorProps> = ({
}
editorState.read(() => {
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}`,
)
}
handleEditorChange(editorState, previewLength, onChange)
})
},
// Ignoring 'ignoreFirstChange' and 'previewLength'

View File

@@ -1,3 +1,5 @@
import { $getRoot, EditorState } from 'lexical'
export function truncateString(string: string, limit: number) {
if (string.length <= limit) {
return string
@@ -5,3 +7,31 @@ export function truncateString(string: string, limit: number) {
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}`,
)
}
}