@cardenelabs/dragon 0.39.0 → 0.40.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/src/render/svg.ts CHANGED
@@ -7,6 +7,7 @@
7
7
  */
8
8
  import { FIGURE_TEXT } from "../figure-text";
9
9
  import { layoutArchitecture } from "../layout/architecture";
10
+ import { isGitLayout, layoutGit } from "../layout/git";
10
11
  import { textWidth } from "../layout/measure";
11
12
  import type { Layout, Rect, SceneLabel } from "../layout/scene";
12
13
  import { layoutSequence } from "../layout/sequence";
@@ -39,6 +40,7 @@ import {
39
40
  strip,
40
41
  type SubChange,
41
42
  } from "./parts";
43
+ import { drawGit } from "./git";
42
44
  import { findOverlaps, type Ink, type Overlap, type Trace } from "./ink";
43
45
  import { Motion } from "./motion";
44
46
 
@@ -148,6 +150,14 @@ const PART_CSS = [
148
150
  `.dg-mark{${font(TYPE.regionNote)};fill:var(--mute)}`,
149
151
  `.dg-done{fill:var(--ink);stroke:var(--surf);stroke-width:${S.done}}`,
150
152
  `.dg-done-ck{fill:none;stroke:var(--surf);stroke-width:${S.glyph};stroke-linecap:round;stroke-linejoin:round}`,
153
+ `.dg-git-l{fill:none;stroke:var(--c);stroke-width:${S.git};stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:1 2}`,
154
+ `.dg-git-cp{fill:none;stroke:var(--c);stroke-width:${S.gitOrphan};stroke-linecap:round;stroke-dasharray:3 3;stroke-opacity:.7}`,
155
+ `.dg-git-c{fill:var(--surf);stroke:var(--c);stroke-width:${S.git}}`,
156
+ `.dg-git-id{${font(TYPE.gitId)};fill:var(--ct)}`,
157
+ `.dg-git-h{${font(TYPE.gitHash)};fill:var(--mute)}`,
158
+ `.dg-git-n{${font(TYPE.gitNote)};fill:var(--ink2)}`,
159
+ `.dg-git-old{fill:var(--surf);stroke:var(--c);stroke-width:${S.gitOrphan};stroke-dasharray:3 3;stroke-opacity:.6}`,
160
+ `.dg-git-oldl{${font(TYPE.gitId)};fill:var(--ct);opacity:.55}`,
151
161
  ".dg-vp-bg{fill:var(--bg)}",
152
162
  ".dg-vp-frame{fill:none;stroke:var(--hair)}",
153
163
  ".dg-hd-bg{fill:var(--bg)}",
@@ -282,6 +292,14 @@ export function drawSvg(diagram: Diagram, layout: Layout, timeline: Timeline, pa
282
292
  const arrivals = new Map((timeline.states ?? []).flatMap((s) => (s.lit[0] ? [[s.box, s.lit[0].start] as [string, number]] : [])));
283
293
  if (layout.strip) L.band.push(strip(layout.strip, fieldChanges(diagram, timeline, layout), ctx, isState, arrivals));
284
294
  for (const line of layout.lifelines) L.base.push(lifeline(line.x, line.y1, line.y2));
295
+ // 枝分かれの図の線と commit と札は、枝分かれの図を描く部品が描く (#3164)
296
+ if (isGitLayout(layout)) {
297
+ const git = drawGit(layout, diagram, timeline, ctx);
298
+ L.base.push(git.links);
299
+ L.lit.push(git.lines);
300
+ L.node.push(git.commits);
301
+ L.top.push(git.tags);
302
+ }
285
303
  for (const e of layout.edges) {
286
304
  const time = timeline.edges.find((t) => t.step === e.step && t.event === e.event);
287
305
  if (!time) continue;
@@ -501,7 +519,14 @@ export function renderDiagram(input: unknown, options: RenderOptions = {}): Rend
501
519
  const read = readDiagram(input, { lang });
502
520
  if (!read.ok) throw new Error(FIGURE_TEXT[lang].cannotDraw(read.errors.map((e) => `${e.pointer} ${e.message}`).join(" / ")));
503
521
  const diagram = read.diagram;
504
- const layout = diagram.kind === "sequence" ? layoutSequence(diagram) : diagram.kind === "state" ? layoutState(diagram, lang) : layoutArchitecture(diagram);
522
+ const layout =
523
+ diagram.kind === "sequence"
524
+ ? layoutSequence(diagram)
525
+ : diagram.kind === "state"
526
+ ? layoutState(diagram, lang)
527
+ : diagram.kind === "git"
528
+ ? layoutGit(diagram)
529
+ : layoutArchitecture(diagram);
505
530
  return renderLayout(diagram, layout, buildTimeline(diagram), options);
506
531
  }
507
532
 
package/src/schema.ts CHANGED
@@ -46,7 +46,29 @@ export type DiagramEvent =
46
46
  | { type: "edge"; from: string; to: string; label?: string; carry: Carry; labelSide?: LabelSide }
47
47
  | { type: "action"; box: string; label: string }
48
48
  | { type: "swap"; box: string; text: string }
49
- | { type: "strip"; field: string; value?: string; tag?: string };
49
+ | { type: "strip"; field: string; value?: string; tag?: string }
50
+ | GitCommit
51
+ | { type: "branch"; branch: string; from: string };
52
+
53
+ /**
54
+ * 枝分かれの図 (#3164) の commit を積む出来事。積んだ枝の札と、その枝を指す HEAD が新しい commit へ進む。
55
+ * 当て直し (`copy`) は枝の札を動かさず、merge commit (`merge`) は取り込む枝の先端も親に持つ
56
+ */
57
+ export interface GitCommit {
58
+ type: "commit";
59
+ id: string;
60
+ branch: string;
61
+ copy?: string;
62
+ merge?: string;
63
+ hash?: string;
64
+ note?: string;
65
+ }
66
+
67
+ /** 枝分かれの図の枝。上から宣言した順に帯を並べる */
68
+ export interface GitBranch {
69
+ id: string;
70
+ note?: string;
71
+ }
50
72
 
51
73
  /** 状態遷移図の、最初の段より前に書いた移れる道。`from` に `start`、`to` に `finish` を書ける */
52
74
  export interface StatePath {
@@ -64,7 +86,7 @@ export interface Step {
64
86
 
65
87
  export interface Diagram {
66
88
  version: number;
67
- kind: "sequence" | "architecture" | "state";
89
+ kind: "sequence" | "architecture" | "state" | "git";
68
90
  title: string;
69
91
  view?: ViewMode;
70
92
  regions?: Region[];
@@ -73,6 +95,7 @@ export interface Diagram {
73
95
  steps: Step[];
74
96
  sequence?: { lifelines: string[] };
75
97
  state?: { paths: StatePath[] };
98
+ git?: { branches: GitBranch[] };
76
99
  }
77
100
 
78
101
  export interface FieldError {
@@ -89,26 +112,32 @@ export interface CheckOptions {
89
112
  }
90
113
 
91
114
  /** 段の中で起きることの種類。schema の event の `discriminator` が見る `type` の値 */
92
- const EVENT_TYPES = ["edge", "action", "swap", "strip"] as const satisfies readonly DiagramEvent["type"][];
115
+ const EVENT_TYPES = ["edge", "action", "swap", "strip", "commit", "branch"] as const satisfies readonly DiagramEvent["type"][];
93
116
 
94
117
  /**
95
- * schema の書けない組み合わせ (`not: { required: [欄] }`) の欄。根の 3 か所は決まりの場所で引き、運ぶ物は誤りの場所の最後の欄で引く。
96
- * 根の `sequence` と運ぶ物の `text` は ajv の誤りでは決まりの場所 (`#/else/not`) が同じになるので、誤りの場所でも分ける
118
+ * その種類の図に書けない根の欄を書いた時の決まりの場所 (#3164)。根の欄に `not: {}` を置く決まり (`absent`) を指し、
119
+ * 誤りの場所の最後の欄が書いた欄になる。種類の欄は、その欄と同じ名前の図の種類だけが書ける
97
120
  */
98
- const ROOT_NOT_ALLOWED: Record<string, Allowed> = {
99
- "#/else/not": { field: "sequence", kind: "sequence" },
100
- "#/then/not": { field: "state", kind: "state" },
101
- "#/else/else/not": { field: "state", kind: "state" },
102
- };
121
+ const ABSENT = "#/definitions/absent/not";
122
+ /** 種類の欄でない根の欄を書ける図の種類。区画は枝分かれの図に書けない */
123
+ const ABSENT_KINDS: Record<string, string> = { regions: "sequence / architecture / state" };
124
+ /** 運ぶ物の書けない組み合わせ (`not: { required: [欄] }`)。誤りの場所の最後の欄で引く */
103
125
  const NOT_ALLOWED: Record<string, Allowed> = {
104
126
  carry: { field: "text", kind: "tag" },
105
127
  };
106
128
 
107
129
  /** 状態遷移図の箱の名札に予約の名前を書いた時の決まりの場所 */
108
130
  const RESERVED_ID = /\/boxes\/items\/properties\/id\/not$/u;
131
+ /** 枝分かれの図でない図に、枝分かれの図だけの出来事を書いた時の決まりの場所 (#3164) */
132
+ const GIT_EVENT = /\/events\/items\/properties\/type\/not$/u;
133
+ /** 枝分かれの図だけが書ける出来事 */
134
+ const GIT_EVENTS = ["commit", "branch"] as const satisfies readonly DiagramEvent["type"][];
109
135
 
110
136
  const escape = (key: string): string => key.replaceAll("~", "~0").replaceAll("/", "~1");
111
137
 
138
+ /** commit の hash の決まり (`^[0-9a-f]{<最小>,<最大>}$`)。字の数の幅を読む */
139
+ const HEX = /^\^\[0-9a-f\]\{(\d+),(\d+)\}\$$/u;
140
+
112
141
  /** `^[^…]+$` の形の決まりから書けない字を読む。違う形の決まりでは `undefined` */
113
142
  function forbiddenOf(pattern: string): Forbidden | undefined {
114
143
  const inside = /^\^\[\^(.+)\]\+\$$/u.exec(pattern)?.[1];
@@ -140,7 +169,10 @@ function toFieldError(error: ErrorObject, errors: readonly ErrorObject[], m: Fie
140
169
  }
141
170
  case "not": {
142
171
  if (RESERVED_ID.test(error.schemaPath)) return { pointer: at, message: m.reserved(STATE_ENDS) };
143
- const allowed = at === "" ? ROOT_NOT_ALLOWED[error.schemaPath] : NOT_ALLOWED[at.slice(at.lastIndexOf("/") + 1)];
172
+ if (GIT_EVENT.test(error.schemaPath)) return { pointer: at, message: m.kindOnly(GIT_EVENTS, "git") };
173
+ const last = at.slice(at.lastIndexOf("/") + 1);
174
+ if (error.schemaPath === ABSENT) return { pointer: at, message: m.not({ field: last, kind: ABSENT_KINDS[last] ?? last }) };
175
+ const allowed = NOT_ALLOWED[last];
144
176
  return { pointer: allowed ? `${at}/${allowed.field}` : at, message: m.not(allowed) };
145
177
  }
146
178
  case "anyOf": {
@@ -158,8 +190,10 @@ function toFieldError(error: ErrorObject, errors: readonly ErrorObject[], m: Fie
158
190
  return { pointer: at, message: m.type(String(params.type)) };
159
191
  case "minLength":
160
192
  return { pointer: at, message: m.minLength(Number(params.limit)) };
161
- case "pattern":
162
- return { pointer: at, message: m.pattern(forbiddenOf(String(params.pattern))) };
193
+ case "pattern": {
194
+ const hex = HEX.exec(String(params.pattern));
195
+ return { pointer: at, message: hex ? m.hex(Number(hex[1]), Number(hex[2])) : m.pattern(forbiddenOf(String(params.pattern))) };
196
+ }
163
197
  case "minItems":
164
198
  return { pointer: at, message: m.minItems(Number(params.limit)) };
165
199
  case "minimum":
@@ -1,31 +1,52 @@
1
1
  {
2
2
  "title": "dragon の図の原本",
3
- "description": "区画、箱、線、運ぶ物、段、中身の帯の 6 部品を共通の骨組みに置き、図の種類だけの物は種類の欄に置く。座標は持たない。",
3
+ "description": "区画、箱、線、運ぶ物、段、中身の帯の 6 部品を共通の骨組みに置き、図の種類だけの物は種類の欄に置く。座標は持たない。箱は枝分かれの図だけが持たなくてよい。",
4
4
  "type": "object",
5
5
  "additionalProperties": false,
6
6
  "required": ["version", "kind", "title", "boxes", "steps"],
7
7
  "properties": {
8
8
  "version": { "description": "形式の版", "type": "integer", "minimum": 1 },
9
- "kind": { "description": "図の種類", "enum": ["sequence", "architecture", "state"] },
9
+ "kind": { "description": "図の種類", "enum": ["sequence", "architecture", "state", "git"] },
10
10
  "title": { "$ref": "#/definitions/text" },
11
11
  "view": { "description": "見せ方。書かなければ字の大きさで選ぶ。fixed は全体を止めて中だけ動かし、follow は寄って動かし全体図で今どこかを示す", "enum": ["fixed", "follow"] },
12
12
  "regions": { "type": "array", "items": { "$ref": "#/definitions/region" } },
13
- "boxes": { "type": "array", "minItems": 1, "items": { "$ref": "#/definitions/box" } },
13
+ "boxes": { "type": "array", "items": { "$ref": "#/definitions/box" } },
14
14
  "strip": { "$ref": "#/definitions/strip" },
15
15
  "steps": { "type": "array", "minItems": 1, "items": { "$ref": "#/definitions/step" } },
16
16
  "sequence": { "$ref": "#/definitions/sequence" },
17
- "state": { "$ref": "#/definitions/state" }
17
+ "state": { "$ref": "#/definitions/state" },
18
+ "git": { "$ref": "#/definitions/git" }
18
19
  },
19
20
  "if": { "required": ["kind"], "properties": { "kind": { "const": "sequence" } } },
20
- "then": { "required": ["sequence"], "not": { "required": ["state"] } },
21
+ "then": {
22
+ "required": ["sequence"],
23
+ "properties": { "boxes": { "minItems": 1 }, "steps": { "$ref": "#/definitions/plainSteps" }, "state": { "$ref": "#/definitions/absent" }, "git": { "$ref": "#/definitions/absent" } }
24
+ },
21
25
  "else": {
22
- "not": { "required": ["sequence"] },
26
+ "properties": { "sequence": { "$ref": "#/definitions/absent" } },
23
27
  "if": { "required": ["kind"], "properties": { "kind": { "const": "state" } } },
24
- "then": { "properties": { "boxes": { "items": { "properties": { "id": { "description": "始まりと終わりの印の名前は箱の名札に使えない", "not": { "enum": ["start", "finish"] } } } } } } },
25
- "else": { "not": { "required": ["state"] } }
28
+ "then": {
29
+ "properties": {
30
+ "boxes": { "minItems": 1, "items": { "properties": { "id": { "description": "始まりと終わりの印の名前は箱の名札に使えない", "not": { "enum": ["start", "finish"] } } } } },
31
+ "steps": { "$ref": "#/definitions/plainSteps" },
32
+ "git": { "$ref": "#/definitions/absent" }
33
+ }
34
+ },
35
+ "else": {
36
+ "if": { "required": ["kind"], "properties": { "kind": { "const": "git" } } },
37
+ "then": { "required": ["git"], "properties": { "state": { "$ref": "#/definitions/absent" }, "regions": { "$ref": "#/definitions/absent" } } },
38
+ "else": {
39
+ "properties": { "boxes": { "minItems": 1 }, "steps": { "$ref": "#/definitions/plainSteps" }, "state": { "$ref": "#/definitions/absent" }, "git": { "$ref": "#/definitions/absent" } }
40
+ }
41
+ }
26
42
  },
27
43
  "definitions": {
28
44
  "text": { "type": "string", "minLength": 1 },
45
+ "absent": { "description": "この図の種類では書けない欄", "not": {} },
46
+ "plainSteps": {
47
+ "description": "枝分かれの図でない図の段。枝分かれの図だけの出来事 (commit と branch) を書けない",
48
+ "items": { "properties": { "events": { "items": { "properties": { "type": { "not": { "enum": ["commit", "branch"] } } } } } } }
49
+ },
29
50
  "id": { "type": "string", "pattern": "^[^\\s\\[\\]「」=:]+$" },
30
51
  "color": { "enum": ["blue", "amber", "green", "violet", "rose", "slate", "coin"] },
31
52
  "region": {
@@ -115,6 +136,32 @@
115
136
  "text": { "$ref": "#/definitions/text" }
116
137
  }
117
138
  },
139
+ {
140
+ "description": "commit を積む。枝分かれの図だけが書ける。積んだ枝の札と、その枝を指す HEAD が進む。当て直し (copy) は枝の札を動かさず、merge commit (merge) は取り込む枝の先端も親に持つ",
141
+ "type": "object",
142
+ "additionalProperties": false,
143
+ "required": ["type", "id", "branch"],
144
+ "properties": {
145
+ "type": { "const": "commit" },
146
+ "id": { "$ref": "#/definitions/id" },
147
+ "branch": { "$ref": "#/definitions/id" },
148
+ "copy": { "$ref": "#/definitions/id" },
149
+ "merge": { "$ref": "#/definitions/id" },
150
+ "hash": { "$ref": "#/definitions/hash" },
151
+ "note": { "$ref": "#/definitions/text" }
152
+ }
153
+ },
154
+ {
155
+ "description": "枝を切る。枝分かれの図だけが書ける。枝の札を from の commit へ置き、HEAD をその枝へ移す",
156
+ "type": "object",
157
+ "additionalProperties": false,
158
+ "required": ["type", "branch", "from"],
159
+ "properties": {
160
+ "type": { "const": "branch" },
161
+ "branch": { "$ref": "#/definitions/id" },
162
+ "from": { "$ref": "#/definitions/id" }
163
+ }
164
+ },
118
165
  {
119
166
  "description": "中身の帯の欄の値と札",
120
167
  "type": "object",
@@ -161,6 +208,26 @@
161
208
  "paths": { "type": "array", "minItems": 1, "items": { "$ref": "#/definitions/path" } }
162
209
  }
163
210
  },
211
+ "git": {
212
+ "description": "枝分かれの図の種類の欄。枝を上から帯に並べる順",
213
+ "type": "object",
214
+ "additionalProperties": false,
215
+ "required": ["branches"],
216
+ "properties": {
217
+ "branches": { "type": "array", "minItems": 1, "items": { "$ref": "#/definitions/branch" } }
218
+ }
219
+ },
220
+ "branch": {
221
+ "description": "枝。帯の左に名札と添え書きを置く",
222
+ "type": "object",
223
+ "additionalProperties": false,
224
+ "required": ["id"],
225
+ "properties": {
226
+ "id": { "$ref": "#/definitions/id" },
227
+ "note": { "$ref": "#/definitions/text" }
228
+ }
229
+ },
230
+ "hash": { "description": "commit の hash。16 進の字で 4 から 40 字", "type": "string", "pattern": "^[0-9a-f]{4,40}$" },
164
231
  "path": {
165
232
  "description": "移れる道。送る側に始まりの印 start、受ける側に終わりの印 finish を書ける。どの段にも出ない道は点線で描き、運ぶ物を走らせない",
166
233
  "type": "object",