Skip to content

Commit 59c95f8

Browse files
Add undo/redo support to editable CodeBlock (#3288)
* Add undo/redo support to editable CodeBlock Enable CodeMirror's history() extension so existing historyKeymap keybindings (Cmd+Z / Cmd+Shift+Z) actually work. Annotate programmatic content replacements with addToHistory: false to keep prop syncs and blur reformats out of the undo stack. * Load history() in dynamicExtensions only if code block is editable * Remove duplicate onblur --------- Co-authored-by: Laura Whitaker <laura.whitaker@temporal.io>
1 parent dce87ac commit 59c95f8

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

src/lib/holocene/code-block.svelte

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import type { HTMLAttributes } from 'svelte/elements';
33
44
import { autocompletion, closeBrackets } from '@codemirror/autocomplete';
5-
import { historyKeymap, standardKeymap } from '@codemirror/commands';
5+
import { history, historyKeymap, standardKeymap } from '@codemirror/commands';
66
import {
77
bracketMatching,
88
ensureSyntaxTree,
@@ -11,7 +11,12 @@
1111
indentUnit,
1212
syntaxHighlighting,
1313
} from '@codemirror/language';
14-
import { Compartment, EditorState, type Extension } from '@codemirror/state';
14+
import {
15+
Compartment,
16+
EditorState,
17+
type Extension,
18+
Transaction,
19+
} from '@codemirror/state';
1520
import { EditorView, keymap } from '@codemirror/view';
1621
import { onMount, type Snippet } from 'svelte';
1722
import { twMerge as merge, twMerge } from 'tailwind-merge';
@@ -116,6 +121,7 @@
116121
to: doc.length,
117122
insert: newContent,
118123
},
124+
annotations: Transaction.addToHistory.of(false),
119125
});
120126
}
121127
};
@@ -156,6 +162,7 @@
156162
157163
let dynamicExtensions: Extension[] = $derived(
158164
[
165+
...(editable ? [history()] : []),
159166
getEditorTheme($useDarkMode, hasHeader),
160167
getActionsTheme({ hasActions: copyable || maximizable }),
161168
EditorState.readOnly.of(!editable),
@@ -285,7 +292,6 @@
285292
class={merge('h-full', className)}
286293
data-testid={testId}
287294
{...editorProps}
288-
onblur={handleEditorBlur}
289295
></div>
290296

291297
{#snippet actions()}

0 commit comments

Comments
 (0)