From 0e1e4b4dbf470d7ec795309e510ce683674a4ce5 Mon Sep 17 00:00:00 2001 From: Ziad Beyens Date: Mon, 21 Oct 2024 13:55:54 +0200 Subject: [PATCH] History: withNewBatch (#5747) * feat * Create gold-cheetahs-rest.md * refactor * fix --- .changeset/gold-cheetahs-rest.md | 5 ++++ packages/slate-history/src/history-editor.ts | 27 ++++++++++++++++++++ packages/slate-history/src/with-history.ts | 7 ++++- 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 .changeset/gold-cheetahs-rest.md diff --git a/.changeset/gold-cheetahs-rest.md b/.changeset/gold-cheetahs-rest.md new file mode 100644 index 0000000000..3a27e12d6e --- /dev/null +++ b/.changeset/gold-cheetahs-rest.md @@ -0,0 +1,5 @@ +--- +'slate-history': patch +--- + +Add `HistoryEditor.withNewBatch` diff --git a/packages/slate-history/src/history-editor.ts b/packages/slate-history/src/history-editor.ts index 4f20a8b8ee..d2f49670b7 100644 --- a/packages/slate-history/src/history-editor.ts +++ b/packages/slate-history/src/history-editor.ts @@ -8,6 +8,7 @@ import { History } from './history' export const HISTORY = new WeakMap() export const SAVING = new WeakMap() export const MERGING = new WeakMap() +export const SPLITTING_ONCE = new WeakMap() /** * `HistoryEditor` contains helpers for history-enabled editors. @@ -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. */ @@ -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. diff --git a/packages/slate-history/src/with-history.ts b/packages/slate-history/src/with-history.ts index 1f7ec6b3da..8b1fa083e8 100644 --- a/packages/slate-history/src/with-history.ts +++ b/packages/slate-history/src/with-history.ts @@ -1,4 +1,4 @@ -import { Editor, Operation, Path, Range, Transforms } from 'slate' +import { Editor, Operation, Path, Transforms } from 'slate' import { HistoryEditor } from './history-editor' @@ -90,6 +90,11 @@ export const withHistory = (editor: T) => { } } + if (HistoryEditor.isSplittingOnce(e)) { + merge = false + HistoryEditor.setSplittingOnce(e, undefined) + } + if (lastBatch && merge) { lastBatch.operations.push(op) } else {