-
Notifications
You must be signed in to change notification settings - Fork 571
/
Copy pathindex.ts
173 lines (146 loc) · 5 KB
/
index.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { sendAnalytics } from '@/lib/utils';
import type { Action, UpdateStyleAction } from '@onlook/models/actions';
import { jsonClone } from '@onlook/utility';
import { makeAutoObservable } from 'mobx';
import type { EditorEngine } from '..';
import { undoAction, updateTransactionActions } from './helpers';
enum TransactionType {
IN_TRANSACTION = 'in-transaction',
NOT_IN_TRANSACTION = 'not-in-transaction',
}
interface InTransaction {
type: TransactionType.IN_TRANSACTION;
actions: Action[];
}
interface NotInTransaction {
type: TransactionType.NOT_IN_TRANSACTION;
}
type TransactionState = InTransaction | NotInTransaction;
export class HistoryManager {
constructor(
private editorEngine: EditorEngine,
private undoStack: Action[] = [],
private redoStack: Action[] = [],
private inTransaction: TransactionState = { type: TransactionType.NOT_IN_TRANSACTION },
private originalStyleMap: Map<string, UpdateStyleAction> = new Map(),
) {
makeAutoObservable(this);
}
get canUndo() {
return this.undoStack.length > 0;
}
get canRedo() {
return this.redoStack.length > 0;
}
get isInTransaction() {
return this.inTransaction.type === TransactionType.IN_TRANSACTION;
}
get length() {
return this.undoStack.length;
}
startTransaction = () => {
this.inTransaction = { type: TransactionType.IN_TRANSACTION, actions: [] };
};
commitTransaction = () => {
if (
this.inTransaction.type === TransactionType.NOT_IN_TRANSACTION ||
this.inTransaction.actions.length === 0
) {
return;
}
const actionsToCommit = this.inTransaction.actions;
this.inTransaction = { type: TransactionType.NOT_IN_TRANSACTION };
for (const action of actionsToCommit) {
this.push(action);
}
};
push = (action: Action) => {
if (this.inTransaction.type === TransactionType.IN_TRANSACTION) {
this.inTransaction.actions = updateTransactionActions(
this.inTransaction.actions,
action,
);
if (action.type === 'update-style') {
const oid = action.targets[0].oid || '';
if (!this.originalStyleMap.has(oid)) {
this.originalStyleMap.set(action.targets[0].oid || '', action);
}
}
return;
}
if (this.redoStack.length > 0) {
this.redoStack = [];
}
let updatedAction = action;
if (action.type === 'update-style' && action.targets.length > 0) {
const oid = action.targets[0].oid || '';
if (this.originalStyleMap.has(oid)) {
const originalValue = this.originalStyleMap.get(oid);
updatedAction = {
...action,
targets: action.targets.map((target, idx) => {
const original = originalValue?.targets[idx]?.change.original ?? {};
return {
...target,
change: {
original,
updated: target.change.updated,
},
};
}),
};
}
}
this.undoStack.push(updatedAction);
this.editorEngine.code.write(updatedAction);
this.originalStyleMap.clear();
switch (updatedAction.type) {
case 'update-style':
sendAnalytics('style action', {
style: jsonClone(
updatedAction.targets.length > 0
? updatedAction.targets[0].change.updated
: {},
),
});
break;
case 'insert-element':
sendAnalytics('insert action');
break;
case 'move-element':
sendAnalytics('move action');
break;
case 'remove-element':
sendAnalytics('remove action');
break;
case 'edit-text':
sendAnalytics('edit text action');
}
};
undo = (): Action | null => {
if (this.inTransaction.type === TransactionType.IN_TRANSACTION) {
this.commitTransaction();
}
const top = this.undoStack.pop();
if (top == null) {
return null;
}
this.redoStack.push(top);
return undoAction(top);
};
redo = (): Action | null => {
if (this.inTransaction.type === TransactionType.IN_TRANSACTION) {
this.commitTransaction();
}
const top = this.redoStack.pop();
if (top == null) {
return null;
}
this.undoStack.push(top);
return top;
};
clear = () => {
this.undoStack = [];
this.redoStack = [];
};
}