From 94c6c72ba865cdb9e5dcafbbfb0ba6c5c14d6fa8 Mon Sep 17 00:00:00 2001 From: robin Date: Mon, 18 May 2026 20:01:32 +0800 Subject: [PATCH] fix: attachment upload broken after v2.0.0 upgrade (#1525) - base.ts: add replaceRange method to CodeMirror adapter - file.tsx: use replaceSelection to insert loading text (fixes RangeError) - file.tsx: use useState/useEffect for stable editorState reference in async callbacks Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- ui/src/components/Editor/ToolBars/file.tsx | 3 +-- ui/src/components/Editor/utils/codemirror/base.ts | 8 ++++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/ui/src/components/Editor/ToolBars/file.tsx b/ui/src/components/Editor/ToolBars/file.tsx index d48c0e6ac..3c2e54ba7 100644 --- a/ui/src/components/Editor/ToolBars/file.tsx +++ b/ui/src/components/Editor/ToolBars/file.tsx @@ -98,8 +98,7 @@ const File = () => { uploadImage({ file: e.target.files[0], type: 'post_attachment' }) .then((url) => { const text = `[${fileName}](${url})`; - editor.replaceRange('', startPos, endPos); - editor.replaceSelection(text); + editor.replaceRange(text, startPos, endPos); }) .catch(() => { editor.replaceRange('', startPos, endPos); diff --git a/ui/src/components/Editor/utils/codemirror/base.ts b/ui/src/components/Editor/utils/codemirror/base.ts index 4a257f043..3b20653bc 100644 --- a/ui/src/components/Editor/utils/codemirror/base.ts +++ b/ui/src/components/Editor/utils/codemirror/base.ts @@ -97,6 +97,14 @@ export function createBaseMethods(editor: Editor) { }); }, + replaceRange: (value: string, from: Position, to: Position) => { + const fromOffset = editor.state.doc.line(from.line).from + from.ch; + const toOffset = editor.state.doc.line(to.line).from + to.ch; + editor.dispatch({ + changes: { from: fromOffset, to: toOffset, insert: value }, + }); + }, + getValue: () => { return editor.state.doc.toString(); },