-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhistory.ts
152 lines (135 loc) · 4.21 KB
/
history.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { fabric } from "fabric";
import Page from "./page";
import Pages from "./pages";
import { FabricObject, ObjectId } from "../types/fabric";
import AssertType from "../types/assert";
interface HistoryItem {
ids: readonly number[];
oldObjects: fabric.Object[] | null;
newObjects: fabric.Object[] | null;
page: number;
}
export interface HistoryCommand {
add?: fabric.Object[];
remove?: fabric.Object[];
clear?: readonly [boolean];
}
export default class HistoryHandler {
history: HistoryItem[] = [];
redoStack: HistoryItem[] = [];
selection: fabric.Object[] | null = null;
latestId = 0;
locked = false;
constructor(
public canvas: Page,
public pages: Pages,
public updateState: () => void
) {}
/**
* Behavior is undefined if the same object is in both the `add` and `remove` properties of {@param command}
*/
execute = (command: HistoryCommand = {}): void => {
if (command.clear) this.clear(command.clear[0]);
this.add(command.add);
this.remove(command.remove);
};
/**
* Creates a history event adding {@param objects} if it is nonempty.
*/
add = (objects?: fabric.Object[]): void => {
if (objects?.length) this.save({ newObjects: objects });
};
/**
* Creates a history event removing {@param objects} if it is nonempty.
*/
remove = (objects?: fabric.Object[]): void => {
if (objects?.length) this.save({ oldObjects: objects });
};
clear = (clearRedo = false): void => {
this.history = [];
if (clearRedo) this.redoStack = [];
this.updateState();
};
/**
* Have history remember the current selection {@param objects},
* in case it is deleted/modified.
*/
store = (objects: readonly fabric.Object[]): void => {
if (this.locked) return;
this.locked = true;
this.selection = this.canvas.serialize(objects);
this.locked = false;
};
/**
* If the active selection (known to history) is modified to become {@param objects}
*/
modify = (objects: fabric.Object[]): void =>
this.save({ oldObjects: this.selection, newObjects: objects });
private getNextId = (): number => {
return ++this.latestId;
};
private setIdIfNotPresent = (object: FabricObject): number => {
AssertType<ObjectId>(object);
if (object.idVersion !== 1 || object.id === undefined) {
// Legacy versions would export ids in the serialized files,
// and we don't want those because they can clash.
// Instead of deleting all these ids at import time,
// we do it at history push time.
object.idVersion = 1;
object.id = this.getNextId();
}
return object.id;
};
/**
* Adds an entry to the history stack where {@param oldObjects} are replaced by {@param newObjects}.
* Does not check to make sure that these are nonempty;
* you may be creating an empty entry in history in this case.
*/
private save = ({
oldObjects,
newObjects,
}:
| { oldObjects: fabric.Object[]; newObjects? }
| {
oldObjects?;
newObjects: fabric.Object[];
}): void => {
if (this.locked) return;
const basis = newObjects || oldObjects;
this.locked = true;
this.history.push({
ids: basis.map(this.setIdIfNotPresent),
oldObjects: newObjects ? oldObjects : this.canvas.serialize(oldObjects),
newObjects: newObjects && this.canvas.serialize(newObjects),
page: this.pages.currentIndex,
});
this.locked = false;
this.redoStack = [];
this.canvas.modified = true;
this.updateState();
};
undo = async (): Promise<void> => {
if (!this.history.length) return;
this.canvas.discardActiveObject();
await this.move(this.history, this.redoStack, true);
};
redo = async (): Promise<void> => {
if (!this.redoStack.length) return;
await this.move(this.redoStack, this.history, false);
};
private move = async (
from: HistoryItem[],
to: HistoryItem[],
isUndo: boolean
): Promise<void> => {
if (!from.length) return;
this.locked = true;
const last = from.pop()!;
await this.pages.loadPage(last.page);
this.canvas.apply(last.ids, isUndo ? last.oldObjects : last.newObjects);
to.push(last);
this.locked = false;
this.canvas.modified = true;
this.updateState();
};
}