Skip to content

Commit

Permalink
feat(with-history): improve redos logic to support withNewBatch handl…
Browse files Browse the repository at this point in the history
…e(inspired by slate) (#996)

ref: ianstormtaylor/slate#5747
  • Loading branch information
pubuzhixing8 authored Dec 9, 2024
1 parent f0f388f commit 6e52c59
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/chatty-kids-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@plait/core': minor
---

improve redos logic to support withNewBatch handle(inspired by slate)

ref: https://github.com/ianstormtaylor/slate/pull/5747
2 changes: 2 additions & 0 deletions packages/core/src/interfaces/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export interface PlaitHistory {

export const SAVING = new WeakMap<PlaitBoard, boolean | undefined>();
export const MERGING = new WeakMap<PlaitBoard, boolean | undefined>();
export const HISTORY = new WeakMap<PlaitBoard, History>();
export const SPLITTING_ONCE = new WeakMap<PlaitBoard, boolean | undefined>();
5 changes: 5 additions & 0 deletions packages/core/src/plugins/with-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ export function withHistory<T extends PlaitBoard>(board: T) {
}
}

if (PlaitHistoryBoard.isSplittingOnce(board)) {
merge = false;
PlaitHistoryBoard.setSplittingOnce(board, undefined);
}

if (lastBatch && merge) {
lastBatch.push(op);
} else {
Expand Down
41 changes: 40 additions & 1 deletion packages/core/src/utils/history.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { MERGING, PlaitBoard, PlaitOperation, SAVING } from '../interfaces';
// Credits to slate - https://github.com/ianstormtaylor/slate

import { MERGING, PlaitBoard, PlaitOperation, SAVING, SPLITTING_ONCE } from '../interfaces';

/**
* Check whether to merge an operation into the previous operation.
Expand Down Expand Up @@ -51,6 +53,43 @@ export const PlaitHistoryBoard = {
return MERGING.get(board);
},

/**
* Get the splitting once flag's current value.
*/

isSplittingOnce(board: PlaitBoard): boolean | undefined {
return SPLITTING_ONCE.get(board);
},

setSplittingOnce(board: PlaitBoard, value: boolean | undefined): void {
SPLITTING_ONCE.set(board, value);
},

/**
* Apply a series of changes inside a synchronous `fn`, These operations will
* be merged into the previous history.
*/
withMerging(board: PlaitBoard, fn: () => void): void {
const prev = PlaitHistoryBoard.isMerging(board);
MERGING.set(board, true);
fn();
MERGING.set(board, 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(board: PlaitBoard, fn: () => void): void {
const prev = PlaitHistoryBoard.isMerging(board);
MERGING.set(board, true);
SPLITTING_ONCE.set(board, true);
fn();
MERGING.set(board, prev);
SPLITTING_ONCE.delete(board);
},

/**
* 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

0 comments on commit 6e52c59

Please sign in to comment.