This is an automated email from the ASF dual-hosted git repository. robinv8 pushed a commit to branch fix/issue-1525-attachment-upload in repository https://gitbox.apache.org/repos/asf/answer.git
commit b99762ed15705372cc81a1d275e287e47fd2aedb 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 | 35 ++++++++++++++--------- ui/src/components/Editor/utils/codemirror/base.ts | 8 ++++++ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/ui/src/components/Editor/ToolBars/file.tsx b/ui/src/components/Editor/ToolBars/file.tsx index d48c0e6a..a6a665af 100644 --- a/ui/src/components/Editor/ToolBars/file.tsx +++ b/ui/src/components/Editor/ToolBars/file.tsx @@ -17,21 +17,31 @@ * under the License. */ -import { memo, useRef, useContext } from 'react'; +import { memo, useRef, useContext, useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { Modal as AnswerModal } from '@/components'; import ToolItem from '../toolItem'; import { EditorContext } from '../EditorContext'; +import { Editor } from '../types'; import { uploadImage } from '@/services'; import { writeSettingStore } from '@/stores'; const File = () => { const { t } = useTranslation('translation', { keyPrefix: 'editor' }); - const { max_attachment_size = 8, authorized_attachment_extensions = [] } = - writeSettingStore((state) => state.write); + const writeSettings = writeSettingStore((state) => state.write) || {}; + const max_attachment_size = writeSettings.max_attachment_size ?? 8; + const authorized_attachment_extensions = + writeSettings.authorized_attachment_extensions ?? []; const fileInputRef = useRef<HTMLInputElement>(null); const editor = useContext(EditorContext); + const [editorState, setEditorState] = useState<Editor | null>(editor); + + useEffect(() => { + if (editor) { + setEditorState(editor); + } + }, [editor]); const item = { label: 'paperclip', @@ -78,7 +88,7 @@ const File = () => { }; const onUpload = async (e) => { - if (!editor) { + if (!editorState) { return; } const files = e.target?.files || []; @@ -89,24 +99,23 @@ const File = () => { } const fileName = files[0].name; const loadingText = `![${t('image.uploading')} ${fileName}...]()`; - const startPos = editor.getCursor(); - + const startPos = editorState.getCursor(); const endPos = { ...startPos, ch: startPos.ch + loadingText.length }; - editor.replaceSelection(loadingText); - editor.setReadOnly(true); + + editorState.replaceSelection(loadingText); + editorState.setReadOnly(true); uploadImage({ file: e.target.files[0], type: 'post_attachment' }) .then((url) => { const text = `[${fileName}](${url})`; - editor.replaceRange('', startPos, endPos); - editor.replaceSelection(text); + editorState.replaceRange(text, startPos, endPos); }) .catch(() => { - editor.replaceRange('', startPos, endPos); + editorState.replaceRange('', startPos, endPos); }) .finally(() => { - editor.setReadOnly(false); - editor.focus(); + editorState?.setReadOnly(false); + editorState?.focus(); }); }; 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(); },
