@nibling/editor 0.0.2 → 0.1.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/dist/element.d.ts CHANGED
@@ -45,6 +45,13 @@ export declare const editorDefaults: {
45
45
  autocomplete: boolean;
46
46
  worker: () => WorkerLike;
47
47
  };
48
+ /**
49
+ * Gets the next editor's tokenizer ready before it opens, e.g. while a pointer rests on a file in a
50
+ * tree: starts its worker and has it load `language`'s grammar, so the first colors don't wait on
51
+ * either. Repeat calls reuse the one waiting worker, which keeps every grammar it loaded. Needs
52
+ * `editorDefaults.grammars`; does nothing without it.
53
+ */
54
+ export declare function preloadEditor(language: string): void;
48
55
  /**
49
56
  * `<nib-editor>`. The shell that wires the followers to one EditorCore; it holds no editing logic
50
57
  * of its own beyond routing SurfaceEvents to commands.
package/dist/element.js CHANGED
@@ -170,6 +170,22 @@ function sameTheme(a, b) {
170
170
  return false;
171
171
  return a.light === b.light && a.dark === b.dark;
172
172
  }
173
+ /** A worker started ahead of the next editor by preloadEditor, its grammars loading. */
174
+ const warm = [];
175
+ /**
176
+ * Gets the next editor's tokenizer ready before it opens, e.g. while a pointer rests on a file in a
177
+ * tree: starts its worker and has it load `language`'s grammar, so the first colors don't wait on
178
+ * either. Repeat calls reuse the one waiting worker, which keeps every grammar it loaded. Needs
179
+ * `editorDefaults.grammars`; does nothing without it.
180
+ */
181
+ export function preloadEditor(language) {
182
+ const grammars = editorDefaults.grammars;
183
+ if (!grammars || !Object.hasOwn(grammars.languages, language))
184
+ return;
185
+ if (warm.length === 0)
186
+ warm.push(editorDefaults.worker());
187
+ warm[0].postMessage({ type: 'preload', language, grammars });
188
+ }
173
189
  const apple = typeof navigator !== 'undefined' && /Mac|iPhone|iPad|iPod/.test(navigator.platform);
174
190
  let instances = 0;
175
191
  function deferred() {
@@ -530,7 +546,7 @@ export class NibEditor extends HTMLElement {
530
546
  this.setAttribute(k, v);
531
547
  // An inline expansion reads its source's tokens and never needs a worker.
532
548
  if (!this.#nested)
533
- this.#spare ??= editorDefaults.worker();
549
+ this.#spare ??= warm.pop() ?? editorDefaults.worker();
534
550
  this.#refreshRender();
535
551
  followers.add(this.#onDefaults);
536
552
  signal.addEventListener('abort', () => followers.delete(this.#onDefaults), { once: true });
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { NibEditor, editorDefaults, defineEditor, type EditorChangeEvent } from './element.ts';
1
+ export { NibEditor, editorDefaults, defineEditor, preloadEditor, type EditorChangeEvent } from './element.ts';
2
2
  export type { Extension, EditorContext, UndoProvider } from './extension.ts';
3
3
  export type { Overlay, OverlayKind, OverlaySource, LineClassSource, PresenceSlot, RemoteCursor, RemoteCursorSource } from './view/decorations.ts';
4
4
  export { autocomplete, wordCompletions, type AutocompleteOptions } from './complete/autocomplete.ts';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // `@nibling/editor`: the element. Importing it defines <nib-editor>.
2
2
  import { defineEditor } from "./element.js";
3
- export { NibEditor, editorDefaults, defineEditor } from "./element.js";
3
+ export { NibEditor, editorDefaults, defineEditor, preloadEditor } from "./element.js";
4
4
  export { autocomplete, wordCompletions } from "./complete/autocomplete.js";
5
5
  export * from "./headless.js";
6
6
  defineEditor();
@@ -87,6 +87,12 @@ export type ToWorker = {
87
87
  readonly version: Version;
88
88
  readonly epoch: number;
89
89
  readonly language: string;
90
+ }
91
+ /** Load a language's grammar before any document needs it (preloadEditor); no reply. */
92
+ | {
93
+ readonly type: 'preload';
94
+ readonly language: string;
95
+ readonly grammars: GrammarManifest;
90
96
  };
91
97
  /**
92
98
  * Worker -> main. The three typed arrays are transferred, not copied.
package/dist/worker.js CHANGED
@@ -25,9 +25,9 @@ const load = (url) => {
25
25
  };
26
26
  let manifest = null;
27
27
  const prefetch = (m) => {
28
- if (m.type === 'open')
28
+ if (m.type === 'open' || m.type === 'preload')
29
29
  manifest = m.grammars;
30
- if ((m.type !== 'open' && m.type !== 'language') || !manifest)
30
+ if ((m.type !== 'open' && m.type !== 'language' && m.type !== 'preload') || !manifest)
31
31
  return;
32
32
  const scope = manifest.languages[m.language];
33
33
  const url = scope === undefined ? undefined : moduleUrl(manifest, scope);
@@ -39,7 +39,9 @@ let queue = [];
39
39
  let handle = (m) => void queue.push(m);
40
40
  self.addEventListener('message', (e) => {
41
41
  prefetch(e.data);
42
- handle(e.data);
42
+ // A preload is only the prefetch: the engine has no document for it.
43
+ if (e.data.type !== 'preload')
44
+ handle(e.data);
43
45
  });
44
46
  const [{ TokenEngine }, { runtime }] = await Promise.all([import("./tokens/engine.js"), import("./tokens/textmate/index.js")]);
45
47
  const channel = new MessageChannel();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nibling/editor",
3
- "version": "0.0.2",
3
+ "version": "0.1.0",
4
4
  "description": "A code editor element for the web, with a headless core and TextMate grammars tokenized off the main thread",
5
5
  "keywords": [
6
6
  "editor",