Skip to content

Commit 62cd5c6

Browse files
CodeEditor component should switch to/from read-only mode without losing its current state
- CodeAutocompleteField correctly supports read-only mode now.
1 parent b2ad056 commit 62cd5c6

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ This is a major release, and it might be not compatible with your current usage
1414
- `<CodeAutocompleteField />:
1515
- outerDivAttributes parameter: Allows to set parameter of the container div element of the code complete field.
1616

17+
### Fixed
18+
19+
- <CodeMirror />:
20+
- Editor is re-created after certain property changes and is reset, i.e. loses it current state.
21+
22+
1723
## [24.3.0] - 2025-06-05
1824

1925
### Added

src/components/AutoSuggestion/AutoSuggestion.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ const AutoSuggestion = ({
676676
showScrollBar,
677677
multiline,
678678
handleInputMouseDown,
679+
readOnly
679680
]);
680681

681682
const hasError = !!value.current && !pathIsValid && !pathValidationPending;
@@ -720,6 +721,7 @@ const AutoSuggestion = ({
720721
data-test-id={"value-path-clear-btn"}
721722
name="operation-clear"
722723
text={clearIconText}
724+
disabled={readOnly}
723725
onClick={handleInputEditorClear}
724726
/>
725727
</span>

src/extensions/codemirror/CodeMirror.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useMemo, useRef } from "react";
22
import { defaultKeymap, indentWithTab } from "@codemirror/commands";
33
import { foldKeymap } from "@codemirror/language";
4-
import { EditorState, Extension } from "@codemirror/state";
4+
import { EditorState, Extension, Compartment } from "@codemirror/state";
55
import { DOMEventHandlers, EditorView, KeyBinding, keymap, Rect, ViewUpdate } from "@codemirror/view";
66
import { minimalSetup } from "codemirror";
77

@@ -221,7 +221,12 @@ export const CodeEditor = ({
221221
}: CodeEditorProps) => {
222222
const parent = useRef<any>(undefined);
223223
const [view, setView] = React.useState<EditorView | undefined>();
224+
const currentView = React.useRef<EditorView>()
225+
currentView.current = view
226+
const currentReadOnly = React.useRef(readOnly)
227+
currentReadOnly.current = readOnly
224228
const [showPreview, setShowPreview] = React.useState<boolean>(false);
229+
const readOnlyCompartment = React.useRef<Compartment>(new Compartment())
225230

226231
const linters = useMemo(() => {
227232
if (!mode) {
@@ -240,7 +245,7 @@ export const CodeEditor = ({
240245

241246
const onKeyDownHandler = (event: KeyboardEvent, view: EditorView) => {
242247
if (onKeyDown && !onKeyDown(event)) {
243-
if (event.key === "Enter") {
248+
if (event.key === "Enter" && !currentReadOnly.current) {
244249
const cursor = view.state.selection.main.head;
245250
const cursorLine = view.state.doc.lineAt(cursor).number;
246251
const offsetFromFirstLine = view.state.doc.line(cursorLine).to;
@@ -291,7 +296,7 @@ export const CodeEditor = ({
291296
useCodeMirrorModeExtension(mode),
292297
keymap?.of(keyMapConfigs),
293298
EditorState?.tabSize.of(tabIntentSize),
294-
EditorState?.readOnly.of(readOnly),
299+
readOnlyCompartment.current.of(EditorState?.readOnly.of(readOnly)),
295300
EditorView?.editable.of(!disabled),
296301
AdaptedEditorViewDomEventHandlers(domEventHandlers) as Extension,
297302
EditorView?.updateListener.of((v: ViewUpdate) => {
@@ -377,6 +382,14 @@ export const CodeEditor = ({
377382
};
378383
}, [parent.current, mode, preventLineNumbers, wrapLines]);
379384

385+
React.useEffect(() => {
386+
const v = EditorState?.readOnly.of(readOnly!)
387+
388+
currentView.current?.dispatch({
389+
effects: readOnlyCompartment.current.reconfigure(v)
390+
})
391+
}, [readOnly])
392+
380393
const hasToolbarSupport = mode && ModeToolbarSupport.indexOf(mode) > -1 && useToolbar;
381394

382395
const editorToolbar = (mode?: SupportedCodeEditorModes): JSX.Element => {

0 commit comments

Comments
 (0)