-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathindex.ts
More file actions
145 lines (132 loc) · 4.38 KB
/
index.ts
File metadata and controls
145 lines (132 loc) · 4.38 KB
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
import type {
ActionElement,
ActionLocation,
ActionTarget,
Change,
GroupContainer,
ImageContentData,
IndexActionLocation,
} from '@onlook/models/actions';
import { WebviewChannels } from '@onlook/models/constants';
import { ipcRenderer } from 'electron';
import { processDom } from '../dom';
import { groupElements, ungroupElements } from '../elements/dom/group';
import { insertImage, removeImage } from '../elements/dom/image';
import { insertElement, removeElement } from '../elements/dom/insert';
import { moveElement } from '../elements/move';
import { editTextByDomId } from '../elements/text';
import cssManager from '../style';
import { listenForDomMutation } from './dom';
import {
publishEditText,
publishGroupElement,
publishInsertElement,
publishMoveElement,
publishRemoveElement,
publishStyleUpdate,
publishUngroupElement,
} from './publish';
export function listenForEvents() {
listenForWindowEvents();
listenForDomMutation();
listenForEditEvents();
}
function listenForWindowEvents() {
window.addEventListener('resize', () => {
ipcRenderer.sendToHost(WebviewChannels.WINDOW_RESIZED);
});
}
function listenForEditEvents() {
ipcRenderer.on(WebviewChannels.UPDATE_STYLE, (_, data) => {
const { domId, change } = data as {
domId: string;
change: Change<Record<string, string>>;
};
cssManager.updateStyle(domId, change.updated);
publishStyleUpdate(domId);
});
ipcRenderer.on(WebviewChannels.INSERT_ELEMENT, (_, data) => {
const { element, location, editText } = data as {
element: ActionElement;
location: ActionLocation;
editText: boolean;
};
const domEl = insertElement(element, location);
if (domEl) {
publishInsertElement(location, domEl, editText);
}
});
ipcRenderer.on(WebviewChannels.REMOVE_ELEMENT, (_, data) => {
const { location } = data as { location: ActionLocation };
removeElement(location);
publishRemoveElement(location);
});
ipcRenderer.on(WebviewChannels.MOVE_ELEMENT, (_, data) => {
const { domId, location } = data as {
domId: string;
location: IndexActionLocation;
};
const { index, targetDomId, oldParentDomId } = location;
if (oldParentDomId) {
const domEl = moveElement(domId, index, targetDomId, oldParentDomId);
if (domEl) {
publishMoveElement(domEl);
}
} else {
const domEl = moveElement(domId, index, targetDomId);
if (domEl) {
publishMoveElement(domEl);
}
}
});
ipcRenderer.on(WebviewChannels.EDIT_ELEMENT_TEXT, (_, data) => {
const { domId, content } = data as {
domId: string;
content: string;
};
const domEl = editTextByDomId(domId, content);
if (domEl) {
publishEditText(domEl);
}
});
ipcRenderer.on(WebviewChannels.GROUP_ELEMENTS, (_, data) => {
const { parent, container, children } = data as {
parent: ActionTarget;
container: GroupContainer;
children: Array<ActionTarget>;
};
const domEl = groupElements(parent, container, children);
if (domEl) {
publishGroupElement(domEl);
}
});
ipcRenderer.on(WebviewChannels.UNGROUP_ELEMENTS, (_, data) => {
const { parent, container, children } = data as {
parent: ActionTarget;
container: GroupContainer;
children: Array<ActionTarget>;
};
const parentDomEl = ungroupElements(parent, container, children);
if (parentDomEl) {
publishUngroupElement(parentDomEl);
}
});
ipcRenderer.on(WebviewChannels.INSERT_IMAGE, (_, data) => {
const { domId, image } = data as {
domId: string;
image: ImageContentData;
};
insertImage(domId, image.content);
publishStyleUpdate(domId);
});
ipcRenderer.on(WebviewChannels.REMOVE_IMAGE, (_, data) => {
const { domId } = data as {
domId: string;
};
removeImage(domId);
publishStyleUpdate(domId);
});
ipcRenderer.on(WebviewChannels.CLEAN_AFTER_WRITE_TO_CODE, () => {
processDom();
});
}