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 +1 -1
- package/dist/core.mjs +37 -40
- package/dist/editor-core.mjs +48099 -46106
- package/dist/editor-worker.mjs +308 -441
- package/dist/index.mjs +11 -9
- package/dist/lsp/client.mjs +14 -1
- package/dist/lsp/css/setup.mjs +1 -2
- package/dist/lsp/css/worker.mjs +39 -42
- package/dist/lsp/html/setup.mjs +1 -2
- package/dist/lsp/html/worker.mjs +52 -59
- package/dist/lsp/json/setup.mjs +1 -2
- package/dist/lsp/json/worker.mjs +654 -62
- package/dist/lsp/typescript/libs.mjs +1 -1
- package/dist/lsp/typescript/setup.mjs +7 -7
- package/dist/shiki-wasm.mjs +1 -1
- package/dist/shiki.mjs +40 -22
- package/dist/util.mjs +9 -0
- package/dist/workspace.mjs +15 -27
- package/package.json +10 -10
- package/types/index.d.ts +5 -5
- package/types/monaco.d.ts +147 -122
- package/types/textmate.d.ts +1 -1
- package/types/workspace.d.ts +1 -1
package/README.md
CHANGED
package/dist/core.mjs
CHANGED
|
@@ -40,15 +40,15 @@ function isObject(v) {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
// package.json
|
|
43
|
-
var version = "0.
|
|
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 {
|
|
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:
|
|
85
|
+
NotFound: NotFoundError
|
|
86
86
|
};
|
|
87
|
-
var cdnUrl = `https://esm.sh/modern-monaco@${version}`;
|
|
88
87
|
var syntaxes = [];
|
|
89
88
|
var lspProviders = {};
|
|
90
|
-
var
|
|
91
|
-
var
|
|
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
|
|
95
|
-
return loadMonaco(
|
|
93
|
+
const shiki = await initShiki({ ...options, langs });
|
|
94
|
+
return loadMonaco(shiki, options?.workspace, options?.lsp, options?.cdn);
|
|
96
95
|
}
|
|
97
|
-
|
|
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 =
|
|
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
|
-
|
|
174
|
-
let widthAttr =
|
|
175
|
-
let heightAttr =
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
217
|
-
|
|
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
|
-
|
|
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
|
|
236
|
+
if (error instanceof NotFoundError) {
|
|
240
237
|
} else {
|
|
241
238
|
throw error;
|
|
242
239
|
}
|
|
243
240
|
}
|
|
244
241
|
}
|
|
245
242
|
if (prerenderEl) {
|
|
246
|
-
|
|
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
|
|
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
|
|
336
|
-
let
|
|
337
|
-
let
|
|
338
|
-
if (
|
|
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(
|
|
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
|
|
347
|
+
const useBuiltinLSP = globalThis.MonacoEnvironment?.useBuiltinLSP;
|
|
351
348
|
const [monaco, { builtinLSPProviders }] = await Promise.all([
|
|
352
349
|
import(editorCoreModuleUrl),
|
|
353
|
-
|
|
350
|
+
useBuiltinLSP ? import(lspModuleUrl) : Promise.resolve({ builtinLSPProviders: {} })
|
|
354
351
|
]);
|
|
355
|
-
const
|
|
352
|
+
const allLspProviders = { ...builtinLSPProviders, ...lspProviders, ...lsp?.providers };
|
|
356
353
|
workspace?.setupMonaco(monaco);
|
|
357
|
-
if (Object.keys(
|
|
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
|
|
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 =
|
|
443
|
+
let lspProvider = allLspProviders[lspLabel];
|
|
447
444
|
if (!lspProvider) {
|
|
448
|
-
const alias = Object.entries(
|
|
445
|
+
const alias = Object.entries(allLspProviders).find(([, lsp2]) => lsp2.aliases?.includes(id));
|
|
449
446
|
if (alias) {
|
|
450
447
|
[lspLabel, lspProvider] = alias;
|
|
451
448
|
}
|