forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.js
More file actions
241 lines (216 loc) · 7.54 KB
/
editor.js
File metadata and controls
241 lines (216 loc) · 7.54 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
// @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: 'import-glue',
id: 'importGlueOutput',
language: 'swift',
placeholder: '// Import Swift Glue will appear here...',
readOnly: true,
modelUri: 'ImportTS.swift'
},
{
key: 'export-glue',
id: 'exportGlueOutput',
language: 'swift',
placeholder: '// Export Swift Glue will appear here...',
readOnly: true,
modelUri: 'ExportTS.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/[email protected]/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 = {
'import-glue': () => result.importSwiftGlue(),
'export-glue': () => result.exportSwiftGlue(),
'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);
}
});
}
// Utility methods
getConfigByKey(key) {
return [...this.config.input, ...this.config.output].find(c => c.key === key);
}
getActiveTabs() {
return this.activeTabs;
}
}