Skip to content

Commit 0e1e4b4

Browse files
authored
History: withNewBatch (#5747)
* feat * Create gold-cheetahs-rest.md * refactor * fix
1 parent f2e2117 commit 0e1e4b4

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

.changeset/gold-cheetahs-rest.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'slate-history': patch
3+
---
4+
5+
Add `HistoryEditor.withNewBatch`

packages/slate-history/src/history-editor.ts

+27
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { History } from './history'
88
export const HISTORY = new WeakMap<Editor, History>()
99
export const SAVING = new WeakMap<Editor, boolean | undefined>()
1010
export const MERGING = new WeakMap<Editor, boolean | undefined>()
11+
export const SPLITTING_ONCE = new WeakMap<Editor, boolean | undefined>()
1112

1213
/**
1314
* `HistoryEditor` contains helpers for history-enabled editors.
@@ -38,6 +39,18 @@ export const HistoryEditor = {
3839
return MERGING.get(editor)
3940
},
4041

42+
/**
43+
* Get the splitting once flag's current value.
44+
*/
45+
46+
isSplittingOnce(editor: HistoryEditor): boolean | undefined {
47+
return SPLITTING_ONCE.get(editor)
48+
},
49+
50+
setSplittingOnce(editor: HistoryEditor, value: boolean | undefined): void {
51+
SPLITTING_ONCE.set(editor, value)
52+
},
53+
4154
/**
4255
* Get the saving flag's current value.
4356
*/
@@ -73,6 +86,20 @@ export const HistoryEditor = {
7386
MERGING.set(editor, prev)
7487
},
7588

89+
/**
90+
* Apply a series of changes inside a synchronous `fn`, ensuring that the first
91+
* operation starts a new batch in the history. Subsequent operations will be
92+
* merged as usual.
93+
*/
94+
withNewBatch(editor: HistoryEditor, fn: () => void): void {
95+
const prev = HistoryEditor.isMerging(editor)
96+
MERGING.set(editor, true)
97+
SPLITTING_ONCE.set(editor, true)
98+
fn()
99+
MERGING.set(editor, prev)
100+
SPLITTING_ONCE.delete(editor)
101+
},
102+
76103
/**
77104
* Apply a series of changes inside a synchronous `fn`, without merging any of
78105
* the new operations into previous save point in the history.

packages/slate-history/src/with-history.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Editor, Operation, Path, Range, Transforms } from 'slate'
1+
import { Editor, Operation, Path, Transforms } from 'slate'
22

33
import { HistoryEditor } from './history-editor'
44

@@ -90,6 +90,11 @@ export const withHistory = <T extends Editor>(editor: T) => {
9090
}
9191
}
9292

93+
if (HistoryEditor.isSplittingOnce(e)) {
94+
merge = false
95+
HistoryEditor.setSplittingOnce(e, undefined)
96+
}
97+
9398
if (lastBatch && merge) {
9499
lastBatch.operations.push(op)
95100
} else {

0 commit comments

Comments
 (0)