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 CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2020-2024 Je Xia <i@jex.me>
3
+ Copyright (c) 2025 Je Xia <i@jex.me>
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,37 +1,26 @@
1
1
  > [!WARNING]
2
- > **This project is currently under active development and is not ready for production use.**
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 **server-side rendering(SSR)**.
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://github.com/WICG/import-maps) for resolving **bare specifier** imports in JavaScript/TypeScript.
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 the package from NPM in your node project with a bundler like [vite](http://vitejs.dev).
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 based code editor:
40
+ `modern-monaco` provides three modes to create a browser-based code editor:
52
41
 
53
- - **Lazy**: pre-hightlight code with Shiki while loading the `editor-core.js` in background.
54
- - **SSR**: render the editor in server side, and hydrate it in client side.
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 dependencies, not mention the `MonacoEnvironment` setup. `modern-monaco` provides a lazy but smart way to load the editor on demand, and it pre-highlights code with Shiki while loading the `editor-core.js` in background.
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 a instant rendered editor in server side, and hydrate it in client side.
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
- filename: "main.js",
92
- code: `console.log("Hello, world!")`,
93
- userAgent: req.headers.get("user-agent"), // detecte default font for different platforms
94
- theme: "theme-id",
95
- });
96
- return new Response(html`
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
- `, { headers: { "Content-Type": "text/html" }});
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 vscode-like workspace features, like edit history, file system provider, etc.
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 at first time. */
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 also add a `tsconfig.json` file to configure the TypeScript compiler options for the TypeScript language service worker.
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://github.com/WICG/import-maps) to resolving **bare specifier** import in JavaScript/TypeScript.
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
- "@jsxRuntime": "https://esm.sh/react@18",
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
- <script type="module">
196
- import React from "react";
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. For example, [solid-js](https://esm.sh/solid-js/jsx-runtime).
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. It loads themes and grammars from esm.sh on demand.
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="theme-id"></monaco-editor>
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: "theme-id",
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
- ### Pre-loading Language Grammars
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
- langs: ["javascript", "typescript", "css", "html", "json", "markdown"],
256
+ theme: OneDark
243
257
  });
244
258
  ```
245
259
 
246
- ### Custom Language Grammars
260
+ ### Pre-loading Language Grammars
247
261
 
248
- You can also add custom language grammars to the editor.
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="theme-id"
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
- // render options
284
- filename: "main.js",
285
- code: `console.log("Hello, world!")`,
286
- userAgent: req.headers.get("user-agent"), // font detection for different platforms
287
- // ...
288
-
289
- // editor options
290
- theme: "theme-id",
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
- - **HTML**
304
- - **CSS/SCSS/LESS**
305
- - **JavaScript/TypeScript**
306
- - **JSON**
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
- - **File System Provider for import completions**
311
- - **Embedded languages in HTML**
312
- - **Auto-closing HTML/JSX tags**
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 builtin LSPs in the `lazy`, `init`, or `hydrate` function.
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 the typescript from CDN. Default: ">= 5.0.0" */
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
- ### Custom LSP
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
- [TODO]
401
+ [MIT License](https://opensource.org/licenses/MIT)
package/dist/cache.js CHANGED
@@ -86,7 +86,7 @@ var Cache = class {
86
86
  await promisifyIDBRequest(tx.put(file));
87
87
  }
88
88
  };
89
- var cache = new Cache("monaco:cache");
89
+ var cache = new Cache("modern-monaco-cache");
90
90
  var cache_default = cache;
91
91
  export {
92
92
  cache,