Skip to content

Commit 84e5a65

Browse files
andreas-schultzhaschek
authored andcommitted
CodeEditor component should switch to/from read-only mode without losing its current state
- CodeAutocompleteField correctly supports read-only mode now.
1 parent 86d4ea8 commit 84e5a65

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
@@ -10,6 +10,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
1010

1111
- Extended existing height and readOnly props from `CodeEditorProps` to `AutoSuggestionProps` & `ExtendedCodeEditorProps` to be configurable from `<CodeAutocompleteField />`
1212

13+
### Fixed
14+
15+
- <CodeMirror />:
16+
- Editor is re-created after certain property changes and is reset, i.e. loses it current state.
17+
18+
1319
## [24.3.0] - 2025-06-05
1420

1521
### Added

src/components/AutoSuggestion/AutoSuggestion.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ const AutoSuggestion = ({
673673
showScrollBar,
674674
multiline,
675675
handleInputMouseDown,
676+
readOnly
676677
]);
677678

678679
const hasError = !!value.current && !pathIsValid && !pathValidationPending;
@@ -716,6 +717,7 @@ const AutoSuggestion = ({
716717
data-test-id={"value-path-clear-btn"}
717718
name="operation-clear"
718719
text={clearIconText}
720+
disabled={readOnly}
719721
onClick={handleInputEditorClear}
720722
/>
721723
</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)