egatari 0.6.17 → 0.6.19
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/CHANGELOG.md +68 -1
- package/README.ja.md +14 -13
- package/README.md +2 -1
- package/components.lock.json +153 -54
- package/components2.lock.json +1001 -1
- package/kit/build.mjs +1 -1
- package/kit/check-messages.mjs +9 -0
- package/kit/check.mjs +8 -0
- package/kit/core/bookmark.mjs +28 -0
- package/kit/core/build-core.mjs +5 -3
- package/kit/core/check-parity.mjs +6 -0
- package/kit/core/check-rules.mjs +4 -3
- package/kit/core/check-source.mjs +10 -1
- package/kit/core/checks.mjs +79 -0
- package/kit/core/find-replace.mjs +170 -0
- package/kit/core/format.mjs +4 -1
- package/kit/core/image-insert.mjs +197 -0
- package/kit/core/image-size.mjs +1 -1
- package/kit/core/layout-scan.mjs +1 -1
- package/kit/core/llm-fix.mjs +1 -1
- package/kit/core/llm-pack-assemble.mjs +40 -67
- package/kit/core/llm-pack-data.mjs +882 -52
- package/kit/core/llm-pack.mjs +6 -4
- package/kit/core/llm-request.mjs +2 -2
- package/kit/core/new-deck-pack.mjs +38 -44
- package/kit/core/new-deck.mjs +82 -74
- package/kit/core/part-labels.mjs +3 -0
- package/kit/core/portable.mjs +5 -2
- package/kit/core/purposes.mjs +9 -3
- package/kit/core/sample-info.mjs +17 -3
- package/kit/core/scorm.mjs +30 -6
- package/kit/core/script-unclosed.mjs +49 -0
- package/kit/core/slide-ops.mjs +27 -1
- package/kit/core/tate-mix.mjs +25 -0
- package/kit/core/template-text.mjs +2 -0
- package/kit/core/theme-derive.mjs +56 -17
- package/kit/core/theme-shapes.mjs +15 -1
- package/kit/deck.js +124 -6
- package/kit/instruction-version.json +1 -1
- package/kit/layouts.css +8 -0
- package/kit/llm-pack.mjs +5 -7
- package/kit/panes.js +17 -1
- package/kit/player.json +1 -1
- package/kit/player.lock.json +2 -2
- package/kit/presenter.js +36 -4
- package/kit/tools.js +54 -2
- package/kit/track.js +46 -5
- package/package.json +1 -1
- package/server/editor-ui.js +21 -1
- package/vendor/components/CATALOG.md +25 -22
- package/vendor/components/sha256/038f9d32c89c8212ab25d6e3180023b6fe024a53053ab2ef1443ecab92e3ce43/index.js +205 -0
- package/vendor/components/sha256/07479cb6e5194a01b2a8a9029f3646060f0dbb673f2737d2132738af2eb379e5/README.en.md +44 -0
- package/vendor/components/sha256/07479cb6e5194a01b2a8a9029f3646060f0dbb673f2737d2132738af2eb379e5/index.js +306 -0
- package/vendor/components/sha256/077678d8b14a8065c4f74a9fe1669728c68a0f763bfdca18b304f5c3d863632a/README.en.md +54 -0
- package/vendor/components/sha256/077678d8b14a8065c4f74a9fe1669728c68a0f763bfdca18b304f5c3d863632a/index.js +470 -0
- package/vendor/components/sha256/0840322bf51eeb3c8188ce21ad70af17c13974ac0151065a39c430a3813ee07f/README.en.md +23 -0
- package/vendor/components/sha256/0840322bf51eeb3c8188ce21ad70af17c13974ac0151065a39c430a3813ee07f/index.js +289 -0
- package/vendor/components/sha256/0c25d49d23529879054737aa181951519fe585c4728fd505e0e3c8869953eac7/index.js +934 -0
- package/vendor/components/sha256/0dd302b82925f35743a893d5c271898ada3659aa0de2d3249ef45936aad10ae0/LICENSE-strokesvg.txt +115 -0
- package/vendor/components/sha256/0dd302b82925f35743a893d5c271898ada3659aa0de2d3249ef45936aad10ae0/OFL.txt +93 -0
- package/vendor/components/sha256/0dd302b82925f35743a893d5c271898ada3659aa0de2d3249ef45936aad10ae0/index.js +755 -0
- package/vendor/components/sha256/0dd302b82925f35743a893d5c271898ada3659aa0de2d3249ef45936aad10ae0/strokes-CHANGES.md +20 -0
- package/vendor/components/sha256/0e9e92df8d70eb6d9fcf10916c361769c7cd37baacece8d7eb96bbbc7fbcf942/README.en.md +25 -0
- package/vendor/components/sha256/0e9e92df8d70eb6d9fcf10916c361769c7cd37baacece8d7eb96bbbc7fbcf942/index.js +317 -0
- package/vendor/components/sha256/12ef6a11055407f5cddc40154a49cf2737b0786429fe0fcc94ee1d8245597b40/index.js +244 -0
- package/vendor/components/sha256/13c9ce43396b2f5693ce430a924dae4ddcd1e8a4ad0c9b9e724ff52acb874d0c/index.js +354 -0
- package/vendor/components/sha256/15306d854d67d52ca2b1d66135f0d3296882e6b21fd85c029399b8077c8a7f9d/README.en.md +34 -0
- package/vendor/components/sha256/15306d854d67d52ca2b1d66135f0d3296882e6b21fd85c029399b8077c8a7f9d/index.js +398 -0
- package/vendor/components/sha256/1a0f21def0efbbdef64461cafa40f3883bc7f7122adf79d01f09ca4a245e5dcb/index.js +465 -0
- package/vendor/components/sha256/208175d05b37d326855fad792b10137f785e5d05d91a2cde119d0dbd69d2ed98/index.js +100 -0
- package/vendor/components/sha256/218d88998b70f0732c18d91ccc238b2d63f6b0aaa483883b83685a25468f214a/README.en.md +34 -0
- package/vendor/components/sha256/218d88998b70f0732c18d91ccc238b2d63f6b0aaa483883b83685a25468f214a/index.js +246 -0
- package/vendor/components/sha256/2579bcb46b6525902763cd15a2e08fc5ebd9a2695cdaba63438e5b47907a47e0/README.en.md +24 -0
- package/vendor/components/sha256/2579bcb46b6525902763cd15a2e08fc5ebd9a2695cdaba63438e5b47907a47e0/index.js +554 -0
- package/vendor/components/sha256/277ac772315f93737a094cd0f0842726f2435d352d9a90822aebfe0d5d7e79a1/README.en.md +23 -0
- package/vendor/components/sha256/277ac772315f93737a094cd0f0842726f2435d352d9a90822aebfe0d5d7e79a1/index.js +294 -0
- package/vendor/components/sha256/2787e410e533e4ad48b0a3c2b652579cf5797f9c51fcf32f5478212a7acef95f/README.en.md +28 -0
- package/vendor/components/sha256/2787e410e533e4ad48b0a3c2b652579cf5797f9c51fcf32f5478212a7acef95f/index.js +214 -0
- package/vendor/components/sha256/2a011338d0fc3c77e102eaa57cde2b6e3726bc279f133174b0b8c9a654263cbc/index.js +824 -0
- package/vendor/components/sha256/2fce5ab0c31dca0aa1586a2c69237870a9cce46c0bc6ab7392c27968d016bd15/index.js +75 -0
- package/vendor/components/sha256/3417efe4722f569690563d9abcea8b144db2f8fa97b83cf0118fb3a34cd4cb2b/README.en.md +23 -0
- package/vendor/components/sha256/3417efe4722f569690563d9abcea8b144db2f8fa97b83cf0118fb3a34cd4cb2b/index.js +150 -0
- package/vendor/components/sha256/3a7962fa87c670447eed54aca08604ef03cd26bf2c334b4667190060e6a17965/README.en.md +18 -0
- package/vendor/components/sha256/3a7962fa87c670447eed54aca08604ef03cd26bf2c334b4667190060e6a17965/index.js +279 -0
- package/vendor/components/sha256/3be5f64deb1951327b614b29a9b632878fab362687c76e3f48f3af1d1ebbb1cf/README.en.md +47 -0
- package/vendor/components/sha256/3be5f64deb1951327b614b29a9b632878fab362687c76e3f48f3af1d1ebbb1cf/index.js +223 -0
- package/vendor/components/sha256/42450c6b36404d66641568f6d6f55e42a4a036d4d9cdf308f709464913b32fe9/README.en.md +42 -0
- package/vendor/components/sha256/42450c6b36404d66641568f6d6f55e42a4a036d4d9cdf308f709464913b32fe9/index.js +272 -0
- package/vendor/components/sha256/45cea6c1abfb3467e3e3dbe7fec58647716999a0c39cf7163531ae389f898d29/README.en.md +30 -0
- package/vendor/components/sha256/45cea6c1abfb3467e3e3dbe7fec58647716999a0c39cf7163531ae389f898d29/index.js +305 -0
- package/vendor/components/sha256/4c8cae404d551a1bb021e8345af5aa45d4f412f13497aa84b142f11a29a365dc/index.js +124 -0
- package/vendor/components/sha256/5521b5b40a43c48c5ae5a50ce87afb3d47bb23b133b16841086d8322364cdd5e/README.en.md +23 -0
- package/vendor/components/sha256/5521b5b40a43c48c5ae5a50ce87afb3d47bb23b133b16841086d8322364cdd5e/index.js +120 -0
- package/vendor/components/sha256/5fe30fd7a84b937a1325d8af2b8e437211efae82ab46d5a433bc4ef07bcaef11/README.en.md +18 -0
- package/vendor/components/sha256/5fe30fd7a84b937a1325d8af2b8e437211efae82ab46d5a433bc4ef07bcaef11/index.js +243 -0
- package/vendor/components/sha256/631f4c4b90c9a41c0f26c9600d5cf2d6447251d04a20f08f48a85aeb4cb0f6fb/index.js +107 -0
- package/vendor/components/sha256/6749f44ed6476a05d4e3026373c188a0e1a19b77a5836579813f62a1477ac35e/README.en.md +31 -0
- package/vendor/components/sha256/6749f44ed6476a05d4e3026373c188a0e1a19b77a5836579813f62a1477ac35e/index.js +271 -0
- package/vendor/components/sha256/701f1deef79fe05eeb18e1119e5a5c4d60a9dfe265e93a4dff79223bc598dbf0/index.js +521 -0
- package/vendor/components/sha256/711cf58fa946c9c0abc953b2f695d02bf5a5a7f73851b09f834e894baf88be38/index.js +122 -0
- package/vendor/components/sha256/728576c57a5ccb39531ad4cb8074fce95480f01603a77dbf57dca0e52199f7c4/index.js +37 -0
- package/vendor/components/sha256/73edc74a2dc4a63b717106c1c1572967b6d306270574e9635d032de16c8ada72/README.en.md +18 -0
- package/vendor/components/sha256/73edc74a2dc4a63b717106c1c1572967b6d306270574e9635d032de16c8ada72/index.js +382 -0
- package/vendor/components/sha256/76bdb63c3e5e14fbb4816bc9f235490817bb5d7be990a826f5077aa08226702c/index.js +162 -0
- package/vendor/components/sha256/7a5adf73fc053b9f2ca56643b5683b42589edb0f63c845ecb7ae7e6b87eccc30/README.en.md +51 -0
- package/vendor/components/sha256/7a5adf73fc053b9f2ca56643b5683b42589edb0f63c845ecb7ae7e6b87eccc30/index.js +230 -0
- package/vendor/components/sha256/7ab483e7fb05f646b062340d70f2749d070b4f64fed7acc0b8a7754d9ef9fded/README.en.md +26 -0
- package/vendor/components/sha256/7ab483e7fb05f646b062340d70f2749d070b4f64fed7acc0b8a7754d9ef9fded/index.js +322 -0
- package/vendor/components/sha256/7af6e5e6dffb618b39f27d883c691ca128f3af06040ee505e75c79c678bfdcfe/README.en.md +18 -0
- package/vendor/components/sha256/7af6e5e6dffb618b39f27d883c691ca128f3af06040ee505e75c79c678bfdcfe/index.js +103 -0
- package/vendor/components/sha256/7c7b0373cfd38fd5ecb4169021e0b908f0a17be1a29b14f85dd89fdb92e7bf81/README.en.md +44 -0
- package/vendor/components/sha256/7c7b0373cfd38fd5ecb4169021e0b908f0a17be1a29b14f85dd89fdb92e7bf81/index.js +306 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/ABCJS-ADAPTER.md +101 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/LICENSE-abcjs.txt +21 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/chords.json +89 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/doremi.json +261 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/extras.json +192 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/kaeru.json +521 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/rhythm.json +248 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/fixtures/ties.json +360 -0
- package/vendor/components/sha256/7dea2c3728b85122b04515aa06ab198657529e7cd101b9ab34d4ecb2905ce94e/index.js +508 -0
- package/vendor/components/sha256/832556ea9c82c292a3ba9e1b332f2405c5c2f969ff8157c91ed80226ec8ac62e/README.en.md +22 -0
- package/vendor/components/sha256/832556ea9c82c292a3ba9e1b332f2405c5c2f969ff8157c91ed80226ec8ac62e/index.js +145 -0
- package/vendor/components/sha256/8a4b6dce84d7a69aaafeb75c1c572fe7904fd49e3169c1c85851ccaeae2a7b11/index.js +41 -0
- package/vendor/components/sha256/8bb511cb90902bc7721e59ca08685b26adbf33539e36a4a45172485d6d3d455b/index.js +250 -0
- package/vendor/components/sha256/8db9c936c36b79b9e3b2be37639487b15502a6937f411ed714621a8091437e9b/README.en.md +21 -0
- package/vendor/components/sha256/8db9c936c36b79b9e3b2be37639487b15502a6937f411ed714621a8091437e9b/index.js +108 -0
- package/vendor/components/sha256/8e5e90c90cb6b06a790b0420e43d79d7da663f1cc19ff06d381ec849b4a8fc57/index.js +312 -0
- package/vendor/components/sha256/939bdae590ebed3d182b0c9940a35b6c03872505bfa242341990a81cae02fd06/README.en.md +34 -0
- package/vendor/components/sha256/939bdae590ebed3d182b0c9940a35b6c03872505bfa242341990a81cae02fd06/index.js +397 -0
- package/vendor/components/sha256/9c5dd0bc0c3279c5f0fea3fdb9d0735870880aef96e8bbf9ecdd77dff0f5ddc8/README.en.md +44 -0
- package/vendor/components/sha256/9c5dd0bc0c3279c5f0fea3fdb9d0735870880aef96e8bbf9ecdd77dff0f5ddc8/index.js +551 -0
- package/vendor/components/sha256/a1c2b2fa06e4229815ab82a9202c266ec887a67632a175854c7d136a92389ca3/index.js +69 -0
- package/vendor/components/sha256/a5b032657720d3beb64f0b3699954e13b0eb985f36161b9ae046c83fd6048ff5/README.en.md +18 -0
- package/vendor/components/sha256/a5b032657720d3beb64f0b3699954e13b0eb985f36161b9ae046c83fd6048ff5/index.js +382 -0
- package/vendor/components/sha256/a7c4282db79d46557bdbaf84ca519606dc46f3c23455db4a26f663453f85813c/README.en.md +38 -0
- package/vendor/components/sha256/a7c4282db79d46557bdbaf84ca519606dc46f3c23455db4a26f663453f85813c/index.js +414 -0
- package/vendor/components/sha256/aac2f8a23cb1b606e9635061cb288f006588c80a7258540cae846ae677f55ff5/index.js +238 -0
- package/vendor/components/sha256/ace7e82e866b577ad42bbc4b64e11bae18982e3236aadceb6c2e6c20f41234cf/README.en.md +45 -0
- package/vendor/components/sha256/ace7e82e866b577ad42bbc4b64e11bae18982e3236aadceb6c2e6c20f41234cf/index.js +211 -0
- package/vendor/components/sha256/aeb0b9b5991a5c1df7191c397780a699f2c7310123ca210a58486c0a23e44006/index.js +342 -0
- package/vendor/components/sha256/af3355efd6739a56c6cb53990100f41703361c475c123b9b2762421c68509036/index.js +315 -0
- package/vendor/components/sha256/b10183a66b58b827a1410cf8b0117af567647684464d5777529dc66f0cea343a/LICENSE-tabler.txt +21 -0
- package/vendor/components/sha256/b10183a66b58b827a1410cf8b0117af567647684464d5777529dc66f0cea343a/index.js +4868 -0
- package/vendor/components/sha256/b10183a66b58b827a1410cf8b0117af567647684464d5777529dc66f0cea343a/names.txt +4808 -0
- package/vendor/components/sha256/b348ea64cc19632df96a91dfd7c435e4a62095aede72b504d03537be70d6d930/index.js +606 -0
- package/vendor/components/sha256/b53bfa03d1e9feb3cb492f440bf9d100c96eb4fe6b76c42b512a56e9158829cd/index.js +351 -0
- package/vendor/components/sha256/b9443e1cdac4850b1ead9f8449a3061678bbb77fbc53eb9b23d01e1cf50750c3/index.js +619 -0
- package/vendor/components/sha256/bc1e2a02c959ac203819d03c0d25b3776a6a9a4290aae860b26cbbe3d29e876d/index.js +268 -0
- package/vendor/components/sha256/beeb180c0e2250bb04490b092c0a235cae47fca2ed1de2d9ef078a5c14b1a963/index.js +429 -0
- package/vendor/components/sha256/c5fd7713e55e96d75824a21146c0251896d7d9dd028b4d169191433135a97981/index.js +200 -0
- package/vendor/components/sha256/c61987e9994940fc668ed8a010e575ee52027b54ed9c5dd1cc9fdf67649092b3/index.js +404 -0
- package/vendor/components/sha256/ccc8bfe174b6903ec254dfef4f5d1c614271ea39963649add7fcb8d83c081eba/index.js +278 -0
- package/vendor/components/sha256/da267bc65fde0a9279127d5a82f6c253ad74984da6fb11391f00604ad9ffe80d/index.js +93 -0
- package/vendor/components/sha256/dea36cd7010991a572ef394ee32b5e17564aeff09d3ec4ed3820d7e43c9a58be/index.js +484 -0
- package/vendor/components/sha256/df3fdb2aed487d4f6e60a8abab1fe0d1e9fa2a4193fd162edf3c384ea8db5a1b/index.js +180 -0
- package/vendor/components/sha256/e27d517f39eb554c7a9ecea7b59fcf1d347c4deeb3fe2250797aa233bfb7e2b0/index.js +747 -0
- package/vendor/components/sha256/e31f6b058503f7980597c89b3e964c512ee08de7cba3fa99904d499abbb5d0e8/README.en.md +20 -0
- package/vendor/components/sha256/e31f6b058503f7980597c89b3e964c512ee08de7cba3fa99904d499abbb5d0e8/index.js +310 -0
- package/vendor/components/sha256/f4100076a37dbde6046d85969d651feffc925bd984ffe9f333ea9bd2b6816918/README.en.md +30 -0
- package/vendor/components/sha256/f4100076a37dbde6046d85969d651feffc925bd984ffe9f333ea9bd2b6816918/index.js +192 -0
- package/vendor/components/sha256/fa6442fa3c5082cf161c29308fe0ecbd85fa33e8e4bad08c12e7961b46b1365b/README.en.md +24 -0
- package/vendor/components/sha256/fa6442fa3c5082cf161c29308fe0ecbd85fa33e8e4bad08c12e7961b46b1365b/index.js +214 -0
- package/vendor/components-2/manifest-2.16.0.json +2919 -0
- package/vendor/components-2/manifest-2.17.0.json +2852 -0
- package/vendor/components-2/manifest-2.18.0.json +3182 -0
- package/vendor/components-2/manifest-2.20.0.json +3217 -0
- package/vendor/components-2/manifest-2.21.0.json +3220 -0
- package/vendor/egatari-spec/SOURCE.json +2 -2
- package/vendor/egatari-spec/schema/deck-meta.schema.json +3 -0
- package/vendor/egatari-spec/schema/egatari-metadata.schema.json +6 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
// m-trace:コードの実行を 1 行ずつ追う。左にコード(今の行に印)、右に変数(変わった変数に印)と、その時点までの出力。
|
|
2
|
+
// 値はトレーサー(tools/trace.py、sys.settrace)が書いた JSON だけを描く。パーツはコードを実行しない。
|
|
3
|
+
// <m-trace style="height:440px"><script type="application/json">
|
|
4
|
+
// {"lang": "python", "code": "xs = [3, 1, 2]\ntotal = 0\nfor x in xs:\n total += x\nprint(total)",
|
|
5
|
+
// "source": {"tool": "egatari-trace 0.1", "python": "3.13.1", "sha256": "…", "truncated": false},
|
|
6
|
+
// "steps": [{"line": 1, "vars": {"xs": [3, 1, 2]}}, …, {"line": 5, "vars": {…}, "out": "6\n"}]}
|
|
7
|
+
// (JSON の終わり)</m-trace>
|
|
8
|
+
// steps[i]:line(または lines: [a, b])、vars(その行を実行した後の、見せる変数すべての値。差分ではない)、out(その行の出力)、note。
|
|
9
|
+
// ステップ:k = 0 はコードと空の変数、k = i + 1 はトレースの i 番目(by="time" なら 1 つのステップの中で rate 個/秒で進む)。
|
|
10
|
+
// 色:今の行 --emph の地と行番号、変わった変数 --emph の字と地と太字(色だけに頼らない)。
|
|
11
|
+
egatariParts.define({name: "trace", version: "2.0.1", requires: {base: "1.3.0", "code-core": "1.0.1"}, element: "m-trace"}, function(deps){
|
|
12
|
+
"use strict";
|
|
13
|
+
var base = deps.base, C = deps["code-core"], MONO = C.MONO, set = C.set, monoWidth = C.monoWidth, cut = C.cut;
|
|
14
|
+
var CAP = {lines: 16, steps: 60, show: 6, str: 40, list: 12, grid: 6, dict: 8, out: 8};
|
|
15
|
+
var TEXT = {
|
|
16
|
+
ja: {vars: "変数", output: "出力", readable: "コードと出力", final: "最後の変数", name: "変数", value: "値", more: function(n){ return "⋮(ほか " + n + " 行)"; }, older: function(n){ return "…(前の " + n + " 行)"; },
|
|
17
|
+
line: function(a, b){ return (a === b ? a : a + "–" + b) + " 行目"; }, summary: function(lang, n, m){ return lang + " のコード " + n + " 行の実行を " + m + " ステップで追う"; }},
|
|
18
|
+
en: {vars: "Variables", output: "Output", readable: "Code and output", final: "Final variables", name: "Variable", value: "Value", more: function(n){ return "⋮ (" + n + " more line" + (n === 1 ? "" : "s") + ")"; }, older: function(n){ return "… (" + n + " earlier)"; },
|
|
19
|
+
line: function(a, b){ return a === b ? "line " + a : "lines " + a + "–" + b; }, summary: function(lang, n, m){ return lang + ": " + n + " lines of code traced in " + m + " steps"; }}
|
|
20
|
+
};
|
|
21
|
+
// 値の文法:数・文字(40 字まで)・真偽・null、リスト(12 個まで、2 次元なら 6 × 6 まで)、辞書(8 個まで)、入れ子は 2 段まで、ほかは {"repr": "…"}
|
|
22
|
+
function grammar(v, depth){
|
|
23
|
+
if (v === null || typeof v === "boolean") return "";
|
|
24
|
+
if (typeof v === "number") return isFinite(v) ? "" : "a number that is not finite";
|
|
25
|
+
if (typeof v === "string") return [...v].length <= CAP.str ? "" : "a string over " + CAP.str + " characters (write {\"repr\": …})";
|
|
26
|
+
if (depth >= 2) return "values nested deeper than 2 levels (write {\"repr\": …})";
|
|
27
|
+
if (Array.isArray(v)){
|
|
28
|
+
if (v.length && v.every(Array.isArray)){ if (v.length > CAP.grid || v.some(function(r){ return r.length > CAP.grid; })) return "a 2-D list over " + CAP.grid + " × " + CAP.grid; }
|
|
29
|
+
else if (v.length > CAP.list) return "a list over " + CAP.list + " items";
|
|
30
|
+
for (var i = 0; i < v.length; i++){ var e = grammar(v[i], depth + 1); if (e) return e; }
|
|
31
|
+
return "";
|
|
32
|
+
}
|
|
33
|
+
if (typeof v === "object"){
|
|
34
|
+
var ks = Object.keys(v);
|
|
35
|
+
if ("repr" in v) return ks.length === 1 && typeof v.repr === "string" ? "" : "{\"repr\"} takes one string and nothing else";
|
|
36
|
+
if (ks.length > CAP.dict) return "a dict over " + CAP.dict + " keys";
|
|
37
|
+
for (var j = 0; j < ks.length; j++){ var e2 = grammar(v[ks[j]], depth + 1); if (e2) return e2; }
|
|
38
|
+
return "";
|
|
39
|
+
}
|
|
40
|
+
return "an unknown value";
|
|
41
|
+
}
|
|
42
|
+
// 値の字(Python なら Python の書き方:'a'・True・None)。2 次元のリストは 1 行ずつ
|
|
43
|
+
function fmt(v, py){
|
|
44
|
+
if (v === null) return py ? "None" : "null";
|
|
45
|
+
if (typeof v === "boolean") return py ? (v ? "True" : "False") : String(v);
|
|
46
|
+
if (typeof v === "number") return String(v);
|
|
47
|
+
if (typeof v === "string") return py ? "'" + v.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\n/g, "\\n") + "'" : JSON.stringify(v);
|
|
48
|
+
if (Array.isArray(v)) return "[" + v.map(function(x){ return fmt(x, py); }).join(", ") + "]";
|
|
49
|
+
if (v && typeof v === "object"){
|
|
50
|
+
if ("repr" in v) return String(v.repr);
|
|
51
|
+
return "{" + Object.keys(v).map(function(k){ return fmt(k, py) + ": " + fmt(v[k], py); }).join(", ") + "}";
|
|
52
|
+
}
|
|
53
|
+
return String(v);
|
|
54
|
+
}
|
|
55
|
+
function rowsOf(v, py){
|
|
56
|
+
if (Array.isArray(v) && v.length > 1 && v.every(Array.isArray)) return v.map(function(r, i){ return (i ? " " : "[") + fmt(r, py) + (i < v.length - 1 ? "," : "]"); });
|
|
57
|
+
return [fmt(v, py)];
|
|
58
|
+
}
|
|
59
|
+
function outLines(s){ var L = s.replace(/\r\n?/g, "\n").split("\n"); if (L[L.length - 1] === "") L.pop(); return L; }
|
|
60
|
+
return class extends base.MotionElement {
|
|
61
|
+
static partName = "trace";
|
|
62
|
+
static preview = "none";
|
|
63
|
+
static shape = {schema: {type: "object", additionalProperties: false, required: ["code", "steps"], properties: {
|
|
64
|
+
lang: {type: "string"}, code: {type: "string"},
|
|
65
|
+
source: {type: "object", additionalProperties: false, properties: {tool: {type: "string"}, python: {type: "string"}, sha256: {type: "string"}, truncated: {type: "boolean"}}},
|
|
66
|
+
show: {type: "array", minItems: 1, maxItems: 6, items: {type: "string"}},
|
|
67
|
+
steps: {type: "array", minItems: 1, maxItems: 60, items: {type: "object", additionalProperties: false, required: ["vars"], anyOf: [{required: ["line"]}, {required: ["lines"]}], properties: {
|
|
68
|
+
line: {type: "integer"}, lines: {type: "array", minItems: 2, maxItems: 2, items: {type: "integer"}}, vars: {type: "object"}, out: {type: "string"}, note: {type: "string"}
|
|
69
|
+
}}}
|
|
70
|
+
}}, example: /*@egatari-block example*/{"lang":"python","code":"total = 0\nfor x in [3, 1]:\n total += x\nprint(total)","source":{"tool":"egatari-trace 0.1","python":"3.13.1","sha256":"","truncated":false},"steps":[{"line":1,"vars":{"total":0}},{"line":2,"vars":{"total":0,"x":3}},{"line":3,"vars":{"total":3,"x":3}},{"line":2,"vars":{"total":3,"x":1}},{"line":3,"vars":{"total":4,"x":1}},{"line":2,"vars":{"total":4,"x":1}},{"line":4,"vars":{"total":4,"x":1},"out":"4\n"}]}/*@egatari-block-end*/};
|
|
71
|
+
static props = {
|
|
72
|
+
dur: {type: "number", default: 0.5, doc: "1 ステップの秒数"},
|
|
73
|
+
by: {type: "enum", values: ["step", "time"], default: "step", doc: "step はトレースの 1 行を 1 ステップに、time は全体を 1 ステップの中で流す"},
|
|
74
|
+
rate: {type: "number", default: 2, doc: "by=\"time\" のとき 1 秒に進む行の数"},
|
|
75
|
+
size: {type: "number", default: 0, doc: "文字 px(0 は既定)"}
|
|
76
|
+
};
|
|
77
|
+
get steps(){ var n = ((this.data || {}).steps || []).length; return this.props.by === "time" ? (n ? 1 : 0) : n; }
|
|
78
|
+
duration(k){ var n = this.data.steps.length; return this.props.by === "time" ? (k === 1 ? n / this.props.rate : 0) : (k >= 1 && k <= n ? this.props.dur : 0); }
|
|
79
|
+
cues(k){ var r = this.props.rate; return this.props.by === "time" && k === 1 ? this.data.steps.map(function(_, i){ return i / r; }) : null; }
|
|
80
|
+
label(g, x, y, s, fs, color, mono){
|
|
81
|
+
var e = base.el("text", {x: x, y: y, "font-size": fs, fill: color || "var(--fg)"}, g);
|
|
82
|
+
if (mono) e.setAttribute("style", "font-family:" + MONO + ";white-space:pre;");
|
|
83
|
+
e.textContent = s; return e;
|
|
84
|
+
}
|
|
85
|
+
mount(ctx){
|
|
86
|
+
var self = this, d = this.data, P = this.props, T = /^ja\b/i.test(ctx.lang || "") ? TEXT.ja : TEXT.en;
|
|
87
|
+
if (!(Number.isFinite(P.dur) && P.dur > 0)){ ctx.report("attr", "[trace] dur must be positive; using 0.5"); P.dur = 0.5; }
|
|
88
|
+
if (!(Number.isFinite(P.rate) && P.rate > 0)){ ctx.report("attr", "[trace] rate must be positive; using 2"); P.rate = 2; }
|
|
89
|
+
if (!(P.size === 0 || Number.isFinite(P.size) && P.size >= 24)){ ctx.report("attr", "[trace] size must be 0 or at least 24; using defaults"); P.size = 0; }
|
|
90
|
+
this.style.display = "block"; this.style.position = "relative"; this.style.writingMode = "horizontal-tb";
|
|
91
|
+
var lang = d.lang || "", py = /^python/i.test(lang), program = C.lines(d.code), marks = C.spans(program, lang.toLowerCase()), N = d.steps.length;
|
|
92
|
+
this.T = T; this.py = py;
|
|
93
|
+
// ---- データの確かめ(形は shape、ここでは欄どうしの関係と値の文法)
|
|
94
|
+
var bad = function(s){ ctx.report("data", "[trace] " + s); };
|
|
95
|
+
if (program.length > CAP.lines) bad("code has " + program.length + " lines; at most " + CAP.lines + " (trace a shorter program or one function)");
|
|
96
|
+
var st = d.steps.map(function(s, i){
|
|
97
|
+
var a = s.line !== undefined ? s.line : s.lines[0], b = s.line !== undefined ? s.line : s.lines[1];
|
|
98
|
+
if (!(a >= 1 && b >= a && b <= program.length)){ bad("steps[" + i + "]: line " + (a === b ? a : a + "–" + b) + " is outside 1–" + program.length); a = b = 0; }
|
|
99
|
+
Object.keys(s.vars).forEach(function(k){ var e = grammar(s.vars[k], 0); if (e) bad("steps[" + i + "].vars." + k + ": " + e); });
|
|
100
|
+
return {a: a, b: b, vars: s.vars, out: s.out || "", note: s.note || ""};
|
|
101
|
+
});
|
|
102
|
+
var seen = []; st.forEach(function(s){ Object.keys(s.vars).forEach(function(k){ if (seen.indexOf(k) < 0) seen.push(k); }); });
|
|
103
|
+
var names = d.show ? d.show.slice() : seen.slice(0, CAP.show);
|
|
104
|
+
if (!d.show && seen.length > CAP.show) bad(seen.length + " variables (" + seen.join(", ") + "); at most " + CAP.show + " are shown: choose them with show");
|
|
105
|
+
if (d.show) d.show.forEach(function(k){ if (seen.indexOf(k) < 0) bad("show: \"" + k + "\" is in no step's vars"); });
|
|
106
|
+
var acc = [], all = "";
|
|
107
|
+
st.forEach(function(s, i){
|
|
108
|
+
all += s.out; acc.push(outLines(all));
|
|
109
|
+
var prev = i ? st[i - 1].vars : {};
|
|
110
|
+
s.changed = names.filter(function(k){ return (k in s.vars) && JSON.stringify(s.vars[k]) !== JSON.stringify(prev[k]); });
|
|
111
|
+
});
|
|
112
|
+
var outTotal = acc.length ? acc[acc.length - 1].length : 0;
|
|
113
|
+
if (outTotal > CAP.out) bad("the output has " + outTotal + " lines; at most " + CAP.out + " (print less, or trace a shorter run)");
|
|
114
|
+
this.st = st; this.names = names; this.acc = acc;
|
|
115
|
+
// ---- 読める層:コードと出力(pre)、最後の変数(table)
|
|
116
|
+
this.setAttribute("role", "figure");
|
|
117
|
+
var langName = py ? "Python" : lang || (T === TEXT.ja ? "プログラム" : "Program");
|
|
118
|
+
if (!this.getAttribute("aria-label")) this.setAttribute("aria-label", T.summary(langName, program.length, N) + (st.some(function(s){ return s.note; }) ? "; " + st.map(function(s){ return s.note; }).filter(Boolean).join("; ") : ""));
|
|
119
|
+
var hide = "position:absolute;width:1px;height:1px;margin:-1px;padding:0;overflow:hidden;clip-path:inset(50%);border:0;";
|
|
120
|
+
var pre = document.createElement("pre"); pre.className = "sr-only"; pre.setAttribute("style", hide + "white-space:pre"); pre.setAttribute("aria-label", T.readable); pre.setAttribute("data-readable", "");
|
|
121
|
+
pre.textContent = d.code + (all ? "\n\n" + T.output + "\n" + all.replace(/\n$/, "") : ""); this.appendChild(pre);
|
|
122
|
+
var tbl = document.createElement("table"); tbl.className = "sr-only"; tbl.setAttribute("style", hide); tbl.setAttribute("data-readable", ""); tbl.setAttribute("aria-label", T.final);
|
|
123
|
+
var last = st[N - 1].vars, head = document.createElement("tr");
|
|
124
|
+
[T.name, T.value].forEach(function(h){ var th = document.createElement("th"); th.textContent = h; head.appendChild(th); }); tbl.appendChild(head);
|
|
125
|
+
names.filter(function(k){ return k in last; }).forEach(function(k){
|
|
126
|
+
var tr = document.createElement("tr"), th = document.createElement("th"), td = document.createElement("td");
|
|
127
|
+
th.textContent = k; td.textContent = fmt(last[k], py); tr.appendChild(th); tr.appendChild(td); tbl.appendChild(tr);
|
|
128
|
+
});
|
|
129
|
+
this.appendChild(tbl);
|
|
130
|
+
// ---- 配置:左にコードと説明の帯、右に変数と出力
|
|
131
|
+
var W = Math.max(1, this.clientWidth), H = Math.max(1, this.clientHeight);
|
|
132
|
+
var fs = P.size || 24, nf = P.size || 26, row = Math.ceil(fs * 1.4), noteRow = Math.ceil(nf * 1.45 + 4), pad = 16, gap = 24, header = row + 16;
|
|
133
|
+
var expanded = program.map(function(s){ return s.replace(/\t/g, " "); });
|
|
134
|
+
var numW = monoWidth(String(program.length), fs) + 16, textX = pad + numW;
|
|
135
|
+
var codeNatural = textX + Math.max(160, Math.max.apply(null, expanded.map(function(s){ return monoWidth(s, fs); }))) + pad;
|
|
136
|
+
var nameW = Math.max(0, Math.max.apply(null, names.map(function(k){ return monoWidth(k + " = ", fs); })));
|
|
137
|
+
var valueW = 0, varRows = 1;
|
|
138
|
+
st.forEach(function(s){
|
|
139
|
+
var r = 0;
|
|
140
|
+
names.forEach(function(k){ if (k in s.vars){ var R = rowsOf(s.vars[k], py); r += R.length; R.forEach(function(t){ valueW = Math.max(valueW, monoWidth(t, fs)); }); } });
|
|
141
|
+
varRows = Math.max(varRows, r);
|
|
142
|
+
});
|
|
143
|
+
var hasOut = all.length > 0, outW = hasOut ? Math.max.apply(null, acc[N - 1].map(function(s){ return monoWidth(s.replace(/\t/g, " "), fs); }).concat([0])) : 0;
|
|
144
|
+
var counterW = monoWidth(N + " / " + N, fs);
|
|
145
|
+
var rightNatural = Math.max(240, 2 * pad + nameW + valueW, 2 * pad + outW, 2 * pad + base.textWidth(T.vars, fs, {font: this}) + 16 + counterW);
|
|
146
|
+
var codeW, rightW;
|
|
147
|
+
if (codeNatural + gap + rightNatural <= W){ codeW = Math.max(codeNatural, Math.min(W * 0.5, W - gap - rightNatural)); }
|
|
148
|
+
else codeW = Math.max(Math.min(codeNatural, W * 0.45), W - gap - rightNatural);
|
|
149
|
+
codeW = Math.max(1, Math.min(codeW, W - gap - 120)); rightW = Math.max(1, W - codeW - gap);
|
|
150
|
+
var rx = codeW + gap;
|
|
151
|
+
// 説明の帯(コードの下、2 行まで)
|
|
152
|
+
var noteLines = st.map(function(s, i){
|
|
153
|
+
if (!s.note) return [];
|
|
154
|
+
var w = C.wrap(s.note.replace(/\s*\n\s*/g, " "), codeW - 24, nf, self);
|
|
155
|
+
if (w.length > 2){ ctx.report("layout", "[trace] note of step " + (i + 1) + " needs " + w.length + " rows in the band (2 fit); shorten it"); w = [w[0], proseCut(w.slice(1).join(""), codeW - 24, nf)]; }
|
|
156
|
+
return w;
|
|
157
|
+
});
|
|
158
|
+
function proseCut(s, width, f){
|
|
159
|
+
if (base.textWidth(s, f, {font: self}) <= width) return s;
|
|
160
|
+
var ch = [...s]; while (ch.length && base.textWidth(ch.join("") + "…", f, {font: self}) > width) ch.pop();
|
|
161
|
+
return width >= f ? ch.join("") + "…" : "";
|
|
162
|
+
}
|
|
163
|
+
var nr = Math.max(0, Math.max.apply(null, noteLines.map(function(w){ return w.length; }))), bandH = nr ? nr * noteRow + 16 : 0;
|
|
164
|
+
var codeH = H - (bandH ? bandH + 8 : 0);
|
|
165
|
+
// コードの見た目の行(長い行は折り返し、続きの行は番号の欄に ↪)
|
|
166
|
+
var textW = Math.max(fs, codeW - textX - pad), vrows = [], rowsOfLine = [];
|
|
167
|
+
program.forEach(function(line, i){
|
|
168
|
+
var ex = expanded[i], chars = [...ex], at = function(m){ return [...line.slice(0, m).replace(/\t/g, " ")].length; }, mine = [];
|
|
169
|
+
C.softWrap(ex, textW, fs).forEach(function(g, j){
|
|
170
|
+
var sp = marks[i].map(function(m){ return [Math.max(at(m[0]), g.from) - g.from, Math.min(at(m[1]), g.to) - g.from, m[2]]; }).filter(function(m){ return m[1] > m[0]; });
|
|
171
|
+
mine.push(vrows.length); vrows.push({line: i + 1, cont: j > 0, text: chars.slice(g.from, g.to).join(""), spans: sp});
|
|
172
|
+
});
|
|
173
|
+
rowsOfLine.push(mine);
|
|
174
|
+
});
|
|
175
|
+
var slots = Math.floor((codeH - header - pad) / row);
|
|
176
|
+
if (slots < 3) ctx.report("size", "[trace] element is " + W + " × " + H + " px; the code needs at least " + Math.ceil(header + pad + 3 * row + (bandH ? bandH + 8 : 0)) + " px height for 3 rows and the notes");
|
|
177
|
+
var shown = Math.max(1, Math.min(slots, vrows.length));
|
|
178
|
+
var span = function(s){ if (!s || !s.a) return null; var r = [].concat.apply([], rowsOfLine.slice(s.a - 1, s.b)); return [Math.min.apply(null, r), Math.max.apply(null, r)]; };
|
|
179
|
+
this.window0 = C.windowFor(vrows.length, shown, 0, 0);
|
|
180
|
+
this.windows = st.map(function(s){ var r = span(s); return r ? C.windowFor(vrows.length, shown, r[0], r[1]) : self.window0; });
|
|
181
|
+
this.spans = st.map(span);
|
|
182
|
+
// 右:変数のパネルと出力のパネル
|
|
183
|
+
var varsH = hasOut ? Math.min(H, header + varRows * row + pad) : H, outY = varsH + 8, outSlots = hasOut ? Math.floor((H - outY - header - pad) / row) : 0;
|
|
184
|
+
if (hasOut && outSlots < 1) ctx.report("size", "[trace] element is " + W + " × " + H + " px; the variables and the output need about " + Math.ceil(header + varRows * row + pad + 8 + header + row + pad) + " px height");
|
|
185
|
+
if (header + varRows * row + pad > H) ctx.report("size", "[trace] the variables need " + varRows + " rows (" + Math.ceil(header + varRows * row + pad) + " px); the element is " + H + " px high");
|
|
186
|
+
var cutValue = false;
|
|
187
|
+
st.forEach(function(s){ names.forEach(function(k){ if (k in s.vars) rowsOf(s.vars[k], py).forEach(function(t){ if (monoWidth(t, fs) > rightW - 2 * pad - nameW) cutValue = true; }); }); });
|
|
188
|
+
if (cutValue) ctx.report("layout", "[trace] a value is wider than the variables panel (" + Math.floor(rightW - 2 * pad - nameW) + " px); it is cut with …");
|
|
189
|
+
this.geo = {W: W, H: H, fs: fs, row: row, pad: pad, header: header, firstY: header + fs, codeW: codeW, codeH: codeH, rx: rx, rightW: rightW, varsH: varsH, outY: outY, outSlots: Math.max(0, outSlots), nameW: nameW, slots: shown, rows: vrows.length, bandH: bandH, varRows: varRows};
|
|
190
|
+
this.geometry = {codeW: codeW, codeH: codeH, rightW: rightW, varsH: varsH, outSlots: Math.max(0, outSlots), slots: shown, rows: vrows.length, windowed: vrows.length > shown, bandRows: nr};
|
|
191
|
+
this.svg = base.el("svg", {viewBox: "0 0 " + W + " " + H, width: W, height: H, "aria-hidden": "true", style: "overflow:hidden"}, this);
|
|
192
|
+
var panel = function(x, y, w, h){ base.el("rect", {x: x + 1, y: y + 1, width: Math.max(1, w - 2), height: Math.max(1, h - 2), rx: 12, fill: "var(--term-tint, color-mix(in srgb, var(--fg) 5%, var(--bg)))", stroke: "var(--line)", "stroke-width": 2}, self.svg); };
|
|
193
|
+
var tint = "var(--emph-tint, color-mix(in srgb, var(--emph) 12%, var(--bg)))";
|
|
194
|
+
// コード
|
|
195
|
+
panel(0, 0, codeW, codeH);
|
|
196
|
+
var tag = cut(lang, Math.max(1, codeW - 2 * pad), fs);
|
|
197
|
+
this.label(this.svg, codeW - pad - monoWidth(tag, fs), pad + fs, tag, fs, "var(--muted)", true);
|
|
198
|
+
this.band = base.el("rect", {x: 4, y: header, width: Math.max(1, codeW - 8), height: row, rx: 4, fill: tint, opacity: 0}, this.svg);
|
|
199
|
+
this.vrows = vrows.map(function(r){
|
|
200
|
+
var num = self.label(self.svg, pad, header + fs, r.cont ? "↪" : String(r.line), fs, "var(--muted)", true);
|
|
201
|
+
var e = self.label(self.svg, textX, header + fs, "", fs, "var(--fg)", true), pos = 0, ch = [...r.text];
|
|
202
|
+
var put = function(a, b, role){ if (b > a) base.el("tspan", role ? {fill: "var(--" + role + ")"} : {}, e).textContent = ch.slice(a, b).join(""); };
|
|
203
|
+
r.spans.forEach(function(m){ put(pos, m[0]); put(m[0], m[1], m[2]); pos = m[1]; });
|
|
204
|
+
put(pos, ch.length);
|
|
205
|
+
return {num: num, text: e, line: r.line};
|
|
206
|
+
});
|
|
207
|
+
this.markTop = this.label(this.svg, pad, header + fs, "", fs, "var(--muted)", true);
|
|
208
|
+
this.markBottom = this.label(this.svg, pad, header + fs, "", fs, "var(--muted)", true);
|
|
209
|
+
// 説明の帯
|
|
210
|
+
this.notes = noteLines.map(function(w){
|
|
211
|
+
var g = base.el("g", {opacity: 0}, self.svg);
|
|
212
|
+
if (w.length){
|
|
213
|
+
base.el("rect", {x: 1, y: codeH + 8, width: Math.max(1, codeW - 2), height: bandH, rx: 8, fill: "var(--bg)", stroke: "var(--line)", "stroke-width": 2}, g);
|
|
214
|
+
w.forEach(function(t, i){ self.label(g, 12, codeH + 8 + 8 + nf + i * noteRow, t, nf); });
|
|
215
|
+
}
|
|
216
|
+
return g;
|
|
217
|
+
});
|
|
218
|
+
// 変数
|
|
219
|
+
panel(rx, 0, rightW, varsH);
|
|
220
|
+
this.label(this.svg, rx + pad, pad + fs, T.vars, fs);
|
|
221
|
+
this.counter = this.label(this.svg, rx + rightW - pad, pad + fs, "", fs, "var(--muted)", true); this.counter.setAttribute("text-anchor", "end");
|
|
222
|
+
var vRoom = Math.max(1, Math.floor((varsH - header - pad) / row));
|
|
223
|
+
this.varSlots = [];
|
|
224
|
+
for (var i = 0; i < Math.min(varRows, vRoom); i++){
|
|
225
|
+
var y = header + fs + i * row;
|
|
226
|
+
this.varSlots.push({
|
|
227
|
+
bg: base.el("rect", {x: rx + 6, y: header + i * row, width: Math.max(1, rightW - 12), height: row, rx: 4, fill: tint, opacity: 0}, this.svg),
|
|
228
|
+
name: this.label(this.svg, rx + pad, y, "", fs, "var(--fg)", true),
|
|
229
|
+
value: this.label(this.svg, rx + pad + nameW, y, "", fs, "var(--fg)", true)
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
// 出力
|
|
233
|
+
this.outLabels = [];
|
|
234
|
+
if (hasOut && outSlots >= 1){
|
|
235
|
+
panel(rx, outY, rightW, H - outY);
|
|
236
|
+
this.label(this.svg, rx + pad, outY + pad + fs, T.output, fs);
|
|
237
|
+
for (var j = 0; j < outSlots; j++) this.outLabels.push(this.label(this.svg, rx + pad, outY + header + fs + j * row, "", fs, "var(--fg)", true));
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
// (k, t) → トレースの何番目(-1 はまだ何も実行していない)と、その中の進み具合 e(0〜1)
|
|
241
|
+
at(k, t){
|
|
242
|
+
var n = this.st.length, P = this.props;
|
|
243
|
+
if (k < 1) return {i: -1, e: 1};
|
|
244
|
+
if (P.by === "time"){
|
|
245
|
+
if (k > 1 || t === Infinity) return {i: n - 1, e: 1};
|
|
246
|
+
var x = Math.max(0, t) * P.rate, i = Math.min(n - 1, Math.floor(x));
|
|
247
|
+
return {i: i, e: i === n - 1 && x >= n ? 1 : Math.min(1, (x - i) * 2.5)};
|
|
248
|
+
}
|
|
249
|
+
if (k > n) return {i: n - 1, e: 1};
|
|
250
|
+
return {i: k - 1, e: base.prog(t, 0, P.dur)};
|
|
251
|
+
}
|
|
252
|
+
render(k, t, ctx){
|
|
253
|
+
if (!this.svg) return;
|
|
254
|
+
var self = this, G = this.geo, T = this.T, py = this.py, s = this.at(k, t), i = s.i, e = s.e, st = this.st;
|
|
255
|
+
var cur = i >= 0 ? st[i] : null, win = function(j){ return j < 0 ? self.window0 : self.windows[j]; };
|
|
256
|
+
// 表示する範囲:変わるときは前半で消えて後半で新しい位置に出る
|
|
257
|
+
var w0 = win(i - 1), w1 = win(i), moving = w0.f !== w1.f || w0.l !== w1.l, w = moving && e < 0.5 ? w0 : w1, fade = moving ? (e < 0.5 ? 1 - 2 * e : 2 * e - 1) : 1;
|
|
258
|
+
var slotY = function(r, ww){ ww = ww || w; return G.firstY + (r - ww.f + (ww.top ? 1 : 0)) * G.row; };
|
|
259
|
+
this.vrows.forEach(function(o, r){
|
|
260
|
+
var vis = r >= w.f && r <= w.l, y = vis ? slotY(r) : G.firstY, lit = cur && o.line >= cur.a && o.line <= cur.b;
|
|
261
|
+
set(o.num, "y", y); set(o.text, "y", y);
|
|
262
|
+
set(o.num, "opacity", vis ? fade.toFixed(3) : 0); set(o.text, "opacity", vis ? fade.toFixed(3) : 0);
|
|
263
|
+
set(o.num, "style", "font-family:" + MONO + ";white-space:pre;fill:var(--" + (lit ? "emph" : "muted") + ")" + (lit ? ";font-weight:700" : ""));
|
|
264
|
+
});
|
|
265
|
+
var vr = this.vrows, linesIn = function(a, b){ var seen = {}; for (var r = a; r <= b; r++) if (vr[r]) seen[vr[r].line] = 1; return Object.keys(seen).length; };
|
|
266
|
+
this.markTop.textContent = w.top ? T.more(Math.max(1, linesIn(0, w.f - 1))) : ""; set(this.markTop, "y", G.firstY); set(this.markTop, "opacity", w.top ? fade.toFixed(3) : 0);
|
|
267
|
+
this.markBottom.textContent = w.bottom ? T.more(Math.max(1, linesIn(w.l + 1, vr.length - 1))) : ""; set(this.markBottom, "y", slotY(w.l + 1)); set(this.markBottom, "opacity", w.bottom ? fade.toFixed(3) : 0);
|
|
268
|
+
// 今の行の地:同じ表示する範囲の中なら前の行から滑らせる
|
|
269
|
+
var sp1 = i >= 0 ? this.spans[i] : null, sp0 = i >= 1 ? this.spans[i - 1] : null;
|
|
270
|
+
if (sp1){
|
|
271
|
+
var top1 = slotY(sp1[0], w1) - G.fs, h1 = (sp1[1] - sp1[0] + 1) * G.row, top = top1, h = h1, op = 1;
|
|
272
|
+
if (!moving && sp0){ var top0 = slotY(sp0[0], w0) - G.fs, h0 = (sp0[1] - sp0[0] + 1) * G.row; top = base.lerp(top0, top1, e); h = base.lerp(h0, h1, e); }
|
|
273
|
+
else if (moving) op = e < 0.5 ? 0 : fade;
|
|
274
|
+
else op = e;
|
|
275
|
+
set(this.band, "y", +top.toFixed(1)); set(this.band, "height", +h.toFixed(1)); set(this.band, "opacity", op.toFixed(3));
|
|
276
|
+
} else { set(this.band, "y", G.header); set(this.band, "height", G.row); set(this.band, "opacity", 0); }
|
|
277
|
+
this.notes.forEach(function(g, j){ set(g, "opacity", j === i ? (i >= 1 && st[i - 1].note === st[i].note ? 1 : e).toFixed(3) : 0); });
|
|
278
|
+
this.counter.textContent = (i + 1) + " / " + st.length;
|
|
279
|
+
// 変数:その時点の値。変わった変数は字と地と太字で
|
|
280
|
+
var slot = 0, vars = cur ? cur.vars : {};
|
|
281
|
+
this.names.forEach(function(k){
|
|
282
|
+
if (!(k in vars)) return;
|
|
283
|
+
var ch = cur.changed.indexOf(k) >= 0, vx = monoWidth(k + " = ", G.fs);
|
|
284
|
+
rowsOf(vars[k], py).forEach(function(txt, r){
|
|
285
|
+
var o = self.varSlots[slot++]; if (!o) return;
|
|
286
|
+
o.name.textContent = r ? "" : k + " = ";
|
|
287
|
+
set(o.value, "x", +(G.rx + G.pad + vx).toFixed(1));
|
|
288
|
+
o.value.textContent = cut(txt, G.rightW - 2 * G.pad - vx, G.fs);
|
|
289
|
+
set(o.value, "style", "font-family:" + MONO + ";white-space:pre;fill:var(--" + (ch ? "emph" : "fg") + ")" + (ch ? ";font-weight:700" : ""));
|
|
290
|
+
set(o.bg, "opacity", ch ? e.toFixed(3) : 0);
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
for (; slot < this.varSlots.length; slot++){ var o = this.varSlots[slot]; o.name.textContent = ""; o.value.textContent = ""; set(o.value, "x", G.rx + G.pad + G.nameW); set(o.value, "style", "font-family:" + MONO + ";white-space:pre;fill:var(--fg)"); set(o.bg, "opacity", 0); }
|
|
294
|
+
// 出力:その時点までの出力。入らなければ新しい行を残し、上に「…(前の n 行)」
|
|
295
|
+
var L = i >= 0 ? this.acc[i] : [], n = this.outLabels.length, from = L.length > n ? L.length - n + 1 : 0;
|
|
296
|
+
var newFrom = i >= 1 ? this.acc[i - 1].length : 0;
|
|
297
|
+
this.outLabels.forEach(function(lab, j){
|
|
298
|
+
var idx = from ? from + j - 1 : j, txt = from && j === 0 ? T.older(from) : idx < L.length ? cut(L[idx].replace(/\t/g, " "), G.rightW - 2 * G.pad, G.fs) : "";
|
|
299
|
+
lab.textContent = txt;
|
|
300
|
+
set(lab, "opacity", !(from && j === 0) && idx >= newFrom && idx < L.length ? e.toFixed(3) : 1);
|
|
301
|
+
set(lab, "style", "font-family:" + MONO + ";white-space:pre;fill:var(--" + (from && j === 0 ? "muted" : "fg") + ")");
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
});
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// m-collect:並んだ札(回答など)が動いて縮み、少数の箱(要約)にまとまる
|
|
2
|
+
// <m-collect style="height:400px"><script type="application/json">
|
|
3
|
+
// {"chips":[{"label":"回答 1","to":[0,1]}, …], "targets":[{"label":"事後平均","tex":"\\mu_n"}, …], "cols":4}
|
|
4
|
+
// (JSON の終わり)</m-collect>
|
|
5
|
+
// 段階 1 で、札 i が targets[to] へ動く(to は番号か番号の配列。配列なら札の写しがそれぞれへ動く)。
|
|
6
|
+
// 箱は始めに点線の枠だけ出し、札が着いてから中身(label / tex / value)を出す
|
|
7
|
+
// 2.0(Egatari spec §6.1 の部品):base の MotionElement・el・mtext・prog・lerp・previewOpacity を使い、プレイヤーのグローバルは使わない。
|
|
8
|
+
// 式(tex)は組み立てのときに KaTeX で描く(spec §6.2):build.fields の targets[].tex を作り手が描いて texHtml を足し、
|
|
9
|
+
// この部品は markup-reader で決まった要素と属性だけを読んで置く(再生のときに KaTeX は動かない)。描いた結果がなければ tex を字のまま出して知らせる
|
|
10
|
+
egatariParts.define({name: "collect", version: "2.1.0", requires: {base: "1.3.0", "markup-reader": "1.0.3"}, element: "m-collect"}, function(deps){
|
|
11
|
+
"use strict";
|
|
12
|
+
var base = deps.base, MR = deps["markup-reader"];
|
|
13
|
+
return class extends base.MotionElement {
|
|
14
|
+
static partName = "collect"; // the registry name (define meta name), for report prefixes
|
|
15
|
+
// 組み立てのときに描く欄(spec §6.2)。式の意味は字(tex)でも持つので、KaTeX の出力は HTML だけ
|
|
16
|
+
static build = {fields: {"targets[].tex": "katex"}, options: {katex: {output: "html"}}};
|
|
17
|
+
// データの形(kit/shape.js)
|
|
18
|
+
static shape = {
|
|
19
|
+
schema: {type: "object", additionalProperties: false, required: ["chips", "targets"], properties: {
|
|
20
|
+
chips: {type: "array", minItems: 1, items: {type: "object", required: ["to"], properties: {label: {type: "string"}, to: {type: ["integer", "array"], items: {type: "integer"}}}}},
|
|
21
|
+
targets: {type: "array", minItems: 1, items: {type: "object", properties: {label: {type: "string"}, tex: {type: "string"}, texHtml: {type: "string"}, value: {type: ["string", "number"]}, tone: {type: "string"}, x: {type: "number"}, y: {type: "number"}}}},
|
|
22
|
+
cols: {type: "integer"}, renderers: {type: "object"}}},
|
|
23
|
+
example: {chips: [{label: "表", to: 0}, {label: "裏", to: 1}], targets: [{label: "表の数"}, {label: "裏の数"}], cols: 2},
|
|
24
|
+
};
|
|
25
|
+
static steps = 1;
|
|
26
|
+
static props = {
|
|
27
|
+
move: {type: "number", default: 0.9, doc: "札 1 枚が動く秒数"},
|
|
28
|
+
stagger: {type: "number", default: 0.08, doc: "札ごとの出発のずれ(秒)"},
|
|
29
|
+
shrink: {type: "number", default: 0.35, doc: "着いたときの札の縮尺"},
|
|
30
|
+
"chip-w": {type: "number", default: 0, doc: "札の幅(0 なら列の数から決める)"},
|
|
31
|
+
"chip-h": {type: "number", default: 56, doc: "札の高さ"},
|
|
32
|
+
"box-w": {type: "number", default: 260, doc: "箱の幅"},
|
|
33
|
+
"box-h": {type: "number", default: 110, doc: "箱の高さ"},
|
|
34
|
+
};
|
|
35
|
+
mount(ctx){
|
|
36
|
+
var P = this.props, D = base, self = this, data = this.data || {chips: [], targets: []}, sk = MR.sink(this, ctx);
|
|
37
|
+
this.style.display = "block";
|
|
38
|
+
var W = this.clientWidth, H = this.clientHeight;
|
|
39
|
+
var svg = D.el("svg", {viewBox: "0 0 " + W + " " + H, width: W, height: H, style: "display:block;overflow:visible"}, this);
|
|
40
|
+
var tone = {term: ["var(--term)", "var(--term-tint)"], emph: ["var(--emph)", "var(--emph-tint)"], muted: ["var(--muted)", "var(--bg)"]};
|
|
41
|
+
// 札の並び:上の段に cols 列で並べる
|
|
42
|
+
var C = data.chips || [], n = C.length, cols = data.cols || n, rows = Math.ceil(n / cols), gap = 14;
|
|
43
|
+
var cw = P["chip-w"] || Math.max(0, Math.min(220, (W - gap * (cols - 1)) / cols)), ch = P["chip-h"];
|
|
44
|
+
var gx = (W - (cw * cols + gap * (cols - 1))) / 2;
|
|
45
|
+
// 箱の並び:下の段に等間隔(x, y を書けばその位置。中心の座標)
|
|
46
|
+
var T = data.targets || [], bw = P["box-w"], bh = P["box-h"];
|
|
47
|
+
// tex と label のある箱は label を箱の下に書くので、その分(36 px)だけ下の段を上げる(要素の下にはみ出さない)
|
|
48
|
+
var gR = D.el("g", {}, svg); // 残る札(箱より下に重ねる。札の段が箱にかかっても箱の字は隠れない)
|
|
49
|
+
var under = T.some(function(tg){ return tg.tex && tg.label && tg.y === undefined; }) ? 36 : 0;
|
|
50
|
+
this.targets = T.map(function(tg, j){
|
|
51
|
+
var cx = tg.x !== undefined ? tg.x : W * (j + 1) / (T.length + 1), cy = tg.y !== undefined ? tg.y : H - bh / 2 - 4 - under;
|
|
52
|
+
var c = tone[tg.tone || "emph"] || tone.emph;
|
|
53
|
+
var frame = D.el("rect", {x: cx - bw / 2, y: cy - bh / 2, width: bw, height: bh, rx: 12, fill: "none", stroke: "var(--line)", "stroke-width": 2, "stroke-dasharray": "8 6", opacity: 0}, svg);
|
|
54
|
+
var g = D.el("g", {opacity: 0}, svg);
|
|
55
|
+
// marks for the geometry check (spec §8.4): the box is a box, its name, formula and value its labels; a name under or over the box stands outside
|
|
56
|
+
var bid = "t" + (j + 1), lab = function(e, out){ if (e){ e.setAttribute("data-part", "label"); e.setAttribute("data-part-id", bid); e.setAttribute("data-part-of", "box"); if (out) e.setAttribute("data-part-at", "outside"); } return e; };
|
|
57
|
+
D.el("rect", {"data-part": "box", "data-part-id": bid, x: cx - bw / 2, y: cy - bh / 2, width: bw, height: bh, rx: 12, fill: c[1], stroke: c[0], "stroke-width": 3}, g);
|
|
58
|
+
var hasV = tg.value !== undefined, ly = cy + (hasV ? -16 : 9);
|
|
59
|
+
if (tg.tex){ var tf = lab(MR.texFrame(g, cx, ly + 4, tg, "tex", {size: 30, anchor: "middle", color: c[0], w: bw, h: 50}, sk)); tf.firstChild.style.verticalAlign = "top"; }
|
|
60
|
+
else lab(D.mtext(g, cx, ly, tg.label || "", {"text-anchor": "middle", "font-size": 28, "font-weight": 700, fill: c[0]}));
|
|
61
|
+
if (hasV) lab(D.mtext(g, cx, cy + 38, String(tg.value), {"text-anchor": "middle", "font-size": 24, fill: "var(--fg)"}));
|
|
62
|
+
// 下に入らなければ(y を書いた箱)箱の上に
|
|
63
|
+
if (tg.tex && tg.label) lab(D.mtext(g, cx, cy + bh / 2 + 32 + 4 <= H ? cy + bh / 2 + 32 : cy - bh / 2 - 10, tg.label, {"text-anchor": "middle", "font-size": 24, fill: "var(--muted)"}), true);
|
|
64
|
+
return {frame: frame, g: g, cx: cx, cy: cy, col: c, last: 0};
|
|
65
|
+
});
|
|
66
|
+
// 札と、行き先ごとの写し。もとの札は元の場所に薄い枠で残る(動いた後も、何を集めたかが図に残る。印刷でも読める)
|
|
67
|
+
this.chips = []; this.rests = [];
|
|
68
|
+
var badTo = [];
|
|
69
|
+
C.forEach(function(cp, i){
|
|
70
|
+
var r = Math.floor(i / cols), q = i % cols;
|
|
71
|
+
var x = gx + q * (cw + gap) + cw / 2, y = 4 + ch / 2 + r * (ch + gap);
|
|
72
|
+
var to = Array.isArray(cp.to) ? cp.to : [cp.to === undefined ? 0 : cp.to];
|
|
73
|
+
var start = i * P.stagger;
|
|
74
|
+
to.forEach(function(j){ if (!self.targets[j]) badTo.push("chip " + i + " (" + JSON.stringify(cp.label || "") + "): to " + JSON.stringify(j)); });
|
|
75
|
+
if (to.some(function(j){ return self.targets[j]; })){
|
|
76
|
+
var rg = D.el("g", {transform: "translate(" + x.toFixed(2) + " " + y.toFixed(2) + ")", opacity: 0}, gR);
|
|
77
|
+
// the chip left in place is a box with its label (the moving copies fade out, so they are not marked)
|
|
78
|
+
D.el("rect", {"data-part": "box", "data-part-id": "c" + (i + 1), x: -cw / 2, y: -ch / 2, width: cw, height: ch, rx: 8, fill: "none", stroke: "var(--line)", "stroke-width": 2, "stroke-dasharray": "6 5"}, rg);
|
|
79
|
+
D.mtext(rg, 0, 9, cp.label || "", {"data-part": "label", "data-part-id": "c" + (i + 1), "data-part-of": "box", "text-anchor": "middle", "font-size": 24, fill: "var(--muted)"});
|
|
80
|
+
self.rests.push({g: rg, start: start});
|
|
81
|
+
}
|
|
82
|
+
to.forEach(function(j){
|
|
83
|
+
var tg = self.targets[j]; if (!tg) return;
|
|
84
|
+
var c = tone[cp.tone || "term"] || tone.term;
|
|
85
|
+
var g = D.el("g", {}, svg);
|
|
86
|
+
D.el("rect", {x: -cw / 2, y: -ch / 2, width: cw, height: ch, rx: 8, fill: c[1], stroke: c[0], "stroke-width": 2}, g);
|
|
87
|
+
D.mtext(g, 0, 9, cp.label || "", {"text-anchor": "middle", "font-size": 24});
|
|
88
|
+
tg.last = Math.max(tg.last, start + P.move);
|
|
89
|
+
self.chips.push({g: g, x0: x, y0: y, x1: tg.cx, y1: tg.cy, start: start});
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
if (badTo.length) ctx.report("data", "[collect] " + badTo.join("; ") + ": not a target number 0–" + (T.length - 1) + " (the copy is not drawn)");
|
|
93
|
+
this.rows = rows;
|
|
94
|
+
sk.flush(); // what the formulas have to report, once per kind (spec §6.2)
|
|
95
|
+
}
|
|
96
|
+
duration(k){ var d = 0; this.targets.forEach(function(tg){ d = Math.max(d, tg.last + 0.3); }); return k >= 1 ? d : 0; }
|
|
97
|
+
render(k, t, ctx){
|
|
98
|
+
var P = this.props, D = base;
|
|
99
|
+
var T = k < 1 ? -Infinity : t; // 段階 0 は動く前
|
|
100
|
+
// 先の段階を薄く見せる表示(授業の表示):段階 0 では、まとめた後の箱をテーマの --preview-opacity の濃さで出す(札は動かさない)
|
|
101
|
+
var PV = 0;
|
|
102
|
+
if (k < 1 && ctx.preview) PV = base.previewOpacity(this);
|
|
103
|
+
var pr = function(start, dur){ return T === -Infinity ? 0 : D.prog(T, start, dur); };
|
|
104
|
+
this.chips.forEach(function(c){
|
|
105
|
+
var e = pr(c.start, P.move);
|
|
106
|
+
// 少し持ち上げてから落ちる弧(中点で上へ 40px)
|
|
107
|
+
var x = D.lerp(c.x0, c.x1, e), y = D.lerp(c.y0, c.y1, e) - Math.sin(Math.PI * e) * 40;
|
|
108
|
+
var s = D.lerp(1, P.shrink, e), op = e < 0.75 ? 1 : 1 - (e - 0.75) / 0.25;
|
|
109
|
+
c.g.setAttribute("transform", "translate(" + x.toFixed(2) + " " + y.toFixed(2) + ") scale(" + s.toFixed(4) + ")");
|
|
110
|
+
c.g.setAttribute("opacity", op.toFixed(3));
|
|
111
|
+
});
|
|
112
|
+
// 残る札:写しが出発したら現れる
|
|
113
|
+
this.rests.forEach(function(c){ c.g.setAttribute("opacity", Math.min(1, pr(c.start, P.move) * 4).toFixed(3)); });
|
|
114
|
+
this.targets.forEach(function(tg){
|
|
115
|
+
tg.frame.setAttribute("opacity", (pr(0, 0.3) * (1 - pr(tg.last - 0.1, 0.3))).toFixed(3));
|
|
116
|
+
var e = pr(tg.last - 0.15, 0.45);
|
|
117
|
+
if (PV){ e = 1; tg.g.setAttribute("opacity", PV.toFixed(3)); }
|
|
118
|
+
else tg.g.setAttribute("opacity", e.toFixed(3));
|
|
119
|
+
var s = D.lerp(0.9, 1, e);
|
|
120
|
+
tg.g.setAttribute("transform", "translate(" + tg.cx + " " + tg.cy + ") scale(" + s.toFixed(4) + ") translate(" + (-tg.cx) + " " + (-tg.cy) + ")");
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
---
|
|
2
|
+
summary: Balance at expectation
|
|
3
|
+
steps: Weights → pivot → sum (3)
|
|
4
|
+
props:
|
|
5
|
+
dur: Seconds
|
|
6
|
+
digits: Decimal places
|
|
7
|
+
size: Text px
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
JSON:
|
|
11
|
+
- `values`: 2–12 distinct numbers.
|
|
12
|
+
- `probs`: sum to 1 (else reported).
|
|
13
|
+
- `name`: X.
|
|
14
|
+
|
|
15
|
+
<!-- llm: omit -->
|
|
16
|
+
Bars proportional to probabilities. Pivot: minimum → E[X]. Moment controls tilt, clamped ±8°, then levels. Beam, bars and ticks rotate together. Starts horizontal; tilts at step 2. Final E and mtext sum; first 6 terms then …; computation and accessible weights include all terms. Negative terms parenthesized.
|
|
17
|
+
|
|
18
|
+
Invalid totals stay unnormalized. Out-of-range probabilities, mismatched lengths, duplicates or unrepresentable calculations: report, no draw. dur 1.5s; digits 0–10 (2), trailing zeros removed; size 0=26px, ≥24. Bad props default/report.
|
|
19
|
+
|
|
20
|
+
1184×440px suggested. Scaled coordinates, nice ticks. Close bars, labels or long sums report size.
|
|
21
|
+
|
|
22
|
+
ctx.lang selects ja/en. Font boxes ≥1.43em plus gap. Image role, final summary; preserve author label, hide SVG. No answer preview. Fictional demo.
|
|
23
|
+
<!-- llm: end -->
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// m-balance: expectation is the zero-moment position of probability weights.
|
|
2
|
+
egatariParts.define({name: "balance", version: "2.0.2", requires: {base: "1.3.0", plot: "1.1.3"}, element: "m-balance"}, function(deps){
|
|
3
|
+
"use strict";
|
|
4
|
+
var base = deps.base;
|
|
5
|
+
var TEXT = {
|
|
6
|
+
ja: {title: "期待値はつり合いの位置", probability: "確率", sum: "確率の合計", expectation: "期待値"},
|
|
7
|
+
en: {title: "Expectation is the balance point", probability: "probability", sum: "probability total", expectation: "expectation"}
|
|
8
|
+
};
|
|
9
|
+
function set(e, a, v){ e.setAttribute(a, String(v)); }
|
|
10
|
+
function ticks(lo, hi){
|
|
11
|
+
var rough = (hi - lo) / 5, power = Math.pow(10, Math.floor(Math.log10(rough))), f = rough / power;
|
|
12
|
+
var step = (f <= 1 ? 1 : f <= 2 ? 2 : f <= 5 ? 5 : 10) * power, out = [];
|
|
13
|
+
for (var i = Math.ceil(lo / step); out.length < 10 && i * step <= hi + step * 1e-9; i++) out.push(+(i * step).toPrecision(12));
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
16
|
+
function num(v){ return String(+v.toPrecision(6)); }
|
|
17
|
+
return class extends base.MotionElement {
|
|
18
|
+
static partName = "balance";
|
|
19
|
+
static preview = "none";
|
|
20
|
+
static steps = 3;
|
|
21
|
+
static shape = {schema: {type: "object", additionalProperties: false, required: ["values", "probs"], properties: {
|
|
22
|
+
values: {type: "array", minItems: 2, maxItems: 12, items: {type: "number"}},
|
|
23
|
+
probs: {type: "array", minItems: 2, maxItems: 12, items: {type: "number"}}, name: {type: "string"}
|
|
24
|
+
}}, example: {values: [0, 1, 2, 3], probs: [0.1, 0.3, 0.4, 0.2], name: "X"}};
|
|
25
|
+
static props = {
|
|
26
|
+
dur: {type: "number", default: 1.5, doc: "ステップの秒数"},
|
|
27
|
+
digits: {type: "number", default: 2, doc: "期待値の小数桁"},
|
|
28
|
+
size: {type: "number", default: 0, doc: "文字 px(0 は 26)"}
|
|
29
|
+
};
|
|
30
|
+
duration(k){ return k >= 1 && k <= 3 ? this.props.dur : 0; }
|
|
31
|
+
label(g, x, y, s, anchor){
|
|
32
|
+
var e = base.el("text", {x: x, y: y, "font-size": this.fs, "text-anchor": anchor || "start", fill: "var(--fg)"}, g);
|
|
33
|
+
e.textContent = s; return e;
|
|
34
|
+
}
|
|
35
|
+
mount(ctx){
|
|
36
|
+
var self = this, d = this.data, P = this.props;
|
|
37
|
+
this.T = /^ja\b/i.test(ctx.lang || "") ? TEXT.ja : TEXT.en;
|
|
38
|
+
if (!(Number.isFinite(P.dur) && P.dur > 0)){ ctx.report("attr", "[balance] dur must be positive; using 1.5"); P.dur = 1.5; }
|
|
39
|
+
if (!Number.isInteger(P.digits) || P.digits < 0 || P.digits > 10){ ctx.report("attr", "[balance] digits must be an integer 0–10; using 2"); P.digits = 2; }
|
|
40
|
+
if (!(P.size === 0 || Number.isFinite(P.size) && P.size >= 24)){ ctx.report("attr", "[balance] size must be 0 or at least 24; using 26"); P.size = 0; }
|
|
41
|
+
this.fs = P.size || 26;
|
|
42
|
+
this.style.display = "block"; this.style.position = "relative"; this.style.writingMode = "horizontal-tb"; this.setAttribute("role", "img");
|
|
43
|
+
if (d.values.length !== d.probs.length || new Set(d.values).size !== d.values.length){ ctx.report("data", "[balance] values must be distinct and probs must have the same length"); return; }
|
|
44
|
+
var total = d.probs.reduce(function(s, p){ return s + p; }, 0);
|
|
45
|
+
if (Math.abs(total - 1) > 1e-9) ctx.report("data", "[balance] probs must sum to 1 (±1e-9); drawing supplied weights, total " + total);
|
|
46
|
+
if (d.probs.some(function(p){ return p < 0 || p > 1; })){ ctx.report("data", "[balance] probabilities must be between 0 and 1"); return; }
|
|
47
|
+
// Scale before sums and coordinate differences to keep finite extreme values usable.
|
|
48
|
+
var scale = Math.pow(10, Math.floor(Math.log10(Math.max(1, ...d.values.map(Math.abs))))), vals = d.values.map(function(v){ return v / scale; });
|
|
49
|
+
var expectedN = vals.reduce(function(s, v, i){ return s + v * d.probs[i]; }, 0), expected = expectedN * scale;
|
|
50
|
+
if (!Number.isFinite(expected)){ ctx.report("data", "[balance] expectation exceeds the numerical range; rescale values"); return; }
|
|
51
|
+
var lo = Math.min(...vals, expectedN), hi = Math.max(...vals, expectedN), span = hi - lo;
|
|
52
|
+
if (!(span > 0)){ ctx.report("data", "[balance] values are too close to represent; rescale values"); return; }
|
|
53
|
+
var name = d.name === undefined ? "X" : d.name, answer = String(+expected.toFixed(P.digits));
|
|
54
|
+
var expectation = "E[" + name + "] = " + answer;
|
|
55
|
+
var terms = d.values.map(function(v, i){ return (v < 0 ? "(" + num(v) + ")" : num(v)) + "×" + num(d.probs[i]); });
|
|
56
|
+
var formula = terms.slice(0, 6).join(" + ") + (terms.length > 6 ? " + …" : "") + " = " + answer;
|
|
57
|
+
this.model = {values: d.values.slice(), probs: d.probs.slice(), total: total, expected: expected, expectedN: expectedN, scale: scale, name: name, formula: formula, lo: lo, hi: hi};
|
|
58
|
+
if (!this.getAttribute("aria-label")) this.setAttribute("aria-label", this.T.title + "; " + d.values.map(function(v, i){ return num(v) + ": " + self.T.probability + " " + num(d.probs[i]); }).join("; ") + "; " + this.T.sum + " " + num(total) + "; " + this.T.expectation + " " + expectation + "; " + formula);
|
|
59
|
+
var W = Math.max(1, this.clientWidth), H = Math.max(1, this.clientHeight), fs = this.fs, line = fs * 1.45 + 4;
|
|
60
|
+
var width = function(s){ return base.textWidth(s, fs, {font: self}); };
|
|
61
|
+
var pad = Math.max(56, ...d.values.map(function(v){ return width(num(v)) / 2 + 16; }));
|
|
62
|
+
var axisY = H / 2, plot = deps.plot.Plot(this, {xr: [lo, hi], yr: [0, 1], xt: [], yt: [], noY: true, m: {l: pad, r: pad, t: 0, b: H - axisY}});
|
|
63
|
+
this.svg = plot.svg; set(this.svg, "width", W); set(this.svg, "height", H); set(this.svg, "aria-hidden", "true");
|
|
64
|
+
var axes = Array.from(this.svg.children).filter(function(e){ return e.getAttribute("class") === "axes"; })[0]; this.svg.removeChild(axes);
|
|
65
|
+
this.weightLayer = base.el("g", {opacity: 0}, this.svg);
|
|
66
|
+
this.beam = base.el("g", {}, this.weightLayer);
|
|
67
|
+
base.el("line", {x1: pad, x2: W - pad, y1: axisY, y2: axisY, stroke: "var(--muted)", "stroke-width": 4}, this.beam);
|
|
68
|
+
var plotWidth = Math.max(1, W - 2 * pad), maxRise = plotWidth * Math.sin(8 * Math.PI / 180) / 4;
|
|
69
|
+
var barH = Math.max(20, axisY - 1.45 * fs - maxRise - 24), maxP = Math.max(1e-12, ...d.probs);
|
|
70
|
+
var positions = vals.map(plot.x), sorted = positions.slice().sort(function(a, b){ return a - b; });
|
|
71
|
+
var minGap = Math.min(...sorted.slice(1).map(function(x, i){ return x - sorted[i]; })), bw = Math.max(2, Math.min(44, minGap - 12));
|
|
72
|
+
var small = W < 2 * pad + 40 || axisY - barH - 1.1 * fs - maxRise < 0 || axisY + plotWidth * Math.sin(8 * Math.PI / 180) + line + fs * 0.4 > H;
|
|
73
|
+
this.bars = d.values.map(function(v, i){
|
|
74
|
+
var h = d.probs[i] / maxP * barH, x = positions[i], s = num(d.probs[i]);
|
|
75
|
+
var bar = base.el("rect", {x: x - bw / 2, y: axisY - h, width: bw, height: h, fill: "var(--term)"}, self.beam);
|
|
76
|
+
var text = self.label(self.beam, x, axisY - h - fs * 0.4 - 8, s, "middle");
|
|
77
|
+
return {bar: bar, text: text, x: x, h: h, w: width(s)};
|
|
78
|
+
});
|
|
79
|
+
this.bars.forEach(function(a, i){ self.bars.slice(i + 1).forEach(function(b){
|
|
80
|
+
if (Math.abs(a.x - b.x) < (a.w + b.w) / 2 + 6 && Math.abs(a.h - b.h) < 1.45 * fs + 4) small = true;
|
|
81
|
+
// A neighbouring tall bar must not run through a probability label.
|
|
82
|
+
if (Math.abs(a.x - b.x) < a.w / 2 + bw / 2 + 4 && b.h > a.h + 4 || Math.abs(a.x - b.x) < b.w / 2 + bw / 2 + 4 && a.h > b.h + 4) small = true;
|
|
83
|
+
}); });
|
|
84
|
+
// Nice ticks in normalized coordinates; labels retain the original units.
|
|
85
|
+
this.ticks = ticks(lo, hi);
|
|
86
|
+
var lastRight = -Infinity;
|
|
87
|
+
this.ticks.forEach(function(v){
|
|
88
|
+
var x = plot.x(v), s = num(v * scale), w = width(s);
|
|
89
|
+
if (x - w / 2 < lastRight + 4 || x - w / 2 < 0 || x + w / 2 > W) small = true;
|
|
90
|
+
lastRight = x + w / 2;
|
|
91
|
+
base.el("line", {x1: x, x2: x, y1: axisY, y2: axisY + 6, stroke: "var(--muted)", "stroke-width": 2}, self.beam);
|
|
92
|
+
self.label(self.beam, x, axisY + line, s, "middle");
|
|
93
|
+
});
|
|
94
|
+
this.fulcrum = base.el("g", {opacity: 0}, this.svg);
|
|
95
|
+
base.el("path", {d: "M0," + (axisY + 2) + "l-18,26h36Z", fill: "var(--emph-tint)", stroke: "var(--emph)", "stroke-width": 3}, this.fulcrum);
|
|
96
|
+
this.finalLayer = base.el("g", {opacity: 0}, this.svg);
|
|
97
|
+
this.start = plot.x(Math.min(...vals)); this.end = plot.x(expectedN); this.axisY = axisY;
|
|
98
|
+
var ew = width(expectation), labelX = Math.max(ew / 2 + 4, Math.min(W - ew / 2 - 4, this.end));
|
|
99
|
+
var labelY = axisY + 2 * line + 14, formulaY = H - fs * 0.4 - 4;
|
|
100
|
+
this.label(this.finalLayer, labelX, labelY, expectation, "middle");
|
|
101
|
+
// The sum has numeric terms only; variable names above are literal text.
|
|
102
|
+
this.formulaText = base.mtext(this.finalLayer, W / 2, formulaY, formula, {"font-size": fs, "text-anchor": "middle", fill: "var(--fg)"});
|
|
103
|
+
if (ew > W - 8 || width(formula) > W - 8 || formulaY - labelY < line || labelY + fs * 0.4 > H) small = true;
|
|
104
|
+
if (small) ctx.report("size", "[balance] weights, ticks, tilt or formula do not fit; enlarge the element or separate close values");
|
|
105
|
+
}
|
|
106
|
+
render(k, t){
|
|
107
|
+
if (!this.model) return;
|
|
108
|
+
var dur = this.props.dur, e = k > 2 ? 1 : k < 2 ? 0 : base.prog(t, 0, dur);
|
|
109
|
+
var pivot = base.lerp(this.start, this.end, e), m = this.model;
|
|
110
|
+
var pivotN = base.lerp(Math.min(...m.values.map(function(v){ return v / m.scale; })), m.expectedN, e);
|
|
111
|
+
// Positive moment means a heavier right side (clockwise in SVG). At E, the beam levels.
|
|
112
|
+
var moment = m.values.reduce(function(s, v, i){ return s + (v / m.scale - pivotN) * m.probs[i]; }, 0);
|
|
113
|
+
var angle = k >= 2 && e < 1 ? Math.max(-8, Math.min(8, 8 * moment / (m.hi - m.lo))) : 0;
|
|
114
|
+
set(this.weightLayer, "opacity", k > 1 ? 1 : k < 1 ? 0 : base.prog(t, 0, dur));
|
|
115
|
+
set(this.beam, "transform", "rotate(" + angle + " " + pivot + " " + this.axisY + ")");
|
|
116
|
+
set(this.fulcrum, "opacity", k >= 1 ? 1 : 0); set(this.fulcrum, "transform", "translate(" + pivot + " 0)");
|
|
117
|
+
set(this.finalLayer, "opacity", k > 3 ? 1 : k < 3 ? 0 : base.prog(t, 0, dur));
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
---
|
|
2
|
+
summary: A positioning table: the cross of two axes (studies in cells, this work framed, empty cells — with a note) or studies × elements (✓ △ × or short text), filled by step
|
|
3
|
+
steps: cross: axes → studies → this work → notes (order="whole": 1). attributes: per column or row (order) + frame
|
|
4
|
+
props:
|
|
5
|
+
order: cross build|whole; attributes whole|col|row
|
|
6
|
+
dur: seconds per step
|
|
7
|
+
size: text px (≥ 24)
|
|
8
|
+
example: {"kind": "cross", "rows": {"axis": "Criterion", "values": ["Error", "Bayes risk"]}, "cols": {"axis": "Output", "values": ["Point", "Distribution"]}, "cells": [[["[1]", "[2]"], ["[3]"]], [{"none": "Trivial"}, {"self": true}]]}
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
`kind`: `cross` (default) or `attributes`.
|
|
12
|
+
|
|
13
|
+
- cross: `rows`, `cols` = `{"axis", "values"}` (2–4 each). `cells[r][c]`: study names (1–3), `{"self": true}` or `{"none": "why"}` (— and a note). `marks`: `[{"mark": "†", "text"}]`
|
|
14
|
+
- attributes: `cols` names; `rows` `{"label", "cells", "self"}`, this work first
|
|
15
|
+
- This work is written out and framed in `--emph`
|
|
16
|
+
<!-- llm: omit -->
|
|
17
|
+
- Readable table: headers as "axis: value". Text shrinks to 24 px, then reports size.
|
|
18
|
+
<!-- llm: end -->
|