modern-monaco 0.2.2 → 0.3.0

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/README.md CHANGED
@@ -249,7 +249,7 @@ lazy({
249
249
  },
250
250
  ],
251
251
  // The CDN for loading language grammars and themes. Default is "https://esm.sh"
252
- tmDownloadCDN: "https://esm.sh",
252
+ cdn: "https://esm.sh",
253
253
  });
254
254
  ```
255
255
 
package/dist/core.mjs CHANGED
@@ -40,15 +40,15 @@ function isObject(v) {
40
40
  }
41
41
 
42
42
  // package.json
43
- var version = "0.2.2";
43
+ var version = "0.3.0";
44
44
 
45
45
  // src/core.ts
46
46
  import { getExtnameFromLanguageId, getLanguageIdFromPath, grammars, initShiki, setDefaultWasmLoader, themes } from "./shiki.mjs";
47
47
  import { initShikiMonacoTokenizer, registerShikiMonacoTokenizer } from "./shiki.mjs";
48
48
  import { render } from "./shiki.mjs";
49
49
  import { getWasmInstance } from "./shiki-wasm.mjs";
50
- import { ErrorNotFound, Workspace } from "./workspace.mjs";
51
- import { debunce, decode, isDigital } from "./util.mjs";
50
+ import { NotFoundError, Workspace } from "./workspace.mjs";
51
+ import { debunce, decode, getCDNUrl, isDigital } from "./util.mjs";
52
52
  import { init as initLspClient } from "./lsp/client.mjs";
53
53
  var editorProps = [
54
54
  "autoDetectHighContrast",
@@ -82,19 +82,18 @@ var editorProps = [
82
82
  "wordWrap"
83
83
  ];
84
84
  var errors = {
85
- NotFound: ErrorNotFound
85
+ NotFound: NotFoundError
86
86
  };
87
- var cdnUrl = `https://esm.sh/modern-monaco@${version}`;
88
87
  var syntaxes = [];
89
88
  var lspProviders = {};
90
- var attr = (el, name) => el.getAttribute(name);
91
- var style = (el, style2) => Object.assign(el.style, style2);
89
+ var getAttr = (el, name) => el.getAttribute(name);
90
+ var setStyle = (el, style) => Object.assign(el.style, style);
92
91
  async function init(options) {
93
92
  const langs = (options?.langs ?? []).concat(syntaxes);
94
- const hightlighter = await initShiki({ ...options, langs });
95
- return loadMonaco(hightlighter, options?.workspace, options?.lsp);
93
+ const shiki = await initShiki({ ...options, langs });
94
+ return loadMonaco(shiki, options?.workspace, options?.lsp, options?.cdn);
96
95
  }
97
- async function lazy(options) {
96
+ function lazy(options) {
98
97
  if (!customElements.get("monaco-editor")) {
99
98
  let monacoPromise = null;
100
99
  customElements.define(
@@ -106,7 +105,7 @@ async function lazy(options) {
106
105
  for (const attrName of this.getAttributeNames()) {
107
106
  const key = editorProps.find((k) => k.toLowerCase() === attrName);
108
107
  if (key) {
109
- let value = attr(this, attrName);
108
+ let value = getAttr(this, attrName);
110
109
  if (value === "") {
111
110
  value = key === "minimap" || key === "stickyScroll" ? { enabled: true } : true;
112
111
  } else {
@@ -170,13 +169,13 @@ async function lazy(options) {
170
169
  }
171
170
  firstEl.remove();
172
171
  }
173
- style(this, { display: "block", position: "relative" });
174
- let widthAttr = attr(this, "width");
175
- let heightAttr = attr(this, "height");
172
+ setStyle(this, { display: "block", position: "relative" });
173
+ let widthAttr = getAttr(this, "width");
174
+ let heightAttr = getAttr(this, "height");
176
175
  if (isDigital(widthAttr) && isDigital(heightAttr)) {
177
176
  const width = Number(widthAttr);
178
177
  const height = Number(heightAttr);
179
- style(this, { width: width + "px", height: height + "px" });
178
+ setStyle(this, { width: width + "px", height: height + "px" });
180
179
  renderOptions.dimension = { width, height };
181
180
  } else {
182
181
  if (isDigital(widthAttr)) {
@@ -190,7 +189,7 @@ async function lazy(options) {
190
189
  }
191
190
  const containerEl = document.createElement("div");
192
191
  containerEl.className = "monaco-editor-container";
193
- style(containerEl, { width: "100%", height: "100%" });
192
+ setStyle(containerEl, { width: "100%", height: "100%" });
194
193
  this.appendChild(containerEl);
195
194
  if (!filename && workspace) {
196
195
  if (workspace.history.state.current) {
@@ -213,14 +212,12 @@ async function lazy(options) {
213
212
  langs.push(lang);
214
213
  }
215
214
  }
216
- if (renderOptions.theme) {
217
- renderOptions.theme = renderOptions.theme.toLowerCase().replace(/ +/g, "-");
215
+ let theme = options?.theme ?? renderOptions.theme;
216
+ if (typeof theme === "string") {
217
+ theme = theme.toLowerCase().replace(/ +/g, "-");
218
218
  }
219
- const highlighter = await initShiki({
220
- ...options,
221
- theme: renderOptions.theme ?? options?.theme,
222
- langs
223
- });
219
+ const highlighter = await initShiki({ ...options, theme, langs });
220
+ renderOptions.theme = highlighter.getLoadedThemes()[0];
224
221
  let prerenderEl;
225
222
  for (const el of this.children) {
226
223
  if (el.className === "monaco-editor-prerender") {
@@ -236,14 +233,14 @@ async function lazy(options) {
236
233
  prerenderEl.className = "monaco-editor-prerender";
237
234
  prerenderEl.innerHTML = render(highlighter, decode(code2), { ...renderOptions, language });
238
235
  } catch (error) {
239
- if (error instanceof ErrorNotFound) {
236
+ if (error instanceof NotFoundError) {
240
237
  } else {
241
238
  throw error;
242
239
  }
243
240
  }
244
241
  }
245
242
  if (prerenderEl) {
246
- style(prerenderEl, { position: "absolute", top: "0", left: "0" });
243
+ setStyle(prerenderEl, { position: "absolute", top: "0", left: "0" });
247
244
  this.appendChild(prerenderEl);
248
245
  if (filename && workspace) {
249
246
  const viewState = await workspace.viewState.get(filename);
@@ -257,7 +254,7 @@ async function lazy(options) {
257
254
  }
258
255
  }
259
256
  {
260
- const monaco = await (monacoPromise ?? (monacoPromise = loadMonaco(highlighter, workspace, options?.lsp)));
257
+ const monaco = await (monacoPromise ?? (monacoPromise = loadMonaco(highlighter, workspace, options?.lsp, options?.cdn)));
261
258
  const editor = monaco.editor.create(containerEl, renderOptions);
262
259
  if (workspace) {
263
260
  const storeViewState = () => {
@@ -285,7 +282,7 @@ async function lazy(options) {
285
282
  model.setValue(code);
286
283
  }
287
284
  } catch (error) {
288
- if (error instanceof ErrorNotFound) {
285
+ if (error instanceof NotFoundError) {
289
286
  if (code) {
290
287
  const dirname = filename.split("/").slice(0, -1).join("/");
291
288
  if (dirname) {
@@ -331,13 +328,13 @@ async function lazy(options) {
331
328
  function hydrate(options) {
332
329
  return lazy(options);
333
330
  }
334
- async function loadMonaco(highlighter, workspace, lsp) {
335
- let importmap = null;
336
- let editorCoreModuleUrl = `${cdnUrl}/es2022/editor-core.mjs`;
337
- let lspModuleUrl = `${cdnUrl}/es2022/lsp.mjs`;
338
- if (importmap = document.querySelector("script[type='importmap']")) {
331
+ async function loadMonaco(highlighter, workspace, lsp, cdn) {
332
+ let editorCoreModuleUrl = getCDNUrl(`/modern-monaco@${version}/dist/editor-core.mjs`, cdn);
333
+ let lspModuleUrl = getCDNUrl(`/modern-monaco@${version}/dist/lsp/index.mjs`, cdn);
334
+ let importmapEl = null;
335
+ if (importmapEl = document.querySelector("script[type='importmap']")) {
339
336
  try {
340
- const { imports = {} } = parseImportMapFromJson(importmap.textContent);
337
+ const { imports = {} } = parseImportMapFromJson(importmapEl.textContent);
341
338
  if (imports["modern-monaco/editor-core"]) {
342
339
  editorCoreModuleUrl = imports["modern-monaco/editor-core"];
343
340
  }
@@ -347,14 +344,14 @@ async function loadMonaco(highlighter, workspace, lsp) {
347
344
  } catch (error) {
348
345
  }
349
346
  }
350
- const builtinLSP = globalThis.MonacoEnvironment?.builtinLSP;
347
+ const useBuiltinLSP = globalThis.MonacoEnvironment?.useBuiltinLSP;
351
348
  const [monaco, { builtinLSPProviders }] = await Promise.all([
352
349
  import(editorCoreModuleUrl),
353
- builtinLSP ? import(lspModuleUrl) : Promise.resolve({ builtinLSPProviders: {} })
350
+ useBuiltinLSP ? import(lspModuleUrl) : Promise.resolve({ builtinLSPProviders: {} })
354
351
  ]);
355
- const lspProviderMap = { ...builtinLSPProviders, ...lspProviders, ...lsp?.providers };
352
+ const allLspProviders = { ...builtinLSPProviders, ...lspProviders, ...lsp?.providers };
356
353
  workspace?.setupMonaco(monaco);
357
- if (Object.keys(lspProviderMap).length > 0) {
354
+ if (Object.keys(allLspProviders).length > 0) {
358
355
  initLspClient(monaco);
359
356
  }
360
357
  if (!document.getElementById("monaco-editor-core-css")) {
@@ -388,7 +385,7 @@ async function loadMonaco(highlighter, workspace, lsp) {
388
385
  await workspace._openTextDocument(resource.toString(), editor, selectionOrPosition);
389
386
  return true;
390
387
  } catch (err) {
391
- if (err instanceof ErrorNotFound) {
388
+ if (err instanceof NotFoundError) {
392
389
  return false;
393
390
  }
394
391
  throw err;
@@ -443,9 +440,9 @@ async function loadMonaco(highlighter, workspace, lsp) {
443
440
  }
444
441
  registerShikiMonacoTokenizer(monaco, highlighter, id);
445
442
  let lspLabel = id;
446
- let lspProvider = lspProviderMap[lspLabel];
443
+ let lspProvider = allLspProviders[lspLabel];
447
444
  if (!lspProvider) {
448
- const alias = Object.entries(lspProviderMap).find(([, lsp2]) => lsp2.aliases?.includes(id));
445
+ const alias = Object.entries(allLspProviders).find(([, lsp2]) => lsp2.aliases?.includes(id));
449
446
  if (alias) {
450
447
  [lspLabel, lspProvider] = alias;
451
448
  }