38 lines
1.4 KiB
JavaScript
38 lines
1.4 KiB
JavaScript
|
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
||
|
import {useEffect} from "react";
|
||
|
import {importFile} from "../../../../utils/File";
|
||
|
import {isEmpty} from "../../../../utils/ObjectUtils";
|
||
|
import {CLEAR_HISTORY_COMMAND} from "lexical";
|
||
|
import {TRANSFORMERS, $convertFromMarkdownString, $convertToMarkdownString,} from "@lexical/markdown";
|
||
|
export default function ImportFilePlugin(props) {
|
||
|
const [editor] = useLexicalComposerContext();
|
||
|
useEffect(() => {
|
||
|
if (props.filePath) {
|
||
|
importFile(props.filePath).then(value => {
|
||
|
if (isEmpty(value)) {
|
||
|
return
|
||
|
}
|
||
|
if (props.filePath.endsWith(".md")) {
|
||
|
console.log("$convertFromMarkdownString(value.toString(), TRANSFORMERS)",editor)
|
||
|
// const editorState = editor.parseEditorState($convertFromMarkdownString(value.toString(), TRANSFORMERS))
|
||
|
// editor.setEditorState(editorState);
|
||
|
// editor.dispatchCommand(CLEAR_HISTORY_COMMAND, undefined);
|
||
|
|
||
|
editor.update(() => {
|
||
|
$convertFromMarkdownString(value.toString(), TRANSFORMERS);
|
||
|
})
|
||
|
|
||
|
} else {
|
||
|
const editorState = editor.parseEditorState(
|
||
|
JSON.stringify(JSON.parse(value.toString()).editorState)
|
||
|
);
|
||
|
editor.setEditorState(editorState);
|
||
|
editor.dispatchCommand(CLEAR_HISTORY_COMMAND, undefined);
|
||
|
}
|
||
|
}).catch(error =>
|
||
|
console.error(error)
|
||
|
)
|
||
|
}
|
||
|
}, [])
|
||
|
}
|