yuhwa 1.3.0 → 1.4.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.
Files changed (57) hide show
  1. package/README.md +5 -3
  2. package/dist/astro.d.ts +1 -1
  3. package/dist/astro.js +4 -3
  4. package/dist/browser.d.ts +10 -4
  5. package/dist/browser.js +85 -22
  6. package/dist/canvas.d.ts +2 -0
  7. package/dist/canvas.js +40 -0
  8. package/dist/cli.js +485 -236
  9. package/dist/editor/assets/index-DWm3xV_v.js +689 -0
  10. package/dist/editor/assets/{lsp.worker-CMSpDTsX.js → lsp.worker-N6A9m364.js} +23 -17
  11. package/dist/editor/index.html +4 -1
  12. package/dist/{figure-QAwSU1Ry.d.ts → figure-BLHfxz_e.d.ts} +28 -5
  13. package/dist/{index-6ZPJGThY.d.ts → index-xVW43gqW.d.ts} +48 -3
  14. package/dist/index.d.ts +3 -3
  15. package/dist/index.js +5 -3
  16. package/dist/ironmark.d.ts +1 -1
  17. package/dist/ironmark.js +1 -1
  18. package/dist/lsp-browser.d.ts +2 -2
  19. package/dist/lsp-browser.js +2 -1
  20. package/dist/lsp-node.js +46 -26
  21. package/dist/lsp-server.d.ts +5 -1
  22. package/dist/lsp-server.js +1 -1
  23. package/dist/lsp.d.ts +1 -1
  24. package/dist/lsp.js +1 -1
  25. package/dist/markdown-it.d.ts +1 -1
  26. package/dist/markdown-it.js +5 -3
  27. package/dist/{node-CLu2Pp_2.js → node-CkrQnPFb.js} +90 -13
  28. package/dist/{plugin-Bs4eiAuz.js → parse--ljBMU60.js} +25 -307
  29. package/dist/plugin-CitzbblK.js +301 -0
  30. package/dist/{plugin-NSkNyoUj.d.ts → plugin-NMP_uf1a.d.ts} +15 -1
  31. package/dist/plugin.d.ts +1 -1
  32. package/dist/plugin.js +3 -1
  33. package/dist/raster-iqi6XRPU.js +299 -0
  34. package/dist/remark.d.ts +1 -1
  35. package/dist/remark.js +1 -1
  36. package/dist/rolldown-runtime-CEjnN5Q1.js +37 -0
  37. package/dist/{server-D6BSNC1P.js → server-BTYtmr6B.js} +41 -63
  38. package/dist/{server--jHjJjOS.d.ts → server-DMAOtqqK.d.ts} +1 -1
  39. package/dist/{service-D7DNWBAU.js → service-C60VdeKS.js} +34 -3
  40. package/dist/{service-Brvpt7Do.d.ts → service-DimTFsE3.d.ts} +2 -0
  41. package/dist/{shared-6aCU_HaV.js → shared-h8tkFLY4.js} +231 -21
  42. package/dist/{src-BjOhvTAB.js → src-NCXCTK_4.js} +81 -45
  43. package/dist/svg-uri-Dx8SGV-n.js +7 -0
  44. package/dist/vite.d.ts +1 -1
  45. package/dist/vite.js +3 -3
  46. package/dist/vuepress.d.ts +3 -2
  47. package/dist/vuepress.js +4 -2
  48. package/dist/watch-2VeW5DUN.js +35 -0
  49. package/dist/yuhwa.iife.js +55 -54
  50. package/dist/zoom-script-CaJ5MNUw.js +29 -0
  51. package/dist/{zoom-QeqB6QlH.js → zoom-yxhxX16c.js} +1 -1
  52. package/dist/zoom.d.ts +16 -7
  53. package/dist/zoom.js +2 -2
  54. package/package.json +19 -2
  55. package/skills/yuhwa/SKILL.md +12 -0
  56. package/dist/editor/assets/index-DAk6b1UB.js +0 -689
  57. package/dist/zoom-script-CVdAzMhX.js +0 -51
@@ -263,6 +263,7 @@ function ramp(tone, level) {
263
263
  }
264
264
  const SANS = "Inter, ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif";
265
265
  const MONO = "'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace";
266
+ const SERIF = "ui-serif, Georgia, Cambria, 'Times New Roman', Times, serif";
266
267
  const lightTone = (t) => RAMPS[t] && {
267
268
  fill: ramp(t, 50),
268
269
  stroke: ramp(t, 300),
@@ -404,15 +405,31 @@ const LINE_STYLES = [
404
405
  ];
405
406
  /** the group border dash for a line style; `fallback` when no style is set */
406
407
  const groupDash = (style, fallback) => style === "dashed" ? "6 4" : style === "dotted" ? "1.5 4" : style === "solid" ? null : fallback;
407
- /** a font setting (`mono`, `sans` or a family name) as a CSS font stack */
408
+ const FONT_KEYWORDS = {
409
+ sans: SANS,
410
+ serif: SERIF,
411
+ mono: MONO
412
+ };
413
+ const GENERIC_FONT = /^(serif|sans-serif|monospace|cursive|fantasy|math|emoji|system-ui|ui-(serif|sans-serif|monospace|rounded))$/i;
414
+ /** whether `family` is a CSS generic family such as `serif` or `system-ui` */
415
+ const isGenericFont = (family) => GENERIC_FONT.test(family);
416
+ /** the family names of a CSS font stack, unquoted */
417
+ const fontFamilies = (stack) => stack.split(",").map((f) => f.trim().replace(/^(['"])(.*)\1$/, "$2")).filter(Boolean);
418
+ /**
419
+ * a font setting as a CSS font stack: a family name or a fallback list such
420
+ * as `Lora, Georgia, serif`. `sans`, `serif` and `mono` expand to their
421
+ * stacks; a list that ends without a generic family gets one that matches
422
+ * the first font
423
+ */
408
424
  function fontStack(font) {
409
- const mono = font === "mono" || /mono|code|courier/i.test(font);
410
- if (font === "mono" || font === "sans") return {
411
- font: mono ? MONO : SANS,
412
- mono
413
- };
425
+ const names = font.split(",").map((f) => f.trim()).filter(Boolean);
426
+ const first = fontFamilies(font)[0] ?? "sans";
427
+ const mono = first === "mono" || /mono|code|courier/i.test(first);
428
+ const stack = names.map((f) => FONT_KEYWORDS[f] ?? f);
429
+ const last = names.at(-1) ?? "";
430
+ if (!FONT_KEYWORDS[last] && !isGenericFont(last)) stack.push(mono ? "ui-monospace, monospace" : "ui-sans-serif, system-ui, sans-serif");
414
431
  return {
415
- font: `${font}, ${mono ? "ui-monospace, monospace" : "ui-sans-serif, system-ui, sans-serif"}`,
432
+ font: stack.join(", "),
416
433
  mono
417
434
  };
418
435
  }
@@ -3864,303 +3881,4 @@ function parseLayoutLine(code, li, d, warn) {
3864
3881
  } else return warn(li, `Unknown layout keyword "${kw}"`);
3865
3882
  }
3866
3883
  }
3867
- // src/core/plugin.ts
3868
- /** what `options[key]` asks for: a select of values, or one kind of field */
3869
- function optionKind(o) {
3870
- return typeof o === "string" ? { kind: o } : {
3871
- kind: "select",
3872
- values: o
3873
- };
3874
- }
3875
- /**
3876
- * `fn` computed once per panel per layout pass: `measure`, `render` and
3877
- * `handles` share one ctx, so a plugin's geometry wrapped in this is worked
3878
- * out (and its text measured) once instead of three times.
3879
- */
3880
- function perPass(fn) {
3881
- const cache = /* @__PURE__ */ new WeakMap();
3882
- return (data, ctx) => {
3883
- const hit = cache.get(ctx);
3884
- if (hit && hit.data === data) return hit.g;
3885
- const g = fn(data, ctx);
3886
- cache.set(ctx, {
3887
- data,
3888
- g
3889
- });
3890
- return g;
3891
- };
3892
- }
3893
- /**
3894
- * an `editPart` for kinds whose parts only take a colour: `fn` gets the part
3895
- * id and the tone (a tone name or `#hex`; null removes it)
3896
- */
3897
- function recolor(fn) {
3898
- return (body, edit, data) => {
3899
- if (edit.op !== "set" || edit.key !== "color") return null;
3900
- const out = fn(body, edit.id, edit.value || null, data);
3901
- return out && {
3902
- body: out,
3903
- id: edit.id
3904
- };
3905
- };
3906
- }
3907
- /** everything registered through `use` */
3908
- const registry = {
3909
- plugins: [],
3910
- rows: [],
3911
- frames: /* @__PURE__ */ new Map(),
3912
- decorations: [],
3913
- panels: PANELS
3914
- };
3915
- const addAll = (list, items) => {
3916
- for (const i of items ?? []) if (!list.includes(i)) list.push(i);
3917
- };
3918
- /** marker names go in front of `none`, so the choices list the drawn ones first */
3919
- const addMarkers = (list, markers) => {
3920
- for (const m of markers ?? []) {
3921
- if (list.includes(m.name)) continue;
3922
- const none = list.indexOf("none");
3923
- list.splice(none < 0 ? list.length : none, 0, m.name);
3924
- }
3925
- };
3926
- const none = () => {};
3927
- const empty = () => "";
3928
- const zero = () => ({
3929
- w: 0,
3930
- h: 0
3931
- });
3932
- /** what each kind's functions return instead when they throw */
3933
- const FALLBACK = {
3934
- row: {
3935
- parse: () => null,
3936
- finish: none,
3937
- measure: zero,
3938
- render: empty
3939
- },
3940
- frame: {
3941
- inset: () => ({ top: 0 }),
3942
- render: empty
3943
- },
3944
- decoration: {
3945
- measure: () => null,
3946
- render: empty
3947
- },
3948
- shape: {
3949
- path: (b) => rectPath(b.x, b.y, b.w, b.h, 0),
3950
- size: (w, h) => ({
3951
- w,
3952
- h
3953
- }),
3954
- detail: () => null,
3955
- port: (b) => ({
3956
- x: b.x + b.w / 2,
3957
- y: b.y + b.h / 2
3958
- })
3959
- },
3960
- marker: {
3961
- render: empty,
3962
- inset: () => 0
3963
- },
3964
- panel: {
3965
- measure: zero,
3966
- render: empty,
3967
- handles: () => [],
3968
- parts: () => [],
3969
- editPart: () => null
3970
- },
3971
- hook: {
3972
- transform: none,
3973
- afterLayout: none,
3974
- postRender: (svg) => svg
3975
- }
3976
- };
3977
- /** `k` with each function named in `fb` wrapped by `guard` */
3978
- function wrap(plugin, what, k, fb) {
3979
- const out = { ...k };
3980
- for (const [m, f] of Object.entries(fb)) {
3981
- const fn = out[m];
3982
- if (typeof fn === "function") out[m] = guard(plugin, what(m), fn.bind(k), f);
3983
- }
3984
- return out;
3985
- }
3986
- /** copies of a plugin's parts whose every function reports a throw instead of passing it on */
3987
- function guarded(p) {
3988
- const n = p.name;
3989
- const kinds = (list, what, fb) => (list ?? []).map((k) => wrap(n, (m) => what(k, m), k, fb));
3990
- return {
3991
- rows: kinds(p.rows, (r) => `row "${r.kind}"`, FALLBACK.row),
3992
- frames: kinds(p.frames, (f) => `frame "${f.name}"`, FALLBACK.frame),
3993
- decorations: kinds(p.decorations, (d) => `decoration "${d.name}"`, FALLBACK.decoration),
3994
- shapes: kinds(p.shapes, (s) => `shape "${s.name}"`, FALLBACK.shape),
3995
- markers: kinds(p.markers, (k) => `marker "${k.name}"`, FALLBACK.marker),
3996
- panels: kinds(p.panels, (pk, m) => `${pk.name} panel ${m === "editPart" ? "edit" : m}`, FALLBACK.panel),
3997
- statements: (p.statements ?? []).map((st) => ({
3998
- ...st,
3999
- parse: guard(n, st.keyword, st.parse.bind(st), none, (msg, [, , ctx]) => ctx.warn(msg, "error"))
4000
- })),
4001
- hooks: wrap(n, (m) => m, {
4002
- transform: p.transform,
4003
- afterLayout: p.afterLayout,
4004
- postRender: p.postRender
4005
- }, FALLBACK.hook)
4006
- };
4007
- }
4008
- /** the tables as they were before the first plugin, so `unuse` can rebuild them */
4009
- let baseline;
4010
- function snapshot() {
4011
- const sets = [KNOWN_SETTINGS].map((s) => [s, [...s]]);
4012
- const maps = [
4013
- PANELS,
4014
- STATEMENTS,
4015
- SHAPE_KINDS,
4016
- MARKER_KINDS,
4017
- registry.frames
4018
- ].map((m) => [m, [...m]]);
4019
- const lists = [
4020
- TRANSFORMS,
4021
- AFTER_LAYOUT,
4022
- POST_RENDER,
4023
- NODE_PROPS,
4024
- EDGE_PROPS,
4025
- GROUP_PROPS,
4026
- BOOLS,
4027
- NUMBERS,
4028
- THEME_NAMES,
4029
- SHAPES,
4030
- registry.rows,
4031
- registry.decorations
4032
- ].map((l) => [l, [...l]]);
4033
- const enums = Object.entries(ENUMS).map(([k, v]) => [
4034
- k,
4035
- v,
4036
- [...v]
4037
- ]);
4038
- const themes = { ...THEMES };
4039
- const aliases = { ...SHAPE_ALIASES };
4040
- return () => {
4041
- for (const [s, v] of sets) {
4042
- s.clear();
4043
- for (const x of v) s.add(x);
4044
- }
4045
- for (const [m, v] of maps) {
4046
- m.clear();
4047
- for (const [k, x] of v) m.set(k, x);
4048
- }
4049
- for (const [l, v] of lists) l.splice(0, l.length, ...v);
4050
- for (const k of Object.keys(ENUMS)) delete ENUMS[k];
4051
- for (const [k, arr, v] of enums) {
4052
- arr.splice(0, arr.length, ...v);
4053
- ENUMS[k] = arr;
4054
- }
4055
- for (const k of Object.keys(THEMES)) delete THEMES[k];
4056
- Object.assign(THEMES, themes);
4057
- for (const k of Object.keys(SHAPE_ALIASES)) delete SHAPE_ALIASES[k];
4058
- Object.assign(SHAPE_ALIASES, aliases);
4059
- clearOwners();
4060
- };
4061
- }
4062
- /** `item` replaces the entry with the same name where it was (order matters for rows), or goes last */
4063
- function place(list, item, same) {
4064
- const at = list.findIndex(same);
4065
- if (at >= 0) list[at] = item;
4066
- else list.push(item);
4067
- }
4068
- /** Register plugins. Registering the same plugin name twice is a no-op. */
4069
- function use(...plugins) {
4070
- baseline ??= snapshot();
4071
- for (const p of plugins) {
4072
- if (registry.plugins.some((q) => q.name === p.name)) continue;
4073
- registry.plugins.push(p);
4074
- const w = guarded(p);
4075
- /** each item, under its name, into `into`: a map, or a function for more work */
4076
- const own = (what, items, key, into) => {
4077
- for (const x of items) {
4078
- const k = key(x);
4079
- claim(what, k, p.name);
4080
- if (typeof into === "function") into(x, k);
4081
- else into.set(k, x);
4082
- }
4083
- };
4084
- for (const t of p.themes ?? []) registerTheme(t);
4085
- own("row", w.rows, (r) => r.kind, (r, k) => place(registry.rows, r, (x) => x.kind === k));
4086
- own("decoration", w.decorations, (dec) => dec.name, (dec, k) => {
4087
- place(registry.decorations, dec, (x) => x.name === k);
4088
- KNOWN_SETTINGS.add(dec.setting);
4089
- });
4090
- own("frame", w.frames, (f) => f.name, registry.frames);
4091
- own("panel", w.panels, (pk) => pk.name, PANELS);
4092
- own("statement", w.statements, (st) => st.keyword, STATEMENTS);
4093
- own("marker", w.markers, (mk) => mk.name, MARKER_KINDS);
4094
- own("shape", w.shapes, (sh) => sh.name, (sh, k) => {
4095
- SHAPE_KINDS.set(k, sh);
4096
- if (!SHAPES.includes(k)) SHAPES.push(k);
4097
- for (const a of sh.aliases ?? []) SHAPE_ALIASES[a] = k;
4098
- });
4099
- addAll(ENUMS.shape ??= [], [...p.shapes?.map((x) => x.name) ?? [], ...p.shapes?.flatMap((x) => x.aliases ?? []) ?? []]);
4100
- addMarkers(ENUMS.head ??= [], p.markers);
4101
- if (ENUMS.tail !== ENUMS.head) addMarkers(ENUMS.tail ??= [], p.markers);
4102
- const { transform, afterLayout, postRender } = w.hooks;
4103
- if (transform) TRANSFORMS.push(transform);
4104
- if (afterLayout) AFTER_LAYOUT.push(afterLayout);
4105
- if (postRender) POST_RENDER.push(postRender);
4106
- for (const s of p.settings ?? []) KNOWN_SETTINGS.add(s);
4107
- addAll(NODE_PROPS, p.props?.node);
4108
- addAll(EDGE_PROPS, p.props?.edge);
4109
- addAll(GROUP_PROPS, p.props?.group);
4110
- for (const [k, v] of Object.entries(p.enums ?? {})) addAll(ENUMS[k] ??= [], v);
4111
- addAll(BOOLS, p.bools);
4112
- addAll(NUMBERS, p.numbers);
4113
- }
4114
- if (registry.frames.size) addAll(ENUMS.frame ??= [], [...registry.frames.keys()]);
4115
- }
4116
- /**
4117
- * Unregister a plugin by name: every table is rebuilt from the state before
4118
- * the first `use` and the other plugins are registered again, in order.
4119
- * Returns false when no plugin has that name.
4120
- */
4121
- function unuse(name) {
4122
- if (!baseline || !registry.plugins.some((p) => p.name === name)) return false;
4123
- const keep = registry.plugins.filter((p) => p.name !== name);
4124
- registry.plugins.length = 0;
4125
- baseline();
4126
- use(...keep);
4127
- return true;
4128
- }
4129
- // src/core/measure.ts
4130
- const NARROW = new Set("iljtfr.,;:'!|()[]{} \"`".split(""));
4131
- const WIDE = new Set("mwMW@%".split(""));
4132
- /** Font-independent width estimate, tuned for Inter / system UI fonts. */
4133
- const approxMeasure = (text, fontSize, weight, mono) => {
4134
- if (mono) return [...text].length * fontSize * .6;
4135
- let w = 0;
4136
- for (const ch of text) {
4137
- const cp = ch.codePointAt(0);
4138
- if (cp > 8192) w += cp > 126976 ? 1.2 : 1;
4139
- else if (NARROW.has(ch)) w += .3;
4140
- else if (WIDE.has(ch)) w += .86;
4141
- else if (ch >= "A" && ch <= "Z") w += .66;
4142
- else if (ch >= "0" && ch <= "9") w += .58;
4143
- else w += .54;
4144
- }
4145
- return w * fontSize * (weight >= 600 ? 1.05 : 1);
4146
- };
4147
- const MONO_STACK = "'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace";
4148
- let canvasCtx;
4149
- /** Accurate measurement using a canvas when running in a browser. */
4150
- function canvasMeasure(fontFamily) {
4151
- return (text, fontSize, weight, mono) => {
4152
- if (canvasCtx === void 0) try {
4153
- canvasCtx = typeof document !== "undefined" ? document.createElement("canvas").getContext("2d") : null;
4154
- } catch {
4155
- canvasCtx = null;
4156
- }
4157
- if (!canvasCtx) return approxMeasure(text, fontSize, weight, mono);
4158
- const family = mono && !/mono|courier|consolas|menlo/i.test(fontFamily) ? MONO_STACK : fontFamily;
4159
- canvasCtx.font = `${weight} ${fontSize}px ${family}`;
4160
- return canvasCtx.measureText(text).width;
4161
- };
4162
- }
4163
- // src/plugin.ts
4164
- /** `p` as it is, typed: no annotation needed for the kinds inside */
4165
- const definePlugin = (p) => p;
4166
- export { edgeWidthOf as $, table as $t, ENUMS as A, tspans as At, normalizeShape as B, createTheme as Bt, setLineProp as C, segsW as Ct, BOOLS as D, sumW as Dt, unquote as E, subInk as Et, PLACEMENT_PROPS as F, THEMES as Ft, sizeForShape as G, isSafeCss as Gt, rectPath as H, fontStack as Ht, lint as I, THEME_KEYS as It, POST_RENDER as J, ramp as Jt, textOffset as K, isSafeLink as Kt, placementOf as L, THEME_NAMES as Lt, NODE_PROPS as M, MONO as Mt, NUMBERS as N, RAMPS as Nt, COLORS as O, textEl as Ot, PANEL_PROPS as P, SANS as Pt, edgeFont as Q, collecting as Qt, propLine as R, TONES as Rt, serializeProps as S, richW as St, splitTrailingProps as T, splitWords as Tt, shapeDetail as U, getTheme as Ut, portOn as V, cssHex as Vt, shapePath as W, isColor as Wt, routeEdges as X, resolveColor as Xt, renderHead as Y, registerTheme as Yt, dashFor as Z, themeNames as Zt, onlyColor as _, parseInline as _t, perPass as a, resolveGroupStyle as at, propKey as b, renderSegs as bt, unuse as c, size as ct, PANELS as d, SPAN_KEYS as dt, effectiveProps as et, SETTING_VALUES as f, esc as ft, lex as g, nodeInk as gt, joinPath as h, lineHeight as ht, optionKind as i, posNum as it, GROUP_PROPS as j, COLOR_TOKEN as jt, EDGE_PROPS as k, textLines as kt, use as l, textFont as lt, isYuhwaPath as m, inkColor as mt, approxMeasure as n, num as nt, recolor as o, resolveNodeStyle as ot, blockBraces as p, f$1 as pt, AFTER_LAYOUT as q, isToneOrHex as qt, canvasMeasure as r, panelTheme as rt, registry as s, resolvePanelStyle as st, definePlugin as t, lineStyle as tt, KNOWN_SETTINGS as u, withEdgeInk as ut, parse as v, parseSpanStyle as vt, setTrailingProp as w, spanKey as wt, quote as x, richText as xt, parseProps as y, plain as yt, SHAPES as z, bool as zt };
3884
+ export { panelTheme as $, guard as $t, placementOf as A, SANS as At, textOffset as B, getTheme as Bt, ENUMS as C, sumW as Ct, PANEL_PROPS as D, COLOR_TOKEN as Dt, NUMBERS as E, tspans as Et, portOn as F, bool as Ft, renderHead as G, isToneOrHex as Gt, MARKER_KINDS as H, isGenericFont as Ht, rectPath as I, createTheme as It, edgeFont as J, resolveColor as Jt, routeEdges as K, ramp as Kt, shapeDetail as L, cssHex as Lt, SHAPES as M, THEME_KEYS as Mt, SHAPE_ALIASES as N, THEME_NAMES as Nt, PLACEMENT_PROPS as O, MONO as Ot, normalizeShape as P, TONES as Pt, num as Q, collecting as Qt, shapePath as R, fontFamilies as Rt, EDGE_PROPS as S, subInk as St, NODE_PROPS as T, textLines as Tt, POST_RENDER as U, isSafeCss as Ut, AFTER_LAYOUT as V, isColor as Vt, SHAPE_KINDS as W, isSafeLink as Wt, effectiveProps as X, claim as Xt, edgeWidthOf as Y, themeNames as Yt, lineStyle as Z, clearOwners as Zt, setTrailingProp as _, richText as _t, TRANSFORMS as a, textFont as at, BOOLS as b, spanKey as bt, joinPath as c, esc as ct, parse as d, lineHeight as dt, table as en, posNum as et, parseProps as f, nodeInk as ft, setLineProp as g, renderSegs as gt, serializeProps as h, plain as ht, STATEMENTS as i, size as it, propLine as j, THEMES as jt, lint as k, RAMPS as kt, lex as l, f$1 as lt, quote as m, parseSpanStyle as mt, PANELS as n, resolveNodeStyle as nt, blockBraces as o, withEdgeInk as ot, propKey as p, parseInline as pt, dashFor as q, registerTheme as qt, SETTING_VALUES as r, resolvePanelStyle as rt, isYuhwaPath as s, SPAN_KEYS as st, KNOWN_SETTINGS as t, resolveGroupStyle as tt, onlyColor as u, inkColor as ut, splitTrailingProps as v, richW as vt, GROUP_PROPS as w, textEl as wt, COLORS as x, splitWords as xt, unquote as y, segsW as yt, sizeForShape as z, fontStack as zt };
@@ -0,0 +1,301 @@
1
+ import { $t as guard, C as ENUMS, E as NUMBERS, H as MARKER_KINDS, I as rectPath, M as SHAPES, N as SHAPE_ALIASES, Nt as THEME_NAMES, S as EDGE_PROPS, T as NODE_PROPS, U as POST_RENDER, V as AFTER_LAYOUT, W as SHAPE_KINDS, Xt as claim, Zt as clearOwners, a as TRANSFORMS, b as BOOLS, i as STATEMENTS, jt as THEMES, n as PANELS, qt as registerTheme, t as KNOWN_SETTINGS, w as GROUP_PROPS } from "./parse--ljBMU60.js";
2
+ // src/core/plugin.ts
3
+ /** what `options[key]` asks for: a select of values, or one kind of field */
4
+ function optionKind(o) {
5
+ return typeof o === "string" ? { kind: o } : {
6
+ kind: "select",
7
+ values: o
8
+ };
9
+ }
10
+ /**
11
+ * `fn` computed once per panel per layout pass: `measure`, `render` and
12
+ * `handles` share one ctx, so a plugin's geometry wrapped in this is worked
13
+ * out (and its text measured) once instead of three times.
14
+ */
15
+ function perPass(fn) {
16
+ const cache = /* @__PURE__ */ new WeakMap();
17
+ return (data, ctx) => {
18
+ const hit = cache.get(ctx);
19
+ if (hit && hit.data === data) return hit.g;
20
+ const g = fn(data, ctx);
21
+ cache.set(ctx, {
22
+ data,
23
+ g
24
+ });
25
+ return g;
26
+ };
27
+ }
28
+ /**
29
+ * an `editPart` for kinds whose parts only take a colour: `fn` gets the part
30
+ * id and the tone (a tone name or `#hex`; null removes it)
31
+ */
32
+ function recolor(fn) {
33
+ return (body, edit, data) => {
34
+ if (edit.op !== "set" || edit.key !== "color") return null;
35
+ const out = fn(body, edit.id, edit.value || null, data);
36
+ return out && {
37
+ body: out,
38
+ id: edit.id
39
+ };
40
+ };
41
+ }
42
+ /** everything registered through `use` */
43
+ const registry = {
44
+ plugins: [],
45
+ rows: [],
46
+ frames: /* @__PURE__ */ new Map(),
47
+ decorations: [],
48
+ panels: PANELS
49
+ };
50
+ const addAll = (list, items) => {
51
+ for (const i of items ?? []) if (!list.includes(i)) list.push(i);
52
+ };
53
+ /** marker names go in front of `none`, so the choices list the drawn ones first */
54
+ const addMarkers = (list, markers) => {
55
+ for (const m of markers ?? []) {
56
+ if (list.includes(m.name)) continue;
57
+ const none = list.indexOf("none");
58
+ list.splice(none < 0 ? list.length : none, 0, m.name);
59
+ }
60
+ };
61
+ const none = () => {};
62
+ const empty = () => "";
63
+ const zero = () => ({
64
+ w: 0,
65
+ h: 0
66
+ });
67
+ /** what each kind's functions return instead when they throw */
68
+ const FALLBACK = {
69
+ row: {
70
+ parse: () => null,
71
+ finish: none,
72
+ measure: zero,
73
+ render: empty
74
+ },
75
+ frame: {
76
+ inset: () => ({ top: 0 }),
77
+ render: empty
78
+ },
79
+ decoration: {
80
+ measure: () => null,
81
+ render: empty
82
+ },
83
+ shape: {
84
+ path: (b) => rectPath(b.x, b.y, b.w, b.h, 0),
85
+ size: (w, h) => ({
86
+ w,
87
+ h
88
+ }),
89
+ detail: () => null,
90
+ port: (b) => ({
91
+ x: b.x + b.w / 2,
92
+ y: b.y + b.h / 2
93
+ })
94
+ },
95
+ marker: {
96
+ render: empty,
97
+ inset: () => 0
98
+ },
99
+ panel: {
100
+ measure: zero,
101
+ render: empty,
102
+ handles: () => [],
103
+ parts: () => [],
104
+ editPart: () => null
105
+ },
106
+ hook: {
107
+ transform: none,
108
+ afterLayout: none,
109
+ postRender: (svg) => svg
110
+ }
111
+ };
112
+ /** `k` with each function named in `fb` wrapped by `guard` */
113
+ function wrap(plugin, what, k, fb) {
114
+ const out = { ...k };
115
+ for (const [m, f] of Object.entries(fb)) {
116
+ const fn = out[m];
117
+ if (typeof fn === "function") out[m] = guard(plugin, what(m), fn.bind(k), f);
118
+ }
119
+ return out;
120
+ }
121
+ /** copies of a plugin's parts whose every function reports a throw instead of passing it on */
122
+ function guarded(p) {
123
+ const n = p.name;
124
+ const kinds = (list, what, fb) => (list ?? []).map((k) => wrap(n, (m) => what(k, m), k, fb));
125
+ return {
126
+ rows: kinds(p.rows, (r) => `row "${r.kind}"`, FALLBACK.row),
127
+ frames: kinds(p.frames, (f) => `frame "${f.name}"`, FALLBACK.frame),
128
+ decorations: kinds(p.decorations, (d) => `decoration "${d.name}"`, FALLBACK.decoration),
129
+ shapes: kinds(p.shapes, (s) => `shape "${s.name}"`, FALLBACK.shape),
130
+ markers: kinds(p.markers, (k) => `marker "${k.name}"`, FALLBACK.marker),
131
+ panels: kinds(p.panels, (pk, m) => `${pk.name} panel ${m === "editPart" ? "edit" : m}`, FALLBACK.panel),
132
+ statements: (p.statements ?? []).map((st) => ({
133
+ ...st,
134
+ parse: guard(n, st.keyword, st.parse.bind(st), none, (msg, [, , ctx]) => ctx.warn(msg, "error"))
135
+ })),
136
+ hooks: wrap(n, (m) => m, {
137
+ transform: p.transform,
138
+ afterLayout: p.afterLayout,
139
+ postRender: p.postRender
140
+ }, FALLBACK.hook)
141
+ };
142
+ }
143
+ /** the tables as they were before the first plugin, so `unuse` can rebuild them */
144
+ let baseline;
145
+ function snapshot() {
146
+ const sets = [KNOWN_SETTINGS].map((s) => [s, [...s]]);
147
+ const maps = [
148
+ PANELS,
149
+ STATEMENTS,
150
+ SHAPE_KINDS,
151
+ MARKER_KINDS,
152
+ registry.frames
153
+ ].map((m) => [m, [...m]]);
154
+ const lists = [
155
+ TRANSFORMS,
156
+ AFTER_LAYOUT,
157
+ POST_RENDER,
158
+ NODE_PROPS,
159
+ EDGE_PROPS,
160
+ GROUP_PROPS,
161
+ BOOLS,
162
+ NUMBERS,
163
+ THEME_NAMES,
164
+ SHAPES,
165
+ registry.rows,
166
+ registry.decorations
167
+ ].map((l) => [l, [...l]]);
168
+ const enums = Object.entries(ENUMS).map(([k, v]) => [
169
+ k,
170
+ v,
171
+ [...v]
172
+ ]);
173
+ const themes = { ...THEMES };
174
+ const aliases = { ...SHAPE_ALIASES };
175
+ return () => {
176
+ for (const [s, v] of sets) {
177
+ s.clear();
178
+ for (const x of v) s.add(x);
179
+ }
180
+ for (const [m, v] of maps) {
181
+ m.clear();
182
+ for (const [k, x] of v) m.set(k, x);
183
+ }
184
+ for (const [l, v] of lists) l.splice(0, l.length, ...v);
185
+ for (const k of Object.keys(ENUMS)) delete ENUMS[k];
186
+ for (const [k, arr, v] of enums) {
187
+ arr.splice(0, arr.length, ...v);
188
+ ENUMS[k] = arr;
189
+ }
190
+ for (const k of Object.keys(THEMES)) delete THEMES[k];
191
+ Object.assign(THEMES, themes);
192
+ for (const k of Object.keys(SHAPE_ALIASES)) delete SHAPE_ALIASES[k];
193
+ Object.assign(SHAPE_ALIASES, aliases);
194
+ clearOwners();
195
+ };
196
+ }
197
+ /** `item` replaces the entry with the same name where it was (order matters for rows), or goes last */
198
+ function place(list, item, same) {
199
+ const at = list.findIndex(same);
200
+ if (at >= 0) list[at] = item;
201
+ else list.push(item);
202
+ }
203
+ /** Register plugins. Registering the same plugin name twice is a no-op. */
204
+ function use(...plugins) {
205
+ baseline ??= snapshot();
206
+ for (const p of plugins) {
207
+ if (registry.plugins.some((q) => q.name === p.name)) continue;
208
+ registry.plugins.push(p);
209
+ const w = guarded(p);
210
+ /** each item, under its name, into `into`: a map, or a function for more work */
211
+ const own = (what, items, key, into) => {
212
+ for (const x of items) {
213
+ const k = key(x);
214
+ claim(what, k, p.name);
215
+ if (typeof into === "function") into(x, k);
216
+ else into.set(k, x);
217
+ }
218
+ };
219
+ for (const t of p.themes ?? []) registerTheme(t);
220
+ own("row", w.rows, (r) => r.kind, (r, k) => place(registry.rows, r, (x) => x.kind === k));
221
+ own("decoration", w.decorations, (dec) => dec.name, (dec, k) => {
222
+ place(registry.decorations, dec, (x) => x.name === k);
223
+ KNOWN_SETTINGS.add(dec.setting);
224
+ });
225
+ own("frame", w.frames, (f) => f.name, registry.frames);
226
+ own("panel", w.panels, (pk) => pk.name, PANELS);
227
+ own("statement", w.statements, (st) => st.keyword, STATEMENTS);
228
+ own("marker", w.markers, (mk) => mk.name, MARKER_KINDS);
229
+ own("shape", w.shapes, (sh) => sh.name, (sh, k) => {
230
+ SHAPE_KINDS.set(k, sh);
231
+ if (!SHAPES.includes(k)) SHAPES.push(k);
232
+ for (const a of sh.aliases ?? []) SHAPE_ALIASES[a] = k;
233
+ });
234
+ addAll(ENUMS.shape ??= [], [...p.shapes?.map((x) => x.name) ?? [], ...p.shapes?.flatMap((x) => x.aliases ?? []) ?? []]);
235
+ addMarkers(ENUMS.head ??= [], p.markers);
236
+ if (ENUMS.tail !== ENUMS.head) addMarkers(ENUMS.tail ??= [], p.markers);
237
+ const { transform, afterLayout, postRender } = w.hooks;
238
+ if (transform) TRANSFORMS.push(transform);
239
+ if (afterLayout) AFTER_LAYOUT.push(afterLayout);
240
+ if (postRender) POST_RENDER.push(postRender);
241
+ for (const s of p.settings ?? []) KNOWN_SETTINGS.add(s);
242
+ addAll(NODE_PROPS, p.props?.node);
243
+ addAll(EDGE_PROPS, p.props?.edge);
244
+ addAll(GROUP_PROPS, p.props?.group);
245
+ for (const [k, v] of Object.entries(p.enums ?? {})) addAll(ENUMS[k] ??= [], v);
246
+ addAll(BOOLS, p.bools);
247
+ addAll(NUMBERS, p.numbers);
248
+ }
249
+ if (registry.frames.size) addAll(ENUMS.frame ??= [], [...registry.frames.keys()]);
250
+ }
251
+ /**
252
+ * Unregister a plugin by name: every table is rebuilt from the state before
253
+ * the first `use` and the other plugins are registered again, in order.
254
+ * Returns false when no plugin has that name.
255
+ */
256
+ function unuse(name) {
257
+ if (!baseline || !registry.plugins.some((p) => p.name === name)) return false;
258
+ const keep = registry.plugins.filter((p) => p.name !== name);
259
+ registry.plugins.length = 0;
260
+ baseline();
261
+ use(...keep);
262
+ return true;
263
+ }
264
+ // src/core/measure.ts
265
+ const NARROW = new Set("iljtfr.,;:'!|()[]{} \"`".split(""));
266
+ const WIDE = new Set("mwMW@%".split(""));
267
+ /** Font-independent width estimate, tuned for Inter / system UI fonts. */
268
+ const approxMeasure = (text, fontSize, weight, mono) => {
269
+ if (mono) return [...text].length * fontSize * .6;
270
+ let w = 0;
271
+ for (const ch of text) {
272
+ const cp = ch.codePointAt(0);
273
+ if (cp > 8192) w += cp > 126976 ? 1.2 : 1;
274
+ else if (NARROW.has(ch)) w += .3;
275
+ else if (WIDE.has(ch)) w += .86;
276
+ else if (ch >= "A" && ch <= "Z") w += .66;
277
+ else if (ch >= "0" && ch <= "9") w += .58;
278
+ else w += .54;
279
+ }
280
+ return w * fontSize * (weight >= 600 ? 1.05 : 1);
281
+ };
282
+ const MONO_STACK = "'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace";
283
+ let canvasCtx;
284
+ /** Accurate measurement using a canvas when running in a browser. */
285
+ function canvasMeasure(fontFamily) {
286
+ return (text, fontSize, weight, mono) => {
287
+ if (canvasCtx === void 0) try {
288
+ canvasCtx = typeof document !== "undefined" ? document.createElement("canvas").getContext("2d") : null;
289
+ } catch {
290
+ canvasCtx = null;
291
+ }
292
+ if (!canvasCtx) return approxMeasure(text, fontSize, weight, mono);
293
+ const family = mono && !/mono|courier|consolas|menlo/i.test(fontFamily) ? MONO_STACK : fontFamily;
294
+ canvasCtx.font = `${weight} ${fontSize}px ${family}`;
295
+ return canvasCtx.measureText(text).width;
296
+ };
297
+ }
298
+ // src/plugin.ts
299
+ /** `p` as it is, typed: no annotation needed for the kinds inside */
300
+ const definePlugin = (p) => p;
301
+ export { perPass as a, unuse as c, optionKind as i, use as l, approxMeasure as n, recolor as o, canvasMeasure as r, registry as s, definePlugin as t };
@@ -83,6 +83,20 @@ declare function cssHex(v: string): string | undefined;
83
83
  /** a `true` / `false` prop value: `false`, `no`, `off`, `0` and `none` are false */
84
84
  declare const bool: (v: string | undefined, d: boolean) => boolean;
85
85
  type LineStyle$1 = "solid" | "dashed" | "dotted";
86
+ /** whether `family` is a CSS generic family such as `serif` or `system-ui` */
87
+ declare const isGenericFont: (family: string) => boolean;
88
+ /** the family names of a CSS font stack, unquoted */
89
+ declare const fontFamilies: (stack: string) => string[];
90
+ /**
91
+ * a font setting as a CSS font stack: a family name or a fallback list such
92
+ * as `Lora, Georgia, serif`. `sans`, `serif` and `mono` expand to their
93
+ * stacks; a list that ends without a generic family gets one that matches
94
+ * the first font
95
+ */
96
+ declare function fontStack(font: string): {
97
+ font: string;
98
+ mono: boolean;
99
+ };
86
100
  /**
87
101
  * A key of a `theme name { … }` block: what it sets, the kind of value it
88
102
  * takes, and the words it offers besides colours (`none`) or instead of them.
@@ -1026,4 +1040,4 @@ declare function resolveNodeStyle(d: Diagram, n: NodeDecl, p: Props, theme: Them
1026
1040
  declare const table: <T>(o: Record<string, T>) => Record<string, T>;
1027
1041
  /** `p` as it is, typed: no annotation needed for the kinds inside */
1028
1042
  declare const definePlugin: <P extends YuhwaPlugin>(p: P) => P;
1029
- export { PanelDecl as $, SANS as $t, setLineProp as A, registry as At, EdgeDecl as B, inkColor as Bt, isYuhwaPath as C, RowKind as Ct, parse as D, optionKind as Dt, onlyColor as E, YuhwaPlugin as Et, Component as F, renderHead as Ft, LaidEdge as G, segsW as Gt, GroupDecl as H, parseInline as Ht, Curve as I, InkColors as It, LaidPanel as J, tspans as Jt, LaidGroup as K, sumW as Kt, Diagnostic as L, Seg as Lt, splitTrailingProps as M, use as Mt, unquote as N, MarkerKind as Nt, parseProps as O, perPass as Ot, Box as P, ShapeKind as Pt, NodeOverride as Q, RAMPS as Qt, Diagram as R, esc as Rt, blockBraces as S, RowCtx as St, lex as T, StatementKind as Tt, HeadKind as U, plain as Ut, EdgeOverride as V, nodeInk as Vt, LabelRef as W, renderSegs as Wt, LayoutOverrides as X, LineStyle$1 as Xt, Layout as Y, COLOR_TOKEN as Yt, NodeDecl as Z, MONO as Zt, PANELS as _, themeNames as _n, Decoration as _t, effectiveProps as a, ThemeInput as an, PartEdit as at, Tok as b, canvasMeasure as bn, PanelKind as bt, posNum as c, cssHex as cn, ResolvedEdgeStyle as ct, normalizeShape as d, isSafeCss as dn, Side as dt, THEMES as en, PanelHandle as et, portOn as f, isSafeLink as fn, SourceRef as ft, LexedLine as g, resolveColor as gn, layout as gt, KNOWN_SETTINGS as h, registerTheme as hn, groupPath as ht, edgeWidthOf as i, Theme as in, PartChange as it, setTrailingProp as j, unuse as jt, serializeProps as k, recolor as kt, resolveNodeStyle as l, getTheme as ln, ResolvedNodeStyle as lt, shapePath as m, ramp as mn, LayoutOptions as mt, table as n, THEME_NAMES as nn, PanelPart as nt, lineStyle as o, bool as on, Point as ot, rectPath as p, isToneOrHex as pn, GRID as pt, LaidNode as q, textEl as qt, dashFor as r, TONES as rn, PartAction as rt, num as s, createTheme as sn, Props as st, definePlugin as t, THEME_KEYS as tn, PanelOverride as tt, SHAPES as u, isColor as un, ShapeName as ut, ParseOptions as v, Measure as vn, FrameKind as vt, joinPath as w, RowPaint as wt, TokKind as x, PanelOption as xt, SETTING_VALUES as y, approxMeasure as yn, PanelCtx as yt, Direction as z, f as zt };
1043
+ export { PanelDecl as $, SANS as $t, setLineProp as A, registry as At, EdgeDecl as B, inkColor as Bt, isYuhwaPath as C, canvasMeasure as Cn, RowKind as Ct, parse as D, optionKind as Dt, onlyColor as E, YuhwaPlugin as Et, Component as F, renderHead as Ft, LaidEdge as G, segsW as Gt, GroupDecl as H, parseInline as Ht, Curve as I, InkColors as It, LaidPanel as J, tspans as Jt, LaidGroup as K, sumW as Kt, Diagnostic as L, Seg as Lt, splitTrailingProps as M, use as Mt, unquote as N, MarkerKind as Nt, parseProps as O, perPass as Ot, Box as P, ShapeKind as Pt, NodeOverride as Q, RAMPS as Qt, Diagram as R, esc as Rt, blockBraces as S, approxMeasure as Sn, RowCtx as St, lex as T, StatementKind as Tt, HeadKind as U, plain as Ut, EdgeOverride as V, nodeInk as Vt, LabelRef as W, renderSegs as Wt, LayoutOverrides as X, LineStyle$1 as Xt, Layout as Y, COLOR_TOKEN as Yt, NodeDecl as Z, MONO as Zt, PANELS as _, ramp as _n, Decoration as _t, effectiveProps as a, ThemeInput as an, PartEdit as at, Tok as b, themeNames as bn, PanelKind as bt, posNum as c, cssHex as cn, ResolvedEdgeStyle as ct, normalizeShape as d, getTheme as dn, Side as dt, THEMES as en, PanelHandle as et, portOn as f, isColor as fn, SourceRef as ft, LexedLine as g, isToneOrHex as gn, layout as gt, KNOWN_SETTINGS as h, isSafeLink as hn, groupPath as ht, edgeWidthOf as i, Theme as in, PartChange as it, setTrailingProp as j, unuse as jt, serializeProps as k, recolor as kt, resolveNodeStyle as l, fontFamilies as ln, ResolvedNodeStyle as lt, shapePath as m, isSafeCss as mn, LayoutOptions as mt, table as n, THEME_NAMES as nn, PanelPart as nt, lineStyle as o, bool as on, Point as ot, rectPath as p, isGenericFont as pn, GRID as pt, LaidNode as q, textEl as qt, dashFor as r, TONES as rn, PartAction as rt, num as s, createTheme as sn, Props as st, definePlugin as t, THEME_KEYS as tn, PanelOverride as tt, SHAPES as u, fontStack as un, ShapeName as ut, ParseOptions as v, registerTheme as vn, FrameKind as vt, joinPath as w, RowPaint as wt, TokKind as x, Measure as xn, PanelOption as xt, SETTING_VALUES as y, resolveColor as yn, PanelCtx as yt, Direction as z, f as zt };