@cardenelabs/dragon 0.45.5 → 0.46.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.
Files changed (72) hide show
  1. package/README.ja.md +331 -0
  2. package/README.md +200 -188
  3. package/dist/cli.cjs +148 -111
  4. package/dist/cli.cjs.map +1 -1
  5. package/dist/cli.js +148 -111
  6. package/dist/cli.js.map +1 -1
  7. package/dist/export.cjs +139 -109
  8. package/dist/export.cjs.map +1 -1
  9. package/dist/export.js +139 -109
  10. package/dist/export.js.map +1 -1
  11. package/dist/index.cjs +99 -62
  12. package/dist/index.cjs.map +1 -1
  13. package/dist/index.js +99 -62
  14. package/dist/index.js.map +1 -1
  15. package/dist/light/chunk-CLS6JVKK.cjs.map +1 -1
  16. package/dist/light/{chunk-EHTQUCW4.js → chunk-DVGDEPCV.js} +6 -2
  17. package/dist/light/chunk-DVGDEPCV.js.map +1 -0
  18. package/dist/light/{chunk-5OQV42W3.cjs → chunk-SUL4AJLI.cjs} +6 -2
  19. package/dist/light/chunk-SUL4AJLI.cjs.map +1 -0
  20. package/dist/light/{chunk-JCEKXALJ.cjs → chunk-V34DCH34.cjs} +90 -87
  21. package/dist/light/chunk-V34DCH34.cjs.map +1 -0
  22. package/dist/light/{chunk-YEQSLVIN.js → chunk-YO3UBEC3.js} +12 -9
  23. package/dist/light/chunk-YO3UBEC3.js.map +1 -0
  24. package/dist/light/{git-K3KAAAWW.cjs → git-WBGFDRBD.cjs} +59 -59
  25. package/dist/light/git-WBGFDRBD.cjs.map +1 -0
  26. package/dist/light/{git-GESUYKGR.js → git-WEJL6VAY.js} +3 -3
  27. package/dist/light/index.cjs +407 -375
  28. package/dist/light/index.cjs.map +1 -1
  29. package/dist/light/index.js +92 -60
  30. package/dist/light/index.js.map +1 -1
  31. package/dist/light/{lineup-2V2U7HZH.js → lineup-6LN6Z5T2.js} +3 -3
  32. package/dist/light/{lineup-623J25LU.cjs → lineup-WLGNGTFX.cjs} +40 -40
  33. package/dist/light/lineup-WLGNGTFX.cjs.map +1 -0
  34. package/dist/light/{state-T4OTVING.js → state-DFYTMQI7.js} +2 -2
  35. package/dist/light/{state-OZVCHX7H.cjs → state-JVAH4TSM.cjs} +41 -41
  36. package/dist/light/state-JVAH4TSM.cjs.map +1 -0
  37. package/dist/light/{time-KVMZIKYF.cjs → time-EXRPI34B.cjs} +84 -84
  38. package/dist/light/time-EXRPI34B.cjs.map +1 -0
  39. package/dist/light/{time-O4CMTUV5.js → time-M3EGEFDU.js} +3 -3
  40. package/dist/light/{tree-6D7KI57H.cjs → tree-DECHTXFU.cjs} +62 -62
  41. package/dist/light/tree-DECHTXFU.cjs.map +1 -0
  42. package/dist/light/{tree-ZJZQI5QT.js → tree-MFU54QKY.js} +3 -3
  43. package/dist/node.cjs +115 -49
  44. package/dist/node.cjs.map +1 -1
  45. package/dist/node.js +115 -49
  46. package/dist/node.js.map +1 -1
  47. package/dist/player.global.js +3 -3
  48. package/package.json +1 -1
  49. package/src/field-messages.ts +4 -0
  50. package/src/look/values.ts +5 -1
  51. package/src/messages.ts +11 -1
  52. package/src/nearest.ts +37 -0
  53. package/src/parser.ts +4 -2
  54. package/src/render/draw.ts +2 -1
  55. package/src/render/parts.ts +28 -7
  56. package/src/schema.ts +27 -3
  57. package/src/schemas/diagram.schema.json +3 -2
  58. package/src/schemas/validate-diagram.js +1 -1
  59. package/dist/light/chunk-5OQV42W3.cjs.map +0 -1
  60. package/dist/light/chunk-EHTQUCW4.js.map +0 -1
  61. package/dist/light/chunk-JCEKXALJ.cjs.map +0 -1
  62. package/dist/light/chunk-YEQSLVIN.js.map +0 -1
  63. package/dist/light/git-K3KAAAWW.cjs.map +0 -1
  64. package/dist/light/lineup-623J25LU.cjs.map +0 -1
  65. package/dist/light/state-OZVCHX7H.cjs.map +0 -1
  66. package/dist/light/time-KVMZIKYF.cjs.map +0 -1
  67. package/dist/light/tree-6D7KI57H.cjs.map +0 -1
  68. /package/dist/light/{git-GESUYKGR.js.map → git-WEJL6VAY.js.map} +0 -0
  69. /package/dist/light/{lineup-2V2U7HZH.js.map → lineup-6LN6Z5T2.js.map} +0 -0
  70. /package/dist/light/{state-T4OTVING.js.map → state-DFYTMQI7.js.map} +0 -0
  71. /package/dist/light/{time-O4CMTUV5.js.map → time-M3EGEFDU.js.map} +0 -0
  72. /package/dist/light/{tree-ZJZQI5QT.js.map → tree-MFU54QKY.js.map} +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cardenelabs/dragon",
3
- "version": "0.45.5",
3
+ "version": "0.46.1",
4
4
  "description": "Dragon — 動く図を、人は行ごとの記法で、AI は JSON で書く。時系列図と構成図を段ごとに動かし、mp4・GIF・HTML・PNG に書き出す。",
5
5
  "license": "MIT",
6
6
  "author": "cardene777",
@@ -23,6 +23,8 @@ export interface FieldMessages {
23
23
  additionalProperties: (key: string) => string;
24
24
  required: (key: string) => string;
25
25
  enum: (values: readonly string[]) => string;
26
+ /** 知らないアイコン名 `value` と、それに一番近い名前 `near` (#3315) */
27
+ unknownIcon: (value: string, near: string) => string;
26
28
  type: (type: string) => string;
27
29
  minLength: (limit: number) => string;
28
30
  maxLength: (limit: number) => string;
@@ -80,6 +82,7 @@ const ja: FieldMessages = {
80
82
  additionalProperties: (key) => `${key} という欄は書けない`,
81
83
  required: (key) => `${key} の欄が要る`,
82
84
  enum: (values) => `${values.join(" / ")} のどれかにする`,
85
+ unknownIcon: (value, near) => `${value} という icon は無い。近い名前は ${near}`,
83
86
  type: (type) => TYPES_JA[type] ?? `${type} にする`,
84
87
  minLength: (limit) => `${limit} 字以上書く`,
85
88
  maxLength: (limit) => `${limit} 字までにする`,
@@ -107,6 +110,7 @@ const en: FieldMessages = {
107
110
  additionalProperties: (key) => `The ${key} field is not allowed`,
108
111
  required: (key) => `The ${key} field is required`,
109
112
  enum: (values) => `Use one of ${values.join(" / ")}`,
113
+ unknownIcon: (value, near) => `There is no icon named ${value}. The closest is ${near}`,
110
114
  type: (type) => TYPES_EN[type] ?? `Use ${type}`,
111
115
  minLength: (limit) => `Write at least ${plural(limit, "character")}`,
112
116
  maxLength: (limit) => `Write at most ${plural(limit, "character")}`,
@@ -149,7 +149,11 @@ export const LOOK = {
149
149
  gap: 10,
150
150
  /** 図の外周の余白 */
151
151
  margin: 14,
152
- box: { h: 72, minW: 92, padX: 12, radius: 12, icon: 24, iconRadius: 7, iconY: -16, iconScale: 0.74, nameY: 12, subY: 27 },
152
+ /**
153
+ * 箱。`iconY` と `nameY` と `subY` は箱の真ん中から絵柄の真ん中と名前と下の字の基準線まで。
154
+ * `nameMid` と `subMid` はアイコンも置き場の枠も持つ箱が無い図の名前と下の字の基準線で、名前を箱の真ん中に置き、下の字を名前から `subY - nameY` と同じ間で置く (#3307、#3309)
155
+ */
156
+ box: { h: 72, minW: 92, padX: 12, radius: 12, icon: 24, iconRadius: 7, iconY: -16, iconScale: 0.74, nameY: 12, nameMid: 5, subY: 27, subMid: 20 },
153
157
  region: { pad: 18, gap: 10, radius: 14, inset: 14, icon: 24, iconRadius: 7, iconScale: 0.68, nameY: 58, line: 16, minH: 108 },
154
158
  architecture: { columns: 4, columnGap: 56, maxColumnGap: 160, rowGap: 44, labelRoom: 16 },
155
159
  /**
package/src/messages.ts CHANGED
@@ -4,7 +4,8 @@
4
4
  * 記法の語は英語だけで file から言葉が決まらないため、誤りの文は呼ぶ側が渡した言葉
5
5
  * (渡さなければ日本語) で出す。文の中の記法の語は語の表から引き、表と文で語が食い違わないようにする。
6
6
  */
7
- import { COLORS } from "./schema";
7
+ import { nearest } from "./nearest";
8
+ import { COLORS, ICON_NAMES } from "./schema";
8
9
  import { CARRIES, DURATION_UNITS, HEADS, KINDS, type NotationLang, SIDES, SPAN_SHAPES, VIEWS } from "./words";
9
10
 
10
11
  /** 新しい形のどの行の書き方を示すか。前の形の行に当たった時に、誤りの文が書き方を示す */
@@ -57,6 +58,8 @@ export interface Messages {
57
58
  attrTwice: (key: string) => string;
58
59
  badAttr: (key: string, allowed: readonly string[]) => string;
59
60
  badColor: string;
61
+ /** 知らないアイコン名 `value`。近い名前が在ればそれだけを、無ければ使える名前を全て挙げる (#3315) */
62
+ badIcon: (value: string) => string;
60
63
  badView: string;
61
64
  badCarry: string;
62
65
  badSide: string;
@@ -162,6 +165,11 @@ export interface Messages {
162
165
  }
163
166
 
164
167
  const list = (words: readonly string[]): string => words.join(" / ");
168
+ /** 書いたアイコン名に近い名前が在れば `near` の文、無ければ `none` の文 (#3315) */
169
+ const withNear = (value: string, near: (name: string) => string, none: string): string => {
170
+ const name = nearest(value, ICON_NAMES);
171
+ return name === undefined ? none : near(name);
172
+ };
165
173
 
166
174
  const ja = (): Messages => {
167
175
  const shapes: Record<Shape, string> = {
@@ -223,6 +231,7 @@ const ja = (): Messages => {
223
231
  attrTwice: (key) => `${key} は 1 つの行に 1 つまで`,
224
232
  badAttr: (key, allowed) => `${key} はこの行に書けない属性。書けるのは ${list(allowed)}`,
225
233
  badColor: `color は ${list(COLORS)} のどれか`,
234
+ badIcon: (value) => withNear(value, (near) => `${value} という icon は無い。近い名前は ${near}`, `icon は ${list(ICON_NAMES)} のどれか`),
226
235
  badView: `view は ${list(VIEWS)} のどれか`,
227
236
  badCarry: `carry は ${list(CARRIES)} のどれか。札は tag: <値> で書く`,
228
237
  badSide: `side は ${list(SIDES)} のどれか`,
@@ -362,6 +371,7 @@ const en = (): Messages => {
362
371
  attrTwice: (key) => `Write ${key} only once on a line`,
363
372
  badAttr: (key, allowed) => `${key} cannot be written on this line. Write ${list(allowed)}`,
364
373
  badColor: `color is one of ${list(COLORS)}`,
374
+ badIcon: (value) => withNear(value, (near) => `There is no icon named ${value}. The closest is ${near}`, `icon is one of ${list(ICON_NAMES)}`),
365
375
  badView: `view is one of ${list(VIEWS)}`,
366
376
  badCarry: `carry is one of ${list(CARRIES)}. Write a tag as tag: <value>`,
367
377
  badSide: `side is one of ${list(SIDES)}`,
package/src/nearest.ts ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * 書いた語に一番近い語 (#3315)。誤りの文で、書き間違えた語の代わりに 1 つだけ候補を示す。
3
+ *
4
+ * 大文字と小文字を区別しない編集距離 (1 字の足し、消し、置き換えを 1 と数える) で比べる。
5
+ * 距離が 2 以下で、書いた語の字数より小さい時だけ近いとみなす。
6
+ * 字数より小さいことを求めるのは、1 字の語がどの 1 字の語とも距離 1 になり、何を書いても候補が出るのを避けるため。
7
+ * 同じ距離の語が 2 つ以上ある時は `candidates` の並びで先の語を返し、近い語が無ければ `undefined` を返す。
8
+ */
9
+ const MAX_DISTANCE = 2;
10
+
11
+ export function nearest(word: string, candidates: readonly string[]): string | undefined {
12
+ const written = [...word.toLowerCase()];
13
+ let limit = Math.min(MAX_DISTANCE, written.length - 1);
14
+ let best: string | undefined;
15
+ for (const candidate of candidates) {
16
+ const d = distance(written, [...candidate.toLowerCase()]);
17
+ if (d > limit) continue;
18
+ best = candidate;
19
+ // 後の語は、今の語より近い時だけ置き換える
20
+ limit = d - 1;
21
+ }
22
+ return best;
23
+ }
24
+
25
+ /** 2 つの字の並びの編集距離 */
26
+ function distance(a: readonly string[], b: readonly string[]): number {
27
+ let previous = Array.from({ length: b.length + 1 }, (_, j) => j);
28
+ for (let i = 1; i <= a.length; i++) {
29
+ const current = [i];
30
+ for (let j = 1; j <= b.length; j++) {
31
+ const replace = (previous[j - 1] as number) + (a[i - 1] === b[j - 1] ? 0 : 1);
32
+ current.push(Math.min((previous[j] as number) + 1, (current[j - 1] as number) + 1, replace));
33
+ }
34
+ previous = current;
35
+ }
36
+ return previous[b.length] as number;
37
+ }
package/src/parser.ts CHANGED
@@ -73,6 +73,7 @@ import {
73
73
  type DiagramEvent,
74
74
  type GitBranch,
75
75
  type GitCommit,
76
+ ICON_NAMES,
76
77
  type LabelSide,
77
78
  type LineupItem,
78
79
  type Region,
@@ -324,12 +325,13 @@ function readDeclared(source: SourceLine, word: string, allowed: readonly string
324
325
  function lookOf(source: SourceLine, attrs: Map<string, Attr>, m: Messages): { note?: string; color?: Color; icon?: string } {
325
326
  const color = attrs.get("color");
326
327
  if (color && !isOneOf(COLORS, color.value)) throw fail(source, color.at, m.badColor);
328
+ const icon = attrs.get("icon");
329
+ if (icon && !ICON_NAMES.includes(icon.value)) throw fail(source, icon.at, m.badIcon(icon.value));
327
330
  const note = attrs.get("note")?.value;
328
- const icon = attrs.get("icon")?.value;
329
331
  return {
330
332
  ...(note !== undefined ? { note } : {}),
331
333
  ...(color ? { color: color.value as Color } : {}),
332
- ...(icon !== undefined ? { icon } : {}),
334
+ ...(icon ? { icon: icon.value } : {}),
333
335
  };
334
336
  }
335
337
 
@@ -27,6 +27,7 @@ import {
27
27
  action,
28
28
  badge,
29
29
  band,
30
+ boxTextY,
30
31
  card,
31
32
  carrier,
32
33
  type Ctx,
@@ -371,7 +372,7 @@ export function drawSvg(diagram: Diagram, layout: Layout, timeline: Timeline, pa
371
372
  const vars = paletteVars(palette, theme);
372
373
  const id = nameOf(`${JSON.stringify(diagram)}|${vars}|${follow ? `${follow.width}|${follow.lang}` : ""}`);
373
374
  const m = new Motion(`dg${id}`, timeline.total, timeline.last);
374
- const ctx: Ctx = { m, t: timeline, id, sh: `filter="url(#dg-sh-${id})"`, end: (timeline.steps[timeline.steps.length - 1] as Span).end, ink, traces };
375
+ const ctx: Ctx = { m, t: timeline, id, sh: `filter="url(#dg-sh-${id})"`, end: (timeline.steps[timeline.steps.length - 1] as Span).end, ink, traces, boxText: boxTextY(layout.boxes) };
375
376
  const on = involved(diagram);
376
377
  // 時系列図の処理は生存線の上の札に出し、他の図の種類は箱の下の字に出す
377
378
  const subs = subChanges(diagram, timeline, layout.kind !== "sequence");
@@ -11,6 +11,7 @@ import {
11
11
  markText,
12
12
  type Point,
13
13
  pathLength,
14
+ type Rect,
14
15
  type SceneAction,
15
16
  type SceneBadge,
16
17
  type SceneBox,
@@ -38,7 +39,7 @@ const r1 =(v: number): number => Math.round(v * 100) / 100;
38
39
  export const cs = (k: ColorKey): string => (k === "coin" ? "--c:var(--coin);--ct:var(--coint)" : `--c:var(--c-${k});--ct:var(--t-${k})`);
39
40
 
40
41
  export function glyph(name: string | undefined, x: number, y: number, scale: number, cls = "dg-gl"): string {
41
- const d = name === undefined ? undefined : ICONS[name];
42
+ const d = name !== undefined && drawsIcon(name) ? ICONS[name] : undefined;
42
43
  return d ? `<path class="${cls}" transform="translate(${r1(x)} ${r1(y)}) scale(${scale})" d="${d}"/>` : "";
43
44
  }
44
45
 
@@ -97,8 +98,28 @@ export interface Ctx {
97
98
  ink: Ink[];
98
99
  /** 描いた線の記録 (#3175)。札を持たない字を横切る線を数える */
99
100
  traces: Trace[];
101
+ /** 箱の名前と下の字の基準線を、箱の真ん中からどれだけ下げるか (`boxTextY`) */
102
+ boxText: BoxTextY;
100
103
  }
101
104
 
105
+ /** 箱の名前 (`name`) と下の字 (`sub`) の基準線の、箱の真ん中からの下がり */
106
+ export interface BoxTextY {
107
+ name: number;
108
+ sub: number;
109
+ }
110
+
111
+ /** 絵柄を描ける名前か。`ICONS` の継いだ鍵 (`constructor` など) は描けない名前に数える */
112
+ export const drawsIcon = (name: string | undefined): boolean => name !== undefined && Object.hasOwn(ICONS, name);
113
+
114
+ /**
115
+ * 図の箱の名前と下の字の基準線を、箱の真ん中からどれだけ下げるか (#3307)。
116
+ * 名前の上の段 (アイコンか置き場の枠) を使う箱が 1 つでもある図は、その段の下に名前を置く高さで全ての箱の名前を揃える。
117
+ * 1 つも無い図は名前を箱の真ん中に置く。置き場の枠はアイコンの無い箱にも同じ段に出るので、真ん中に寄せると名前が枠に重なる。
118
+ * 下の字は名前から同じ間を空けて置き、名前を真ん中に上げた図で下の字だけが下の縁に残らないようにする (#3309)
119
+ */
120
+ export const boxTextY = (boxes: readonly { icon?: string; slot?: Rect }[]): BoxTextY =>
121
+ boxes.some((b) => drawsIcon(b.icon) || b.slot !== undefined) ? { name: LOOK.box.nameY, sub: LOOK.box.subY } : { name: LOOK.box.nameMid, sub: LOOK.box.subMid };
122
+
102
123
  /** 和文を含む字の、和文の字で描く時の字の形 */
103
124
  export const sansOf = (t: TextStyle): TextStyle => ({ ...t, family: "sans" });
104
125
 
@@ -136,7 +157,7 @@ export function band(region: SceneRegion, ink: Ink[]): string {
136
157
  const { x, y } = region.header;
137
158
  const lines = region.lines.map((t, k) => write(ink, "dg-bz", x, region.y + nameY + k * line, t, TYPE.regionName)).join("");
138
159
  const note = region.note ? write(ink, "dg-bx", x, region.y + nameY + region.lines.length * line + 1, region.note, TYPE.regionNote) : "";
139
- const mark = region.icon && ICONS[region.icon] ? `<rect class="dg-bi" x="${r1(x)}" y="${r1(y)}" width="${icon}" height="${icon}" rx="${iconRadius}"/>${glyph(region.icon, x + icon / 2, y + icon / 2, iconScale)}` : "";
160
+ const mark = drawsIcon(region.icon) ? `<rect class="dg-bi" x="${r1(x)}" y="${r1(y)}" width="${icon}" height="${icon}" rx="${iconRadius}"/>${glyph(region.icon, x + icon / 2, y + icon / 2, iconScale)}` : "";
140
161
  return `<g style="${cs(region.color)}"><rect class="dg-band" x="${r1(region.x + 0.5)}" y="${r1(region.y + 0.5)}" width="${r1(region.w - 1)}" height="${r1(region.h - 1)}" rx="${radius}"/>${mark}${lines}${note}</g>`;
141
162
  }
142
163
 
@@ -173,21 +194,21 @@ function cardFrame(box: SceneBox, ctx: Ctx, act: string | undefined, y: number,
173
194
  * `lit` を渡すと、動く段ではなくその間だけ光らせる (状態遷移図の、運ぶ物が乗っている状態)。
174
195
  */
175
196
  export function card(box: SceneBox, ctx: Ctx, on: number[], changes: SubChange[], lit?: Span[]): string {
176
- const { icon, iconRadius, iconScale, iconY, nameY, subY } = LOOK.box;
177
- const subs = subLines(box, ctx.m, changes, box.cy + subY, ctx.ink);
197
+ const { icon, iconRadius, iconScale, iconY } = LOOK.box;
198
+ const subs = subLines(box, ctx.m, changes, box.cy + ctx.boxText.sub, ctx.ink);
178
199
  const act = lit ? (lit.length > 0 ? rangeKf(ctx.m, lit.map((s) => [s.start, s.end])) : undefined) : on.length > 0 ? activeKf(ctx, on) : undefined;
179
200
  // 置き場を持つ箱は絵柄を左へ寄せ、右に点線の枠を初めから見せる (#3176)
180
201
  const ix = box.iconX ?? box.cx;
181
202
  const slot = box.slot ? `<rect class="dg-slot" x="${r1(box.slot.x)}" y="${r1(box.slot.y)}" width="${r1(box.slot.w)}" height="${r1(box.slot.h)}" rx="${LOOK.slot.radius}"/>` : "";
182
203
  let mark = "";
183
- if (box.icon && ICONS[box.icon]) {
204
+ if (drawsIcon(box.icon)) {
184
205
  const rect = { x: ix - icon / 2, y: box.cy + iconY - icon / 2, w: icon, h: icon };
185
206
  ctx.ink.push({ text: `${box.name} の絵柄`, rect, shown: ALWAYS, boxed: true, part: { kind: "icon", box: box.name } });
186
207
  mark = `<rect class="dg-ic" x="${r1(rect.x)}" y="${r1(rect.y)}" width="${icon}" height="${icon}" rx="${iconRadius}"/>${glyph(box.icon, ix, box.cy + iconY, iconScale)}`;
187
208
  }
188
209
  return (
189
210
  `<g style="${cs(box.color)}">${cardFrame(box, ctx, act, box.y, box.h)}` +
190
- `${slot}${mark}${write(ctx.ink, "dg-sn", box.cx, box.cy + nameY, box.name, TYPE.boxName, ALWAYS, "middle")}${subs}</g>`
211
+ `${slot}${mark}${write(ctx.ink, "dg-sn", box.cx, box.cy + ctx.boxText.name, box.name, TYPE.boxName, ALWAYS, "middle")}${subs}</g>`
191
212
  );
192
213
  }
193
214
 
@@ -208,7 +229,7 @@ export function ghostCard(box: SceneBox, ink: Ink[]): string {
208
229
  const G = LOOK.state.ghost;
209
230
  const ix = box.x + G.pad + icon / 2;
210
231
  const tx = box.x + G.pad + icon + G.gap;
211
- const mark = box.icon && ICONS[box.icon] ? `<rect class="dg-ic" x="${r1(ix - icon / 2)}" y="${r1(box.cy - icon / 2)}" width="${icon}" height="${icon}" rx="${iconRadius}"/>${glyph(box.icon, ix, box.cy, iconScale)}` : "";
232
+ const mark = drawsIcon(box.icon) ? `<rect class="dg-ic" x="${r1(ix - icon / 2)}" y="${r1(box.cy - icon / 2)}" width="${icon}" height="${icon}" rx="${iconRadius}"/>${glyph(box.icon, ix, box.cy, iconScale)}` : "";
212
233
  const nameY = box.sub ? G.nameY : LOOK.baseline.mark;
213
234
  const sub = box.sub ? write(ink, "dg-ss", tx, box.cy + G.subY, box.sub, TYPE.boxSub, ALWAYS, "start") : "";
214
235
  return (
package/src/schema.ts CHANGED
@@ -11,12 +11,19 @@
11
11
  */
12
12
  import type { ErrorObject } from "ajv";
13
13
  import { type Allowed, FIELD_MESSAGES, type FieldMessages, type Forbidden } from "./field-messages";
14
+ import { nearest } from "./nearest";
15
+ import { ICONS } from "./render/icons";
14
16
  import validate from "./schemas/validate-diagram.js";
15
17
  import { type NotationLang, STATE_ENDS } from "./words";
16
18
 
17
19
  export type Color = "blue" | "amber" | "green" | "violet" | "rose" | "slate" | "coin";
18
20
  /** 色の役の一覧。記法の `color:` に書ける値と、誤りの文に並べる値 */
19
21
  export const COLORS: readonly Color[] = ["blue", "amber", "green", "violet", "rose", "slate", "coin"];
22
+ /**
23
+ * アイコン名の一覧。記法の `icon:` に書ける値と、誤りの文に並べる値 (#3311)。
24
+ * 描く部品のアイコンの表から引く。書き間違えた名前は何も描かれず、名前の高さ (#3307) まで変わるため、読む所で断る
25
+ */
26
+ export const ICON_NAMES: readonly string[] = Object.keys(ICONS);
20
27
 
21
28
  export interface Region {
22
29
  id: string;
@@ -289,9 +296,22 @@ const RATIO = /%\)\$$/u;
289
296
 
290
297
  const escape = (key: string): string => key.replaceAll("~", "~0").replaceAll("/", "~1");
291
298
 
299
+ /** JSON Pointer `pointer` が指す `input` の中の値。辿れない時は `undefined` */
300
+ function valueAt(input: unknown, pointer: string): unknown {
301
+ let value = input;
302
+ for (const raw of pointer.split("/").slice(1)) {
303
+ if (typeof value !== "object" || value === null) return undefined;
304
+ value = (value as Record<string, unknown>)[raw.replaceAll("~1", "/").replaceAll("~0", "~")];
305
+ }
306
+ return value;
307
+ }
308
+
292
309
  /** 箱の飛び先と段の出典の URL の決まりの場所 (#3199) */
293
310
  const URL_RULE = "#/definitions/url/pattern";
294
311
 
312
+ /** 箱と区画のアイコン名の決まりの場所 (#3311)。近い名前が在れば一覧の代わりにそれを示す (#3315) */
313
+ const ICON_RULE = "#/definitions/icon/enum";
314
+
295
315
  /** commit の hash の決まり (`^[0-9a-f]{<最小>,<最大>}$`)。字の数の幅を読む */
296
316
  const HEX = /^\^\[0-9a-f\]\{(\d+),(\d+)\}\$$/u;
297
317
 
@@ -312,7 +332,7 @@ const branchesOf = (anyOf: ErrorObject, errors: readonly ErrorObject[]): ErrorOb
312
332
  (error.instancePath === anyOf.instancePath || error.instancePath.startsWith(`${anyOf.instancePath}/`)),
313
333
  );
314
334
 
315
- function toFieldError(error: ErrorObject, errors: readonly ErrorObject[], m: FieldMessages): FieldError {
335
+ function toFieldError(error: ErrorObject, errors: readonly ErrorObject[], m: FieldMessages, input: unknown): FieldError {
316
336
  const params = error.params as Record<string, unknown>;
317
337
  const at = error.instancePath;
318
338
  switch (error.keyword) {
@@ -345,8 +365,12 @@ function toFieldError(error: ErrorObject, errors: readonly ErrorObject[], m: Fie
345
365
  const tag = String(params.tag);
346
366
  return { pointer: `${at}/${escape(tag)}`, message: m.discriminator(tag, EVENT_TYPES) };
347
367
  }
348
- case "enum":
368
+ case "enum": {
369
+ const written = error.schemaPath === ICON_RULE ? valueAt(input, at) : undefined;
370
+ const near = typeof written === "string" ? nearest(written, ICON_NAMES) : undefined;
371
+ if (typeof written === "string" && near !== undefined) return { pointer: at, message: m.unknownIcon(written, near) };
349
372
  return { pointer: at, message: m.enum((params.allowedValues as unknown[]).map(String)) };
373
+ }
350
374
  case "type":
351
375
  return { pointer: at, message: m.type(String(params.type)) };
352
376
  case "minLength":
@@ -385,6 +409,6 @@ export function validateDiagram(input: unknown, options: CheckOptions = {}): Che
385
409
  const inBranch = new Set(all.filter((error) => error.keyword === "anyOf").flatMap((anyOf) => branchesOf(anyOf, all)));
386
410
  const m = FIELD_MESSAGES[options.lang ?? "ja"];
387
411
  // 条件付きの決まり (if / then) は中で落ちた決まりの誤りを別に出すので、包む側の if の誤りは出さない
388
- const errors = all.filter((error) => error.keyword !== "if" && !inBranch.has(error)).map((error) => toFieldError(error, all, m));
412
+ const errors = all.filter((error) => error.keyword !== "if" && !inBranch.has(error)).map((error) => toFieldError(error, all, m, input));
389
413
  return { ok: false, errors };
390
414
  }
@@ -220,6 +220,7 @@
220
220
  },
221
221
  "id": { "type": "string", "pattern": "^[^\\s\\[\\]「」=:]+$" },
222
222
  "color": { "enum": ["blue", "amber", "green", "violet", "rose", "slate", "coin"] },
223
+ "icon": { "description": "箱と区画のアイコン名。描く部品のアイコンの表 (ICONS) に在る名前だけ", "enum": ["app", "router", "on", "off", "pool", "db", "exe", "idx", "sign", "env", "lock", "check", "flag", "x", "chain", "net", "user", "server", "key", "shield", "globe", "bolt", "doc", "box", "truck", "card", "cart", "clock", "branch", "cloud", "refresh", "undo", "yen", "layers", "code", "paint", "tree", "block"] },
223
224
  "region": {
224
225
  "type": "object",
225
226
  "additionalProperties": false,
@@ -229,7 +230,7 @@
229
230
  "name": { "$ref": "#/definitions/text" },
230
231
  "note": { "$ref": "#/definitions/text" },
231
232
  "color": { "$ref": "#/definitions/color" },
232
- "icon": { "$ref": "#/definitions/text" }
233
+ "icon": { "$ref": "#/definitions/icon" }
233
234
  }
234
235
  },
235
236
  "box": {
@@ -241,7 +242,7 @@
241
242
  "name": { "$ref": "#/definitions/text" },
242
243
  "sub": { "$ref": "#/definitions/text" },
243
244
  "color": { "$ref": "#/definitions/color" },
244
- "icon": { "$ref": "#/definitions/text" },
245
+ "icon": { "$ref": "#/definitions/icon" },
245
246
  "region": { "$ref": "#/definitions/id" },
246
247
  "link": { "description": "飛び先。描いた図ではこの箱がここへの link になる", "$ref": "#/definitions/url" }
247
248
  }