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

Feature: Specifying source for undo / redo #4570

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 10 additions & 10 deletions packages/quill/src/modules/history.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Scope } from 'parchment';
import type Delta from 'quill-delta';
import Module from '../core/module.js';
import Quill from '../core/quill.js';
import Quill, { EmitterSource } from '../core/quill.js';
import type Scroll from '../blots/scroll.js';
import type { Range } from '../core/selection.js';

Expand Down Expand Up @@ -82,19 +82,19 @@ class History extends Module<HistoryOptions> {
});
}

change(source: 'undo' | 'redo', dest: 'redo' | 'undo') {
if (this.stack[source].length === 0) return;
const item = this.stack[source].pop();
change(sourceStack: 'undo' | 'redo', destStack: 'redo' | 'undo', source: EmitterSource) {
if (this.stack[sourceStack].length === 0) return;
const item = this.stack[sourceStack].pop();
if (!item) return;
const base = this.quill.getContents();
const inverseDelta = item.delta.invert(base);
this.stack[dest].push({
this.stack[destStack].push({
delta: inverseDelta,
range: transformRange(item.range, inverseDelta),
});
this.lastRecorded = 0;
this.ignoreChange = true;
this.quill.updateContents(item.delta, Quill.sources.USER);
this.quill.updateContents(item.delta, source);
this.ignoreChange = false;

this.restoreSelection(item);
Expand Down Expand Up @@ -135,17 +135,17 @@ class History extends Module<HistoryOptions> {
}
}

redo() {
this.change('redo', 'undo');
redo(source: EmitterSource = Quill.sources.USER) {
this.change('redo', 'undo', source);
}

transform(delta: Delta) {
transformStack(this.stack.undo, delta);
transformStack(this.stack.redo, delta);
}

undo() {
this.change('undo', 'redo');
undo(source: EmitterSource = Quill.sources.USER) {
this.change('undo', 'redo', source);
}

protected restoreSelection(stackItem: StackItem) {
Expand Down