fix: issue where converting empty plain note to super would result in error

This commit is contained in:
Mo
2022-11-18 14:53:18 -06:00
parent 328bbb797d
commit e58c563795
3 changed files with 19 additions and 6 deletions

View File

@@ -5,7 +5,7 @@ import {BlockEditorNodes} from '../Lexical/Nodes/AllNodes';
import {Klass, LexicalNode} from 'lexical';
type BlocksEditorComposerProps = {
initialValue: string;
initialValue: string | undefined;
children: React.ReactNode;
nodes?: Array<Klass<LexicalNode>>;
readonly?: boolean;

View File

@@ -9,6 +9,11 @@ export default function ImportPlugin({ text, format }: { text: string; format: '
const [editor] = useLexicalComposerContext()
useEffect(() => {
const dontAllowConversionOfEmptyStringWhichWouldResultInError = text.length === 0
if (dontAllowConversionOfEmptyStringWhichWouldResultInError) {
return
}
editor.update(() => {
if (format === 'md') {
$convertFromMarkdownString(text, [...TRANSFORMERS])

View File

@@ -25,6 +25,7 @@ type Props = {
}
export const SuperNoteImporter: FunctionComponent<Props> = ({ note, application, closeDialog, onConvertComplete }) => {
const isSeamlessConvert = note.text.length === 0
const [lastValue, setLastValue] = useState({ text: '', previewPlain: '' })
const format =
@@ -51,16 +52,18 @@ export const SuperNoteImporter: FunctionComponent<Props> = ({ note, application,
)
const confirmConvert = useCallback(async () => {
await performConvert(lastValue.text, lastValue.previewPlain)
closeDialog()
await performConvert(lastValue.text, lastValue.previewPlain)
onConvertComplete()
}, [closeDialog, performConvert, onConvertComplete, lastValue])
useEffect(() => {
if (note.text.length === 0) {
if (isSeamlessConvert) {
void confirmConvert()
}
}, [note, confirmConvert])
}, [isSeamlessConvert, confirmConvert])
const convertAsIs = useCallback(async () => {
const confirmed = await application.alertService.confirm(
@@ -75,12 +78,17 @@ export const SuperNoteImporter: FunctionComponent<Props> = ({ note, application,
return
}
closeDialog()
await performConvert(note.text, note.preview_plain)
closeDialog()
onConvertComplete()
}, [closeDialog, application, note, onConvertComplete, performConvert])
if (isSeamlessConvert) {
return null
}
return (
<ModalDialog>
<ModalDialogLabel closeDialog={closeDialog}>
@@ -93,7 +101,7 @@ export const SuperNoteImporter: FunctionComponent<Props> = ({ note, application,
<ModalDialogDescription>
<div className="relative w-full">
<ErrorBoundary>
<BlocksEditorComposer readonly initialValue={''}>
<BlocksEditorComposer readonly initialValue={undefined}>
<BlocksEditor
onChange={handleChange}
ignoreFirstChange={false}