Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

Guard editor commands during teardown

tianmac1cb780a7826
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsx
index 3633774d..50fad5b9 100644
--- a/apps/web/src/components/EditorPane.tsx
+++ b/apps/web/src/components/EditorPane.tsx
@@ -68,10 +68,13 @@ type NoteSearchMatch = {
to: number;
};
+const isEditorReady = (editor: Editor | null | undefined): editor is Editor =>
+ Boolean(editor && !editor.isDestroyed && (editor as { extensionManager?: unknown }).extensionManager);
+
const getEditorSearchMatches = (editor: Editor | null, query: string): NoteSearchMatch[] => {
const needle = query.trim().toLocaleLowerCase();
- if (!editor || needle.length === 0) {
+ if (!isEditorReady(editor) || needle.length === 0) {
return [];
}
@@ -314,7 +317,10 @@ export const EditorPane = ({
setIsMobileEditing(true);
window.requestAnimationFrame(() => {
window.requestAnimationFrame(() => {
- editorRef.current?.commands.focus("end");
+ const currentEditor = editorRef.current;
+ if (isEditorReady(currentEditor)) {
+ currentEditor.commands.focus("end");
+ }
});
});
onMobileDefaultEditConsumed();
@@ -344,12 +350,13 @@ export const EditorPane = ({
const { resource } = await api.uploadMemoResource(targetMemoId, uploadFile);
void queryClient.invalidateQueries({ queryKey: ["resources"] });
- if (memoRef.current?.id !== targetMemoId || !editorRef.current) {
+ const activeEditor = editorRef.current;
+ if (memoRef.current?.id !== targetMemoId || !isEditorReady(activeEditor)) {
setImageUploadState("idle");
return;
}
- editorRef.current
+ activeEditor
.chain()
.focus()
.setImage({
@@ -392,13 +399,14 @@ export const EditorPane = ({
const { resource } = await api.uploadMemoResource(targetMemoId, uploadFile);
void queryClient.invalidateQueries({ queryKey: ["resources"] });
- if (memoRef.current?.id !== targetMemoId || !editorRef.current) {
+ const activeEditor = editorRef.current;
+ if (memoRef.current?.id !== targetMemoId || !isEditorReady(activeEditor)) {
setImageUploadState("idle");
return;
}
if (resource.kind === "image") {
- editorRef.current
+ activeEditor
.chain()
.focus()
.setImage({
@@ -408,7 +416,7 @@ export const EditorPane = ({
})
.run();
} else {
- editorRef.current
+ activeEditor
.chain()
.focus()
.insertContent({
@@ -491,7 +499,7 @@ export const EditorPane = ({
(index: number) => {
const match = noteSearchMatches[index];
- if (!editor || !match) {
+ if (!isEditorReady(editor) || !match) {
return;
}
@@ -521,7 +529,9 @@ export const EditorPane = ({
const closeNoteSearch = useCallback(() => {
setNoteSearchOpen(false);
- editor?.commands.focus();
+ if (isEditorReady(editor)) {
+ editor.commands.focus();
+ }
}, [editor]);
const moveNoteSearchMatch = useCallback(
@@ -564,7 +574,7 @@ export const EditorPane = ({
}, [noteSearchMatches, noteSearchOpen, selectNoteSearchMatch]);
const replaceAllNoteSearchMatches = useCallback(() => {
- if (!editor || effectiveReadOnly || noteSearchMatches.length === 0) {
+ if (!isEditorReady(editor) || effectiveReadOnly || noteSearchMatches.length === 0) {
return;
}
@@ -586,7 +596,7 @@ export const EditorPane = ({
}, [editor, effectiveReadOnly, noteSearchMatches, noteSearchReplacement]);
useEffect(() => {
- if (!editor) {
+ if (!isEditorReady(editor)) {
return;
}
@@ -605,7 +615,7 @@ export const EditorPane = ({
const currentMemo = memoRef.current;
const currentEditor = editorRef.current;
- if (!currentMemo || currentMemo.isDeleted || !currentEditor) {
+ if (!currentMemo || currentMemo.isDeleted || !isEditorReady(currentEditor)) {
return;
}
@@ -634,7 +644,7 @@ export const EditorPane = ({
const currentSnapshot = useCallback(() => {
const currentEditor = editorRef.current;
- if (!currentEditor) {
+ if (!isEditorReady(currentEditor)) {
return null;
}
@@ -657,7 +667,9 @@ export const EditorPane = ({
setTitle("");
setTagsText("");
setSaveState("idle");
- currentEditor?.commands.clearContent();
+ if (isEditorReady(currentEditor)) {
+ currentEditor.commands.clearContent();
+ }
return;
}
@@ -696,7 +708,7 @@ export const EditorPane = ({
setTitle(nextTitle);
setTagsText(nextTagsText);
- if (currentEditor) {
+ if (isEditorReady(currentEditor)) {
currentEditor.commands.setContent(nextContent);
}
@@ -711,11 +723,13 @@ export const EditorPane = ({
}, [isTrashView, memo, editor]);
useEffect(() => {
- editor?.setEditable(Boolean(memo && !effectiveReadOnly));
+ if (isEditorReady(editor)) {
+ editor.setEditable(Boolean(memo && !effectiveReadOnly));
+ }
}, [editor, effectiveReadOnly, memo]);
useEffect(() => {
- if (!editor || !memo) {
+ if (!isEditorReady(editor) || !memo) {
return;
}
@@ -738,7 +752,7 @@ export const EditorPane = ({
const currentMemo = memoRef.current;
const currentEditor = editorRef.current;
- if (!currentMemo || !currentEditor) {
+ if (!currentMemo || !isEditorReady(currentEditor)) {
throw new Error("No memo selected");
}
@@ -1371,7 +1385,7 @@ export const EditorPane = ({
{historyOpen && (
<RevisionHistoryDialog
- currentMarkdown={editor ? docToMarkdown(editor.getJSON() as TiptapDoc) : memo.contentMarkdown}
+ currentMarkdown={isEditorReady(editor) ? docToMarkdown(editor.getJSON() as TiptapDoc) : memo.contentMarkdown}
memo={memo}
onClose={() => setHistoryOpen(false)}
onRestored={async (restoredMemo) => {
diff --git a/apps/web/src/components/EditorToolbar.tsx b/apps/web/src/components/EditorToolbar.tsx
index 5808bf02..59760894 100644
--- a/apps/web/src/components/EditorToolbar.tsx
+++ b/apps/web/src/components/EditorToolbar.tsx
@@ -51,22 +51,47 @@ const EditorToolbarButton = ({
const ToolbarDivider = () => <div className="h-6 w-px shrink-0 bg-slate-200" />;
+const isToolbarEditorReady = (editor: Editor | null): editor is Editor =>
+ Boolean(editor && !editor.isDestroyed && (editor as { extensionManager?: unknown }).extensionManager);
+
export const EditorToolbar = ({ editor, readOnly }: { editor: Editor | null; readOnly: boolean }) => {
- const disabled = readOnly || !editor;
+ const editorReady = isToolbarEditorReady(editor);
+ const disabled = readOnly || !editorReady;
const blockValue = getActiveBlockValue(editor);
+ const isActive = (name: string) => {
+ if (!editorReady) {
+ return false;
+ }
+
+ try {
+ return editor.isActive(name);
+ } catch {
+ return false;
+ }
+ };
const canRun = (command: (editor: Editor) => boolean) => {
- if (!editor || readOnly) {
+ if (!isToolbarEditorReady(editor) || readOnly) {
+ return false;
+ }
+
+ try {
+ return command(editor);
+ } catch {
return false;
}
- return command(editor);
};
const run = (command: (editor: Editor) => void) => {
- if (!editor || readOnly) {
+ if (!isToolbarEditorReady(editor) || readOnly) {
+ return;
+ }
+
+ try {
+ command(editor);
+ } catch {
return;
}
- command(editor);
};
const setBlock = (value: string) => {
@@ -138,7 +163,7 @@ export const EditorToolbar = ({ editor, readOnly }: { editor: Editor | null; rea
<ToolbarDivider />
<EditorToolbarButton
title="加粗"
- active={Boolean(editor?.isActive("bold"))}
+ active={isActive("bold")}
disabled={!canRun((current) => current.can().chain().focus().toggleBold().run())}
onClick={() => run((current) => current.chain().focus().toggleBold().run())}
>
@@ -146,7 +171,7 @@ export const EditorToolbar = ({ editor, readOnly }: { editor: Editor | null; rea
</EditorToolbarButton>
<EditorToolbarButton
title="斜体"
- active={Boolean(editor?.isActive("italic"))}
+ active={isActive("italic")}
disabled={!canRun((current) => current.can().chain().focus().toggleItalic().run())}
onClick={() => run((current) => current.chain().focus().toggleItalic().run())}
>
@@ -154,7 +179,7 @@ export const EditorToolbar = ({ editor, readOnly }: { editor: Editor | null; rea
</EditorToolbarButton>
<EditorToolbarButton
title="删除线"
- active={Boolean(editor?.isActive("strike"))}
+ active={isActive("strike")}
disabled={!canRun((current) => current.can().chain().focus().toggleStrike().run())}
onClick={() => run((current) => current.chain().focus().toggleStrike().run())}
>
@@ -162,7 +187,7 @@ export const EditorToolbar = ({ editor, readOnly }: { editor: Editor | null; rea
</EditorToolbarButton>
<EditorToolbarButton
title="行内代码"
- active={Boolean(editor?.isActive("code"))}
+ active={isActive("code")}
disabled={!canRun((current) => current.can().chain().focus().toggleCode().run())}
onClick={() => run((current) => current.chain().focus().toggleCode().run())}
>
@@ -172,7 +197,7 @@ export const EditorToolbar = ({ editor, readOnly }: { editor: Editor | null; rea
<ToolbarDivider />
<EditorToolbarButton
title="无序列表"
- active={Boolean(editor?.isActive("bulletList"))}
+ active={isActive("bulletList")}
disabled={disabled}
onClick={() => run((current) => current.chain().focus().toggleBulletList().run())}
>
@@ -180,7 +205,7 @@ export const EditorToolbar = ({ editor, readOnly }: { editor: Editor | null; rea
</EditorToolbarButton>
<EditorToolbarButton
title="有序列表"
- active={Boolean(editor?.isActive("orderedList"))}
+ active={isActive("orderedList")}
disabled={disabled}
onClick={() => run((current) => current.chain().focus().toggleOrderedList().run())}
>
@@ -188,7 +213,7 @@ export const EditorToolbar = ({ editor, readOnly }: { editor: Editor | null; rea
</EditorToolbarButton>
<EditorToolbarButton
title="引用"
- active={Boolean(editor?.isActive("blockquote"))}
+ active={isActive("blockquote")}
disabled={disabled}
onClick={() => run((current) => current.chain().focus().toggleBlockquote().run())}
>
@@ -196,7 +221,7 @@ export const EditorToolbar = ({ editor, readOnly }: { editor: Editor | null; rea
</EditorToolbarButton>
<EditorToolbarButton
title="代码块"
- active={Boolean(editor?.isActive("codeBlock"))}
+ active={isActive("codeBlock")}
disabled={disabled}
onClick={() => run((current) => current.chain().focus().toggleCodeBlock().run())}
>
diff --git a/apps/web/src/lib/app-helpers.ts b/apps/web/src/lib/app-helpers.ts
index 4dec7632..a9e754e2 100644
--- a/apps/web/src/lib/app-helpers.ts
+++ b/apps/web/src/lib/app-helpers.ts
@@ -173,17 +173,22 @@ export const MEMO_FILTER_OPTIONS: Array<{ value: MemoFilterMode; label: string }
export const getMemoTitle = (title: string | null | undefined) => title?.trim() || DEFAULT_MEMO_TITLE;
export const getActiveBlockValue = (editor: any): string => {
- if (!editor) {
+ if (!editor || editor.isDestroyed || !editor.extensionManager) {
return "paragraph";
}
- if (editor.isActive("heading", { level: 1 })) {
- return "heading-1";
- }
- if (editor.isActive("heading", { level: 2 })) {
- return "heading-2";
- }
- if (editor.isActive("heading", { level: 3 })) {
- return "heading-3";
+
+ try {
+ if (editor.isActive("heading", { level: 1 })) {
+ return "heading-1";
+ }
+ if (editor.isActive("heading", { level: 2 })) {
+ return "heading-2";
+ }
+ if (editor.isActive("heading", { level: 3 })) {
+ return "heading-3";
+ }
+ } catch {
+ return "paragraph";
}
return "paragraph";
};