This is an automated email from the ASF dual-hosted git repository.

robinv8 pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/answer.git


The following commit(s) were added to refs/heads/dev by this push:
     new 31641796 fix: attachment upload broken after v2.0.0 upgrade (#1525)
31641796 is described below

commit 316417961ad03525d8f2b06263a3a900e1664aa3
Author: robin <[email protected]>
AuthorDate: Mon May 18 20:01:32 2026 +0800

    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 <[email protected]>
---
 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 d48c0e6a..3c2e54ba 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 4a257f04..3b20653b 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();
     },

Reply via email to