modern-monaco 0.0.0-beta.0 → 0.0.0-beta.1

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
@@ -5,13 +5,13 @@
5
5
 
6
6
  Meeting the modern version of [Monaco Editor](https://www.npmjs.com/package/monaco-editor):
7
7
 
8
- - Easy to use, no `MonacoEnvironment` setup and web-worker/css loader
8
+ - Easy to use, no `MonacoEnvironment` setup and web-worker/css loader needed.
9
9
  - Using [Shiki](https://shiki.style) for syntax highlighting with tons of grammars and themes.
10
10
  - Lazy loading: pre-highlighting code with Shiki while loading `monaco-editor-core` in background.
11
11
  - Support **server-side rendering(SSR)**.
12
- - Workspace (edit history, file system provider, etc).
12
+ - Workspace (edit history, file system provider, persist protocol, etc).
13
13
  - 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.
14
+ - 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
15
  - VSCode `window` APIs like `showInputBox`, `showQuickPick`, etc.
16
16
  - Embedded languages(importmap/CSS/JavaScript) in HTML.
17
17
  - Inline `html` and `css` in JavaScript/TypeScript.
@@ -19,11 +19,15 @@ Meeting the modern version of [Monaco Editor](https://www.npmjs.com/package/mona
19
19
 
20
20
  Planned features:
21
21
 
22
+ - [ ] Built-in sidebar
23
+ - [ ] file explorer
24
+ - [ ] chat
25
+ - [ ] Workspace persist API
22
26
  - [ ] Show a loading indicator while loading the editor
23
27
  - [ ] Quick open menu (only if a workspace is provided)
24
28
  - [ ] Drag and drop file (only if a workspace is provided)
25
29
  - [ ] Display non-code files, like images, videos, etc
26
- - [ ] VSCode `winodow.show<XXX>Message` APIs
30
+ - [ ] VSCode `window.show<XXX>Message` APIs
27
31
  - [ ] Emmet
28
32
  - [ ] Markdown language service
29
33
  - [ ] [Volar](https://github.com/volarjs/volar.js) integration
@@ -48,15 +52,17 @@ import * from "https://esm.sh/modern-monaco"
48
52
 
49
53
  ## Usage
50
54
 
51
- `modern-monaco` provides three modes to create a browser based code editor:
55
+ `modern-monaco` provides three modes to create a browser-based code editor:
52
56
 
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.
57
+ - **Lazy**: pre-highlight code with Shiki while loading the `editor-core.js` in the background.
58
+ - **SSR**: render a mock editor on the server side, and hydrate it on the client side.
55
59
  - **Manual**: create a monaco editor instance manually.
56
60
 
57
61
  ### Lazy Mode
58
62
 
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.
63
+ [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.
64
+
65
+ By pre-highlighting code with Shiki while loading editor modules in the background, `modern-monaco` can reduce the loading screen time.
60
66
 
61
67
  ```html
62
68
  <monaco-editor></monaco-editor>
@@ -80,29 +86,34 @@ import * from "https://esm.sh/modern-monaco"
80
86
 
81
87
  ### SSR Mode
82
88
 
83
- SSR mode returns a instant rendered editor in server side, and hydrate it in client side.
89
+ SSR mode returns an instant pre-rendered editor on the server side, and hydrate it on the client side.
84
90
 
85
91
  ```js
86
92
  import { renderToWebComponent } from "modern-monaco/ssr";
87
93
 
88
94
  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`
95
+ async fetch(req) {
96
+ const ssrOut = await renderToWebComponent(
97
+ `console.log("Hello, world!")`,
98
+ {
99
+ theme: "OneDark-Pro",
100
+ language: "javascript",
101
+ userAgent: req.headers.get("user-agent"), // detect default font for different platforms
102
+ },
103
+ );
104
+ return new Response(
105
+ html`
97
106
  ${ssrOut}
98
107
  <script type="module">
99
108
  import { hydrate } from "https://esm.sh/modern-monaco";
100
109
  // hydrate the editor
101
110
  hydrate();
102
111
  </script>
103
- `, { headers: { "Content-Type": "text/html" }});
104
- }
105
- }
112
+ `,
113
+ { headers: { "Content-Type": "text/html" } },
114
+ );
115
+ },
116
+ };
106
117
  ```
107
118
 
108
119
  ### Manual Mode
@@ -128,7 +139,7 @@ You can also create a [monaco editor](https://microsoft.github.io/monaco-editor/
128
139
 
129
140
  ## Using Workspace
130
141
 
131
- `modern-monaco` provides vscode-like workspace features, like edit history, file system provider, etc.
142
+ `modern-monaco` provides VSCode-like workspace features, like edit history, file system provider, etc.
132
143
 
133
144
  ```js
134
145
  import { lazy, Workspace } from "modern-monaco";
@@ -142,7 +153,7 @@ const workspace = new Workspace({
142
153
  "index.html": `<html><head><title>Hello, world!</title></head><body><script src="main.js"></script></body></html>`,
143
154
  "main.js": `console.log("Hello, world!")`,
144
155
  },
145
- /** file to open when the editor is loaded at first time. */
156
+ /** file to open when the editor is loaded for the first time. */
146
157
  entryFile: "index.html",
147
158
  });
148
159
 
@@ -150,12 +161,12 @@ const workspace = new Workspace({
150
161
  lazy({ workspace });
151
162
 
152
163
  // 3. open a file in the workspace
153
- workspace.openTextDocument("main.js")
164
+ workspace.openTextDocument("main.js");
154
165
  ```
155
166
 
156
167
  ### Adding `tsconfig.json`
157
168
 
158
- You can also add a `tsconfig.json` file to configure the TypeScript compiler options for the TypeScript language service worker.
169
+ You can add a `tsconfig.json` file to configure the TypeScript compiler options for the TypeScript language service.
159
170
 
160
171
  ```js
161
172
  const tsconfig = {
@@ -175,8 +186,7 @@ const workspace = new Workspace({
175
186
 
176
187
  ### Using Import Maps
177
188
 
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`.
189
+ `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
190
 
181
191
  ```js
182
192
  const indexHtml = html`<!DOCTYPE html>
@@ -185,22 +195,42 @@ const indexHtml = html`<!DOCTYPE html>
185
195
  <script type="importmap">
186
196
  {
187
197
  "imports": {
188
- "@jsxRuntime": "https://esm.sh/react@18",
189
- "react": "https://esm.sh/react@18"
198
+ "react": "https://esm.sh/react@18",
199
+ "react-dom/": "https://esm.sh/react-dom@18/"
190
200
  }
191
201
  }
192
202
  </script>
193
203
  </head>
194
204
  <body>
195
- <script type="module">
196
- import React from "react";
197
- </script>
205
+ <div id="root"></div>
206
+ <script type="module" src="app.tsx"></script>
198
207
  </body>
199
208
  </html>
200
209
  `;
210
+ const appTsx = `import { createRoot } from "react-dom/client";
211
+
212
+ createRoot(document.getElementById("root")).render(<div>Hello, world!</div>);
213
+ `;
214
+
201
215
  const workspace = new Workspace({
202
216
  initialFiles: {
203
217
  "index.html": indexHtml,
218
+ "app.tsx": appTsx,
219
+ },
220
+ });
221
+ ```
222
+
223
+ You can also provide an importmap object as the `lsp.typescript.importMap` option in the `lazy`, `init`, or `hydrate` function.
224
+
225
+ ```js
226
+ lazy({
227
+ lsp: {
228
+ typescript: {
229
+ importMap: {
230
+ "react": "https://esm.sh/react@18",
231
+ "react-dom/": "https://esm.sh/react-dom@18/",
232
+ },
233
+ },
204
234
  },
205
235
  });
206
236
  ```
@@ -209,56 +239,75 @@ const workspace = new Workspace({
209
239
  > By default, `modern-monaco` uses `react` or `preact` in the `importmap` script as the `jsxImportSource` option for typescript worker.
210
240
  > 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
241
 
212
-
213
242
  ## Editor Theme & Language Grammars
214
243
 
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.
244
+ `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
245
 
217
246
  ### Setting the Editor Theme
218
247
 
219
248
  To set the theme of the editor, you can add a `theme` attribute to the `<monaco-editor>` element.
220
249
 
221
250
  ```html
222
- <monaco-editor theme="theme-id"></monaco-editor>
251
+ <monaco-editor theme="OneDark-Pro"></monaco-editor>
223
252
  ```
224
253
 
225
254
  or set it in the `lazy`, `init`, or `hydrate` function.
226
255
 
227
256
  ```js
228
257
  lazy({
229
- theme: "theme-id",
258
+ theme: "OneDark-Pro",
230
259
  });
231
260
  ```
232
261
 
233
262
  > [!Note]
234
263
  > The theme ID should be one of the [Shiki Themes](https://shiki.style/themes).
235
264
 
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.
265
+ `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
266
 
240
267
  ```js
268
+ import OneDark from "tm-themes/themes/OneDark-Pro.json" with { type: "json" };
269
+
241
270
  lazy({
242
- langs: ["javascript", "typescript", "css", "html", "json", "markdown"],
271
+ theme: OneDark
243
272
  });
244
273
  ```
245
274
 
246
- ### Custom Language Grammars
275
+ ### Pre-loading Language Grammars
247
276
 
248
- You can also add custom language grammars to the editor.
277
+ 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
278
 
250
279
  ```js
280
+ import markdown from "tm-grammars/markdown.json" with { type: "json" };
281
+
251
282
  lazy({
252
283
  langs: [
284
+ // load language grammars from CDN
285
+ "html",
286
+ "css",
287
+ "javascript",
288
+ "json",
289
+
290
+ // load language grammar from a URL
291
+ "https://example.com/grammars/mylang.json",
292
+
293
+ // load language grammar from a local file
294
+ "/assets/mylang.json",
295
+
296
+ // use `tm-grammars` package without extra http requests, but increases the bundle size
297
+ markdown,
298
+
299
+ // dynamically import
300
+ () => import("tm-grammars/markdown.json", { with: { type: "json" } }),
301
+
253
302
  // hand-crafted language grammar
254
303
  {
255
304
  name: "mylang",
256
305
  scopeName: "source.mylang",
257
306
  patterns: [/* ... */],
258
307
  },
259
- // or load a grammar from URL
260
- "https://example.com/grammars/mylang.json",
261
308
  ],
309
+ // the CDN for loading language grammars and themes, default is "https://esm.sh"
310
+ tmDownloadCDN: "https://unpkg.com",
262
311
  });
263
312
  ```
264
313
 
@@ -268,7 +317,7 @@ You can set the editor options in the `<monaco-editor>` element as attributes. T
268
317
 
269
318
  ```html
270
319
  <monaco-editor
271
- theme="theme-id"
320
+ theme="OneDark-Pro"
272
321
  fontFamily="Geist Mono"
273
322
  fontSize="16"
274
323
  ></monaco-editor>
@@ -279,26 +328,22 @@ For SSR mode, you can set the editor options in the `renderToWebComponent` funct
279
328
  ```js
280
329
  import { renderToWebComponent } from "modern-monaco/ssr";
281
330
 
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
- });
331
+ const html = await renderToWebComponent(
332
+ `console.log("Hello, world!")`,
333
+ {
334
+ theme: "OneDark-Pro",
335
+ language: "javascript",
336
+ fontFamily: "Geist Mono",
337
+ fontSize: 16,
338
+ },
339
+ );
295
340
  ```
296
341
 
297
342
  For manual mode, check [here](https://microsoft.github.io/monaco-editor/docs.html#functions/editor.create.html) for more details.
298
343
 
299
- ## Language Server Protocol(LSP)
344
+ ## Language Server Protocol (LSP)
300
345
 
301
- `modern-monaco` by default supports full LSP features for following languages:
346
+ `modern-monaco` by default supports full LSP features for the following languages:
302
347
 
303
348
  - **HTML**
304
349
  - **CSS/SCSS/LESS**
@@ -309,15 +354,16 @@ Plus, `modern-monaco` also supports features like:
309
354
 
310
355
  - **File System Provider for import completions**
311
356
  - **Embedded languages in HTML**
357
+ - **Inline `html` and `css` in JavaScript/TypeScript.**
312
358
  - **Auto-closing HTML/JSX tags**
313
359
 
314
360
  > [!Note]
315
361
  > You don't need to set the `MonacoEnvironment.getWorker` for LSP support.
316
- > `modern-monaco` will automatically load required LSP workers.
362
+ > `modern-monaco` will automatically load the required LSP workers.
317
363
 
318
364
  ### LSP language configuration
319
365
 
320
- You can configure builtin LSPs in the `lazy`, `init`, or `hydrate` function.
366
+ You can configure built-in LSPs in the `lazy`, `init`, or `hydrate` function.
321
367
 
322
368
  ```js
323
369
  lazy({
@@ -341,6 +387,7 @@ export interface LSPLanguageConfig {
341
387
  };
342
388
  css?: {};
343
389
  json?: {
390
+ /** JSON schemas for JSON language service. */
344
391
  schemas?: JSONSchemaSource[];
345
392
  };
346
393
  typescript?: {
@@ -348,7 +395,7 @@ export interface LSPLanguageConfig {
348
395
  compilerOptions?: ts.CompilerOptions;
349
396
  /** The global import map. */
350
397
  importMap?: ImportMap;
351
- /** The version of the typescript from CDN. Default: ">= 5.0.0" */
398
+ /** The version of TypeScript from the CDN. Default: ">= 5.0.0" */
352
399
  tsVersion?: string;
353
400
  };
354
401
  }
@@ -357,3 +404,13 @@ export interface LSPLanguageConfig {
357
404
  ### Custom LSP
358
405
 
359
406
  [TODO]
407
+
408
+ ### Using `core` module
409
+
410
+ `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.
411
+
412
+ ```js
413
+ import { lazy } from "modern-monaco/core";
414
+
415
+ lazy();
416
+ ```
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,