modern-monaco 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/core.js CHANGED
@@ -83,75 +83,75 @@ var editorProps = [
83
83
  var errors = {
84
84
  NotFound: ErrorNotFound
85
85
  };
86
- var builtinLSPProviders = {};
87
- var builtinSyntaxes = [];
86
+ var syntaxes = [];
87
+ var lSPProviders = {};
88
+ var { promise: editorWorkerPromise, resolve: onDidEditorWorkerResolve } = promiseWithResolvers();
89
+ var attr = (el, name) => el.getAttribute(name);
90
+ var style = (el, style2) => Object.assign(el.style, style2);
88
91
  async function init(options) {
89
- const langs = (options?.langs ?? []).concat(builtinSyntaxes);
92
+ const langs = (options?.langs ?? []).concat(syntaxes);
90
93
  const hightlighter = await initShiki({ ...options, langs });
91
94
  return loadMonaco(hightlighter, options?.workspace, options?.lsp);
92
95
  }
93
- async function lazy(options, hydrate2) {
94
- const { promise: editorWorkerPromise, resolve: onDidEditorWorkerResolve } = promiseWithResolvers();
95
- const getAttr = (el, name) => el.getAttribute(name);
96
- const setStyle = (el, style) => Object.assign(el.style, style);
97
- let monacoPromise = null;
98
- customElements.define(
99
- "monaco-editor",
100
- class extends HTMLElement {
101
- async connectedCallback() {
102
- const workspace = options?.workspace;
103
- const renderOptions = {};
104
- for (const attrName of this.getAttributeNames()) {
105
- const key = editorProps.find((k) => k.toLowerCase() === attrName);
106
- if (key) {
107
- let value = getAttr(this, attrName);
108
- if (value === "") {
109
- value = key === "minimap" || key === "stickyScroll" ? { enabled: true } : true;
110
- } else {
111
- value = value.trim();
112
- if (value === "true") {
113
- value = true;
114
- } else if (value === "false") {
115
- value = false;
116
- } else if (value === "null") {
117
- value = null;
118
- } else if (/^\d+$/.test(value)) {
119
- value = Number(value);
120
- } else if (/^\{.+\}$/.test(value)) {
121
- try {
122
- value = JSON.parse(value);
123
- } catch (error) {
124
- value = void 0;
96
+ async function lazy(options) {
97
+ if (!customElements.get("monaco-editor")) {
98
+ let monacoPromise = null;
99
+ customElements.define(
100
+ "monaco-editor",
101
+ class extends HTMLElement {
102
+ async connectedCallback() {
103
+ const workspace = options?.workspace;
104
+ const renderOptions = {};
105
+ for (const attrName of this.getAttributeNames()) {
106
+ const key = editorProps.find((k) => k.toLowerCase() === attrName);
107
+ if (key) {
108
+ let value = attr(this, attrName);
109
+ if (value === "") {
110
+ value = key === "minimap" || key === "stickyScroll" ? { enabled: true } : true;
111
+ } else {
112
+ value = value.trim();
113
+ if (value === "true") {
114
+ value = true;
115
+ } else if (value === "false") {
116
+ value = false;
117
+ } else if (value === "null") {
118
+ value = null;
119
+ } else if (/^\d+$/.test(value)) {
120
+ value = Number(value);
121
+ } else if (/^\{.+\}$/.test(value)) {
122
+ try {
123
+ value = JSON.parse(value);
124
+ } catch (error) {
125
+ value = void 0;
126
+ }
125
127
  }
126
128
  }
127
- }
128
- if (key === "padding") {
129
- if (typeof value === "number") {
130
- value = { top: value, bottom: value };
131
- } else if (/^\d+\s+\d+$/.test(value)) {
132
- const [top, bottom] = value.split(/\s+/);
133
- if (top && bottom) {
134
- value = { top: Number(top), bottom: Number(bottom) };
129
+ if (key === "padding") {
130
+ if (typeof value === "number") {
131
+ value = { top: value, bottom: value };
132
+ } else if (/^\d+\s+\d+$/.test(value)) {
133
+ const [top, bottom] = value.split(/\s+/);
134
+ if (top && bottom) {
135
+ value = { top: Number(top), bottom: Number(bottom) };
136
+ }
137
+ } else {
138
+ value = void 0;
135
139
  }
136
- } else {
137
- value = void 0;
138
140
  }
139
- }
140
- if (key === "wordWrap" && (value === "on" || value === true)) {
141
- value = "on";
142
- }
143
- if (value !== void 0) {
144
- renderOptions[key] = value;
141
+ if (key === "wordWrap" && (value === "on" || value === true)) {
142
+ value = "on";
143
+ }
144
+ if (value !== void 0) {
145
+ renderOptions[key] = value;
146
+ }
145
147
  }
146
148
  }
147
- }
148
- let filename;
149
- let code;
150
- if (hydrate2) {
151
- const optionsEl = this.children[0];
152
- if (optionsEl && optionsEl.tagName === "SCRIPT" && optionsEl.className === "monaco-editor-options") {
149
+ let filename;
150
+ let code;
151
+ const firstEl = this.firstElementChild;
152
+ if (firstEl && firstEl.tagName === "SCRIPT" && firstEl.className === "monaco-editor-options") {
153
153
  try {
154
- const v = JSON.parse(optionsEl.textContent);
154
+ const v = JSON.parse(firstEl.textContent);
155
155
  if (Array.isArray(v) && v.length === 2) {
156
156
  const [input, opts] = v;
157
157
  Object.assign(renderOptions, opts);
@@ -167,164 +167,176 @@ async function lazy(options, hydrate2) {
167
167
  }
168
168
  } catch {
169
169
  }
170
- optionsEl.remove();
171
- }
172
- }
173
- setStyle(this, { display: "block", position: "relative" });
174
- let widthAttr = getAttr(this, "width");
175
- let heightAttr = getAttr(this, "height");
176
- if (isDigital(widthAttr) && isDigital(heightAttr)) {
177
- const width = Number(widthAttr);
178
- const height = Number(heightAttr);
179
- setStyle(this, { width: width + "px", height: height + "px" });
180
- renderOptions.dimension = { width, height };
181
- } else {
182
- if (isDigital(widthAttr)) {
183
- widthAttr += "px";
170
+ firstEl.remove();
184
171
  }
185
- if (isDigital(heightAttr)) {
186
- heightAttr += "px";
187
- }
188
- this.style.width ||= widthAttr ?? "100%";
189
- this.style.height ||= heightAttr ?? "100%";
190
- }
191
- const containerEl = document.createElement("div");
192
- containerEl.className = "monaco-editor-container";
193
- setStyle(containerEl, { width: "100%", height: "100%" });
194
- this.appendChild(containerEl);
195
- if (!filename && workspace) {
196
- if (workspace.history.state.current) {
197
- filename = workspace.history.state.current;
198
- } else if (workspace.entryFile) {
199
- filename = workspace.entryFile;
200
- workspace.history.replace(filename);
172
+ style(this, { display: "block", position: "relative" });
173
+ let widthAttr = attr(this, "width");
174
+ let heightAttr = attr(this, "height");
175
+ if (isDigital(widthAttr) && isDigital(heightAttr)) {
176
+ const width = Number(widthAttr);
177
+ const height = Number(heightAttr);
178
+ style(this, { width: width + "px", height: height + "px" });
179
+ renderOptions.dimension = { width, height };
201
180
  } else {
202
- const rootFiles = (await workspace.fs.readDirectory("/")).filter(([name, type]) => type === 1).map(([name]) => name);
203
- filename = rootFiles.includes("index.html") ? "index.html" : rootFiles[0];
204
- if (filename) {
205
- workspace.history.replace(filename);
181
+ if (isDigital(widthAttr)) {
182
+ widthAttr += "px";
206
183
  }
184
+ if (isDigital(heightAttr)) {
185
+ heightAttr += "px";
186
+ }
187
+ this.style.width ||= widthAttr ?? "100%";
188
+ this.style.height ||= heightAttr ?? "100%";
207
189
  }
208
- }
209
- const langs = (options?.langs ?? []).concat(builtinSyntaxes);
210
- if (renderOptions.language || filename) {
211
- const lang = renderOptions.language ?? getLanguageIdFromPath(filename) ?? "plaintext";
212
- if (!builtinSyntaxes.find((s) => s.name === lang)) {
213
- langs.push(lang);
214
- }
215
- }
216
- if (renderOptions.theme) {
217
- renderOptions.theme = renderOptions.theme.toLowerCase().replace(/ +/g, "-");
218
- }
219
- const highlighter = await initShiki({
220
- ...options,
221
- theme: renderOptions.theme ?? options?.theme,
222
- langs
223
- });
224
- let prerenderEl = hydrate2 ? this.querySelector(".monaco-editor-prerender") : void 0;
225
- if (!prerenderEl && filename && workspace) {
226
- try {
227
- const code2 = await workspace.fs.readFile(filename);
228
- const language = getLanguageIdFromPath(filename);
229
- prerenderEl = containerEl.cloneNode(true);
230
- prerenderEl.className = "monaco-editor-prerender";
231
- prerenderEl.innerHTML = render(highlighter, decode(code2), { ...renderOptions, language });
232
- } catch (error) {
233
- if (error instanceof ErrorNotFound) {
190
+ const containerEl = document.createElement("div");
191
+ containerEl.className = "monaco-editor-container";
192
+ style(containerEl, { width: "100%", height: "100%" });
193
+ this.appendChild(containerEl);
194
+ if (!filename && workspace) {
195
+ if (workspace.history.state.current) {
196
+ filename = workspace.history.state.current;
197
+ } else if (workspace.entryFile) {
198
+ filename = workspace.entryFile;
199
+ workspace.history.replace(filename);
234
200
  } else {
235
- throw error;
201
+ const rootFiles = (await workspace.fs.readDirectory("/")).filter(([name, type]) => type === 1).map(([name]) => name);
202
+ filename = rootFiles.includes("index.html") ? "index.html" : rootFiles[0];
203
+ if (filename) {
204
+ workspace.history.replace(filename);
205
+ }
236
206
  }
237
207
  }
238
- }
239
- if (prerenderEl) {
240
- setStyle(prerenderEl, { position: "absolute", top: "0", left: "0" });
241
- this.appendChild(prerenderEl);
242
- if (filename && workspace) {
243
- const viewState = await workspace.viewState.get(filename);
244
- const scrollTop = viewState?.viewState.scrollTop ?? 0;
245
- if (scrollTop) {
246
- const mockEl = prerenderEl.querySelector(".mock-monaco-editor");
247
- if (mockEl) {
248
- mockEl.scrollTop = scrollTop;
249
- }
208
+ const langs = (options?.langs ?? []).concat(syntaxes);
209
+ if (renderOptions.language || filename) {
210
+ const lang = renderOptions.language ?? getLanguageIdFromPath(filename) ?? "plaintext";
211
+ if (!syntaxes.find((s) => s.name === lang)) {
212
+ langs.push(lang);
250
213
  }
251
214
  }
252
- }
253
- async function createEditor() {
254
- const monaco = await (monacoPromise ?? (monacoPromise = loadMonaco(highlighter, workspace, options?.lsp, onDidEditorWorkerResolve)));
255
- const editor = monaco.editor.create(containerEl, renderOptions);
256
- if (workspace) {
257
- const storeViewState = () => {
258
- const currentModel = editor.getModel();
259
- if (currentModel?.uri.scheme === "file") {
260
- const state = editor.saveViewState();
261
- if (state) {
262
- state.viewState.scrollTop ??= editor.getScrollTop();
263
- workspace.viewState.save(currentModel.uri.toString(), Object.freeze(state));
264
- }
265
- }
266
- };
267
- editor.onDidChangeCursorSelection(debunce(storeViewState, 500));
268
- editor.onDidScrollChange(debunce(storeViewState, 500));
269
- workspace.history.onChange((state) => {
270
- if (editor.getModel()?.uri.toString() !== state.current) {
271
- workspace._openTextDocument(state.current, editor);
272
- }
273
- });
215
+ if (renderOptions.theme) {
216
+ renderOptions.theme = renderOptions.theme.toLowerCase().replace(/ +/g, "-");
274
217
  }
275
- if (filename && workspace) {
218
+ const highlighter = await initShiki({
219
+ ...options,
220
+ theme: renderOptions.theme ?? options?.theme,
221
+ langs
222
+ });
223
+ let prerenderEl;
224
+ for (const el of this.children) {
225
+ if (el.className === "monaco-editor-prerender") {
226
+ prerenderEl = el;
227
+ break;
228
+ }
229
+ }
230
+ if (!prerenderEl && filename && workspace) {
276
231
  try {
277
- const model = await workspace._openTextDocument(filename, editor);
278
- if (code && code !== model.getValue()) {
279
- model.setValue(code);
280
- }
232
+ const code2 = await workspace.fs.readFile(filename);
233
+ const language = getLanguageIdFromPath(filename);
234
+ prerenderEl = containerEl.cloneNode(true);
235
+ prerenderEl.className = "monaco-editor-prerender";
236
+ prerenderEl.innerHTML = render(highlighter, decode(code2), { ...renderOptions, language });
281
237
  } catch (error) {
282
238
  if (error instanceof ErrorNotFound) {
283
- if (code) {
284
- const dirname = filename.split("/").slice(0, -1).join("/");
285
- if (dirname) {
286
- await workspace.fs.createDirectory(dirname);
287
- }
288
- await workspace.fs.writeFile(filename, code);
289
- workspace._openTextDocument(filename, editor);
290
- } else {
291
- editor.setModel(monaco.editor.createModel(""));
292
- }
293
239
  } else {
294
240
  throw error;
295
241
  }
296
242
  }
297
- } else if (code && (renderOptions.language || filename)) {
298
- const model = monaco.editor.createModel(code, renderOptions.language, filename);
299
- editor.setModel(model);
300
- } else {
301
- editor.setModel(monaco.editor.createModel(""));
302
243
  }
303
244
  if (prerenderEl) {
304
- editorWorkerPromise.then(() => {
305
- setTimeout(() => {
306
- const animate = prerenderEl.animate?.([{ opacity: 1 }, { opacity: 0 }], { duration: 150 });
307
- if (animate) {
308
- animate.finished.then(() => prerenderEl.remove());
245
+ style(prerenderEl, { position: "absolute", top: "0", left: "0" });
246
+ this.appendChild(prerenderEl);
247
+ if (filename && workspace) {
248
+ const viewState = await workspace.viewState.get(filename);
249
+ const scrollTop = viewState?.viewState.scrollTop ?? 0;
250
+ if (scrollTop) {
251
+ const mockEl = prerenderEl.querySelector(".mock-monaco-editor");
252
+ if (mockEl) {
253
+ mockEl.scrollTop = scrollTop;
254
+ }
255
+ }
256
+ }
257
+ }
258
+ async function createEditor() {
259
+ const monaco = await (monacoPromise ?? (monacoPromise = loadMonaco(highlighter, workspace, options?.lsp, onDidEditorWorkerResolve)));
260
+ const editor = monaco.editor.create(containerEl, renderOptions);
261
+ if (workspace) {
262
+ const storeViewState = () => {
263
+ const currentModel = editor.getModel();
264
+ if (currentModel?.uri.scheme === "file") {
265
+ const state = editor.saveViewState();
266
+ if (state) {
267
+ state.viewState.scrollTop ??= editor.getScrollTop();
268
+ workspace.viewState.save(currentModel.uri.toString(), Object.freeze(state));
269
+ }
270
+ }
271
+ };
272
+ editor.onDidChangeCursorSelection(debunce(storeViewState, 500));
273
+ editor.onDidScrollChange(debunce(storeViewState, 500));
274
+ workspace.history.onChange((state) => {
275
+ if (editor.getModel()?.uri.toString() !== state.current) {
276
+ workspace._openTextDocument(state.current, editor);
277
+ }
278
+ });
279
+ }
280
+ if (filename && workspace) {
281
+ try {
282
+ const model = await workspace._openTextDocument(filename, editor);
283
+ if (code && code !== model.getValue()) {
284
+ model.setValue(code);
285
+ }
286
+ } catch (error) {
287
+ if (error instanceof ErrorNotFound) {
288
+ if (code) {
289
+ const dirname = filename.split("/").slice(0, -1).join("/");
290
+ if (dirname) {
291
+ await workspace.fs.createDirectory(dirname);
292
+ }
293
+ await workspace.fs.writeFile(filename, code);
294
+ workspace._openTextDocument(filename, editor);
295
+ } else {
296
+ editor.setModel(monaco.editor.createModel(""));
297
+ }
309
298
  } else {
310
- setTimeout(() => prerenderEl.remove(), 150);
299
+ throw error;
311
300
  }
312
- }, 100);
313
- });
301
+ }
302
+ } else if (code && (renderOptions.language || filename)) {
303
+ const modelUri = filename ? monaco.Uri.file(filename) : void 0;
304
+ let model = modelUri ? monaco.editor.getModel(modelUri) : null;
305
+ if (!model) {
306
+ model = monaco.editor.createModel(code, renderOptions.language, modelUri);
307
+ } else if (code !== model.getValue()) {
308
+ model.setValue(code);
309
+ }
310
+ editor.setModel(model);
311
+ } else {
312
+ editor.setModel(monaco.editor.createModel(""));
313
+ }
314
+ if (prerenderEl) {
315
+ editorWorkerPromise.then(() => {
316
+ setTimeout(() => {
317
+ const animate = prerenderEl.animate?.([{ opacity: 1 }, { opacity: 0 }], { duration: 150 });
318
+ if (animate) {
319
+ animate.finished.then(() => prerenderEl.remove());
320
+ } else {
321
+ setTimeout(() => prerenderEl.remove(), 150);
322
+ }
323
+ }, 100);
324
+ });
325
+ }
314
326
  }
327
+ await createEditor();
315
328
  }
316
- await createEditor();
317
329
  }
318
- }
319
- );
330
+ );
331
+ }
320
332
  await editorWorkerPromise;
321
333
  }
322
334
  function hydrate(options) {
323
- return lazy(options, true);
335
+ return lazy(options);
324
336
  }
325
- async function loadMonaco(highlighter, workspace, lsp, onDidEditorWorkerResolve) {
337
+ async function loadMonaco(highlighter, workspace, lsp, onDidEditorWorkerResolve2) {
326
338
  const monaco = await import("./editor-core.js");
327
- const lspProviders = { ...builtinLSPProviders, ...lsp?.providers };
339
+ const lspProviders = { ...lSPProviders, ...lsp?.providers };
328
340
  workspace?.setupMonaco(monaco);
329
341
  if (Object.keys(lspProviders).length > 0) {
330
342
  initLS(monaco);
@@ -352,7 +364,7 @@ async function loadMonaco(highlighter, workspace, lsp, onDidEditorWorkerResolve)
352
364
  const worker = createWebWorker(url, void 0);
353
365
  if (!provider) {
354
366
  const onMessage = (e) => {
355
- onDidEditorWorkerResolve?.();
367
+ onDidEditorWorkerResolve2?.();
356
368
  worker.removeEventListener("message", onMessage);
357
369
  };
358
370
  worker.addEventListener("message", onMessage);
@@ -447,17 +459,17 @@ async function loadMonaco(highlighter, workspace, lsp, onDidEditorWorkerResolve)
447
459
  initShikiMonacoTokenizer(monaco, highlighter);
448
460
  return monaco;
449
461
  }
450
- function registerLSPProvider(lang, provider) {
451
- builtinLSPProviders[lang] = provider;
452
- }
453
- function registerSyntax(...syntaxes) {
454
- builtinSyntaxes.push(...syntaxes);
462
+ function registerSyntax(...syntaxes2) {
463
+ syntaxes2.push(...syntaxes2);
455
464
  }
456
465
  function registerTheme(theme) {
457
466
  if (theme.name) {
458
467
  themes.set(theme.name, theme);
459
468
  }
460
469
  }
470
+ function registerLSPProvider(lang, provider) {
471
+ lSPProviders[lang] = provider;
472
+ }
461
473
  setDefaultWasmLoader(getWasmInstance);
462
474
  export {
463
475
  Workspace,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modern-monaco",
3
3
  "description": "A modern version of Monaco Editor",
4
- "version": "0.1.7",
4
+ "version": "0.1.8",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",
package/types/core.d.ts CHANGED
@@ -1,4 +1,7 @@
1
- export function registerLSPProvider(lang: string, provider: import("./lsp.d.ts").LSPProvider): void;
2
- export function registerSyntax(...syntaxes: import("./index.d.ts").TextmateGrammar[]): void;
3
- export function registerTheme(theme: import("./index.d.ts").TextmateTheme): void;
1
+ import type { TextmateGrammar, TextmateTheme } from "./index.d.ts";
2
+ import type { LSPProvider } from "./lsp.d.ts";
3
+
4
+ export function registerSyntax(...syntaxes: TextmateGrammar[]): void;
5
+ export function registerTheme(theme: TextmateTheme): void;
6
+ export function registerLSPProvider(lang: string, provider: LSPProvider): void;
4
7
  export * from "./index";