egatari 0.6.17 → 0.6.18

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 (137) hide show
  1. package/CHANGELOG.md +36 -1
  2. package/README.ja.md +14 -13
  3. package/README.md +2 -1
  4. package/components.lock.json +119 -45
  5. package/components2.lock.json +639 -1
  6. package/kit/check-messages.mjs +5 -0
  7. package/kit/check.mjs +6 -0
  8. package/kit/core/bookmark.mjs +28 -0
  9. package/kit/core/build-core.mjs +4 -2
  10. package/kit/core/check-parity.mjs +4 -0
  11. package/kit/core/check-rules.mjs +3 -2
  12. package/kit/core/check-source.mjs +6 -0
  13. package/kit/core/llm-pack-assemble.mjs +37 -66
  14. package/kit/core/llm-pack-data.mjs +839 -45
  15. package/kit/core/llm-pack.mjs +6 -4
  16. package/kit/core/llm-request.mjs +2 -2
  17. package/kit/core/new-deck-pack.mjs +38 -44
  18. package/kit/core/new-deck.mjs +82 -74
  19. package/kit/core/part-labels.mjs +2 -0
  20. package/kit/core/purposes.mjs +9 -3
  21. package/kit/core/sample-info.mjs +17 -3
  22. package/kit/core/slide-ops.mjs +22 -0
  23. package/kit/core/tate-mix.mjs +25 -0
  24. package/kit/core/theme-derive.mjs +56 -17
  25. package/kit/core/theme-shapes.mjs +15 -1
  26. package/kit/deck.js +53 -2
  27. package/kit/instruction-version.json +1 -1
  28. package/kit/llm-pack.mjs +5 -7
  29. package/kit/player.json +1 -1
  30. package/kit/player.lock.json +2 -2
  31. package/kit/presenter.js +36 -4
  32. package/kit/tools.js +54 -2
  33. package/package.json +1 -1
  34. package/vendor/components/CATALOG.md +24 -22
  35. package/vendor/components/sha256/038f9d32c89c8212ab25d6e3180023b6fe024a53053ab2ef1443ecab92e3ce43/index.js +205 -0
  36. package/vendor/components/sha256/07479cb6e5194a01b2a8a9029f3646060f0dbb673f2737d2132738af2eb379e5/README.en.md +44 -0
  37. package/vendor/components/sha256/07479cb6e5194a01b2a8a9029f3646060f0dbb673f2737d2132738af2eb379e5/index.js +306 -0
  38. package/vendor/components/sha256/077678d8b14a8065c4f74a9fe1669728c68a0f763bfdca18b304f5c3d863632a/README.en.md +54 -0
  39. package/vendor/components/sha256/077678d8b14a8065c4f74a9fe1669728c68a0f763bfdca18b304f5c3d863632a/index.js +470 -0
  40. package/vendor/components/sha256/0840322bf51eeb3c8188ce21ad70af17c13974ac0151065a39c430a3813ee07f/README.en.md +23 -0
  41. package/vendor/components/sha256/0840322bf51eeb3c8188ce21ad70af17c13974ac0151065a39c430a3813ee07f/index.js +289 -0
  42. package/vendor/components/sha256/0c25d49d23529879054737aa181951519fe585c4728fd505e0e3c8869953eac7/index.js +934 -0
  43. package/vendor/components/sha256/0dd302b82925f35743a893d5c271898ada3659aa0de2d3249ef45936aad10ae0/LICENSE-strokesvg.txt +115 -0
  44. package/vendor/components/sha256/0dd302b82925f35743a893d5c271898ada3659aa0de2d3249ef45936aad10ae0/OFL.txt +93 -0
  45. package/vendor/components/sha256/0dd302b82925f35743a893d5c271898ada3659aa0de2d3249ef45936aad10ae0/index.js +755 -0
  46. package/vendor/components/sha256/0dd302b82925f35743a893d5c271898ada3659aa0de2d3249ef45936aad10ae0/strokes-CHANGES.md +20 -0
  47. package/vendor/components/sha256/0e9e92df8d70eb6d9fcf10916c361769c7cd37baacece8d7eb96bbbc7fbcf942/README.en.md +25 -0
  48. package/vendor/components/sha256/0e9e92df8d70eb6d9fcf10916c361769c7cd37baacece8d7eb96bbbc7fbcf942/index.js +317 -0
  49. package/vendor/components/sha256/12ef6a11055407f5cddc40154a49cf2737b0786429fe0fcc94ee1d8245597b40/index.js +244 -0
  50. package/vendor/components/sha256/13c9ce43396b2f5693ce430a924dae4ddcd1e8a4ad0c9b9e724ff52acb874d0c/index.js +354 -0
  51. package/vendor/components/sha256/1a0f21def0efbbdef64461cafa40f3883bc7f7122adf79d01f09ca4a245e5dcb/index.js +465 -0
  52. package/vendor/components/sha256/208175d05b37d326855fad792b10137f785e5d05d91a2cde119d0dbd69d2ed98/index.js +100 -0
  53. package/vendor/components/sha256/218d88998b70f0732c18d91ccc238b2d63f6b0aaa483883b83685a25468f214a/README.en.md +34 -0
  54. package/vendor/components/sha256/218d88998b70f0732c18d91ccc238b2d63f6b0aaa483883b83685a25468f214a/index.js +246 -0
  55. package/vendor/components/sha256/2579bcb46b6525902763cd15a2e08fc5ebd9a2695cdaba63438e5b47907a47e0/README.en.md +24 -0
  56. package/vendor/components/sha256/2579bcb46b6525902763cd15a2e08fc5ebd9a2695cdaba63438e5b47907a47e0/index.js +554 -0
  57. package/vendor/components/sha256/2787e410e533e4ad48b0a3c2b652579cf5797f9c51fcf32f5478212a7acef95f/README.en.md +28 -0
  58. package/vendor/components/sha256/2787e410e533e4ad48b0a3c2b652579cf5797f9c51fcf32f5478212a7acef95f/index.js +214 -0
  59. package/vendor/components/sha256/2a011338d0fc3c77e102eaa57cde2b6e3726bc279f133174b0b8c9a654263cbc/index.js +824 -0
  60. package/vendor/components/sha256/2fce5ab0c31dca0aa1586a2c69237870a9cce46c0bc6ab7392c27968d016bd15/index.js +75 -0
  61. package/vendor/components/sha256/3417efe4722f569690563d9abcea8b144db2f8fa97b83cf0118fb3a34cd4cb2b/README.en.md +23 -0
  62. package/vendor/components/sha256/3417efe4722f569690563d9abcea8b144db2f8fa97b83cf0118fb3a34cd4cb2b/index.js +150 -0
  63. package/vendor/components/sha256/3a7962fa87c670447eed54aca08604ef03cd26bf2c334b4667190060e6a17965/README.en.md +18 -0
  64. package/vendor/components/sha256/3a7962fa87c670447eed54aca08604ef03cd26bf2c334b4667190060e6a17965/index.js +279 -0
  65. package/vendor/components/sha256/42450c6b36404d66641568f6d6f55e42a4a036d4d9cdf308f709464913b32fe9/README.en.md +42 -0
  66. package/vendor/components/sha256/42450c6b36404d66641568f6d6f55e42a4a036d4d9cdf308f709464913b32fe9/index.js +272 -0
  67. package/vendor/components/sha256/45cea6c1abfb3467e3e3dbe7fec58647716999a0c39cf7163531ae389f898d29/README.en.md +30 -0
  68. package/vendor/components/sha256/45cea6c1abfb3467e3e3dbe7fec58647716999a0c39cf7163531ae389f898d29/index.js +305 -0
  69. package/vendor/components/sha256/4c8cae404d551a1bb021e8345af5aa45d4f412f13497aa84b142f11a29a365dc/index.js +124 -0
  70. package/vendor/components/sha256/5521b5b40a43c48c5ae5a50ce87afb3d47bb23b133b16841086d8322364cdd5e/README.en.md +23 -0
  71. package/vendor/components/sha256/5521b5b40a43c48c5ae5a50ce87afb3d47bb23b133b16841086d8322364cdd5e/index.js +120 -0
  72. package/vendor/components/sha256/5fe30fd7a84b937a1325d8af2b8e437211efae82ab46d5a433bc4ef07bcaef11/README.en.md +18 -0
  73. package/vendor/components/sha256/5fe30fd7a84b937a1325d8af2b8e437211efae82ab46d5a433bc4ef07bcaef11/index.js +243 -0
  74. package/vendor/components/sha256/631f4c4b90c9a41c0f26c9600d5cf2d6447251d04a20f08f48a85aeb4cb0f6fb/index.js +107 -0
  75. package/vendor/components/sha256/6749f44ed6476a05d4e3026373c188a0e1a19b77a5836579813f62a1477ac35e/README.en.md +31 -0
  76. package/vendor/components/sha256/6749f44ed6476a05d4e3026373c188a0e1a19b77a5836579813f62a1477ac35e/index.js +271 -0
  77. package/vendor/components/sha256/701f1deef79fe05eeb18e1119e5a5c4d60a9dfe265e93a4dff79223bc598dbf0/index.js +521 -0
  78. package/vendor/components/sha256/711cf58fa946c9c0abc953b2f695d02bf5a5a7f73851b09f834e894baf88be38/index.js +122 -0
  79. package/vendor/components/sha256/728576c57a5ccb39531ad4cb8074fce95480f01603a77dbf57dca0e52199f7c4/index.js +37 -0
  80. package/vendor/components/sha256/73edc74a2dc4a63b717106c1c1572967b6d306270574e9635d032de16c8ada72/README.en.md +18 -0
  81. package/vendor/components/sha256/73edc74a2dc4a63b717106c1c1572967b6d306270574e9635d032de16c8ada72/index.js +382 -0
  82. package/vendor/components/sha256/76bdb63c3e5e14fbb4816bc9f235490817bb5d7be990a826f5077aa08226702c/index.js +162 -0
  83. package/vendor/components/sha256/7a5adf73fc053b9f2ca56643b5683b42589edb0f63c845ecb7ae7e6b87eccc30/README.en.md +51 -0
  84. package/vendor/components/sha256/7a5adf73fc053b9f2ca56643b5683b42589edb0f63c845ecb7ae7e6b87eccc30/index.js +230 -0
  85. package/vendor/components/sha256/7ab483e7fb05f646b062340d70f2749d070b4f64fed7acc0b8a7754d9ef9fded/README.en.md +26 -0
  86. package/vendor/components/sha256/7ab483e7fb05f646b062340d70f2749d070b4f64fed7acc0b8a7754d9ef9fded/index.js +322 -0
  87. package/vendor/components/sha256/7af6e5e6dffb618b39f27d883c691ca128f3af06040ee505e75c79c678bfdcfe/README.en.md +18 -0
  88. package/vendor/components/sha256/7af6e5e6dffb618b39f27d883c691ca128f3af06040ee505e75c79c678bfdcfe/index.js +103 -0
  89. package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/ABCJS-ADAPTER.md +101 -0
  90. package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/LICENSE-abcjs.txt +21 -0
  91. package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/chords.json +89 -0
  92. package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/doremi.json +261 -0
  93. package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/extras.json +192 -0
  94. package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/kaeru.json +521 -0
  95. package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/rhythm.json +248 -0
  96. package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/ties.json +360 -0
  97. package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/index.js +508 -0
  98. package/vendor/components/sha256/832556ea9c82c292a3ba9e1b332f2405c5c2f969ff8157c91ed80226ec8ac62e/README.en.md +22 -0
  99. package/vendor/components/sha256/832556ea9c82c292a3ba9e1b332f2405c5c2f969ff8157c91ed80226ec8ac62e/index.js +145 -0
  100. package/vendor/components/sha256/8a4b6dce84d7a69aaafeb75c1c572fe7904fd49e3169c1c85851ccaeae2a7b11/index.js +41 -0
  101. package/vendor/components/sha256/8bb511cb90902bc7721e59ca08685b26adbf33539e36a4a45172485d6d3d455b/index.js +250 -0
  102. package/vendor/components/sha256/8db9c936c36b79b9e3b2be37639487b15502a6937f411ed714621a8091437e9b/README.en.md +21 -0
  103. package/vendor/components/sha256/8db9c936c36b79b9e3b2be37639487b15502a6937f411ed714621a8091437e9b/index.js +108 -0
  104. package/vendor/components/sha256/8e5e90c90cb6b06a790b0420e43d79d7da663f1cc19ff06d381ec849b4a8fc57/index.js +312 -0
  105. package/vendor/components/sha256/a1c2b2fa06e4229815ab82a9202c266ec887a67632a175854c7d136a92389ca3/index.js +69 -0
  106. package/vendor/components/sha256/a7c4282db79d46557bdbaf84ca519606dc46f3c23455db4a26f663453f85813c/README.en.md +38 -0
  107. package/vendor/components/sha256/a7c4282db79d46557bdbaf84ca519606dc46f3c23455db4a26f663453f85813c/index.js +414 -0
  108. package/vendor/components/sha256/aac2f8a23cb1b606e9635061cb288f006588c80a7258540cae846ae677f55ff5/index.js +238 -0
  109. package/vendor/components/sha256/ace7e82e866b577ad42bbc4b64e11bae18982e3236aadceb6c2e6c20f41234cf/README.en.md +45 -0
  110. package/vendor/components/sha256/ace7e82e866b577ad42bbc4b64e11bae18982e3236aadceb6c2e6c20f41234cf/index.js +211 -0
  111. package/vendor/components/sha256/aeb0b9b5991a5c1df7191c397780a699f2c7310123ca210a58486c0a23e44006/index.js +342 -0
  112. package/vendor/components/sha256/af3355efd6739a56c6cb53990100f41703361c475c123b9b2762421c68509036/index.js +315 -0
  113. package/vendor/components/sha256/b10183a66b58b827a1410cf8b0117af567647684464d5777529dc66f0cea343a/LICENSE-tabler.txt +21 -0
  114. package/vendor/components/sha256/b10183a66b58b827a1410cf8b0117af567647684464d5777529dc66f0cea343a/index.js +4868 -0
  115. package/vendor/components/sha256/b10183a66b58b827a1410cf8b0117af567647684464d5777529dc66f0cea343a/names.txt +4808 -0
  116. package/vendor/components/sha256/b348ea64cc19632df96a91dfd7c435e4a62095aede72b504d03537be70d6d930/index.js +606 -0
  117. package/vendor/components/sha256/b53bfa03d1e9feb3cb492f440bf9d100c96eb4fe6b76c42b512a56e9158829cd/index.js +351 -0
  118. package/vendor/components/sha256/b9443e1cdac4850b1ead9f8449a3061678bbb77fbc53eb9b23d01e1cf50750c3/index.js +619 -0
  119. package/vendor/components/sha256/bc1e2a02c959ac203819d03c0d25b3776a6a9a4290aae860b26cbbe3d29e876d/index.js +268 -0
  120. package/vendor/components/sha256/beeb180c0e2250bb04490b092c0a235cae47fca2ed1de2d9ef078a5c14b1a963/index.js +429 -0
  121. package/vendor/components/sha256/c5fd7713e55e96d75824a21146c0251896d7d9dd028b4d169191433135a97981/index.js +200 -0
  122. package/vendor/components/sha256/c61987e9994940fc668ed8a010e575ee52027b54ed9c5dd1cc9fdf67649092b3/index.js +404 -0
  123. package/vendor/components/sha256/ccc8bfe174b6903ec254dfef4f5d1c614271ea39963649add7fcb8d83c081eba/index.js +278 -0
  124. package/vendor/components/sha256/da267bc65fde0a9279127d5a82f6c253ad74984da6fb11391f00604ad9ffe80d/index.js +93 -0
  125. package/vendor/components/sha256/dea36cd7010991a572ef394ee32b5e17564aeff09d3ec4ed3820d7e43c9a58be/index.js +484 -0
  126. package/vendor/components/sha256/df3fdb2aed487d4f6e60a8abab1fe0d1e9fa2a4193fd162edf3c384ea8db5a1b/index.js +180 -0
  127. package/vendor/components/sha256/e27d517f39eb554c7a9ecea7b59fcf1d347c4deeb3fe2250797aa233bfb7e2b0/index.js +747 -0
  128. package/vendor/components/sha256/e31f6b058503f7980597c89b3e964c512ee08de7cba3fa99904d499abbb5d0e8/README.en.md +20 -0
  129. package/vendor/components/sha256/e31f6b058503f7980597c89b3e964c512ee08de7cba3fa99904d499abbb5d0e8/index.js +310 -0
  130. package/vendor/components/sha256/f4100076a37dbde6046d85969d651feffc925bd984ffe9f333ea9bd2b6816918/README.en.md +30 -0
  131. package/vendor/components/sha256/f4100076a37dbde6046d85969d651feffc925bd984ffe9f333ea9bd2b6816918/index.js +192 -0
  132. package/vendor/components/sha256/fa6442fa3c5082cf161c29308fe0ecbd85fa33e8e4bad08c12e7961b46b1365b/README.en.md +24 -0
  133. package/vendor/components/sha256/fa6442fa3c5082cf161c29308fe0ecbd85fa33e8e4bad08c12e7961b46b1365b/index.js +214 -0
  134. package/vendor/components-2/manifest-2.16.0.json +2919 -0
  135. package/vendor/components-2/manifest-2.17.0.json +2852 -0
  136. package/vendor/components-2/manifest-2.18.0.json +3182 -0
  137. package/vendor/egatari-spec/SOURCE.json +2 -2
@@ -0,0 +1,107 @@
1
+ // code-core 1.0.0: shared code drawing for m-code and m-trace (a library part, Egatari spec §6.1). Pure functions of their
2
+ // arguments except wrap, which measures prose with base.textWidth. No DOM is created here and no state is kept.
3
+ // Moved verbatim from m-code 2.1.0 (lines, monoWidth, cut, the colour roles of KEYWORDS / dialect / spans, wrap) so that m-code
4
+ // 2.2.0 draws the same pixels; new in 1.0.0: softWrap (visual rows of a long code line) and windowFor (the lines a moving
5
+ // window shows for a step).
6
+ // returns {MONO, set, lines, monoWidth, cut, dialect, spans, wrap, softWrap, windowFor}
7
+ egatariParts.define({name: "code-core", version: "1.0.1", requires: {base: "1.3.0"}}, function(deps){
8
+ "use strict";
9
+ var base = deps.base, MONO = 'ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace';
10
+ function set(e, a, v){ e.setAttribute(a, String(v)); }
11
+ function lines(s){ return s.replace(/\r\n?/g, "\n").split("\n"); }
12
+ function monoWidth(s, fs){ return [...s].reduce(function(w, ch){ return w + (/[\u0000-\u024f]/.test(ch) ? 0.6 : 1); }, 0) * fs; }
13
+ function cut(s, w, fs){
14
+ if (monoWidth(s, fs) <= w) return s;
15
+ var ch = [...s];
16
+ while (ch.length && monoWidth(ch.join("") + "…", fs) > w) ch.pop();
17
+ return w >= fs ? ch.join("") + "…" : "";
18
+ }
19
+ // Colour roles for code: keywords term, strings accent2, comments muted (theme roles only; the text stays readable without colour).
20
+ var KEYWORDS = {
21
+ 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",
22
+ 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",
23
+ r: "if else repeat while function for in next break TRUE FALSE NULL Inf NaN NA return",
24
+ shell: "if then else elif fi case esac for while until do done in function return export local"
25
+ };
26
+ function dialect(lang){ return lang === "javascript" ? "js" : lang === "sh" || lang === "bash" ? "shell" : KEYWORDS[lang] ? lang : ""; }
27
+ // Split each line into [start, end, role] spans: strings (also across lines for Python triple quotes / JS templates),
28
+ // line comments outside strings, keywords outside both. Unknown languages give no spans.
29
+ function spans(program, lang){
30
+ var L = dialect(lang), kw = L ? new Set(KEYWORDS[L].split(" ")) : null, quote = "", triple = false;
31
+ return program.map(function(s){
32
+ var out = [], from = quote ? 0 : -1;
33
+ if (!L) return out;
34
+ for (var i = 0; i < s.length; i++){
35
+ var c = s[i];
36
+ if (quote){
37
+ if (c === "\\"){ i++; continue; }
38
+ 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; }
39
+ } else if (c === '"' || c === "'" || L === "js" && c === "`"){
40
+ from = i; quote = c; triple = L === "python" && s.slice(i, i + 3) === c.repeat(3); if (triple) i += 2;
41
+ } else if (L !== "js" && c === "#" && (L !== "shell" || i === 0 || /\s/.test(s[i - 1])) || L === "js" && s.slice(i, i + 2) === "//"){
42
+ out.push([i, s.length, "muted"]); return out;
43
+ } else if (/[A-Za-z_]/.test(c) && (i === 0 || !/[\w.$]/.test(s[i - 1]))){
44
+ var m = /^[A-Za-z_]\w*/.exec(s.slice(i))[0];
45
+ if (kw.has(m)) out.push([i, i + m.length, "term"]);
46
+ i += m.length - 1;
47
+ }
48
+ }
49
+ if (quote){ out.push([from, s.length, "accent2"]); if (!triple && quote !== "`") quote = ""; }
50
+ return out;
51
+ });
52
+ }
53
+ // Wrap prose on words where possible, falling back to characters for Japanese / long tokens.
54
+ function wrap(s, w, fs, font){
55
+ var out = [];
56
+ lines(s).forEach(function(p){
57
+ var rest = [...p];
58
+ while (rest.length){
59
+ var n = 0;
60
+ while (n < rest.length && base.textWidth(rest.slice(0, n + 1).join(""), fs, {font: font}) <= w) n++;
61
+ n = Math.max(1, n);
62
+ if (n < rest.length){ var space = rest.slice(0, n + 1).lastIndexOf(" "); if (space > 0) n = space; }
63
+ out.push(rest.splice(0, n).join("").trimEnd());
64
+ while (rest[0] === " ") rest.shift();
65
+ }
66
+ if (!p) out.push("");
67
+ });
68
+ return out;
69
+ }
70
+ // Visual rows of one code line (tabs already expanded) that fit `width` px at `fs` px: break after the last space, comma or
71
+ // opening bracket that fits, else at the width. → [{from, to}] character offsets into the line (code points), at least one row.
72
+ function softWrap(s, width, fs){
73
+ var ch = [...s], out = [], from = 0;
74
+ if (!ch.length) return [{from: 0, to: 0}];
75
+ while (from < ch.length){
76
+ var n = 0;
77
+ while (from + n < ch.length && monoWidth(ch.slice(from, from + n + 1).join(""), fs) <= width) n++;
78
+ n = Math.max(1, n);
79
+ if (from + n < ch.length){
80
+ for (var k = n; k > Math.floor(n / 3); k--){ if (/[ ,([{]/.test(ch[from + k - 1])){ n = k; break; } }
81
+ }
82
+ out.push({from: from, to: from + n}); from += n;
83
+ }
84
+ return out;
85
+ }
86
+ // The rows a moving window of `slots` rows shows so that rows ra..rb are visible, centred where possible. A hidden part
87
+ // above or below costs one row for its "⋮" marker. → {f, l, top, bottom, fits}: rows f..l are shown; top / bottom are true when
88
+ // a marker sits above / below them; fits is false when ra..rb is taller than the window (then the window starts at ra).
89
+ function windowFor(n, slots, ra, rb){
90
+ if (n <= slots) return {f: 0, l: n - 1, top: false, bottom: false, fits: true};
91
+ var best = null;
92
+ for (var f = 0; f < n; f++){
93
+ var top = f > 0, room = slots - (top ? 1 : 0), l = f + room - 1, bottom = l < n - 1;
94
+ if (bottom) l -= 1;
95
+ if (l >= n - 1){ l = n - 1; bottom = false; }
96
+ if (l < f) continue;
97
+ // まず表示する範囲を埋める(空いた行が少ない方)、次に中心が近い方
98
+ var unused = slots - (top ? 1 : 0) - (bottom ? 1 : 0) - (l - f + 1), fits = ra >= f && rb <= l, score = unused * 1000 + Math.abs((f + l) / 2 - (ra + rb) / 2);
99
+ if (fits && (!best || score < best.score - 1e-9)) best = {f: f, l: l, top: top, bottom: bottom, fits: true, score: score};
100
+ }
101
+ if (best){ delete best.score; return best; }
102
+ var f2 = Math.max(0, Math.min(ra, n - 1)), top2 = f2 > 0, l2 = Math.min(n - 1, f2 + slots - (top2 ? 1 : 0) - 1), bottom2 = l2 < n - 1;
103
+ if (bottom2) l2 -= 1;
104
+ return {f: f2, l: Math.max(f2, l2), top: top2, bottom: bottom2, fits: false};
105
+ }
106
+ return {MONO: MONO, set: set, lines: lines, monoWidth: monoWidth, cut: cut, dialect: dialect, spans: spans, wrap: wrap, softWrap: softWrap, windowFor: windowFor};
107
+ });
@@ -0,0 +1,31 @@
1
+ ---
2
+ summary: Outcomes → values → distribution
3
+ steps: Outcomes → arrows → groups → bars (4)
4
+ props:
5
+ dur: Motion s
6
+ stagger: Arrow delay s
7
+ digits: Decimals
8
+ size: Text px
9
+ example: {"outcomes":[{"label":"HH","x":2},{"label":"HT","x":1},{"label":"TH","x":1},{"label":"TT","x":0}],"name":"X","unit":"","fractions":true}
10
+ ---
11
+ - `outcomes`: 2–12; `label`, `x`, `p` (default equal; sum ≠ 1 reported)
12
+ - `name` (X), `fractions`
13
+ - `picture`: `dots`, `grapes`, `dice`, `coins`, `balls` (colour names; 11 basic)
14
+
15
+ <!-- llm: omit -->
16
+ For introductory probability. Left: Ω; centre: values; right: distribution. Chips → arrows → shared colours, counts and sums → bars, end labels and total. Final state stands alone; no answer preview.
17
+
18
+ - `outcomes`: 2–12 items; short `label` (≤6 chars advised), finite `x`. Repeated values allowed. Input chip order; numerical axis increases upwards.
19
+ - `p`: optional. All omitted: `1/n`. Mixed: report `data`, missing = 0. Sum outside 1±1e-9 or p outside 0–1: report, retain values. No normalization.
20
+ - `name`: X; `unit`: suffix, default empty. Literal labels.
21
+ - `fractions`: defaults true for equal probabilities. Reduced fractions: denominator ≤10000, error ≤1e-9; otherwise `digits` decimals. Invalid totals stay unrounded.
22
+ - `picture`: draw outcomes: `dots`/`grapes` (x items, 0–15), `dice` (label face 1–6), `coins` (◯ heads, ● tails), `balls` (colour names per outcome `balls` or label characters; 11 basic colours; name shown)
23
+
24
+ Equal probabilities: shown once in the title. Colours cycle series-1..4 by ascending value. Bar scale includes 0–1; extends for invalid data.
25
+
26
+ About 1184×440px for 4 outcomes; rows ≥40px; tight space reports `size`.
27
+
28
+ Fictional grapes: X = berries per bunch.
29
+
30
+ Keep enough height for probabilities below chip labels.
31
+ <!-- llm: end -->
@@ -0,0 +1,271 @@
1
+ // m-mapping: outcomes → numerical values → a probability distribution.
2
+ egatariParts.define({name: "mapping", version: "2.1.0", requires: {base: "1.3.0"}, element: "m-mapping"}, function(deps){
3
+ "use strict";
4
+ var base = deps.base;
5
+ var TEXT = {
6
+ ja: {outcomes: "起こりうる結果", values: function(n){ return n + " の値"; }, count: function(n){ return n + " 通り"; }, total: "確率の合計 ", variable: "確率変数 ", each: function(t){ return "どれも " + t; }},
7
+ en: {outcomes: "Outcomes", values: function(n){ return "Value of " + n; }, count: function(n){ return n + (n === 1 ? " outcome" : " outcomes"); }, total: "Total ", variable: "Random variable ", each: function(t){ return "each " + t; }}
8
+ };
9
+
10
+ // 結果の絵(picture):数えられる形で結果を描く。dots・grapes は x 個の丸(0〜15)、dice は label の目(1〜6)、coins は label の表・裏(H・T)の並び。
11
+ // 描けないときは null(字のカードに戻す)。返す値は {w, h, draw(parent, x0, cy)}
12
+ // 物の色(形式 §8.4):11 の基本色。日本語・英語の名前(1 字の日本語も)から --obj-* へ。テーマになければ同じ色の値で
13
+ var OBJ = {white: "#ffffff", black: "#222222", red: "#d62728", green: "#2ca02c", yellow: "#f2c12e", blue: "#1f77b4", brown: "#8c564b", purple: "#9467bd", pink: "#e377c2", orange: "#ff7f0e", gray: "#7f7f7f"};
14
+ var OBJ_NAME = {"白": "white", "黒": "black", "赤": "red", "緑": "green", "黄": "yellow", "青": "blue", "茶": "brown", "紫": "purple", "桃": "pink", "橙": "orange", "灰": "gray",
15
+ "白色": "white", "黒色": "black", "赤色": "red", "緑色": "green", "黄色": "yellow", "青色": "blue", "茶色": "brown", "紫色": "purple", "桃色": "pink", "橙色": "orange", "灰色": "gray", "ピンク": "pink", "オレンジ": "orange", "グレー": "gray", "grey": "gray"};
16
+ function objColor(name){
17
+ var k = String(name).trim(), en = OBJ[k.toLowerCase()] ? k.toLowerCase() : OBJ_NAME[k] || OBJ_NAME[k.toLowerCase()];
18
+ return en && OBJ[en] ? "var(--obj-" + en + ", " + OBJ[en] + ")" : null;
19
+ }
20
+ function pictureOf(kind, o){
21
+ var n = o.x, R = 7, G = 3;
22
+ var circle = function(par, cx, cy, filled, r){ base.el("circle", {cx: cx.toFixed(1), cy: cy.toFixed(1), r: r || R, fill: filled ? "var(--fg)" : "var(--bg)", stroke: "var(--fg)", "stroke-width": 1.5}, par); };
23
+ if (kind === "dots" || kind === "grapes"){
24
+ if (!(Number.isInteger(n) && n >= 0 && n <= 15)) return null;
25
+ if (kind === "dots"){
26
+ var per = n > 8 ? Math.ceil(n / 2) : n, rows = n > 8 ? 2 : 1, w = Math.max(1, per) * (2 * R + G) - G;
27
+ return {w: w, h: rows * (2 * R + G) - G, draw: function(par, x0, cy){ for (var i = 0; i < n; i++){ var r = Math.floor(i / per), c = i % per; circle(par, x0 + R + c * (2 * R + G), cy - (rows - 1) * (R + G / 2) + r * (2 * R + G), true); } }};
28
+ }
29
+ // ぶどう:逆三角形の房(上の段が k 粒、1 段ごとに 1 粒へる。k は k(k+1)/2 ≥ n の最小)。上に軸
30
+ R = 10; // 房の粒は数えやすい大きさに
31
+ var kk = 1; while (kk * (kk + 1) / 2 < n) kk++;
32
+ var rowsG = [], left = n; for (var k = kk; k >= 1 && left > 0; k--){ var m = Math.min(k, left); rowsG.push(m); left -= m; }
33
+ var wG = Math.max(1, kk) * (2 * R + 1), hG = 10 + rowsG.length * (2 * R - 1);
34
+ return {w: wG, h: hG, draw: function(par, x0, cy){
35
+ var top = cy - hG / 2 + 10, mid = x0 + wG / 2;
36
+ base.el("path", {d: "M" + mid.toFixed(1) + "," + (top - 1).toFixed(1) + "l0,-8m0,4l7,-3", fill: "none", stroke: "var(--fg)", "stroke-width": 2, "stroke-linecap": "round"}, par);
37
+ rowsG.forEach(function(m, r){ for (var c = 0; c < m; c++) circle(par, mid + (c - (m - 1) / 2) * (2 * R + 1), top + R + r * (2 * R - 1), true); });
38
+ }};
39
+ }
40
+ // dice:結果の目(label の数字 1〜6。X とは別でよい:「5 以上なら 1」など)
41
+ if (kind === "dice"){
42
+ n = /^\s*[1-6]\s*$/.test(String(o.label)) ? +o.label : NaN;
43
+ if (!(Number.isInteger(n) && n >= 1 && n <= 6)) return null;
44
+ var S = 40, P = {1: [[0, 0]], 2: [[-1, -1], [1, 1]], 3: [[-1, -1], [0, 0], [1, 1]], 4: [[-1, -1], [1, -1], [-1, 1], [1, 1]], 5: [[-1, -1], [1, -1], [0, 0], [-1, 1], [1, 1]], 6: [[-1, -1], [1, -1], [-1, 0], [1, 0], [-1, 1], [1, 1]]}[n];
45
+ return {w: S, h: S, draw: function(par, x0, cy){
46
+ base.el("rect", {x: x0, y: (cy - S / 2).toFixed(1), width: S, height: S, rx: 6, fill: "var(--bg)", stroke: "var(--fg)", "stroke-width": 2}, par);
47
+ P.forEach(function(q){ circle(par, x0 + S / 2 + q[0] * 11, cy + q[1] * 11, true, 4); });
48
+ }};
49
+ }
50
+ // coins:表 = ◯(白)、裏 = ●(塗り)。見出しに「◯ 表・● 裏」と書く
51
+ if (kind === "coins"){
52
+ var seq = String(o.label).split("").filter(function(ch){ return /\S/.test(ch); });
53
+ if (!seq.length || seq.length > 6 || seq.some(function(ch){ return !/[表裏HT]/.test(ch); })) return null;
54
+ var Rc = 13, wC = seq.length * (2 * Rc + 6) - 6;
55
+ return {w: wC, h: 2 * Rc, draw: function(par, x0, cy){ seq.forEach(function(ch, i){ circle(par, x0 + Rc + i * (2 * Rc + 6), cy, ch === "裏" || ch === "T", Rc); }); }};
56
+ }
57
+ // balls:玉の色は物の色(形式 §8.4 Object colours、--obj-*。テーマになければ同じ色の字の値)。色は中身そのものなので、
58
+ // 名前を玉の横に必ず書く(label。色だけに頼らない)。玉の並びは outcome の balls(色の名前の列、日本語か英語)か、
59
+ // なければ label の 1 字ずつ(赤・青 …)。知らない名前は描かずに知らせる
60
+ if (kind === "balls"){
61
+ // label を 1 字ずつ読むのは、どの字も 1 字の色(白黒赤緑黄青茶紫桃橙灰)で、隣り合う 2 字が合わせた色(黄緑・青緑・赤紫・青紫・赤茶)に
62
+ // ならないときだけ。「黄緑」「水色」「茶色」などは推し量らずに描かず、balls の列で書くよう知らせる
63
+ var names = Array.isArray(o.balls) ? o.balls.map(String) : String(o.label).split("").filter(function(ch){ return /\S/.test(ch); });
64
+ if (!Array.isArray(o.balls)){
65
+ if (names.some(function(ch){ return "白黒赤緑黄青茶紫桃橙灰".indexOf(ch) < 0; })) return null;
66
+ for (var q = 1; q < names.length; q++) if (["黄緑", "青緑", "赤紫", "青紫", "赤茶"].indexOf(names[q - 1] + names[q]) >= 0) return null;
67
+ }
68
+ var cols = names.map(objColor);
69
+ if (!names.length || names.length > 6 || cols.some(function(c){ return !c; })) return null;
70
+ var Rb = 13, wB = names.length * (2 * Rb + 6) - 6;
71
+ return {w: wB, h: 2 * Rb, draw: function(par, x0, cy){ cols.forEach(function(c, i){
72
+ base.el("circle", {cx: (x0 + Rb + i * (2 * Rb + 6)).toFixed(1), cy: cy.toFixed(1), r: Rb, fill: c, stroke: "var(--fg)", "stroke-width": 1.5}, par);
73
+ }); }};
74
+ }
75
+ return null;
76
+ }
77
+ function set(e, a, v){ v = String(v); if (e.getAttribute(a) !== v) e.setAttribute(a, v); }
78
+ function gcd(a, b){ while (b){ var r = a % b; a = b; b = r; } return a; }
79
+ // Only use a fraction when it represents the supplied value to the sum tolerance.
80
+ function fraction(v){
81
+ for (var d = 1; d <= 10000; d++){
82
+ var n = Math.round(v * d);
83
+ if (Number.isSafeInteger(n) && Math.abs(n / d - v) <= 1e-9){ var g = gcd(Math.abs(n), d); return d / g === 1 ? String(n / g) : n / g + "/" + d / g; }
84
+ }
85
+ return null;
86
+ }
87
+ return class extends base.MotionElement {
88
+ static partName = "mapping";
89
+ static preview = "none";
90
+ static steps = 4;
91
+ static shape = {
92
+ schema: {type: "object", additionalProperties: false, required: ["outcomes"], properties: {
93
+ outcomes: {type: "array", minItems: 2, maxItems: 12, items: {type: "object", additionalProperties: false, required: ["label", "x"], properties: {
94
+ label: {type: "string"}, x: {type: "number"}, p: {type: "number"}, balls: {type: "array", items: {type: "string"}, maxItems: 6}
95
+ }}},
96
+ name: {type: "string"}, unit: {type: "string"}, fractions: {type: "boolean"}, picture: {enum: ["dots", "dice", "coins", "grapes", "balls"]}
97
+ }},
98
+ example: {outcomes: [{label: "表表", x: 2}, {label: "表裏", x: 1}, {label: "裏表", x: 1}, {label: "裏裏", x: 0}], name: "X", unit: "", fractions: true}
99
+ };
100
+ static props = {
101
+ dur: {type: "number", default: 1, doc: "各ステップの動きの秒数(矢印は stagger のずれを足す)"},
102
+ stagger: {type: "number", default: 0.15, doc: "ステップ 2 の矢印ごとの開始のずれ(秒。0 以上)"},
103
+ digits: {type: "number", default: 2, doc: "分数でない確率の小数桁(0〜10 の整数)"},
104
+ size: {type: "number", default: 0, doc: "文字の大きさ(px。0 は既定の 26、指定時は 24 以上)"}
105
+ };
106
+ plan(){
107
+ if (this._plan) return this._plan;
108
+ var d = this.data, given = d.outcomes.filter(function(o){ return o.p !== undefined; }).length;
109
+ var p = {name: d.name === undefined ? "X" : d.name, unit: d.unit || "", notes: [], groups: []};
110
+ p.outcomes = d.outcomes.map(function(o){ return {label: o.label, x: o.x, balls: o.balls, p: given ? (o.p === undefined ? 0 : o.p) : 1 / d.outcomes.length}; });
111
+ p.total = p.outcomes.reduce(function(s, o){ return s + o.p; }, 0);
112
+ p.validTotal = Math.abs(p.total - 1) <= 1e-9;
113
+ if (given && given !== d.outcomes.length) p.notes.push("mixed p: missing probabilities are treated as 0");
114
+ if (!p.validTotal) p.notes.push("probability sum is " + p.total + ", expected 1 (not normalised)");
115
+ if (p.outcomes.some(function(o){ return o.p < 0 || o.p > 1; })) p.notes.push("p must be between 0 and 1; drawing the supplied values");
116
+ p.equal = p.validTotal && p.outcomes.every(function(o){ return Math.abs(o.p - 1 / d.outcomes.length) <= 1e-9; });
117
+ p.fractions = d.fractions === undefined ? p.equal : d.fractions;
118
+ // どれも同じ確率なら、結果ごとには書かず、見出しに「どれも 1/n」と 1 度だけ書く
119
+ p.showP = !p.equal && (given > 0 || p.fractions);
120
+ var values = Array.from(new Set(p.outcomes.map(function(o){ return o.x; }))).sort(function(a, b){ return a - b; });
121
+ p.groups = values.map(function(x, j){
122
+ var members = p.outcomes.filter(function(o){ return o.x === x; });
123
+ var g = {x: x, count: members.length, p: members.reduce(function(s, o){ return s + o.p; }, 0), tone: "series-" + (j % 4 + 1)};
124
+ members.forEach(function(o, i){ o.group = j; o.slot = i; });
125
+ return g;
126
+ });
127
+ return (this._plan = p);
128
+ }
129
+ duration(k){
130
+ var P = this.props, dur = Number.isFinite(P.dur) && P.dur > 0 ? P.dur : 1;
131
+ var stagger = Number.isFinite(P.stagger) && P.stagger >= 0 ? P.stagger : 0.15;
132
+ return k >= 1 && k <= 4 ? dur + (k === 2 ? (this.plan().outcomes.length - 1) * stagger : 0) : 0;
133
+ }
134
+ fmt(v){
135
+ var f = this.plan().fractions && fraction(v);
136
+ return f || v.toFixed(this.props.digits);
137
+ }
138
+ value(x){ return String(x) + this.plan().unit; }
139
+ accessible(){
140
+ var p = this.plan(), self = this, sep = this.T === TEXT.ja ? "、" : ", ";
141
+ return this.T.variable + p.name + ": " + p.outcomes.map(function(o){ return o.label + "→" + self.value(o.x); }).join(sep) + ". " +
142
+ p.groups.map(function(g){ return "P(" + p.name + "=" + self.value(g.x) + ")=" + self.fmt(g.p); }).join(", ") + ". " + this.totalText();
143
+ }
144
+ totalText(){
145
+ var p = this.plan();
146
+ // Do not round an invalid total to 1 just because digits is small.
147
+ return this.T.total + (p.validTotal ? "1" : String(p.total));
148
+ }
149
+ label(g, x, y, s, fs, anchor, mark){
150
+ var e = base.el("text", Object.assign({x: x, y: y, "font-size": fs || this.fs, "text-anchor": anchor || "start", fill: "var(--fg)"}, mark || {}), g);
151
+ e.textContent = s; return e;
152
+ }
153
+ mount(ctx){
154
+ var P = this.props, self = this, p = this.plan();
155
+ [["dur", 1, function(v){ return Number.isFinite(v) && v > 0; }], ["stagger", 0.15, function(v){ return Number.isFinite(v) && v >= 0; }],
156
+ ["digits", 2, function(v){ return Number.isInteger(v) && v >= 0 && v <= 10; }], ["size", 0, function(v){ return v === 0 || Number.isFinite(v) && v >= 24; }]].forEach(function(a){
157
+ if (!a[2](P[a[0]])){ ctx.report("attr", "[mapping] invalid " + a[0] + "; using " + a[1]); P[a[0]] = a[1]; }
158
+ });
159
+ p.notes.forEach(function(n){ ctx.report("data", "[mapping] " + n); });
160
+ var lang = this.closest("[lang]");
161
+ this.T = /^ja\b/i.test((lang && lang.getAttribute("lang")) || ctx.lang || "") ? TEXT.ja : TEXT.en;
162
+ this.fs = P.size || 26;
163
+ this.style.display = "block"; this.style.position = "relative"; this.style.writingMode = "horizontal-tb";
164
+ this.setAttribute("role", "img");
165
+ if (!this.getAttribute("aria-label")) this.setAttribute("aria-label", this.accessible());
166
+ var W = this.clientWidth, H = this.clientHeight, fs = this.fs, small = Math.max(24, fs - 2);
167
+ var width = function(s, f){ return base.textWidth(s, f || fs, {font: self}) + 8; };
168
+ var max = function(a){ return Math.max.apply(null, a.concat([0])); };
169
+ var omega = "Ω " + this.T.outcomes + (p.equal ? "(" + this.T.each(this.fmt(p.outcomes[0].p)) + ")" : "");
170
+ if (p.equal && this.T === TEXT.en) omega = "Ω " + this.T.outcomes + " (" + this.T.each(this.fmt(p.outcomes[0].p)) + ")";
171
+ var kind0 = (this.data || {}).picture;
172
+ if (kind0 === "coins"){
173
+ var each = p.equal ? this.fmt(p.outcomes[0].p) : "";
174
+ omega = this.T === TEXT.en ? "Ω (" + (each ? "each " + each + "; " : "") + "◯ H ● T)" : "Ω(" + (each ? "どれも " + each + "・" : "") + "◯表 ●裏)";
175
+ }
176
+ // 結果の絵(picture)。描けない結果があれば知らせて、その結果は字だけ
177
+ var kind = (this.data || {}).picture, badPic = [];
178
+ var pics = p.outcomes.map(function(o){ if (!kind) return null; var q = pictureOf(kind, o); if (!q) badPic.push(o.label); return q; });
179
+ if (badPic.length) ctx.report("data", "[mapping] picture=\"" + kind + "\" cannot draw " + badPic.join(", ") + " (" + {dots: "x must be an integer 0–15", grapes: "x must be an integer 0–15", dice: "label must be a die face 1–6", coins: "label must be 1–6 of 表/裏 or H/T", balls: "1–6 balls named by colour: single-kanji colours in the label (赤青), or a balls array such as [\"黄緑\", \"青\"] for compound or other names (see --obj-* in spec §8.4)"}[kind] + "); shown as text");
180
+ // coins・balls は丸の中に字を書くので、名前は並べない
181
+ // coins は絵だけ(見出しに ◯表 ●裏)。balls は色の名前を横に書く(色だけに頼らない。形式 §8.4)
182
+ var labelOf = function(o, i){ return kind === "coins" && pics[i] ? "" : o.label; };
183
+ var picW = max(pics.map(function(q){ return q ? q.w : 0; })), picH = max(pics.map(function(q){ return q ? q.h : 0; }));
184
+ var cw = Math.max(width(omega), max(p.outcomes.map(function(o, i){ var lw = labelOf(o, i) ? width(o.label) : 0; return (picW ? picW + (lw ? 12 : 0) : 0) + Math.max(lw, p.showP ? width(self.fmt(o.p), small) : 0); })) + 24);
185
+ var vw = max(p.groups.map(function(g){ return width(self.value(g.x)); }));
186
+ var sw = max(p.groups.map(function(g){ return Math.max(width(self.T.count(g.count), small), width("= " + self.fmt(g.p), small)); }));
187
+ var bw = vw + sw + 44, bx = Math.max(cw + 160, W * 0.43), barX = bx + bw + 30;
188
+ var probW = max(p.groups.map(function(g){ return width(self.fmt(g.p), small); }));
189
+ var negativePad = p.groups.some(function(g){ return g.p < 0; }) ? probW + 12 : 0;
190
+ var barW = Math.max(0, W - barX - negativePad - probW - 24);
191
+ // Source Sans 3's SVG text box extends 1.024em above and 0.4em below the baseline.
192
+ // Reserve those full boxes, a 4px line gap and space inside/outside the chip frame.
193
+ var rowMin = Math.max(p.showP ? (fs + small) * 1.43 + 12 : Math.max(40, fs * 1.43 + 12), picH + 16);
194
+ var boxH = Math.max(40, (fs + small) * 1.43 + 12), top = fs * 1.43 + 16, bottom = H - fs * 1.43 - 8;
195
+ var row = Math.max(rowMin, (bottom - top) / p.outcomes.length);
196
+ if (bottom - top < p.outcomes.length * rowMin || barW < 100 || width(this.T.values(p.name)) > bw || width("P(" + p.name + " = x)") > barW + probW || width(this.totalText()) > W - barX){
197
+ ctx.report("size", "[mapping] labels or outcome rows do not fit; enlarge the element (each row needs at least " + Math.ceil(rowMin) + "px) or shorten labels");
198
+ }
199
+ this.svg = base.el("svg", {viewBox: "0 0 " + W + " " + H, width: W, height: H, "aria-hidden": "true", style: "display:block"}, this);
200
+ this.outcomeLayer = base.el("g", {opacity: 0}, this.svg);
201
+ this.label(this.outcomeLayer, 8, fs + 3, omega);
202
+ this.valueLayer = base.el("g", {opacity: 0}, this.svg);
203
+ this.label(this.valueLayer, bx, fs + 3, this.T.values(p.name));
204
+ var first = top + boxH / 2, last = Math.max(first, bottom - boxH / 2);
205
+ // True numerical axis, increasing upwards. Scale first to avoid overflow of max-min.
206
+ var scale = Math.max(1, max(p.groups.map(function(g){ return Math.abs(g.x); }))), lo = p.groups[0].x / scale, hi = p.groups[p.groups.length - 1].x / scale;
207
+ var ys = p.groups.map(function(g){ return hi === lo ? (first + last) / 2 : base.lerp(last, first, (g.x / scale - lo) / (hi - lo)); });
208
+ if (ys.some(function(y, i){ return i && ys[i - 1] - y < boxH + 8; })) ctx.report("size", "[mapping] value boxes are too close on the numerical axis; enlarge the element or use fewer/less closely spaced values");
209
+ // 値の箱は数の大きさの順に上へ(大きい値が上)。縦の軸の線は描かない(矢印の先と紛れるため)
210
+ this.distribution = base.el("g", {opacity: 0}, this.svg);
211
+ this.label(this.distribution, barX, fs + 3, "P(" + p.name + " = x)");
212
+ this.label(this.distribution, barX, H - fs * 0.4 - 2, this.totalText());
213
+ var minP = Math.min.apply(null, p.groups.map(function(g){ return g.p; }).concat([0]));
214
+ var maxP = Math.max.apply(null, p.groups.map(function(g){ return g.p; }).concat([1]));
215
+ var zero = barX + negativePad + barW * (-minP / (maxP - minP));
216
+ this.chips = []; this.boxes = []; this.arrows = []; this.bars = [];
217
+ // data-part:Studio の形の検査(R-geometry)が測る印。結果の札(o1, o2 …)と値の箱(v1, v2 …)、その中の字、札から箱への矢印(矢じりは除く)。
218
+ // 値の箱の右の数(何通り・確率)は箱の外に置く字。右の棒グラフは測らない
219
+ var lab = function(id, outside){ return Object.assign({"data-part": "label", "data-part-id": id, "data-part-of": "box"}, outside ? {"data-part-at": "outside"} : {}); };
220
+ p.groups.forEach(function(g, j){
221
+ var y = ys[j], box = base.el("g", {}, self.valueLayer), vid = "v" + (j + 1);
222
+ var frame = base.el("rect", {class: "box", x: bx, y: y - boxH / 2, width: bw, height: boxH, rx: 10, fill: "none", stroke: "var(--muted)", "stroke-width": 2, "data-part": "box", "data-part-id": vid}, box);
223
+ self.label(box, bx + 12, y + fs * 0.312, self.value(g.x), undefined, undefined, lab(vid));
224
+ var summary = base.el("g", {opacity: 0}, box);
225
+ var summaryY = y - boxH / 2 + 6 + small * 1.024;
226
+ self.label(summary, bx + vw + 28, summaryY, self.T.count(g.count), small, undefined, lab(vid, true));
227
+ self.label(summary, bx + vw + 28, summaryY + small * 1.424 + 4, "= " + self.fmt(g.p), small, undefined, lab(vid, true));
228
+ self.boxes.push({frame: frame, summary: summary, tone: g.tone});
229
+ var end = zero + barW * (g.p / (maxP - minP));
230
+ base.el("line", {x1: zero, x2: zero, y1: y - 20, y2: y + 20, stroke: "var(--muted)", "stroke-width": 2}, self.distribution);
231
+ var bar = base.el("rect", {x: zero, y: y - 14, width: 0, height: 28, fill: "var(--" + g.tone + ")"}, self.distribution);
232
+ var label = self.label(self.distribution, end + (g.p < 0 ? -10 : 10), y + small * 0.3, self.fmt(g.p), small, g.p < 0 ? "end" : "start");
233
+ self.bars.push({bar: bar, label: label, zero: zero, end: end});
234
+ });
235
+ p.outcomes.forEach(function(o, i){
236
+ var y = top + row * (i + 0.5), g = p.groups[o.group]; // y:行の真ん中
237
+ var chip = base.el("g", {}, self.outcomeLayer), oid = "o" + (i + 1);
238
+ var frame = base.el("rect", {class: "box", x: 8, y: y - rowMin / 2 + 2, width: cw - 8, height: rowMin - 4, rx: 10, fill: "none", stroke: "var(--term)", "stroke-width": 2, "data-part": "box", "data-part-id": oid}, chip);
239
+ var labelY = p.showP ? y - (fs + small) * 0.715 - 2 + fs * 1.024 : y + fs * 0.312, pic = pics[i], lab = labelOf(o, i), mk = function(id){ return {"data-part": "label", "data-part-id": id, "data-part-of": "box"}; };
240
+ // 絵があれば左に絵、右に名前(と確率)。なければ真ん中に名前
241
+ var tx = pic ? 20 + picW + 12 : cw / 2 + 4, anchor = pic ? "start" : "middle";
242
+ if (pic) pic.draw(chip, 20 + (picW - pic.w) / 2, y);
243
+ if (lab) self.label(chip, tx, labelY, lab, fs, anchor, mk(oid));
244
+ if (p.showP) self.label(chip, tx, (lab ? labelY + fs * 0.4 + small * 1.024 + 4 : y + small * 0.312), self.fmt(o.p), small, anchor, mk(oid));
245
+ self.chips.push({frame: frame, tone: g.tone});
246
+ var eg = base.el("g", {"data-part": "edge", "data-part-id": oid + "-v" + (o.group + 1), "data-part-from": oid, "data-part-to": "v" + (o.group + 1)}, self.svg);
247
+ var path = base.el("path", {fill: "none", stroke: "var(--muted)", "stroke-width": 2, pathLength: 1, "stroke-dasharray": "1", opacity: 0, "data-part-line": ""}, eg);
248
+ var head = base.el("path", {fill: "none", stroke: "var(--muted)", "stroke-width": 2, opacity: 0}, eg);
249
+ self.arrows.push({path: path, head: head, x: cw + 4, y: y, endX: bx - 5, endY: ys[o.group], spread: (o.slot - (g.count - 1) / 2) * Math.min(6, (boxH - 24) / g.count), tone: g.tone});
250
+ });
251
+ }
252
+ render(k, t){
253
+ if (!this.svg) return;
254
+ var P = this.props;
255
+ var progress = function(step, offset){ return k > step ? 1 : k < step ? 0 : base.prog(t, offset || 0, P.dur); };
256
+ var bundle = progress(3), bars = progress(4);
257
+ set(this.outcomeLayer, "opacity", progress(1)); set(this.valueLayer, "opacity", progress(2)); set(this.distribution, "opacity", bars);
258
+ this.chips.forEach(function(c){ set(c.frame, "style", "stroke:var(--" + (bundle > 0 ? c.tone : "term") + ")"); });
259
+ this.boxes.forEach(function(b){ set(b.frame, "style", "stroke:var(--" + (bundle > 0 ? b.tone : "muted") + ")"); set(b.summary, "opacity", bundle); });
260
+ this.arrows.forEach(function(a, i){
261
+ var e = progress(2, i * P.stagger), endY = a.endY + a.spread * bundle;
262
+ var bend = base.lerp((a.endX - a.x) * 0.35, 32, bundle);
263
+ set(a.path, "d", "M" + a.x + "," + a.y + "C" + (a.x + bend) + "," + a.y + " " + (a.endX - bend) + "," + endY + " " + a.endX + "," + endY);
264
+ set(a.path, "stroke-dashoffset", 1 - e); set(a.path, "opacity", e > 0 ? 1 : 0);
265
+ set(a.head, "d", "M" + (a.endX - 8) + "," + (endY - 5) + "L" + a.endX + "," + endY + "l-8,5"); set(a.head, "opacity", e === 1 ? 1 : 0);
266
+ [a.path, a.head].forEach(function(n){ set(n, "style", "stroke:var(--" + (bundle > 0 ? a.tone : "muted") + ")"); });
267
+ });
268
+ this.bars.forEach(function(b){ var end = base.lerp(b.zero, b.end, bars); set(b.bar, "x", Math.min(b.zero, end)); set(b.bar, "width", Math.abs(end - b.zero)); set(b.label, "opacity", bars === 1 ? 1 : 0); });
269
+ }
270
+ };
271
+ });