forked from onlook-dev/onlook
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcode.ts
More file actions
188 lines (167 loc) · 6.56 KB
/
code.ts
File metadata and controls
188 lines (167 loc) · 6.56 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import type { CodeDiff, CodeDiffRequest } from '@onlook/models/code';
import { MainChannels } from '@onlook/models/constants';
import type { TemplateNode } from '@onlook/models/element';
import { ipcMain } from 'electron';
import { openFileInIde, openInIde, pickDirectory, readCodeBlock, writeCode } from '../code/';
import { getTemplateNodeClass } from '../code/classes';
import { createNewComponent, deleteComponent, duplicateComponent, extractComponentsFromDirectory, renameComponent } from '../code/components';
import { getCodeDiffs } from '../code/diff';
import { isChildTextEditable } from '../code/diff/text';
import { readFile } from '../code/files';
import { getTemplateNodeChild } from '../code/templateNode';
import runManager from '../run';
import { getFileContentWithoutIds } from '../run/cleanup';
import { getTemplateNodeProps } from '../code/props';
import {
scanTailwindConfig,
updateTailwindColorConfig,
deleteTailwindColorGroup,
} from '../assets/styles';
export function listenForCodeMessages() {
ipcMain.handle(MainChannels.VIEW_SOURCE_CODE, (e: Electron.IpcMainInvokeEvent, args) => {
const oid = args as string;
const templateNode = runManager.getTemplateNode(oid);
if (!templateNode) {
console.error('Failed to get code block. No template node found.');
return;
}
openInIde(templateNode);
});
ipcMain.handle(MainChannels.VIEW_SOURCE_FILE, (e: Electron.IpcMainInvokeEvent, args) => {
const { filePath, line } = args as {
filePath: string;
line?: number;
};
openFileInIde(filePath, line);
});
ipcMain.handle(MainChannels.GET_CODE_BLOCK, (e: Electron.IpcMainInvokeEvent, args) => {
const { oid, stripIds } = args as {
oid: string;
stripIds: boolean;
};
const templateNode = runManager.getTemplateNode(oid);
if (!templateNode) {
console.error('Failed to get code block. No template node found.');
return null;
}
return readCodeBlock(templateNode, stripIds);
});
ipcMain.handle(MainChannels.GET_FILE_CONTENT, (e: Electron.IpcMainInvokeEvent, args) => {
const { filePath, stripIds } = args as {
filePath: string;
stripIds: boolean;
};
if (stripIds) {
return getFileContentWithoutIds(filePath);
}
return readFile(filePath);
});
ipcMain.handle(MainChannels.GET_TEMPLATE_NODE_CLASS, (e: Electron.IpcMainInvokeEvent, args) => {
const templateNode = args as TemplateNode;
return getTemplateNodeClass(templateNode);
});
ipcMain.handle(MainChannels.WRITE_CODE_DIFFS, async (e: Electron.IpcMainInvokeEvent, args) => {
const codeResults = args as CodeDiff[];
const res = await writeCode(codeResults);
return res;
});
ipcMain.handle(
MainChannels.GET_AND_WRITE_CODE_DIFFS,
async (e: Electron.IpcMainInvokeEvent, args) => {
const { requests, write } = args as { requests: CodeDiffRequest[]; write: boolean };
const codeDiffs = await getCodeDiffs(requests);
if (write) {
return writeCode(codeDiffs);
}
return codeDiffs;
},
);
ipcMain.handle(MainChannels.GET_TEMPLATE_NODE_CHILD, (e: Electron.IpcMainInvokeEvent, args) => {
const { parent, child, index } = args as {
parent: TemplateNode;
child: TemplateNode;
index: number;
};
return getTemplateNodeChild(parent, child, index);
});
ipcMain.handle(MainChannels.PICK_COMPONENTS_DIRECTORY, async () => {
const result = await pickDirectory();
if (result.canceled) {
return null;
}
return result.filePaths.at(0) ?? null;
});
ipcMain.handle(MainChannels.GET_COMPONENTS, async (_, args) => {
if (typeof args !== 'string') {
throw new Error('`args` must be a string');
}
const result = extractComponentsFromDirectory(args);
return result;
});
ipcMain.handle(
MainChannels.IS_CHILD_TEXT_EDITABLE,
async (e: Electron.IpcMainInvokeEvent, args) => {
const { oid } = args as { oid: string };
return isChildTextEditable(oid);
},
);
ipcMain.handle(MainChannels.GET_TEMPLATE_NODE_PROPS, (e: Electron.IpcMainInvokeEvent, args) => {
const templateNode = args as TemplateNode;
return getTemplateNodeProps(templateNode);
});
ipcMain.handle(
MainChannels.SCAN_TAILWIND_CONFIG,
async (e: Electron.IpcMainInvokeEvent, args) => {
const { projectRoot } = args as { projectRoot: string };
return scanTailwindConfig(projectRoot);
},
);
ipcMain.handle(MainChannels.UPDATE_TAILWIND_CONFIG, async (e, args) => {
const { projectRoot, originalKey, newColor, newName, parentName, theme } = args;
return updateTailwindColorConfig(
projectRoot,
originalKey,
newColor,
newName,
theme,
parentName,
);
});
ipcMain.handle(MainChannels.DELETE_TAILWIND_CONFIG, async (_, args) => {
const { projectRoot, groupName, colorName } = args;
return deleteTailwindColorGroup(projectRoot, groupName, colorName);
});
ipcMain.handle(MainChannels.DUPLICATE_COMPONENT, async (_, args) => {
const { filePath, componentName } = args as {
filePath: string;
componentName: string
};
const result = duplicateComponent(filePath, componentName);
return result;
});
ipcMain.handle(MainChannels.RENAME_COMPONENT, async (_, args) => {
const { newComponentName , filePath } = args as {
newComponentName: string
filePath: string;
};
const result = renameComponent(newComponentName , filePath);
return result;
});
ipcMain.handle(MainChannels.CREATE_COMPONENT, async (_, args) => {
const { componentName , oid } = args as {
componentName: string
filePath: string;
oid: string;
};
const templateNode = await runManager.getTemplateNode(oid);
const result = createNewComponent(componentName, templateNode?.path || "");
return result;
});
ipcMain.handle(MainChannels.DELETE_COMPONENT, async (_, args) => {
const { filePath } = args as {
filePath: string;
};
const result = deleteComponent(filePath);
return result;
});
}