@coldsmirk/inkstone-core 0.35.0 → 0.36.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
@@ -11,7 +11,7 @@ Part of [inkstone](https://github.com/coldsmirk/inkstone). The editor layers ([`
11
11
  - **`onigurumaEngine`** — the Oniguruma engine on its WASM, inlined into a chunk of your own bundle (about 600 KB, nothing fetched from anywhere else) and instantiated once, in about 20 ms. Which engine: the JavaScript one translates each grammar's patterns into JavaScript at the first tokenization, on the main thread, and pays for it on whole files — a TypeScript file's first sixty lines cost it about 170 ms, against Oniguruma's 20. Colouring fenced snippets, keep the default; reading whole files in an editor (`@coldsmirk/inkstone-monaco`'s Shiki bridge, `<MonacoEditor>`), pass `onigurumaEngine` as the factory. `onigurumaEngine(wasm)` takes the WASM from elsewhere in any form Shiki's `createOnigurumaEngine` accepts, such as a `fetch("/onig.wasm")`.
12
12
  - **`shikiThemeId(dark)`** plus the `INKSTONE_LIGHT_THEME` / `INKSTONE_DARK_THEME` constants — the theme names to pass wherever a Shiki theme id is expected.
13
13
  - **`highlighterSources(highlighter)`** — what the shared highlighter was built from and has loaded since: every grammar registration in the order it was given them (the configured `langs` and every later `loadLanguage` / `loadLanguageSync`, which the highlighter wraps to keep them) and the kind of its engine — `oniguruma` wherever an engine was configured, `javascript` where none was. `@coldsmirk/inkstone-monaco`'s tokenization worker builds a highlighter of the same grammars from it; undefined for a highlighter inkstone did not build.
14
- - **`configureEditorColors(config)`** — set every color the editors paint that is not syntax (a VS Code theme's `colors`, beside its `tokenColors`) in your design language, once, before any editor mounts. Per scheme, CSS color literals for the editor (`background` — content, gutter and the fields of its chrome — `lineHighlight`, `lineNumber`, `lineNumberActive`, `cursor`, `selection`, `scrollbar`), its floating chrome — completion, hover, parameter hints, the find widget and search panels, the context menu — (`widget`, `widgetBorder`, `widgetSelection`, `widgetForeground`, `widgetForegroundMuted`), the `accent` that rings a focused field and marks what a completion matched, and the diagnostic severities `error`, `warning`, `info` and `hint`, named as CodeMirror's `Diagnostic.severity` names them. The catppuccin pair keeps its syntax palette — token colors, the editor's default text, the completion-kind icons that mirror them — and gets these baked into its theme colors — Monaco, static rendering, and CodeMirror's chrome all read the same values — so an editor sits inside the app instead of bringing a second dark. A role or scheme left out keeps the flavor's own color, a later call replaces the schemes it names, and values must be hex colors (`#rrggbb`, `#rrggbbaa`) — Monaco parses theme colors as hex and paints red for anything else, so a `var()` reference or an `rgb(…)` is refused up front. Keep `lineHighlight` translucent: CodeMirror draws selections behind the content, so an opaque active line would hide them. `selection` shows at half its alpha while the editor is unfocused, in both engines; `scrollbar` has no hover state (CodeMirror's `scrollbar-color` has none, so Monaco's slider keeps it too). `getEditorColors(dark)` reads what was configured, and `withEditorColors(theme, colors)` applies a set of them to a Shiki theme of your own.
14
+ - **`configureEditorColors(config)`** — set every color the editors paint that is not syntax (a VS Code theme's `colors`, beside its `tokenColors`) in your design language — before any editor mounts, and again whenever your design language moves (a theme of your app's, picked). Per scheme, CSS color literals for the editor (`background` — content, gutter and the fields of its chrome — `lineHighlight`, `lineNumber`, `lineNumberActive`, `cursor`, `selection`, `scrollbar`), its floating chrome — completion, hover, parameter hints, the find widget and search panels, the context menu — (`widget`, `widgetBorder`, `widgetSelection`, `widgetForeground`, `widgetForegroundMuted`), the `accent` that rings a focused field and marks what a completion matched, and the diagnostic severities `error`, `warning`, `info` and `hint`, named as CodeMirror's `Diagnostic.severity` names them. The catppuccin pair keeps its syntax palette — token colors, the editor's default text, the completion-kind icons that mirror them — and gets these baked into its theme colors — Monaco, static rendering, and CodeMirror's chrome all read the same values — so an editor sits inside the app instead of bringing a second dark. A role or scheme left out keeps the flavor's own color, a later call replaces the schemes it names — after the highlighter is built, the pair is lit again in the new colors under the same ids, and every editor mounted repaints in place: Monaco's through the Shiki bridge, CodeMirror's chrome by itself; `subscribeEditorColors(painter)` hears each move, once the pair is lit — and values must be hex colors (`#rrggbb`, `#rrggbbaa`) — Monaco parses theme colors as hex and paints red for anything else, so a `var()` reference or an `rgb(…)` is refused up front. Keep `lineHighlight` translucent: CodeMirror draws selections behind the content, so an opaque active line would hide them. `selection` shows at half its alpha while the editor is unfocused, in both engines; `scrollbar` has no hover state (CodeMirror's `scrollbar-color` has none, so Monaco's slider keeps it too). `getEditorColors(dark)` reads what was configured, and `withEditorColors(theme, colors)` applies a set of them to a Shiki theme of your own.
15
15
  - **`CODE_FONT_FAMILY`** — the shared code font stack both editor engines render with: the page's `--inkstone-font-family-code` CSS token when defined, else the platform's native monospace.
16
16
  - **`inkstoneRadius(size, fallback)` / `inkstoneShadow(size, fallback)`** — `var()` references to the host's `--inkstone-radius-{small,medium,large,xlarge}` and `--inkstone-shadow-{medium,large,xlarge}` tokens. Set them once on your root and both engines' chrome follows: the Monaco host maps them onto VS Code's `--vscode-cornerRadius-*` / `--vscode-shadow-*` design tokens (which the standalone editor never defines), CodeMirror's polish reads them directly; unset, each engine keeps the look it ships with.
17
17
 
package/dist/index.d.ts CHANGED
@@ -233,18 +233,30 @@ export declare function highlighterSources(highlighter: HighlighterCore$1): High
233
233
  */
234
234
  export declare function configureHighlighter(options: HighlighterConfig): void;
235
235
  /**
236
- * Paint the editors in the host's own colors, once, before the first {@link getHighlighter}
237
- * call — the catppuccin pair is registered with these colors baked in (Monaco reads them from
238
- * the theme, static rendering too), and CodeMirror's chrome reads them at mount. A scheme left
239
- * out keeps the flavor's own colors; a role left out keeps that role's flavor color; a later
240
- * call replaces the schemes it names. Process-global like the highlighter itself, for the same
241
- * reason: one palette across every editor and rendered block.
236
+ * Paint the editors in the host's own colors — before the first {@link getHighlighter} call, or
237
+ * after it, as often as the host's own design language moves (a theme it offers, picked).
238
+ * Before, the catppuccin pair is built with these colors baked in. After, the pair is lit again
239
+ * in the built highlighter under the same ids, and every editor painted in it repaints: Monaco's
240
+ * through the bridge (`@coldsmirk/inkstone-monaco`), which defines its themes again, and
241
+ * CodeMirror's chrome, which reconfigures itself in every editor it is mounted in, once
242
+ * {@link subscribeEditorColors}'s listeners are told — after the highlighter is lit again, so what they read is the new pair. A
243
+ * block rendered statically is painted anew when it renders again.
244
+ *
245
+ * A scheme left out keeps the flavor's own colors; a role left out keeps that role's flavor
246
+ * color; a later call replaces the schemes it names. Process-global like the highlighter
247
+ * itself, for the same reason: one palette across every editor and rendered block.
242
248
  *
243
249
  * Every value must be a hex color (`#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`) — Monaco parses
244
250
  * theme colors as hex and paints red for anything else, a custom property reference or an
245
251
  * `rgb(…)` alike, so those are refused here instead of failing on screen.
246
252
  */
247
253
  export declare function configureEditorColors(config: EditorColorsConfig): void;
254
+ /**
255
+ * Hear the editor colors move ({@link configureEditorColors}): `painter` is called once the
256
+ * colors stand, and the built highlighter's catppuccin pair is lit in them. Returns the way to
257
+ * stop hearing them.
258
+ */
259
+ export declare function subscribeEditorColors(painter: () => void): () => void;
248
260
  /**
249
261
  * The editor colors configured for a color scheme through {@link configureEditorColors}, or
250
262
  * undefined when the flavor's own colors stand.
package/dist/index.js CHANGED
@@ -164,12 +164,14 @@ function getHighlighterRegistry() {
164
164
  config: {},
165
165
  surfaces: {},
166
166
  promise: null,
167
- sources: /* @__PURE__ */ new WeakMap()
167
+ sources: /* @__PURE__ */ new WeakMap(),
168
+ painters: /* @__PURE__ */ new Set()
168
169
  };
169
170
  host[HIGHLIGHTER_REGISTRY_KEY] = registry;
170
171
  }
171
172
  registry.surfaces ??= {};
172
173
  registry.sources ??= /* @__PURE__ */ new WeakMap();
174
+ registry.painters ??= /* @__PURE__ */ new Set();
173
175
  return registry;
174
176
  }
175
177
  async function resolveLanguages(langs) {
@@ -216,9 +218,17 @@ function configureHighlighter(options) {
216
218
  };
217
219
  }
218
220
  const HEX_COLOR = /^#(?:[\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$/i;
221
+ function ownTheme(theme) {
222
+ const owned = { ...theme };
223
+ if (theme.tokenColors !== void 0) owned.tokenColors = [...theme.tokenColors];
224
+ if (theme.settings !== void 0) owned.settings = [...theme.settings];
225
+ return owned;
226
+ }
227
+ function inkstoneThemes(surfaces) {
228
+ return [withEditorColors(ownTheme(catppuccinLatte), surfaces.light), withEditorColors(ownTheme(catppuccinMacchiato), surfaces.dark)];
229
+ }
219
230
  function configureEditorColors(config) {
220
231
  const registry = getHighlighterRegistry();
221
- assertConfigurable(registry, "configureEditorColors");
222
232
  for (const [scheme, colors] of Object.entries(config)) {
223
233
  const roles = Object.entries(colors ?? {});
224
234
  for (const [role, value] of roles) if (typeof value === "string" && !HEX_COLOR.test(value)) throw new Error(`configureEditorColors: ${scheme}.${role} must be a hex color, which is all Monaco parses (${value})`);
@@ -227,6 +237,33 @@ function configureEditorColors(config) {
227
237
  ...registry.surfaces,
228
238
  ...config
229
239
  };
240
+ const built = registry.promise;
241
+ if (built === null) {
242
+ paint(registry);
243
+ return;
244
+ }
245
+ built.then((highlighter) => {
246
+ highlighter.loadThemeSync(...inkstoneThemes(registry.surfaces));
247
+ }, () => void 0).then(() => {
248
+ paint(registry);
249
+ });
250
+ }
251
+ function paint(registry) {
252
+ for (const painter of registry.painters) try {
253
+ painter();
254
+ } catch (error) {
255
+ console.error("inkstone: an editor could not repaint in the new editor colors", error);
256
+ }
257
+ }
258
+ function subscribeEditorColors(painter) {
259
+ const { painters } = getHighlighterRegistry();
260
+ const entry = () => {
261
+ painter();
262
+ };
263
+ painters.add(entry);
264
+ return () => {
265
+ painters.delete(entry);
266
+ };
230
267
  }
231
268
  function getEditorColors(dark) {
232
269
  const { surfaces } = getHighlighterRegistry();
@@ -240,11 +277,7 @@ function getHighlighter() {
240
277
  const attempt = (async () => {
241
278
  const langs = await resolveLanguages(config.langs ?? DEFAULT_HIGHLIGHTER_LANGS);
242
279
  return keepingSources(await createHighlighterCore({
243
- themes: [
244
- withEditorColors(catppuccinLatte, surfaces.light),
245
- withEditorColors(catppuccinMacchiato, surfaces.dark),
246
- ...config.themes ?? []
247
- ],
280
+ themes: [...inkstoneThemes(surfaces), ...config.themes ?? []],
248
281
  langs,
249
282
  engine: engineOf(config)
250
283
  }), langs, config.engine === void 0 ? "javascript" : "oniguruma", registry);
@@ -259,4 +292,4 @@ function shikiThemeId(dark) {
259
292
  return dark ? INKSTONE_DARK_THEME : INKSTONE_LIGHT_THEME;
260
293
  }
261
294
  //#endregion
262
- export { CODE_FONT_FAMILY, DEFAULT_HIGHLIGHTER_LANGS, INKSTONE_DARK_THEME, INKSTONE_LIGHT_THEME, configureEditorColors, configureHighlighter, getEditorColors, getHighlighter, highlighterSources, inkstoneRadius, inkstoneShadow, onigurumaEngine, shikiThemeId, withEditorColors };
295
+ export { CODE_FONT_FAMILY, DEFAULT_HIGHLIGHTER_LANGS, INKSTONE_DARK_THEME, INKSTONE_LIGHT_THEME, configureEditorColors, configureHighlighter, getEditorColors, getHighlighter, highlighterSources, inkstoneRadius, inkstoneShadow, onigurumaEngine, shikiThemeId, subscribeEditorColors, withEditorColors };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coldsmirk/inkstone-core",
3
- "version": "0.35.0",
3
+ "version": "0.36.1",
4
4
  "description": "Shared Shiki highlighter service for the inkstone editor toolkit: one fine-grained singleton (catppuccin themes, the JS regex engine by default or Oniguruma on request) reused by Monaco, static rendering, and markdown surfaces.",
5
5
  "keywords": [
6
6
  "shiki",