@marimo-team/islands 0.19.12-dev1 → 0.20.0

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 (202) hide show
  1. package/dist/Combination-Du-o_hC9.js +11897 -0
  2. package/dist/{ConnectedDataExplorerComponent-CkXO-pKy.js → ConnectedDataExplorerComponent-BMiGWK57.js} +19 -18
  3. package/dist/{_baseIsEqual-CBSjxu-D.js → _baseIsEqual-DN5YkPnl.js} +1 -1
  4. package/dist/{_baseProperty-BVGrW_NZ.js → _baseProperty-6juuyX7Z.js} +5 -5
  5. package/dist/{_baseUniq-4lqa8rDi.js → _baseUniq-BlF21ach.js} +1 -1
  6. package/dist/{any-language-editor-t_VsTNa-.js → any-language-editor-1OMbohwD.js} +19 -19
  7. package/dist/architecture-U656AL7Q-Jd2CvPgJ.js +6 -0
  8. package/dist/{architectureDiagram-VXUJARFQ-DmJQhcJb.js → architectureDiagram-VXUJARFQ-DhN0C3Xf.js} +15 -15
  9. package/dist/{blockDiagram-VD42YOAC-CRofISJs.js → blockDiagram-VD42YOAC-DrBkIcbV.js} +7 -7
  10. package/dist/{button-Cy0ElmIm.js → button-KYalaJYu.js} +783 -200
  11. package/dist/{c4Diagram-YG6GDRKO-Deqoag4I.js → c4Diagram-YG6GDRKO-pWt4zmu0.js} +4 -4
  12. package/dist/{channel-CMsnebrL.js → channel-C56Jz8EL.js} +1 -1
  13. package/dist/{check-DkNR52Mm.js → check-C50jsehH.js} +1 -1
  14. package/dist/{chunk-76Q3JFCE-jPuajZH_.js → chunk-76Q3JFCE-CQ6a2yGJ.js} +1 -1
  15. package/dist/{chunk-ABZYJK2D-BGWvKte3.js → chunk-ABZYJK2D-BwNsaa1P.js} +1 -1
  16. package/dist/{chunk-ATLVNIR6-BYZB6C5T.js → chunk-ATLVNIR6-DtFMAawc.js} +1 -1
  17. package/dist/{chunk-B4BG7PRW-CwYUp6Uj.js → chunk-B4BG7PRW-lfWcLlzS.js} +4 -4
  18. package/dist/{chunk-DI55MBZ5-Gyro6dvN.js → chunk-DI55MBZ5-RhhAimfG.js} +4 -4
  19. package/dist/{chunk-EXTU4WIE-BlA7aWEw.js → chunk-EXTU4WIE-Bmo660a9.js} +1 -1
  20. package/dist/{chunk-FPAJGGOC-CduL34ft.js → chunk-FPAJGGOC-quWdfNUB.js} +7 -7
  21. package/dist/{chunk-FWNWRKHM-C-2TI4gt.js → chunk-FWNWRKHM-DmrwhAQr.js} +1 -1
  22. package/dist/{chunk-JA3XYJ7Z-Cm-pccR-.js → chunk-JA3XYJ7Z-n8UTzfok.js} +2 -2
  23. package/dist/{chunk-JZLCHNYA-CoLqqXMe.js → chunk-JZLCHNYA-ChKqHUdB.js} +4 -4
  24. package/dist/{chunk-LBM3YZW2-DWgQiioW.js → chunk-LBM3YZW2-BkxsqkNK.js} +1 -1
  25. package/dist/{chunk-LHMN2FUI-Dj_AHSvI.js → chunk-LHMN2FUI-CgYPnxfN.js} +1 -1
  26. package/dist/{chunk-N4CR4FBY-ByLbY9L-.js → chunk-N4CR4FBY-BxOHGL3P.js} +5 -5
  27. package/dist/{chunk-O7ZBX7Z2-CRZ8i1rP.js → chunk-O7ZBX7Z2-CdpLwOP0.js} +1 -1
  28. package/dist/{chunk-QN33PNHL-eodIiY9F.js → chunk-QN33PNHL-Dda-55xY.js} +1 -1
  29. package/dist/{chunk-QXUST7PY-CuJlDW6A.js → chunk-QXUST7PY-ClIEpoCT.js} +5 -5
  30. package/dist/{chunk-S3R3BYOJ-CwDGYMVf.js → chunk-S3R3BYOJ-C_1SJcWo.js} +3 -3
  31. package/dist/{chunk-S6J4BHB3-DNVMr0_v.js → chunk-S6J4BHB3-zfWjyfUg.js} +1 -1
  32. package/dist/{chunk-T53DSG4Q-BFWkpOX5.js → chunk-T53DSG4Q-Bewz1tiU.js} +1 -1
  33. package/dist/{chunk-TZMSLE5B-DFDr5FCr.js → chunk-TZMSLE5B-XW3duOft.js} +1 -1
  34. package/dist/{classDiagram-2ON5EDUG-BDBX9etk.js → classDiagram-2ON5EDUG-CUJlS_eo.js} +10 -10
  35. package/dist/{classDiagram-v2-WZHVMYZB-Bzj_L_BF.js → classDiagram-v2-WZHVMYZB-BhSPpbkE.js} +10 -10
  36. package/dist/{clone-Cc_6PW77.js → clone-V9hndNcj.js} +1 -1
  37. package/dist/{constants-DrOu5vvd.js → constants-BGRTDzdW.js} +2 -2
  38. package/dist/{copy-DRaXIb_a.js → copy-oc-FcZzt.js} +2 -2
  39. package/dist/{dagre-6UL2VRFP-r2rSdJYL.js → dagre-6UL2VRFP-BArPH353.js} +11 -11
  40. package/dist/{dagre-D2F8UdM6.js → dagre-Dcgyn_Uy.js} +15 -15
  41. package/dist/{diagram-PSM6KHXK-BpxVUe9U.js → diagram-PSM6KHXK-B1xAkr9y.js} +16 -16
  42. package/dist/{diagram-QEK2KX5R-q3dHUcp6.js → diagram-QEK2KX5R-CaoqwzPb.js} +14 -14
  43. package/dist/{diagram-S2PKOQOG-MDBKrxSC.js → diagram-S2PKOQOG-NXCsFLvR.js} +14 -14
  44. package/dist/dist-B8Y11RWn.js +1381 -0
  45. package/dist/dist-BA-HK7pI.js +5 -0
  46. package/dist/dist-BD5GU948.js +5 -0
  47. package/dist/{dist-BfactX3G.js → dist-BGzkWRSl.js} +4 -4
  48. package/dist/dist-BIYmAsND.js +5 -0
  49. package/dist/{dist-CmZYrgd_.js → dist-BUEi7EKT.js} +1 -1
  50. package/dist/{dist-B94MxrQS.js → dist-B_i29Q6L.js} +2 -2
  51. package/dist/dist-BcKTJXJi.js +5 -0
  52. package/dist/dist-BgnrtcWg.js +8 -0
  53. package/dist/{dist-glA_fIK_.js → dist-BoagoQQw.js} +2 -2
  54. package/dist/{dist-C2-m5aEk.js → dist-BswsDM4k.js} +2 -2
  55. package/dist/dist-C1njTlBq.js +5 -0
  56. package/dist/{dist-B2-r9y-0.js → dist-C5QB1NtD.js} +3 -3
  57. package/dist/{dist-Crk9ejOy.js → dist-CD7uLx0M.js} +2 -2
  58. package/dist/{dist-B4tYJP_i.js → dist-CMOy93xY.js} +2 -2
  59. package/dist/dist-CSKHwJYH.js +5 -0
  60. package/dist/dist-CSKKyiIq.js +5 -0
  61. package/dist/{dist-iiugPhCC.js → dist-C_9IMrtt.js} +1 -1
  62. package/dist/{dist-CE43BRmt.js → dist-Cb3iqED3.js} +1 -1
  63. package/dist/{dist-Dit9tk8a.js → dist-CoZ8kKKW.js} +1 -1
  64. package/dist/{dist-B5ATpkxy.js → dist-CrAYcS_4.js} +2 -2
  65. package/dist/dist-CrQ_pOuK.js +6 -0
  66. package/dist/dist-Cskx1daf.js +5 -0
  67. package/dist/dist-D4i0Ef34.js +8 -0
  68. package/dist/{dist-T4g7Sr6e.js → dist-D8EhXZ4S.js} +3 -3
  69. package/dist/{dist-CJrHMxlI.js → dist-DOLQQtWK.js} +3 -3
  70. package/dist/dist-DOcn61TX.js +8 -0
  71. package/dist/{dist-DqJdzAYM.js → dist-Dmr_nXF6.js} +2 -2
  72. package/dist/{dist-yVJ4xE5n.js → dist-DpAbrLuF.js} +5 -5
  73. package/dist/{dist-CcOGT46m.js → dist-DrC0QKFK.js} +1 -1
  74. package/dist/{dist-BYmtF1W6.js → dist-Dv2Phbh5.js} +2 -2
  75. package/dist/dist-DwMejAPB.js +6 -0
  76. package/dist/dist-DzSe1wby.js +8 -0
  77. package/dist/{dist-BbBnU4tG.js → dist-EZFqUJhh.js} +1 -1
  78. package/dist/{dist-Cgf353Ki.js → dist-Ey9hP8-j.js} +1 -1
  79. package/dist/{dist-BLwfpZD-.js → dist-IlWGXVjO.js} +2 -2
  80. package/dist/{dist-DOil6y-3.js → dist-LNp8svLl.js} +4 -4
  81. package/dist/{dist-CPTE45iS.js → dist-W6TdeACj.js} +1 -1
  82. package/dist/{dist-Dc1SFk5I.js → dist-a6Obzr07.js} +2 -2
  83. package/dist/{dist-Bsv_ARko.js → dist-bz6WguLy.js} +2 -2
  84. package/dist/{dist-CC9VUnXd.js → dist-iDeoXzdN.js} +1 -1
  85. package/dist/{dist-BoAHOW2l.js → dist-iyBCcLRa.js} +2 -2
  86. package/dist/{dist-CkEUrAus.js → dist-xCB683Dh.js} +2 -2
  87. package/dist/{erDiagram-Q2GNP2WA-CX1XdqVD.js → erDiagram-Q2GNP2WA-DWCa11g5.js} +10 -10
  88. package/dist/error-banner-vCG-EbUQ.js +619 -0
  89. package/dist/{esm-BAS2d2Ad.js → esm-DZSk8vt3.js} +27 -27
  90. package/dist/{flatten-eGRGXrC3.js → flatten-CWZjF1fc.js} +1 -1
  91. package/dist/{flowDiagram-NV44I4VS-BCj-ONTw.js → flowDiagram-NV44I4VS-BQ5PQs4L.js} +10 -10
  92. package/dist/{ganttDiagram-JELNMOA3-D1l5ewiQ.js → ganttDiagram-JELNMOA3-NTOuNWeT.js} +3 -3
  93. package/dist/{gitGraph-F6HP7TQM-CDM3aU-T.js → gitGraph-F6HP7TQM-DfRNsaDw.js} +3 -3
  94. package/dist/{gitGraphDiagram-NY62KEGX-KdZh0iiW.js → gitGraphDiagram-NY62KEGX-CYke62Ot.js} +13 -13
  95. package/dist/{glide-data-editor-2RvcPqmc.js → glide-data-editor-DttqGjrT.js} +571 -572
  96. package/dist/{graphlib-7UgfJadv.js → graphlib-CwMnCnQ9.js} +8 -8
  97. package/dist/{info-NVLQJR56-CoL1x1Fy.js → info-NVLQJR56-CUaoPtis.js} +3 -3
  98. package/dist/{infoDiagram-WHAUD3N6-PSH7lQ0D.js → infoDiagram-WHAUD3N6-B42WjAPh.js} +13 -13
  99. package/dist/{isEmpty-DQXRKNtW.js → isEmpty-6z2uv6gM.js} +2 -2
  100. package/dist/{isString-Clqvtgmo.js → isString-D6abkXrl.js} +1 -1
  101. package/dist/{isSymbol-TWXhTa8k.js → isSymbol-hk7foJ70.js} +1 -1
  102. package/dist/{journeyDiagram-XKPGCS4Q-BrTAxQ1J.js → journeyDiagram-XKPGCS4Q-ahXD97kr.js} +3 -3
  103. package/dist/{kanban-definition-3W4ZIXB7-BoYCDp_9.js → kanban-definition-3W4ZIXB7-CiTIpnhy.js} +7 -7
  104. package/dist/{label-CxU5JNBW.js → label-Cc5tEavt.js} +250 -250
  105. package/dist/{loader-C0-eIoas.js → loader-Cob3XFOw.js} +2 -2
  106. package/dist/main.js +1791 -1056
  107. package/dist/{memoize-Bag7B41I.js → memoize-Ckyqzyu_.js} +1 -1
  108. package/dist/{merge-Dl1bfxsj.js → merge-Db4Uulx4.js} +1 -1
  109. package/dist/{mermaid-C2cSe5YL.js → mermaid-B5xl_2hx.js} +73 -62
  110. package/dist/{mermaid-parser.core-D20zFbMa.js → mermaid-parser.core-BXj7Il0J.js} +8 -8
  111. package/dist/{min-Bg4bqmiD.js → min-ypdVXicC.js} +4 -4
  112. package/dist/{mindmap-definition-VGOIOE7T-CmRjsKEt.js → mindmap-definition-VGOIOE7T-Mni766A_.js} +9 -9
  113. package/dist/{now-mivqkCIv.js → now-Dwu5ou19.js} +2 -2
  114. package/dist/{once-BqS42WgZ.js → once-C9dA9qgQ.js} +1 -1
  115. package/dist/{packet-BFZMPI3H-C6aZmgV-.js → packet-BFZMPI3H-DHtQCusE.js} +3 -3
  116. package/dist/{pie-7BOR55EZ-NB6xYwcB.js → pie-7BOR55EZ-2sVLYbpR.js} +3 -3
  117. package/dist/{pieDiagram-ADFJNKIX-CtxQlnsU.js → pieDiagram-ADFJNKIX-PbXpgT8_.js} +14 -14
  118. package/dist/{quadrantDiagram-AYHSOK5B-DllnB2Hl.js → quadrantDiagram-AYHSOK5B-BtXGnx8i.js} +2 -2
  119. package/dist/{radar-NHE76QYJ-RKhErikV.js → radar-NHE76QYJ-Be0pEUux.js} +3 -3
  120. package/dist/{range-LoQMRQIX.js → range-D9jxVFd_.js} +5 -5
  121. package/dist/{reduce-B9mZDxPo.js → reduce-C6NEPj6s.js} +4 -4
  122. package/dist/{requirementDiagram-UZGBJVZJ-D36MI1k0.js → requirementDiagram-UZGBJVZJ-DxzXQRgq.js} +9 -9
  123. package/dist/{sankeyDiagram-TZEHDZUN-D1mygNPC.js → sankeyDiagram-TZEHDZUN-D-I7dJ0_.js} +2 -2
  124. package/dist/{sequenceDiagram-WL72ISMW-CWdn91Rf.js → sequenceDiagram-WL72ISMW-VDme2ljw.js} +4 -4
  125. package/dist/{slides-component-DfwLApNr.js → slides-component-ql7-5GDI.js} +2 -2
  126. package/dist/{spec-HoYHAQo2.js → spec-GwhMEXwK.js} +8 -9
  127. package/dist/{stateDiagram-FKZM4ZOC-CPxroWXd.js → stateDiagram-FKZM4ZOC-g3GI1EcK.js} +12 -12
  128. package/dist/{stateDiagram-v2-4FDKWEC3-BpM9Q54b.js → stateDiagram-v2-4FDKWEC3-7i6jBXe6.js} +10 -10
  129. package/dist/stex-D2rme5UG.js +4 -0
  130. package/dist/style.css +1 -1
  131. package/dist/{timeline-definition-IT6M3QCI-CVnRHx_t.js → timeline-definition-IT6M3QCI-bhvLlX_b.js} +2 -2
  132. package/dist/{toString-C4TLO6FA.js → toString-BwTJvlyD.js} +2 -2
  133. package/dist/tooltip-CL8m4f9y.js +404 -0
  134. package/dist/{treemap-KMMF4GRG-B37ugcLd.js → treemap-KMMF4GRG-Ba9ifjpG.js} +3 -3
  135. package/dist/{types-Ckva8JJq.js → types-Dsh6yC4B.js} +412 -413
  136. package/dist/{useAsyncData-dr8GazGv.js → useAsyncData-BPpyKjTJ.js} +2 -2
  137. package/dist/{useDeepCompareMemoize-ChviuF5n.js → useDeepCompareMemoize-C8Ms87P-.js} +18 -19
  138. package/dist/{useIframeCapabilities-DurI5SJh.js → useIframeCapabilities-C7z8VrZ1.js} +2 -2
  139. package/dist/{useTheme-SlKl8MlS.js → useTheme-Cq-gIssy.js} +299 -300
  140. package/dist/{vega-component-CnG0vAjf.js → vega-component-B5sxdjMq.js} +10 -10
  141. package/dist/{xychartDiagram-PRI3JC2R-BltwMWKC.js → xychartDiagram-PRI3JC2R-CFxuifYY.js} +5 -5
  142. package/package.json +1 -1
  143. package/src/components/editor/Output.tsx +8 -6
  144. package/src/components/editor/__tests__/Output.test.tsx +59 -0
  145. package/src/components/editor/chrome/__tests__/state.test.ts +321 -0
  146. package/src/components/editor/chrome/state.ts +27 -2
  147. package/src/components/editor/file-tree/upload.tsx +46 -23
  148. package/src/components/editor/links/cell-link.tsx +3 -2
  149. package/src/components/editor/output/console/ConsoleOutput.tsx +13 -3
  150. package/src/components/pages/gallery-page.tsx +1 -1
  151. package/src/components/pages/home-page.tsx +5 -3
  152. package/src/components/tracing/tracing.tsx +50 -39
  153. package/src/core/documentation/DocHoverTarget.tsx +23 -0
  154. package/src/core/documentation/doc-lookup.ts +50 -0
  155. package/src/core/islands/main.ts +1 -0
  156. package/src/core/websocket/useMarimoKernelConnection.tsx +3 -0
  157. package/src/css/app/Cell.css +5 -0
  158. package/src/mount.tsx +2 -2
  159. package/src/plugins/core/RenderHTML.tsx +15 -0
  160. package/src/plugins/core/__test__/registerReactComponent.test.ts +204 -0
  161. package/src/plugins/core/registerReactComponent.tsx +33 -0
  162. package/src/plugins/impl/MatrixPlugin.tsx +275 -0
  163. package/src/plugins/impl/__tests__/MatrixPlugin.test.tsx +415 -0
  164. package/src/plugins/impl/anywidget/model.ts +1 -2
  165. package/src/plugins/impl/matplotlib/MatplotlibPlugin.tsx +70 -0
  166. package/src/plugins/impl/matplotlib/__tests__/matplotlib-renderer.test.ts +152 -0
  167. package/src/plugins/impl/matplotlib/matplotlib-renderer.ts +781 -0
  168. package/src/plugins/impl/matrix.css +45 -0
  169. package/src/plugins/layout/mermaid/mermaid.tsx +11 -3
  170. package/src/plugins/plugins.ts +4 -0
  171. package/src/utils/__tests__/download.test.tsx +47 -0
  172. package/src/utils/download.ts +13 -1
  173. package/src/utils/links.ts +1 -1
  174. package/src/utils/urls.ts +1 -1
  175. package/dist/Combination-BTMrlhzT.js +0 -2611
  176. package/dist/architecture-U656AL7Q-COfwZju8.js +0 -6
  177. package/dist/dist-4YNZxwMI.js +0 -8
  178. package/dist/dist-7nR3r2kG.js +0 -5
  179. package/dist/dist-B2gkyT3r.js +0 -5
  180. package/dist/dist-B8G3I6vJ.js +0 -8
  181. package/dist/dist-BJ96Ykfp.js +0 -8
  182. package/dist/dist-BKLIWGw4.js +0 -5
  183. package/dist/dist-Bf3ou00A.js +0 -6
  184. package/dist/dist-BvkKXuPm.js +0 -5
  185. package/dist/dist-C6NJ3n6r.js +0 -5
  186. package/dist/dist-CecLPYY5.js +0 -5
  187. package/dist/dist-Ch0SwRzK.js +0 -5
  188. package/dist/dist-D6eWHiFh.js +0 -6
  189. package/dist/dist-DCQ710Bv.js +0 -5
  190. package/dist/dist-P_pkS5f-.js +0 -8
  191. package/dist/error-banner-D2zjeN_a.js +0 -1015
  192. package/dist/hotkeys-B5WnGZXF.js +0 -587
  193. package/dist/stex-ChDHQs3R.js +0 -4
  194. package/dist/zod-bjADtMKr.js +0 -10663
  195. /package/dist/{_arrayReduce-DlK7U3Q6.js → _arrayReduce-REKcIEj3.js} +0 -0
  196. /package/dist/{_baseFor-DSVmVciX.js → _baseFor-B69PDbIz.js} +0 -0
  197. /package/dist/{_hasUnicode-Bz2x6u6r.js → _hasUnicode-DrSAc5A5.js} +0 -0
  198. /package/dist/{dist-r8ecBV-v.js → dist-CUOuFgHt.js} +0 -0
  199. /package/dist/{invariant-D9QLJ4SZ.js → invariant-D-K49MfV.js} +0 -0
  200. /package/dist/{main-DhFbkwoC.js → main-DmxVpB19.js} +0 -0
  201. /package/dist/{purify.es-Brw-U87Q.js → purify.es-D4vaFt5N.js} +0 -0
  202. /package/dist/{stex-DrxP7bb3.js → stex-DIvyJfNO.js} +0 -0
@@ -0,0 +1,781 @@
1
+ /* Copyright 2026 Marimo. All rights reserved. */
2
+
3
+ import type { Setter } from "@/plugins/types";
4
+
5
+ export type ScaleType = "linear" | "log";
6
+
7
+ export interface Data {
8
+ chartBase64: string;
9
+ xBounds: [number, number];
10
+ yBounds: [number, number];
11
+ axesPixelBounds: [number, number, number, number]; // [left, top, right, bottom]
12
+ width: number;
13
+ height: number;
14
+ selectionColor: string;
15
+ selectionOpacity: number;
16
+ strokeWidth: number;
17
+ debounce: boolean;
18
+ xScale: ScaleType;
19
+ yScale: ScaleType;
20
+ }
21
+
22
+ export interface BoxData {
23
+ x_min: number;
24
+ x_max: number;
25
+ y_min: number;
26
+ y_max: number;
27
+ }
28
+
29
+ export type SelectionValue =
30
+ | { type: "box"; has_selection: true; data: BoxData }
31
+ | { type: "lasso"; has_selection: true; data: [number, number][] }
32
+ | { has_selection: false }
33
+ | undefined;
34
+
35
+ type AxesGeometry = Pick<
36
+ Data,
37
+ "axesPixelBounds" | "xBounds" | "xScale" | "yBounds" | "yScale"
38
+ >;
39
+
40
+ // Pixel coordinate in canvas space
41
+ interface PixelPoint {
42
+ x: number;
43
+ y: number;
44
+ }
45
+
46
+ // Data coordinate in plot space
47
+ interface DataPoint {
48
+ x: number;
49
+ y: number;
50
+ }
51
+
52
+ // -- State machine types --
53
+
54
+ type BoxAction =
55
+ | { type: "drawing" }
56
+ | { type: "dragging"; origin: PixelPoint }
57
+ | {
58
+ type: "resizing";
59
+ anchor: PixelPoint;
60
+ lockX: number | null;
61
+ lockY: number | null;
62
+ };
63
+
64
+ type LassoAction =
65
+ | { type: "drawing" }
66
+ | { type: "dragging"; origin: PixelPoint };
67
+
68
+ type Interaction =
69
+ | { type: "idle" }
70
+ | {
71
+ type: "box";
72
+ start: PixelPoint;
73
+ end: PixelPoint;
74
+ action: BoxAction | null;
75
+ }
76
+ | { type: "lasso"; points: PixelPoint[]; action: LassoAction | null };
77
+
78
+ // -- Resize handle detection --
79
+
80
+ type ResizeHandle = "n" | "s" | "e" | "w" | "nw" | "ne" | "sw" | "se";
81
+
82
+ const HANDLE_SIZE = 7; // pixels from edge to trigger resize
83
+
84
+ const RESIZE_CURSORS: Record<ResizeHandle, string> = {
85
+ n: "ns-resize",
86
+ s: "ns-resize",
87
+ e: "ew-resize",
88
+ w: "ew-resize",
89
+ nw: "nwse-resize",
90
+ se: "nwse-resize",
91
+ ne: "nesw-resize",
92
+ sw: "nesw-resize",
93
+ };
94
+
95
+ function getResizeHandle(
96
+ pt: PixelPoint,
97
+ boxStart: PixelPoint,
98
+ boxEnd: PixelPoint,
99
+ ): ResizeHandle | null {
100
+ const minX = Math.min(boxStart.x, boxEnd.x);
101
+ const maxX = Math.max(boxStart.x, boxEnd.x);
102
+ const minY = Math.min(boxStart.y, boxEnd.y);
103
+ const maxY = Math.max(boxStart.y, boxEnd.y);
104
+
105
+ const nearLeft = Math.abs(pt.x - minX) <= HANDLE_SIZE;
106
+ const nearRight = Math.abs(pt.x - maxX) <= HANDLE_SIZE;
107
+ const nearTop = Math.abs(pt.y - minY) <= HANDLE_SIZE;
108
+ const nearBottom = Math.abs(pt.y - maxY) <= HANDLE_SIZE;
109
+ const withinX = pt.x >= minX - HANDLE_SIZE && pt.x <= maxX + HANDLE_SIZE;
110
+ const withinY = pt.y >= minY - HANDLE_SIZE && pt.y <= maxY + HANDLE_SIZE;
111
+
112
+ // Corners first (more specific)
113
+ if (nearLeft && nearTop) {
114
+ return "nw";
115
+ }
116
+ if (nearRight && nearTop) {
117
+ return "ne";
118
+ }
119
+ if (nearLeft && nearBottom) {
120
+ return "sw";
121
+ }
122
+ if (nearRight && nearBottom) {
123
+ return "se";
124
+ }
125
+
126
+ // Edges
127
+ if (nearTop && withinX) {
128
+ return "n";
129
+ }
130
+ if (nearBottom && withinX) {
131
+ return "s";
132
+ }
133
+ if (nearLeft && withinY) {
134
+ return "w";
135
+ }
136
+ if (nearRight && withinY) {
137
+ return "e";
138
+ }
139
+
140
+ return null;
141
+ }
142
+
143
+ function getResizeConfig(
144
+ handle: ResizeHandle,
145
+ start: PixelPoint,
146
+ end: PixelPoint,
147
+ ): { anchor: PixelPoint; lockX: number | null; lockY: number | null } {
148
+ const minX = Math.min(start.x, end.x);
149
+ const maxX = Math.max(start.x, end.x);
150
+ const minY = Math.min(start.y, end.y);
151
+ const maxY = Math.max(start.y, end.y);
152
+
153
+ switch (handle) {
154
+ case "nw":
155
+ return { anchor: { x: maxX, y: maxY }, lockX: null, lockY: null };
156
+ case "ne":
157
+ return { anchor: { x: minX, y: maxY }, lockX: null, lockY: null };
158
+ case "sw":
159
+ return { anchor: { x: maxX, y: minY }, lockX: null, lockY: null };
160
+ case "se":
161
+ return { anchor: { x: minX, y: minY }, lockX: null, lockY: null };
162
+ case "n":
163
+ return { anchor: { x: minX, y: maxY }, lockX: maxX, lockY: null };
164
+ case "s":
165
+ return { anchor: { x: minX, y: minY }, lockX: maxX, lockY: null };
166
+ case "w":
167
+ return { anchor: { x: maxX, y: minY }, lockX: null, lockY: maxY };
168
+ case "e":
169
+ return { anchor: { x: minX, y: minY }, lockX: null, lockY: maxY };
170
+ }
171
+ }
172
+
173
+ export interface MatplotlibState extends Data {
174
+ value: SelectionValue;
175
+ setValue: Setter<SelectionValue>;
176
+ }
177
+
178
+ // Minimal d3-like scale: maps between data domain and [0, 1] fraction
179
+ interface Scale {
180
+ /** data value → normalized [0, 1] */
181
+ normalize(value: number): number;
182
+ /** normalized [0, 1] → data value */
183
+ invert(frac: number): number;
184
+ }
185
+
186
+ function createScale(type: ScaleType, bounds: [number, number]): Scale {
187
+ const [min, max] = bounds;
188
+ if (type === "log") {
189
+ const logMin = Math.log10(min);
190
+ const logRange = Math.log10(max) - logMin;
191
+ return {
192
+ normalize: (v) => (Math.log10(v) - logMin) / logRange,
193
+ invert: (f) => 10 ** (logMin + f * logRange),
194
+ };
195
+ }
196
+ const range = max - min;
197
+ return {
198
+ normalize: (v) => (v - min) / range,
199
+ invert: (f) => min + f * range,
200
+ };
201
+ }
202
+
203
+ // Ray-casting point-in-polygon test
204
+ function pointInPolygon(pt: PixelPoint, polygon: PixelPoint[]): boolean {
205
+ let inside = false;
206
+ for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
207
+ const xi = polygon[i].x;
208
+ const yi = polygon[i].y;
209
+ const xj = polygon[j].x;
210
+ const yj = polygon[j].y;
211
+ if (
212
+ yi > pt.y !== yj > pt.y &&
213
+ pt.x < ((xj - xi) * (pt.y - yi)) / (yj - yi) + xi
214
+ ) {
215
+ inside = !inside;
216
+ }
217
+ }
218
+ return inside;
219
+ }
220
+
221
+ function pixelToData(px: PixelPoint, g: AxesGeometry): DataPoint {
222
+ const [axLeft, axTop, axRight, axBottom] = g.axesPixelBounds;
223
+ const fracX = (px.x - axLeft) / (axRight - axLeft);
224
+ const fracY = (px.y - axTop) / (axBottom - axTop);
225
+ const sx = createScale(g.xScale, g.xBounds);
226
+ const sy = createScale(g.yScale, g.yBounds);
227
+ return { x: sx.invert(fracX), y: sy.invert(1 - fracY) };
228
+ }
229
+
230
+ function dataToPixel(data: DataPoint, g: AxesGeometry): PixelPoint {
231
+ const [axLeft, axTop, axRight, axBottom] = g.axesPixelBounds;
232
+ const sx = createScale(g.xScale, g.xBounds);
233
+ const sy = createScale(g.yScale, g.yBounds);
234
+ return {
235
+ x: axLeft + sx.normalize(data.x) * (axRight - axLeft),
236
+ y: axTop + (1 - sy.normalize(data.y)) * (axBottom - axTop),
237
+ };
238
+ }
239
+
240
+ function clampToAxes(pt: PixelPoint, g: AxesGeometry) {
241
+ const [axLeft, axTop, axRight, axBottom] = g.axesPixelBounds;
242
+ return {
243
+ x: Math.max(axLeft, Math.min(axRight, pt.x)),
244
+ y: Math.max(axTop, Math.min(axBottom, pt.y)),
245
+ };
246
+ }
247
+
248
+ function isPointInBox(
249
+ pt: PixelPoint,
250
+ boxStart: PixelPoint,
251
+ boxEnd: PixelPoint,
252
+ ): boolean {
253
+ const minX = Math.min(boxStart.x, boxEnd.x);
254
+ const maxX = Math.max(boxStart.x, boxEnd.x);
255
+ const minY = Math.min(boxStart.y, boxEnd.y);
256
+ const maxY = Math.max(boxStart.y, boxEnd.y);
257
+ return pt.x >= minX && pt.x <= maxX && pt.y >= minY && pt.y <= maxY;
258
+ }
259
+
260
+ export const visibleForTesting = {
261
+ createScale,
262
+ pixelToData,
263
+ dataToPixel,
264
+ pointInPolygon,
265
+ clampToAxes,
266
+ isPointInBox,
267
+ };
268
+
269
+ export class MatplotlibRenderer {
270
+ #canvas: HTMLCanvasElement;
271
+ #container: HTMLDivElement;
272
+ #state: MatplotlibState;
273
+ #interaction: Interaction = { type: "idle" };
274
+ #rafId = 0;
275
+ #image: HTMLImageElement | null = null;
276
+ #imageGeneration = 0;
277
+ #currentChartBase64 = "";
278
+
279
+ constructor(
280
+ container: HTMLDivElement,
281
+ options: { state: MatplotlibState; signal: AbortSignal },
282
+ ) {
283
+ this.#container = container;
284
+ this.#state = options.state;
285
+
286
+ // Configure container
287
+ container.tabIndex = -1;
288
+ container.role = "application";
289
+ container.className = "relative inline-block select-none outline-none";
290
+
291
+ // Create canvas
292
+ const canvas = document.createElement("canvas");
293
+ canvas.className = "block cursor-crosshair";
294
+ const dpr = globalThis.devicePixelRatio ?? 1;
295
+ canvas.width = this.#state.width * dpr;
296
+ canvas.height = this.#state.height * dpr;
297
+ canvas.style.width = `${this.#state.width}px`;
298
+ canvas.style.height = `${this.#state.height}px`;
299
+ canvas.style.maxWidth = "100%";
300
+ canvas.style.touchAction = "none";
301
+ container.append(canvas);
302
+ this.#canvas = canvas;
303
+
304
+ // Register event listeners with AbortSignal for auto-cleanup
305
+ canvas.addEventListener("pointerdown", this.#handlePointerDown, {
306
+ signal: options.signal,
307
+ });
308
+ canvas.addEventListener("pointermove", this.#handlePointerMove, {
309
+ signal: options.signal,
310
+ });
311
+ canvas.addEventListener("pointerup", this.#handlePointerUp, {
312
+ signal: options.signal,
313
+ });
314
+ container.addEventListener("keydown", this.#handleKeyDown, {
315
+ signal: options.signal,
316
+ });
317
+
318
+ // Clean up on abort
319
+ options.signal.addEventListener("abort", () => {
320
+ cancelAnimationFrame(this.#rafId);
321
+ this.#canvas.remove();
322
+ });
323
+
324
+ // Initial load
325
+ this.#loadImage(this.#state.chartBase64);
326
+ this.#restoreSelection(this.#state.value);
327
+ }
328
+
329
+ update(state: MatplotlibState): void {
330
+ const prev = this.#state;
331
+ this.#state = state;
332
+
333
+ if (state.chartBase64 !== this.#currentChartBase64) {
334
+ this.#loadImage(state.chartBase64);
335
+ return;
336
+ }
337
+
338
+ // Update canvas dimensions if changed
339
+ if (state.width !== prev.width || state.height !== prev.height) {
340
+ const dpr = globalThis.devicePixelRatio ?? 1;
341
+ this.#canvas.width = state.width * dpr;
342
+ this.#canvas.height = state.height * dpr;
343
+ this.#canvas.style.width = `${state.width}px`;
344
+ this.#canvas.style.height = `${state.height}px`;
345
+ }
346
+
347
+ // Redraw if style props changed or dimensions changed
348
+ if (
349
+ state.selectionColor !== prev.selectionColor ||
350
+ state.selectionOpacity !== prev.selectionOpacity ||
351
+ state.strokeWidth !== prev.strokeWidth ||
352
+ state.width !== prev.width ||
353
+ state.height !== prev.height
354
+ ) {
355
+ this.#drawCanvas();
356
+ }
357
+ }
358
+
359
+ #loadImage(chartBase64: string): void {
360
+ this.#currentChartBase64 = chartBase64;
361
+ this.#imageGeneration++;
362
+ const generation = this.#imageGeneration;
363
+
364
+ // Clear selection on new chart
365
+ this.#interaction = { type: "idle" };
366
+
367
+ const img = new Image();
368
+ img.onload = () => {
369
+ if (generation !== this.#imageGeneration) {
370
+ return;
371
+ }
372
+ this.#image = img;
373
+ this.#drawCanvas();
374
+ };
375
+ img.src = chartBase64;
376
+ }
377
+
378
+ #drawCanvas = (): void => {
379
+ const img = this.#image;
380
+ if (!img) {
381
+ return;
382
+ }
383
+
384
+ const ctx = this.#canvas.getContext("2d");
385
+ if (!ctx) {
386
+ return;
387
+ }
388
+
389
+ const s = this.#state;
390
+ const ix = this.#interaction;
391
+
392
+ // Scale for HiDPI: all coordinates remain in logical pixels
393
+ const dpr = globalThis.devicePixelRatio ?? 1;
394
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
395
+
396
+ // Clear and draw the base image
397
+ ctx.clearRect(0, 0, s.width, s.height);
398
+ ctx.drawImage(img, 0, 0, s.width, s.height);
399
+
400
+ // Draw box selection overlay
401
+ if (ix.type === "box") {
402
+ const x = Math.min(ix.start.x, ix.end.x);
403
+ const y = Math.min(ix.start.y, ix.end.y);
404
+ const w = Math.abs(ix.end.x - ix.start.x);
405
+ const h = Math.abs(ix.end.y - ix.start.y);
406
+
407
+ ctx.save();
408
+ ctx.fillStyle = s.selectionColor;
409
+ ctx.globalAlpha = s.selectionOpacity;
410
+ ctx.fillRect(x, y, w, h);
411
+ ctx.restore();
412
+
413
+ ctx.strokeStyle = s.selectionColor;
414
+ ctx.lineWidth = s.strokeWidth;
415
+ ctx.strokeRect(x, y, w, h);
416
+ }
417
+
418
+ // Draw lasso selection overlay
419
+ if (ix.type === "lasso" && ix.points.length >= 2) {
420
+ ctx.beginPath();
421
+ ctx.moveTo(ix.points[0].x, ix.points[0].y);
422
+ for (let i = 1; i < ix.points.length; i++) {
423
+ ctx.lineTo(ix.points[i].x, ix.points[i].y);
424
+ }
425
+ ctx.closePath();
426
+
427
+ ctx.save();
428
+ ctx.fillStyle = s.selectionColor;
429
+ ctx.globalAlpha = s.selectionOpacity;
430
+ ctx.fill("evenodd");
431
+ ctx.restore();
432
+
433
+ ctx.strokeStyle = s.selectionColor;
434
+ ctx.lineWidth = s.strokeWidth;
435
+ ctx.stroke();
436
+ }
437
+ };
438
+
439
+ #scheduleRedraw = (): void => {
440
+ cancelAnimationFrame(this.#rafId);
441
+ this.#rafId = requestAnimationFrame(this.#drawCanvas);
442
+ };
443
+
444
+ #getCanvasPoint = (e: PointerEvent): PixelPoint => {
445
+ const rect = this.#canvas.getBoundingClientRect();
446
+ const scaleX = this.#state.width / rect.width;
447
+ const scaleY = this.#state.height / rect.height;
448
+ return {
449
+ x: (e.clientX - rect.left) * scaleX,
450
+ y: (e.clientY - rect.top) * scaleY,
451
+ };
452
+ };
453
+
454
+ #emitBoxSelection = (start: PixelPoint, end: PixelPoint): void => {
455
+ const d1 = pixelToData(start, this.#state);
456
+ const d2 = pixelToData(end, this.#state);
457
+ this.#state.setValue({
458
+ type: "box",
459
+ has_selection: true,
460
+ data: {
461
+ x_min: Math.min(d1.x, d2.x),
462
+ x_max: Math.max(d1.x, d2.x),
463
+ y_min: Math.min(d1.y, d2.y),
464
+ y_max: Math.max(d1.y, d2.y),
465
+ },
466
+ });
467
+ };
468
+
469
+ #emitLassoSelection = (points: PixelPoint[]): void => {
470
+ if (points.length >= 3) {
471
+ const data: [number, number][] = points.map((p) => {
472
+ const d = pixelToData(p, this.#state);
473
+ return [d.x, d.y];
474
+ });
475
+ this.#state.setValue({
476
+ type: "lasso",
477
+ has_selection: true,
478
+ data,
479
+ });
480
+ }
481
+ };
482
+
483
+ #clearSelection = (): void => {
484
+ this.#interaction = { type: "idle" };
485
+ this.#state.setValue({ has_selection: false });
486
+ this.#scheduleRedraw();
487
+ };
488
+
489
+ #hasSelection = (): boolean => {
490
+ return this.#interaction.type !== "idle";
491
+ };
492
+
493
+ #isPointInSelection = (pt: PixelPoint): boolean => {
494
+ const ix = this.#interaction;
495
+ if (ix.type === "box") {
496
+ return isPointInBox(pt, ix.start, ix.end);
497
+ }
498
+ if (ix.type === "lasso") {
499
+ return pointInPolygon(pt, ix.points);
500
+ }
501
+ return false;
502
+ };
503
+
504
+ #updateCursor = (pt: PixelPoint): void => {
505
+ const [axLeft, axTop, axRight, axBottom] = this.#state.axesPixelBounds;
506
+ const inAxes =
507
+ pt.x >= axLeft && pt.x <= axRight && pt.y >= axTop && pt.y <= axBottom;
508
+
509
+ if (!inAxes) {
510
+ this.#canvas.style.cursor = "default";
511
+ return;
512
+ }
513
+
514
+ const ix = this.#interaction;
515
+
516
+ // Resize handles on idle box selection
517
+ if (ix.type === "box" && ix.action === null) {
518
+ const handle = getResizeHandle(pt, ix.start, ix.end);
519
+ if (handle) {
520
+ this.#canvas.style.cursor = RESIZE_CURSORS[handle];
521
+ return;
522
+ }
523
+ }
524
+
525
+ if (this.#hasSelection() && this.#isPointInSelection(pt)) {
526
+ this.#canvas.style.cursor = "move";
527
+ return;
528
+ }
529
+
530
+ this.#canvas.style.cursor = "crosshair";
531
+ };
532
+
533
+ #restoreSelection = (value: SelectionValue): void => {
534
+ if (!value || !("has_selection" in value) || !value.has_selection) {
535
+ return;
536
+ }
537
+
538
+ if (value.type === "box") {
539
+ const sel = value.data;
540
+ const start = dataToPixel({ x: sel.x_min, y: sel.y_min }, this.#state);
541
+ const end = dataToPixel({ x: sel.x_max, y: sel.y_max }, this.#state);
542
+ this.#interaction = { type: "box", start, end, action: null };
543
+ } else if (value.type === "lasso") {
544
+ const points = value.data.map(([vx, vy]) =>
545
+ dataToPixel({ x: vx, y: vy }, this.#state),
546
+ );
547
+ this.#interaction = { type: "lasso", points, action: null };
548
+ }
549
+ this.#scheduleRedraw();
550
+ };
551
+
552
+ #handlePointerDown = (e: PointerEvent): void => {
553
+ this.#canvas.setPointerCapture(e.pointerId);
554
+ this.#container.focus();
555
+ const pt = this.#getCanvasPoint(e);
556
+ const ix = this.#interaction;
557
+
558
+ // Shift+click -> start lasso
559
+ if (e.shiftKey) {
560
+ this.#interaction = {
561
+ type: "lasso",
562
+ points: [clampToAxes(pt, this.#state)],
563
+ action: { type: "drawing" },
564
+ };
565
+ this.#scheduleRedraw();
566
+ return;
567
+ }
568
+
569
+ // Box exists + near edge/corner -> start resize
570
+ if (ix.type === "box" && ix.action === null) {
571
+ const handle = getResizeHandle(pt, ix.start, ix.end);
572
+ if (handle) {
573
+ const { anchor, lockX, lockY } = getResizeConfig(
574
+ handle,
575
+ ix.start,
576
+ ix.end,
577
+ );
578
+ this.#interaction = {
579
+ type: "box",
580
+ start: ix.start,
581
+ end: ix.end,
582
+ action: { type: "resizing", anchor, lockX, lockY },
583
+ };
584
+ return;
585
+ }
586
+ }
587
+
588
+ // Inside existing selection -> start drag
589
+ if (this.#hasSelection() && this.#isPointInSelection(pt)) {
590
+ if (ix.type === "box") {
591
+ this.#interaction = {
592
+ type: "box",
593
+ start: ix.start,
594
+ end: ix.end,
595
+ action: { type: "dragging", origin: pt },
596
+ };
597
+ } else if (ix.type === "lasso") {
598
+ this.#interaction = {
599
+ type: "lasso",
600
+ points: ix.points,
601
+ action: { type: "dragging", origin: pt },
602
+ };
603
+ }
604
+ return;
605
+ }
606
+
607
+ // Outside selection with existing one -> clear, then start new box
608
+ if (this.#hasSelection()) {
609
+ this.#clearSelection();
610
+ }
611
+
612
+ // Start new box selection
613
+ const clamped = clampToAxes(pt, this.#state);
614
+ this.#interaction = {
615
+ type: "box",
616
+ start: clamped,
617
+ end: clamped,
618
+ action: { type: "drawing" },
619
+ };
620
+ this.#scheduleRedraw();
621
+ };
622
+
623
+ #handlePointerMove = (e: PointerEvent): void => {
624
+ const pt = this.#getCanvasPoint(e);
625
+ const ix = this.#interaction;
626
+ const s = this.#state;
627
+
628
+ // No active action -> just update cursor
629
+ if (ix.type === "idle" || ix.action === null) {
630
+ this.#updateCursor(pt);
631
+ return;
632
+ }
633
+
634
+ // Lasso drawing: append clamped point
635
+ if (ix.type === "lasso" && ix.action.type === "drawing") {
636
+ ix.points.push(clampToAxes(pt, s));
637
+ this.#scheduleRedraw();
638
+ return;
639
+ }
640
+
641
+ // Box drawing: update end point
642
+ if (ix.type === "box" && ix.action.type === "drawing") {
643
+ const clamped = clampToAxes(pt, s);
644
+ ix.end = clamped;
645
+ this.#scheduleRedraw();
646
+ if (!s.debounce) {
647
+ this.#emitBoxSelection(ix.start, clamped);
648
+ }
649
+ return;
650
+ }
651
+
652
+ // Box resizing: recompute box from anchor + clamped mouse
653
+ if (ix.type === "box" && ix.action.type === "resizing") {
654
+ const clamped = clampToAxes(pt, s);
655
+ const { anchor, lockX, lockY } = ix.action;
656
+ ix.start = anchor;
657
+ ix.end = { x: lockX ?? clamped.x, y: lockY ?? clamped.y };
658
+ this.#scheduleRedraw();
659
+ if (!s.debounce) {
660
+ this.#emitBoxSelection(ix.start, ix.end);
661
+ }
662
+ return;
663
+ }
664
+
665
+ // Box dragging
666
+ if (ix.type === "box" && ix.action.type === "dragging") {
667
+ const [axLeft, axTop, axRight, axBottom] = s.axesPixelBounds;
668
+ let dx = pt.x - ix.action.origin.x;
669
+ let dy = pt.y - ix.action.origin.y;
670
+ ix.action.origin = pt;
671
+
672
+ const minX = Math.min(ix.start.x, ix.end.x);
673
+ const maxX = Math.max(ix.start.x, ix.end.x);
674
+ const minY = Math.min(ix.start.y, ix.end.y);
675
+ const maxY = Math.max(ix.start.y, ix.end.y);
676
+ dx = Math.max(axLeft - minX, Math.min(axRight - maxX, dx));
677
+ dy = Math.max(axTop - minY, Math.min(axBottom - maxY, dy));
678
+ ix.start = { x: ix.start.x + dx, y: ix.start.y + dy };
679
+ ix.end = { x: ix.end.x + dx, y: ix.end.y + dy };
680
+ this.#scheduleRedraw();
681
+ if (!s.debounce) {
682
+ this.#emitBoxSelection(ix.start, ix.end);
683
+ }
684
+ return;
685
+ }
686
+
687
+ // Lasso dragging
688
+ if (ix.type === "lasso" && ix.action.type === "dragging") {
689
+ const [axLeft, axTop, axRight, axBottom] = s.axesPixelBounds;
690
+ let dx = pt.x - ix.action.origin.x;
691
+ let dy = pt.y - ix.action.origin.y;
692
+ ix.action.origin = pt;
693
+
694
+ let lMinX = Number.POSITIVE_INFINITY;
695
+ let lMaxX = Number.NEGATIVE_INFINITY;
696
+ let lMinY = Number.POSITIVE_INFINITY;
697
+ let lMaxY = Number.NEGATIVE_INFINITY;
698
+ for (const lp of ix.points) {
699
+ if (lp.x < lMinX) {
700
+ lMinX = lp.x;
701
+ }
702
+ if (lp.x > lMaxX) {
703
+ lMaxX = lp.x;
704
+ }
705
+ if (lp.y < lMinY) {
706
+ lMinY = lp.y;
707
+ }
708
+ if (lp.y > lMaxY) {
709
+ lMaxY = lp.y;
710
+ }
711
+ }
712
+ dx = Math.max(axLeft - lMinX, Math.min(axRight - lMaxX, dx));
713
+ dy = Math.max(axTop - lMinY, Math.min(axBottom - lMaxY, dy));
714
+ for (let i = 0; i < ix.points.length; i++) {
715
+ ix.points[i] = {
716
+ x: ix.points[i].x + dx,
717
+ y: ix.points[i].y + dy,
718
+ };
719
+ }
720
+ this.#scheduleRedraw();
721
+ if (!s.debounce) {
722
+ this.#emitLassoSelection(ix.points);
723
+ }
724
+ }
725
+ };
726
+
727
+ #handlePointerUp = (e: PointerEvent): void => {
728
+ this.#canvas.releasePointerCapture(e.pointerId);
729
+
730
+ const ix = this.#interaction;
731
+ const s = this.#state;
732
+
733
+ // Lasso drawing complete
734
+ if (ix.type === "lasso" && ix.action?.type === "drawing") {
735
+ if (ix.points.length >= 3) {
736
+ ix.action = null;
737
+ this.#emitLassoSelection(ix.points);
738
+ } else {
739
+ this.#interaction = { type: "idle" };
740
+ this.#state.setValue({ has_selection: false });
741
+ }
742
+ this.#scheduleRedraw();
743
+ return;
744
+ }
745
+
746
+ // Lasso dragging complete
747
+ if (ix.type === "lasso" && ix.action?.type === "dragging") {
748
+ ix.action = null;
749
+ if (s.debounce) {
750
+ this.#emitLassoSelection(ix.points);
751
+ }
752
+ return;
753
+ }
754
+
755
+ // Box action complete (drawing, dragging, or resizing)
756
+ if (ix.type === "box" && ix.action !== null) {
757
+ ix.action = null;
758
+ if (s.debounce) {
759
+ this.#emitBoxSelection(ix.start, ix.end);
760
+ }
761
+ }
762
+ };
763
+
764
+ #handleKeyDown = (e: KeyboardEvent): void => {
765
+ if (e.key === "Escape") {
766
+ const ix = this.#interaction;
767
+ if (ix.type === "idle") {
768
+ return;
769
+ }
770
+
771
+ // During initial drawing -> cancel without emitting
772
+ if (ix.action?.type === "drawing") {
773
+ this.#interaction = { type: "idle" };
774
+ this.#scheduleRedraw();
775
+ } else {
776
+ // Completed selection, dragging, or resizing -> clear
777
+ this.#clearSelection();
778
+ }
779
+ }
780
+ };
781
+ }