thursday-agent 0.16.0 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (278) hide show
  1. package/.next/BUILD_ID +1 -1
  2. package/.next/build-manifest.json +3 -3
  3. package/.next/prerender-manifest.json +3 -3
  4. package/.next/server/app/_global-error/page.js.nft.json +1 -1
  5. package/.next/server/app/_global-error.html +1 -1
  6. package/.next/server/app/_global-error.rsc +1 -1
  7. package/.next/server/app/_global-error.segments/__PAGE__.segment.rsc +1 -1
  8. package/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  9. package/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  10. package/.next/server/app/_not-found/page.js.nft.json +1 -1
  11. package/.next/server/app/_not-found.html +1 -1
  12. package/.next/server/app/_not-found.rsc +1 -1
  13. package/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  14. package/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  15. package/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  16. package/.next/server/app/api/artifact/route.js +9 -9
  17. package/.next/server/app/api/artifact/route.js.nft.json +1 -1
  18. package/.next/server/app/api/bot/memory/files/route.js +3 -3
  19. package/.next/server/app/api/bot/memory/files/route.js.nft.json +1 -1
  20. package/.next/server/app/api/bot/memory/route.js +8 -8
  21. package/.next/server/app/api/bot/memory/route.js.nft.json +1 -1
  22. package/.next/server/app/api/bot/route.js +8 -8
  23. package/.next/server/app/api/bot/route.js.nft.json +1 -1
  24. package/.next/server/app/api/bot/thread/[id]/route.js +3 -3
  25. package/.next/server/app/api/bot/thread/[id]/route.js.nft.json +1 -1
  26. package/.next/server/app/api/bot/thread/route.js +3 -3
  27. package/.next/server/app/api/bot/thread/route.js.nft.json +1 -1
  28. package/.next/server/app/api/config/route.js.nft.json +1 -1
  29. package/.next/server/app/api/events/route.js.nft.json +1 -1
  30. package/.next/server/app/api/favicon/[host]/route.js.nft.json +1 -1
  31. package/.next/server/app/api/file/[...path]/route.js +4 -4
  32. package/.next/server/app/api/file/[...path]/route.js.nft.json +1 -1
  33. package/.next/server/app/api/llm-model/catalog/route.js.nft.json +1 -1
  34. package/.next/server/app/api/llm-model/credits/route.js.nft.json +1 -1
  35. package/.next/server/app/api/llm-model/route.js.nft.json +1 -1
  36. package/.next/server/app/api/llm-model/subscription/route.js.nft.json +1 -1
  37. package/.next/server/app/api/mcp/[name]/route.js.nft.json +1 -1
  38. package/.next/server/app/api/mcp/oauth/callback/route.js.nft.json +1 -1
  39. package/.next/server/app/api/mcp/route.js.nft.json +1 -1
  40. package/.next/server/app/api/mcp/tools/route.js.nft.json +1 -1
  41. package/.next/server/app/api/memory/edit/route.js +11 -11
  42. package/.next/server/app/api/memory/edit/route.js.nft.json +1 -1
  43. package/.next/server/app/api/memory/route.js.nft.json +1 -1
  44. package/.next/server/app/api/reach/route.js +11 -11
  45. package/.next/server/app/api/reach/route.js.nft.json +1 -1
  46. package/.next/server/app/api/routine/route.js +7 -7
  47. package/.next/server/app/api/routine/route.js.nft.json +1 -1
  48. package/.next/server/app/api/signins/route.js +5 -5
  49. package/.next/server/app/api/signins/route.js.nft.json +1 -1
  50. package/.next/server/app/api/skills/[source]/[name]/route.js +7 -7
  51. package/.next/server/app/api/skills/[source]/[name]/route.js.nft.json +1 -1
  52. package/.next/server/app/api/skills/route.js +7 -7
  53. package/.next/server/app/api/skills/route.js.nft.json +1 -1
  54. package/.next/server/app/api/thursday/call/route.js +5 -5
  55. package/.next/server/app/api/thursday/call/route.js.nft.json +1 -1
  56. package/.next/server/app/api/thursday/settings/route.js +5 -5
  57. package/.next/server/app/api/thursday/settings/route.js.nft.json +1 -1
  58. package/.next/server/app/api/thursday/text/route.js +13 -13
  59. package/.next/server/app/api/thursday/text/route.js.nft.json +1 -1
  60. package/.next/server/app/api/thursday/tool-call/route.js +12 -12
  61. package/.next/server/app/api/thursday/tool-call/route.js.nft.json +1 -1
  62. package/.next/server/app/api/workspace/files/route.js +8 -8
  63. package/.next/server/app/api/workspace/files/route.js.nft.json +1 -1
  64. package/.next/server/app/api/workspace/route.js +9 -9
  65. package/.next/server/app/api/workspace/route.js.nft.json +1 -1
  66. package/.next/server/app/artifact/[...path]/page/server-reference-manifest.json +3 -3
  67. package/.next/server/app/artifact/[...path]/page.js +4 -4
  68. package/.next/server/app/artifact/[...path]/page.js.nft.json +1 -1
  69. package/.next/server/app/artifact/[...path]/page_client-reference-manifest.js +1 -1
  70. package/.next/server/app/favicon.ico/route.js.nft.json +1 -1
  71. package/.next/server/app/manifest.webmanifest/route.js.nft.json +1 -1
  72. package/.next/server/app/page/server-reference-manifest.json +60 -60
  73. package/.next/server/app/page.js +5 -5
  74. package/.next/server/app/page.js.nft.json +1 -1
  75. package/.next/server/app/page_client-reference-manifest.js +1 -1
  76. package/.next/server/chunks/[root-of-the-server]__00qyw01._.js +1 -1
  77. package/.next/server/chunks/[root-of-the-server]__01ypm_z._.js +3 -0
  78. package/.next/server/chunks/[root-of-the-server]__021dkui._.js +3 -0
  79. package/.next/server/chunks/{[root-of-the-server]__18e65_-._.js → [root-of-the-server]__03xamry._.js} +2 -2
  80. package/.next/server/chunks/[root-of-the-server]__05dp5tr._.js +1 -1
  81. package/.next/server/chunks/[root-of-the-server]__09gdscj._.js +7 -0
  82. package/.next/server/chunks/{[root-of-the-server]__10_7vqz._.js → [root-of-the-server]__0b36yfw._.js} +1 -1
  83. package/.next/server/chunks/{[root-of-the-server]__1gamvcz._.js → [root-of-the-server]__0jzwdg-._.js} +2 -2
  84. package/.next/server/chunks/[root-of-the-server]__0mrk2it._.js +1 -1
  85. package/.next/server/chunks/[root-of-the-server]__0nmust6._.js +5 -0
  86. package/.next/server/chunks/[root-of-the-server]__0svzjwa._.js +7 -0
  87. package/.next/server/chunks/{[root-of-the-server]__16yw3mb._.js → [root-of-the-server]__12m24xl._.js} +1 -1
  88. package/.next/server/chunks/{[root-of-the-server]__1zrwxis._.js → [root-of-the-server]__12tahb4._.js} +1 -1
  89. package/.next/server/chunks/{[root-of-the-server]__1ep-pao._.js → [root-of-the-server]__1_h4hf9._.js} +4 -4
  90. package/.next/server/chunks/{[root-of-the-server]__1-tdj60._.js → [root-of-the-server]__1de1o6m._.js} +2 -2
  91. package/.next/server/chunks/{[root-of-the-server]__1voqqyc._.js → [root-of-the-server]__1ignh6-._.js} +1 -1
  92. package/.next/server/chunks/[root-of-the-server]__1t02-1p._.js +3 -0
  93. package/.next/server/chunks/[root-of-the-server]__1ygplx1._.js +1 -1
  94. package/.next/server/chunks/{[root-of-the-server]__11g20j-._.js → [root-of-the-server]__1yl5l25._.js} +1 -1
  95. package/.next/server/chunks/{[root-of-the-server]__1mbd2w_._.js → [root-of-the-server]__208vmyf._.js} +1 -1
  96. package/.next/server/chunks/{_1mfu9a3._.js → _02lipqs._.js} +1 -1
  97. package/.next/server/chunks/_05sa7xl._.js +1 -1
  98. package/.next/server/chunks/{_1rg5bfd._.js → _074f2ts._.js} +1 -1
  99. package/.next/server/chunks/_07nxi75._.js +37 -37
  100. package/.next/server/chunks/_08354bn._.js +12 -12
  101. package/.next/server/chunks/_08btiir._.js +7 -0
  102. package/.next/server/chunks/_0caua_s._.js +1 -1
  103. package/.next/server/chunks/{_0qoush_._.js → _0gt6cbj._.js} +1 -1
  104. package/.next/server/chunks/_0xygcxs._.js +37 -37
  105. package/.next/server/chunks/{_09c4we8._.js → _17i1lt2._.js} +1 -1
  106. package/.next/server/chunks/_19kea1l._.js +9 -9
  107. package/.next/server/chunks/_1gn2r2q._.js +3 -3
  108. package/.next/server/chunks/{_03niwml._.js → _1ntwp1s._.js} +1 -1
  109. package/.next/server/chunks/_1q6e56i._.js +3 -3
  110. package/.next/server/chunks/{_02fn1o3._.js → _1tkzgtm._.js} +1 -1
  111. package/.next/server/chunks/_1wl2g8_._.js +2 -2
  112. package/.next/server/chunks/{_1w6uf9k._.js → _1y3mnz4._.js} +1 -1
  113. package/.next/server/chunks/features_workspace_workspace_ts_13h82gv._.js +4 -4
  114. package/.next/server/chunks/features_workspace_workspace_ts_1iyjroj._.js +4 -4
  115. package/.next/server/chunks/{node_modules_consola_dist_chunks_prompt_mjs_0cux6zt._.js → node_modules_consola_dist_chunks_prompt_mjs_04oxl25._.js} +1 -1
  116. package/.next/server/chunks/{node_modules_consola_dist_chunks_prompt_mjs_0w32bqh._.js → node_modules_consola_dist_chunks_prompt_mjs_0g87zst._.js} +1 -1
  117. package/.next/server/chunks/{node_modules_consola_dist_chunks_prompt_mjs_1clmnaj._.js → node_modules_consola_dist_chunks_prompt_mjs_1341cnf._.js} +1 -1
  118. package/.next/server/chunks/ssr/{[root-of-the-server]__02jdtqa._.js → [root-of-the-server]__07660xu._.js} +3 -3
  119. package/.next/server/chunks/ssr/[root-of-the-server]__07iyl1_._.js +3 -0
  120. package/.next/server/chunks/ssr/{[root-of-the-server]__0rnjkw2._.js → [root-of-the-server]__0g__u45._.js} +2 -2
  121. package/.next/server/chunks/ssr/[root-of-the-server]__17h1ggo._.js +1 -1
  122. package/.next/server/chunks/ssr/[root-of-the-server]__1jyeu61._.js +1 -1
  123. package/.next/server/chunks/ssr/_0-i6wdi._.js +3 -0
  124. package/.next/server/chunks/ssr/{_1k0efi3._.js → _00g5sq-._.js} +2 -2
  125. package/.next/server/chunks/ssr/{_0ncuqct._.js → _03akcjz._.js} +2 -2
  126. package/.next/server/chunks/ssr/{_0l1-xc6._.js → _06xy39t._.js} +2 -2
  127. package/.next/server/chunks/ssr/_088vzgn._.js +3 -0
  128. package/.next/server/chunks/ssr/_0fo4552._.js +7 -7
  129. package/.next/server/chunks/ssr/_0r0g9ki._.js +1 -1
  130. package/.next/server/chunks/ssr/{_1dqaksc._.js → _0sefgiu._.js} +2 -2
  131. package/.next/server/chunks/ssr/_0v9r8g2._.js +3 -0
  132. package/.next/server/chunks/ssr/_0x8ophf._.js +1 -1
  133. package/.next/server/chunks/ssr/{_0w8r7o7._.js → _1-41lck._.js} +2 -2
  134. package/.next/server/chunks/ssr/{_11pnnvd._.js → _113ukfo._.js} +3 -3
  135. package/.next/server/chunks/ssr/_12p8f9_._.js +3 -0
  136. package/.next/server/chunks/ssr/{_10fwcz_._.js → _14dfh7a._.js} +2 -2
  137. package/.next/server/chunks/ssr/{_1f6s34p._.js → _1_gwau0._.js} +2 -2
  138. package/.next/server/chunks/ssr/{_0wupyvd._.js → _1hn3szq._.js} +2 -2
  139. package/.next/server/chunks/ssr/_1neoj22._.js +1 -1
  140. package/.next/server/chunks/ssr/{_17ble8k._.js → _1o7a11b._.js} +3 -3
  141. package/.next/server/chunks/ssr/{_0y28gl3._.js → _1swhm5m._.js} +3 -3
  142. package/.next/server/chunks/ssr/_1uij_rj._.js +1 -1
  143. package/.next/server/chunks/ssr/_1umbr78._.js +1 -1
  144. package/.next/server/chunks/ssr/_1w9w1ok._.js +1 -1
  145. package/.next/server/chunks/ssr/_1wac78_._.js +3 -0
  146. package/.next/server/chunks/ssr/{_1z586yw._.js → _20ywznt._.js} +2 -2
  147. package/.next/server/chunks/ssr/features_0796o8r._.js +1 -1
  148. package/.next/server/chunks/ssr/{features_bot_1k25jdg._.js → features_bot_0j4h9qu._.js} +2 -2
  149. package/.next/server/chunks/ssr/features_bot_1knyan7._.js +3 -0
  150. package/.next/server/chunks/ssr/{features_config_11dpa7r._.js → features_config_1xrlh3l._.js} +3 -3
  151. package/.next/server/chunks/ssr/features_connectors_components_mcp-setting_tsx_0jmbgvp._.js +1 -1
  152. package/.next/server/chunks/ssr/features_memory_components_memory-setting_tsx_0oa43sw._.js +1 -1
  153. package/.next/server/chunks/ssr/features_routine_components_routine-setting_tsx_055fknz._.js +1 -1
  154. package/.next/server/chunks/ssr/features_workspace_workspace_ts_1wc7_u_._.js +4 -4
  155. package/.next/server/chunks/ssr/{node_modules_1r5o-bo._.js → node_modules_00kqq8n._.js} +1 -1
  156. package/.next/server/chunks/ssr/{node_modules_0u24mg0._.js → node_modules_04o9dd2._.js} +1 -1
  157. package/.next/server/chunks/ssr/{node_modules_0mark4f._.js → node_modules_05jatne._.js} +1 -1
  158. package/.next/server/chunks/ssr/{node_modules_0bas34k._.js → node_modules_06rkzq5._.js} +1 -1
  159. package/.next/server/chunks/ssr/{node_modules_0y5uubt._.js → node_modules_07w6r9n._.js} +1 -1
  160. package/.next/server/chunks/ssr/{node_modules_0ka8te1._.js → node_modules_08ld3lm._.js} +1 -1
  161. package/.next/server/chunks/ssr/{node_modules_0n1js5k._.js → node_modules_0ft86sp._.js} +1 -1
  162. package/.next/server/chunks/ssr/{node_modules_156m1qh._.js → node_modules_0g0lr19._.js} +1 -1
  163. package/.next/server/chunks/ssr/{node_modules_0bbwe-u._.js → node_modules_0kg-vd-._.js} +1 -1
  164. package/.next/server/chunks/ssr/{node_modules_0x_2io2._.js → node_modules_0nl9i29._.js} +1 -1
  165. package/.next/server/chunks/ssr/{node_modules_18yn_bs._.js → node_modules_0p0xf5f._.js} +1 -1
  166. package/.next/server/chunks/ssr/{node_modules_0-8r9yw._.js → node_modules_0pyepmf._.js} +1 -1
  167. package/.next/server/chunks/ssr/{node_modules_1dzye0n._.js → node_modules_0vg-4fo._.js} +1 -1
  168. package/.next/server/chunks/ssr/{node_modules_1xua-g1._.js → node_modules_0y5upun._.js} +1 -1
  169. package/.next/server/chunks/ssr/{node_modules_1o9n7_n._.js → node_modules_13seln7._.js} +1 -1
  170. package/.next/server/chunks/ssr/{node_modules_1ozyial._.js → node_modules_15vsfpv._.js} +1 -1
  171. package/.next/server/chunks/ssr/{node_modules_0sdjzae._.js → node_modules_17azpeb._.js} +1 -1
  172. package/.next/server/chunks/ssr/{node_modules_1zkzksd._.js → node_modules_1_il5l7._.js} +1 -1
  173. package/.next/server/chunks/ssr/{node_modules_0-ew8f8._.js → node_modules_1aeg65a._.js} +1 -1
  174. package/.next/server/chunks/ssr/{node_modules_0etz1i0._.js → node_modules_1d73q89._.js} +1 -1
  175. package/.next/server/chunks/ssr/{node_modules_0ld7p-1._.js → node_modules_1iq8zjs._.js} +1 -1
  176. package/.next/server/chunks/ssr/{node_modules_138kcrz._.js → node_modules_1mcsva4._.js} +1 -1
  177. package/.next/server/chunks/ssr/{node_modules_0uibk9z._.js → node_modules_1mhg5br._.js} +1 -1
  178. package/.next/server/chunks/ssr/{node_modules_1tj7_9z._.js → node_modules_1mqisle._.js} +1 -1
  179. package/.next/server/chunks/ssr/{node_modules_02uwbwp._.js → node_modules_1va4lz9._.js} +1 -1
  180. package/.next/server/chunks/ssr/{node_modules_097cl9f._.js → node_modules_207ay1p._.js} +1 -1
  181. package/.next/server/chunks/ssr/{node_modules_@mermaid-js_parser_dist_chunks_mermaid-parser_core_133hjui._.js → node_modules_@mermaid-js_parser_dist_chunks_mermaid-parser_core_0hefprb._.js} +1 -1
  182. package/.next/server/chunks/ssr/{node_modules_@mermaid-js_parser_dist_chunks_mermaid-parser_core_1b3vwd3._.js → node_modules_@mermaid-js_parser_dist_chunks_mermaid-parser_core_1uuvrfp._.js} +1 -1
  183. package/.next/server/middleware-build-manifest.js +3 -3
  184. package/.next/server/pages/404.html +1 -1
  185. package/.next/server/pages/500.html +1 -1
  186. package/.next/server/server-reference-manifest.js +1 -1
  187. package/.next/server/server-reference-manifest.json +61 -61
  188. package/.next/static/chunks/04toe_3nqfjqt.js +1 -0
  189. package/.next/static/chunks/{245_cp14iz637.js → 0780wmy0_rva8.js} +1 -1
  190. package/.next/static/chunks/{314szf8yer-3n.js → 09opsnra7k4k0.js} +2 -2
  191. package/.next/static/chunks/{42m371fn687r5.js → 0if7xqoj3ysfj.js} +1 -1
  192. package/.next/static/chunks/{0609ejkwuy4sf.js → 1-q6so9vze75d.js} +2 -2
  193. package/.next/static/chunks/{23tx-2paw9qa6.js → 106jbz6qd0c5l.js} +1 -1
  194. package/.next/static/chunks/19wkcgzetkh6-.js +1 -0
  195. package/.next/static/chunks/{39miwvg4k-b2d.js → 1_me_-lnpwdfd.js} +1 -1
  196. package/.next/static/chunks/{2oa4gpkzjtpni.js → 1mkw_05uc8_ft.js} +1 -1
  197. package/.next/static/chunks/1njjf8bfkg4_v.js +1 -0
  198. package/.next/static/chunks/{1ub9obhgfm4jl.js → 25ciqm-ee14nf.js} +1 -1
  199. package/.next/static/chunks/{2xjr5b-yk_h3o.js → 29laj9_xhx0d1.js} +1 -1
  200. package/.next/static/chunks/{2jbjyazqfqyoh.js → 2b306ek_blfp1.js} +1 -1
  201. package/.next/static/chunks/2mpqmpf8wcst1.js +15 -0
  202. package/.next/static/chunks/{3ro66d9dc70bc.js → 2rpyc6mevfhu6.js} +1 -1
  203. package/.next/static/chunks/{0cl_fgkphk1oz.js → 2wj77vx62ng3n.js} +1 -1
  204. package/.next/static/chunks/{3j-l8un0d77v7.js → 2y2v3bk65fdle.js} +3 -3
  205. package/.next/static/chunks/{3vwjs5kq11417.js → 32oho7re_n3dq.js} +1 -1
  206. package/.next/static/chunks/388l-zszwa_jf.js +1 -0
  207. package/.next/static/chunks/{0q4ws7qd21chx.js → 3_s5svfgu9kl_.js} +3 -3
  208. package/.next/static/chunks/3bwrvpjk95_x4.js +1 -0
  209. package/.next/static/chunks/{07vcuxousbmy1.js → 3suz18umxycuk.js} +1 -1
  210. package/.next/static/chunks/{0unonxbajyknw.js → 44n-w1bs83ejz.js} +1 -1
  211. package/README.md +12 -12
  212. package/guide/bots.md +48 -8
  213. package/guide/calls.md +13 -12
  214. package/guide/setup.md +4 -2
  215. package/package.json +1 -1
  216. package/public/sounds/NOTICE +3 -0
  217. package/public/sounds/opening-land.ogg +0 -0
  218. package/public/sounds/opening-rise.ogg +0 -0
  219. package/public/sounds/opening-step-1.ogg +0 -0
  220. package/public/sounds/opening-step-2.ogg +0 -0
  221. package/public/sounds/opening-step-3.ogg +0 -0
  222. package/skills/README.md +3 -0
  223. package/skills/artifact/SKILL.md +6 -5
  224. package/skills/artifact/references/book.md +23 -8
  225. package/skills/artifact/references/deck.md +4 -2
  226. package/skills/artifact/references/document.md +9 -0
  227. package/skills/artifact/references/sheet.md +113 -0
  228. package/skills/artifact/runtime/book/book.css +75 -0
  229. package/skills/artifact/runtime/book/book.html +6 -3
  230. package/skills/artifact/runtime/book/book.js +21 -0
  231. package/skills/artifact/runtime/canvas/canvas.css +1 -1
  232. package/skills/artifact/runtime/canvas/canvas.js +11 -1
  233. package/skills/artifact/runtime/deck/deck.css +164 -1
  234. package/skills/artifact/runtime/deck/deck.js +76 -1
  235. package/skills/artifact/runtime/document/docx.js +631 -0
  236. package/skills/artifact/runtime/document/markdown.mjs +58 -2
  237. package/skills/artifact/runtime/document/quick.css +66 -0
  238. package/skills/artifact/runtime/document/quick.html +1 -0
  239. package/skills/artifact/runtime/document/quick.js +154 -0
  240. package/skills/artifact/runtime/document/word.mjs +65 -0
  241. package/skills/artifact/runtime/sheet/format.mjs +392 -0
  242. package/skills/artifact/runtime/sheet/formula.mjs +755 -0
  243. package/skills/artifact/runtime/sheet/sheet.css +499 -0
  244. package/skills/artifact/runtime/sheet/sheet.html +102 -0
  245. package/skills/artifact/runtime/sheet/sheet.js +1361 -0
  246. package/skills/artifact/runtime/sheet/xlsx.mjs +320 -0
  247. package/skills/artifact/runtime/shell/head.html +1 -1
  248. package/skills/artifact/runtime/shell/shell.css +3 -0
  249. package/skills/artifact/runtime/shell/wear.mjs +47 -1
  250. package/skills/artifact/runtime/vendor/fflate.LICENSE.md +23 -0
  251. package/skills/artifact/runtime/vendor/fflate.mjs +2710 -0
  252. package/skills/artifact/scripts/book.mjs +160 -11
  253. package/skills/artifact/scripts/chart.mjs +229 -11
  254. package/skills/artifact/scripts/document.mjs +29 -2
  255. package/skills/artifact/scripts/spreadsheet.mjs +1165 -0
  256. package/skills/data-report/SKILL.md +8 -1
  257. package/.next/server/chunks/[root-of-the-server]__0aijj66._.js +0 -3
  258. package/.next/server/chunks/[root-of-the-server]__0tvoct-._.js +0 -3
  259. package/.next/server/chunks/[root-of-the-server]__118qjk_._.js +0 -7
  260. package/.next/server/chunks/[root-of-the-server]__1b8tp42._.js +0 -5
  261. package/.next/server/chunks/[root-of-the-server]__1gzcbda._.js +0 -3
  262. package/.next/server/chunks/[root-of-the-server]__1qcn1ti._.js +0 -7
  263. package/.next/server/chunks/_0xx3ryc._.js +0 -7
  264. package/.next/server/chunks/ssr/[root-of-the-server]__1y5_1cv._.js +0 -3
  265. package/.next/server/chunks/ssr/_0-ou1b0._.js +0 -3
  266. package/.next/server/chunks/ssr/_1g48r6t._.js +0 -3
  267. package/.next/server/chunks/ssr/_1u5vsy-._.js +0 -3
  268. package/.next/server/chunks/ssr/features_bot_1s_-2vj._.js +0 -3
  269. package/.next/server/chunks/ssr/lib_utils_ts_0_f1xvm._.js +0 -3
  270. package/.next/static/chunks/00d54fcfm-lko.js +0 -1
  271. package/.next/static/chunks/0gjlsu6vrfzod.js +0 -1
  272. package/.next/static/chunks/17yqt1p_svv13.js +0 -1
  273. package/.next/static/chunks/203pbwecfs3tm.js +0 -1
  274. package/.next/static/chunks/3ghvoe_mlljzs.js +0 -15
  275. package/.next/static/chunks/3gn7dubb4-pwy.js +0 -1
  276. /package/.next/static/{RtLRT9EEaGn-OqwpMFdYg → SNCDmWKeIrXB6yqnwzClS}/_buildManifest.js +0 -0
  277. /package/.next/static/{RtLRT9EEaGn-OqwpMFdYg → SNCDmWKeIrXB6yqnwzClS}/_clientMiddlewareManifest.js +0 -0
  278. /package/.next/static/{RtLRT9EEaGn-OqwpMFdYg → SNCDmWKeIrXB6yqnwzClS}/_ssgManifest.js +0 -0
@@ -0,0 +1,1361 @@
1
+ // What a sheet does in the page: its tabs, cells picked with the mouse or the arrow keys (the
2
+ // formula line shows the one the ring is on, the foot the sum of the rest), a column sorted or
3
+ // filtered from its heading, the totals worked out again over the rows in view, and the sheet
4
+ // copied or saved as CSV. Sorting and filtering are a view. Edit changes the workbook itself:
5
+ // cells, the header's names, rows and columns put in or taken out with the formulas moved as
6
+ // Excel moves them, all worked out again here (formula.mjs), and kept by the app into the page
7
+ // and the .xlsx beside it (spreadsheet.mjs sync).
8
+ (() => {
9
+ const source = document.getElementById("sheet-data");
10
+ let book = null;
11
+ try {
12
+ book = JSON.parse(source?.textContent || "null");
13
+ } catch {}
14
+ if (!Array.isArray(book?.sheets)) return;
15
+
16
+ const table = document.getElementById("table");
17
+ const grid = document.getElementById("grid");
18
+ const tabs = document.getElementById("tabs");
19
+ const ref = document.getElementById("ref");
20
+ const formula = document.getElementById("formula");
21
+ const stat = document.getElementById("stat");
22
+ const size = document.getElementById("size");
23
+ const more = document.getElementById("more");
24
+ const menu = document.getElementById("menu");
25
+ const find = document.getElementById("find");
26
+ const picks = document.getElementById("picks");
27
+ const tools = document.getElementById("tools");
28
+ const formatPick = document.getElementById("format");
29
+ const note = document.getElementById("note");
30
+ const fxBox = document.getElementById("fx-box");
31
+ const fxShow = document.getElementById("fx-show");
32
+
33
+ /** The most rows drawn at once; the .xlsx holds the rest. */
34
+ const MOST = 10000;
35
+ const colName = (index) => {
36
+ let name = "";
37
+ for (let n = index + 1; n > 0; n = Math.floor((n - 1) / 26))
38
+ name = String.fromCharCode(65 + ((n - 1) % 26)) + name;
39
+ return name;
40
+ };
41
+ const isNumber = (v) => typeof v === "number";
42
+ const shown = (v, column) => formatValue(v, column?.format);
43
+ // A format's colour in the shell's own, so it reads in light and dark alike
44
+ const COLORS = {
45
+ Red: "var(--sh-bad)",
46
+ Green: "var(--sh-good)",
47
+ Blue: "var(--sh-brand)",
48
+ Black: "var(--sh-ink)",
49
+ };
50
+ const paintColor = (el, v, code) => {
51
+ const color = formatColor(v, code);
52
+ if (color) el.style.color = COLORS[color];
53
+ };
54
+ /** A value as it is copied or typed: a date, in a column of dates, as YYYY-MM-DD. */
55
+ const plain = (v, column) =>
56
+ isNumber(v) && isDateFormat(column?.format) ? isoOf(v) : v;
57
+ const figure = new Intl.NumberFormat(undefined, {
58
+ maximumFractionDigits: 2,
59
+ });
60
+
61
+ // Each sheet's view: its sort, its filters, the cells picked
62
+ const views = book.sheets.map(() => ({
63
+ sort: null, // { c, dir: 1 | -1 }
64
+ filters: new Map(), // column → the shown values kept
65
+ pick: { r1: 0, c1: 0, r2: 0, c2: 0 }, // rows counted in view: -1 the header
66
+ }));
67
+ let at = 0;
68
+ const sheet = () => book.sheets[at];
69
+ const view = () => views[at];
70
+
71
+ /** The rows in view, by their index in the sheet, sorted and filtered. */
72
+ const order = () => {
73
+ const { rows, columns } = sheet();
74
+ const { sort, filters } = view();
75
+ let list = rows.map((_, i) => i);
76
+ for (const [c, kept] of filters)
77
+ list = list.filter((i) =>
78
+ kept.has(shown(rows[i][c]?.v ?? null, columns[c])),
79
+ );
80
+ if (sort) {
81
+ const collator = new Intl.Collator(undefined, { numeric: true });
82
+ list.sort((a, b) => {
83
+ const [x, y] = [rows[a][sort.c]?.v ?? null, rows[b][sort.c]?.v ?? null];
84
+ // Empty cells stay last whichever way it is sorted
85
+ if (x === null || y === null)
86
+ return x === y ? a - b : x === null ? 1 : -1;
87
+ const c =
88
+ isNumber(x) && isNumber(y)
89
+ ? x - y
90
+ : isNumber(x) !== isNumber(y)
91
+ ? isNumber(x)
92
+ ? -1
93
+ : 1
94
+ : collator.compare(String(x), String(y));
95
+ return c * sort.dir || a - b;
96
+ });
97
+ }
98
+ return list;
99
+ };
100
+
101
+ /** A totals cell worked out over the rows in view, as Excel's SUBTOTAL does over a filter. */
102
+ const total = (fn, c, list) => {
103
+ const values = list.map((i) => sheet().rows[i][c]?.v).filter(isNumber);
104
+ if (fn === "count") return values.length;
105
+ if (!values.length) return fn === "sum" ? 0 : null;
106
+ if (fn === "sum")
107
+ return Number(values.reduce((s, n) => s + n, 0).toPrecision(15));
108
+ if (fn === "average")
109
+ return Number(
110
+ (values.reduce((s, n) => s + n, 0) / values.length).toPrecision(15),
111
+ );
112
+ return fn === "min" ? Math.min(...values) : Math.max(...values);
113
+ };
114
+
115
+ const cell = (tag, text, className, r, c) => {
116
+ const el = document.createElement(tag);
117
+ if (className) el.className = className;
118
+ el.textContent = text;
119
+ if (r !== undefined) {
120
+ el.dataset.r = r;
121
+ el.dataset.c = c;
122
+ }
123
+ return el;
124
+ };
125
+ const FUNNEL =
126
+ '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M3 5h18l-7 8v6l-4-2v-4Z"/></svg>';
127
+ const ARROW_UP =
128
+ '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 19V5M6 11l6-6 6 6"/></svg>';
129
+ const ARROW_DOWN =
130
+ '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 5v14M6 13l6 6 6-6"/></svg>';
131
+ const CHEVRON =
132
+ '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="m7 10 5 5 5-5"/></svg>';
133
+
134
+ let list = [];
135
+ const draw = () => {
136
+ const { columns, rows, totals } = sheet();
137
+ const { sort, filters } = view();
138
+ list = order();
139
+ table.replaceChildren();
140
+
141
+ const widths = document.createElement("colgroup");
142
+ widths.append(
143
+ Object.assign(document.createElement("col"), { style: "width:48px" }),
144
+ );
145
+ let wide = 48;
146
+ for (const column of columns) {
147
+ const col = document.createElement("col");
148
+ const px = Math.round(Math.max(4, column.width) * 7.2 + 18);
149
+ col.style.width = `${px}px`;
150
+ wide += px;
151
+ widths.append(col);
152
+ }
153
+ // Its whole width, so a narrow window scrolls it rather than squeezing every column
154
+ table.style.width = `${wide}px`;
155
+
156
+ const head = document.createElement("thead");
157
+ const letters = document.createElement("tr");
158
+ letters.className = "ss-letters";
159
+ letters.append(cell("th", "", "ss-row"));
160
+ columns.forEach((_, c) =>
161
+ letters.append(cell("th", colName(c), "", "L", c)),
162
+ );
163
+ const names = document.createElement("tr");
164
+ names.className = "ss-names";
165
+ names.append(cell("th", "1", "ss-row", -1, -1));
166
+ columns.forEach((column, c) => {
167
+ const th = cell("th", "", column.num ? "num" : "", -1, c);
168
+ const name = document.createElement("span");
169
+ name.className = "ss-name";
170
+ const words = document.createElement("span");
171
+ words.textContent = column.name;
172
+ const button = document.createElement("button");
173
+ button.type = "button";
174
+ button.className = `ss-col${sort?.c === c || filters.has(c) ? " on" : ""}`;
175
+ button.dataset.col = c;
176
+ button.setAttribute("aria-label", `Sort or filter ${column.name}`);
177
+ button.setAttribute("aria-haspopup", "dialog");
178
+ button.setAttribute("aria-expanded", "false");
179
+ button.innerHTML = filters.has(c)
180
+ ? FUNNEL
181
+ : sort?.c === c
182
+ ? sort.dir > 0
183
+ ? ARROW_UP
184
+ : ARROW_DOWN
185
+ : CHEVRON;
186
+ name.append(words, button);
187
+ th.append(name);
188
+ names.append(th);
189
+ });
190
+ head.append(letters, names);
191
+
192
+ const body = document.createElement("tbody");
193
+ const drawn = list.slice(0, MOST);
194
+ drawn.forEach((i, r) => {
195
+ const tr = document.createElement("tr");
196
+ tr.append(cell("th", String(i + 2), "ss-row", r, -1));
197
+ columns.forEach((column, c) => {
198
+ const v = rows[i][c]?.v ?? null;
199
+ const error = v !== null && typeof v === "object";
200
+ const td = cell(
201
+ "td",
202
+ shown(v, column),
203
+ error ? "err" : isNumber(v) ? "num" : "",
204
+ r,
205
+ c,
206
+ );
207
+ paintColor(td, v, column.format);
208
+ if (rows[i][c]?.f !== undefined) td.classList.add("fx");
209
+ tr.append(td);
210
+ });
211
+ body.append(tr);
212
+ });
213
+ table.append(widths, head, body);
214
+
215
+ if (totals) {
216
+ const foot = document.createElement("tfoot");
217
+ const tr = document.createElement("tr");
218
+ tr.append(cell("th", String(rows.length + 2), "ss-row", "T", -1));
219
+ columns.forEach((column, c) => {
220
+ const fn = totals.cells[c];
221
+ const v = fn ? total(fn, c, list) : c === 0 ? totals.label : null;
222
+ const td = cell(
223
+ "td",
224
+ fn ? shown(v, fn === "count" ? null : column) : (v ?? ""),
225
+ fn ? "num" : "",
226
+ "T",
227
+ c,
228
+ );
229
+ if (fn && fn !== "count") paintColor(td, v, column.format);
230
+ tr.append(td);
231
+ });
232
+ foot.append(tr);
233
+ table.append(foot);
234
+ }
235
+
236
+ more.hidden = list.length <= MOST;
237
+ more.textContent = `The first ${MOST.toLocaleString()} of ${list.length.toLocaleString()} rows are shown; the .xlsx holds them all.`;
238
+ const filtered = filters.size
239
+ ? ` · ${list.length.toLocaleString()} shown`
240
+ : "";
241
+ size.textContent = `${rows.length.toLocaleString()} rows${filtered}`;
242
+ tabs.querySelectorAll("button").forEach((one, i) => {
243
+ one.setAttribute("aria-selected", String(i === at));
244
+ one.tabIndex = i === at ? 0 : -1;
245
+ });
246
+ paint();
247
+ };
248
+
249
+ /* ── picking cells ──────────────────────────────────────────────────────── */
250
+
251
+ const clampPick = (p) => {
252
+ const last = Math.min(list.length, MOST) - 1;
253
+ const width = sheet().columns.length - 1;
254
+ const fit = (r) => Math.max(-1, Math.min(last, r));
255
+ const fitC = (c) => Math.max(0, Math.min(width, c));
256
+ return { r1: fit(p.r1), c1: fitC(p.c1), r2: fit(p.r2), c2: fitC(p.c2) };
257
+ };
258
+ const inside = (p, r, c) =>
259
+ r >= Math.min(p.r1, p.r2) &&
260
+ r <= Math.max(p.r1, p.r2) &&
261
+ c >= Math.min(p.c1, p.c2) &&
262
+ c <= Math.max(p.c1, p.c2);
263
+
264
+ /** The raw value at a picked position: the header's name, or a row's cell. */
265
+ const valueAt = (r, c) =>
266
+ r === -1
267
+ ? sheet().columns[c].name
268
+ : (sheet().rows[list[r]]?.[c]?.v ?? null);
269
+ /** The value at a picked position as it is copied: a date as YYYY-MM-DD. */
270
+ const plainAt = (r, c) =>
271
+ r === -1 ? valueAt(r, c) : plain(valueAt(r, c), sheet().columns[c]);
272
+
273
+ const paint = () => {
274
+ const p = view().pick;
275
+ for (const el of table.querySelectorAll(".in, .at"))
276
+ el.classList.remove("in", "at");
277
+ for (const el of table.querySelectorAll("[data-r]")) {
278
+ const { r, c } = el.dataset;
279
+ if (r === "L" || r === "T") {
280
+ if (
281
+ r === "L" &&
282
+ Number(c) >= Math.min(p.c1, p.c2) &&
283
+ Number(c) <= Math.max(p.c1, p.c2)
284
+ )
285
+ el.classList.add("in");
286
+ continue;
287
+ }
288
+ const [row, col] = [Number(r), Number(c)];
289
+ if (col === -1) {
290
+ if (row >= Math.min(p.r1, p.r2) && row <= Math.max(p.r1, p.r2))
291
+ el.classList.add("in");
292
+ continue;
293
+ }
294
+ if (inside(p, row, col))
295
+ el.classList.add(row === p.r1 && col === p.c1 ? "at" : "in");
296
+ }
297
+ // The cell the ring is on, as Excel names it, with its formula or its value
298
+ const excelRow = p.r1 === -1 ? 1 : list[p.r1] + 2;
299
+ ref.textContent = `${colName(p.c1)}${excelRow}`;
300
+ const picked = p.r1 === -1 ? null : sheet().rows[list[p.r1]]?.[p.c1];
301
+ const v = plainAt(p.r1, p.c1);
302
+ formula.value =
303
+ picked?.f ??
304
+ (v === null
305
+ ? ""
306
+ : typeof v === "object"
307
+ ? v.error
308
+ : typeof v === "boolean"
309
+ ? v
310
+ ? "TRUE"
311
+ : "FALSE"
312
+ : String(v));
313
+ // What the picked cells add up to, when more than one is picked
314
+ const numbers = [];
315
+ let filled = 0;
316
+ for (let r = Math.min(p.r1, p.r2); r <= Math.max(p.r1, p.r2); r++)
317
+ for (let c = Math.min(p.c1, p.c2); c <= Math.max(p.c1, p.c2); c++) {
318
+ const one = valueAt(r, c);
319
+ if (one === null || one === "") continue;
320
+ filled++;
321
+ if (isNumber(one) && r !== -1) numbers.push(one);
322
+ }
323
+ stat.replaceChildren();
324
+ if (filled > 1) {
325
+ const part = (label, value) => {
326
+ const span = document.createElement("span");
327
+ const b = document.createElement("b");
328
+ b.textContent = value;
329
+ span.append(`${label} `, b);
330
+ stat.append(span);
331
+ };
332
+ if (numbers.length) {
333
+ const sum = Number(numbers.reduce((s, n) => s + n, 0).toPrecision(15));
334
+ part("Sum", figure.format(sum));
335
+ part("Average", figure.format(sum / numbers.length));
336
+ }
337
+ part("Count", filled.toLocaleString());
338
+ }
339
+ if (editing && !typing) {
340
+ place();
341
+ drawFormat();
342
+ }
343
+ showRefs(picked?.f ?? null);
344
+ };
345
+
346
+ /* ── what a formula names ───────────────────────────────────────────────── */
347
+
348
+ /** A colour for each reference a formula names, as Excel gives them. */
349
+ const REF_COLORS = [
350
+ "#2563eb",
351
+ "#dc2626",
352
+ "#16a34a",
353
+ "#9333ea",
354
+ "#ea580c",
355
+ "#0891b2",
356
+ ];
357
+ let marked = [];
358
+ /**
359
+ * The cells `text` names on this sheet, each reference edged in its colour, and the formula
360
+ * line with its references in the same colours. Null clears them.
361
+ */
362
+ function showRefs(text) {
363
+ for (const el of marked) {
364
+ el.removeAttribute("data-ref");
365
+ el.removeAttribute("style");
366
+ if (el.dataset.color) el.style.color = el.dataset.color;
367
+ }
368
+ marked = [];
369
+ fxShow.replaceChildren();
370
+ fxBox.classList.remove("colored");
371
+ if (!text || !String(text).startsWith("=")) return;
372
+ const refs = Formula.referencesIn(text);
373
+ if (!refs.length) return;
374
+ const colors = new Map();
375
+ const colorOf = (ref) => {
376
+ const key = text
377
+ .slice(ref.start, ref.end)
378
+ .replaceAll("$", "")
379
+ .toUpperCase();
380
+ if (!colors.has(key))
381
+ colors.set(key, REF_COLORS[colors.size % REF_COLORS.length]);
382
+ return colors.get(key);
383
+ };
384
+ // The line: the formula's text, each reference in its colour
385
+ let from = 0;
386
+ for (const ref of refs) {
387
+ fxShow.append(text.slice(from, ref.start));
388
+ const b = document.createElement("b");
389
+ b.style.color = colorOf(ref);
390
+ b.textContent = text.slice(ref.start, ref.end);
391
+ fxShow.append(b);
392
+ from = ref.end;
393
+ }
394
+ fxShow.append(text.slice(from));
395
+ if (document.activeElement !== formula) fxBox.classList.add("colored");
396
+ // The cells: where each reference on this sheet lands among the rows drawn
397
+ const here = sheet().name.toLowerCase();
398
+ const { rows } = sheet();
399
+ const excelRow = (r) =>
400
+ r === "-1" ? 0 : r === "T" ? rows.length + 1 : list[Number(r)] + 1;
401
+ const boxes = refs
402
+ .filter((ref) => (ref.sheet ?? sheet().name).toLowerCase() === here)
403
+ .map((ref) => {
404
+ const b = ref.b ?? ref.a;
405
+ return {
406
+ color: colorOf(ref),
407
+ top: Math.min(ref.a.r ?? 0, b.r ?? 0),
408
+ bottom:
409
+ ref.a.r === null || b.r === null
410
+ ? Infinity
411
+ : Math.max(ref.a.r, b.r),
412
+ left: Math.min(ref.a.c, b.c),
413
+ right: Math.max(ref.a.c, b.c),
414
+ };
415
+ });
416
+ if (!boxes.length) return;
417
+ const lastRow = rows.length + (sheet().totals ? 1 : 0);
418
+ for (const el of table.querySelectorAll(
419
+ "td[data-r], .ss-names th[data-r]",
420
+ )) {
421
+ const c = Number(el.dataset.c);
422
+ if (c < 0) continue;
423
+ const r = excelRow(el.dataset.r);
424
+ const box = boxes.find(
425
+ (one) =>
426
+ r >= one.top && r <= one.bottom && c >= one.left && c <= one.right,
427
+ );
428
+ if (!box) continue;
429
+ if (el.style.color) el.dataset.color = el.style.color;
430
+ el.dataset.ref = "";
431
+ el.style.setProperty("--ref", box.color);
432
+ if (r === box.top) el.style.setProperty("--rt", "2px");
433
+ if (r === Math.min(box.bottom, lastRow))
434
+ el.style.setProperty("--rb", "2px");
435
+ if (c === box.left) el.style.setProperty("--rl", "2px");
436
+ if (c === box.right) el.style.setProperty("--rr", "2px");
437
+ marked.push(el);
438
+ }
439
+ }
440
+
441
+ const pickTo = (r, c, extend) => {
442
+ const p = view().pick;
443
+ view().pick = clampPick(
444
+ extend ? { ...p, r2: r, c2: c } : { r1: r, c1: c, r2: r, c2: c },
445
+ );
446
+ paint();
447
+ closeNote();
448
+ const target = table.querySelector(
449
+ `[data-r="${view().pick.r2}"][data-c="${view().pick.c2}"]`,
450
+ );
451
+ target?.scrollIntoView({ block: "nearest", inline: "nearest" });
452
+ };
453
+
454
+ let dragging = false;
455
+ table.addEventListener("mousedown", (event) => {
456
+ if (event.target.closest(".ss-col")) return;
457
+ const el = event.target.closest("[data-r]");
458
+ if (!el || el.dataset.r === "T") return;
459
+ const { r, c } = el.dataset;
460
+ if (typing) finish(true);
461
+ // The grid would take the focus after this, from the editor that types into the cell
462
+ event.preventDefault();
463
+ focusGrid();
464
+ if (r === "L") {
465
+ // A column letter picks the whole column
466
+ const last = Math.min(list.length, MOST) - 1;
467
+ view().pick = clampPick({
468
+ r1: -1,
469
+ c1: Number(c),
470
+ r2: last,
471
+ c2: Number(c),
472
+ });
473
+ paint();
474
+ return;
475
+ }
476
+ if (Number(c) === -1) {
477
+ const width = sheet().columns.length - 1;
478
+ view().pick = clampPick({
479
+ r1: Number(r),
480
+ c1: 0,
481
+ r2: Number(r),
482
+ c2: width,
483
+ });
484
+ paint();
485
+ return;
486
+ }
487
+ pickTo(Number(r), Number(c), event.shiftKey);
488
+ dragging = true;
489
+ });
490
+ table.addEventListener("mouseover", (event) => {
491
+ if (!dragging) return;
492
+ const el = event.target.closest("[data-r]");
493
+ if (!el || ["L", "T"].includes(el.dataset.r) || Number(el.dataset.c) === -1)
494
+ return;
495
+ pickTo(Number(el.dataset.r), Number(el.dataset.c), true);
496
+ });
497
+ addEventListener("mouseup", () => {
498
+ dragging = false;
499
+ });
500
+
501
+ /** Rows and columns as tab-separated text, which every spreadsheet pastes into cells. */
502
+ const tsv = (rows) =>
503
+ rows
504
+ .map((row) =>
505
+ row
506
+ .map((v) => {
507
+ const text =
508
+ v === null
509
+ ? ""
510
+ : typeof v === "object"
511
+ ? v.error
512
+ : typeof v === "boolean"
513
+ ? v
514
+ ? "TRUE"
515
+ : "FALSE"
516
+ : String(v);
517
+ return /[\t\n"]/.test(text)
518
+ ? `"${text.replaceAll('"', '""')}"`
519
+ : text;
520
+ })
521
+ .join("\t"),
522
+ )
523
+ .join("\n");
524
+ const copy = async (text) => {
525
+ try {
526
+ await navigator.clipboard.writeText(text);
527
+ } catch {
528
+ const area = Object.assign(document.createElement("textarea"), {
529
+ value: text,
530
+ });
531
+ document.body.append(area);
532
+ area.select();
533
+ document.execCommand("copy");
534
+ area.remove();
535
+ }
536
+ };
537
+
538
+ grid.addEventListener("keydown", (event) => {
539
+ const p = view().pick;
540
+ const step = {
541
+ ArrowUp: [-1, 0],
542
+ ArrowDown: [1, 0],
543
+ ArrowLeft: [0, -1],
544
+ ArrowRight: [0, 1],
545
+ }[event.key];
546
+ if (step) {
547
+ event.preventDefault();
548
+ const [r, c] = event.shiftKey ? [p.r2, p.c2] : [p.r1, p.c1];
549
+ pickTo(r + step[0], c + step[1], event.shiftKey);
550
+ return;
551
+ }
552
+ const mod = event.metaKey || event.ctrlKey;
553
+ if (mod && event.key.toLowerCase() === "c") {
554
+ event.preventDefault();
555
+ const out = [];
556
+ for (let r = Math.min(p.r1, p.r2); r <= Math.max(p.r1, p.r2); r++) {
557
+ const row = [];
558
+ for (let c = Math.min(p.c1, p.c2); c <= Math.max(p.c1, p.c2); c++)
559
+ row.push(plainAt(r, c));
560
+ out.push(row);
561
+ }
562
+ copy(tsv(out));
563
+ } else if (mod && event.key.toLowerCase() === "a") {
564
+ event.preventDefault();
565
+ view().pick = clampPick({
566
+ r1: -1,
567
+ c1: 0,
568
+ r2: list.length,
569
+ c2: sheet().columns.length,
570
+ });
571
+ paint();
572
+ }
573
+ });
574
+
575
+ /* ── a column's sort and filter ─────────────────────────────────────────── */
576
+
577
+ let open = null; // the column whose menu is open
578
+ const closeMenu = () => {
579
+ menu.hidden = true;
580
+ table
581
+ .querySelector('.ss-col[aria-expanded="true"]')
582
+ ?.setAttribute("aria-expanded", "false");
583
+ open = null;
584
+ };
585
+ const drawPicks = () => {
586
+ const { rows, columns } = sheet();
587
+ const kept = view().filters.get(open);
588
+ const counts = new Map();
589
+ for (const row of rows) {
590
+ const text = shown(row[open]?.v ?? null, columns[open]);
591
+ counts.set(text, (counts.get(text) ?? 0) + 1);
592
+ }
593
+ const word = find.value.trim().toLowerCase();
594
+ const values = [...counts.keys()].sort(
595
+ new Intl.Collator(undefined, { numeric: true }).compare,
596
+ );
597
+ picks.replaceChildren();
598
+ for (const text of values.slice(0, 500)) {
599
+ if (word && !text.toLowerCase().includes(word)) continue;
600
+ const label = document.createElement("label");
601
+ const box = Object.assign(document.createElement("input"), {
602
+ type: "checkbox",
603
+ checked: !kept || kept.has(text),
604
+ });
605
+ box.dataset.value = text;
606
+ const name = document.createElement("span");
607
+ name.textContent = text === "" ? "(empty)" : text;
608
+ const count = document.createElement("small");
609
+ count.textContent = counts.get(text).toLocaleString();
610
+ label.append(box, name, count);
611
+ picks.append(label);
612
+ }
613
+ };
614
+ table.addEventListener("click", (event) => {
615
+ const button = event.target.closest(".ss-col");
616
+ if (!button) return;
617
+ const c = Number(button.dataset.col);
618
+ if (open === c) return closeMenu();
619
+ closeMenu();
620
+ open = c;
621
+ button.setAttribute("aria-expanded", "true");
622
+ const box = button.getBoundingClientRect();
623
+ menu.hidden = false;
624
+ menu.style.top = `${box.bottom + 4}px`;
625
+ menu.style.left = `${Math.max(8, Math.min(box.left - 8, innerWidth - menu.offsetWidth - 8))}px`;
626
+ const sort = view().sort;
627
+ for (const one of menu.querySelectorAll("[data-sort]"))
628
+ one.classList.toggle(
629
+ "on",
630
+ one.dataset.sort ===
631
+ (sort?.c === c ? (sort.dir > 0 ? "up" : "down") : "none"),
632
+ );
633
+ find.value = "";
634
+ drawPicks();
635
+ });
636
+ menu.addEventListener("click", (event) => {
637
+ const sort = event.target.closest("[data-sort]")?.dataset.sort;
638
+ if (sort) {
639
+ view().sort =
640
+ sort === "none" ? null : { c: open, dir: sort === "up" ? 1 : -1 };
641
+ closeMenu();
642
+ draw();
643
+ return;
644
+ }
645
+ const pick = event.target.closest("[data-pick]")?.dataset.pick;
646
+ if (pick) {
647
+ if (pick === "all") view().filters.delete(open);
648
+ const c = open;
649
+ draw();
650
+ open = c;
651
+ drawPicks();
652
+ }
653
+ });
654
+ menu.addEventListener("change", (event) => {
655
+ if (!event.target.matches('input[type="checkbox"]')) return;
656
+ const { rows, columns } = sheet();
657
+ const all = new Set(
658
+ rows.map((row) => shown(row[open]?.v ?? null, columns[open])),
659
+ );
660
+ const kept = new Set(view().filters.get(open) ?? all);
661
+ if (event.target.checked) kept.add(event.target.dataset.value);
662
+ else kept.delete(event.target.dataset.value);
663
+ if (kept.size === all.size) view().filters.delete(open);
664
+ else view().filters.set(open, kept);
665
+ const c = open;
666
+ draw();
667
+ open = c;
668
+ table
669
+ .querySelector(`.ss-col[data-col="${c}"]`)
670
+ ?.setAttribute("aria-expanded", "true");
671
+ });
672
+ find.addEventListener("input", drawPicks);
673
+ addEventListener("mousedown", (event) => {
674
+ if (
675
+ !menu.hidden &&
676
+ !menu.contains(event.target) &&
677
+ !event.target.closest(".ss-col")
678
+ )
679
+ closeMenu();
680
+ });
681
+ addEventListener("keydown", (event) => {
682
+ if (event.key === "Escape" && !menu.hidden) closeMenu();
683
+ });
684
+
685
+ /* ── tabs, and the sheet out as text ────────────────────────────────────── */
686
+
687
+ book.sheets.forEach((one, i) => {
688
+ const button = document.createElement("button");
689
+ button.type = "button";
690
+ button.setAttribute("role", "tab");
691
+ button.textContent = one.name;
692
+ button.addEventListener("click", () => {
693
+ closeMenu();
694
+ at = i;
695
+ draw();
696
+ });
697
+ tabs.append(button);
698
+ });
699
+ tabs.addEventListener("keydown", (event) => {
700
+ const step = { ArrowLeft: -1, ArrowRight: 1 }[event.key];
701
+ if (!step) return;
702
+ at = (at + step + book.sheets.length) % book.sheets.length;
703
+ draw();
704
+ tabs.children[at].focus();
705
+ });
706
+
707
+ /** The sheet as written, every row: the header, the rows, the totals as the file has them. */
708
+ const whole = () => {
709
+ const { columns, rows, totals } = sheet();
710
+ const all = rows.map((_, i) => i);
711
+ const out = [
712
+ columns.map((column) => column.name),
713
+ ...rows.map((row) =>
714
+ columns.map((column, c) => plain(row[c]?.v ?? null, column)),
715
+ ),
716
+ ];
717
+ if (totals)
718
+ out.push(
719
+ columns.map((_, c) =>
720
+ totals.cells[c]
721
+ ? total(totals.cells[c], c, all)
722
+ : c === 0
723
+ ? totals.label
724
+ : null,
725
+ ),
726
+ );
727
+ return out;
728
+ };
729
+ document
730
+ .querySelector("[data-copy-sheet]")
731
+ ?.addEventListener("click", () => copy(tsv(whole())));
732
+ document.querySelector("[data-csv]")?.addEventListener("click", () => {
733
+ const csv = whole()
734
+ .map((row) =>
735
+ row
736
+ .map((v) => {
737
+ const text =
738
+ v === null ? "" : typeof v === "object" ? v.error : String(v);
739
+ return /[",\n]/.test(text)
740
+ ? `"${text.replaceAll('"', '""')}"`
741
+ : text;
742
+ })
743
+ .join(","),
744
+ )
745
+ .join("\r\n");
746
+ // A byte-order mark, so Excel reads the file as UTF-8 and Korean or Japanese stays whole
747
+ const url = URL.createObjectURL(new Blob([`${csv}`], { type: "text/csv" }));
748
+ const a = Object.assign(document.createElement("a"), {
749
+ href: url,
750
+ download: `${sheet().name}.csv`,
751
+ });
752
+ document.body.append(a);
753
+ a.click();
754
+ a.remove();
755
+ setTimeout(() => URL.revokeObjectURL(url), 1000);
756
+ });
757
+
758
+ /* ── editing ────────────────────────────────────────────────────────────── */
759
+
760
+ /** How each totals function is written into the .xlsx (spreadsheet.mjs TOTALS). */
761
+ const TOTALS = { sum: 109, average: 101, count: 102, max: 104, min: 105 };
762
+ /** How many changes Undo goes back through. */
763
+ const UNDO_MOST = 100;
764
+ const edits = window.shell?.edits;
765
+ let editing = false;
766
+ let typing = false; // the cell editor is open over a cell
767
+ let typingAt = null; // { s, r, c } the cell it is open over, r in view (-1 the header)
768
+ const undone = [];
769
+ const redone = [];
770
+
771
+ const editor = document.createElement("input");
772
+ editor.className = "ss-edit";
773
+ editor.spellcheck = false;
774
+ editor.autocomplete = "off";
775
+ editor.setAttribute("aria-label", "Cell");
776
+ grid.append(editor);
777
+
778
+ const say = (text, bad) => {
779
+ note.textContent = text;
780
+ note.classList.toggle("bad", Boolean(bad));
781
+ };
782
+ function closeNote() {
783
+ if (!note.classList.contains("bad")) say("");
784
+ }
785
+ function focusGrid() {
786
+ (editing ? editor : grid).focus({ preventScroll: true });
787
+ }
788
+
789
+ /** The whole book worked out again, as the .xlsx will hold it, and the columns' kind with it. */
790
+ const recompute = () => {
791
+ const sheets = book.sheets.map((one) => {
792
+ const cells = [
793
+ one.columns.map((column) => ({ v: column.name })),
794
+ ...one.rows.map((row) =>
795
+ one.columns.map((_, c) => {
796
+ const x = row[c] ?? { v: null };
797
+ return x.f !== undefined ? { f: x.f } : { v: x.v ?? null };
798
+ }),
799
+ ),
800
+ ];
801
+ if (one.totals) {
802
+ const last = Math.max(2, one.rows.length + 1);
803
+ cells.push(
804
+ one.columns.map((_, c) => {
805
+ const fn = one.totals.cells[c];
806
+ if (fn)
807
+ return {
808
+ f: `=SUBTOTAL(${TOTALS[fn]},${colName(c)}2:${colName(c)}${last})`,
809
+ };
810
+ return { v: c === 0 ? one.totals.label : null };
811
+ }),
812
+ );
813
+ }
814
+ return { name: one.name, cells };
815
+ });
816
+ const problems = [];
817
+ Formula.workOut(sheets, { problems });
818
+ sheets.forEach((worked, s) => {
819
+ const one = book.sheets[s];
820
+ one.rows = worked.cells
821
+ .slice(1, 1 + one.rows.length)
822
+ .map((row) =>
823
+ row.map((x) =>
824
+ x.f !== undefined ? { v: x.v ?? null, f: x.f } : { v: x.v ?? null },
825
+ ),
826
+ );
827
+ one.columns.forEach((column, c) => {
828
+ const values = one.rows
829
+ .map((row) => row[c]?.v ?? null)
830
+ .filter((v) => v !== null);
831
+ column.num =
832
+ values.length > 0 &&
833
+ values.filter(isNumber).length >= values.length / 2;
834
+ });
835
+ });
836
+ return problems;
837
+ };
838
+
839
+ /** One change: kept for Undo, worked out, drawn, and handed to the app to keep. */
840
+ const change = (fn, { resets } = {}) => {
841
+ const before = JSON.stringify(book.sheets);
842
+ const refused = fn();
843
+ if (refused) {
844
+ say(refused, true);
845
+ return false;
846
+ }
847
+ undone.push(before);
848
+ if (undone.length > UNDO_MOST) undone.shift();
849
+ redone.length = 0;
850
+ settle(resets);
851
+ return true;
852
+ };
853
+ const settle = (resets) => {
854
+ const problems = recompute();
855
+ // A sort or filter names columns by their place, which a column put in or taken out moves
856
+ if (resets) {
857
+ view().sort = null;
858
+ view().filters.clear();
859
+ }
860
+ draw();
861
+ view().pick = clampPick(view().pick);
862
+ paint();
863
+ say(
864
+ problems.length
865
+ ? `${problems[0]}${problems.length > 1 ? ` (+${problems.length - 1} more)` : ""}`
866
+ : "",
867
+ problems.length > 0,
868
+ );
869
+ edits?.changed();
870
+ };
871
+ const back = (from, to) => {
872
+ if (!from.length) return;
873
+ to.push(JSON.stringify(book.sheets));
874
+ book.sheets = JSON.parse(from.pop());
875
+ settle(true);
876
+ };
877
+
878
+ /** A cell's text to edit: its formula, or its value as typed. */
879
+ const rawAt = (r, c) => {
880
+ if (r === -1) return sheet().columns[c].name;
881
+ const x = sheet().rows[list[r]]?.[c];
882
+ if (!x) return "";
883
+ if (x.f !== undefined) return x.f;
884
+ const v = x.v ?? null;
885
+ if (v === null) return "";
886
+ if (typeof v === "object") return v.error;
887
+ if (typeof v === "boolean") return v ? "TRUE" : "FALSE";
888
+ if (isNumber(v) && isDateFormat(sheet().columns[c].format)) return isoOf(v);
889
+ if (isNumber(v) && /%/.test(sheet().columns[c].format ?? ""))
890
+ return `${Number((v * 100).toPrecision(12))}%`;
891
+ return String(v);
892
+ };
893
+
894
+ /** What `text` puts in a cell, or why it cannot: a formula has to read. */
895
+ const cellFrom = (text, column) => {
896
+ const typed = String(text);
897
+ if (typed.startsWith("=") && typed.trim().length > 1) {
898
+ try {
899
+ Formula.parse(typed.trim());
900
+ } catch (failed) {
901
+ return { refused: `${typed.trim()}: ${failed.message}` };
902
+ }
903
+ return { cell: { v: null, f: typed.trim() } };
904
+ }
905
+ return { cell: { v: valueOf(typed, column.format) } };
906
+ };
907
+
908
+ /** A column's name set, unless it is empty or another column has it. */
909
+ const rename = (c, text) => {
910
+ const name = String(text).trim();
911
+ if (!name) return "A column needs a name.";
912
+ const taken = sheet().columns.some(
913
+ (column, i) =>
914
+ i !== c && column.name.toLowerCase() === name.toLowerCase(),
915
+ );
916
+ if (taken) return `Another column is named "${name}".`;
917
+ sheet().columns[c].name = name;
918
+ return null;
919
+ };
920
+
921
+ /** Text put into the cell at (r, c) in view; a row past the last is a new one. */
922
+ const putAt = (r, c, text) => {
923
+ if (r === -1) return rename(c, text);
924
+ const { cell: made, refused } = cellFrom(text, sheet().columns[c]);
925
+ if (refused) return refused;
926
+ while (r >= list.length) {
927
+ addRow(sheet().rows.length);
928
+ list.push(sheet().rows.length - 1);
929
+ }
930
+ sheet().rows[list[r]][c] = made;
931
+ return null;
932
+ };
933
+
934
+ /** Every formula in the book moved as rows or columns of the sheet named move. */
935
+ const moveAll = (axis, at, count) => {
936
+ const target = sheet().name;
937
+ for (const one of book.sheets)
938
+ for (const row of one.rows)
939
+ for (const x of row)
940
+ if (x?.f !== undefined)
941
+ x.f = Formula.moveRefs(x.f, {
942
+ on: one.name,
943
+ target,
944
+ axis,
945
+ at,
946
+ count,
947
+ });
948
+ };
949
+
950
+ /**
951
+ * A row put in at `i` (a row's index in the sheet), the formulas moved round it; a column
952
+ * the row next to it works out by formula is worked out the same way in the new one.
953
+ */
954
+ const addRow = (i) => {
955
+ const { rows, columns } = sheet();
956
+ moveAll("r", i + 1, 1);
957
+ const [near, step] = i > 0 ? [rows[i - 1], 1] : [rows[i], -1];
958
+ rows.splice(
959
+ i,
960
+ 0,
961
+ columns.map((_, c) =>
962
+ near?.[c]?.f !== undefined
963
+ ? { v: null, f: Formula.fillDown(near[c].f, step) }
964
+ : { v: null },
965
+ ),
966
+ );
967
+ };
968
+
969
+ const picked = () => {
970
+ const p = view().pick;
971
+ return {
972
+ top: Math.min(p.r1, p.r2),
973
+ bottom: Math.max(p.r1, p.r2),
974
+ left: Math.min(p.c1, p.c2),
975
+ right: Math.max(p.c1, p.c2),
976
+ };
977
+ };
978
+
979
+ const act = {
980
+ row() {
981
+ const { bottom } = picked();
982
+ const i = bottom === -1 ? 0 : list[bottom] + 1;
983
+ const done = change(() => addRow(i));
984
+ if (done) {
985
+ const r = list.indexOf(i);
986
+ if (r !== -1) pickTo(r, view().pick.c1);
987
+ }
988
+ },
989
+ unrow() {
990
+ const { top, bottom } = picked();
991
+ const gone = [];
992
+ for (let r = Math.max(0, top); r <= bottom; r++) gone.push(list[r]);
993
+ if (!gone.length) return say("Pick the rows to take out.", true);
994
+ change(() => {
995
+ for (const i of gone.sort((a, b) => b - a)) {
996
+ moveAll("r", i + 1, -1);
997
+ sheet().rows.splice(i, 1);
998
+ }
999
+ });
1000
+ },
1001
+ col() {
1002
+ const c = picked().right + 1;
1003
+ let n = sheet().columns.length + 1;
1004
+ const names = new Set(
1005
+ sheet().columns.map((one) => one.name.toLowerCase()),
1006
+ );
1007
+ while (names.has(`column ${n}`.toLowerCase())) n++;
1008
+ const done = change(
1009
+ () => {
1010
+ moveAll("c", c, 1);
1011
+ const { columns, rows, totals } = sheet();
1012
+ columns.splice(c, 0, {
1013
+ name: `Column ${n}`,
1014
+ format: null,
1015
+ num: false,
1016
+ width: 12,
1017
+ });
1018
+ for (const row of rows) row.splice(c, 0, { v: null });
1019
+ totals?.cells.splice(c, 0, null);
1020
+ },
1021
+ { resets: true },
1022
+ );
1023
+ if (done) {
1024
+ pickTo(-1, c);
1025
+ openEditor(rawAt(-1, c));
1026
+ // Its made-up name picked, so the first key names it
1027
+ editor.select();
1028
+ }
1029
+ },
1030
+ uncol() {
1031
+ const { left, right } = picked();
1032
+ const { columns, totals } = sheet();
1033
+ if (right - left + 1 >= columns.length)
1034
+ return say("A sheet keeps one column at least.", true);
1035
+ if (left === 0 && totals?.cells[right + 1])
1036
+ return say(
1037
+ "The first column holds the totals' label, and the next one is totalled.",
1038
+ true,
1039
+ );
1040
+ change(
1041
+ () => {
1042
+ for (let c = right; c >= left; c--) {
1043
+ moveAll("c", c, -1);
1044
+ sheet().columns.splice(c, 1);
1045
+ for (const row of sheet().rows) row.splice(c, 1);
1046
+ totals?.cells.splice(c, 1);
1047
+ }
1048
+ },
1049
+ { resets: true },
1050
+ );
1051
+ },
1052
+ undo: () => back(undone, redone),
1053
+ redo: () => back(redone, undone),
1054
+ };
1055
+ tools.addEventListener("click", (event) => {
1056
+ const what = event.target.closest("[data-do]")?.dataset.do;
1057
+ if (!what) return;
1058
+ if (typing) finish(true);
1059
+ act[what]();
1060
+ focusGrid();
1061
+ });
1062
+
1063
+ const drawFormat = () => {
1064
+ const code = sheet().columns[view().pick.c1]?.format || "General";
1065
+ let option = [...formatPick.options].find((one) => one.value === code);
1066
+ if (!option) {
1067
+ formatPick.querySelector("[data-own]")?.remove();
1068
+ option = new Option(
1069
+ formatValue(
1070
+ isDateFormat(code) ? serialOf("2026-07-02") : -1234.5,
1071
+ code,
1072
+ ),
1073
+ code,
1074
+ );
1075
+ option.dataset.own = "";
1076
+ formatPick.append(option);
1077
+ }
1078
+ formatPick.value = code;
1079
+ };
1080
+ formatPick.addEventListener("change", () => {
1081
+ const { left, right } = picked();
1082
+ const code = formatPick.value === "General" ? null : formatPick.value;
1083
+ change(() => {
1084
+ for (let c = left; c <= right; c++) {
1085
+ sheet().columns[c].format = code;
1086
+ // Made a column of dates: the dates written in it as text become dates
1087
+ if (!isDateFormat(code)) continue;
1088
+ for (const row of sheet().rows) {
1089
+ const v = row[c]?.v;
1090
+ if (
1091
+ typeof v === "string" &&
1092
+ row[c].f === undefined &&
1093
+ serialOf(v) !== null
1094
+ )
1095
+ row[c] = { v: serialOf(v) };
1096
+ }
1097
+ }
1098
+ });
1099
+ focusGrid();
1100
+ });
1101
+
1102
+ /** The editor over the ring's cell, drawn where the cell is, or out of the way when closed. */
1103
+ const place = () => {
1104
+ if (!editing) return;
1105
+ const at = typingAt ?? { r: view().pick.r1, c: view().pick.c1 };
1106
+ const td = table.querySelector(`[data-r="${at.r}"][data-c="${at.c}"]`);
1107
+ if (!td) return;
1108
+ const g = grid.getBoundingClientRect();
1109
+ const b = td.getBoundingClientRect();
1110
+ editor.style.left = `${b.left - g.left + grid.scrollLeft}px`;
1111
+ editor.style.top = `${b.top - g.top + grid.scrollTop}px`;
1112
+ editor.dataset.base = String(Math.max(b.width, 80));
1113
+ editor.style.width = `${editor.dataset.base}px`;
1114
+ editor.style.height = `${b.height}px`;
1115
+ grow();
1116
+ };
1117
+ /** The editor as wide as what is typed in it, as Excel's is, up to the grid's right edge. */
1118
+ const grow = () => {
1119
+ if (!typing) return;
1120
+ const room = grid.scrollLeft + grid.clientWidth - editor.offsetLeft - 4;
1121
+ const base = Number(editor.dataset.base) || 80;
1122
+ editor.style.width = `${base}px`;
1123
+ editor.style.width = `${Math.max(base, Math.min(room, editor.scrollWidth + 2))}px`;
1124
+ };
1125
+
1126
+ /** The editor opened on the ring's cell, holding `text` (null keeps what was just typed). */
1127
+ function openEditor(text) {
1128
+ const p = view().pick;
1129
+ if (p.r1 === -1 && sheet().columns[p.c1] === undefined) return;
1130
+ typing = true;
1131
+ typingAt = { s: at, r: p.r1, c: p.c1 };
1132
+ if (text !== null) editor.value = text;
1133
+ editor.classList.add("open");
1134
+ place();
1135
+ editor.focus({ preventScroll: true });
1136
+ formula.value = editor.value;
1137
+ }
1138
+ /** The editor closed: what it holds kept, or let go. False when it could not be kept. */
1139
+ function finish(keep) {
1140
+ if (!typing) return true;
1141
+ const where = typingAt;
1142
+ const text = editor.value;
1143
+ typing = false;
1144
+ typingAt = null;
1145
+ editor.classList.remove("open");
1146
+ editor.value = "";
1147
+ if (keep && where.s === at && text !== rawAt(where.r, where.c)) {
1148
+ const done = change(() => putAt(where.r, where.c, text));
1149
+ if (!done) {
1150
+ // Back to what was being typed, so a formula that does not read is put right, not lost
1151
+ typing = true;
1152
+ typingAt = where;
1153
+ editor.value = text;
1154
+ editor.classList.add("open");
1155
+ place();
1156
+ editor.focus({ preventScroll: true });
1157
+ return false;
1158
+ }
1159
+ } else paint();
1160
+ return true;
1161
+ }
1162
+
1163
+ const clearPicked = () => {
1164
+ const { top, bottom, left, right } = picked();
1165
+ change(() => {
1166
+ for (let r = Math.max(0, top); r <= bottom; r++)
1167
+ for (let c = left; c <= right; c++)
1168
+ sheet().rows[list[r]][c] = { v: null };
1169
+ });
1170
+ };
1171
+
1172
+ /** Tab-separated text read into rows of cells, as a spreadsheet copies them. */
1173
+ const readTsv = (text) => {
1174
+ const rows = [[]];
1175
+ let cellText = "";
1176
+ let quoted = false;
1177
+ const body = text.replace(/\r\n?/g, "\n").replace(/\n$/, "");
1178
+ for (let i = 0; i < body.length; i++) {
1179
+ const ch = body[i];
1180
+ if (quoted) {
1181
+ if (ch === '"' && body[i + 1] === '"') {
1182
+ cellText += '"';
1183
+ i++;
1184
+ } else if (ch === '"') quoted = false;
1185
+ else cellText += ch;
1186
+ } else if (ch === '"' && cellText === "") quoted = true;
1187
+ else if (ch === "\t") {
1188
+ rows.at(-1).push(cellText);
1189
+ cellText = "";
1190
+ } else if (ch === "\n") {
1191
+ rows.at(-1).push(cellText);
1192
+ cellText = "";
1193
+ rows.push([]);
1194
+ } else cellText += ch;
1195
+ }
1196
+ rows.at(-1).push(cellText);
1197
+ return rows;
1198
+ };
1199
+ editor.addEventListener("paste", (event) => {
1200
+ if (typing) return;
1201
+ event.preventDefault();
1202
+ const rows = readTsv(event.clipboardData?.getData("text/plain") ?? "");
1203
+ const { top, left } = picked();
1204
+ const width = sheet().columns.length;
1205
+ let cut = 0;
1206
+ const done = change(() => {
1207
+ for (let i = 0; i < rows.length; i++)
1208
+ for (let j = 0; j < rows[i].length; j++) {
1209
+ if (left + j >= width) {
1210
+ cut = Math.max(cut, left + j - width + 1);
1211
+ continue;
1212
+ }
1213
+ const refused = putAt(top + i, left + j, rows[i][j]);
1214
+ if (refused) return refused;
1215
+ }
1216
+ });
1217
+ if (done && cut)
1218
+ say(
1219
+ `${cut} column${cut > 1 ? "s" : ""} past the last were left out; add columns first.`,
1220
+ true,
1221
+ );
1222
+ });
1223
+
1224
+ editor.addEventListener("keydown", (event) => {
1225
+ if (event.isComposing || event.keyCode === 229) return;
1226
+ const mod = event.metaKey || event.ctrlKey;
1227
+ if (typing) {
1228
+ event.stopPropagation();
1229
+ if (event.key === "Enter" || event.key === "Tab") {
1230
+ event.preventDefault();
1231
+ const { r, c } = typingAt;
1232
+ if (!finish(true)) return;
1233
+ const back = event.shiftKey ? -1 : 1;
1234
+ if (event.key === "Enter") pickTo(r + back, c);
1235
+ else pickTo(r, c + back);
1236
+ } else if (event.key === "Escape") {
1237
+ event.preventDefault();
1238
+ finish(false);
1239
+ }
1240
+ return;
1241
+ }
1242
+ const p = view().pick;
1243
+ if (mod && event.key.toLowerCase() === "z") {
1244
+ event.preventDefault();
1245
+ act[event.shiftKey ? "redo" : "undo"]();
1246
+ } else if (mod && event.key.toLowerCase() === "y") {
1247
+ event.preventDefault();
1248
+ act.redo();
1249
+ } else if (event.key === "F2") {
1250
+ event.preventDefault();
1251
+ openEditor(rawAt(p.r1, p.c1));
1252
+ } else if (event.key === "Enter" || event.key === "Tab") {
1253
+ event.preventDefault();
1254
+ const back = event.shiftKey ? -1 : 1;
1255
+ if (event.key === "Enter") pickTo(p.r1 + back, p.c1);
1256
+ else pickTo(p.r1, p.c1 + back);
1257
+ } else if (event.key === "Delete" || event.key === "Backspace") {
1258
+ event.preventDefault();
1259
+ clearPicked();
1260
+ }
1261
+ // The rest reaches the grid: arrows, copy, select all
1262
+ });
1263
+ // A key that types opens the editor with what it typed, a word being composed too
1264
+ editor.addEventListener("input", () => {
1265
+ if (!typing) openEditor(null);
1266
+ else formula.value = editor.value;
1267
+ grow();
1268
+ showRefs(editor.value);
1269
+ });
1270
+ editor.addEventListener("compositionstart", () => {
1271
+ if (!typing) openEditor(null);
1272
+ });
1273
+ editor.addEventListener("blur", () => {
1274
+ if (!typing) return;
1275
+ // The formula line takes over what was being typed
1276
+ requestAnimationFrame(() => {
1277
+ if (typing && document.activeElement !== formula) finish(true);
1278
+ });
1279
+ });
1280
+ table.addEventListener("dblclick", (event) => {
1281
+ if (!editing) return;
1282
+ const el = event.target.closest("[data-r]");
1283
+ if (!el || ["L", "T"].includes(el.dataset.r) || Number(el.dataset.c) === -1)
1284
+ return;
1285
+ openEditor(rawAt(Number(el.dataset.r), Number(el.dataset.c)));
1286
+ });
1287
+
1288
+ // The formula line edits the ring's cell as well
1289
+ formula.addEventListener("focus", () => {
1290
+ // Typed into, the line shows its own text
1291
+ fxBox.classList.remove("colored");
1292
+ if (!editing || typing) return;
1293
+ openEditor(rawAt(view().pick.r1, view().pick.c1));
1294
+ formula.focus();
1295
+ });
1296
+ formula.addEventListener("input", () => {
1297
+ if (typing) editor.value = formula.value;
1298
+ grow();
1299
+ showRefs(formula.value);
1300
+ });
1301
+ formula.addEventListener("keydown", (event) => {
1302
+ if (!typing || event.isComposing) return;
1303
+ if (event.key === "Enter") {
1304
+ event.preventDefault();
1305
+ const { r, c } = typingAt;
1306
+ if (finish(true)) pickTo(r + 1, c);
1307
+ focusGrid();
1308
+ } else if (event.key === "Escape") {
1309
+ event.preventDefault();
1310
+ finish(false);
1311
+ focusGrid();
1312
+ }
1313
+ });
1314
+ formula.addEventListener("blur", () => {
1315
+ requestAnimationFrame(() => {
1316
+ if (typing && document.activeElement !== editor) finish(true);
1317
+ });
1318
+ });
1319
+
1320
+ edits?.onToggle((on) => {
1321
+ if (!on) finish(true);
1322
+ editing = on;
1323
+ tools.hidden = !on;
1324
+ formula.readOnly = !on;
1325
+ grid.classList.toggle("editing", on);
1326
+ editor.hidden = !on;
1327
+ if (on) {
1328
+ drawFormat();
1329
+ place();
1330
+ }
1331
+ focusGrid();
1332
+ });
1333
+ editor.hidden = true;
1334
+
1335
+ // The page as a file again: without the grid this session drew, the workbook as it now is
1336
+ window.shell.clean = (copy) => {
1337
+ const data = copy.querySelector("#sheet-data");
1338
+ if (data)
1339
+ data.textContent = JSON.stringify(book).replaceAll("<", "\\u003c");
1340
+ copy.querySelector(".ss-edit")?.remove();
1341
+ copy.querySelector("#tools")?.setAttribute("hidden", "");
1342
+ copy.querySelector("#formula")?.setAttribute("readonly", "");
1343
+ copy.querySelector("#grid")?.classList.remove("editing");
1344
+ copy.querySelector("#note")?.replaceChildren();
1345
+ copy.querySelector("#fx-show")?.replaceChildren();
1346
+ copy.querySelector("#fx-box")?.classList.remove("colored");
1347
+ copy.querySelector("#table")?.replaceChildren();
1348
+ copy.querySelector("#table")?.removeAttribute("style");
1349
+ copy.querySelector("#tabs")?.replaceChildren();
1350
+ copy.querySelector("#stat")?.replaceChildren();
1351
+ copy.querySelector("#menu")?.setAttribute("hidden", "");
1352
+ for (const id of ["ref", "size"]) {
1353
+ const el = copy.querySelector(`#${id}`);
1354
+ if (el) el.textContent = "";
1355
+ }
1356
+ };
1357
+
1358
+ draw();
1359
+ view().pick = clampPick({ r1: 0, c1: 0, r2: 0, c2: 0 });
1360
+ paint();
1361
+ })();