A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
fix(editor): Fix codemirror readOnly configuration
robin84aee63b0951
diff --git a/ui/src/components/Editor/ToolBars/image.tsx b/ui/src/components/Editor/ToolBars/image.tsxindex 60099193..12b3d1d9 100644--- a/ui/src/components/Editor/ToolBars/image.tsx+++ b/ui/src/components/Editor/ToolBars/image.tsx@@ -21,8 +21,6 @@ import { useEffect, useState, memo } from 'react';import { Button, Form, Modal, Tab, Tabs } from 'react-bootstrap';import { useTranslation } from 'react-i18next';-import { EditorState, StateEffect } from '@codemirror/state';-import { Modal as AnswerModal } from '@/components';import ToolItem from '../toolItem';import { IEditorContext, Editor } from '../types';@@ -149,18 +147,14 @@ const Image = ({ editorInstance }) => {const endPos = { ...startPos, ch: startPos.ch + loadingText.length };editor.replaceSelection(loadingText);- editor.dispatch({- effects: StateEffect.appendConfig.of([EditorState.readOnly.of(true)]),- });+ editor.setReadOnly(true);const urls = await upload(clipboard.files);const text = urls.map(({ name, url }) => {return ``;});editor.replaceRange(text.join('\n'), startPos, endPos);- editor.dispatch({- effects: StateEffect.appendConfig.of([EditorState.readOnly.of(false)]),- });+ editor.setReadOnly(false);return;}diff --git a/ui/src/components/Editor/types.ts b/ui/src/components/Editor/types.tsindex ee16d6b3..ce33833f 100644--- a/ui/src/components/Editor/types.ts+++ b/ui/src/components/Editor/types.ts@@ -62,6 +62,7 @@ export interface ExtendEditor {getCursor: () => Position;replaceRange: (value: string, from: Position, to: Position) => void;setSelection: (anchor: Position, head?: Position) => void;+ setReadOnly: (readOnly: boolean) => void;}export type Editor = EditorView & ExtendEditor;diff --git a/ui/src/components/Editor/utils/index.ts b/ui/src/components/Editor/utils/index.tsindex d380620b..bc9905d6 100644--- a/ui/src/components/Editor/utils/index.ts+++ b/ui/src/components/Editor/utils/index.ts@@ -20,7 +20,7 @@import { useEffect, useState } from 'react';import { minimalSetup } from 'codemirror';-import { EditorState } from '@codemirror/state';+import { EditorState, Compartment } from '@codemirror/state';import { EditorView, placeholder } from '@codemirror/view';import { markdown, markdownLanguage } from '@codemirror/lang-markdown';import { languages } from '@codemirror/language-data';@@ -29,6 +29,7 @@ import { Editor } from '../types';import createEditorUtils from './extension';+const editableCompartment = new Compartment();export function htmlRender(el: HTMLElement | null) {if (!el) return;// Replace all br tags with newlines@@ -111,6 +112,7 @@ export const useEditor = ({theme,placeholder(placeholderText),EditorView.lineWrapping,+ editableCompartment.of(EditorView.editable.of(true)),],});@@ -121,6 +123,14 @@ export const useEditor = ({const cm = createEditorUtils(view as Editor);+ cm.setReadOnly = (readOnly: boolean) => {+ cm.dispatch({+ effects: editableCompartment.reconfigure(+ EditorView.editable.of(!readOnly),+ ),+ });+ };+if (autoFocus) {setTimeout(() => {cm.focus();