egatari 0.6.10 → 0.6.11-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.
Files changed (124) hide show
  1. package/README.ja.md +22 -22
  2. package/README.md +1 -1
  3. package/bin/egatari.mjs +1 -1
  4. package/components.lock.json +288 -144
  5. package/components2.lock.json +657 -1
  6. package/kit/build.mjs +3 -2
  7. package/kit/check-messages.mjs +46 -12
  8. package/kit/check.mjs +54 -19
  9. package/kit/core/article-messages.mjs +5 -4
  10. package/kit/core/attachments.mjs +2 -1
  11. package/kit/core/build-core.mjs +2 -1
  12. package/kit/core/check-parity.mjs +3 -1
  13. package/kit/core/check-result.mjs +2 -1
  14. package/kit/core/check-rules.mjs +132 -0
  15. package/kit/core/colab-core.mjs +2 -2
  16. package/kit/core/geometry-check.mjs +302 -0
  17. package/kit/core/layout-scan.mjs +9 -2
  18. package/kit/core/live-results.mjs +1 -1
  19. package/kit/core/llm-fix.mjs +3 -2
  20. package/kit/core/llm-outline.mjs +3 -2
  21. package/kit/core/llm-pack-data.mjs +114 -114
  22. package/kit/core/narration-lint.mjs +37 -0
  23. package/kit/core/new-deck-pack.mjs +2 -1
  24. package/kit/core/new-deck.mjs +1 -1
  25. package/kit/core/plural.mjs +5 -0
  26. package/kit/core/portable.mjs +6 -5
  27. package/kit/core/sample-layout.mjs +14 -0
  28. package/kit/core/text-band.mjs +37 -0
  29. package/kit/core/tier-a.mjs +2 -1
  30. package/kit/instruction-version.json +1 -1
  31. package/kit/player.json +1 -1
  32. package/kit/player.lock.json +2 -2
  33. package/kit/render-compare.mjs +5 -4
  34. package/kit/tools.js +2 -2
  35. package/package.json +3 -2
  36. package/server/editor-ui.js +2 -2
  37. package/vendor/components/CATALOG.md +12 -12
  38. package/vendor/components/sha256/00d9072cb279a1a68166787f53395c747072388971d916c082a106844dbf888f/index.js +315 -0
  39. package/vendor/components/sha256/03e20fb6803de0d94e93de7c01d7d00cede57b0b2604b449daa4cb156367500d/README.en.md +23 -0
  40. package/vendor/components/sha256/03e20fb6803de0d94e93de7c01d7d00cede57b0b2604b449daa4cb156367500d/index.js +289 -0
  41. package/vendor/components/sha256/0e5458deb63e32aefdbf8722fa156fd44cd90d5d8f9d6a25a636007b4b599450/README.en.md +40 -0
  42. package/vendor/components/sha256/0e5458deb63e32aefdbf8722fa156fd44cd90d5d8f9d6a25a636007b4b599450/index.js +341 -0
  43. package/vendor/components/sha256/1073e03374f0fe65d2691f160bfd8c092986c168d0bd6a6094ebd444d4b89c27/index.js +592 -0
  44. package/vendor/components/sha256/12b1b32eb3a75e9f11eff407b7876473674ae996d7ce276e2ab6ffaf41ad4376/README.en.md +48 -0
  45. package/vendor/components/sha256/12b1b32eb3a75e9f11eff407b7876473674ae996d7ce276e2ab6ffaf41ad4376/index.js +384 -0
  46. package/vendor/components/sha256/147987bd84722249dab609293d4fafa93f0a2aac25b8517fda1710840b575d00/index.js +341 -0
  47. package/vendor/components/sha256/170e9072285213a2b4e79d7c2e4ddb882d1501e60b5da63c59b1679643163005/README.en.md +23 -0
  48. package/vendor/components/sha256/170e9072285213a2b4e79d7c2e4ddb882d1501e60b5da63c59b1679643163005/index.js +120 -0
  49. package/vendor/components/sha256/17da8ef5ce58354df54ebb52c8a9b0726e69d06ac28cc8e78f807ec338002782/README.en.md +32 -0
  50. package/vendor/components/sha256/17da8ef5ce58354df54ebb52c8a9b0726e69d06ac28cc8e78f807ec338002782/index.js +222 -0
  51. package/vendor/components/sha256/1b30dfd9b366ff07556024eb8ce75cca2eac8001fa392eed4e1232047001ec74/index.js +404 -0
  52. package/vendor/components/sha256/37846655693f8dd869d11d3d4cddb43be57f848037d0e49564b2250401448ab5/README.en.md +66 -0
  53. package/vendor/components/sha256/37846655693f8dd869d11d3d4cddb43be57f848037d0e49564b2250401448ab5/index.js +920 -0
  54. package/vendor/components/sha256/3fdd15756f779a16362dd854f143ca4343154059882d88d44adf64745c7876e3/README.en.md +50 -0
  55. package/vendor/components/sha256/3fdd15756f779a16362dd854f143ca4343154059882d88d44adf64745c7876e3/index.js +171 -0
  56. package/vendor/components/sha256/435c3f8e0c0abc123866e57ae2c7f3409e19d14a2eb67b1fde84c4da5d95f14c/README.en.md +31 -0
  57. package/vendor/components/sha256/435c3f8e0c0abc123866e57ae2c7f3409e19d14a2eb67b1fde84c4da5d95f14c/index.js +232 -0
  58. package/vendor/components/sha256/43740da51de85c1b7ec1eaf688f93fd9ea25b15977ab075e6abac196f08643a6/index.js +122 -0
  59. package/vendor/components/sha256/4817f77bd021ec5c77d5a15ee56e2e34d31d1d98630e86225eb20f2f9bf095e9/index.js +812 -0
  60. package/vendor/components/sha256/4e4bf3b2968c36a334dc997e32e36f22ecbc4f5dfe1c76bb7a5beef002fe8899/index.js +228 -0
  61. package/vendor/components/sha256/553e132d1f84caad648bb7175a206d4c5d18c005b5ca17989f5e8a5e3f5cdfa1/index.js +75 -0
  62. package/vendor/components/sha256/5b02d8a81a41f4d1658f1972f3fc91de3cc431b2c71bb6b92601d58f734de9ec/README.en.md +34 -0
  63. package/vendor/components/sha256/5b02d8a81a41f4d1658f1972f3fc91de3cc431b2c71bb6b92601d58f734de9ec/index.js +244 -0
  64. package/vendor/components/sha256/5c72414df5a948a7cbb882be1e5bdd4113d188e357d4a0f01fabda15781417eb/LICENSE-tabler.txt +21 -0
  65. package/vendor/components/sha256/5c72414df5a948a7cbb882be1e5bdd4113d188e357d4a0f01fabda15781417eb/index.js +4868 -0
  66. package/vendor/components/sha256/5c72414df5a948a7cbb882be1e5bdd4113d188e357d4a0f01fabda15781417eb/names.txt +4808 -0
  67. package/vendor/components/sha256/636f47e955f5550f046cfc7a3de8de70c4b97aa77771bde2ee0e31d706bb032e/index.js +278 -0
  68. package/vendor/components/sha256/65ddc34bd9c75e073f4c2cd283369417fe88769521809670c6b8730852d4f647/index.js +97 -0
  69. package/vendor/components/sha256/6b0c75b0f697fe451197301de82a113f1c2548b67f7a8d2b56576d2bae8e240c/index.js +607 -0
  70. package/vendor/components/sha256/6cdf432d3e61be3ff15e80910369324ac7ea0e8b9f62c32ef37bf09a5bfe93b9/README.en.md +33 -0
  71. package/vendor/components/sha256/6cdf432d3e61be3ff15e80910369324ac7ea0e8b9f62c32ef37bf09a5bfe93b9/index.js +390 -0
  72. package/vendor/components/sha256/6d648d0e5c96ec680c04a14b7c3d672010d3b9f101197689e9672ec00174383d/README.en.md +31 -0
  73. package/vendor/components/sha256/6d648d0e5c96ec680c04a14b7c3d672010d3b9f101197689e9672ec00174383d/index.js +267 -0
  74. package/vendor/components/sha256/71208afc5c8ea1ed6d2180f46de7385e43990cfb046ba350eefb917eca5d92fe/README.en.md +22 -0
  75. package/vendor/components/sha256/71208afc5c8ea1ed6d2180f46de7385e43990cfb046ba350eefb917eca5d92fe/index.js +145 -0
  76. package/vendor/components/sha256/7552a029f30f9c2f2ec8c5af4f144b63eeffc9172b53e35a925821f69a738b64/README.en.md +44 -0
  77. package/vendor/components/sha256/7552a029f30f9c2f2ec8c5af4f144b63eeffc9172b53e35a925821f69a738b64/index.js +203 -0
  78. package/vendor/components/sha256/781a45d97f1325cfb40aa98d00e1db9600949158fdb2749b0223b019c67a80f2/index.js +919 -0
  79. package/vendor/components/sha256/850944419734168be3dc202057de128131869c471cb8ab3baec2cfe789d56493/index.js +465 -0
  80. package/vendor/components/sha256/863d4d42075fbae1084089730f10da943c88d3e3e655235f28c24b9a3cf95c88/README.en.md +37 -0
  81. package/vendor/components/sha256/863d4d42075fbae1084089730f10da943c88d3e3e655235f28c24b9a3cf95c88/index.js +424 -0
  82. package/vendor/components/sha256/8a892d25d0bb6105ba31ea690b465876ce3af1fbaf18e2a9ce44ad2fee81260e/index.js +162 -0
  83. package/vendor/components/sha256/8acec8f299510287fbad6a69311ec58a39b0e99647dd03609c818e053b3b6087/index.js +484 -0
  84. package/vendor/components/sha256/8ef66bd1e800bcbedef436585469d308665aaa1ab6011aee699544302d0a8ad0/index.js +312 -0
  85. package/vendor/components/sha256/8fa5ed6ab12a27542e077ff965bea626ff9450b330609304dc1148fc8dbb68b0/README.en.md +40 -0
  86. package/vendor/components/sha256/8fa5ed6ab12a27542e077ff965bea626ff9450b330609304dc1148fc8dbb68b0/index.js +348 -0
  87. package/vendor/components/sha256/a0c71874d83045baaef70285bd00d87a7c6c43219fa5630dfff60d5cb08509be/index.js +250 -0
  88. package/vendor/components/sha256/a31f39625d7d1087d06a72b644cec417372f98add940288257ef07f812d8395a/index.js +747 -0
  89. package/vendor/components/sha256/ae04a57be4a26b7443796a93c58adcf1cbcf746120eba619bf9c2bcdce53c640/README.en.md +38 -0
  90. package/vendor/components/sha256/ae04a57be4a26b7443796a93c58adcf1cbcf746120eba619bf9c2bcdce53c640/index.js +289 -0
  91. package/vendor/components/sha256/ae06622a328da2a01260bc68b28bb632c391e8d62a4e8387638e5a944525a418/index.js +521 -0
  92. package/vendor/components/sha256/b490139e9afa84164054738532d5ec4985bf071e40791d903012fe3b850bae43/README.en.md +24 -0
  93. package/vendor/components/sha256/b490139e9afa84164054738532d5ec4985bf071e40791d903012fe3b850bae43/index.js +103 -0
  94. package/vendor/components/sha256/b756c66d5c6e6ace90912ab467dea3031f2c9b291d8b2a786ca660faa0e8ac84/index.js +121 -0
  95. package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/fixtures/ABCJS-ADAPTER.md +101 -0
  96. package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/fixtures/LICENSE-abcjs.txt +21 -0
  97. package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/fixtures/chords.json +89 -0
  98. package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/fixtures/doremi.json +261 -0
  99. package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/fixtures/extras.json +192 -0
  100. package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/fixtures/kaeru.json +521 -0
  101. package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/fixtures/rhythm.json +248 -0
  102. package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/fixtures/ties.json +360 -0
  103. package/vendor/components/sha256/babd98da50ea334b63f79c66be436d092e8aea32654e011a6444667abb882e77/index.js +508 -0
  104. package/vendor/components/sha256/bea7cb344f55677d5756c9711b90cc971126830ec5f66a03f71f13dbfadc401e/index.js +351 -0
  105. package/vendor/components/sha256/c08b25a2f7be68c36634cdee180aa617fd018d80b7ddf2a484a2edb4acffdbe0/README.en.md +42 -0
  106. package/vendor/components/sha256/c08b25a2f7be68c36634cdee180aa617fd018d80b7ddf2a484a2edb4acffdbe0/index.js +268 -0
  107. package/vendor/components/sha256/cbe8bb4b2fd0845a9333bc2064d86758ea6465992ad86a99167ba17f57f3e382/README.en.md +51 -0
  108. package/vendor/components/sha256/cbe8bb4b2fd0845a9333bc2064d86758ea6465992ad86a99167ba17f57f3e382/index.js +228 -0
  109. package/vendor/components/sha256/d2df3d70fc7c6e07e3cad7332a7e5501e84abeb24653786e567f57046adebcba/index.js +268 -0
  110. package/vendor/components/sha256/d386f2b94cd9fbed765ad301aa7009228599edb2859efe7f37a98d7aaa44b9bb/README.en.md +35 -0
  111. package/vendor/components/sha256/d386f2b94cd9fbed765ad301aa7009228599edb2859efe7f37a98d7aaa44b9bb/index.js +179 -0
  112. package/vendor/components/sha256/e6d219f67d241fdf00a40d4cec261d9dd3c09ad3110d3418d4e843d0f2b8c196/index.js +429 -0
  113. package/vendor/components/sha256/ef60e6c089f5f6e909f439af8fc0c9c0a2d7a1aafad5e65be6328ba993b990a0/index.js +354 -0
  114. package/vendor/components/sha256/f6f47601c90f0195353e94b76d57499591d6330a7e35714958a2c41229f70da2/index.js +198 -0
  115. package/vendor/components/sha256/f9159c87fa998487787f738fdfab52aa2483f52f170fc258840b127f6ed06051/README.en.md +44 -0
  116. package/vendor/components/sha256/f9159c87fa998487787f738fdfab52aa2483f52f170fc258840b127f6ed06051/index.js +304 -0
  117. package/vendor/components/sha256/fb482cbe2ca1da07d08f41880e03fe75ff3f1d29842ff40946541e63030219b8/README.en.md +42 -0
  118. package/vendor/components/sha256/fb482cbe2ca1da07d08f41880e03fe75ff3f1d29842ff40946541e63030219b8/index.js +481 -0
  119. package/vendor/components/sha256/fdcea28e27638d8bfe234016672375e14de8bb97c32f7b6840d1b7b10ac4b1cd/index.js +233 -0
  120. package/vendor/components/sha256/ff5eaa083a46db6aefc13d97b27c9487f9fc318805f5c89b05a12e63703b7a72/LICENSE-strokesvg.txt +115 -0
  121. package/vendor/components/sha256/ff5eaa083a46db6aefc13d97b27c9487f9fc318805f5c89b05a12e63703b7a72/OFL.txt +93 -0
  122. package/vendor/components/sha256/ff5eaa083a46db6aefc13d97b27c9487f9fc318805f5c89b05a12e63703b7a72/index.js +749 -0
  123. package/vendor/components/sha256/ff5eaa083a46db6aefc13d97b27c9487f9fc318805f5c89b05a12e63703b7a72/strokes-CHANGES.md +20 -0
  124. package/vendor/components-2/manifest-2.7.0.json +2118 -0
@@ -0,0 +1,38 @@
1
+ ---
2
+ summary: Code notes
3
+ steps: Code → notes → output (n+1/+2)
4
+ props:
5
+ dur: Seconds
6
+ size: Text px
7
+ outputPos: Output
8
+ example: {"lang":"python","code":"import pandas as pd\ndf = pd.read_csv(\"sales.csv\")\ndf.groupby(\"store\")[\"sales\"].mean()","steps":[{"lines":[1],"note":"Load pandas"},{"lines":[2],"note":"Read CSV"},{"lines":[3],"note":"Mean"}],"output":"store\nA 120.5\nB 98.0"}
9
+ ---
10
+
11
+ `<m-code>` child JSON.
12
+
13
+ - `code`: ≤76 chars/line; height ≈ 132+34×lines+35×(list rows−1), ~40 chars/row; >440px: cut/split.
14
+ - `lang`: tag; `steps[{lines,note}]`: 1-based, `"lines":[3]` or range `[3,4]`; note.
15
+ - `output`: ≤8 lines.
16
+
17
+ Script: explain each line; never read code aloud.
18
+
19
+ <!-- llm: omit -->
20
+ Displays supplied text; never runs code. Code first, then highlights and notes. Beside: natural code width ≤55% and every note fits; earlier notes stay muted, all shown at completion. Otherwise band: only the current note, prefixed “lines 3–4”. Final/print: numbered compact list, no code highlight. If the list needs more height, report and keep the last numbered note. Deterministic seeking. Preview uses --preview-opacity; the final list previews before notes begin.
21
+
22
+ dur .6s; size 0=24px code/26px notes, otherwise ≥24. outputPos auto/below/right. Beside auto: right for ≤8 lines when columns fit. Band auto: right when natural code width ≤70% and output fits, else below the band. Explicit right/below retained. Bad props report/default.
23
+
24
+ Colours are theme roles: for Python, R, shell and JavaScript (js), keywords term, strings accent2 (Python triple quotes and JS templates span lines), # (JS //) comments outside strings muted, the rest fg; other languages stay fg. Code reads the same without colour. `[[3,4]]` reads as `[3,4]`; two ranges in one step report range. Tabs: four spaces. Monospace width .6em, wide characters 1em. Full-width band mode at 1184px/24px fits 77 ASCII characters with single-digit line numbers, 76 with two digits. Beside notes/right output reduce this. Beside notes wrap/stack with connectors; each band note is one truncated row.
25
+
26
+ Default height, one band row, right/no output: header 50 + n × row 34 + padding 16 + gap 8 + band 58 = **132 + 34n px**. General: r=ceil(1.4×size), header=r+16, b=ceil(1.45×note px+4)+16; H=header+n×r+16+8+b. No notes: omit 8+b. Below output adds 16+header+output rows×r+16. Final list (24px, 35px rows) packs “3–4: note” items; each row beyond one adds 35px. Auto output that would go below sits beside the band when its natural width ≤45%; the zone is then max(band, output). Every “give the element about H px” uses this formula; above 440px (body under a heading) reports give lines that fit at 440px and say fewer lines/shorter notes/split instead. If only the list misses, all code stays and the final state keeps the last note.
27
+
28
+ | Height | Code lines (one band row) |
29
+ |---|---|
30
+ | 380px | 7 (needs 370px) |
31
+ | 440px | 9 (438px) |
32
+ | 540px | 12 (540px) |
33
+ | 680px | 16 (676px) |
34
+
35
+ A 59-character line exceeds 70% width: auto output goes below. Seven lines + band + one output row need 486px; two output rows need 520px. Without output, 380px fits. Separate layout reports give line count/capacity/needed height, longest line/character capacity, or note index/width/capacity/target length. Oversize text truncates with …; tiny elements retain clipped content and numeric size reports. Code ≤16 lines, output ≤8. Invalid ranges report; no highlight/connector, notes/steps preserved.
36
+
37
+ ctx.lang ja/en. Figure role; author label preserved; SVG hidden. Full source/output in non-focusable sr-only pre. All notes remain in the accessible summary. Fictional sales data.
38
+ <!-- llm: end -->
@@ -0,0 +1,289 @@
1
+ // m-code: a program, successive line explanations, then its supplied output. Never executes code.
2
+ egatariParts.define({name: "code", version: "2.1.0", requires: {base: "1.2.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
+ // Colour roles for code: keywords term, strings accent2, comments muted (theme roles only; the text stays readable without colour).
19
+ var KEYWORDS = {
20
+ python: "False None True and as assert async await break class continue def del elif else except finally for from global if import in is lambda nonlocal not or pass raise return try while with yield",
21
+ js: "async await break case catch class const continue default delete do else export extends false finally for function if import in instanceof let new null of return super switch this throw true try typeof undefined var void while yield",
22
+ r: "if else repeat while function for in next break TRUE FALSE NULL Inf NaN NA return",
23
+ shell: "if then else elif fi case esac for while until do done in function return export local"
24
+ };
25
+ function dialect(lang){ return lang === "javascript" ? "js" : lang === "sh" || lang === "bash" ? "shell" : KEYWORDS[lang] ? lang : ""; }
26
+ // Split each line into [start, end, role] spans: strings (also across lines for Python triple quotes / JS templates),
27
+ // line comments outside strings, keywords outside both. Unknown languages give no spans.
28
+ function spans(program, lang){
29
+ var L = dialect(lang), kw = L ? new Set(KEYWORDS[L].split(" ")) : null, quote = "", triple = false;
30
+ return program.map(function(s){
31
+ var out = [], from = quote ? 0 : -1;
32
+ if (!L) return out;
33
+ for (var i = 0; i < s.length; i++){
34
+ var c = s[i];
35
+ if (quote){
36
+ if (c === "\\"){ i++; continue; }
37
+ if (c === quote && (!triple || s.slice(i, i + 3) === quote.repeat(3))){ i += triple ? 2 : 0; out.push([from, i + 1, "accent2"]); quote = ""; triple = false; }
38
+ } else if (c === '"' || c === "'" || L === "js" && c === "`"){
39
+ from = i; quote = c; triple = L === "python" && s.slice(i, i + 3) === c.repeat(3); if (triple) i += 2;
40
+ } else if (L !== "js" && c === "#" && (L !== "shell" || i === 0 || /\s/.test(s[i - 1])) || L === "js" && s.slice(i, i + 2) === "//"){
41
+ out.push([i, s.length, "muted"]); return out;
42
+ } else if (/[A-Za-z_]/.test(c) && (i === 0 || !/[\w.$]/.test(s[i - 1]))){
43
+ var m = /^[A-Za-z_]\w*/.exec(s.slice(i))[0];
44
+ if (kw.has(m)) out.push([i, i + m.length, "term"]);
45
+ i += m.length - 1;
46
+ }
47
+ }
48
+ if (quote){ out.push([from, s.length, "accent2"]); if (!triple && quote !== "`") quote = ""; }
49
+ return out;
50
+ });
51
+ }
52
+ // Wrap prose on words where possible, falling back to characters for Japanese / long tokens.
53
+ function wrap(s, w, fs, font){
54
+ var out = [];
55
+ lines(s).forEach(function(p){
56
+ var rest = [...p];
57
+ while (rest.length){
58
+ var n = 0;
59
+ while (n < rest.length && base.textWidth(rest.slice(0, n + 1).join(""), fs, {font: font}) <= w) n++;
60
+ n = Math.max(1, n);
61
+ if (n < rest.length){ var space = rest.slice(0, n + 1).lastIndexOf(" "); if (space > 0) n = space; }
62
+ out.push(rest.splice(0, n).join("").trimEnd());
63
+ while (rest[0] === " ") rest.shift();
64
+ }
65
+ if (!p) out.push("");
66
+ });
67
+ return out;
68
+ }
69
+ return class extends base.MotionElement {
70
+ static partName = "code";
71
+ static shape = {schema: {type: "object", additionalProperties: false, required: ["code", "steps"], properties: {
72
+ code: {type: "string"}, lang: {type: "string"}, output: {type: "string"},
73
+ steps: {type: "array", items: {type: "object", additionalProperties: false, required: ["lines", "note"], properties: {
74
+ lines: {type: "array", minItems: 1, maxItems: 2, items: {type: ["integer", "array"], minItems: 2, maxItems: 2, items: {type: "integer"}}}, note: {type: "string"}
75
+ }}}
76
+ }}, 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*/};
77
+ static props = {
78
+ dur: {type: "number", default: 0.6, doc: "ステップの秒数"},
79
+ size: {type: "number", default: 0, doc: "文字 px(0 は既定)"},
80
+ outputPos: {type: "enum", values: ["auto", "below", "right"], default: "auto", doc: "出力の位置"}
81
+ };
82
+ get steps(){ return this.data.steps.length + 1 + (this.data.output !== undefined ? 1 : 0); }
83
+ duration(k){ return k >= 1 && k <= this.steps ? this.props.dur : 0; }
84
+ label(g, x, y, s, fs, color, mono){
85
+ var e = base.el("text", {x: x, y: y, "font-size": fs, fill: color || "var(--fg)"}, g);
86
+ // 等幅と空白:テーマの `svg text{font-family}` は属性より強いので style に書く。行頭の字下げを残す(white-space: pre)
87
+ if (mono){ e.setAttribute("style", (e.getAttribute("style") || "") + "font-family:" + MONO + ";white-space:pre;"); }
88
+ e.textContent = s; return e;
89
+ }
90
+ mount(ctx){
91
+ // [[3, 4]] is read as [3, 4] (one range per step); any other nesting is a range problem below
92
+ var self = this, d = Object.assign({}, this.data, {steps: this.data.steps.map(function(s, j){
93
+ var r = s.lines.length === 1 && Array.isArray(s.lines[0]) ? s.lines[0] : s.lines;
94
+ if (r.some(Array.isArray)){ ctx.report("range", "[code] steps[" + j + "].lines: one range per step, e.g. [3, 4]"); return {lines: [0], note: s.note, nested: true}; }
95
+ return {lines: r, note: s.note};
96
+ })}), P = this.props, T = /^ja\b/i.test(ctx.lang || "") ? TEXT.ja : TEXT.en;
97
+ if (!(Number.isFinite(P.dur) && P.dur > 0)){ ctx.report("attr", "[code] dur must be positive; using 0.6"); P.dur = 0.6; }
98
+ 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; }
99
+ if (!["auto", "below", "right"].includes(P.outputPos)){ ctx.report("attr", "[code] invalid outputPos; using auto"); P.outputPos = "auto"; }
100
+ // 大きさを測る前にブロックにする(インラインのままだと幅が 0 になる)
101
+ this.style.display = "block"; this.style.position = "relative"; this.style.writingMode = "horizontal-tb";
102
+ this.fs = P.size || 24; this.noteSize = P.size || 26;
103
+ var fs = this.fs, nf = this.noteSize, row = Math.ceil(fs * 1.4), noteRow = Math.ceil(nf * 1.45 + 4);
104
+ var W = Math.max(1, this.clientWidth), H = Math.max(1, this.clientHeight), program = lines(d.code), output = d.output === undefined ? [] : lines(d.output);
105
+ var lang = d.lang || "", marks = spans(program, lang.toLowerCase());
106
+ this.setAttribute("role", "figure"); // 中の読める pre が読まれるように(img の子は読まれない)
107
+ 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("; "));
108
+ // Keep the full source readable inside the figure even when the SVG is truncated.
109
+ var pre = document.createElement("pre"); pre.className = "sr-only";
110
+ pre.setAttribute("style", "position:absolute;width:1px;height:1px;margin:-1px;padding:0;overflow:hidden;clip-path:inset(50%);white-space:pre;border:0");
111
+ pre.setAttribute("aria-label", T.readable);
112
+ pre.textContent = d.code + (d.output !== undefined ? "\n\n" + T.output + "\n" + d.output : ""); this.appendChild(pre);
113
+ this.svg = base.el("svg", {viewBox: "0 0 " + W + " " + H, width: W, height: H, "aria-hidden": "true"}, this);
114
+ var header = row + 16, pad = 16, gap = 32, numW = monoWidth(String(program.length), fs) + 16;
115
+ var expanded = program.map(function(s){ return s.replace(/\t/g, " "); });
116
+ var longest = expanded.reduce(function(a, b){ return monoWidth(a, fs) >= monoWidth(b, fs) ? a : b; }, "");
117
+ var natural = Math.max(160, monoWidth(longest, fs)) + numW + 2 * pad;
118
+ 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;
119
+ var outputH = output.length ? header + row * Math.min(8, output.length) + pad : 0;
120
+ var right = output.length > 0 && (P.outputPos === "right" || P.outputPos === "auto" && program.length <= 8 && natural + 280 + outNatural + 2 * gap <= W);
121
+ var outW = right ? Math.max(120, Math.min(outNatural, W * 0.3)) : 0;
122
+ var available = W - (right ? outW + gap : 0), codeW = natural;
123
+ var nx = codeW + gap, nw = Math.max(1, available - nx);
124
+ var areaH = H - (!right && output.length ? outputH + 16 : 0), nextY = header;
125
+ // Keep the beside layout only if *all* the notes fit at their line targets.
126
+ var beside = natural <= W * 0.55 && natural + gap + 260 <= available;
127
+ d.steps.forEach(function(s){
128
+ var a = s.lines[0], b = s.lines.length === 2 ? s.lines[1] : a;
129
+ var target = header + ((a + b - 2) / 2 + 0.5) * row;
130
+ var h = Math.max(1, wrap(s.note, nw - 24, nf, self).length) * noteRow + 16;
131
+ nextY = Math.max(nextY, target - h / 2) + h + 8;
132
+ if (nextY - 8 > areaH) beside = false;
133
+ });
134
+ this.noteLayout = beside ? "beside" : "band";
135
+ if (!beside){
136
+ // 出力は、コードの横に入るなら右に(幅で決める。短い出力なら広いコードの横にも入る)
137
+ right = output.length > 0 && (P.outputPos === "right" || P.outputPos === "auto" && natural + gap + Math.max(120, outNatural) <= W);
138
+ outW = right ? Math.max(120, Math.min(outNatural, W - gap - natural)) : 0;
139
+ available = W - (right ? outW + gap : 0);
140
+ codeW = Math.max(1, Math.min(natural, available));
141
+ nx = 0; nw = right ? codeW : W;
142
+ areaH = H - (!right && output.length ? outputH + 16 : 0);
143
+ }
144
+ // 下帯の最終一覧:説明ごとに行の範囲を付け(「3–4:…」)、説明の途中では折らずに行へ詰める。字は 24px、行間は字の箱 1.45em
145
+ var listFont = Math.max(24, nf - 2), listRow = Math.ceil(listFont * 1.45);
146
+ 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, " "); });
147
+ // 出力が下に回るときは説明帯の右に並べ、帯と出力の高い方だけを使う
148
+ var sideW = Math.max(120, outNatural);
149
+ var side = !beside && !right && output.length > 0 && P.outputPos === "auto" && d.steps.length > 0 && outNatural <= W * 0.45 && W - sideW - gap >= W * 0.4;
150
+ if (side){ nw = W - sideW - gap; areaH = H; }
151
+ var list = [];
152
+ if (!beside && d.steps.length){
153
+ var cur = "", sep = T === TEXT.ja ? " " : " ";
154
+ items.forEach(function(t){
155
+ var cand = cur ? cur + sep + t : t;
156
+ if (cur && base.textWidth(cand, listFont, {font: self}) > nw - 24){ list.push(cur); cur = t; } else cur = cand;
157
+ });
158
+ if (cur) list.push(cur);
159
+ }
160
+ // 高さの式は 1 つ:コード部 + 帯(一覧 r 行)と横の出力の高い方 + 下の出力。報告もこの式で数える
161
+ function bandFor(r){ return r ? noteRow + 16 + (r - 1) * listRow : 0; }
162
+ 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)); }
163
+ var want = Math.min(16, program.length), rows = list.length, fullList = true;
164
+ function fitsAt(h, r){ return Math.max(0, Math.floor((h - need(0, r)) / row)); }
165
+ var capacity = fitsAt(H, rows);
166
+ if (capacity < want && rows > 1 && fitsAt(H, 1) >= want){ fullList = false; capacity = fitsAt(H, 1); } // コードを全部残し、最終一覧は最後の説明だけ
167
+ var bandH = bandFor(fullList ? rows : Math.min(1, rows)), bandSpace = bandH ? bandH + 8 : 0;
168
+ var count = Math.min(want, capacity), codeH = header + count * row + pad;
169
+ var neededH = need(want, rows), SLIDE = 440;
170
+ function advice(){
171
+ 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";
172
+ }
173
+ var textX = pad + numW, firstY = header + fs, textW = Math.max(0, codeW - textX - pad);
174
+ 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" : ""));
175
+ 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)");
176
+ 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};
177
+ this.codeLayer = base.el("g", {opacity: 0}, this.svg);
178
+ 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); }
179
+ panel(this.codeLayer, 1, 1, codeW - 2, codeH - 2);
180
+ var tagW = Math.max(1, codeW - 2 * pad), tag = cut(lang, tagW, fs);
181
+ if (count || H >= header + pad) this.label(this.codeLayer, codeW - pad - monoWidth(tag, fs), pad + fs, tag, fs, "var(--muted)", true);
182
+ 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); });
183
+ if (tag !== lang) ctx.report("layout", "[code] language tag needs " + Math.ceil(monoWidth(lang, fs)) + " px; header fits " + Math.floor(tagW) + " px");
184
+ this.numbers = [];
185
+ for (var i = 0; i < count; i++){
186
+ var y = firstY + i * row, original = program[i].replace(/\t/g, " "), s = cut(original, Math.max(0, codeW - textX - pad), fs);
187
+ if (i === count - 1 && count < program.length) s = cut(s + "…", Math.max(0, codeW - textX - pad), fs);
188
+ this.numbers.push(this.label(this.codeLayer, pad, y, String(i + 1), fs, "var(--muted)", true));
189
+ // One <text> per line; coloured runs are tspans, so the browser's own advance places them (indices after tab expansion)
190
+ var e = this.label(this.codeLayer, textX, y, "", fs, "var(--fg)", true), pos = 0, line = program[i];
191
+ var put = function(a, b, role){ if (b > a) base.el("tspan", role ? {fill: "var(--" + role + ")"} : {}, e).textContent = s.slice(a, b); };
192
+ marks[i].forEach(function(m){
193
+ var a = Math.min(s.length, line.slice(0, m[0]).replace(/\t/g, " ").length), b = Math.min(s.length, line.slice(0, m[1]).replace(/\t/g, " ").length);
194
+ put(pos, a); put(a, b, m[2]); pos = b;
195
+ });
196
+ put(pos, s.length);
197
+ }
198
+ if (count === 0 || W < 360 || textW < fs){
199
+ 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");
200
+ }
201
+ // Even unusably small boxes retain a clipped panel and an ellipsis.
202
+ this.svg.setAttribute("style", "overflow:hidden");
203
+ if (!count) this.label(this.codeLayer, Math.min(pad, W / 4), Math.min(fs, H), "…", fs);
204
+ nextY = header;
205
+ var bandY = codeH + 8, bandRoom = Math.max(0, areaH - bandY);
206
+ function proseCut(s, width, f){
207
+ f = f || nf;
208
+ if (base.textWidth(s, f, {font: self}) <= width) return s;
209
+ var chars = [...s];
210
+ while (chars.length && base.textWidth(chars.join("") + "…", f, {font: self}) > width) chars.pop();
211
+ return width >= f ? chars.join("") + "…" : "";
212
+ }
213
+ function marker(a, b){ return T === TEXT.ja ? a + (b === a ? "" : "–" + b) + " 行目" : (a === b ? "line " + a : "lines " + a + "–" + b); }
214
+ this.notes = d.steps.map(function(s, j){
215
+ var a = s.lines[0], b = s.lines.length === 2 ? s.lines[1] : a;
216
+ var valid = a >= 1 && b >= a && b <= program.length;
217
+ if (!valid && !s.nested) ctx.report("range", "[code] steps[" + j + "].lines must be an inclusive range within 1–" + program.length);
218
+ var target = valid && a <= count ? header + ((a - 1 + Math.min(b, count) - 1) / 2 + 0.5) * row : header;
219
+ var g = base.el("g", {opacity: 0}, self.svg), text, top, h, labels;
220
+ if (beside){
221
+ text = wrap(s.note, nw - 24, nf, self); h = Math.max(noteRow, text.length * noteRow) + 16;
222
+ top = Math.max(nextY, target - h / 2);
223
+ 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);
224
+ base.el("rect", {x: nx, y: top, width: nw - 1, height: h, rx: 8, fill: "var(--bg)", stroke: "var(--line)", "stroke-width": 2}, g);
225
+ labels = text.map(function(t, k){ return self.label(g, nx + 12, top + 8 + nf + k * noteRow, t, nf); });
226
+ nextY = top + h + 8;
227
+ } else {
228
+ top = bandY; h = Math.min(bandFor(1), bandRoom);
229
+ var prefix = marker(a, b) + " · ", prefixW = base.textWidth(prefix, nf, {font: self}), room = Math.max(0, nw - 24 - prefixW);
230
+ var width = base.textWidth(s.note, nf, {font: self});
231
+ if (width > room || lines(s.note).length > 1){
232
+ var approx = Math.max(0, Math.floor([...s.note].length * room / Math.max(1, width)));
233
+ 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");
234
+ }
235
+ 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);
236
+ labels = [self.label(g, 12, top + 8 + nf, proseCut(prefix + s.note.replace(/\s*\n\s*/g, " "), nw - 24), nf)];
237
+ }
238
+ 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); }
239
+ return {g: g, labels: labels, a: a, b: b, valid: valid && a <= count, top: top, height: h, fits: true};
240
+ });
241
+ this.finalNotes = base.el("g", {opacity: 0}, this.svg);
242
+ if (!beside && d.steps.length){
243
+ list = list.map(function(t){ return proseCut(t, nw - 24, listFont); });
244
+ if (!fullList || bandFor(list.length) > bandRoom){
245
+ 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");
246
+ list = [proseCut(items[items.length - 1], nw - 24, listFont)];
247
+ }
248
+ 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);
249
+ list.forEach(function(s, i){ self.label(self.finalNotes, 12, bandY + 8 + nf + i * listRow, s, listFont); });
250
+ }
251
+ this.outputLayer = base.el("g", {opacity: 0}, this.svg);
252
+ if (output.length){
253
+ var ox = right ? W - outW : side ? W - sideW : 0, oy = right ? 0 : side ? codeH + 8 : areaH + 16, ow = right ? outW : side ? sideW : codeW;
254
+ var oc = Math.max(0, Math.min(8, output.length, Math.floor((H - oy - header - pad) / row)));
255
+ panel(this.outputLayer, ox + 1, oy + 1, ow - 2, header + oc * row + pad - 2);
256
+ self.label(this.outputLayer, ox + pad, oy + pad + fs, cut(T.output, ow - 2 * pad, fs), fs);
257
+ // Run precedes the supplied plain text in a separate header region.
258
+ var run = cut(T.run, ow - 2 * pad - monoWidth(T.output, fs) - pad, fs);
259
+ self.label(this.outputLayer, ox + ow - pad - monoWidth(run, fs), oy + pad + fs, run, fs, "var(--muted)", true);
260
+ var outputCut = oc < output.length || run !== T.run || monoWidth(T.output, fs) > ow - 2 * pad;
261
+ output.slice(0, oc).forEach(function(s, i){
262
+ var txt = cut(s.replace(/\t/g, " "), ow - 2 * pad, fs);
263
+ if (i === oc - 1 && oc < output.length) txt = cut(txt + "…", ow - 2 * pad, fs);
264
+ if (txt !== s) outputCut = true;
265
+ self.label(self.outputLayer, ox + pad, oy + header + fs + i * row, txt, fs, "var(--fg)", true);
266
+ });
267
+ if (outputCut) ctx.report("layout", "[code] output exceeds eight lines or panel width/height; showing what fits with …");
268
+ }
269
+ if (outputH > H) ctx.report("size", "[code] output needs " + outputH + " px height; element has " + H + " px");
270
+ }
271
+ render(k, t, ctx){
272
+ if (!this.codeLayer) return;
273
+ ctx = ctx || {};
274
+ var self = this, preview = ctx.preview || ctx.mode === "lecture", faint = preview ? base.previewOpacity(this) : 0;
275
+ var phase = function(step){ return k > step ? 1 : k < step ? faint : base.prog(t, 0, self.props.dur); };
276
+ set(this.codeLayer, "opacity", phase(1));
277
+ var final = k > this.steps || k === this.steps && t >= this.props.dur, active = !final && k >= 2 && k <= this.notes.length + 1 ? k - 2 : -1;
278
+ this.notes.forEach(function(n, i){
279
+ var band = self.noteLayout === "band", shown = !final && (i === active || active < 0 && k > self.notes.length + 1 && i === self.notes.length - 1);
280
+ set(n.g, "opacity", band ? (shown ? phase(i + 2) : 0) : phase(i + 2));
281
+ n.labels.forEach(function(e){ set(e, "style", "fill:var(--" + (i < k - 2 && !final ? "muted" : "fg") + ")"); });
282
+ set(self.bands[i], "opacity", i === active && n.valid ? base.prog(t, 0, self.props.dur) : 0);
283
+ });
284
+ set(this.finalNotes, "opacity", this.noteLayout === "band" ? (final ? 1 : k < 2 ? faint : 0) : 0);
285
+ 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") + ")"); });
286
+ set(this.outputLayer, "opacity", phase(this.notes.length + 2));
287
+ }
288
+ };
289
+ });