Skip to content

Commit

Permalink
fix: fix md handle error
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanyxh committed May 19, 2024
1 parent ad070b4 commit 5fcf2dc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 30 deletions.
4 changes: 3 additions & 1 deletion src/filehandle/md_editor/component/MDContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export const MDContent = forwardRef<IMDContentExpose, IMDContentProps>(
() => ({
confirm() {
return new Promise<boolean>((resolve, reject) => {
if (!changed) return resolve(true);

confirm({
title: '温馨提示',
content: '是否保存更改?如果不保存,您的更改会丢失。',
Expand All @@ -80,7 +82,7 @@ export const MDContent = forwardRef<IMDContentExpose, IMDContentProps>(
});
}
}),
[currentHandle]
[currentHandle, changed]
);

const handleSelect = async (handle: FH) => {
Expand Down
45 changes: 16 additions & 29 deletions src/filehandle/md_editor/component/MDEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,11 @@ const MDEditor = forwardRef<IMDEditorExpose, IMDEditorProps>(
function MDEditor(props, ref) {
const { currentHandle, changed, onChanged, onSave } = props;

uploadInfo = useMDStore().uploadInfo;

const editorContainerRef = useRef<HTMLDivElement>(null);
const editorRef = useRef<Editor>();
const mdStringRef = useRef('');
const creatingRef = useRef(false);

uploadInfo = useMDStore().uploadInfo;

useImperativeHandle(ref, () => ({
getMarkdown() {
Expand All @@ -177,32 +176,20 @@ const MDEditor = forwardRef<IMDEditorExpose, IMDEditorProps>(
}));

useMemo(() => {
if (creatingRef.current) {
return void 0;
}

if (editorRef.current) {
editorRef.current.destroy(true);
}

if (currentHandle) {
creatingRef.current = true;

currentHandle
.getFile()
.then((file) => file.text())
.then((markdown) => {
createMDEditor(editorContainerRef.current!, markdown, onUpdate)
.then((value) => {
editorRef.current = value;
mdStringRef.current = markdown;
onChanged(false);
})
.finally(() => {
creatingRef.current = false;
});
});
}
currentHandle
?.getFile()
.then((file) => file.text())
.then((markdown) => {
createMDEditor(editorContainerRef.current!, markdown, onUpdate).then(
(value) => {
editorRef.current && editorRef.current.destroy(true);

editorRef.current = value;
mdStringRef.current = markdown;
onChanged(false);
}
);
});

return () => {
editorRef.current?.destroy(true);
Expand Down

0 comments on commit 5fcf2dc

Please sign in to comment.