modern-monaco 0.0.0-beta.0 → 0.0.0-beta.2
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/LICENSE +1 -1
- package/README.md +129 -87
- package/dist/cache.js +1 -1
- package/dist/core.js +479 -0
- package/dist/editor-core.js +128 -21
- package/dist/index.js +29166 -449
- package/dist/lsp/css/setup.js +6 -7
- package/dist/lsp/css/worker.js +8961 -1355
- package/dist/lsp/html/setup.js +7 -8
- package/dist/lsp/html/worker.js +6506 -672
- package/dist/lsp/json/setup.js +5 -6
- package/dist/lsp/json/worker.js +395 -393
- package/dist/lsp/language-service.js +21 -21
- package/dist/lsp/typescript/libs.js +11 -9
- package/dist/lsp/typescript/setup.js +6 -7
- package/dist/shiki-wasm.js +1 -1
- package/dist/shiki.js +129 -751
- package/dist/ssr/index.js +13 -18
- package/dist/ssr/workerd.js +13 -18
- package/dist/util.js +7 -3
- package/dist/workspace.js +16 -16
- package/package.json +31 -13
- package/types/core.d.ts +4 -0
- package/types/index.d.ts +31 -8
- package/types/jsonSchema.d.ts +2 -0
- package/types/lsp.d.ts +7 -1
- package/types/ssr.d.ts +5 -5
- package/types/textmate.d.ts +1 -1
- package/types/cache.d.ts +0 -7
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,37 +1,26 @@
|
|
|
1
1
|
> [!WARNING]
|
|
2
|
-
> **This project is currently under active development
|
|
2
|
+
> **This project is currently under active development, the API may change at any time. Use at your own risk.**
|
|
3
|
+
> Please report any issues or feature requests on the [issues](https://github.com/esm-dev/modern-monaco/issues) page.
|
|
3
4
|
|
|
4
5
|
# Modern Monaco
|
|
5
6
|
|
|
6
7
|
Meeting the modern version of [Monaco Editor](https://www.npmjs.com/package/monaco-editor):
|
|
7
8
|
|
|
8
|
-
- Easy to use, no `MonacoEnvironment` setup and web-worker/css loader
|
|
9
|
+
- Easy to use, no `MonacoEnvironment` setup and web-worker/css loader needed.
|
|
9
10
|
- Using [Shiki](https://shiki.style) for syntax highlighting with tons of grammars and themes.
|
|
10
11
|
- Lazy loading: pre-highlighting code with Shiki while loading `monaco-editor-core` in background.
|
|
11
|
-
- Support
|
|
12
|
-
- Workspace (edit history, file system provider, etc).
|
|
12
|
+
- Support server-side rendering(SSR).
|
|
13
|
+
- Workspace (edit history, file system provider, persist protocol, etc).
|
|
13
14
|
- Automatically loading `.d.ts` from [esm.sh](https://esm.sh) CDN for type checking.
|
|
14
|
-
- Using [import maps](https://
|
|
15
|
+
- Using [import maps](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap) for resolving **bare specifier** imports in JavaScript/TypeScript.
|
|
15
16
|
- VSCode `window` APIs like `showInputBox`, `showQuickPick`, etc.
|
|
16
17
|
- Embedded languages(importmap/CSS/JavaScript) in HTML.
|
|
17
18
|
- Inline `html` and `css` in JavaScript/TypeScript.
|
|
18
19
|
- Auto-closing HTML/JSX tags.
|
|
19
20
|
|
|
20
|
-
Planned features:
|
|
21
|
-
|
|
22
|
-
- [ ] Show a loading indicator while loading the editor
|
|
23
|
-
- [ ] Quick open menu (only if a workspace is provided)
|
|
24
|
-
- [ ] Drag and drop file (only if a workspace is provided)
|
|
25
|
-
- [ ] Display non-code files, like images, videos, etc
|
|
26
|
-
- [ ] VSCode `winodow.show<XXX>Message` APIs
|
|
27
|
-
- [ ] Emmet
|
|
28
|
-
- [ ] Markdown language service
|
|
29
|
-
- [ ] [Volar](https://github.com/volarjs/volar.js) integration
|
|
30
|
-
- [ ] Support [Shiki JS RegExp Engine](https://shiki.style/guide/regex-engines#javascript-regexp-engine-experimental)
|
|
31
|
-
|
|
32
21
|
## Installation
|
|
33
22
|
|
|
34
|
-
You can install
|
|
23
|
+
You can install `modern-monaco` from NPM:
|
|
35
24
|
|
|
36
25
|
```bash
|
|
37
26
|
npm i modern-monaco typescript
|
|
@@ -40,7 +29,7 @@ npm i modern-monaco typescript
|
|
|
40
29
|
> [!Note]
|
|
41
30
|
> The `typescript` package is required by JavaScript/TypeScript LSP worker. We recommend `typescript@5.5.x` or later.
|
|
42
31
|
|
|
43
|
-
or import it from [esm.sh](https://esm.sh/) in browser without build step:
|
|
32
|
+
or import it from [esm.sh](https://esm.sh/) CDN in browser without build step:
|
|
44
33
|
|
|
45
34
|
```js
|
|
46
35
|
import * from "https://esm.sh/modern-monaco"
|
|
@@ -48,15 +37,17 @@ import * from "https://esm.sh/modern-monaco"
|
|
|
48
37
|
|
|
49
38
|
## Usage
|
|
50
39
|
|
|
51
|
-
`modern-monaco` provides three modes to create a browser
|
|
40
|
+
`modern-monaco` provides three modes to create a browser-based code editor:
|
|
52
41
|
|
|
53
|
-
- **Lazy**: pre-
|
|
54
|
-
- **SSR**: render
|
|
42
|
+
- **Lazy**: pre-highlight code with Shiki while loading the `editor-core.js` in the background.
|
|
43
|
+
- **SSR**: render a mock editor on the server side, and hydrate it on the client side.
|
|
55
44
|
- **Manual**: create a monaco editor instance manually.
|
|
56
45
|
|
|
57
46
|
### Lazy Mode
|
|
58
47
|
|
|
59
|
-
[monaco-editor](https://www.npmjs.com/package/monaco-editor) is a large package with extra CSS/Worker
|
|
48
|
+
[monaco-editor](https://www.npmjs.com/package/monaco-editor) is a large package with extra CSS/Worker modules, and needs the `MonacoEnvironment` setup for language service support. `modern-monaco` provides a lazy but smart way to load the editor modules on demand.
|
|
49
|
+
|
|
50
|
+
By pre-highlighting code with Shiki while loading editor modules in the background, `modern-monaco` can reduce the loading screen time.
|
|
60
51
|
|
|
61
52
|
```html
|
|
62
53
|
<monaco-editor></monaco-editor>
|
|
@@ -80,29 +71,34 @@ import * from "https://esm.sh/modern-monaco"
|
|
|
80
71
|
|
|
81
72
|
### SSR Mode
|
|
82
73
|
|
|
83
|
-
SSR mode returns
|
|
74
|
+
SSR mode returns an instant pre-rendered editor on the server side, and hydrate it on the client side.
|
|
84
75
|
|
|
85
76
|
```js
|
|
86
77
|
import { renderToWebComponent } from "modern-monaco/ssr";
|
|
87
78
|
|
|
88
79
|
export default {
|
|
89
|
-
fetch(req)
|
|
90
|
-
const ssrOut = renderToWebComponent(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
80
|
+
async fetch(req) {
|
|
81
|
+
const ssrOut = await renderToWebComponent(
|
|
82
|
+
`console.log("Hello, world!")`,
|
|
83
|
+
{
|
|
84
|
+
theme: "OneDark-Pro",
|
|
85
|
+
language: "javascript",
|
|
86
|
+
userAgent: req.headers.get("user-agent"), // detect default font for different platforms
|
|
87
|
+
},
|
|
88
|
+
);
|
|
89
|
+
return new Response(
|
|
90
|
+
html`
|
|
97
91
|
${ssrOut}
|
|
98
92
|
<script type="module">
|
|
99
93
|
import { hydrate } from "https://esm.sh/modern-monaco";
|
|
100
94
|
// hydrate the editor
|
|
101
95
|
hydrate();
|
|
102
96
|
</script>
|
|
103
|
-
`,
|
|
104
|
-
|
|
105
|
-
|
|
97
|
+
`,
|
|
98
|
+
{ headers: { "Content-Type": "text/html" } },
|
|
99
|
+
);
|
|
100
|
+
},
|
|
101
|
+
};
|
|
106
102
|
```
|
|
107
103
|
|
|
108
104
|
### Manual Mode
|
|
@@ -128,7 +124,7 @@ You can also create a [monaco editor](https://microsoft.github.io/monaco-editor/
|
|
|
128
124
|
|
|
129
125
|
## Using Workspace
|
|
130
126
|
|
|
131
|
-
`modern-monaco` provides
|
|
127
|
+
`modern-monaco` provides VSCode-like workspace features, like edit history, file system provider, etc.
|
|
132
128
|
|
|
133
129
|
```js
|
|
134
130
|
import { lazy, Workspace } from "modern-monaco";
|
|
@@ -142,7 +138,7 @@ const workspace = new Workspace({
|
|
|
142
138
|
"index.html": `<html><head><title>Hello, world!</title></head><body><script src="main.js"></script></body></html>`,
|
|
143
139
|
"main.js": `console.log("Hello, world!")`,
|
|
144
140
|
},
|
|
145
|
-
/** file to open when the editor is loaded
|
|
141
|
+
/** file to open when the editor is loaded for the first time. */
|
|
146
142
|
entryFile: "index.html",
|
|
147
143
|
});
|
|
148
144
|
|
|
@@ -150,12 +146,12 @@ const workspace = new Workspace({
|
|
|
150
146
|
lazy({ workspace });
|
|
151
147
|
|
|
152
148
|
// 3. open a file in the workspace
|
|
153
|
-
workspace.openTextDocument("main.js")
|
|
149
|
+
workspace.openTextDocument("main.js");
|
|
154
150
|
```
|
|
155
151
|
|
|
156
152
|
### Adding `tsconfig.json`
|
|
157
153
|
|
|
158
|
-
You can
|
|
154
|
+
You can add a `tsconfig.json` file to configure the TypeScript compiler options for the TypeScript language service.
|
|
159
155
|
|
|
160
156
|
```js
|
|
161
157
|
const tsconfig = {
|
|
@@ -175,8 +171,7 @@ const workspace = new Workspace({
|
|
|
175
171
|
|
|
176
172
|
### Using Import Maps
|
|
177
173
|
|
|
178
|
-
`modern-monaco` uses [import maps](https://
|
|
179
|
-
By default, `modern-monaco` dedetects the `importmap` script of root `index.html`.
|
|
174
|
+
`modern-monaco` uses [import maps](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap) to resolve **bare specifier** imports in JavaScript/TypeScript. By default, `modern-monaco` detects the `importmap` from the root `index.html` in the workspace.
|
|
180
175
|
|
|
181
176
|
```js
|
|
182
177
|
const indexHtml = html`<!DOCTYPE html>
|
|
@@ -185,80 +180,119 @@ const indexHtml = html`<!DOCTYPE html>
|
|
|
185
180
|
<script type="importmap">
|
|
186
181
|
{
|
|
187
182
|
"imports": {
|
|
188
|
-
"
|
|
189
|
-
"react": "https://esm.sh/react@18"
|
|
183
|
+
"react": "https://esm.sh/react@18",
|
|
184
|
+
"react-dom/": "https://esm.sh/react-dom@18/"
|
|
190
185
|
}
|
|
191
186
|
}
|
|
192
187
|
</script>
|
|
193
188
|
</head>
|
|
194
189
|
<body>
|
|
195
|
-
<
|
|
196
|
-
|
|
197
|
-
</script>
|
|
190
|
+
<div id="root"></div>
|
|
191
|
+
<script type="module" src="app.tsx"></script>
|
|
198
192
|
</body>
|
|
199
193
|
</html>
|
|
200
194
|
`;
|
|
195
|
+
const appTsx = `import { createRoot } from "react-dom/client";
|
|
196
|
+
|
|
197
|
+
createRoot(document.getElementById("root")).render(<div>Hello, world!</div>);
|
|
198
|
+
`;
|
|
199
|
+
|
|
201
200
|
const workspace = new Workspace({
|
|
202
201
|
initialFiles: {
|
|
203
202
|
"index.html": indexHtml,
|
|
203
|
+
"app.tsx": appTsx,
|
|
204
|
+
},
|
|
205
|
+
});
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
You can also provide an importmap object as the `lsp.typescript.importMap` option in the `lazy`, `init`, or `hydrate` function.
|
|
209
|
+
|
|
210
|
+
```js
|
|
211
|
+
lazy({
|
|
212
|
+
lsp: {
|
|
213
|
+
typescript: {
|
|
214
|
+
importMap: {
|
|
215
|
+
"react": "https://esm.sh/react@18",
|
|
216
|
+
"react-dom/": "https://esm.sh/react-dom@18/",
|
|
217
|
+
},
|
|
218
|
+
},
|
|
204
219
|
},
|
|
205
220
|
});
|
|
206
221
|
```
|
|
207
222
|
|
|
208
223
|
> [!Note]
|
|
209
224
|
> By default, `modern-monaco` uses `react` or `preact` in the `importmap` script as the `jsxImportSource` option for typescript worker.
|
|
210
|
-
> To use a custom `jsxImportSource` option, add `@jsxRuntime` specifier in the `importmap` script.
|
|
211
|
-
|
|
225
|
+
> To use a custom `jsxImportSource` option, add `@jsxRuntime` specifier in the `importmap` script.
|
|
212
226
|
|
|
213
227
|
## Editor Theme & Language Grammars
|
|
214
228
|
|
|
215
|
-
`modern-monaco` uses [Shiki](https://shiki.style) for syntax highlighting with tons of grammars and themes.
|
|
229
|
+
`modern-monaco` uses [Shiki](https://shiki.style) for syntax highlighting with tons of grammars and themes. By default, it loads themes and grammars from esm.sh on demand.
|
|
216
230
|
|
|
217
231
|
### Setting the Editor Theme
|
|
218
232
|
|
|
219
233
|
To set the theme of the editor, you can add a `theme` attribute to the `<monaco-editor>` element.
|
|
220
234
|
|
|
221
235
|
```html
|
|
222
|
-
<monaco-editor theme="
|
|
236
|
+
<monaco-editor theme="OneDark-Pro"></monaco-editor>
|
|
223
237
|
```
|
|
224
238
|
|
|
225
239
|
or set it in the `lazy`, `init`, or `hydrate` function.
|
|
226
240
|
|
|
227
241
|
```js
|
|
228
242
|
lazy({
|
|
229
|
-
theme: "
|
|
243
|
+
theme: "OneDark-Pro",
|
|
230
244
|
});
|
|
231
245
|
```
|
|
232
246
|
|
|
233
247
|
> [!Note]
|
|
234
248
|
> The theme ID should be one of the [Shiki Themes](https://shiki.style/themes).
|
|
235
249
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
By default, `modern-monaco` loads language grammars when a specific language mode is attached in the editor. You can also pre-load language grammars by adding the `langs` option to the `lazy`, `init`, or `hydrate` function.
|
|
250
|
+
`modern-monaco` loads the theme data from the CDN when a theme ID is provided. You can also use a theme from the `tm-themes` package:
|
|
239
251
|
|
|
240
252
|
```js
|
|
253
|
+
import OneDark from "tm-themes/themes/OneDark-Pro.json" with { type: "json" };
|
|
254
|
+
|
|
241
255
|
lazy({
|
|
242
|
-
|
|
256
|
+
theme: OneDark
|
|
243
257
|
});
|
|
244
258
|
```
|
|
245
259
|
|
|
246
|
-
###
|
|
260
|
+
### Pre-loading Language Grammars
|
|
247
261
|
|
|
248
|
-
You can also
|
|
262
|
+
By default, `modern-monaco` loads language grammars when a specific language mode is attached in the editor. You can also pre-load language grammars by adding the `langs` option to the `lazy`, `init`, or `hydrate` function. The `langs` option is an array of language grammars, which can be a language grammar object, a language ID, or a URL to the language grammar.
|
|
249
263
|
|
|
250
264
|
```js
|
|
265
|
+
import markdown from "tm-grammars/markdown.json" with { type: "json" };
|
|
266
|
+
|
|
251
267
|
lazy({
|
|
252
268
|
langs: [
|
|
269
|
+
// load language grammars from CDN
|
|
270
|
+
"html",
|
|
271
|
+
"css",
|
|
272
|
+
"javascript",
|
|
273
|
+
"json",
|
|
274
|
+
|
|
275
|
+
// load language grammar from a URL
|
|
276
|
+
"https://example.com/grammars/mylang.json",
|
|
277
|
+
|
|
278
|
+
// load language grammar from a local file
|
|
279
|
+
"/assets/mylang.json",
|
|
280
|
+
|
|
281
|
+
// use `tm-grammars` package without extra http requests, but increases the bundle size
|
|
282
|
+
markdown,
|
|
283
|
+
|
|
284
|
+
// dynamically import
|
|
285
|
+
() => import("tm-grammars/markdown.json", { with: { type: "json" } }),
|
|
286
|
+
|
|
253
287
|
// hand-crafted language grammar
|
|
254
288
|
{
|
|
255
289
|
name: "mylang",
|
|
256
290
|
scopeName: "source.mylang",
|
|
257
291
|
patterns: [/* ... */],
|
|
258
292
|
},
|
|
259
|
-
// or load a grammar from URL
|
|
260
|
-
"https://example.com/grammars/mylang.json",
|
|
261
293
|
],
|
|
294
|
+
// the CDN for loading language grammars and themes, default is "https://esm.sh"
|
|
295
|
+
tmDownloadCDN: "https://unpkg.com",
|
|
262
296
|
});
|
|
263
297
|
```
|
|
264
298
|
|
|
@@ -268,7 +302,7 @@ You can set the editor options in the `<monaco-editor>` element as attributes. T
|
|
|
268
302
|
|
|
269
303
|
```html
|
|
270
304
|
<monaco-editor
|
|
271
|
-
theme="
|
|
305
|
+
theme="OneDark-Pro"
|
|
272
306
|
fontFamily="Geist Mono"
|
|
273
307
|
fontSize="16"
|
|
274
308
|
></monaco-editor>
|
|
@@ -279,45 +313,42 @@ For SSR mode, you can set the editor options in the `renderToWebComponent` funct
|
|
|
279
313
|
```js
|
|
280
314
|
import { renderToWebComponent } from "modern-monaco/ssr";
|
|
281
315
|
|
|
282
|
-
const html = renderToWebComponent(
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
fontFamily: "Geist Mono",
|
|
292
|
-
fontSize: 16,
|
|
293
|
-
// ...
|
|
294
|
-
});
|
|
316
|
+
const html = await renderToWebComponent(
|
|
317
|
+
`console.log("Hello, world!")`,
|
|
318
|
+
{
|
|
319
|
+
theme: "OneDark-Pro",
|
|
320
|
+
language: "javascript",
|
|
321
|
+
fontFamily: "Geist Mono",
|
|
322
|
+
fontSize: 16,
|
|
323
|
+
},
|
|
324
|
+
);
|
|
295
325
|
```
|
|
296
326
|
|
|
297
327
|
For manual mode, check [here](https://microsoft.github.io/monaco-editor/docs.html#functions/editor.create.html) for more details.
|
|
298
328
|
|
|
299
|
-
## Language Server Protocol(LSP)
|
|
329
|
+
## Language Server Protocol (LSP)
|
|
300
330
|
|
|
301
|
-
`modern-monaco` by default supports full LSP features for following languages:
|
|
331
|
+
`modern-monaco` by default supports full LSP features for the following languages:
|
|
302
332
|
|
|
303
|
-
-
|
|
304
|
-
-
|
|
305
|
-
-
|
|
306
|
-
-
|
|
333
|
+
- HTML
|
|
334
|
+
- CSS/SCSS/LESS
|
|
335
|
+
- JavaScript/TypeScript
|
|
336
|
+
- JSON
|
|
307
337
|
|
|
308
338
|
Plus, `modern-monaco` also supports features like:
|
|
309
339
|
|
|
310
|
-
-
|
|
311
|
-
-
|
|
312
|
-
-
|
|
340
|
+
- File System Provider for import completions
|
|
341
|
+
- Embedded languages in HTML
|
|
342
|
+
- Inline `html` and `css` in JavaScript/TypeScript.
|
|
343
|
+
- Auto-closing HTML/JSX tags
|
|
313
344
|
|
|
314
345
|
> [!Note]
|
|
315
346
|
> You don't need to set the `MonacoEnvironment.getWorker` for LSP support.
|
|
316
|
-
> `modern-monaco` will automatically load required LSP workers.
|
|
347
|
+
> `modern-monaco` will automatically load the required LSP workers.
|
|
317
348
|
|
|
318
349
|
### LSP language configuration
|
|
319
350
|
|
|
320
|
-
You can configure
|
|
351
|
+
You can configure built-in LSPs in the `lazy`, `init`, or `hydrate` function.
|
|
321
352
|
|
|
322
353
|
```js
|
|
323
354
|
lazy({
|
|
@@ -341,6 +372,7 @@ export interface LSPLanguageConfig {
|
|
|
341
372
|
};
|
|
342
373
|
css?: {};
|
|
343
374
|
json?: {
|
|
375
|
+
/** JSON schemas for JSON language service. */
|
|
344
376
|
schemas?: JSONSchemaSource[];
|
|
345
377
|
};
|
|
346
378
|
typescript?: {
|
|
@@ -348,12 +380,22 @@ export interface LSPLanguageConfig {
|
|
|
348
380
|
compilerOptions?: ts.CompilerOptions;
|
|
349
381
|
/** The global import map. */
|
|
350
382
|
importMap?: ImportMap;
|
|
351
|
-
/** The version of
|
|
383
|
+
/** The version of TypeScript from the CDN. Default: ">= 5.0.0" */
|
|
352
384
|
tsVersion?: string;
|
|
353
385
|
};
|
|
354
386
|
}
|
|
355
387
|
```
|
|
356
388
|
|
|
357
|
-
|
|
389
|
+
## Using `core` module
|
|
390
|
+
|
|
391
|
+
`modern-monaco` includes built-in grammars and LSP providers for HTML, CSS, JavaScript/TypeScript, and JSON. If you don't need these features, you can use the `modern-monaco/core` sub-module to reduce the bundle size.
|
|
392
|
+
|
|
393
|
+
```js
|
|
394
|
+
import { lazy } from "modern-monaco/core";
|
|
395
|
+
|
|
396
|
+
lazy();
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
## License
|
|
358
400
|
|
|
359
|
-
[
|
|
401
|
+
[MIT License](https://opensource.org/licenses/MIT)
|