Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

History: withNewBatch #5747

Merged
merged 4 commits into from
Oct 21, 2024
Merged
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 .changeset/gold-cheetahs-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-history': patch
---

Add `HistoryEditor.withNewBatch`
27 changes: 27 additions & 0 deletions packages/slate-history/src/history-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { History } from './history'
export const HISTORY = new WeakMap<Editor, History>()
export const SAVING = 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.
Expand Down Expand Up @@ -38,6 +39,18 @@ export const HistoryEditor = {
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.
*/
Expand Down Expand Up @@ -73,6 +86,20 @@ export const HistoryEditor = {
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
* the new operations into previous save point in the history.
Expand Down
7 changes: 6 additions & 1 deletion packages/slate-history/src/with-history.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Editor, Operation, Path, Range, Transforms } from 'slate'
import { Editor, Operation, Path, Transforms } from 'slate'

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

Expand Down Expand Up @@ -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) {
lastBatch.operations.push(op)
} else {
Expand Down
Loading