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

View File

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

View File

@@ -3,9 +3,19 @@ import { useEffect } from 'react'
import { $convertFromMarkdownString, TRANSFORMERS } from '@lexical/markdown' import { $convertFromMarkdownString, TRANSFORMERS } from '@lexical/markdown'
import { $generateNodesFromDOM } from '@lexical/html' import { $generateNodesFromDOM } from '@lexical/html'
import { $createParagraphNode, $createRangeSelection } from 'lexical' import { $createParagraphNode, $createRangeSelection } from 'lexical'
import { handleEditorChange } from '@standardnotes/blocks-editor/src/Editor/Utils'
import { SuperNotePreviewCharLimit } from '../../SuperEditor'
/** Note that markdown conversion does not insert new lines. See: https://github.com/facebook/lexical/issues/2815 */ /** Note that markdown conversion does not insert new lines. See: https://github.com/facebook/lexical/issues/2815 */
export default function ImportPlugin({ text, format }: { text: string; format: 'md' | 'html' }): JSX.Element | null { export default function ImportPlugin({
text,
format,
onChange,
}: {
text: string
format: 'md' | 'html'
onChange: (value: string, preview: string) => void
}): JSX.Element | null {
const [editor] = useLexicalComposerContext() const [editor] = useLexicalComposerContext()
useEffect(() => { useEffect(() => {
@@ -28,5 +38,13 @@ export default function ImportPlugin({ text, format }: { text: string; format: '
}) })
}, [editor, text, format]) }, [editor, text, format])
useEffect(() => {
return editor.registerUpdateListener(({ editorState }) => {
editorState.read(() => {
handleEditorChange(editorState, SuperNotePreviewCharLimit, onChange)
})
})
}, [editor, onChange])
return null return null
} }

View File

@@ -44,7 +44,7 @@ import ModalOverlay from '@/Components/Modal/ModalOverlay'
import MobileToolbarPlugin from './Plugins/MobileToolbarPlugin/MobileToolbarPlugin' import MobileToolbarPlugin from './Plugins/MobileToolbarPlugin/MobileToolbarPlugin'
import { SuperEditorNodes } from './SuperEditorNodes' import { SuperEditorNodes } from './SuperEditorNodes'
const NotePreviewCharLimit = 160 export const SuperNotePreviewCharLimit = 160
type Props = { type Props = {
application: WebApplication application: WebApplication
@@ -182,7 +182,7 @@ export const SuperEditor: FunctionComponent<Props> = ({
lineHeight && `leading-${lineHeight.toLowerCase()}`, lineHeight && `leading-${lineHeight.toLowerCase()}`,
fontSize ? getPlaintextFontSize(fontSize) : 'text-base', fontSize ? getPlaintextFontSize(fontSize) : 'text-base',
)} )}
previewLength={NotePreviewCharLimit} previewLength={SuperNotePreviewCharLimit}
spellcheck={spellcheck} spellcheck={spellcheck}
> >
<ItemSelectionPlugin currentNote={note.current} /> <ItemSelectionPlugin currentNote={note.current} />

View File

@@ -122,7 +122,7 @@ export const SuperNoteImporter: FunctionComponent<Props> = ({ note, application,
previewLength={NotePreviewCharLimit} previewLength={NotePreviewCharLimit}
spellcheck={note.spellcheck} spellcheck={note.spellcheck}
> >
<ImportPlugin text={note.text} format={format} /> <ImportPlugin text={note.text} format={format} onChange={handleChange} />
</BlocksEditor> </BlocksEditor>
</BlocksEditorComposer> </BlocksEditorComposer>
</ErrorBoundary> </ErrorBoundary>