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

feat(with-history): improve redos logic to support withNewBatch handl… #996

Merged
merged 1 commit into from
Dec 9, 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
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
Loading