Skip to content

Commit 5312ca0

Browse files
committed
fix: undo color change functionality for elements not working
1 parent ad24e03 commit 5312ca0

File tree

1 file changed

+39
-5
lines changed
  • apps/studio/src/lib/editor/engine/history

1 file changed

+39
-5
lines changed

apps/studio/src/lib/editor/engine/history/index.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { sendAnalytics } from '@/lib/utils';
2-
import type { Action } from '@onlook/models/actions';
2+
import type { Action, UpdateStyleAction } from '@onlook/models/actions';
33
import { jsonClone } from '@onlook/utility';
44
import { makeAutoObservable } from 'mobx';
55
import type { EditorEngine } from '..';
@@ -27,6 +27,7 @@ export class HistoryManager {
2727
private undoStack: Action[] = [],
2828
private redoStack: Action[] = [],
2929
private inTransaction: TransactionState = { type: TransactionType.NOT_IN_TRANSACTION },
30+
private originalStyleMap: Map<string, UpdateStyleAction> = new Map(),
3031
) {
3132
makeAutoObservable(this);
3233
}
@@ -72,21 +73,54 @@ export class HistoryManager {
7273
this.inTransaction.actions,
7374
action,
7475
);
76+
if (action.type === 'update-style') {
77+
const oid = action.targets[0].oid || '';
78+
if (!this.originalStyleMap.has(oid)) {
79+
this.originalStyleMap.set(action.targets[0].oid || '', action);
80+
}
81+
}
7582
return;
7683
}
7784

7885
if (this.redoStack.length > 0) {
7986
this.redoStack = [];
8087
}
8188

82-
this.undoStack.push(action);
83-
this.editorEngine.code.write(action);
89+
let updatedAction = action;
90+
91+
if (action.type === 'update-style' && action.targets.length > 0) {
92+
const oid = action.targets[0].oid || '';
93+
if (this.originalStyleMap.has(oid)) {
94+
const originalValue = this.originalStyleMap.get(oid);
95+
96+
updatedAction = {
97+
...action,
98+
targets: action.targets.map((target, idx) => {
99+
const original = originalValue?.targets[idx]?.change.original ?? {};
100+
return {
101+
...target,
102+
change: {
103+
original,
104+
updated: target.change.updated,
105+
},
106+
};
107+
}),
108+
};
109+
}
110+
}
111+
112+
this.undoStack.push(updatedAction);
113+
this.editorEngine.code.write(updatedAction);
114+
115+
this.originalStyleMap.clear();
84116

85-
switch (action.type) {
117+
switch (updatedAction.type) {
86118
case 'update-style':
87119
sendAnalytics('style action', {
88120
style: jsonClone(
89-
action.targets.length > 0 ? action.targets[0].change.updated : {},
121+
updatedAction.targets.length > 0
122+
? updatedAction.targets[0].change.updated
123+
: {},
90124
),
91125
});
92126
break;

0 commit comments

Comments
 (0)