egatari 0.6.8 → 0.6.9-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 +2 -2
- package/README.md +2 -2
- package/components.lock.json +84 -3
- package/components2.lock.json +202 -1
- package/kit/check-messages.mjs +5 -0
- package/kit/check.mjs +9 -4
- package/kit/core/check-parity.mjs +62 -0
- package/kit/core/check-source.mjs +56 -0
- package/kit/core/colab-core.mjs +1 -1
- package/kit/core/color-vocab.mjs +8 -2
- package/kit/core/layout-scan.mjs +7 -4
- package/kit/core/llm-pack-assemble.mjs +1 -1
- package/kit/core/llm-pack-data.mjs +185 -9
- package/kit/core/llm-pack.mjs +1 -1
- package/kit/core/narration-lint.mjs +12 -0
- package/kit/core/new-deck.mjs +3 -0
- package/kit/core/part-labels.mjs +6 -0
- package/kit/core/template-text.mjs +5 -3
- package/kit/deck.js +88 -13
- package/kit/instruction-version.json +1 -1
- package/kit/player.json +1 -1
- package/kit/player.lock.json +2 -2
- package/kit/presenter.js +33 -9
- package/kit/tools.js +4 -0
- package/package.json +1 -1
- package/themes/asagi/theme.json +1 -1
- package/themes/asagi/tokens.css +12 -0
- package/themes/business/theme.json +1 -1
- package/themes/business/tokens.css +12 -0
- package/themes/default/README.ja.md +24 -1
- package/themes/default/README.md +24 -1
- package/themes/default/theme.json +1 -1
- package/themes/default/tokens.css +12 -0
- package/themes/kogecha/theme.json +1 -1
- package/themes/kogecha/tokens.css +12 -0
- package/themes/kokugo/theme.json +1 -1
- package/themes/kokugo/tokens.css +12 -0
- package/themes/murasaki/theme.json +1 -1
- package/themes/murasaki/tokens.css +12 -0
- package/themes/sumi/theme.json +1 -1
- package/themes/sumi/tokens.css +12 -0
- package/themes/tokiwa/theme.json +1 -1
- package/themes/tokiwa/tokens.css +12 -0
- package/themes.lock.json +80 -24
- package/vendor/components/CATALOG.md +7 -1
- package/vendor/components/sha256/1298d779fe60ccfc09747fbdba47e2111175bd7fd2385a5b68c8f2644cd267be/README.en.md +23 -0
- package/vendor/components/sha256/1298d779fe60ccfc09747fbdba47e2111175bd7fd2385a5b68c8f2644cd267be/index.js +120 -0
- package/vendor/components/sha256/7aea8341beffcd4ad95c12b7e7e3be3f0f52eb7932d424be9e74e2fbd7339e45/README.en.md +23 -0
- package/vendor/components/sha256/7aea8341beffcd4ad95c12b7e7e3be3f0f52eb7932d424be9e74e2fbd7339e45/index.js +289 -0
- package/vendor/components/sha256/90ed438b40ed8d765348834b2bed2baad9b0053c2168c218335b6d95b9c80283/README.en.md +44 -0
- package/vendor/components/sha256/90ed438b40ed8d765348834b2bed2baad9b0053c2168c218335b6d95b9c80283/index.js +203 -0
- package/vendor/components/sha256/a69ab76ff7d0746d093b4e606d7b113be921cbb156edc16c2ad40aafab280d42/README.en.md +22 -0
- package/vendor/components/sha256/a69ab76ff7d0746d093b4e606d7b113be921cbb156edc16c2ad40aafab280d42/index.js +145 -0
- package/vendor/components/sha256/d75c461bb7b2b1c9a2b6d15bee1e11bcec88ef94931174d6b222cd309f6a0287/README.en.md +24 -0
- package/vendor/components/sha256/d75c461bb7b2b1c9a2b6d15bee1e11bcec88ef94931174d6b222cd309f6a0287/index.js +103 -0
- package/vendor/components/sha256/decb7c026527ea269a401efedcb5d4d56d186074b286d2f94772068bef56b256/README.en.md +31 -0
- package/vendor/components/sha256/decb7c026527ea269a401efedcb5d4d56d186074b286d2f94772068bef56b256/index.js +267 -0
- package/vendor/components/sha256/f13af97d7ebf9893d5783eff27e4e1ce092ccd01783b332ad01aa0d201356548/README.en.md +38 -0
- package/vendor/components/sha256/f13af97d7ebf9893d5783eff27e4e1ce092ccd01783b332ad01aa0d201356548/index.js +266 -0
- package/vendor/components-2/manifest-2.6.0.json +2104 -0
- package/vendor/egatari-spec/SOURCE.json +2 -2
- package/vendor/egatari-spec/schema/egatari-audio.schema.json +21 -0
- package/vendor/themes/asagi/1.1.0/theme.json +14 -0
- package/vendor/themes/asagi/1.1.0/tokens.css +18 -0
- package/vendor/themes/business/1.1.0/theme.json +5 -0
- package/vendor/themes/business/1.1.0/tokens.css +29 -0
- package/vendor/themes/default/1.3.0/theme.json +4 -0
- package/vendor/themes/default/1.3.0/tokens.css +74 -0
- package/vendor/themes/kogecha/1.1.0/theme.json +10 -0
- package/vendor/themes/kogecha/1.1.0/tokens.css +17 -0
- package/vendor/themes/kokugo/1.1.0/theme.json +5 -0
- package/vendor/themes/kokugo/1.1.0/tokens.css +13 -0
- package/vendor/themes/murasaki/1.1.0/theme.json +10 -0
- package/vendor/themes/murasaki/1.1.0/tokens.css +13 -0
- package/vendor/themes/sumi/1.1.0/theme.json +10 -0
- package/vendor/themes/sumi/1.1.0/tokens.css +37 -0
- package/vendor/themes/tokiwa/1.1.0/theme.json +10 -0
- package/vendor/themes/tokiwa/1.1.0/tokens.css +15 -0
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
// m-code: a program, successive line explanations, then its supplied output. Never executes code.
|
|
2
|
+
egatariParts.define({name: "code", version: "2.0.0", requires: {base: "1.1.0"}, element: "m-code"}, function(deps){
|
|
3
|
+
"use strict";
|
|
4
|
+
var base = deps.base, MONO = 'ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace';
|
|
5
|
+
var TEXT = {
|
|
6
|
+
ja: {output: "出力", run: "▶ 実行", readable: "コードと出力", summary: function(lang, n, output){ return (lang || "プログラム") + " のコード " + n + " 行" + (output ? "と出力" : ""); }},
|
|
7
|
+
en: {output: "Output", run: "▶ Run", readable: "Code and output", summary: function(lang, n, output){ return (lang || "Program") + ": " + n + " lines of code" + (output ? " and output" : ""); }}
|
|
8
|
+
};
|
|
9
|
+
function set(e, a, v){ e.setAttribute(a, String(v)); }
|
|
10
|
+
function lines(s){ return s.replace(/\r\n?/g, "\n").split("\n"); }
|
|
11
|
+
function monoWidth(s, fs){ return [...s].reduce(function(w, ch){ return w + (/[\u0000-\u024f]/.test(ch) ? 0.6 : 1); }, 0) * fs; }
|
|
12
|
+
function cut(s, w, fs){
|
|
13
|
+
if (monoWidth(s, fs) <= w) return s;
|
|
14
|
+
var ch = [...s];
|
|
15
|
+
while (ch.length && monoWidth(ch.join("") + "…", fs) > w) ch.pop();
|
|
16
|
+
return w >= fs ? ch.join("") + "…" : "";
|
|
17
|
+
}
|
|
18
|
+
// Recognize line comments outside quoted strings; retain multiline Python strings / JS templates.
|
|
19
|
+
function comments(program, lang){
|
|
20
|
+
var quote = "", triple = false;
|
|
21
|
+
return program.map(function(s){
|
|
22
|
+
for (var i = 0; i < s.length; i++){
|
|
23
|
+
var c = s[i];
|
|
24
|
+
if (c === "\\"){ i++; continue; }
|
|
25
|
+
if (quote){
|
|
26
|
+
if (c === quote && (!triple || s.slice(i, i + 3) === quote.repeat(3))){ i += triple ? 2 : 0; quote = ""; triple = false; }
|
|
27
|
+
} else if (c === '"' || c === "'" || (lang === "js" || lang === "javascript") && c === "`"){
|
|
28
|
+
quote = c; triple = lang === "python" && s.slice(i, i + 3) === c.repeat(3); if (triple) i += 2;
|
|
29
|
+
} else if ((lang === "python" || lang === "r" || lang === "shell" || lang === "sh" || lang === "bash") && c === "#" && (!(lang === "shell" || lang === "sh" || lang === "bash") || i === 0 || /\s/.test(s[i - 1])) || (lang === "js" || lang === "javascript") && s.slice(i, i + 2) === "//") return i;
|
|
30
|
+
}
|
|
31
|
+
if (!triple && quote !== "`") quote = "";
|
|
32
|
+
return -1;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
// Wrap prose on words where possible, falling back to characters for Japanese / long tokens.
|
|
36
|
+
function wrap(s, w, fs, font){
|
|
37
|
+
var out = [];
|
|
38
|
+
lines(s).forEach(function(p){
|
|
39
|
+
var rest = [...p];
|
|
40
|
+
while (rest.length){
|
|
41
|
+
var n = 0;
|
|
42
|
+
while (n < rest.length && base.textWidth(rest.slice(0, n + 1).join(""), fs, {font: font}) <= w) n++;
|
|
43
|
+
n = Math.max(1, n);
|
|
44
|
+
if (n < rest.length){ var space = rest.slice(0, n + 1).lastIndexOf(" "); if (space > 0) n = space; }
|
|
45
|
+
out.push(rest.splice(0, n).join("").trimEnd());
|
|
46
|
+
while (rest[0] === " ") rest.shift();
|
|
47
|
+
}
|
|
48
|
+
if (!p) out.push("");
|
|
49
|
+
});
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
return class extends base.MotionElement {
|
|
53
|
+
static partName = "code";
|
|
54
|
+
static shape = {schema: {type: "object", additionalProperties: false, required: ["code", "steps"], properties: {
|
|
55
|
+
code: {type: "string"}, lang: {type: "string"}, output: {type: "string"},
|
|
56
|
+
steps: {type: "array", items: {type: "object", additionalProperties: false, required: ["lines", "note"], properties: {
|
|
57
|
+
lines: {type: "array", minItems: 1, maxItems: 2, items: {type: "integer"}}, note: {type: "string"}
|
|
58
|
+
}}}
|
|
59
|
+
}}, example: /*@egatari-block example*/{"lang":"python","code":"import pandas as pd\ndf = pd.read_csv(\"sales.csv\")\ndf.groupby(\"store\")[\"sales\"].mean()","steps":[{"lines":[1],"note":"pandas を読み込む"},{"lines":[2],"note":"CSV を表として読む"},{"lines":[3],"note":"店ごとの平均"}],"output":"store\nA 120.5\nB 98.0"}/*@egatari-block-end*/};
|
|
60
|
+
static props = {
|
|
61
|
+
dur: {type: "number", default: 0.6, doc: "段階の秒数"},
|
|
62
|
+
size: {type: "number", default: 0, doc: "文字 px(0 は既定)"},
|
|
63
|
+
outputPos: {type: "enum", values: ["auto", "below", "right"], default: "auto", doc: "出力の位置"}
|
|
64
|
+
};
|
|
65
|
+
get steps(){ return this.data.steps.length + 1 + (this.data.output !== undefined ? 1 : 0); }
|
|
66
|
+
duration(k){ return k >= 1 && k <= this.steps ? this.props.dur : 0; }
|
|
67
|
+
label(g, x, y, s, fs, color, mono){
|
|
68
|
+
var e = base.el("text", {x: x, y: y, "font-size": fs, fill: color || "var(--fg)"}, g);
|
|
69
|
+
// 等幅と空白:テーマの `svg text{font-family}` は属性より強いので style に書く。行頭の字下げを残す(white-space: pre)
|
|
70
|
+
if (mono){ e.setAttribute("style", (e.getAttribute("style") || "") + "font-family:" + MONO + ";white-space:pre;"); }
|
|
71
|
+
e.textContent = s; return e;
|
|
72
|
+
}
|
|
73
|
+
mount(ctx){
|
|
74
|
+
var self = this, d = this.data, P = this.props, T = /^ja\b/i.test(ctx.lang || "") ? TEXT.ja : TEXT.en;
|
|
75
|
+
if (!(Number.isFinite(P.dur) && P.dur > 0)){ ctx.report("attr", "[code] dur must be positive; using 0.6"); P.dur = 0.6; }
|
|
76
|
+
if (!(P.size === 0 || Number.isFinite(P.size) && P.size >= 24)){ ctx.report("attr", "[code] size must be 0 or at least 24; using defaults"); P.size = 0; }
|
|
77
|
+
if (!["auto", "below", "right"].includes(P.outputPos)){ ctx.report("attr", "[code] invalid outputPos; using auto"); P.outputPos = "auto"; }
|
|
78
|
+
// 大きさを測る前にブロックにする(インラインのままだと幅が 0 になる)
|
|
79
|
+
this.style.display = "block"; this.style.position = "relative"; this.style.writingMode = "horizontal-tb";
|
|
80
|
+
this.fs = P.size || 24; this.noteSize = P.size || 26;
|
|
81
|
+
var fs = this.fs, nf = this.noteSize, row = Math.ceil(fs * 1.4), noteRow = Math.ceil(nf * 1.45 + 4);
|
|
82
|
+
var W = Math.max(1, this.clientWidth), H = Math.max(1, this.clientHeight), program = lines(d.code), output = d.output === undefined ? [] : lines(d.output);
|
|
83
|
+
var lang = d.lang || "", commentAt = comments(program, lang.toLowerCase());
|
|
84
|
+
this.setAttribute("role", "figure"); // 中の読める pre が読まれるように(img の子は読まれない)
|
|
85
|
+
if (!this.getAttribute("aria-label")) this.setAttribute("aria-label", T.summary(lang.toLowerCase() === "python" ? "Python" : lang, program.length, d.output !== undefined) + "; " + d.steps.map(function(s){ return s.note; }).join("; "));
|
|
86
|
+
// Keep the full source readable inside the figure even when the SVG is truncated.
|
|
87
|
+
var pre = document.createElement("pre"); pre.className = "sr-only";
|
|
88
|
+
pre.setAttribute("style", "position:absolute;width:1px;height:1px;margin:-1px;padding:0;overflow:hidden;clip-path:inset(50%);white-space:pre;border:0");
|
|
89
|
+
pre.setAttribute("aria-label", T.readable);
|
|
90
|
+
pre.textContent = d.code + (d.output !== undefined ? "\n\n" + T.output + "\n" + d.output : ""); this.appendChild(pre);
|
|
91
|
+
this.svg = base.el("svg", {viewBox: "0 0 " + W + " " + H, width: W, height: H, "aria-hidden": "true"}, this);
|
|
92
|
+
var header = row + 16, pad = 16, gap = 32, numW = monoWidth(String(program.length), fs) + 16;
|
|
93
|
+
var expanded = program.map(function(s){ return s.replace(/\t/g, " "); });
|
|
94
|
+
var longest = expanded.reduce(function(a, b){ return monoWidth(a, fs) >= monoWidth(b, fs) ? a : b; }, "");
|
|
95
|
+
var natural = Math.max(160, monoWidth(longest, fs)) + numW + 2 * pad;
|
|
96
|
+
var outNatural = Math.max(monoWidth(T.output, fs) + monoWidth(T.run, fs) + pad + 8, ...output.slice(0, 8).map(function(s){ return monoWidth(s.replace(/\t/g, " "), fs); })) + 2 * pad;
|
|
97
|
+
var outputH = output.length ? header + row * Math.min(8, output.length) + pad : 0;
|
|
98
|
+
var right = output.length > 0 && (P.outputPos === "right" || P.outputPos === "auto" && program.length <= 8 && natural + 280 + outNatural + 2 * gap <= W);
|
|
99
|
+
var outW = right ? Math.max(120, Math.min(outNatural, W * 0.3)) : 0;
|
|
100
|
+
var available = W - (right ? outW + gap : 0), codeW = natural;
|
|
101
|
+
var nx = codeW + gap, nw = Math.max(1, available - nx);
|
|
102
|
+
var areaH = H - (!right && output.length ? outputH + 16 : 0), nextY = header;
|
|
103
|
+
// Keep the beside layout only if *all* the notes fit at their line targets.
|
|
104
|
+
var beside = natural <= W * 0.55 && natural + gap + 260 <= available;
|
|
105
|
+
d.steps.forEach(function(s){
|
|
106
|
+
var a = s.lines[0], b = s.lines.length === 2 ? s.lines[1] : a;
|
|
107
|
+
var target = header + ((a + b - 2) / 2 + 0.5) * row;
|
|
108
|
+
var h = Math.max(1, wrap(s.note, nw - 24, nf, self).length) * noteRow + 16;
|
|
109
|
+
nextY = Math.max(nextY, target - h / 2) + h + 8;
|
|
110
|
+
if (nextY - 8 > areaH) beside = false;
|
|
111
|
+
});
|
|
112
|
+
this.noteLayout = beside ? "beside" : "band";
|
|
113
|
+
if (!beside){
|
|
114
|
+
// 出力は、コードの横に入るなら右に(幅で決める。短い出力なら広いコードの横にも入る)
|
|
115
|
+
right = output.length > 0 && (P.outputPos === "right" || P.outputPos === "auto" && natural + gap + Math.max(120, outNatural) <= W);
|
|
116
|
+
outW = right ? Math.max(120, Math.min(outNatural, W - gap - natural)) : 0;
|
|
117
|
+
available = W - (right ? outW + gap : 0);
|
|
118
|
+
codeW = Math.max(1, Math.min(natural, available));
|
|
119
|
+
nx = 0; nw = right ? codeW : W;
|
|
120
|
+
areaH = H - (!right && output.length ? outputH + 16 : 0);
|
|
121
|
+
}
|
|
122
|
+
// 下帯の最終一覧:説明ごとに行の範囲を付け(「3–4:…」)、説明の途中では折らずに行へ詰める。字は 24px、行間は字の箱 1.45em
|
|
123
|
+
var listFont = Math.max(24, nf - 2), listRow = Math.ceil(listFont * 1.45);
|
|
124
|
+
var items = d.steps.map(function(s){ var r = Array.isArray(s.lines) ? s.lines : []; var x = r[0], y = r.length > 1 ? r[1] : r[0]; return x + (y === x ? "" : "–" + y) + (T === TEXT.ja ? ":" : ": ") + s.note.replace(/\s*\n\s*/g, " "); });
|
|
125
|
+
// 出力が下に回るときは説明帯の右に並べ、帯と出力の高い方だけを使う
|
|
126
|
+
var sideW = Math.max(120, outNatural);
|
|
127
|
+
var side = !beside && !right && output.length > 0 && P.outputPos === "auto" && d.steps.length > 0 && outNatural <= W * 0.45 && W - sideW - gap >= W * 0.4;
|
|
128
|
+
if (side){ nw = W - sideW - gap; areaH = H; }
|
|
129
|
+
var list = [];
|
|
130
|
+
if (!beside && d.steps.length){
|
|
131
|
+
var cur = "", sep = T === TEXT.ja ? " " : " ";
|
|
132
|
+
items.forEach(function(t){
|
|
133
|
+
var cand = cur ? cur + sep + t : t;
|
|
134
|
+
if (cur && base.textWidth(cand, listFont, {font: self}) > nw - 24){ list.push(cur); cur = t; } else cur = cand;
|
|
135
|
+
});
|
|
136
|
+
if (cur) list.push(cur);
|
|
137
|
+
}
|
|
138
|
+
// 高さの式は 1 つ:コード部 + 帯(一覧 r 行)と横の出力の高い方 + 下の出力。報告もこの式で数える
|
|
139
|
+
function bandFor(r){ return r ? noteRow + 16 + (r - 1) * listRow : 0; }
|
|
140
|
+
function need(n, r){ var zone = Math.max(bandFor(r), side ? outputH : 0); return Math.ceil(header + n * row + pad + (zone ? 8 + zone : 0) + (!right && !side && output.length ? outputH + 16 : 0)); }
|
|
141
|
+
var want = Math.min(16, program.length), rows = list.length, fullList = true;
|
|
142
|
+
function fitsAt(h, r){ return Math.max(0, Math.floor((h - need(0, r)) / row)); }
|
|
143
|
+
var capacity = fitsAt(H, rows);
|
|
144
|
+
if (capacity < want && rows > 1 && fitsAt(H, 1) >= want){ fullList = false; capacity = fitsAt(H, 1); } // コードを全部残し、最終一覧は最後の説明だけ
|
|
145
|
+
var bandH = bandFor(fullList ? rows : Math.min(1, rows)), bandSpace = bandH ? bandH + 8 : 0;
|
|
146
|
+
var count = Math.min(want, capacity), codeH = header + count * row + pad;
|
|
147
|
+
var neededH = need(want, rows), SLIDE = 440;
|
|
148
|
+
function advice(){
|
|
149
|
+
return neededH <= SLIDE ? "give the element about " + neededH + " px" : "more than a slide body under a heading (about " + SLIDE + " px) holds; " + SLIDE + " px fits " + Math.min(want, fitsAt(SLIDE, rows)) + " lines with these notes: show fewer lines, shorten notes or split the slide";
|
|
150
|
+
}
|
|
151
|
+
var textX = pad + numW, firstY = header + fs, textW = Math.max(0, codeW - textX - pad);
|
|
152
|
+
if (count < program.length) ctx.report("layout", "[code] " + program.length + " lines; this height fits " + Math.min(16, capacity) + " (" + advice() + (neededH <= SLIDE ? ", or show fewer lines" : "") + ")" + (program.length > 16 ? "; at most 16 lines per panel" : ""));
|
|
153
|
+
if (monoWidth(longest, fs) > textW) ctx.report("layout", "[code] the longest line is " + [...longest].length + " characters; this width fits " + Math.floor(textW / (0.6 * fs)) + " at " + fs + "px (break the line)");
|
|
154
|
+
this.geometry = {side: side, listRows: rows, codeW: codeW, codeH: codeH, nx: nx, nw: nw, row: row, firstY: firstY, right: right, count: count, neededH: neededH, bandH: bandH, noteLayout: this.noteLayout};
|
|
155
|
+
this.codeLayer = base.el("g", {opacity: 0}, this.svg);
|
|
156
|
+
function panel(g, x, y, w, h){ base.el("rect", {x: x, y: y, width: Math.max(1, w), height: Math.max(1, h), rx: 12, fill: "var(--term-tint, color-mix(in srgb, var(--fg) 5%, var(--bg)))", stroke: "var(--line)", "stroke-width": 2}, g); }
|
|
157
|
+
panel(this.codeLayer, 1, 1, codeW - 2, codeH - 2);
|
|
158
|
+
var tagW = Math.max(1, codeW - 2 * pad), tag = cut(lang, tagW, fs);
|
|
159
|
+
if (count || H >= header + pad) this.label(this.codeLayer, codeW - pad - monoWidth(tag, fs), pad + fs, tag, fs, "var(--muted)", true);
|
|
160
|
+
this.bands = d.steps.map(function(){ return base.el("rect", {x: 4, y: header, width: Math.max(1, codeW - 8), height: row, fill: "var(--emph-tint, color-mix(in srgb, var(--emph) 12%, var(--bg)))", opacity: 0}, self.codeLayer); });
|
|
161
|
+
if (tag !== lang) ctx.report("layout", "[code] language tag needs " + Math.ceil(monoWidth(lang, fs)) + " px; header fits " + Math.floor(tagW) + " px");
|
|
162
|
+
this.numbers = [];
|
|
163
|
+
for (var i = 0; i < count; i++){
|
|
164
|
+
var y = firstY + i * row, original = program[i].replace(/\t/g, " "), s = cut(original, Math.max(0, codeW - textX - pad), fs);
|
|
165
|
+
if (i === count - 1 && count < program.length) s = cut(s + "…", Math.max(0, codeW - textX - pad), fs);
|
|
166
|
+
this.numbers.push(this.label(this.codeLayer, pad, y, String(i + 1), fs, "var(--muted)", true));
|
|
167
|
+
// Index after tab expansion (comment scanner operates on the original program).
|
|
168
|
+
var at = commentAt[i] < 0 ? -1 : program[i].slice(0, commentAt[i]).replace(/\t/g, " ").length;
|
|
169
|
+
if (at < 0 || at >= s.length) this.label(this.codeLayer, textX, y, s, fs, "var(--fg)", true);
|
|
170
|
+
else {
|
|
171
|
+
this.label(this.codeLayer, textX, y, s.slice(0, at), fs, "var(--fg)", true);
|
|
172
|
+
this.label(this.codeLayer, textX + monoWidth(s.slice(0, at), fs), y, s.slice(at), fs, "var(--muted)", true);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (count === 0 || W < 360 || textW < fs){
|
|
176
|
+
ctx.report("size", "[code] element is " + W + " × " + H + " px; allow at least 360 × " + Math.ceil(header + row + pad + bandSpace) + " px for one code line and notes");
|
|
177
|
+
}
|
|
178
|
+
// Even unusably small boxes retain a clipped panel and an ellipsis.
|
|
179
|
+
this.svg.setAttribute("style", "overflow:hidden");
|
|
180
|
+
if (!count) this.label(this.codeLayer, Math.min(pad, W / 4), Math.min(fs, H), "…", fs);
|
|
181
|
+
nextY = header;
|
|
182
|
+
var bandY = codeH + 8, bandRoom = Math.max(0, areaH - bandY);
|
|
183
|
+
function proseCut(s, width, f){
|
|
184
|
+
f = f || nf;
|
|
185
|
+
if (base.textWidth(s, f, {font: self}) <= width) return s;
|
|
186
|
+
var chars = [...s];
|
|
187
|
+
while (chars.length && base.textWidth(chars.join("") + "…", f, {font: self}) > width) chars.pop();
|
|
188
|
+
return width >= f ? chars.join("") + "…" : "";
|
|
189
|
+
}
|
|
190
|
+
function marker(a, b){ return T === TEXT.ja ? a + (b === a ? "" : "–" + b) + " 行目" : (a === b ? "line " + a : "lines " + a + "–" + b); }
|
|
191
|
+
this.notes = d.steps.map(function(s, j){
|
|
192
|
+
var a = s.lines[0], b = s.lines.length === 2 ? s.lines[1] : a;
|
|
193
|
+
var valid = a >= 1 && b >= a && b <= program.length;
|
|
194
|
+
if (!valid) ctx.report("range", "[code] steps[" + j + "].lines must be an inclusive range within 1–" + program.length);
|
|
195
|
+
var target = valid && a <= count ? header + ((a - 1 + Math.min(b, count) - 1) / 2 + 0.5) * row : header;
|
|
196
|
+
var g = base.el("g", {opacity: 0}, self.svg), text, top, h, labels;
|
|
197
|
+
if (beside){
|
|
198
|
+
text = wrap(s.note, nw - 24, nf, self); h = Math.max(noteRow, text.length * noteRow) + 16;
|
|
199
|
+
top = Math.max(nextY, target - h / 2);
|
|
200
|
+
if (valid && a <= count) base.el("path", {d: "M " + codeW + " " + target + " H " + (nx - 12) + " V " + (top + h / 2) + " H " + nx, fill: "none", stroke: "var(--muted)", "stroke-width": 2}, g);
|
|
201
|
+
base.el("rect", {x: nx, y: top, width: nw - 1, height: h, rx: 8, fill: "var(--bg)", stroke: "var(--line)", "stroke-width": 2}, g);
|
|
202
|
+
labels = text.map(function(t, k){ return self.label(g, nx + 12, top + 8 + nf + k * noteRow, t, nf); });
|
|
203
|
+
nextY = top + h + 8;
|
|
204
|
+
} else {
|
|
205
|
+
top = bandY; h = Math.min(bandFor(1), bandRoom);
|
|
206
|
+
var prefix = marker(a, b) + " · ", prefixW = base.textWidth(prefix, nf, {font: self}), room = Math.max(0, nw - 24 - prefixW);
|
|
207
|
+
var width = base.textWidth(s.note, nf, {font: self});
|
|
208
|
+
if (width > room || lines(s.note).length > 1){
|
|
209
|
+
var approx = Math.max(0, Math.floor([...s.note].length * room / Math.max(1, width)));
|
|
210
|
+
ctx.report("layout", "[code] note " + (j + 1) + " is too long for the band (" + Math.ceil(width) + " px of " + Math.floor(room) + "); shorten it to about " + approx + " characters");
|
|
211
|
+
}
|
|
212
|
+
base.el("rect", {x: 1, y: top, width: Math.max(1, nw - 2), height: Math.max(1, h), rx: 8, fill: "var(--bg)", stroke: "var(--line)", "stroke-width": 2}, g);
|
|
213
|
+
labels = [self.label(g, 12, top + 8 + nf, proseCut(prefix + s.note.replace(/\s*\n\s*/g, " "), nw - 24), nf)];
|
|
214
|
+
}
|
|
215
|
+
if (valid && a <= count){ set(self.bands[j], "y", header + (a - 1) * row); set(self.bands[j], "height", (Math.min(b, count) - a + 1) * row); }
|
|
216
|
+
return {g: g, labels: labels, a: a, b: b, valid: valid && a <= count, top: top, height: h, fits: true};
|
|
217
|
+
});
|
|
218
|
+
this.finalNotes = base.el("g", {opacity: 0}, this.svg);
|
|
219
|
+
if (!beside && d.steps.length){
|
|
220
|
+
list = list.map(function(t){ return proseCut(t, nw - 24, listFont); });
|
|
221
|
+
if (!fullList || bandFor(list.length) > bandRoom){
|
|
222
|
+
ctx.report("layout", "[code] " + d.steps.length + " final notes need " + list.length + " band rows (" + bandFor(list.length) + " px); this height fits " + Math.max(0, Math.floor((bandRoom - noteRow - 16) / listRow) + 1) + " (" + advice() + "); showing the last note");
|
|
223
|
+
list = [proseCut(items[items.length - 1], nw - 24, listFont)];
|
|
224
|
+
}
|
|
225
|
+
base.el("rect", {x: 1, y: bandY, width: Math.max(1, nw - 2), height: Math.max(1, Math.min(bandRoom, bandFor(list.length))), rx: 8, fill: "var(--bg)", stroke: "var(--line)", "stroke-width": 2}, this.finalNotes);
|
|
226
|
+
list.forEach(function(s, i){ self.label(self.finalNotes, 12, bandY + 8 + nf + i * listRow, s, listFont); });
|
|
227
|
+
}
|
|
228
|
+
this.outputLayer = base.el("g", {opacity: 0}, this.svg);
|
|
229
|
+
if (output.length){
|
|
230
|
+
var ox = right ? W - outW : side ? W - sideW : 0, oy = right ? 0 : side ? codeH + 8 : areaH + 16, ow = right ? outW : side ? sideW : codeW;
|
|
231
|
+
var oc = Math.max(0, Math.min(8, output.length, Math.floor((H - oy - header - pad) / row)));
|
|
232
|
+
panel(this.outputLayer, ox + 1, oy + 1, ow - 2, header + oc * row + pad - 2);
|
|
233
|
+
self.label(this.outputLayer, ox + pad, oy + pad + fs, cut(T.output, ow - 2 * pad, fs), fs);
|
|
234
|
+
// Run precedes the supplied plain text in a separate header region.
|
|
235
|
+
var run = cut(T.run, ow - 2 * pad - monoWidth(T.output, fs) - pad, fs);
|
|
236
|
+
self.label(this.outputLayer, ox + ow - pad - monoWidth(run, fs), oy + pad + fs, run, fs, "var(--muted)", true);
|
|
237
|
+
var outputCut = oc < output.length || run !== T.run || monoWidth(T.output, fs) > ow - 2 * pad;
|
|
238
|
+
output.slice(0, oc).forEach(function(s, i){
|
|
239
|
+
var txt = cut(s.replace(/\t/g, " "), ow - 2 * pad, fs);
|
|
240
|
+
if (i === oc - 1 && oc < output.length) txt = cut(txt + "…", ow - 2 * pad, fs);
|
|
241
|
+
if (txt !== s) outputCut = true;
|
|
242
|
+
self.label(self.outputLayer, ox + pad, oy + header + fs + i * row, txt, fs, "var(--fg)", true);
|
|
243
|
+
});
|
|
244
|
+
if (outputCut) ctx.report("layout", "[code] output exceeds eight lines or panel width/height; showing what fits with …");
|
|
245
|
+
}
|
|
246
|
+
if (outputH > H) ctx.report("size", "[code] output needs " + outputH + " px height; element has " + H + " px");
|
|
247
|
+
}
|
|
248
|
+
render(k, t, ctx){
|
|
249
|
+
if (!this.codeLayer) return;
|
|
250
|
+
ctx = ctx || {};
|
|
251
|
+
var self = this, preview = ctx.preview || ctx.mode === "lecture", faint = preview ? base.previewOpacity(this) : 0;
|
|
252
|
+
var phase = function(step){ return k > step ? 1 : k < step ? faint : base.prog(t, 0, self.props.dur); };
|
|
253
|
+
set(this.codeLayer, "opacity", phase(1));
|
|
254
|
+
var final = k > this.steps || k === this.steps && t >= this.props.dur, active = !final && k >= 2 && k <= this.notes.length + 1 ? k - 2 : -1;
|
|
255
|
+
this.notes.forEach(function(n, i){
|
|
256
|
+
var band = self.noteLayout === "band", shown = !final && (i === active || active < 0 && k > self.notes.length + 1 && i === self.notes.length - 1);
|
|
257
|
+
set(n.g, "opacity", band ? (shown ? phase(i + 2) : 0) : phase(i + 2));
|
|
258
|
+
n.labels.forEach(function(e){ set(e, "style", "fill:var(--" + (i < k - 2 && !final ? "muted" : "fg") + ")"); });
|
|
259
|
+
set(self.bands[i], "opacity", i === active && n.valid ? base.prog(t, 0, self.props.dur) : 0);
|
|
260
|
+
});
|
|
261
|
+
set(this.finalNotes, "opacity", this.noteLayout === "band" ? (final ? 1 : k < 2 ? faint : 0) : 0);
|
|
262
|
+
this.numbers.forEach(function(e, i){ var n = self.notes[active]; set(e, "style", "font-family:" + MONO + ";white-space:pre;fill:var(--" + (n && n.valid && i + 1 >= n.a && i + 1 <= n.b ? "emph" : "muted") + ")"); });
|
|
263
|
+
set(this.outputLayer, "opacity", phase(this.notes.length + 2));
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
});
|