@cardenelabs/dragon 0.37.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/README.md +38 -1
- package/dist/cli.cjs +2403 -550
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +2408 -555
- package/dist/cli.js.map +1 -1
- package/dist/export.cjs +2252 -537
- package/dist/export.cjs.map +1 -1
- package/dist/export.d.cts +3 -1
- package/dist/export.d.ts +3 -1
- package/dist/export.js +2257 -542
- package/dist/export.js.map +1 -1
- package/dist/index.cjs +2387 -495
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -154
- package/dist/index.d.ts +11 -154
- package/dist/index.js +2387 -496
- package/dist/index.js.map +1 -1
- package/dist/ink-B_7OXxtb.d.cts +360 -0
- package/dist/ink-B_7OXxtb.d.ts +360 -0
- package/dist/player.global.js +2 -2
- package/package.json +1 -1
- package/src/cli.ts +9 -0
- package/src/export/index.ts +4 -1
- package/src/field-messages.ts +8 -0
- package/src/figure-text.ts +28 -0
- package/src/index.ts +3 -0
- package/src/layout/architecture.ts +123 -44
- package/src/layout/git.ts +728 -0
- package/src/layout/measure.ts +10 -0
- package/src/layout/scene.ts +300 -19
- package/src/layout/sequence.ts +29 -11
- package/src/layout/state.ts +82 -62
- package/src/look/values.ts +57 -4
- package/src/messages.ts +47 -4
- package/src/parser.ts +142 -7
- package/src/player.ts +44 -0
- package/src/printer.ts +16 -4
- package/src/render/git.ts +161 -0
- package/src/render/ink.ts +104 -0
- package/src/render/parts.ts +103 -36
- package/src/render/svg.ts +63 -15
- package/src/schema.ts +47 -13
- package/src/schemas/diagram.schema.json +75 -8
- package/src/schemas/validate-diagram.js +1 -1
- package/src/timeline.ts +18 -3
- package/src/words.ts +19 -3
- package/dist/schema-B2MTRv66.d.cts +0 -133
- package/dist/schema-B2MTRv66.d.ts +0 -133
package/src/parser.ts
CHANGED
|
@@ -26,14 +26,34 @@
|
|
|
26
26
|
* 最初の `step` より前に書いた線は、移れるが段で通るとは限らない道 (`state.paths`) になり、属性を持たない (#3163)。
|
|
27
27
|
* 状態遷移図の `start` と `finish` は箱の名札に使えない。時系列図と構成図では宣言して使う普通の名札のまま。
|
|
28
28
|
*
|
|
29
|
+
* 枝分かれの図 (`git`、#3164) は、最初の `step` より前に `branch <名札> [note: <添え書き>]` で枝を上から順に宣言し、
|
|
30
|
+
* 段の中に `commit <名札> on <枝> [hash: <hash>, note: <一言>]` で commit を積み、`<枝> from <commit>` で枝を切る。
|
|
31
|
+
* 当て直しは `on <枝>` の後ろに `copy <commit>` を、merge commit は `merge <枝>` を続ける。
|
|
32
|
+
* 線の行は札を動かす行になり、送る側に枝の名札か `HEAD`、受ける側に積んだ commit を書く。運ぶ物は書かなければ送る側の名前の札。
|
|
33
|
+
* 枝分かれの図では `HEAD` を名札に使えず、区画 (`group`) は書けない。`branch` と `commit` の行は枝分かれの図でだけ書ける。
|
|
34
|
+
*
|
|
29
35
|
* 行の頭の語と同じ綴りの名札 (`end` や `step`) も書ける。名札の次に `->` が来る行は線と読む。
|
|
30
36
|
* 誤りは最初の 1 つで止め、行と列を示す。誤りの文の言葉は呼ぶ側が渡す。
|
|
31
37
|
*/
|
|
32
38
|
import { type Messages, messagesFor, type Shape } from "./messages";
|
|
33
|
-
import {
|
|
39
|
+
import {
|
|
40
|
+
type Box,
|
|
41
|
+
type Carry,
|
|
42
|
+
type Color,
|
|
43
|
+
COLORS,
|
|
44
|
+
type Diagram,
|
|
45
|
+
type DiagramEvent,
|
|
46
|
+
type GitBranch,
|
|
47
|
+
type GitCommit,
|
|
48
|
+
type LabelSide,
|
|
49
|
+
type Region,
|
|
50
|
+
type StatePath,
|
|
51
|
+
type Step,
|
|
52
|
+
type ViewMode,
|
|
53
|
+
} from "./schema";
|
|
34
54
|
import { columnOf, isBlankOrComment, type SourceLine, tokenize } from "./tokenize";
|
|
35
55
|
import { FORMAT_VERSION } from "./version";
|
|
36
|
-
import { AS, ATTRS, CARRIES, HEADS, KINDS, type NotationLang, OLD_HEADS, SIDES, STATE_ENDS, VIEWS } from "./words";
|
|
56
|
+
import { AS, ATTRS, CARRIES, COPY, FROM, HEAD, HEADS, KINDS, MERGE, type NotationLang, OLD_HEADS, ON, SIDES, STATE_ENDS, VIEWS } from "./words";
|
|
37
57
|
|
|
38
58
|
export interface LineError {
|
|
39
59
|
line: number;
|
|
@@ -65,6 +85,12 @@ const fail = (source: SourceLine, index: number, message: string): NotationError
|
|
|
65
85
|
const ID = /^[^\s[\]「」=:]+$/u;
|
|
66
86
|
/** 線の行。名札、`->`、名札 */
|
|
67
87
|
const EDGE = /^([^\s:[\]]+?)\s*->\s*([^\s:[\]]+)/du;
|
|
88
|
+
/** 枝分かれの図の `commit` の行。名札と、`on` の枝と、`copy` の当て直しの元と、`merge` の取り込む枝 */
|
|
89
|
+
const COMMIT = new RegExp(`^commit\\s+([^\\s[]+)(?:\\s+${ON}\\s+([^\\s[]+))?(?:\\s+${COPY}\\s+([^\\s[]+))?(?:\\s+${MERGE}\\s+([^\\s[]+))?`, "du");
|
|
90
|
+
/** 枝分かれの図の、枝を切る行。枝と `from` と切る commit */
|
|
91
|
+
const CUT = new RegExp(`^([^\\s[]+)\\s+${FROM}(?:\\s+([^\\s[]+))?\\s*$`, "du");
|
|
92
|
+
/** commit の hash。原本の JSON の hash の形と同じ */
|
|
93
|
+
const HASH = /^[0-9a-f]{4,40}$/u;
|
|
68
94
|
|
|
69
95
|
const isOneOf = <T extends string>(list: readonly T[], word: string): word is T => (list as readonly string[]).includes(word);
|
|
70
96
|
|
|
@@ -260,6 +286,11 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
|
|
|
260
286
|
const view = shown?.value as ViewMode | undefined;
|
|
261
287
|
|
|
262
288
|
const isState = kind === "state";
|
|
289
|
+
const isGit = kind === "git";
|
|
290
|
+
const branches: GitBranch[] = [];
|
|
291
|
+
/** 枝分かれの図で積んだ commit と、枝の札が今指す commit (まだ何も指さない枝は `null`) */
|
|
292
|
+
const commits = new Set<string>();
|
|
293
|
+
const tips = new Map<string, string | null>();
|
|
263
294
|
const regions: Region[] = [];
|
|
264
295
|
const boxes: Box[] = [];
|
|
265
296
|
const paths: StatePath[] = [];
|
|
@@ -278,15 +309,32 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
|
|
|
278
309
|
if (!boxes.some((box) => box.id === id)) throw fail(source, at, m.undeclaredBox(id));
|
|
279
310
|
return id;
|
|
280
311
|
};
|
|
281
|
-
|
|
312
|
+
const branchAt = (source: SourceLine, id: string, at: number): string => {
|
|
313
|
+
if (!tips.has(id)) throw fail(source, at, m.undeclaredBranch(id));
|
|
314
|
+
return id;
|
|
315
|
+
};
|
|
316
|
+
const commitAt = (source: SourceLine, id: string, at: number): string => {
|
|
317
|
+
if (!commits.has(id)) throw fail(source, at, m.noCommit(id));
|
|
318
|
+
return id;
|
|
319
|
+
};
|
|
320
|
+
/**
|
|
321
|
+
* 線の端。状態遷移図では送る側に `start`、受ける側に `finish` を書ける。
|
|
322
|
+
* 枝分かれの図では送る側が枝の名札か `HEAD`、受ける側が積んだ commit
|
|
323
|
+
*/
|
|
282
324
|
const endAt = (source: SourceLine, id: string, at: number, side: "from" | "to"): string => {
|
|
325
|
+
if (isGit) {
|
|
326
|
+
if (side === "to") return commitAt(source, id, at);
|
|
327
|
+
if (id !== HEAD && !tips.has(id)) throw fail(source, at, m.undeclaredRef(id));
|
|
328
|
+
return id;
|
|
329
|
+
}
|
|
283
330
|
if (!isState || !isOneOf(STATE_ENDS, id)) return boxAt(source, id, at);
|
|
284
331
|
if ((id === "start") !== (side === "from")) throw fail(source, at, id === "start" ? m.intoStart : m.outOfFinish);
|
|
285
332
|
return id;
|
|
286
333
|
};
|
|
287
|
-
/**
|
|
334
|
+
/** 状態遷移図の箱と区画の名札に、始まりと終わりの印の名前は使えない。枝分かれの図の名札に、今いる場所の札の名前は使えない */
|
|
288
335
|
const notReserved = (source: SourceLine, id: string, at: number): void => {
|
|
289
336
|
if (isState && isOneOf(STATE_ENDS, id)) throw fail(source, at, m.reservedId(id));
|
|
337
|
+
if (isGit && id === HEAD) throw fail(source, at, m.headReserved(id));
|
|
290
338
|
};
|
|
291
339
|
/** 段の中に書く行の、書き先の段。最初の `step` より前なら誤り */
|
|
292
340
|
const inStep = (source: SourceLine): Building => {
|
|
@@ -321,7 +369,14 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
|
|
|
321
369
|
else inStep(source).events.push(readEdge(source, edge));
|
|
322
370
|
continue;
|
|
323
371
|
}
|
|
324
|
-
const word = text.split(/\s
|
|
372
|
+
const [word = "", second] = text.split(/\s+/u);
|
|
373
|
+
// 枝分かれの図で、宣言した枝の名札の次に `from` が来る行は、枝を切る行
|
|
374
|
+
if (isGit && tips.has(word) && second === FROM) {
|
|
375
|
+
const step = inStep(source);
|
|
376
|
+
step.events.push(readCut(source));
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
if (!isGit && (word === "branch" || word === "commit")) throw fail(source, 0, m.gitOnly(word));
|
|
325
380
|
if (!isOneOf(HEADS, word)) {
|
|
326
381
|
const old = oldForm(text, building !== undefined);
|
|
327
382
|
throw fail(source, 0, old ? m.oldForm(old.word, old.shape) : m.unknownHead(word));
|
|
@@ -375,12 +430,21 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
|
|
|
375
430
|
case "set":
|
|
376
431
|
inStep(source).events.push(readSet(source));
|
|
377
432
|
break;
|
|
433
|
+
case "branch":
|
|
434
|
+
readBranch(source);
|
|
435
|
+
break;
|
|
436
|
+
case "commit": {
|
|
437
|
+
const step = inStep(source);
|
|
438
|
+
step.events.push(readCommit(source));
|
|
439
|
+
break;
|
|
440
|
+
}
|
|
378
441
|
}
|
|
379
442
|
}
|
|
380
443
|
if (group) throw fail(group.source, 0, m.unclosedGroup(group.id));
|
|
381
444
|
finish();
|
|
382
445
|
|
|
383
|
-
if (
|
|
446
|
+
if (isGit && branches.length === 0) throw fail(head, 0, m.noBranches);
|
|
447
|
+
if (boxes.length === 0 && !isGit) throw fail(head, 0, m.noBoxes);
|
|
384
448
|
if (steps.length === 0) throw fail(head, 0, m.noSteps);
|
|
385
449
|
|
|
386
450
|
return {
|
|
@@ -394,11 +458,13 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
|
|
|
394
458
|
steps,
|
|
395
459
|
...(kind === "sequence" ? { sequence: { lifelines: boxes.map((box) => box.id) } } : {}),
|
|
396
460
|
...(paths.length > 0 ? { state: { paths } } : {}),
|
|
461
|
+
...(isGit ? { git: { branches } } : {}),
|
|
397
462
|
};
|
|
398
463
|
|
|
399
464
|
/** `group` の行。同じ名札をもう一度開いた時は、名前も属性も書かない */
|
|
400
465
|
function readGroup(source: SourceLine): void {
|
|
401
466
|
beforeSteps(source);
|
|
467
|
+
if (isGit) throw fail(source, 0, m.gitNoGroup);
|
|
402
468
|
if (group) throw fail(source, 0, m.nestedGroup);
|
|
403
469
|
const declared = readDeclared(source, "group", ATTRS.group, m);
|
|
404
470
|
if (regions.some((region) => region.id === declared.id)) {
|
|
@@ -427,6 +493,73 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
|
|
|
427
493
|
});
|
|
428
494
|
}
|
|
429
495
|
|
|
496
|
+
/** 枝分かれの図の `branch <名札> [note: <添え書き>]` の行。枝は宣言した順に上から帯に並ぶ */
|
|
497
|
+
function readBranch(source: SourceLine): void {
|
|
498
|
+
beforeSteps(source);
|
|
499
|
+
const declared = readDeclared(source, "branch", ATTRS.branch, m);
|
|
500
|
+
if (declared.name !== undefined) throw fail(source, 0, m.branchShape);
|
|
501
|
+
notReserved(source, declared.id, declared.idAt);
|
|
502
|
+
declare(source, declared.id, declared.idAt);
|
|
503
|
+
const note = declared.attrs.get("note")?.value;
|
|
504
|
+
branches.push({ id: declared.id, ...(note !== undefined ? { note } : {}) });
|
|
505
|
+
tips.set(declared.id, null);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* 枝分かれの図の `commit <名札> on <枝> [copy <commit>] [merge <枝>] [hash: <hash>, note: <一言>]` の行。
|
|
510
|
+
* 積んだ枝の札は新しい commit へ進む。当て直しは枝の札を動かさない
|
|
511
|
+
*/
|
|
512
|
+
function readCommit(source: SourceLine): GitCommit {
|
|
513
|
+
const text = source.text;
|
|
514
|
+
const found = COMMIT.exec(text);
|
|
515
|
+
const at = (k: number): number => found?.indices?.[k]?.[0] ?? 0;
|
|
516
|
+
const id = found?.[1];
|
|
517
|
+
const on = found?.[2];
|
|
518
|
+
if (!found || id === undefined || on === undefined) throw fail(source, 0, m.commitShape);
|
|
519
|
+
const rest = skipSpaces(text, found[0].length);
|
|
520
|
+
if (rest < text.length && text[rest] !== "[") throw fail(source, rest, m.commitShape);
|
|
521
|
+
if (!ID.test(id)) throw fail(source, at(1), m.badId(id));
|
|
522
|
+
notReserved(source, id, at(1));
|
|
523
|
+
declare(source, id, at(1));
|
|
524
|
+
const branch = branchAt(source, on, at(2));
|
|
525
|
+
const copy = found[3] === undefined ? undefined : commitAt(source, found[3], at(3));
|
|
526
|
+
const merge = found[4] === undefined ? undefined : mergedAt(source, found[4], at(4), branch);
|
|
527
|
+
const attrs = rest < text.length ? readAttrs(source, rest, ATTRS.commit, m) : new Map<string, Attr>();
|
|
528
|
+
const hash = attrs.get("hash");
|
|
529
|
+
if (hash && !HASH.test(hash.value)) throw fail(source, hash.at, m.badHash);
|
|
530
|
+
const note = attrs.get("note")?.value;
|
|
531
|
+
commits.add(id);
|
|
532
|
+
if (copy === undefined) tips.set(branch, id);
|
|
533
|
+
return {
|
|
534
|
+
type: "commit",
|
|
535
|
+
id,
|
|
536
|
+
branch,
|
|
537
|
+
...(copy !== undefined ? { copy } : {}),
|
|
538
|
+
...(merge !== undefined ? { merge } : {}),
|
|
539
|
+
...(hash ? { hash: hash.value } : {}),
|
|
540
|
+
...(note !== undefined ? { note } : {}),
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/** merge commit が取り込む枝。積む枝と別の、もう commit を指している枝 */
|
|
545
|
+
function mergedAt(source: SourceLine, id: string, at: number, into: string): string {
|
|
546
|
+
branchAt(source, id, at);
|
|
547
|
+
if (id === into) throw fail(source, at, m.mergeSelf(id));
|
|
548
|
+
if (tips.get(id) === null) throw fail(source, at, m.emptyBranch(id));
|
|
549
|
+
return id;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/** 枝分かれの図の `<枝> from <commit>` の行。枝の札を commit へ置く */
|
|
553
|
+
function readCut(source: SourceLine): DiagramEvent {
|
|
554
|
+
const found = CUT.exec(source.text);
|
|
555
|
+
const branch = found?.[1];
|
|
556
|
+
const from = found?.[2];
|
|
557
|
+
if (!found || branch === undefined || from === undefined) throw fail(source, 0, m.fromShape);
|
|
558
|
+
commitAt(source, from, found.indices?.[2]?.[0] ?? 0);
|
|
559
|
+
tips.set(branch, from);
|
|
560
|
+
return { type: "branch", branch, from };
|
|
561
|
+
}
|
|
562
|
+
|
|
430
563
|
/** `strip <題>: <欄>, <欄>` の行 */
|
|
431
564
|
function readStrip(source: SourceLine): void {
|
|
432
565
|
beforeSteps(source);
|
|
@@ -514,9 +647,11 @@ function parseLines(lines: SourceLine[], m: Messages): Diagram {
|
|
|
514
647
|
if (carried && !isOneOf(CARRIES, carried.value)) throw fail(source, carried.at, m.badCarry);
|
|
515
648
|
const side = attrs.get("side");
|
|
516
649
|
if (side && !isOneOf(SIDES, side.value)) throw fail(source, side.at, m.badSide);
|
|
517
|
-
|
|
650
|
+
// 枝分かれの図の線は札を動かす線で、書かなければ送る側の名前の札を運ぶ
|
|
651
|
+
let carry: Carry = isGit ? { kind: "tag", text: from } : { kind: "envelope" };
|
|
518
652
|
if (tag) carry = { kind: "tag", text: tag.value };
|
|
519
653
|
else if (carried) carry = carried.value === "coin" ? { kind: "coin" } : { kind: "none" };
|
|
654
|
+
if (isGit && from !== HEAD) tips.set(from, to);
|
|
520
655
|
return {
|
|
521
656
|
type: "edge",
|
|
522
657
|
from,
|
package/src/player.ts
CHANGED
|
@@ -7,11 +7,13 @@
|
|
|
7
7
|
* 見せ方は枠の幅で選び (`view.ts`)、寄って追う時は窓と全体図で描き直す。
|
|
8
8
|
* 釦と段の一覧の読み上げの名前と全体図の札は、渡した言葉 (`options.lang`、書かなければ日本語) で出す (#3001)。
|
|
9
9
|
* 段が替わるたびに `options.onStep` で今の段を知らせ、頁の側が段の札や進み具合を出せるようにする (#3031)。
|
|
10
|
+
* 頁は `goTo` で段へ移って止め、`mark` で図の上に点線の四角を描く。頁の側では SVG を組まないので、編集画面が字の重なりを囲む時もこれを呼ぶ (#3178)。
|
|
10
11
|
* 段の本文の帯は本文の最も長い段の高さに保ち、段を送っても図の大きさと位置を変えない (#3049)。
|
|
11
12
|
* 帯の中身の幅が `LOOK.player.narrow` より狭い時は、段を送る釦を指で押せる大きさにして段の番号と上の段に並べ、
|
|
12
13
|
* 題と本文をその下の段の全幅に置く (#3057、#3069)。
|
|
13
14
|
*/
|
|
14
15
|
import { FIGURE_TEXT } from "./figure-text";
|
|
16
|
+
import type { Rect } from "./layout/scene";
|
|
15
17
|
import { FONT_VARS, LOOK, paletteVars, resolvePalette, type TextStyle, TYPE } from "./look/values";
|
|
16
18
|
import { drawSvg, type RenderOptions, renderDiagram } from "./render/svg";
|
|
17
19
|
import type { Span } from "./timeline";
|
|
@@ -36,6 +38,10 @@ export interface Player {
|
|
|
36
38
|
prev(): void;
|
|
37
39
|
play(): void;
|
|
38
40
|
pause(): void;
|
|
41
|
+
/** 段 `step` (0 から) へ移って止める。段の数を超えた時は最後の段で止める (#3178) */
|
|
42
|
+
goTo(step: number): void;
|
|
43
|
+
/** 図の上に点線の四角を描く。四角は図の座標で渡し、空を渡すと消す。図を置き直しても残る (#3178) */
|
|
44
|
+
mark(rects: readonly Rect[]): void;
|
|
39
45
|
destroy(): void;
|
|
40
46
|
}
|
|
41
47
|
|
|
@@ -79,6 +85,7 @@ function playerCss(): string {
|
|
|
79
85
|
// 指で触る時は、広い帯でも段を送る釦を指で押せる大きさにする (#3076)。並びは横 1 列のまま変えない。
|
|
80
86
|
// 広い帯は本文が 2 行あれば帯の高さが変わらず、本文の無い帯だけが釦の分だけ高くなる
|
|
81
87
|
`@media (pointer: coarse){.dg-btn{width:${px(P.touch)};height:${px(P.touch)}}}`,
|
|
88
|
+
`.dg-spot{fill:none;stroke:var(--t-amber);stroke-width:${px(LOOK.stroke.mark)};stroke-dasharray:${LOOK.mark.dash} ${LOOK.mark.gap};pointer-events:none}`,
|
|
82
89
|
].join("");
|
|
83
90
|
}
|
|
84
91
|
|
|
@@ -93,6 +100,9 @@ function ensureCss(doc: Document): void {
|
|
|
93
100
|
/** 釦に出す字。HTML に書き出す時は、この字も字の file に残す。 */
|
|
94
101
|
export const BUTTON_TEXT = { prev: "‹", pause: "❚❚", play: "▶", next: "›" } as const;
|
|
95
102
|
|
|
103
|
+
/** 図の上に四角を描く時の SVG の名前空間 */
|
|
104
|
+
const SVG_NS = "http://www.w3.org/2000/svg";
|
|
105
|
+
|
|
96
106
|
const pad = (n: number): string => String(n).padStart(2, "0");
|
|
97
107
|
/** 段の番号の札 (`01/12`)。`step` は 0 から数える。 */
|
|
98
108
|
export const stepNo = (step: number, count: number): string => `${pad(step + 1)}/${pad(count)}`;
|
|
@@ -217,6 +227,31 @@ export function mountDiagram(host: HTMLElement, diagram: unknown, options: Playe
|
|
|
217
227
|
let playing = !reduce;
|
|
218
228
|
let at = reduce ? (timeline.stills[0] ?? 0) : 0;
|
|
219
229
|
let born = Date.now();
|
|
230
|
+
let marked: readonly Rect[] = [];
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* 渡された四角を点線で囲む。寄って追う図では窓の中身と一緒に動く所に置き、止めて見せる図では図の上に置く。
|
|
234
|
+
* 囲みは字の四角より `LOOK.mark.pad` だけ広げ、字の縁に線が掛からないようにする
|
|
235
|
+
*/
|
|
236
|
+
const drawMarks = (): void => {
|
|
237
|
+
if (!svg) return;
|
|
238
|
+
for (const old of svg.querySelectorAll(".dg-spots")) old.remove();
|
|
239
|
+
if (marked.length === 0) return;
|
|
240
|
+
const M = LOOK.mark;
|
|
241
|
+
const group = doc.createElementNS(SVG_NS, "g");
|
|
242
|
+
group.setAttribute("class", "dg-spots");
|
|
243
|
+
for (const r of marked) {
|
|
244
|
+
const rect = doc.createElementNS(SVG_NS, "rect");
|
|
245
|
+
rect.setAttribute("class", "dg-spot");
|
|
246
|
+
rect.setAttribute("x", String(r.x - M.pad));
|
|
247
|
+
rect.setAttribute("y", String(r.y - M.pad));
|
|
248
|
+
rect.setAttribute("width", String(r.w + M.pad * 2));
|
|
249
|
+
rect.setAttribute("height", String(r.h + M.pad * 2));
|
|
250
|
+
rect.setAttribute("rx", String(M.radius));
|
|
251
|
+
group.append(rect);
|
|
252
|
+
}
|
|
253
|
+
(svg.querySelector('g[clip-path^="url(#dg-vp-"] > g.dg-a') ?? svg).append(group);
|
|
254
|
+
};
|
|
220
255
|
|
|
221
256
|
/** 動きの変数を付けた図を置き直す。置き直すと動きの時計が最初から数え直される */
|
|
222
257
|
const place = (markup?: string): void => {
|
|
@@ -239,6 +274,7 @@ export function mountDiagram(host: HTMLElement, diagram: unknown, options: Playe
|
|
|
239
274
|
else frame.append(next);
|
|
240
275
|
svg = next;
|
|
241
276
|
born = Date.now();
|
|
277
|
+
drawMarks();
|
|
242
278
|
};
|
|
243
279
|
const markup = (): string => (mode === "follow" ? drawSvg(rendered.diagram, rendered.layout, timeline, palette, rendered.theme, { width, ...(height === undefined ? {} : { height }), lang }) : rendered.svg);
|
|
244
280
|
|
|
@@ -329,6 +365,14 @@ export function mountDiagram(host: HTMLElement, diagram: unknown, options: Playe
|
|
|
329
365
|
pause: () => {
|
|
330
366
|
if (playing) restart(now(), false);
|
|
331
367
|
},
|
|
368
|
+
goTo: (target: number) => {
|
|
369
|
+
playing = false;
|
|
370
|
+
go(target);
|
|
371
|
+
},
|
|
372
|
+
mark: (rects: readonly Rect[]) => {
|
|
373
|
+
marked = rects;
|
|
374
|
+
drawMarks();
|
|
375
|
+
},
|
|
332
376
|
destroy: () => {
|
|
333
377
|
if (timer !== undefined) win?.clearInterval(timer);
|
|
334
378
|
observer?.disconnect();
|
package/src/printer.ts
CHANGED
|
@@ -10,8 +10,10 @@
|
|
|
10
10
|
* 箱は JSON の並びのまま書き、同じ区画の箱が続く所を 1 つの `group … end` にする。
|
|
11
11
|
* 区画の箱が飛び飛びの時は同じ `group` を開き直す。区画の並びが箱の現れる順と違う時と、
|
|
12
12
|
* 箱の無い区画がある時は、先に全ての区画を中の無い `group … end` で宣言してから開き直す。
|
|
13
|
+
* 枝分かれの図 (#3164) は、枝の `branch` の行を箱より前に書き、札を動かす線の運ぶ物が送る側の名前の札なら書かない。
|
|
13
14
|
*/
|
|
14
15
|
import type { Box, Diagram, DiagramEvent, Region } from "./schema";
|
|
16
|
+
import { COPY, FROM, MERGE, ON } from "./words";
|
|
15
17
|
|
|
16
18
|
const quote = (text: string): string => `"${text.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
17
19
|
/** 行の終わりか `[` の前に置く名前と文 */
|
|
@@ -48,10 +50,12 @@ const declareBox = (b: Box): string =>
|
|
|
48
50
|
/** 線の行の両端と名前 */
|
|
49
51
|
const line = (from: string, to: string, label: string | undefined): string => `${from} -> ${to}${label === undefined ? "" : `: ${name(label)}`}`;
|
|
50
52
|
|
|
51
|
-
|
|
53
|
+
/** 段の中の 1 行。`git` は枝分かれの図か。枝分かれの図の線は、送る側の名前の札を書かない時の運ぶ物にする */
|
|
54
|
+
function event(e: DiagramEvent, git: boolean): string {
|
|
52
55
|
switch (e.type) {
|
|
53
56
|
case "edge": {
|
|
54
|
-
const
|
|
57
|
+
const plain = git ? e.carry.kind === "tag" && e.carry.text === e.from : e.carry.kind === "envelope";
|
|
58
|
+
const carry: [string, string][] = plain ? [] : e.carry.kind === "tag" ? [["tag", e.carry.text]] : [["carry", e.carry.kind]];
|
|
55
59
|
return `${line(e.from, e.to, e.label)}${attrs([...carry, ["side", e.labelSide]])}`;
|
|
56
60
|
}
|
|
57
61
|
case "action":
|
|
@@ -60,6 +64,13 @@ function event(e: DiagramEvent): string {
|
|
|
60
64
|
return `note ${e.box}: ${e.text}`;
|
|
61
65
|
case "strip":
|
|
62
66
|
return `set ${key(e.field)}${e.value === undefined ? "" : `: ${name(e.value)}`}${attrs([["tag", e.tag]])}`;
|
|
67
|
+
case "commit":
|
|
68
|
+
return `commit ${e.id} ${ON} ${e.branch}${e.copy === undefined ? "" : ` ${COPY} ${e.copy}`}${e.merge === undefined ? "" : ` ${MERGE} ${e.merge}`}${attrs([
|
|
69
|
+
["hash", e.hash],
|
|
70
|
+
["note", e.note],
|
|
71
|
+
])}`;
|
|
72
|
+
case "branch":
|
|
73
|
+
return `${e.branch} ${FROM} ${e.from}`;
|
|
63
74
|
}
|
|
64
75
|
}
|
|
65
76
|
|
|
@@ -97,14 +108,15 @@ function declarations(diagram: Diagram): string[] {
|
|
|
97
108
|
}
|
|
98
109
|
|
|
99
110
|
export function printNotation(diagram: Diagram): string {
|
|
100
|
-
const
|
|
111
|
+
const branches = (diagram.git?.branches ?? []).map((b) => `branch ${b.id}${attrs([["note", b.note]])}`);
|
|
112
|
+
const blocks = [`${diagram.kind} ${name(diagram.title)}${attrs([["view", diagram.view]])}`, ...(branches.length > 0 ? [branches.join("\n")] : []), ...declarations(diagram)];
|
|
101
113
|
if (diagram.strip) blocks.push(`strip ${key(diagram.strip.title)}: ${diagram.strip.fields.map(field).join(", ")}`);
|
|
102
114
|
if (diagram.state) blocks.push(diagram.state.paths.map((p) => line(p.from, p.to, p.label)).join("\n"));
|
|
103
115
|
for (const step of diagram.steps) {
|
|
104
116
|
const body = step.body.split("\n");
|
|
105
117
|
const say = body.length === 1 ? [` say ${step.body}`] : [" say", ...body.map((line) => (line === "" ? "" : ` ${line}`))];
|
|
106
118
|
blocks.push(
|
|
107
|
-
[`step ${step.title}`, ...say, ...(step.code === undefined ? [] : [` code ${step.code}`]), ...step.events.map((e) => ` ${event(e)}`)].join("\n"),
|
|
119
|
+
[`step ${step.title}`, ...say, ...(step.code === undefined ? [] : [` code ${step.code}`]), ...step.events.map((e) => ` ${event(e, diagram.kind === "git")}`)].join("\n"),
|
|
108
120
|
);
|
|
109
121
|
}
|
|
110
122
|
return `${blocks.join("\n\n")}\n`;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 枝分かれの図 (#3164) を描く部品。見本帳の試作 `docs/design/base/scenes/git.js` の commit と札を写す。
|
|
3
|
+
*
|
|
4
|
+
* commit の丸は、親から引く線を引き終えた時に弾んで出て、hash と一言を丸の外に添える。
|
|
5
|
+
* 親から引く線は引いた後も残し、当て直しの線は点線で出す。どの札からも辿れなくなった commit は、その時に点線の丸を重ねる。
|
|
6
|
+
* 札は枝ごとと HEAD に 1 つずつ置き、出来事のたびに次の置き場へ走らせる。札を動かす線は、その段の間だけ引いて消す。
|
|
7
|
+
* 札はその後も動くので、線を残すと札の去った所を指したまま残る。
|
|
8
|
+
* 札の見た目は運ぶ物の札と同じで、どの札も主役ではないので、時刻の表の運ぶ物とは別に動かす。
|
|
9
|
+
*/
|
|
10
|
+
import type { GitLayout, GitMove } from "../layout/git";
|
|
11
|
+
import type { Diagram } from "../schema";
|
|
12
|
+
import { hasWide, textWidth } from "../layout/measure";
|
|
13
|
+
import type { Point, Rect } from "../layout/scene";
|
|
14
|
+
import { EASE, LOOK, MOTION, TYPE } from "../look/values";
|
|
15
|
+
import type { EdgeTime, Span, Timeline } from "../timeline";
|
|
16
|
+
import type { Frame } from "./motion";
|
|
17
|
+
import { type Ctx, carrier, cs, esc, pathOf, write } from "./parts";
|
|
18
|
+
|
|
19
|
+
const r1 = (v: number): number => Math.round(v * 100) / 100;
|
|
20
|
+
const center = (r: Rect): Point => [r.x + r.w / 2, r.y + r.h / 2];
|
|
21
|
+
|
|
22
|
+
/** 札の形。真ん中を (0, 0) に置き、動きで置き場へ運ぶ */
|
|
23
|
+
function tagShape(text: string, sh: string): string {
|
|
24
|
+
const C = LOOK.carrier;
|
|
25
|
+
const ja = hasWide(text);
|
|
26
|
+
const w = textWidth(text, ja ? TYPE.carrierJa : TYPE.carrier) + C.tagPadX * 2;
|
|
27
|
+
return `<rect class="dg-car" x="${r1(-w / 2)}" y="${-C.tagH / 2}" width="${r1(w)}" height="${C.tagH}" rx="${C.tagH / 2}" ${sh}/><text class="dg-car-t${ja ? " dg-car-tj" : ""}" y="${LOOK.baseline.carrier}" text-anchor="middle">${esc(text)}</text>`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function drawGit(layout: GitLayout, diagram: Diagram, timeline: Timeline, ctx: Ctx): { links: string; lines: string; commits: string; tags: string } {
|
|
31
|
+
const { m } = ctx;
|
|
32
|
+
const G = LOOK.git;
|
|
33
|
+
const timeOf = (step: number, event: number): EdgeTime | undefined => timeline.edges.find((t) => t.step === step && t.event === event);
|
|
34
|
+
|
|
35
|
+
const links = layout.links
|
|
36
|
+
.map((l) => {
|
|
37
|
+
const time = timeOf(l.step, l.event);
|
|
38
|
+
if (!time) return "";
|
|
39
|
+
ctx.traces.push({ from: l.from, to: l.to, points: l.points, shown: [[time.start, Number.POSITIVE_INFINITY]] });
|
|
40
|
+
const d = pathOf(l.points);
|
|
41
|
+
if (l.kind === "copy") return `<g style="${cs(l.color)}"><path class="dg-git-cp dg-a" d="${d}" style="${m.vis(time.start, m.last)}"/></g>`;
|
|
42
|
+
const kf = m.kf([
|
|
43
|
+
[0, { opacity: 0, "stroke-dashoffset": 1 }],
|
|
44
|
+
[time.start, { opacity: 0, "stroke-dashoffset": 1 }, EASE.inOut],
|
|
45
|
+
[time.start + 0.05, { opacity: 1 }],
|
|
46
|
+
[time.drawn, { "stroke-dashoffset": 0 }],
|
|
47
|
+
[m.last, { opacity: 1, "stroke-dashoffset": 0 }, EASE.inOut],
|
|
48
|
+
[m.total, { opacity: 0, "stroke-dashoffset": 0 }],
|
|
49
|
+
]);
|
|
50
|
+
return `<g style="${cs(l.color)}"><path class="dg-git-l dg-a" pathLength="1" d="${d}" style="${kf}"/></g>`;
|
|
51
|
+
})
|
|
52
|
+
.join("");
|
|
53
|
+
|
|
54
|
+
const commits = layout.commits
|
|
55
|
+
.map((c) => {
|
|
56
|
+
const at = timeOf(c.step, c.event)?.drawn ?? 0;
|
|
57
|
+
const ring = { x: c.cx - c.r, y: c.cy - c.r, w: c.r * 2, h: c.r * 2 };
|
|
58
|
+
ctx.ink.push({ text: c.id, rect: ring, shown: [[at, Number.POSITIVE_INFINITY]], boxed: true });
|
|
59
|
+
const lines = (c.text?.lines ?? [])
|
|
60
|
+
.map((line, k) => {
|
|
61
|
+
const hash = k === 0 && c.hash !== undefined;
|
|
62
|
+
const baseline = c.text?.baselines[k] ?? c.cy;
|
|
63
|
+
return write(ctx.ink, hash ? "dg-git-h" : "dg-git-n", c.cx, baseline, line, hash ? TYPE.gitHash : TYPE.gitNote, [[at, Number.POSITIVE_INFINITY]], "middle");
|
|
64
|
+
})
|
|
65
|
+
.join("");
|
|
66
|
+
const id = `<text class="dg-git-id" x="${r1(c.cx)}" y="${r1(c.cy + LOOK.baseline.badge)}" text-anchor="middle">${esc(c.id)}</text>`;
|
|
67
|
+
const node = `<g class="dg-popc dg-a" style="${m.pop(at)}"><circle class="dg-git-c" cx="${r1(c.cx)}" cy="${r1(c.cy)}" r="${c.r}" ${ctx.sh}/>${id}</g>`;
|
|
68
|
+
const text = lines === "" ? "" : `<g class="dg-a" style="${m.vis(at)}">${lines}</g>`;
|
|
69
|
+
const orphanAt = c.orphan === undefined ? undefined : timeOf(c.orphan, c.orphanEvent ?? 0)?.drawn;
|
|
70
|
+
const orphan =
|
|
71
|
+
orphanAt === undefined
|
|
72
|
+
? ""
|
|
73
|
+
: `<g class="dg-a" data-orphan="${esc(c.id)}" style="${m.vis(orphanAt, m.last, { f: 0.5 })}"><circle class="dg-git-old" cx="${r1(c.cx)}" cy="${r1(c.cy)}" r="${G.orphan}"/><text class="dg-git-oldl" x="${r1(c.cx)}" y="${r1(c.cy + LOOK.baseline.badge)}" text-anchor="middle">${esc(c.id)}</text></g>`;
|
|
74
|
+
return `<g data-commit="${esc(c.id)}" style="${cs(c.color)}">${node}${text}${orphan}</g>`;
|
|
75
|
+
})
|
|
76
|
+
.join("");
|
|
77
|
+
|
|
78
|
+
const lines = layout.moves
|
|
79
|
+
.flatMap((mv) => {
|
|
80
|
+
const time = timeOf(mv.step, mv.event);
|
|
81
|
+
if (!mv.line || !time) return [];
|
|
82
|
+
const end = (timeline.steps[mv.step] as Span).end;
|
|
83
|
+
ctx.traces.push({ from: mv.ref, to: mv.target ?? mv.ref, points: mv.line, shown: [[time.start, end + 0.4]] });
|
|
84
|
+
const kf = m.kf([
|
|
85
|
+
[0, { opacity: 0, "stroke-dashoffset": 1 }],
|
|
86
|
+
[time.start, { opacity: 0, "stroke-dashoffset": 1 }, EASE.inOut],
|
|
87
|
+
[time.start + 0.05, { opacity: 1 }],
|
|
88
|
+
[time.drawn, { "stroke-dashoffset": 0 }],
|
|
89
|
+
[end, { opacity: 1 }, EASE.inOut],
|
|
90
|
+
[end + 0.4, { opacity: 0 }],
|
|
91
|
+
[m.total, { opacity: 0, "stroke-dashoffset": 0 }],
|
|
92
|
+
]);
|
|
93
|
+
return [`<g style="${cs(mv.color)}"><path class="dg-e-on dg-a" pathLength="1" d="${pathOf(mv.line)}" style="${kf}"/></g>`];
|
|
94
|
+
})
|
|
95
|
+
.join("");
|
|
96
|
+
|
|
97
|
+
// 線に書いた運ぶ物は、線を走って着いた所で消える。着いた所には枝の札が留まるので、運ぶ物は留めない
|
|
98
|
+
const carried = timeline.carriers
|
|
99
|
+
.flatMap((t) => {
|
|
100
|
+
const mv = layout.moves.find((x) => x.step === t.step && x.event === t.event && x.line);
|
|
101
|
+
const source = diagram.steps[t.step]?.events[t.event];
|
|
102
|
+
if (!mv?.line || source?.type !== "edge") return [];
|
|
103
|
+
const edge = { step: t.step, event: t.event, from: mv.ref, to: mv.target ?? mv.ref, points: mv.line, color: mv.color, carry: source.carry };
|
|
104
|
+
return [carrier(edge, { ...t, start: t.depart, end: t.arrive + MOTION.fade }, ctx)];
|
|
105
|
+
})
|
|
106
|
+
.join("");
|
|
107
|
+
const refs = [...new Set(layout.moves.map((mv) => mv.ref))];
|
|
108
|
+
const tags = refs.map((ref) => tagMotion(ref, layout.moves.filter((mv) => mv.ref === ref), timeOf, ctx)).join("") + carried;
|
|
109
|
+
return { links, lines, commits, tags };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 1 つの札の動き。現れる出来事では着いた時に出し、動く出来事では前の置き場から次の置き場へ走らせ、消える出来事では着いた時に消す。
|
|
114
|
+
* 留まっている間の四角を、字の重なりを数える記録に残す
|
|
115
|
+
*/
|
|
116
|
+
function tagMotion(ref: string, list: GitMove[], timeOf: (step: number, event: number) => EdgeTime | undefined, ctx: Ctx): string {
|
|
117
|
+
const { m } = ctx;
|
|
118
|
+
const points: [number, number, number][] = [];
|
|
119
|
+
const fades: Frame[] = [[0, { opacity: 0 }]];
|
|
120
|
+
let on = false;
|
|
121
|
+
let rest: { rect: Rect; since: number } | undefined;
|
|
122
|
+
const leave = (t: number): void => {
|
|
123
|
+
if (rest) ctx.ink.push({ text: ref, rect: rest.rect, shown: [[rest.since, t]], boxed: true });
|
|
124
|
+
rest = undefined;
|
|
125
|
+
};
|
|
126
|
+
for (const mv of list) {
|
|
127
|
+
const time = timeOf(mv.step, mv.event);
|
|
128
|
+
if (!time) continue;
|
|
129
|
+
const { start: a, drawn: b } = time;
|
|
130
|
+
const from = mv.from ? center(mv.from) : undefined;
|
|
131
|
+
const to = mv.to ? center(mv.to) : undefined;
|
|
132
|
+
leave(a);
|
|
133
|
+
// 線に運ぶ物を書いた時は、枝の札は走らずに消え、運ぶ物が着いた時に着いた所へ出る
|
|
134
|
+
if (mv.carried && from && to && on) {
|
|
135
|
+
points.push([a + MOTION.fade, from[0], from[1]], [b - MOTION.fade, to[0], to[1]]);
|
|
136
|
+
fades.push([a, { opacity: 1 }, EASE.inOut], [a + MOTION.fade, { opacity: 0 }], [b - MOTION.fade, { opacity: 0 }, EASE.out], [b, { opacity: 1 }]);
|
|
137
|
+
rest = { rect: mv.to as Rect, since: b };
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (from) points.push([a, from[0], from[1]]);
|
|
141
|
+
if (to) points.push([b, to[0], to[1]]);
|
|
142
|
+
if (to && !on) {
|
|
143
|
+
// 前の置き場から走る札は走り出す時に、何も無い所に現れる札は着いた時に出す
|
|
144
|
+
const show = from ? a : b - MOTION.fade;
|
|
145
|
+
fades.push([show, { opacity: 0 }, EASE.out], [show + MOTION.fade, { opacity: 1 }]);
|
|
146
|
+
on = true;
|
|
147
|
+
} else if (!to && on) {
|
|
148
|
+
fades.push([b, { opacity: 1 }, EASE.inOut], [b + MOTION.untilFade, { opacity: 0 }]);
|
|
149
|
+
on = false;
|
|
150
|
+
}
|
|
151
|
+
if (mv.to) rest = { rect: mv.to, since: b };
|
|
152
|
+
}
|
|
153
|
+
leave(Number.POSITIVE_INFINITY);
|
|
154
|
+
if (points.length === 0) return "";
|
|
155
|
+
const first = points[0] as [number, number, number];
|
|
156
|
+
const last = points[points.length - 1] as [number, number, number];
|
|
157
|
+
if (on) fades.push([m.last, { opacity: 1 }, EASE.inOut], [m.total, { opacity: 0 }]);
|
|
158
|
+
else fades.push([m.total, { opacity: 0 }]);
|
|
159
|
+
const style = m.move([[0, first[1], first[2]], ...points, [m.total, last[1], last[2]]], fades);
|
|
160
|
+
return `<g class="dg-a" data-ref="${esc(ref)}" style="${style}">${tagShape(ref, ctx.sh)}</g>`;
|
|
161
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 描いた字の記録と、字どうしの重なり (#3175)。
|
|
3
|
+
*
|
|
4
|
+
* 字を描く部品 (`parts.ts`) は、字を描く時に、その字が占める四角と見えている時間をここへ 1 件ずつ記録する。
|
|
5
|
+
* 描いた結果は、段ごとの止め絵の時刻に同時に見えている字どうしの重なりを返す。
|
|
6
|
+
* 字の幅は置き場所と同じ字の幅の表 (`layout/measure.ts`) で測るので、ブラウザを使わず、node でも頁の中でも同じ結果になる。
|
|
7
|
+
*
|
|
8
|
+
* 札に入った字 (線の名前、処理、段の番号、帯の札、運ぶ物の札) は、札の形が人に見える物なので札の四角を記録する。
|
|
9
|
+
* 走っている途中の運ぶ物は数えず、着いて留まっている間だけ記録する。
|
|
10
|
+
*
|
|
11
|
+
* 重なりは重なる字ごとに字の四角と段の番号かどうか (`spots`) を持ち、編集画面はその四角を図の上で囲む (#3178)。
|
|
12
|
+
* 知らせる文は `describeOverlap` が作り、編集画面と書き出しの命令が同じ文を出す。
|
|
13
|
+
* 書き手が書いていない図の部品 (箱の絵柄、運ぶ物の硬貨と封筒) は部品の種類 (`part`) を記録し、知らせる文の言葉で呼ぶ (#3184)。
|
|
14
|
+
*/
|
|
15
|
+
import { FIGURE_TEXT } from "../figure-text";
|
|
16
|
+
import { crossesRect, type Point, type Rect } from "../layout/scene";
|
|
17
|
+
import type { NotationLang } from "../words";
|
|
18
|
+
|
|
19
|
+
/** 見えている間。`[出る時刻, 消える時刻)` */
|
|
20
|
+
export type Shown = [number, number];
|
|
21
|
+
|
|
22
|
+
/** 初めから最後まで見えている字と線 */
|
|
23
|
+
export const ALWAYS: Shown[] = [[0, Number.POSITIVE_INFINITY]];
|
|
24
|
+
|
|
25
|
+
/** 書き手が書いていない図の部品。`icon` は箱 `box` の絵柄、`coin` と `envelope` は運ぶ物の硬貨と封筒 (#3184) */
|
|
26
|
+
export type InkPart = { kind: "icon"; box: string } | { kind: "coin" } | { kind: "envelope" };
|
|
27
|
+
|
|
28
|
+
export interface Ink {
|
|
29
|
+
text: string;
|
|
30
|
+
rect: Rect;
|
|
31
|
+
shown: Shown[];
|
|
32
|
+
/** 札に入った字。札の地が下の線を隠すので、線が横切っても重なりに数えない */
|
|
33
|
+
boxed?: boolean;
|
|
34
|
+
/** 段の番号の丸の字。字だけでは何か分からないので、知らせる文で段の番号と呼ぶ */
|
|
35
|
+
badge?: boolean;
|
|
36
|
+
/** 図の部品。`text` は日本語の名前なので、知らせる文は部品の種類から言葉ごとに呼び方を作る */
|
|
37
|
+
part?: InkPart;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 描いた線。`from` と `to` は線の両端の箱の名札 */
|
|
41
|
+
export interface Trace {
|
|
42
|
+
from: string;
|
|
43
|
+
to: string;
|
|
44
|
+
points: Point[];
|
|
45
|
+
shown: Shown[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 重なる字。`rect` は字 (札に入った字は札) の四角、`badge` は段の番号の丸の字か、`part` は図の部品の種類 (部品の時だけ持つ) */
|
|
49
|
+
export interface OverlapSpot {
|
|
50
|
+
text: string;
|
|
51
|
+
rect: Rect;
|
|
52
|
+
badge: boolean;
|
|
53
|
+
part?: InkPart;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 重なり。`step` は重なりが見える段 (0 から数える)。
|
|
58
|
+
* `texts` は重なる 2 つの字、`line` は札を持たない字 `text` を横切る線の両端の名札。
|
|
59
|
+
* `spots` は重なる字で、`texts` は 2 つの字を同じ順に、`line` は横切られる字を 1 つ持つ
|
|
60
|
+
*/
|
|
61
|
+
export type Overlap =
|
|
62
|
+
| { kind: "texts"; step: number; texts: [string, string]; spots: [OverlapSpot, OverlapSpot] }
|
|
63
|
+
| { kind: "line"; step: number; text: string; line: { from: string; to: string }; spots: [OverlapSpot] };
|
|
64
|
+
|
|
65
|
+
/** 重なりに数える食い込みの下限 (px)。縁が触れただけの字を数えない */
|
|
66
|
+
const BITE = 0.5;
|
|
67
|
+
|
|
68
|
+
const visible = (item: { shown: Shown[] }, t: number): boolean => item.shown.some(([a, b]) => a <= t && t < b);
|
|
69
|
+
|
|
70
|
+
/** 重なる字。札の四角は札の字の名札などを持つので、四角だけを写す */
|
|
71
|
+
const spotOf = (ink: Ink): OverlapSpot => ({ text: ink.text, rect: { x: ink.rect.x, y: ink.rect.y, w: ink.rect.w, h: ink.rect.h }, badge: ink.badge === true, ...(ink.part ? { part: ink.part } : {}) });
|
|
72
|
+
|
|
73
|
+
/** 段ごとの止め絵の時刻 `stills` で、同時に見えている字どうしの重なりと、札を持たない字を横切る線を、段の順と記録した順に並べる。 */
|
|
74
|
+
export function findOverlaps(inks: Ink[], stills: number[], traces: Trace[] = []): Overlap[] {
|
|
75
|
+
const out: Overlap[] = [];
|
|
76
|
+
stills.forEach((t, step) => {
|
|
77
|
+
const seen = inks.filter((ink) => visible(ink, t));
|
|
78
|
+
seen.forEach((a, i) => {
|
|
79
|
+
for (const b of seen.slice(i + 1)) {
|
|
80
|
+
const w = Math.min(a.rect.x + a.rect.w, b.rect.x + b.rect.w) - Math.max(a.rect.x, b.rect.x);
|
|
81
|
+
const h = Math.min(a.rect.y + a.rect.h, b.rect.y + b.rect.h) - Math.max(a.rect.y, b.rect.y);
|
|
82
|
+
if (w > BITE && h > BITE) out.push({ kind: "texts", step, texts: [a.text, b.text], spots: [spotOf(a), spotOf(b)] });
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
const lines = traces.filter((trace) => visible(trace, t));
|
|
86
|
+
for (const ink of seen) {
|
|
87
|
+
if (ink.boxed) continue;
|
|
88
|
+
for (const line of lines) if (crossesRect(line.points, ink.rect, BITE)) out.push({ kind: "line", step, text: ink.text, line: { from: line.from, to: line.to }, spots: [spotOf(ink)] });
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
return out;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 重なりを知らせる文 (#3178)。段の番号は「段の番号 N」と呼び、図の部品は言葉ごとの呼び方で呼び (#3184)、ほかの字は鉤括弧か引用符で囲む。
|
|
96
|
+
* 段の番号は文に含めない
|
|
97
|
+
*/
|
|
98
|
+
export function describeOverlap(overlap: Overlap, lang: NotationLang = "ja"): string {
|
|
99
|
+
const t = FIGURE_TEXT[lang];
|
|
100
|
+
const partName = (part: InkPart): string => (part.kind === "icon" ? t.iconOf(part.box) : part.kind === "coin" ? t.coin : t.envelope);
|
|
101
|
+
const name = (spot: OverlapSpot): string => (spot.badge ? t.badgeName(spot.text) : spot.part ? partName(spot.part) : t.quoted(spot.text));
|
|
102
|
+
if (overlap.kind === "texts") return t.textsOverlap(name(overlap.spots[0]), name(overlap.spots[1]));
|
|
103
|
+
return t.lineCrosses(overlap.line.from, overlap.line.to, name(overlap.spots[0]));
|
|
104
|
+
}
|