egatari 0.6.9 → 0.6.10-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +151 -116
- package/README.md +145 -116
- package/bin/egatari.mjs +10 -7
- package/components.lock.json +12 -6
- package/components2.lock.json +151 -1
- package/kit/check-messages.mjs +123 -114
- package/kit/check.mjs +12 -8
- package/kit/core/article-messages.mjs +7 -7
- package/kit/core/build-core.mjs +8 -8
- package/kit/core/check-parity.mjs +1 -1
- package/kit/core/check-source.mjs +4 -0
- package/kit/core/colab-core.mjs +1 -1
- package/kit/core/llm-fix.mjs +15 -15
- package/kit/core/llm-insert.mjs +7 -7
- package/kit/core/llm-outline.mjs +6 -6
- package/kit/core/llm-pack-data.mjs +33 -33
- package/kit/core/narration-parse.mjs +3 -3
- package/kit/core/new-deck.mjs +1 -1
- package/kit/core/predict-steps.mjs +46 -0
- package/kit/core/template-text.mjs +2 -2
- package/kit/instruction-version.json +1 -1
- package/kit/player.json +1 -1
- package/kit/player.lock.json +2 -2
- package/kit/templates/new-deck.mjs +1 -1
- package/package.json +1 -1
- package/render/only.mjs +2 -2
- package/render/render.mjs +4 -4
- package/tools/ids.mjs +7 -7
- package/vendor/components/CATALOG.md +1 -1
- package/vendor/components/sha256/19f34ad938c3513d9268fcedb947e0049380232da2293d5c00922fe32ec1a088/README.en.md +32 -0
- package/vendor/components/sha256/19f34ad938c3513d9268fcedb947e0049380232da2293d5c00922fe32ec1a088/index.js +352 -0
- package/vendor/components/sha256/1d10fec0552198e3926b3d960046606668fa7f42f562cea539c6a16cc3e83338/README.en.md +40 -0
- package/vendor/components/sha256/1d10fec0552198e3926b3d960046606668fa7f42f562cea539c6a16cc3e83338/index.js +341 -0
- package/vendor/components-2/manifest-2.6.2.json +2104 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
// m-flow:流れ図の箱と矢印を段階ごとに出して、図を組み立てる
|
|
2
|
+
// <m-flow style="height:400px"><script type="application/json">
|
|
3
|
+
// {"nodes":[{"id":"a","label":"観測 *w*","x":20,"y":40,"w":200,"h":70,"step":1}, …],
|
|
4
|
+
// "edges":[{"from":"a","to":"b","step":2,"label":"更新"}]}
|
|
5
|
+
// (JSON の終わり)</m-flow>
|
|
6
|
+
// 座標は要素の箱の px(x, y は箱の左上)。箱は段階 step で現れ、矢印は同じ段階の箱の後に引く
|
|
7
|
+
// 箱の字は label(*斜体*、_下付き、^上付き、\n で改行)か tex(KaTeX)。tone は term / emph / muted
|
|
8
|
+
// お金・モノの流れ(ビジネスモデルの図):edge の kind(money / service / data)で色と線種を決め、amount(金額など)を太字で添える。
|
|
9
|
+
// 同じ 2 つの箱の行きと帰りは shift(線に垂直にずらす px)で分ける。pulse の辺は、点の列が始点から終点へ流れる
|
|
10
|
+
// (pulse: true なら辺の段階に 1 回、[段階, …] ならその段階ごとに 1 回。位置は (k, t) だけで決まり、流れ終わると消える)
|
|
11
|
+
// 流れ図の判断:node の shape: "diamond"(箱の x, y, w, h に内接するひし形)。はい/いいえは出ていく辺の label
|
|
12
|
+
// 戻る辺:edge の loop(true / "left" / "right" / "top" / "bottom")。箱の横から出て、間の箱の外側を回って戻り先の箱の横に入る
|
|
13
|
+
// 2.0(Egatari spec §6.1 の部品):base の MotionElement・el・mtext・prog・lerp・previewOpacity を使い、プレイヤーのグローバルは使わない。
|
|
14
|
+
// 式(tex)は組み立てのときに KaTeX で描く(spec §6.2):build.fields の nodes[].tex を作り手が描いて texHtml を足し、
|
|
15
|
+
// この部品は markup-reader で決まった要素と属性だけを読んで置く(再生のときに KaTeX は動かない)。描いた結果がなければ tex を字のまま出して知らせる。
|
|
16
|
+
// 知らないデータ(loop の値、ない箱を指す辺)は ctx.report で知らせる(1.x の console.warn / console.error の代わり)
|
|
17
|
+
egatariParts.define({name: "flow", version: "2.0.4", requires: {base: "1.1.0", "markup-reader": "1.0.1"}, element: "m-flow"}, function(deps){
|
|
18
|
+
"use strict";
|
|
19
|
+
var base = deps.base, MR = deps["markup-reader"];
|
|
20
|
+
// 複数行の名前の行の位置(1 行目からの下がり、px)と、上下に足す余白。base 1.1.0 で mtext の下付き・上付きは 24px 以上になり、
|
|
21
|
+
// 行の間 lh(字の 1.25 倍)では上の行の下付きと下の行の上付きが重なる。下付きの字の下端は基線から添字の 0.75 倍まで、上付きの字の
|
|
22
|
+
// 上端は添字の 1.4 倍まで(Source Sans 3 で測った値。BIZ UDPGothic ではそれぞれ 0.5 倍と 1.2 倍)。上の行に下付き(_)があれば
|
|
23
|
+
// その下端から字の下の余白(字の 0.12 倍)を引いた分、下の行に上付き(^)があればその上端から字の上の高さ(0.88 倍)を引いた分だけ
|
|
24
|
+
// 行の間を広げる。top・bottom は 1 行目の上付き・最後の行の下付きの分(添字がなければどれも 0 で、行は lh ずつ)
|
|
25
|
+
function subX(s, fs){ var ss = Math.max(24, Math.round(fs * 0.75)); return /_./.test(s) ? Math.max(0, 0.75 * ss - 0.12 * fs) : 0; }
|
|
26
|
+
function supX(s, fs){ var ss = Math.max(24, Math.round(fs * 0.75)); return /\^./.test(s) ? Math.max(0, 1.4 * ss - 0.88 * fs) : 0; }
|
|
27
|
+
function lineYs(lines, lh, fs){
|
|
28
|
+
var y = [0];
|
|
29
|
+
for (var j = 1; j < lines.length; j++) y.push(y[j - 1] + lh + subX(lines[j - 1], fs) + supX(lines[j], fs));
|
|
30
|
+
y.top = supX(lines[0], fs); y.bottom = subX(lines[lines.length - 1], fs);
|
|
31
|
+
return y;
|
|
32
|
+
}
|
|
33
|
+
return class extends base.MotionElement {
|
|
34
|
+
static partName = "flow"; // the registry name (define meta name), for report prefixes
|
|
35
|
+
// 組み立てのときに描く欄(spec §6.2)。式の意味は字(tex)でも持つので、KaTeX の出力は HTML だけ
|
|
36
|
+
static build = {fields: {"nodes[].tex": "katex"}, options: {katex: {output: "html"}}};
|
|
37
|
+
// データの形(kit/shape.js)。座標は要素の箱の px(x, y は箱の左上)
|
|
38
|
+
static shape = {
|
|
39
|
+
schema: {type: "object", additionalProperties: false, required: ["nodes"], properties: {
|
|
40
|
+
nodes: {type: "array", minItems: 1, items: {type: "object", required: ["id", "x", "y", "w", "h"], properties: {
|
|
41
|
+
id: {type: ["string", "integer"]}, label: {type: "string"}, tex: {type: "string"}, texHtml: {type: "string"}, x: {type: "number"}, y: {type: "number"}, w: {type: "number"}, h: {type: "number"},
|
|
42
|
+
step: {type: "integer"}, tone: {type: "string"}, at: {type: "number"}, shape: {type: "string"}}}},
|
|
43
|
+
edges: {type: "array", items: {type: "object", required: ["from", "to"], properties: {
|
|
44
|
+
from: {type: ["string", "integer"]}, to: {type: ["string", "integer"]}, step: {type: "integer"}, label: {type: "string"}, tone: {type: "string"}, dash: {type: "boolean"},
|
|
45
|
+
kind: {enum: ["money", "service", "data"]}, amount: {type: ["string", "number"]}, shift: {type: "number"}, pulse: {type: ["boolean", "array"]}, pulseAt: {type: "number"}, offset: {type: "number"}, at: {type: "number"},
|
|
46
|
+
loop: {type: ["boolean", "string"]}}}},
|
|
47
|
+
renderers: {type: "object"}}},
|
|
48
|
+
example: {nodes: [{id: "a", label: "観測", x: 20, y: 40, w: 200, h: 70, step: 1}, {id: "b", label: "更新", x: 320, y: 40, w: 200, h: 70, step: 2}], edges: [{from: "a", to: "b", step: 2}]},
|
|
49
|
+
};
|
|
50
|
+
static props = {
|
|
51
|
+
fade: {type: "number", default: 0.45, doc: "箱が現れる秒数"},
|
|
52
|
+
draw: {type: "number", default: 0.6, doc: "矢印を引く秒数"},
|
|
53
|
+
size: {type: "number", default: 26, doc: "箱の字の大きさ(px)"},
|
|
54
|
+
"pulse-dur": {type: "number", default: 1.2, doc: "pulse の点が始点から終点まで流れる秒数"},
|
|
55
|
+
"pulse-n": {type: "number", default: 3, doc: "pulse で流れる点の数"},
|
|
56
|
+
"label-gap": {type: "number", default: 12, doc: "辺の名前・金額と箱との最小の間(px)。近ければ線から離す"},
|
|
57
|
+
};
|
|
58
|
+
get steps(){
|
|
59
|
+
if (this.getAttribute("steps") !== null) return +this.getAttribute("steps");
|
|
60
|
+
var d = this.data || {}, m = 0;
|
|
61
|
+
(d.nodes || []).concat(d.edges || []).forEach(function(x){ m = Math.max(m, x.step || 1); });
|
|
62
|
+
// pulse の段階([段階, …])も数える
|
|
63
|
+
(d.edges || []).forEach(function(x){ if (Array.isArray(x.pulse)) x.pulse.forEach(function(k){ m = Math.max(m, k); }); });
|
|
64
|
+
return m;
|
|
65
|
+
}
|
|
66
|
+
mount(ctx){
|
|
67
|
+
var P = this.props, D = base, self = this, data = this.data || {nodes: [], edges: []}, sk = MR.sink(this, ctx);
|
|
68
|
+
this.style.display = "block"; this.style.position = "relative";
|
|
69
|
+
var W = this.clientWidth, H = this.clientHeight;
|
|
70
|
+
var svg = D.el("svg", {viewBox: "0 0 " + W + " " + H, width: W, height: H, style: "display:block;overflow:visible"}, this);
|
|
71
|
+
var gE = D.el("g", {}, svg), gN = D.el("g", {}, svg);
|
|
72
|
+
var tone = {term: ["var(--term)", "var(--term-tint)", "var(--fg)"], emph: ["var(--emph)", "var(--emph-tint)", "var(--emph)"],
|
|
73
|
+
muted: ["var(--muted)", "var(--bg)", "var(--fg)"]}; // muted:枠は --muted、字は --fg(字は情報。薄い字は投影で読めない)
|
|
74
|
+
var byId = {}, dup = [];
|
|
75
|
+
// 箱。段階の中の開始時刻 at(既定 0)
|
|
76
|
+
this.nodes = (data.nodes || []).map(function(n){
|
|
77
|
+
var c = tone[n.tone || "term"] || tone.term, cx = n.x + n.w / 2, cy = n.y + n.h / 2;
|
|
78
|
+
var g = D.el("g", {opacity: 0}, gN);
|
|
79
|
+
if (n.shape === "diamond") D.el("polygon", {class: "box", points: [cx, n.y, n.x + n.w, cy, cx, n.y + n.h, n.x, cy].join(" "), "stroke-linejoin": "round", fill: c[1], stroke: c[0], "stroke-width": n.tone === "emph" ? 3 : 2.2}, g);
|
|
80
|
+
else D.el("rect", {class: "box", x: n.x, y: n.y, width: n.w, height: n.h, rx: 10, fill: c[1], stroke: c[0], "stroke-width": n.tone === "emph" ? 3 : 2.2}, g);
|
|
81
|
+
// 式の div は行の中の inline-block なので、上端にそろえないと親の行の高さの分だけ下へずれる
|
|
82
|
+
if (n.tex) MR.texFrame(g, cx, cy + P.size * 0.36, n, "tex", {size: P.size, anchor: "middle", color: c[2], w: n.w, h: n.h}, sk).firstChild.style.verticalAlign = "top";
|
|
83
|
+
else {
|
|
84
|
+
var lines = String(n.label || "").split("\n"), ys = lineYs(lines, P.size * 1.25, P.size), y0 = cy - ys[ys.length - 1] / 2 + P.size * 0.36;
|
|
85
|
+
lines.forEach(function(s, j){ D.mtext(g, cx, y0 + ys[j], s, {"text-anchor": "middle", "font-size": P.size, fill: c[2]}); });
|
|
86
|
+
}
|
|
87
|
+
var o = {g: g, cx: cx, cy: cy, n: n, step: n.step || 1, at: n.at || 0};
|
|
88
|
+
if (byId[n.id]) dup.push(JSON.stringify(n.id));
|
|
89
|
+
byId[n.id] = o;
|
|
90
|
+
return o;
|
|
91
|
+
});
|
|
92
|
+
// 同じ id の箱が 2 つ以上あれば、辺は後に書いた箱を指す(知らせる)
|
|
93
|
+
if (dup.length) ctx.report("data", "[flow] node ids are not unique: " + dup.join(", ") + " (edges use the last node with that id)");
|
|
94
|
+
// 矢印:箱の中心どうしを結ぶ線を、箱の縁で切る
|
|
95
|
+
function edgePt(n, dx, dy, gap){
|
|
96
|
+
var hw = n.w / 2 + gap, hh = n.h / 2 + gap;
|
|
97
|
+
// ひし形は |dx| / hw + |dy| / hh = 1 の上で切る
|
|
98
|
+
var s = n.shape === "diamond" ? 1 / (Math.abs(dx) / hw + Math.abs(dy) / hh) : Math.min(dx ? hw / Math.abs(dx) : Infinity, dy ? hh / Math.abs(dy) : Infinity);
|
|
99
|
+
return [n.x + n.w / 2 + dx * s, n.y + n.h / 2 + dy * s];
|
|
100
|
+
}
|
|
101
|
+
// 戻る辺の道:箱の side の側の辺の中点から出て、両端の間にある箱(と両端)の外側 28 px を回り、戻り先の同じ側に入る
|
|
102
|
+
function loopRoute(lp, A, B){
|
|
103
|
+
var sd = lp === true ? (Math.abs(B.y + B.h / 2 - A.y - A.h / 2) >= Math.abs(B.x + B.w / 2 - A.x - A.w / 2) ? "right" : "bottom") : lp;
|
|
104
|
+
if (!/^(left|right|top|bottom)$/.test(sd)){ ctx.report("data", "[flow] loop=" + JSON.stringify(lp) + " is not true, left, right, top or bottom (right is used)"); sd = "right"; }
|
|
105
|
+
var hz = sd === "left" || sd === "right", sg = sd === "right" || sd === "bottom" ? 1 : -1;
|
|
106
|
+
var out = function(n){ return hz ? [sg > 0 ? n.x + n.w + 6 : n.x - 6, n.y + n.h / 2] : [n.x + n.w / 2, sg > 0 ? n.y + n.h + 6 : n.y - 6]; };
|
|
107
|
+
var q0 = out(A), q1 = out(B), j = hz ? 1 : 0;
|
|
108
|
+
// 箱から同じ箱へ戻る辺:その側の辺の 1/4 と 3/4 の所から出入りする(小さな輪)
|
|
109
|
+
if (A === B){ var u = (hz ? A.h : A.w) / 4; q0[j] -= u; q1[j] += u; }
|
|
110
|
+
var lo = Math.min(q0[j], q1[j]), hi = Math.max(q0[j], q1[j]);
|
|
111
|
+
var ch = sg > 0 ? Math.max(q0[1 - j], q1[1 - j]) : Math.min(q0[1 - j], q1[1 - j]);
|
|
112
|
+
self.nodes.forEach(function(o){
|
|
113
|
+
var n = o.n, a0 = hz ? n.y : n.x, a1 = hz ? n.y + n.h : n.x + n.w;
|
|
114
|
+
if (a1 < lo - 12 || a0 > hi + 12) return;
|
|
115
|
+
var f = hz ? (sg > 0 ? n.x + n.w : n.x) : (sg > 0 ? n.y + n.h : n.y);
|
|
116
|
+
ch = sg > 0 ? Math.max(ch, f + 6) : Math.min(ch, f - 6);
|
|
117
|
+
});
|
|
118
|
+
ch += sg * 28;
|
|
119
|
+
var pts = hz ? [q0, [ch, q0[1]], [ch, q1[1]], q1] : [q0, [q0[0], ch], [q1[0], ch], q1];
|
|
120
|
+
return {pts: pts, poly: self.poly(pts), hz: hz, sg: sg, ch: ch};
|
|
121
|
+
}
|
|
122
|
+
// 線の形を先に全部決める(辺の字は、ほかの辺の線と先に置いた字を避けて置くので)
|
|
123
|
+
var es = data.edges || [];
|
|
124
|
+
// 同じ 2 つの箱の行きと帰りで、どちらも shift も loop も書いていなければ、それぞれ 10 px ずらす(2 本の間は 20 px)
|
|
125
|
+
var autoShift = function(e){
|
|
126
|
+
return !e.shift && !e.loop && es.some(function(f){ return f !== e && f.from === e.to && f.to === e.from && !f.shift && !f.loop; });
|
|
127
|
+
};
|
|
128
|
+
var geo = es.map(function(e){
|
|
129
|
+
var a = byId[e.from], b = byId[e.to];
|
|
130
|
+
if (!a || !b) return null;
|
|
131
|
+
var dx = b.cx - a.cx, dy = b.cy - a.cy, L = Math.hypot(dx, dy) || 1;
|
|
132
|
+
// 同じ箱へ戻る辺(loop なし)は、右に回る輪にする。中心の重なる 2 つの箱の辺は引けない
|
|
133
|
+
var lp = e.loop;
|
|
134
|
+
if (!lp && a === b){ ctx.report("data", "[flow] the edge " + JSON.stringify(e.from) + " → " + JSON.stringify(e.to) + " goes from a node to itself; it is drawn as a loop on the right (set loop to choose the side)"); lp = "right"; }
|
|
135
|
+
if (!lp && !dx && !dy){ ctx.report("data", "[flow] the edge " + JSON.stringify(e.from) + " → " + JSON.stringify(e.to) + " joins two nodes with the same centre (not drawn)"); return false; }
|
|
136
|
+
var p0 = lp ? null : edgePt(a.n, dx / L, dy / L, 6), p1 = lp ? null : edgePt(b.n, -dx / L, -dy / L, 6), route = null;
|
|
137
|
+
// Boxes apart along one axis: the edge ends at the middle of the target's facing side and starts on the source's facing
|
|
138
|
+
// side, as near the target's centre line as that side allows (a centre-to-centre line would enter a wide, flat target
|
|
139
|
+
// through its top or bottom, between stacked boxes)
|
|
140
|
+
var face = 0, sh = e.shift || (autoShift(e) ? 10 : 0); // face 1: left/right sides face each other, 2: top/bottom
|
|
141
|
+
if (!lp){
|
|
142
|
+
var A = a.n, B = b.n, gx = Math.max(B.x - A.x - A.w, A.x - B.x - B.w), gy = Math.max(B.y - A.y - A.h, A.y - B.y - B.h);
|
|
143
|
+
if (gx > 0 || gy > 0){
|
|
144
|
+
// shift runs along the facing sides (to the right of travel); the start follows the shifted end's line
|
|
145
|
+
var hz = gx >= gy, sg = hz ? Math.sign(dx) : Math.sign(dy), m = function(n){ return Math.min((hz ? n.h : n.w) / 4, 12); };
|
|
146
|
+
p1 = hz ? [b.cx - sg * (B.w / 2 + 6), b.cy + sg * sh] : [b.cx - sg * sh, b.cy - sg * (B.h / 2 + 6)];
|
|
147
|
+
if (A.shape === "diamond"){ var ux = p1[0] - a.cx, uy = p1[1] - a.cy, ul = Math.hypot(ux, uy) || 1; p0 = edgePt(A, ux / ul, uy / ul, 6); }
|
|
148
|
+
else p0 = hz ? [a.cx + sg * (A.w / 2 + 6), Math.max(A.y + m(A), Math.min(A.y + A.h - m(A), p1[1]))]
|
|
149
|
+
: [Math.max(A.x + m(A), Math.min(A.x + A.w - m(A), p1[0])), a.cy + sg * (A.h / 2 + 6)];
|
|
150
|
+
dx = p1[0] - p0[0]; dy = p1[1] - p0[1]; L = Math.hypot(dx, dy) || 1; face = hz ? 1 : 2;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (lp) route = loopRoute(lp, a.n, b.n);
|
|
154
|
+
if (route){ p0 = route.pts[0]; p1 = route.pts[3]; }
|
|
155
|
+
// shift:線に垂直に(進む向きの右へ)ずらす。行きと帰りの 2 本を並べるときに使う(向かい合う辺の間はもうずらしてある)
|
|
156
|
+
if (sh && !face){ var sx = -dy / L * sh, sy = dx / L * sh; p0 = [p0[0] + sx, p0[1] + sy]; p1 = [p1[0] + sx, p1[1] + sy]; }
|
|
157
|
+
// self-check: an end that leaves its box's side (a shift larger than the side) is reported, not silently drawn
|
|
158
|
+
var off = function(p, n){ return face === 1 ? p[1] < n.y || p[1] > n.y + n.h : face === 2 ? p[0] < n.x || p[0] > n.x + n.w : false; };
|
|
159
|
+
if (off(p1, b.n)) ctx.report("layout", "[flow] the edge " + JSON.stringify(e.from) + " → " + JSON.stringify(e.to) + " leaves the side of its box (shift " + sh + " is larger than the side allows); use a smaller shift or a taller box");
|
|
160
|
+
return {a: a, b: b, dx: dx, dy: dy, L: L, p0: p0, p1: p1, route: route, shift: sh};
|
|
161
|
+
});
|
|
162
|
+
// 線分(戻る辺は折れ線の 3 本)と、置いた字の箱 [x0, y0, x1, y1]
|
|
163
|
+
var segs = geo.map(function(q){ return !q ? [] : q.route ? [0, 1, 2].map(function(i){ return [q.route.pts[i], q.route.pts[i + 1]]; }) : [[q.p0, q.p1]]; });
|
|
164
|
+
var boxes = [];
|
|
165
|
+
this.edges = es.map(function(e, ei){
|
|
166
|
+
var q = geo[ei];
|
|
167
|
+
if (q === false) return null; // 知らせてある
|
|
168
|
+
if (!q){ ctx.report("data", "[flow] an edge refers to an unknown node: " + JSON.stringify(e.from) + " → " + JSON.stringify(e.to) + " (not drawn)"); return null; }
|
|
169
|
+
var a = q.a, b = q.b, dx = q.dx, dy = q.dy, L = q.L, p0 = q.p0, p1 = q.p1, route = q.route;
|
|
170
|
+
var others = [].concat.apply([], segs.filter(function(_, j){ return j !== ei; }));
|
|
171
|
+
// kind:money(--emph の太線)、service(--term)、data(--muted の破線)。tone と dash を書けばそちらが勝つ
|
|
172
|
+
var kind = {money: ["emph", false, 3.5], service: ["term", false, 2.5], data: ["muted", true, 2.5]}[e.kind] || [null, false, 2.5];
|
|
173
|
+
var tn = e.tone || kind[0], dash = e.dash !== undefined ? e.dash : kind[1];
|
|
174
|
+
var col = tn ? (tone[tn] || tone.term)[0] : "var(--fg)";
|
|
175
|
+
var g = D.el("g", {opacity: 0}, gE);
|
|
176
|
+
var line = route ? D.el("path", {d: "", fill: "none", "stroke-linejoin": "round", stroke: col, "stroke-width": kind[2], "stroke-dasharray": dash ? "8 6" : "none"}, g)
|
|
177
|
+
: D.el("line", {x1: p0[0], y1: p0[1], x2: p0[0], y2: p0[1], stroke: col, "stroke-width": kind[2], "stroke-dasharray": dash ? "8 6" : "none"}, g);
|
|
178
|
+
var head = D.el("path", {d: "M0,0L-16,-7L-16,7Z", fill: col}, g);
|
|
179
|
+
var lab = null, amt = null;
|
|
180
|
+
// 線の中点から、線に垂直な向き(上か右)へずらして置く。amount は反対の側(label がなければ同じ側)に太字で
|
|
181
|
+
var nx = -dy / L, ny = dx / L;
|
|
182
|
+
if (ny > 0 || (ny === 0 && nx < 0)){ nx = -nx; ny = -ny; }
|
|
183
|
+
// 字の箱が箱(node)に label-gap px より近いか、図の外に出るか、ほかの辺の線(4 px の間をとる)か先に置いた字に
|
|
184
|
+
// かかるか、自分の線とのあいだにほかの辺の線が入るなら、線から離していく(6 px ずつ、60 px まで)。
|
|
185
|
+
// それでも置けなければ線の反対の側で同じように試す(label は amount があるときは自分の側だけ。amount は両側)。
|
|
186
|
+
// どこにも置けなければ最初の位置に置いて知らせる(layout)。
|
|
187
|
+
// 幅は字から見積もる(base の textWidth:テーマの字で測った幅)ので、字の読み込みによらず同じ位置になる
|
|
188
|
+
var mid = [(p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2];
|
|
189
|
+
var side = function(sg, text, attrs, both){
|
|
190
|
+
var fs = attrs["font-size"] || 24, base = e.offset !== undefined ? e.offset : 18, pos = null, anc0 = null, box0 = null, free = false;
|
|
191
|
+
var w = D.textWidth(text, fs, {font: self, mark: true});
|
|
192
|
+
var sides = both ? [sg, -sg] : [sg];
|
|
193
|
+
search: for (var si = 0; si < sides.length; si++){
|
|
194
|
+
var sgn = sides[si], anc = Math.abs(nx) < 0.3 ? "middle" : nx * sgn > 0 ? "start" : "end";
|
|
195
|
+
for (var extra = 0; extra <= 60; extra += 6){
|
|
196
|
+
var off = (base + extra) * sgn, mx = (p0[0] + p1[0]) / 2 + nx * off, my = (p0[1] + p1[1]) / 2 + ny * off;
|
|
197
|
+
var y = my + (ny * sgn < -0.3 ? 0 : ny * sgn > 0.3 ? 20 : 8);
|
|
198
|
+
var x0 = anc === "start" ? mx : anc === "end" ? mx - w : mx - w / 2, gp = P["label-gap"];
|
|
199
|
+
var bx = [x0, y - fs * 0.88, x0 + w, y + fs * 0.22], c = [(bx[0] + bx[2]) / 2, (bx[1] + bx[3]) / 2];
|
|
200
|
+
var hit = x0 < 0 || x0 + w > W || y - fs * 0.88 < 0 || y + fs * 0.22 > H || self.nodes.some(function(o){ var n = o.n;
|
|
201
|
+
return x0 < n.x + n.w + gp && x0 + w > n.x - gp && y - fs * 0.88 < n.y + n.h + gp && y + fs * 0.22 > n.y - gp; }) ||
|
|
202
|
+
boxes.some(function(r){ return bx[0] < r[2] + 4 && bx[2] > r[0] - 4 && bx[1] < r[3] + 4 && bx[3] > r[1] - 4; }) ||
|
|
203
|
+
others.some(function(sgm){ return segHitsBox(sgm, [bx[0] - 4, bx[1] - 4, bx[2] + 4, bx[3] + 4]) || segsCross(sgm, [mid, c]); });
|
|
204
|
+
if (!pos){ pos = [mx, y]; anc0 = anc; box0 = bx; }
|
|
205
|
+
if (!hit){ pos = [mx, y]; anc0 = anc; box0 = bx; free = true; break search; }
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (!free) ctx.report("layout", "[flow] no free place for the edge text " + JSON.stringify(text) + " (" + JSON.stringify(e.from) + " → " + JSON.stringify(e.to) + "): it touches a box, a line or another text; move the boxes apart or set offset");
|
|
209
|
+
boxes.push(box0);
|
|
210
|
+
return D.mtext(g, pos[0], pos[1], text, Object.assign({"text-anchor": anc0, "font-size": 24, opacity: 0}, attrs));
|
|
211
|
+
};
|
|
212
|
+
// shift でずらした辺は、ずらした側に書く(行きと帰りの字が同じ側に重ならない)
|
|
213
|
+
var sg = q.shift && (-dy * nx + dx * ny) * q.shift < 0 ? -1 : 1;
|
|
214
|
+
var hasAmt = e.amount !== undefined && e.amount !== "";
|
|
215
|
+
// 戻る辺の名前は回り道の長い線の外側に、金額は内側に。外側が図の外に出るなら名前も内側に
|
|
216
|
+
var rside = function(out, text, attrs){
|
|
217
|
+
var r = route, m = r.hz ? (r.pts[1][1] + r.pts[2][1]) / 2 : (r.pts[1][0] + r.pts[2][0]) / 2, o2 = out ? r.sg : -r.sg;
|
|
218
|
+
var tw = D.textWidth(text, 26, {font: self, mark: true});
|
|
219
|
+
if (out && (r.hz ? (o2 > 0 ? r.ch + 10 + tw > W : r.ch - 10 - tw < 0) : (o2 > 0 ? r.ch + 30 > H : r.ch - 34 < 0))) o2 = -o2;
|
|
220
|
+
var at2 = r.hz ? {x: r.ch + o2 * 10, y: m + 8, a: o2 > 0 ? "start" : "end"} : {x: m, y: r.ch + (o2 > 0 ? 30 : -10), a: "middle"};
|
|
221
|
+
var bx0 = at2.a === "start" ? at2.x : at2.a === "end" ? at2.x - tw : at2.x - tw / 2;
|
|
222
|
+
boxes.push([bx0, at2.y - 26 * 0.88, bx0 + tw, at2.y + 26 * 0.22]);
|
|
223
|
+
return D.mtext(g, +at2.x.toFixed(2), +at2.y.toFixed(2), text, Object.assign({"text-anchor": at2.a, "font-size": 24, opacity: 0}, attrs));
|
|
224
|
+
};
|
|
225
|
+
if (route){
|
|
226
|
+
if (e.label) lab = rside(true, e.label, {fill: tn ? col : "var(--fg)"});
|
|
227
|
+
if (hasAmt) amt = rside(!e.label, String(e.amount), {fill: col, "font-weight": 700, "font-size": 26});
|
|
228
|
+
} else {
|
|
229
|
+
if (e.label) lab = side(sg, e.label, {fill: tn ? col : "var(--fg)"}, !hasAmt);
|
|
230
|
+
if (hasAmt) amt = side(e.label ? -sg : sg, String(e.amount), {fill: col, "font-weight": 700, "font-size": 26}, true);
|
|
231
|
+
}
|
|
232
|
+
var step = e.step || Math.max(a.step, b.step);
|
|
233
|
+
// 同じ段階で出る箱があれば、その箱が出てから引く
|
|
234
|
+
var after = Math.max(a.step === step ? a.at + P.fade * 0.6 : 0, b.step === step ? b.at + P.fade * 0.6 : 0);
|
|
235
|
+
var at = e.at !== undefined ? e.at : after;
|
|
236
|
+
// pulse の段階と開始時刻:辺の段階なら引き終わってから、ほかの段階なら pulseAt(既定 0.2 秒)から
|
|
237
|
+
var pk = e.pulse === true ? [step] : Array.isArray(e.pulse) ? e.pulse : [];
|
|
238
|
+
var pulses = pk.map(function(k){ return {k: k, at: e.pulseAt !== undefined ? e.pulseAt : k === step ? +(at + P.draw).toFixed(3) : 0.2}; });
|
|
239
|
+
var dots = pulses.length ? Array.from({length: Math.max(1, P["pulse-n"])}, function(){ return D.el("circle", {r: kind[2] + 3.5, fill: col, stroke: "var(--bg)", "stroke-width": 2, opacity: 0}, gE); }) : [];
|
|
240
|
+
var o = {g: g, line: line, head: head, lab: lab, amt: amt, p0: p0, p1: p1, ang: Math.atan2(dy, dx) * 180 / Math.PI, step: step, at: at, pulses: pulses, dots: dots};
|
|
241
|
+
if (route) o.poly = route.poly;
|
|
242
|
+
return o;
|
|
243
|
+
}).filter(Boolean);
|
|
244
|
+
sk.flush(); // what the formulas have to report, once per kind (spec §6.2)
|
|
245
|
+
// 線分 s = [[x, y], [x, y]] が箱 r = [x0, y0, x1, y1] にかかるか(Liang–Barsky)
|
|
246
|
+
function segHitsBox(sg, r){
|
|
247
|
+
var x = sg[0][0], y = sg[0][1], ddx = sg[1][0] - x, ddy = sg[1][1] - y, t0 = 0, t1 = 1;
|
|
248
|
+
var pp = [-ddx, ddx, -ddy, ddy], qq = [x - r[0], r[2] - x, y - r[1], r[3] - y];
|
|
249
|
+
for (var i = 0; i < 4; i++){
|
|
250
|
+
if (pp[i] === 0){ if (qq[i] < 0) return false; continue; }
|
|
251
|
+
var u = qq[i] / pp[i];
|
|
252
|
+
if (pp[i] < 0){ if (u > t1) return false; if (u > t0) t0 = u; }
|
|
253
|
+
else { if (u < t0) return false; if (u < t1) t1 = u; }
|
|
254
|
+
}
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
// 2 つの線分が交わるか(端を除く)
|
|
258
|
+
function segsCross(s1, s2){
|
|
259
|
+
var o = function(a, b, c){ return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); };
|
|
260
|
+
var d1 = o(s2[0], s2[1], s1[0]), d2 = o(s2[0], s2[1], s1[1]), d3 = o(s1[0], s1[1], s2[0]), d4 = o(s1[0], s1[1], s2[1]);
|
|
261
|
+
return d1 * d2 < 0 && d3 * d4 < 0;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
// 折れ線:点と累積の長さ(戻る辺)
|
|
265
|
+
poly(pts){
|
|
266
|
+
var L = [0];
|
|
267
|
+
for (var i = 1; i < pts.length; i++) L.push(L[i - 1] + Math.hypot(pts[i][0] - pts[i - 1][0], pts[i][1] - pts[i - 1][1]));
|
|
268
|
+
return {pts: pts, L: L, len: L[L.length - 1] || 1};
|
|
269
|
+
}
|
|
270
|
+
// 折れ線の始めから長さ len の点と向き(度)。d:そこまでの線
|
|
271
|
+
polyAt(pl, len){
|
|
272
|
+
var pts = pl.pts, want = Math.max(0, Math.min(pl.len, len)), i = 1, d = "M" + pts[0][0].toFixed(2) + "," + pts[0][1].toFixed(2);
|
|
273
|
+
while (i < pts.length - 1 && pl.L[i] < want){ d += "L" + pts[i][0].toFixed(2) + "," + pts[i][1].toFixed(2); i++; }
|
|
274
|
+
var u = (want - pl.L[i - 1]) / ((pl.L[i] - pl.L[i - 1]) || 1), x = base.lerp(pts[i - 1][0], pts[i][0], u), y = base.lerp(pts[i - 1][1], pts[i][1], u);
|
|
275
|
+
return {x: x, y: y, ang: Math.atan2(pts[i][1] - pts[i - 1][1], pts[i][0] - pts[i - 1][0]) * 180 / Math.PI, d: d + "L" + x.toFixed(2) + "," + y.toFixed(2)};
|
|
276
|
+
}
|
|
277
|
+
// pulse の点の列が流れ終わるまでの長さ(点の間は 0.22 秒)
|
|
278
|
+
pulseLen(){ return this.props["pulse-dur"] + 0.22 * (Math.max(1, this.props["pulse-n"]) - 1); }
|
|
279
|
+
duration(k){
|
|
280
|
+
var P = this.props, d = 0, pl = this.pulseLen();
|
|
281
|
+
this.nodes.forEach(function(o){ if (o.step === k) d = Math.max(d, o.at + P.fade); });
|
|
282
|
+
this.edges.forEach(function(o){
|
|
283
|
+
if (o.step === k) d = Math.max(d, o.at + P.draw + (o.lab || o.amt ? 0.3 : 0));
|
|
284
|
+
o.pulses.forEach(function(q){ if (q.k === k) d = Math.max(d, q.at + pl); });
|
|
285
|
+
});
|
|
286
|
+
return d;
|
|
287
|
+
}
|
|
288
|
+
// pulse の開始時刻を口述原稿の {cue} に合わせられる(pulse のない図はこれまでどおり null)
|
|
289
|
+
cues(k){
|
|
290
|
+
var c = [];
|
|
291
|
+
this.edges.forEach(function(o){ o.pulses.forEach(function(q){ if (q.k === k && q.at > 0 && c.indexOf(q.at) < 0) c.push(q.at); }); });
|
|
292
|
+
return c.length ? c.sort(function(a, b){ return a - b; }) : null;
|
|
293
|
+
}
|
|
294
|
+
render(k, t, ctx){
|
|
295
|
+
var P = this.props, D = base, self = this, preview = !!ctx.preview, PV = 0;
|
|
296
|
+
// 先を薄く見せる表示:後の段階の箱と矢印(名前・金額も)を、最後の形のままテーマの --preview-opacity(なければ 0.45)で描く。
|
|
297
|
+
// pulse の点は今の段階のものだけ
|
|
298
|
+
if (preview) PV = base.previewOpacity(this);
|
|
299
|
+
var ahead = function(step){ return preview && step > k; };
|
|
300
|
+
var pr = function(step, at, dur){ return step < k || ahead(step) ? 1 : step > k ? 0 : D.prog(t, at, dur); };
|
|
301
|
+
this.nodes.forEach(function(o){
|
|
302
|
+
var e = pr(o.step, o.at, P.fade), s = D.lerp(0.92, 1, e);
|
|
303
|
+
o.g.setAttribute("opacity", ahead(o.step) ? PV.toFixed(3) : e.toFixed(3));
|
|
304
|
+
o.g.setAttribute("transform", "translate(" + o.cx + " " + o.cy + ") scale(" + s.toFixed(4) + ") translate(" + (-o.cx) + " " + (-o.cy) + ")");
|
|
305
|
+
});
|
|
306
|
+
this.edges.forEach(function(o){
|
|
307
|
+
var e = pr(o.step, o.at, P.draw);
|
|
308
|
+
if (o.poly){
|
|
309
|
+
// 戻る辺:折れ線を始めから引き、矢じりは先端について動く(線は矢じりの根元まで)
|
|
310
|
+
var want = e * o.poly.len, tip = self.polyAt(o.poly, want), hs2 = Math.min(1, want / 20);
|
|
311
|
+
o.line.setAttribute("d", want > 0 ? self.polyAt(o.poly, want - Math.min(14, want)).d : "");
|
|
312
|
+
o.head.setAttribute("transform", "translate(" + tip.x.toFixed(2) + " " + tip.y.toFixed(2) + ") rotate(" + tip.ang.toFixed(2) + ") scale(" + hs2.toFixed(4) + ")");
|
|
313
|
+
} else {
|
|
314
|
+
var tx = D.lerp(o.p0[0], o.p1[0], e), ty = D.lerp(o.p0[1], o.p1[1], e);
|
|
315
|
+
var L = Math.hypot(o.p1[0] - o.p0[0], o.p1[1] - o.p0[1]) || 1, back = Math.min(14, e * L);
|
|
316
|
+
// 線は矢じりの根元まで。矢じりは線の先端について動く
|
|
317
|
+
o.line.setAttribute("x2", (tx - (o.p1[0] - o.p0[0]) / L * back).toFixed(2));
|
|
318
|
+
o.line.setAttribute("y2", (ty - (o.p1[1] - o.p0[1]) / L * back).toFixed(2));
|
|
319
|
+
// 矢じりは線が 20px 伸びるまでに大きくなる(始めに箱からはみ出さない)
|
|
320
|
+
var hs = Math.min(1, e * L / 20);
|
|
321
|
+
o.head.setAttribute("transform", "translate(" + tx.toFixed(2) + " " + ty.toFixed(2) + ") rotate(" + o.ang.toFixed(2) + ") scale(" + hs.toFixed(4) + ")");
|
|
322
|
+
}
|
|
323
|
+
o.g.setAttribute("opacity", ahead(o.step) ? PV : e > 0 ? 1 : 0);
|
|
324
|
+
var lo = pr(o.step, o.at + P.draw * 0.8, 0.3).toFixed(3);
|
|
325
|
+
if (o.lab) o.lab.setAttribute("opacity", lo);
|
|
326
|
+
if (o.amt) o.amt.setAttribute("opacity", lo);
|
|
327
|
+
// pulse:今の段階に pulse があれば、点の列を (t - 開始) から置く。流れ終わった点と t = Infinity では出さない
|
|
328
|
+
var q = null;
|
|
329
|
+
o.pulses.forEach(function(x){ if (x.k === k) q = x; });
|
|
330
|
+
o.dots.forEach(function(dot, j){
|
|
331
|
+
var u = q && t !== Infinity ? (t - q.at - 0.22 * j) / P["pulse-dur"] : -1;
|
|
332
|
+
if (u <= 0 || u >= 1 || e < 1){ dot.setAttribute("opacity", 0); return; }
|
|
333
|
+
var w = D.prog(u, 0, 1);
|
|
334
|
+
if (o.poly){ var pp = self.polyAt(o.poly, w * o.poly.len); dot.setAttribute("cx", pp.x.toFixed(2)); dot.setAttribute("cy", pp.y.toFixed(2)); }
|
|
335
|
+
else { dot.setAttribute("cx", D.lerp(o.p0[0], o.p1[0], w).toFixed(2)); dot.setAttribute("cy", D.lerp(o.p0[1], o.p1[1], w).toFixed(2)); }
|
|
336
|
+
dot.setAttribute("opacity", Math.min(1, u * 8, (1 - u) * 8).toFixed(3));
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
});
|