Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [Unreleased]

### Changed

- `<CodeEditor />`:
- `shouldHaveMinimalSetup`: Even if set to false, the edit history feature will still be explicitly enabled.

### Fixed

- `<Tooltip />`:
Expand Down
11 changes: 8 additions & 3 deletions src/extensions/codemirror/CodeMirror.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useMemo, useRef } from "react";
import { defaultKeymap, indentWithTab } from "@codemirror/commands";
import { defaultKeymap, history, historyKeymap, indentWithTab } from "@codemirror/commands";
import { defaultHighlightStyle, foldKeymap } from "@codemirror/language";
import { Compartment, EditorState, Extension } from "@codemirror/state";
import { DOMEventHandlers, EditorView, KeyBinding, keymap, Rect, ViewUpdate } from "@codemirror/view";
Expand Down Expand Up @@ -141,7 +141,8 @@ export interface CodeEditorProps
*/
additionalExtensions?: Extension[];
/**
* codemirror minimal setup flag
* CodeMirror minimal setup flag. If disabled, the rest of CodeMirror's minimal setup stays off,
* but editor history remains enabled explicitly.
*/
shouldHaveMinimalSetup?: boolean;
/**
Expand Down Expand Up @@ -265,6 +266,7 @@ export const CodeEditor = ({
const wrapLinesCompartment = React.useRef<Compartment>(compartment());
const preventLineNumbersCompartment = React.useRef<Compartment>(compartment());
const shouldHaveMinimalSetupCompartment = React.useRef<Compartment>(compartment());
const historyCompartment = React.useRef<Compartment>(compartment());
const placeholderCompartment = React.useRef<Compartment>(compartment());
const modeCompartment = React.useRef<Compartment>(compartment());
const keyMapConfigsCompartment = React.useRef<Compartment>(compartment());
Expand Down Expand Up @@ -319,6 +321,7 @@ export const CodeEditor = ({
!!(tabIntentStyle === "tab" && mode && !(tabForceSpaceForModes ?? []).includes(mode)) || enableTab;
return [
defaultKeymap as KeyBinding,
...addToKeyMapConfigFor(!shouldHaveMinimalSetup, ...historyKeymap),
...addToKeyMapConfigFor(supportCodeFolding, ...foldKeymap),
...addToKeyMapConfigFor(tabIndent, indentWithTab),
];
Expand Down Expand Up @@ -354,6 +357,7 @@ export const CodeEditor = ({
...addHandlersFor(!!onKeyDown, "keydown", onKeyDownHandler),
} as DOMEventHandlers<any>;
const extensions = [
historyCompartment.current.of(addExtensionsFor(!shouldHaveMinimalSetup, history())),
markField,
placeholderCompartment.current.of(adaptedPlaceholder(placeholder)),
adaptedHighlightSpecialChars(),
Expand Down Expand Up @@ -478,7 +482,7 @@ export const CodeEditor = ({

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

React.useEffect(() => {
updateExtension(EditorState?.tabSize.of(tabIntentSize ?? 2), tabIntentSizeCompartment.current);
Expand Down Expand Up @@ -526,6 +530,7 @@ export const CodeEditor = ({
addExtensionsFor(shouldHaveMinimalSetup ?? true, minimalSetup),
shouldHaveMinimalSetupCompartment.current,
);
updateExtension(addExtensionsFor(!shouldHaveMinimalSetup, history()), historyCompartment.current);
}, [shouldHaveMinimalSetup]);

React.useEffect(() => {
Expand Down
Loading