mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-27 09:04:31 +02:00
History: withNewBatch (#5747)
* feat * Create gold-cheetahs-rest.md * refactor * fix
This commit is contained in:
5
.changeset/gold-cheetahs-rest.md
Normal file
5
.changeset/gold-cheetahs-rest.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'slate-history': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Add `HistoryEditor.withNewBatch`
|
@@ -8,6 +8,7 @@ import { History } from './history'
|
|||||||
export const HISTORY = new WeakMap<Editor, History>()
|
export const HISTORY = new WeakMap<Editor, History>()
|
||||||
export const SAVING = new WeakMap<Editor, boolean | undefined>()
|
export const SAVING = new WeakMap<Editor, boolean | undefined>()
|
||||||
export const MERGING = new WeakMap<Editor, boolean | undefined>()
|
export const MERGING = new WeakMap<Editor, boolean | undefined>()
|
||||||
|
export const SPLITTING_ONCE = new WeakMap<Editor, boolean | undefined>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* `HistoryEditor` contains helpers for history-enabled editors.
|
* `HistoryEditor` contains helpers for history-enabled editors.
|
||||||
@@ -38,6 +39,18 @@ export const HistoryEditor = {
|
|||||||
return MERGING.get(editor)
|
return MERGING.get(editor)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the splitting once flag's current value.
|
||||||
|
*/
|
||||||
|
|
||||||
|
isSplittingOnce(editor: HistoryEditor): boolean | undefined {
|
||||||
|
return SPLITTING_ONCE.get(editor)
|
||||||
|
},
|
||||||
|
|
||||||
|
setSplittingOnce(editor: HistoryEditor, value: boolean | undefined): void {
|
||||||
|
SPLITTING_ONCE.set(editor, value)
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the saving flag's current value.
|
* Get the saving flag's current value.
|
||||||
*/
|
*/
|
||||||
@@ -73,6 +86,20 @@ export const HistoryEditor = {
|
|||||||
MERGING.set(editor, prev)
|
MERGING.set(editor, prev)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply a series of changes inside a synchronous `fn`, ensuring that the first
|
||||||
|
* operation starts a new batch in the history. Subsequent operations will be
|
||||||
|
* merged as usual.
|
||||||
|
*/
|
||||||
|
withNewBatch(editor: HistoryEditor, fn: () => void): void {
|
||||||
|
const prev = HistoryEditor.isMerging(editor)
|
||||||
|
MERGING.set(editor, true)
|
||||||
|
SPLITTING_ONCE.set(editor, true)
|
||||||
|
fn()
|
||||||
|
MERGING.set(editor, prev)
|
||||||
|
SPLITTING_ONCE.delete(editor)
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply a series of changes inside a synchronous `fn`, without merging any of
|
* Apply a series of changes inside a synchronous `fn`, without merging any of
|
||||||
* the new operations into previous save point in the history.
|
* the new operations into previous save point in the history.
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { Editor, Operation, Path, Range, Transforms } from 'slate'
|
import { Editor, Operation, Path, Transforms } from 'slate'
|
||||||
|
|
||||||
import { HistoryEditor } from './history-editor'
|
import { HistoryEditor } from './history-editor'
|
||||||
|
|
||||||
@@ -90,6 +90,11 @@ export const withHistory = <T extends Editor>(editor: T) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (HistoryEditor.isSplittingOnce(e)) {
|
||||||
|
merge = false
|
||||||
|
HistoryEditor.setSplittingOnce(e, undefined)
|
||||||
|
}
|
||||||
|
|
||||||
if (lastBatch && merge) {
|
if (lastBatch && merge) {
|
||||||
lastBatch.operations.push(op)
|
lastBatch.operations.push(op)
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user