|
1 | 1 | import { sendAnalytics } from '@/lib/utils';
|
2 |
| -import type { Action } from '@onlook/models/actions'; |
| 2 | +import type { Action, UpdateStyleAction } from '@onlook/models/actions'; |
3 | 3 | import { jsonClone } from '@onlook/utility';
|
4 | 4 | import { makeAutoObservable } from 'mobx';
|
5 | 5 | import type { EditorEngine } from '..';
|
@@ -27,6 +27,7 @@ export class HistoryManager {
|
27 | 27 | private undoStack: Action[] = [],
|
28 | 28 | private redoStack: Action[] = [],
|
29 | 29 | private inTransaction: TransactionState = { type: TransactionType.NOT_IN_TRANSACTION },
|
| 30 | + private originalStyleMap: Map<string, UpdateStyleAction> = new Map(), |
30 | 31 | ) {
|
31 | 32 | makeAutoObservable(this);
|
32 | 33 | }
|
@@ -72,21 +73,54 @@ export class HistoryManager {
|
72 | 73 | this.inTransaction.actions,
|
73 | 74 | action,
|
74 | 75 | );
|
| 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 | + } |
75 | 82 | return;
|
76 | 83 | }
|
77 | 84 |
|
78 | 85 | if (this.redoStack.length > 0) {
|
79 | 86 | this.redoStack = [];
|
80 | 87 | }
|
81 | 88 |
|
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(); |
84 | 116 |
|
85 |
| - switch (action.type) { |
| 117 | + switch (updatedAction.type) { |
86 | 118 | case 'update-style':
|
87 | 119 | sendAnalytics('style action', {
|
88 | 120 | style: jsonClone(
|
89 |
| - action.targets.length > 0 ? action.targets[0].change.updated : {}, |
| 121 | + updatedAction.targets.length > 0 |
| 122 | + ? updatedAction.targets[0].change.updated |
| 123 | + : {}, |
90 | 124 | ),
|
91 | 125 | });
|
92 | 126 | break;
|
|
0 commit comments