-
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy patheditor.js
More file actions
298 lines (264 loc) · 9.66 KB
/
editor.js
File metadata and controls
298 lines (264 loc) · 9.66 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// @ts-check
/**
* The editor system for the BridgeJS Playground.
*/
export class EditorSystem {
/**
* Creates a new instance of the EditorSystem.
*/
constructor() {
this.editors = new Map();
this.config = {
input: [
{
key: 'swift',
id: 'swiftEditor',
language: 'swift',
placeholder: '',
readOnly: false,
modelUri: 'Playground.swift'
},
{
key: 'dts',
id: 'dtsEditor',
language: 'typescript',
placeholder: '',
readOnly: false,
modelUri: 'bridge-js.d.ts'
}
],
output: [
{
key: 'dts-generated',
id: 'dtsOutput',
language: 'typescript',
placeholder: '// Generated TypeScript will appear here...',
readOnly: true,
modelUri: 'Playground.d.ts'
},
{
key: 'swift-import-macros',
id: 'swiftImportMacrosOutput',
language: 'swift',
placeholder: '// Import Swift Macros will appear here...',
readOnly: true,
modelUri: 'Playground.Macros.swift'
},
{
key: 'swift-glue',
id: 'swiftGlueOutput',
language: 'swift',
placeholder: '// Swift Glue will appear here...',
readOnly: true,
modelUri: 'BridgeJS.swift'
},
{
key: 'js-generated',
id: 'jsOutput',
language: 'javascript',
placeholder: '// Generated JavaScript will appear here...',
readOnly: true,
modelUri: 'bridge-js.js'
}
]
};
this.activeTabs = {
input: this.config.input[0]?.key,
output: this.config.output[0]?.key
};
}
async init() {
await this.loadMonaco();
this.createEditors();
this.setupTabSystem();
this.setupResizeHandling();
}
async loadMonaco() {
return new Promise((resolve) => {
// @ts-ignore
require.config({ paths: { vs: 'https://unpkg.com/monaco-editor@0.45.0/min/vs' } });
// @ts-ignore
require(['vs/editor/editor.main'], resolve);
});
}
createEditors() {
const commonOptions = {
automaticLayout: true,
minimap: { enabled: false },
scrollBeyondLastLine: false,
fontSize: 14,
fontFamily: '"SF Mono", Monaco, "Cascadia Code", "Roboto Mono", Consolas, "Courier New", monospace',
lineNumbers: 'on',
roundedSelection: false,
scrollbar: { vertical: 'visible', horizontal: 'visible' },
fixedOverflowWidgets: true,
renderWhitespace: 'none',
wordWrap: 'on'
};
// Create all editors from config
[...this.config.input, ...this.config.output].forEach(config => {
const element = document.getElementById(config.id);
if (!element) {
console.warn(`Editor element not found: ${config.id}`);
return;
}
// @ts-ignore
const model = monaco.editor.createModel(
config.placeholder,
config.language,
// @ts-ignore
monaco.Uri.parse(config.modelUri)
);
// @ts-ignore
const editor = monaco.editor.create(element, {
...commonOptions,
value: config.placeholder,
language: config.language,
readOnly: config.readOnly,
model: model
});
this.editors.set(config.key, editor);
});
}
setupTabSystem() {
// Setup tab listeners
[...this.config.input, ...this.config.output].forEach(config => {
const button = document.querySelector(`[data-tab="${config.key}"]`);
if (button) {
button.addEventListener('click', () => this.switchTab(config.key));
}
});
// Initial tab state
this.updateTabStates();
}
switchTab(tabKey) {
const config = this.getConfigByKey(tabKey);
if (!config) return;
if (this.config.input.some(c => c.key === tabKey)) {
this.activeTabs.input = tabKey;
} else {
this.activeTabs.output = tabKey;
}
this.updateTabStates();
}
updateTabStates() {
// Update all tab buttons
[...this.config.input, ...this.config.output].forEach(config => {
const button = document.querySelector(`[data-tab="${config.key}"]`);
const content = document.getElementById(`${config.id}Tab`);
if (button) {
const isActive = config.key === this.activeTabs.input || config.key === this.activeTabs.output;
button.classList.toggle('active', isActive);
}
if (content) {
const isActive = config.key === this.activeTabs.input || config.key === this.activeTabs.output;
content.classList.toggle('active', isActive);
}
});
}
setupResizeHandling() {
const layoutEditor = (editor) => {
editor.layout({ width: 0, height: 0 });
window.requestAnimationFrame(() => {
const { width, height } = editor.getContainerDomNode().getBoundingClientRect();
editor.layout({ width, height });
});
};
window.addEventListener("resize", () => {
this.editors.forEach(editor => layoutEditor(editor));
});
}
// Data access
getInputs() {
return {
swift: this.editors.get('swift')?.getValue() || '',
dts: this.editors.get('dts')?.getValue() || ''
};
}
/**
* Sets the inputs for the editor system.
* @param {{swift: string, dts: string}} sampleCode - The sample code to set the inputs to.
*/
setInputs({ swift, dts }) {
this.editors.get('swift')?.setValue(swift);
this.editors.get('dts')?.setValue(dts);
}
updateOutputs(result) {
const outputMap = {
'swift-glue': () => result.swiftGlue,
'swift-import-macros': () => result.importSwiftMacroDecls,
'js-generated': () => result.outputJs,
'dts-generated': () => result.outputDts
};
Object.entries(outputMap).forEach(([key, getContent]) => {
const editor = this.editors.get(key);
if (editor) {
const content = getContent();
editor.setValue(content || `// No ${key} output generated`);
}
});
}
addChangeListeners(callback) {
this.config.input.forEach(config => {
const editor = this.editors.get(config.key);
if (editor) {
editor.onDidChangeModelContent(callback);
}
});
}
clearDiagnostics() {
// Remove all diagnostics owned by the playground.
this.editors.forEach(editor => {
const model = editor.getModel();
if (!model || typeof monaco === 'undefined') return;
monaco.editor.setModelMarkers(model, 'bridgejs', []);
});
}
/**
* @param {{file: string, startLineNumber: number, startColumn: number, endLineNumber?: number, endColumn?: number, message: string}[]} diagnostics
*/
showDiagnostics(diagnostics) {
if (typeof monaco === 'undefined') return;
// Group diagnostics per model so we can set markers in batches.
const markersByModel = new Map();
diagnostics.forEach(diag => {
const model = this.findModelForFile(diag.file);
if (!model) return;
const markers = markersByModel.get(model) ?? [];
const lineLength = model.getLineMaxColumn(diag.startLineNumber);
const endLine = diag.endLineNumber ?? diag.startLineNumber;
const endColumn = Math.min(lineLength, diag.endColumn ?? diag.startColumn + 1);
markers.push({
severity: monaco.MarkerSeverity.Error,
message: diag.message,
startLineNumber: diag.startLineNumber,
startColumn: diag.startColumn,
endLineNumber: endLine,
endColumn
});
markersByModel.set(model, markers);
});
markersByModel.forEach((markers, model) => {
monaco.editor.setModelMarkers(model, 'bridgejs', markers);
});
}
findModelForFile(fileName) {
const normalized = fileName.startsWith('/') ? fileName.slice(1) : fileName;
for (const editor of this.editors.values()) {
const model = editor.getModel();
if (!model) continue;
const uriPath = model.uri.path.startsWith('/') ? model.uri.path.slice(1) : model.uri.path;
if (uriPath === normalized || uriPath.endsWith('/' + normalized)) {
return model;
}
}
return null;
}
// Utility methods
getConfigByKey(key) {
return [...this.config.input, ...this.config.output].find(c => c.key === key);
}
getActiveTabs() {
return this.activeTabs;
}
}