Skip to content

Commit b4ef376

Browse files
Do not disable history in CodeEditor component when shouldHaveMinimalSetup==false
1 parent c74f47f commit b4ef376

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
66

77
## [Unreleased]
88

9+
### Changed
10+
11+
- `<CodeEditor />`:
12+
- `shouldHaveMinimalSetup`: Even if set to false, the edit history feature will still be explicitly enabled.
13+
914
### Fixed
1015

1116
- `<Tooltip />`:

src/extensions/codemirror/CodeMirror.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useMemo, useRef } from "react";
2-
import { defaultKeymap, indentWithTab } from "@codemirror/commands";
2+
import { defaultKeymap, history, historyKeymap, indentWithTab } from "@codemirror/commands";
33
import { defaultHighlightStyle, foldKeymap } from "@codemirror/language";
44
import { Compartment, EditorState, Extension } from "@codemirror/state";
55
import { DOMEventHandlers, EditorView, KeyBinding, keymap, Rect, ViewUpdate } from "@codemirror/view";
@@ -141,7 +141,8 @@ export interface CodeEditorProps
141141
*/
142142
additionalExtensions?: Extension[];
143143
/**
144-
* codemirror minimal setup flag
144+
* CodeMirror minimal setup flag. If disabled, the rest of CodeMirror's minimal setup stays off,
145+
* but editor history remains enabled explicitly.
145146
*/
146147
shouldHaveMinimalSetup?: boolean;
147148
/**
@@ -265,6 +266,7 @@ export const CodeEditor = ({
265266
const wrapLinesCompartment = React.useRef<Compartment>(compartment());
266267
const preventLineNumbersCompartment = React.useRef<Compartment>(compartment());
267268
const shouldHaveMinimalSetupCompartment = React.useRef<Compartment>(compartment());
269+
const historyCompartment = React.useRef<Compartment>(compartment());
268270
const placeholderCompartment = React.useRef<Compartment>(compartment());
269271
const modeCompartment = React.useRef<Compartment>(compartment());
270272
const keyMapConfigsCompartment = React.useRef<Compartment>(compartment());
@@ -319,6 +321,7 @@ export const CodeEditor = ({
319321
!!(tabIntentStyle === "tab" && mode && !(tabForceSpaceForModes ?? []).includes(mode)) || enableTab;
320322
return [
321323
defaultKeymap as KeyBinding,
324+
...addToKeyMapConfigFor(!shouldHaveMinimalSetup, ...historyKeymap),
322325
...addToKeyMapConfigFor(supportCodeFolding, ...foldKeymap),
323326
...addToKeyMapConfigFor(tabIndent, indentWithTab),
324327
];
@@ -354,6 +357,7 @@ export const CodeEditor = ({
354357
...addHandlersFor(!!onKeyDown, "keydown", onKeyDownHandler),
355358
} as DOMEventHandlers<any>;
356359
const extensions = [
360+
historyCompartment.current.of(addExtensionsFor(!shouldHaveMinimalSetup, history())),
357361
markField,
358362
placeholderCompartment.current.of(adaptedPlaceholder(placeholder)),
359363
adaptedHighlightSpecialChars(),
@@ -478,7 +482,7 @@ export const CodeEditor = ({
478482

479483
React.useEffect(() => {
480484
updateExtension(keymap?.of(createKeyMapConfigs()), keyMapConfigsCompartment.current);
481-
}, [supportCodeFolding, mode, tabIntentStyle, (tabForceSpaceForModes ?? []).join(", "), enableTab]);
485+
}, [supportCodeFolding, mode, tabIntentStyle, (tabForceSpaceForModes ?? []).join(", "), enableTab, shouldHaveMinimalSetup]);
482486

483487
React.useEffect(() => {
484488
updateExtension(EditorState?.tabSize.of(tabIntentSize ?? 2), tabIntentSizeCompartment.current);
@@ -526,6 +530,7 @@ export const CodeEditor = ({
526530
addExtensionsFor(shouldHaveMinimalSetup ?? true, minimalSetup),
527531
shouldHaveMinimalSetupCompartment.current,
528532
);
533+
updateExtension(addExtensionsFor(!shouldHaveMinimalSetup, history()), historyCompartment.current);
529534
}, [shouldHaveMinimalSetup]);
530535

531536
React.useEffect(() => {

0 commit comments

Comments
 (0)