@dxos/plugin-sheet 0.6.11 → 0.6.12-main.568932b

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 (277) hide show
  1. package/dist/lib/browser/SheetContainer-T2QWJOFD.mjs +262 -0
  2. package/dist/lib/browser/SheetContainer-T2QWJOFD.mjs.map +7 -0
  3. package/dist/lib/browser/{chunk-FUAGSXA4.mjs → chunk-5ZMVZYGB.mjs} +24 -19
  4. package/dist/lib/browser/chunk-5ZMVZYGB.mjs.map +7 -0
  5. package/dist/lib/browser/chunk-GSV5QNLD.mjs +2966 -0
  6. package/dist/lib/browser/chunk-GSV5QNLD.mjs.map +7 -0
  7. package/dist/lib/browser/{chunk-JRL5LGCE.mjs → chunk-QILRZNE5.mjs} +2 -5
  8. package/dist/lib/browser/chunk-QILRZNE5.mjs.map +7 -0
  9. package/dist/lib/browser/{SheetContainer-U4H5D34A.mjs → chunk-ZL2V5UJR.mjs} +1182 -249
  10. package/dist/lib/browser/chunk-ZL2V5UJR.mjs.map +7 -0
  11. package/dist/lib/browser/graph-M4IQ76QX.mjs +33 -0
  12. package/dist/lib/browser/graph-M4IQ76QX.mjs.map +7 -0
  13. package/dist/lib/browser/index.mjs +96 -60
  14. package/dist/lib/browser/index.mjs.map +4 -4
  15. package/dist/lib/browser/meta.json +1 -1
  16. package/dist/lib/browser/meta.mjs +1 -1
  17. package/dist/lib/browser/types.mjs +4 -6
  18. package/dist/lib/node/SheetContainer-PV5ET4UJ.cjs +280 -0
  19. package/dist/lib/node/SheetContainer-PV5ET4UJ.cjs.map +7 -0
  20. package/dist/lib/node/{SheetContainer-AXQV3ZT5.cjs → chunk-2K53Z2TU.cjs} +1212 -287
  21. package/dist/lib/node/chunk-2K53Z2TU.cjs.map +7 -0
  22. package/dist/lib/node/{chunk-5KKJ4NPP.cjs → chunk-5XPK2V4A.cjs} +418 -678
  23. package/dist/lib/node/chunk-5XPK2V4A.cjs.map +7 -0
  24. package/dist/lib/node/{chunk-BJ6ZD7MN.cjs → chunk-BNARJ5GM.cjs} +5 -18
  25. package/dist/lib/node/chunk-BNARJ5GM.cjs.map +7 -0
  26. package/dist/lib/node/{chunk-DSYKOI4E.cjs → chunk-STAVQ2JE.cjs} +28 -24
  27. package/dist/lib/node/chunk-STAVQ2JE.cjs.map +7 -0
  28. package/dist/lib/node/graph-Q3N2X26H.cjs +55 -0
  29. package/dist/lib/node/graph-Q3N2X26H.cjs.map +7 -0
  30. package/dist/lib/node/index.cjs +106 -66
  31. package/dist/lib/node/index.cjs.map +4 -4
  32. package/dist/lib/node/meta.cjs +3 -3
  33. package/dist/lib/node/meta.cjs.map +1 -1
  34. package/dist/lib/node/meta.json +1 -1
  35. package/dist/lib/node/types.cjs +10 -12
  36. package/dist/lib/node/types.cjs.map +2 -2
  37. package/dist/lib/node-esm/SheetContainer-FOZD2WLT.mjs +263 -0
  38. package/dist/lib/node-esm/SheetContainer-FOZD2WLT.mjs.map +7 -0
  39. package/dist/lib/node-esm/chunk-2HAM45RC.mjs +88 -0
  40. package/dist/lib/node-esm/chunk-2HAM45RC.mjs.map +7 -0
  41. package/dist/lib/{browser/chunk-D5AGLXJP.mjs → node-esm/chunk-5WPZCXNS.mjs} +411 -678
  42. package/dist/lib/node-esm/chunk-5WPZCXNS.mjs.map +7 -0
  43. package/dist/lib/node-esm/chunk-IU2L277A.mjs +17 -0
  44. package/dist/lib/node-esm/chunk-IU2L277A.mjs.map +7 -0
  45. package/dist/lib/node-esm/chunk-QEUCIHIN.mjs +2706 -0
  46. package/dist/lib/node-esm/chunk-QEUCIHIN.mjs.map +7 -0
  47. package/dist/lib/node-esm/graph-SMPUMOV2.mjs +34 -0
  48. package/dist/lib/node-esm/graph-SMPUMOV2.mjs.map +7 -0
  49. package/dist/lib/node-esm/index.mjs +285 -0
  50. package/dist/lib/node-esm/index.mjs.map +7 -0
  51. package/dist/lib/node-esm/meta.json +1 -0
  52. package/dist/lib/node-esm/meta.mjs +10 -0
  53. package/dist/lib/node-esm/meta.mjs.map +7 -0
  54. package/dist/lib/node-esm/types.mjs +21 -0
  55. package/dist/lib/node-esm/types.mjs.map +7 -0
  56. package/dist/types/src/SheetPlugin.d.ts.map +1 -1
  57. package/dist/types/src/components/CellEditor/CellEditor.d.ts +23 -3
  58. package/dist/types/src/components/CellEditor/CellEditor.d.ts.map +1 -1
  59. package/dist/types/src/components/CellEditor/CellEditor.stories.d.ts +2 -2
  60. package/dist/types/src/components/CellEditor/CellEditor.stories.d.ts.map +1 -1
  61. package/dist/types/src/components/CellEditor/extension.d.ts +1 -1
  62. package/dist/types/src/components/CellEditor/extension.d.ts.map +1 -1
  63. package/dist/types/src/components/ComputeGraph/ComputeGraphContextProvider.d.ts +11 -0
  64. package/dist/types/src/components/ComputeGraph/ComputeGraphContextProvider.d.ts.map +1 -0
  65. package/dist/types/src/components/ComputeGraph/index.d.ts +1 -3
  66. package/dist/types/src/components/ComputeGraph/index.d.ts.map +1 -1
  67. package/dist/types/src/components/GridSheet/GridSheet.d.ts +10 -0
  68. package/dist/types/src/components/GridSheet/GridSheet.d.ts.map +1 -0
  69. package/dist/types/src/components/GridSheet/GridSheet.stories.d.ts +9 -0
  70. package/dist/types/src/components/GridSheet/GridSheet.stories.d.ts.map +1 -0
  71. package/dist/types/src/components/GridSheet/util.d.ts +16 -0
  72. package/dist/types/src/components/GridSheet/util.d.ts.map +1 -0
  73. package/dist/types/src/components/Sheet/Sheet.d.ts +1 -1
  74. package/dist/types/src/components/Sheet/Sheet.d.ts.map +1 -1
  75. package/dist/types/src/components/Sheet/Sheet.stories.d.ts +5 -6
  76. package/dist/types/src/components/Sheet/Sheet.stories.d.ts.map +1 -1
  77. package/dist/types/src/components/Sheet/decorations.d.ts +24 -0
  78. package/dist/types/src/components/Sheet/decorations.d.ts.map +1 -0
  79. package/dist/types/src/components/Sheet/grid.d.ts +2 -2
  80. package/dist/types/src/components/Sheet/grid.d.ts.map +1 -1
  81. package/dist/types/src/components/Sheet/nav.d.ts +3 -3
  82. package/dist/types/src/components/Sheet/nav.d.ts.map +1 -1
  83. package/dist/types/src/components/Sheet/sheet-context.d.ts +8 -7
  84. package/dist/types/src/components/Sheet/sheet-context.d.ts.map +1 -1
  85. package/dist/types/src/components/Sheet/threads.d.ts +2 -0
  86. package/dist/types/src/components/Sheet/threads.d.ts.map +1 -0
  87. package/dist/types/src/components/SheetContainer.d.ts +2 -3
  88. package/dist/types/src/components/SheetContainer.d.ts.map +1 -1
  89. package/dist/types/src/components/Toolbar/Toolbar.d.ts +19 -3
  90. package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -1
  91. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +18 -13
  92. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -1
  93. package/dist/types/src/components/index.d.ts +2 -2
  94. package/dist/types/src/components/index.d.ts.map +1 -1
  95. package/dist/types/src/defs/index.d.ts +3 -0
  96. package/dist/types/src/defs/index.d.ts.map +1 -0
  97. package/dist/types/src/{model → defs}/types.d.ts +8 -3
  98. package/dist/types/src/defs/types.d.ts.map +1 -0
  99. package/dist/types/src/defs/types.test.d.ts.map +1 -0
  100. package/dist/types/src/defs/util.d.ts +43 -0
  101. package/dist/types/src/defs/util.d.ts.map +1 -0
  102. package/dist/types/src/extensions/compute.d.ts +6 -0
  103. package/dist/types/src/extensions/compute.d.ts.map +1 -0
  104. package/dist/types/src/extensions/compute.stories.d.ts +26 -0
  105. package/dist/types/src/extensions/compute.stories.d.ts.map +1 -0
  106. package/dist/types/src/extensions/index.d.ts +2 -0
  107. package/dist/types/src/extensions/index.d.ts.map +1 -0
  108. package/dist/types/src/graph/compute-graph-registry.d.ts +34 -0
  109. package/dist/types/src/graph/compute-graph-registry.d.ts.map +1 -0
  110. package/dist/types/src/graph/compute-graph.d.ts +64 -0
  111. package/dist/types/src/graph/compute-graph.d.ts.map +1 -0
  112. package/dist/types/src/graph/compute-graph.stories.d.ts +10 -0
  113. package/dist/types/src/graph/compute-graph.stories.d.ts.map +1 -0
  114. package/dist/types/src/graph/compute-graph.test.d.ts +2 -0
  115. package/dist/types/src/graph/compute-graph.test.d.ts.map +1 -0
  116. package/dist/types/src/graph/compute-node.d.ts +26 -0
  117. package/dist/types/src/graph/compute-node.d.ts.map +1 -0
  118. package/dist/types/src/{components/ComputeGraph → graph/functions}/async-function.d.ts +14 -5
  119. package/dist/types/src/graph/functions/async-function.d.ts.map +1 -0
  120. package/dist/types/src/graph/functions/edge-function.d.ts +21 -0
  121. package/dist/types/src/graph/functions/edge-function.d.ts.map +1 -0
  122. package/dist/types/src/{model/functions.d.ts → graph/functions/function-defs.d.ts} +1 -1
  123. package/dist/types/src/graph/functions/function-defs.d.ts.map +1 -0
  124. package/dist/types/src/graph/functions/index.d.ts +4 -0
  125. package/dist/types/src/graph/functions/index.d.ts.map +1 -0
  126. package/dist/types/src/graph/hyperformula.test.d.ts +2 -0
  127. package/dist/types/src/graph/hyperformula.test.d.ts.map +1 -0
  128. package/dist/types/src/graph/index.d.ts +5 -0
  129. package/dist/types/src/graph/index.d.ts.map +1 -0
  130. package/dist/types/src/graph/testing/index.d.ts +3 -0
  131. package/dist/types/src/graph/testing/index.d.ts.map +1 -0
  132. package/dist/types/src/graph/testing/test-builder.d.ts +15 -0
  133. package/dist/types/src/graph/testing/test-builder.d.ts.map +1 -0
  134. package/dist/types/src/graph/testing/test-plugin.d.ts +36 -0
  135. package/dist/types/src/graph/testing/test-plugin.d.ts.map +1 -0
  136. package/dist/types/src/graph/util.d.ts +2 -0
  137. package/dist/types/src/graph/util.d.ts.map +1 -0
  138. package/dist/types/src/hooks/hooks.stories.d.ts +11 -0
  139. package/dist/types/src/hooks/hooks.stories.d.ts.map +1 -0
  140. package/dist/types/src/hooks/index.d.ts +4 -0
  141. package/dist/types/src/hooks/index.d.ts.map +1 -0
  142. package/dist/types/src/hooks/useComputeGraph.d.ts +7 -0
  143. package/dist/types/src/hooks/useComputeGraph.d.ts.map +1 -0
  144. package/dist/types/src/hooks/useFormattingModel.d.ts +3 -0
  145. package/dist/types/src/hooks/useFormattingModel.d.ts.map +1 -0
  146. package/dist/types/src/hooks/useSheetModel.d.ts +8 -0
  147. package/dist/types/src/hooks/useSheetModel.d.ts.map +1 -0
  148. package/dist/types/src/meta.d.ts +1 -4
  149. package/dist/types/src/meta.d.ts.map +1 -1
  150. package/dist/types/src/model/formatting-model.d.ts +16 -0
  151. package/dist/types/src/model/formatting-model.d.ts.map +1 -0
  152. package/dist/types/src/model/index.d.ts +2 -3
  153. package/dist/types/src/model/index.d.ts.map +1 -1
  154. package/dist/types/src/model/{model.d.ts → sheet-model.d.ts} +10 -65
  155. package/dist/types/src/model/sheet-model.d.ts.map +1 -0
  156. package/dist/types/src/model/sheet-model.test.d.ts +2 -0
  157. package/dist/types/src/model/sheet-model.test.d.ts.map +1 -0
  158. package/dist/types/src/sanity.test.d.ts +2 -0
  159. package/dist/types/src/sanity.test.d.ts.map +1 -0
  160. package/dist/types/src/testing/index.d.ts +2 -0
  161. package/dist/types/src/testing/index.d.ts.map +1 -0
  162. package/dist/types/src/testing/testing.d.ts +8 -0
  163. package/dist/types/src/testing/testing.d.ts.map +1 -0
  164. package/dist/types/src/translations.d.ts +17 -12
  165. package/dist/types/src/translations.d.ts.map +1 -1
  166. package/dist/types/src/types.d.ts +86 -5
  167. package/dist/types/src/types.d.ts.map +1 -1
  168. package/dist/vendor/hyperformula.mjs +37145 -0
  169. package/package.json +55 -47
  170. package/src/SheetPlugin.tsx +50 -73
  171. package/src/components/CellEditor/CellEditor.stories.tsx +6 -6
  172. package/src/components/CellEditor/CellEditor.tsx +59 -9
  173. package/src/components/CellEditor/extension.test.ts +4 -6
  174. package/src/components/CellEditor/extension.ts +5 -6
  175. package/src/components/ComputeGraph/ComputeGraphContextProvider.tsx +20 -0
  176. package/src/components/ComputeGraph/index.ts +1 -3
  177. package/src/components/GridSheet/GridSheet.stories.tsx +36 -0
  178. package/src/components/GridSheet/GridSheet.tsx +171 -0
  179. package/src/components/GridSheet/util.ts +148 -0
  180. package/src/components/Sheet/Sheet.stories.tsx +52 -88
  181. package/src/components/Sheet/Sheet.tsx +87 -32
  182. package/src/components/Sheet/decorations.ts +62 -0
  183. package/src/components/Sheet/grid.ts +3 -3
  184. package/src/components/Sheet/nav.ts +19 -19
  185. package/src/components/Sheet/sheet-context.tsx +18 -80
  186. package/src/components/Sheet/threads.tsx +205 -0
  187. package/src/components/SheetContainer.tsx +68 -16
  188. package/src/components/Toolbar/Toolbar.tsx +53 -12
  189. package/src/components/index.ts +1 -0
  190. package/src/defs/index.ts +6 -0
  191. package/src/{model → defs}/types.test.ts +8 -9
  192. package/src/{model → defs}/types.ts +24 -14
  193. package/src/defs/util.ts +151 -0
  194. package/src/extensions/compute.stories.tsx +151 -0
  195. package/src/extensions/compute.ts +147 -0
  196. package/src/extensions/index.ts +5 -0
  197. package/src/graph/compute-graph-registry.ts +90 -0
  198. package/src/graph/compute-graph.stories.tsx +93 -0
  199. package/src/graph/compute-graph.test.ts +87 -0
  200. package/src/graph/compute-graph.ts +242 -0
  201. package/src/graph/compute-node.ts +63 -0
  202. package/src/{components/ComputeGraph → graph/functions}/async-function.ts +25 -15
  203. package/src/{components/ComputeGraph → graph/functions}/edge-function.ts +16 -14
  204. package/src/graph/functions/index.ts +7 -0
  205. package/src/graph/hyperformula.test.ts +14 -0
  206. package/src/graph/index.ts +8 -0
  207. package/src/graph/testing/index.ts +6 -0
  208. package/src/graph/testing/test-builder.ts +54 -0
  209. package/src/{components/ComputeGraph/custom.ts → graph/testing/test-plugin.ts} +44 -14
  210. package/src/graph/util.ts +8 -0
  211. package/src/hooks/hooks.stories.tsx +50 -0
  212. package/src/hooks/index.ts +7 -0
  213. package/src/hooks/useComputeGraph.ts +28 -0
  214. package/src/hooks/useFormattingModel.ts +11 -0
  215. package/src/hooks/useSheetModel.ts +40 -0
  216. package/src/meta.tsx +1 -5
  217. package/src/{components/Sheet/formatting.ts → model/formatting-model.ts} +20 -13
  218. package/src/model/index.ts +2 -3
  219. package/src/model/sheet-model.test.ts +57 -0
  220. package/src/model/sheet-model.ts +416 -0
  221. package/src/sanity.test.ts +40 -0
  222. package/src/testing/index.ts +5 -0
  223. package/src/testing/testing.tsx +68 -0
  224. package/src/translations.ts +6 -1
  225. package/src/types.ts +35 -10
  226. package/dist/lib/browser/SheetContainer-U4H5D34A.mjs.map +0 -7
  227. package/dist/lib/browser/chunk-APHOLYUB.mjs +0 -175
  228. package/dist/lib/browser/chunk-APHOLYUB.mjs.map +0 -7
  229. package/dist/lib/browser/chunk-D5AGLXJP.mjs.map +0 -7
  230. package/dist/lib/browser/chunk-FUAGSXA4.mjs.map +0 -7
  231. package/dist/lib/browser/chunk-JRL5LGCE.mjs.map +0 -7
  232. package/dist/lib/browser/chunk-NU4PBN33.mjs +0 -8
  233. package/dist/lib/browser/chunk-NU4PBN33.mjs.map +0 -7
  234. package/dist/lib/browser/testing.mjs +0 -92
  235. package/dist/lib/browser/testing.mjs.map +0 -7
  236. package/dist/lib/node/SheetContainer-AXQV3ZT5.cjs.map +0 -7
  237. package/dist/lib/node/chunk-5KKJ4NPP.cjs.map +0 -7
  238. package/dist/lib/node/chunk-BJ6ZD7MN.cjs.map +0 -7
  239. package/dist/lib/node/chunk-CN3RPESU.cjs +0 -202
  240. package/dist/lib/node/chunk-CN3RPESU.cjs.map +0 -7
  241. package/dist/lib/node/chunk-DSYKOI4E.cjs.map +0 -7
  242. package/dist/lib/node/chunk-PYXHNAAK.cjs +0 -40
  243. package/dist/lib/node/chunk-PYXHNAAK.cjs.map +0 -7
  244. package/dist/lib/node/testing.cjs +0 -111
  245. package/dist/lib/node/testing.cjs.map +0 -7
  246. package/dist/types/src/components/ComputeGraph/async-function.d.ts.map +0 -1
  247. package/dist/types/src/components/ComputeGraph/custom.d.ts +0 -21
  248. package/dist/types/src/components/ComputeGraph/custom.d.ts.map +0 -1
  249. package/dist/types/src/components/ComputeGraph/edge-function.d.ts +0 -20
  250. package/dist/types/src/components/ComputeGraph/edge-function.d.ts.map +0 -1
  251. package/dist/types/src/components/ComputeGraph/graph-context.d.ts +0 -12
  252. package/dist/types/src/components/ComputeGraph/graph-context.d.ts.map +0 -1
  253. package/dist/types/src/components/ComputeGraph/graph.browser.test.d.ts +0 -2
  254. package/dist/types/src/components/ComputeGraph/graph.browser.test.d.ts.map +0 -1
  255. package/dist/types/src/components/ComputeGraph/graph.d.ts +0 -26
  256. package/dist/types/src/components/ComputeGraph/graph.d.ts.map +0 -1
  257. package/dist/types/src/components/Sheet/formatting.d.ts +0 -14
  258. package/dist/types/src/components/Sheet/formatting.d.ts.map +0 -1
  259. package/dist/types/src/model/functions.d.ts.map +0 -1
  260. package/dist/types/src/model/model.browser.test.d.ts +0 -2
  261. package/dist/types/src/model/model.browser.test.d.ts.map +0 -1
  262. package/dist/types/src/model/model.d.ts.map +0 -1
  263. package/dist/types/src/model/types.d.ts.map +0 -1
  264. package/dist/types/src/model/types.test.d.ts.map +0 -1
  265. package/dist/types/src/model/util.d.ts +0 -15
  266. package/dist/types/src/model/util.d.ts.map +0 -1
  267. package/dist/types/src/testing.d.ts +0 -9
  268. package/dist/types/src/testing.d.ts.map +0 -1
  269. package/src/components/ComputeGraph/graph-context.tsx +0 -50
  270. package/src/components/ComputeGraph/graph.browser.test.ts +0 -50
  271. package/src/components/ComputeGraph/graph.ts +0 -62
  272. package/src/model/model.browser.test.ts +0 -100
  273. package/src/model/model.ts +0 -550
  274. package/src/model/util.ts +0 -36
  275. package/src/testing.ts +0 -50
  276. /package/dist/types/src/{model → defs}/types.test.d.ts +0 -0
  277. /package/src/{model/functions.ts → graph/functions/function-defs.ts} +0 -0
@@ -0,0 +1,2706 @@
1
+ import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
+ import {
3
+ SheetType,
4
+ ValueTypeEnum
5
+ } from "./chunk-2HAM45RC.mjs";
6
+ import {
7
+ SHEET_PLUGIN
8
+ } from "./chunk-IU2L277A.mjs";
9
+ import {
10
+ createSheetName
11
+ } from "./chunk-5WPZCXNS.mjs";
12
+
13
+ // packages/plugins/plugin-sheet/src/components/Sheet/Sheet.tsx
14
+ import { DndContext, DragOverlay, KeyboardSensor, MouseSensor, TouchSensor, useDraggable, useDroppable, useSensor, useSensors } from "@dnd-kit/core";
15
+ import { restrictToHorizontalAxis, restrictToVerticalAxis } from "@dnd-kit/modifiers";
16
+ import { getEventCoordinates, useCombinedRefs } from "@dnd-kit/utilities";
17
+ import { Function as FunctionIcon } from "@phosphor-icons/react";
18
+ import { Resizable } from "re-resizable";
19
+ import React6, { forwardRef, useEffect as useEffect4, useImperativeHandle, useMemo as useMemo4, useRef, useState as useState5 } from "react";
20
+ import { createPortal } from "react-dom";
21
+ import { useResizeDetector } from "react-resize-detector";
22
+ import { debounce as debounce2 } from "@dxos/async";
23
+ import { fullyQualifiedId as fullyQualifiedId2, createDocAccessor } from "@dxos/client/echo";
24
+ import { log as log2 } from "@dxos/log";
25
+ import { ATTENABLE_ATTRIBUTE, useAttendableAttributes, useAttention, useAttentionPath } from "@dxos/react-ui-attention";
26
+ import { mx as mx2 } from "@dxos/react-ui-theme";
27
+
28
+ // packages/plugins/plugin-sheet/src/components/Sheet/grid.ts
29
+ import { useEffect, useState } from "react";
30
+
31
+ // packages/plugins/plugin-sheet/src/defs/types.ts
32
+ import { invariant } from "@dxos/invariant";
33
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/plugins/plugin-sheet/src/defs/types.ts";
34
+ var DEFAULT_ROWS = 50;
35
+ var DEFAULT_COLUMNS = 26;
36
+ var MAX_ROWS = 500;
37
+ var MAX_COLUMNS = 26 * 2;
38
+ var posEquals = (a, b) => {
39
+ return a?.col === b?.col && a?.row === b?.row;
40
+ };
41
+ var columnLetter = (col) => {
42
+ invariant(col < MAX_COLUMNS, `Invalid column: ${col}`, {
43
+ F: __dxlog_file,
44
+ L: 26,
45
+ S: void 0,
46
+ A: [
47
+ "col < MAX_COLUMNS",
48
+ "`Invalid column: ${col}`"
49
+ ]
50
+ });
51
+ return (col >= 26 ? String.fromCharCode("A".charCodeAt(0) + Math.floor(col / 26) - 1) : "") + String.fromCharCode("A".charCodeAt(0) + col % 26);
52
+ };
53
+ var addressToA1Notation = ({ col, row }) => {
54
+ return `${columnLetter(col)}${row + 1}`;
55
+ };
56
+ var addressFromA1Notation = (ref) => {
57
+ const match = ref.match(/([A-Z]+)(\d+)/);
58
+ invariant(match, `Invalid notation: ${ref}`, {
59
+ F: __dxlog_file,
60
+ L: 40,
61
+ S: void 0,
62
+ A: [
63
+ "match",
64
+ "`Invalid notation: ${ref}`"
65
+ ]
66
+ });
67
+ return {
68
+ row: parseInt(match[2], 10) - 1,
69
+ col: match[1].split("").reduce((acc, c) => acc * 26 + c.charCodeAt(0) - "A".charCodeAt(0) + 1, 0) - 1
70
+ };
71
+ };
72
+ var rangeToA1Notation = (range) => {
73
+ return [
74
+ range?.from && addressToA1Notation(range?.from),
75
+ range?.to && addressToA1Notation(range?.to)
76
+ ].filter(Boolean).join(":");
77
+ };
78
+ var inRange = (range, cell) => {
79
+ if (!range) {
80
+ return false;
81
+ }
82
+ const { from, to } = range;
83
+ if (from && posEquals(from, cell) || to && posEquals(to, cell)) {
84
+ return true;
85
+ }
86
+ if (!from || !to) {
87
+ return false;
88
+ }
89
+ const { col: c1, row: r1 } = from;
90
+ const { col: c2, row: r2 } = to;
91
+ const cMin = Math.min(c1, c2);
92
+ const cMax = Math.max(c1, c2);
93
+ const rMin = Math.min(r1, r2);
94
+ const rMax = Math.max(r1, r2);
95
+ const { col, row } = cell;
96
+ return col >= cMin && col <= cMax && row >= rMin && row <= rMax;
97
+ };
98
+
99
+ // packages/plugins/plugin-sheet/src/defs/util.ts
100
+ import { randomBytes } from "@dxos/crypto";
101
+ import { create } from "@dxos/echo-schema";
102
+ var ApiError = class extends Error {
103
+ };
104
+ var ReadonlyException = class extends ApiError {
105
+ };
106
+ var RangeException = class extends ApiError {
107
+ constructor(n) {
108
+ super();
109
+ }
110
+ };
111
+ var createIndex = (length = 8) => {
112
+ const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
113
+ const charactersLength = characters.length;
114
+ const randomBuffer = randomBytes(length);
115
+ return Array.from(randomBuffer, (byte) => characters[byte % charactersLength]).join("");
116
+ };
117
+ var createIndices = (length) => Array.from({
118
+ length
119
+ }).map(() => createIndex());
120
+ var insertIndices = (indices, i, n, max) => {
121
+ if (i + n > max) {
122
+ throw new RangeException(i + n);
123
+ }
124
+ const idx = createIndices(n);
125
+ indices.splice(i, 0, ...idx);
126
+ };
127
+ var initialize = (sheet, { rows = DEFAULT_ROWS, columns = DEFAULT_COLUMNS } = {}) => {
128
+ if (!sheet.rows.length) {
129
+ insertIndices(sheet.rows, 0, rows, MAX_ROWS);
130
+ }
131
+ if (!sheet.columns.length) {
132
+ insertIndices(sheet.columns, 0, columns, MAX_COLUMNS);
133
+ }
134
+ };
135
+ var createSheet = ({ name, cells, ...size } = {}) => {
136
+ const sheet = create(SheetType, {
137
+ name,
138
+ cells: {},
139
+ rows: [],
140
+ columns: [],
141
+ rowMeta: {},
142
+ columnMeta: {},
143
+ formatting: {}
144
+ });
145
+ initialize(sheet, size);
146
+ if (cells) {
147
+ Object.entries(cells).forEach(([key, { value }]) => {
148
+ const idx = addressToIndex(sheet, addressFromA1Notation(key));
149
+ sheet.cells[idx] = {
150
+ value
151
+ };
152
+ });
153
+ }
154
+ return sheet;
155
+ };
156
+ var addressToIndex = (sheet, cell) => {
157
+ return `${sheet.columns[cell.col]}@${sheet.rows[cell.row]}`;
158
+ };
159
+ var addressFromIndex = (sheet, idx) => {
160
+ const [column, row] = idx.split("@");
161
+ return {
162
+ col: sheet.columns.indexOf(column),
163
+ row: sheet.rows.indexOf(row)
164
+ };
165
+ };
166
+ var rangeFromIndex = (sheet, idx) => {
167
+ const [from, to] = idx.split(":").map((index) => addressFromIndex(sheet, index));
168
+ return {
169
+ from,
170
+ to
171
+ };
172
+ };
173
+ var closest = (cursor, cells) => {
174
+ let closestCell;
175
+ let closestDistance = Number.MAX_SAFE_INTEGER;
176
+ for (const cell of cells) {
177
+ const distance = Math.abs(cell.row - cursor.row) + Math.abs(cell.col - cursor.col);
178
+ if (distance < closestDistance) {
179
+ closestCell = cell;
180
+ closestDistance = distance;
181
+ }
182
+ }
183
+ return closestCell;
184
+ };
185
+ var compareIndexPositions = (sheet, indexA, indexB) => {
186
+ const { row: rowA, col: columnA } = addressFromIndex(sheet, indexA);
187
+ const { row: rowB, col: columnB } = addressFromIndex(sheet, indexB);
188
+ if (rowA !== rowB) {
189
+ return rowA - rowB;
190
+ } else {
191
+ return columnA - columnB;
192
+ }
193
+ };
194
+
195
+ // packages/plugins/plugin-sheet/src/components/Sheet/grid.ts
196
+ var axisWidth = "calc(var(--rail-size)-2px)";
197
+ var axisHeight = 34;
198
+ var minWidth = 40;
199
+ var maxWidth = 800;
200
+ var minHeight = axisHeight;
201
+ var maxHeight = 400;
202
+ var defaultWidth = 200;
203
+ var defaultHeight = minHeight;
204
+ var CELL_DATA_KEY = "cell";
205
+ var useGridLayout = ({ scroller, size, rows, columns, rowSizes, columnSizes }) => {
206
+ const [rowPositions, setRowPositions] = useState([]);
207
+ useEffect(() => {
208
+ if (!rowSizes) {
209
+ return;
210
+ }
211
+ let y = 0;
212
+ setRowPositions(rows.map((idx, i) => {
213
+ const height2 = rowSizes?.[idx] ?? defaultHeight;
214
+ const top = y;
215
+ y += height2 - 1;
216
+ return {
217
+ row: i,
218
+ top,
219
+ height: height2
220
+ };
221
+ }));
222
+ }, [
223
+ rows,
224
+ rowSizes
225
+ ]);
226
+ const [columnPositions, setColumnPositions] = useState([]);
227
+ useEffect(() => {
228
+ if (!columns) {
229
+ return;
230
+ }
231
+ let x = 0;
232
+ setColumnPositions(columns.map((idx, i) => {
233
+ const width2 = columnSizes?.[idx] ?? defaultWidth;
234
+ const left = x;
235
+ x += width2 - 1;
236
+ return {
237
+ col: i,
238
+ left,
239
+ width: width2
240
+ };
241
+ }));
242
+ }, [
243
+ columns,
244
+ columnSizes
245
+ ]);
246
+ const height = rowPositions.length ? rowPositions[rowPositions.length - 1].top + rowPositions[rowPositions.length - 1].height : 0;
247
+ const width = columnPositions.length ? columnPositions[columnPositions.length - 1].left + columnPositions[columnPositions.length - 1].width : 0;
248
+ const [{ rowRange, columnRange }, setWindow] = useState({
249
+ rowRange: [],
250
+ columnRange: []
251
+ });
252
+ useEffect(() => {
253
+ const handleScroll = () => {
254
+ if (!scroller) {
255
+ return;
256
+ }
257
+ const { scrollLeft: left, scrollTop: top, clientWidth: width2, clientHeight: height2 } = scroller;
258
+ let rowStart = 0;
259
+ let rowEnd = 0;
260
+ for (let i = 0; i < rowPositions.length; i++) {
261
+ const row = rowPositions[i];
262
+ if (row.top <= top) {
263
+ rowStart = i;
264
+ }
265
+ if (row.top + row.height >= top + height2) {
266
+ rowEnd = i;
267
+ break;
268
+ }
269
+ }
270
+ let columnStart = 0;
271
+ let columnEnd = 0;
272
+ for (let i = 0; i < columnPositions.length; i++) {
273
+ const column = columnPositions[i];
274
+ if (column.left <= left) {
275
+ columnStart = i;
276
+ }
277
+ if (column.left + column.width >= left + width2) {
278
+ columnEnd = i;
279
+ break;
280
+ }
281
+ }
282
+ const overscan = 5;
283
+ setWindow({
284
+ rowRange: rowPositions.slice(Math.max(0, rowStart - overscan), Math.min(rowPositions.length, rowEnd + overscan)),
285
+ columnRange: columnPositions.slice(Math.max(0, columnStart - overscan), Math.min(columnPositions.length, columnEnd + overscan))
286
+ });
287
+ };
288
+ scroller?.addEventListener("scroll", handleScroll);
289
+ handleScroll();
290
+ return () => {
291
+ scroller?.removeEventListener("scroll", handleScroll);
292
+ };
293
+ }, [
294
+ size.width,
295
+ size.height,
296
+ rowPositions,
297
+ columnPositions
298
+ ]);
299
+ return {
300
+ width,
301
+ height,
302
+ rowRange,
303
+ columnRange
304
+ };
305
+ };
306
+ var getCellAtPointer = (event) => {
307
+ const element = document.elementFromPoint(event.clientX, event.clientY);
308
+ const root = element?.closest(`[data-${CELL_DATA_KEY}]`);
309
+ if (root) {
310
+ const value = root.dataset[CELL_DATA_KEY];
311
+ if (value) {
312
+ return addressFromA1Notation(value);
313
+ }
314
+ }
315
+ };
316
+ var getCellElement = (root, cell) => {
317
+ const pos = addressToA1Notation(cell);
318
+ return root.querySelector(`[data-${CELL_DATA_KEY}="${pos}"]`);
319
+ };
320
+
321
+ // packages/plugins/plugin-sheet/src/components/Sheet/nav.ts
322
+ import { useState as useState2 } from "react";
323
+ var handleNav = (ev, cursor, range, size) => {
324
+ if (cursor && ev.shiftKey) {
325
+ const opposite = range?.to ?? {
326
+ ...cursor
327
+ };
328
+ switch (ev.key) {
329
+ case "ArrowUp": {
330
+ if (opposite.row > 0) {
331
+ opposite.row -= 1;
332
+ }
333
+ break;
334
+ }
335
+ case "ArrowDown": {
336
+ if (opposite.row < size.numRows - 1) {
337
+ opposite.row += 1;
338
+ }
339
+ break;
340
+ }
341
+ case "ArrowLeft": {
342
+ if (opposite.col > 0) {
343
+ opposite.col -= 1;
344
+ }
345
+ break;
346
+ }
347
+ case "ArrowRight": {
348
+ if (opposite.col < size.numCols - 1) {
349
+ opposite.col += 1;
350
+ }
351
+ break;
352
+ }
353
+ }
354
+ return {
355
+ cursor,
356
+ range: {
357
+ from: cursor,
358
+ to: opposite
359
+ }
360
+ };
361
+ }
362
+ const next = handleArrowNav(ev, cursor, size);
363
+ return {
364
+ cursor: next
365
+ };
366
+ };
367
+ var handleArrowNav = (ev, cursor, { numRows, numCols }) => {
368
+ switch (ev.key) {
369
+ case "ArrowUp":
370
+ if (cursor === void 0) {
371
+ return {
372
+ row: 0,
373
+ col: 0
374
+ };
375
+ } else if (cursor.row > 0) {
376
+ return {
377
+ row: ev.metaKey ? 0 : cursor.row - 1,
378
+ col: cursor.col
379
+ };
380
+ }
381
+ break;
382
+ case "ArrowDown":
383
+ if (cursor === void 0) {
384
+ return {
385
+ row: 0,
386
+ col: 0
387
+ };
388
+ } else if (cursor.row < numRows - 1) {
389
+ return {
390
+ row: ev.metaKey ? numRows - 1 : cursor.row + 1,
391
+ col: cursor.col
392
+ };
393
+ }
394
+ break;
395
+ case "ArrowLeft":
396
+ if (cursor === void 0) {
397
+ return {
398
+ row: 0,
399
+ col: 0
400
+ };
401
+ } else if (cursor.col > 0) {
402
+ return {
403
+ row: cursor.row,
404
+ col: ev.metaKey ? 0 : cursor.col - 1
405
+ };
406
+ }
407
+ break;
408
+ case "ArrowRight":
409
+ if (cursor === void 0) {
410
+ return {
411
+ row: 0,
412
+ col: 0
413
+ };
414
+ } else if (cursor.col < numCols - 1) {
415
+ return {
416
+ row: cursor.row,
417
+ col: ev.metaKey ? numCols - 1 : cursor.col + 1
418
+ };
419
+ }
420
+ break;
421
+ case "Home":
422
+ return {
423
+ row: 0,
424
+ col: 0
425
+ };
426
+ case "End":
427
+ return {
428
+ row: numRows - 1,
429
+ col: numCols - 1
430
+ };
431
+ }
432
+ };
433
+ var useRangeSelect = (cb) => {
434
+ const [from, setFrom] = useState2();
435
+ const [to, setTo] = useState2();
436
+ const onMouseDown = (ev) => {
437
+ const current = getCellAtPointer(ev);
438
+ setFrom(current);
439
+ if (current) {
440
+ setTimeout(() => cb("start", {
441
+ from: current
442
+ }));
443
+ }
444
+ };
445
+ const onMouseMove = (ev) => {
446
+ if (from) {
447
+ let current = getCellAtPointer(ev);
448
+ if (posEquals(current, from)) {
449
+ current = void 0;
450
+ }
451
+ setTo(current);
452
+ setTimeout(() => cb("move", {
453
+ from,
454
+ to: current
455
+ }));
456
+ }
457
+ };
458
+ const onMouseUp = (ev) => {
459
+ if (from) {
460
+ let current = getCellAtPointer(ev);
461
+ if (posEquals(current, from)) {
462
+ current = void 0;
463
+ }
464
+ setFrom(void 0);
465
+ setTo(void 0);
466
+ setTimeout(() => cb("end", current ? {
467
+ from,
468
+ to: current
469
+ } : void 0));
470
+ }
471
+ };
472
+ return {
473
+ range: from ? {
474
+ from,
475
+ to
476
+ } : void 0,
477
+ handlers: {
478
+ onMouseDown,
479
+ onMouseMove,
480
+ onMouseUp
481
+ }
482
+ };
483
+ };
484
+
485
+ // packages/plugins/plugin-sheet/src/components/Sheet/sheet-context.tsx
486
+ import React3, { createContext as createContext2, useContext as useContext2, useMemo as useMemo2, useState as useState4 } from "react";
487
+ import { invariant as invariant3 } from "@dxos/invariant";
488
+
489
+ // packages/plugins/plugin-sheet/src/components/Sheet/decorations.ts
490
+ import { create as create2 } from "@dxos/echo-schema";
491
+ var createDecorations = () => {
492
+ const { decorations } = create2({
493
+ decorations: {}
494
+ });
495
+ const addDecoration = (cellIndex, decorator) => {
496
+ decorations[cellIndex] = [
497
+ ...decorations[cellIndex] || [],
498
+ decorator
499
+ ];
500
+ };
501
+ const removeDecoration = (cellIndex, type) => {
502
+ if (type) {
503
+ decorations[cellIndex] = (decorations[cellIndex] || []).filter((d) => d.type !== type);
504
+ } else {
505
+ delete decorations[cellIndex];
506
+ }
507
+ };
508
+ const getDecorationsForCell = (cellIndex) => {
509
+ return decorations[cellIndex];
510
+ };
511
+ const getAllDecorations = () => {
512
+ const result = [];
513
+ for (const decoratorArray of Object.values(decorations)) {
514
+ for (const decorator of decoratorArray) {
515
+ result.push(decorator);
516
+ }
517
+ }
518
+ return result;
519
+ };
520
+ return {
521
+ addDecoration,
522
+ removeDecoration,
523
+ getDecorationsForCell,
524
+ getAllDecorations
525
+ };
526
+ };
527
+
528
+ // packages/plugins/plugin-sheet/src/hooks/useComputeGraph.ts
529
+ import { useContext } from "react";
530
+ import { raise } from "@dxos/debug";
531
+ import { useAsyncState } from "@dxos/react-hooks";
532
+
533
+ // packages/plugins/plugin-sheet/src/components/index.ts
534
+ import React2 from "react";
535
+
536
+ // packages/plugins/plugin-sheet/src/components/ComputeGraph/ComputeGraphContextProvider.tsx
537
+ import React, { createContext } from "react";
538
+ var ComputeGraphContext = /* @__PURE__ */ createContext(void 0);
539
+ var ComputeGraphContextProvider = ({ registry, children }) => {
540
+ return /* @__PURE__ */ React.createElement(ComputeGraphContext.Provider, {
541
+ value: {
542
+ registry
543
+ }
544
+ }, children);
545
+ };
546
+
547
+ // packages/plugins/plugin-sheet/src/components/index.ts
548
+ var SheetContainer = React2.lazy(() => import("./SheetContainer-FOZD2WLT.mjs"));
549
+
550
+ // packages/plugins/plugin-sheet/src/hooks/useComputeGraph.ts
551
+ var useComputeGraph = (space) => {
552
+ const { registry } = useContext(ComputeGraphContext) ?? raise(new Error("Missing ComputeGraphContext"));
553
+ const [graph] = useAsyncState(async () => {
554
+ if (space) {
555
+ const graph2 = registry.getOrCreateGraph(space);
556
+ await graph2.open();
557
+ return graph2;
558
+ }
559
+ }, [
560
+ space,
561
+ registry
562
+ ]);
563
+ return graph;
564
+ };
565
+
566
+ // packages/plugins/plugin-sheet/src/hooks/useFormattingModel.ts
567
+ import { useMemo } from "react";
568
+
569
+ // packages/plugins/plugin-sheet/src/model/formatting-model.ts
570
+ var FormattingModel = class {
571
+ constructor(_model) {
572
+ this._model = _model;
573
+ }
574
+ /**
575
+ * Get formatted string value and className for cell.
576
+ */
577
+ getFormatting(cell) {
578
+ const value = this._model.getValue(cell);
579
+ if (value === void 0 || value === null) {
580
+ return {};
581
+ }
582
+ const locales = void 0;
583
+ const idx = addressToIndex(this._model.sheet, cell);
584
+ let formatting = this._model.sheet.formatting?.[idx] ?? {};
585
+ const classNames = [
586
+ ...formatting?.classNames ?? []
587
+ ];
588
+ for (const [idx2, _formatting] of Object.entries(this._model.sheet.formatting)) {
589
+ const range = rangeFromIndex(this._model.sheet, idx2);
590
+ if (inRange(range, cell)) {
591
+ if (_formatting.classNames) {
592
+ classNames.push(..._formatting.classNames);
593
+ }
594
+ if (_formatting.type) {
595
+ formatting = _formatting;
596
+ }
597
+ }
598
+ }
599
+ const defaultNumber = "justify-end font-mono";
600
+ const type = formatting?.type ?? this._model.getValueType(cell);
601
+ switch (type) {
602
+ case ValueTypeEnum.Boolean: {
603
+ return {
604
+ value: value.toLocaleString().toUpperCase(),
605
+ classNames: [
606
+ ...classNames,
607
+ value ? "!text-greenText" : "!text-orangeText"
608
+ ]
609
+ };
610
+ }
611
+ //
612
+ // Numbers.
613
+ //
614
+ case ValueTypeEnum.Number: {
615
+ return {
616
+ value: value.toLocaleString(locales),
617
+ classNames: [
618
+ ...classNames,
619
+ defaultNumber
620
+ ]
621
+ };
622
+ }
623
+ case ValueTypeEnum.Percent: {
624
+ return {
625
+ value: value * 100 + "%",
626
+ classNames: [
627
+ ...classNames,
628
+ defaultNumber
629
+ ]
630
+ };
631
+ }
632
+ case ValueTypeEnum.Currency: {
633
+ return {
634
+ value: value.toLocaleString(locales, {
635
+ style: "currency",
636
+ currency: "USD",
637
+ minimumFractionDigits: 2,
638
+ maximumFractionDigits: 2
639
+ }),
640
+ classNames: [
641
+ ...classNames,
642
+ defaultNumber
643
+ ]
644
+ };
645
+ }
646
+ //
647
+ // Dates.
648
+ //
649
+ case ValueTypeEnum.DateTime: {
650
+ const date = this._model.toLocalDate(value);
651
+ return {
652
+ value: date.toLocaleString(locales),
653
+ classNames
654
+ };
655
+ }
656
+ case ValueTypeEnum.Date: {
657
+ const date = this._model.toLocalDate(value);
658
+ return {
659
+ value: date.toLocaleDateString(locales),
660
+ classNames
661
+ };
662
+ }
663
+ case ValueTypeEnum.Time: {
664
+ const date = this._model.toLocalDate(value);
665
+ return {
666
+ value: date.toLocaleTimeString(locales),
667
+ classNames
668
+ };
669
+ }
670
+ default: {
671
+ return {
672
+ value: String(value),
673
+ classNames
674
+ };
675
+ }
676
+ }
677
+ }
678
+ };
679
+
680
+ // packages/plugins/plugin-sheet/src/model/sheet-model.ts
681
+ import { Event } from "@dxos/async";
682
+ import { Resource } from "@dxos/context";
683
+ import { getTypename } from "@dxos/echo-schema";
684
+ import { invariant as invariant2 } from "@dxos/invariant";
685
+ import { PublicKey } from "@dxos/keys";
686
+ import { log } from "@dxos/log";
687
+ import { DetailedCellError, ExportedCellChange } from "#hyperformula";
688
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/plugins/plugin-sheet/src/model/sheet-model.ts";
689
+ var typeMap = {
690
+ BOOLEAN: ValueTypeEnum.Boolean,
691
+ NUMBER_RAW: ValueTypeEnum.Number,
692
+ NUMBER_PERCENT: ValueTypeEnum.Percent,
693
+ NUMBER_CURRENCY: ValueTypeEnum.Currency,
694
+ NUMBER_DATETIME: ValueTypeEnum.DateTime,
695
+ NUMBER_DATE: ValueTypeEnum.Date,
696
+ NUMBER_TIME: ValueTypeEnum.Time
697
+ };
698
+ var getTopLeft = (range) => {
699
+ const to = range.to ?? range.from;
700
+ return {
701
+ row: Math.min(range.from.row, to.row),
702
+ col: Math.min(range.from.col, to.col)
703
+ };
704
+ };
705
+ var toSimpleCellAddress = (sheet, cell) => ({
706
+ sheet,
707
+ row: cell.row,
708
+ col: cell.col
709
+ });
710
+ var toModelRange = (sheet, range) => ({
711
+ start: toSimpleCellAddress(sheet, range.from),
712
+ end: toSimpleCellAddress(sheet, range.to ?? range.from)
713
+ });
714
+ var SheetModel = class extends Resource {
715
+ constructor(_graph, _sheet, _options = {}) {
716
+ super();
717
+ this._graph = _graph;
718
+ this._sheet = _sheet;
719
+ this._options = _options;
720
+ this.id = `model-${PublicKey.random().truncate()}`;
721
+ this.update = new Event();
722
+ }
723
+ get graph() {
724
+ return this._graph;
725
+ }
726
+ get sheet() {
727
+ return this._sheet;
728
+ }
729
+ get readonly() {
730
+ return this._options.readonly;
731
+ }
732
+ get bounds() {
733
+ return {
734
+ rows: this._sheet.rows.length,
735
+ columns: this._sheet.columns.length
736
+ };
737
+ }
738
+ /**
739
+ * Initialize sheet and engine.
740
+ */
741
+ async _open() {
742
+ log("initialize", {
743
+ id: this.id
744
+ }, {
745
+ F: __dxlog_file2,
746
+ L: 104,
747
+ S: this,
748
+ C: (f, a) => f(...a)
749
+ });
750
+ initialize(this._sheet);
751
+ this._node = this._graph.getOrCreateNode(createSheetName({
752
+ type: getTypename(this._sheet),
753
+ id: this._sheet.id
754
+ }));
755
+ await this._node.open();
756
+ const unsubscribe = this._node.update.on((event) => this.update.emit(event));
757
+ this._ctx.onDispose(unsubscribe);
758
+ this.reset();
759
+ }
760
+ /**
761
+ * Update engine.
762
+ * NOTE: This resets the undo history.
763
+ * @deprecated
764
+ */
765
+ reset() {
766
+ invariant2(this._node, void 0, {
767
+ F: __dxlog_file2,
768
+ L: 124,
769
+ S: this,
770
+ A: [
771
+ "this._node",
772
+ ""
773
+ ]
774
+ });
775
+ this._node.graph.hf.clearSheet(this._node.sheetId);
776
+ Object.entries(this._sheet.cells).forEach(([key, { value }]) => {
777
+ invariant2(this._node, void 0, {
778
+ F: __dxlog_file2,
779
+ L: 127,
780
+ S: this,
781
+ A: [
782
+ "this._node",
783
+ ""
784
+ ]
785
+ });
786
+ const { col, row } = addressFromIndex(this._sheet, key);
787
+ if (typeof value === "string" && value.charAt(0) === "=") {
788
+ value = this._graph.mapFormulaToNative(this._graph.mapFunctionBindingFromId(this.mapFormulaIndicesToRefs(value)));
789
+ }
790
+ this._node.graph.hf.setCellContents({
791
+ sheet: this._node.sheetId,
792
+ row,
793
+ col
794
+ }, value);
795
+ });
796
+ }
797
+ /**
798
+ * Recalculate formulas.
799
+ * NOTE: This resets the undo history.
800
+ * https://hyperformula.handsontable.com/guide/volatile-functions.html#volatile-actions
801
+ * @deprecated
802
+ */
803
+ // TODO(burdon): Remove.
804
+ recalculate() {
805
+ this._node?.graph.hf.rebuildAndRecalculate();
806
+ }
807
+ insertRows(i, n = 1) {
808
+ insertIndices(this._sheet.rows, i, n, MAX_ROWS);
809
+ this.reset();
810
+ }
811
+ insertColumns(i, n = 1) {
812
+ insertIndices(this._sheet.columns, i, n, MAX_COLUMNS);
813
+ this.reset();
814
+ }
815
+ //
816
+ // Undoable actions.
817
+ // TODO(burdon): Group undoable methods; consistently update hf/sheet.
818
+ //
819
+ /**
820
+ * Clear range of values.
821
+ */
822
+ clear(range) {
823
+ invariant2(this._node, void 0, {
824
+ F: __dxlog_file2,
825
+ L: 169,
826
+ S: this,
827
+ A: [
828
+ "this._node",
829
+ ""
830
+ ]
831
+ });
832
+ const topLeft = getTopLeft(range);
833
+ const values = this._iterRange(range, () => null);
834
+ this._node.graph.hf.setCellContents(toSimpleCellAddress(this._node.sheetId, topLeft), values);
835
+ this._iterRange(range, (cell) => {
836
+ const idx = addressToIndex(this._sheet, cell);
837
+ delete this._sheet.cells[idx];
838
+ });
839
+ }
840
+ cut(range) {
841
+ invariant2(this._node, void 0, {
842
+ F: __dxlog_file2,
843
+ L: 180,
844
+ S: this,
845
+ A: [
846
+ "this._node",
847
+ ""
848
+ ]
849
+ });
850
+ this._node.graph.hf.cut(toModelRange(this._node.sheetId, range));
851
+ this._iterRange(range, (cell) => {
852
+ const idx = addressToIndex(this._sheet, cell);
853
+ delete this._sheet.cells[idx];
854
+ });
855
+ }
856
+ copy(range) {
857
+ invariant2(this._node, void 0, {
858
+ F: __dxlog_file2,
859
+ L: 189,
860
+ S: this,
861
+ A: [
862
+ "this._node",
863
+ ""
864
+ ]
865
+ });
866
+ this._node.graph.hf.copy(toModelRange(this._node.sheetId, range));
867
+ }
868
+ paste(cell) {
869
+ invariant2(this._node, void 0, {
870
+ F: __dxlog_file2,
871
+ L: 194,
872
+ S: this,
873
+ A: [
874
+ "this._node",
875
+ ""
876
+ ]
877
+ });
878
+ if (!this._node.graph.hf.isClipboardEmpty()) {
879
+ const changes = this._node.graph.hf.paste(toSimpleCellAddress(this._node.sheetId, cell));
880
+ for (const change of changes) {
881
+ if (change instanceof ExportedCellChange) {
882
+ const { address, newValue } = change;
883
+ const idx = addressToIndex(this._sheet, {
884
+ row: address.row,
885
+ col: address.col
886
+ });
887
+ this._sheet.cells[idx] = {
888
+ value: newValue
889
+ };
890
+ }
891
+ }
892
+ }
893
+ }
894
+ // TODO(burdon): Display undo/redo state.
895
+ undo() {
896
+ invariant2(this._node, void 0, {
897
+ F: __dxlog_file2,
898
+ L: 209,
899
+ S: this,
900
+ A: [
901
+ "this._node",
902
+ ""
903
+ ]
904
+ });
905
+ if (this._node.graph.hf.isThereSomethingToUndo()) {
906
+ this._node.graph.hf.undo();
907
+ }
908
+ }
909
+ redo() {
910
+ invariant2(this._node, void 0, {
911
+ F: __dxlog_file2,
912
+ L: 217,
913
+ S: this,
914
+ A: [
915
+ "this._node",
916
+ ""
917
+ ]
918
+ });
919
+ if (this._node.graph.hf.isThereSomethingToRedo()) {
920
+ this._node.graph.hf.redo();
921
+ }
922
+ }
923
+ /**
924
+ * Get value from sheet.
925
+ */
926
+ getCellValue(cell) {
927
+ const idx = addressToIndex(this._sheet, cell);
928
+ return this._sheet.cells[idx]?.value ?? null;
929
+ }
930
+ /**
931
+ * Get value as a string for editing.
932
+ */
933
+ getCellText(cell) {
934
+ const value = this.getCellValue(cell);
935
+ if (value == null) {
936
+ return void 0;
937
+ }
938
+ if (typeof value === "string" && value.charAt(0) === "=") {
939
+ return this._graph.mapFunctionBindingFromId(this.mapFormulaIndicesToRefs(value));
940
+ } else {
941
+ return String(value);
942
+ }
943
+ }
944
+ /**
945
+ * Get array of raw values from sheet.
946
+ */
947
+ getCellValues(range) {
948
+ return this._iterRange(range, (cell) => this.getCellValue(cell));
949
+ }
950
+ /**
951
+ * Gets the regular or computed value from the engine.
952
+ */
953
+ getValue(cell) {
954
+ invariant2(this._node, void 0, {
955
+ F: __dxlog_file2,
956
+ L: 260,
957
+ S: this,
958
+ A: [
959
+ "this._node",
960
+ ""
961
+ ]
962
+ });
963
+ const value = this._node.graph.hf.getCellValue(toSimpleCellAddress(this._node.sheetId, cell));
964
+ if (value instanceof DetailedCellError) {
965
+ return value.toString();
966
+ }
967
+ return value;
968
+ }
969
+ /**
970
+ * Get value type.
971
+ */
972
+ getValueType(cell) {
973
+ invariant2(this._node, void 0, {
974
+ F: __dxlog_file2,
975
+ L: 273,
976
+ S: this,
977
+ A: [
978
+ "this._node",
979
+ ""
980
+ ]
981
+ });
982
+ const addr = toSimpleCellAddress(this._node.sheetId, cell);
983
+ const type = this._node.graph.hf.getCellValueDetailedType(addr);
984
+ return typeMap[type];
985
+ }
986
+ /**
987
+ * Sets the value, updating the sheet and engine.
988
+ */
989
+ setValue(cell, value) {
990
+ invariant2(this._node, void 0, {
991
+ F: __dxlog_file2,
992
+ L: 283,
993
+ S: this,
994
+ A: [
995
+ "this._node",
996
+ ""
997
+ ]
998
+ });
999
+ if (this._options.readonly) {
1000
+ throw new ReadonlyException();
1001
+ }
1002
+ let refresh = false;
1003
+ if (cell.row >= this._sheet.rows.length) {
1004
+ insertIndices(this._sheet.rows, cell.row, 1, MAX_ROWS);
1005
+ refresh = true;
1006
+ }
1007
+ if (cell.col >= this._sheet.columns.length) {
1008
+ insertIndices(this._sheet.columns, cell.col, 1, MAX_COLUMNS);
1009
+ refresh = true;
1010
+ }
1011
+ if (refresh) {
1012
+ this.reset();
1013
+ }
1014
+ this._node.graph.hf.setCellContents({
1015
+ sheet: this._node.sheetId,
1016
+ row: cell.row,
1017
+ col: cell.col
1018
+ }, [
1019
+ [
1020
+ typeof value === "string" && value.charAt(0) === "=" ? this._graph.mapFormulaToNative(value) : value
1021
+ ]
1022
+ ]);
1023
+ const idx = addressToIndex(this._sheet, cell);
1024
+ if (value === void 0 || value === null) {
1025
+ delete this._sheet.cells[idx];
1026
+ } else {
1027
+ if (typeof value === "string" && value.charAt(0) === "=") {
1028
+ value = this._graph.mapFunctionBindingToId(this.mapFormulaRefsToIndices(value));
1029
+ }
1030
+ this._sheet.cells[idx] = {
1031
+ value
1032
+ };
1033
+ }
1034
+ }
1035
+ /**
1036
+ * Sets values from a simple map.
1037
+ */
1038
+ setValues(values) {
1039
+ Object.entries(values).forEach(([key, { value }]) => {
1040
+ this.setValue(addressFromA1Notation(key), value);
1041
+ });
1042
+ }
1043
+ /**
1044
+ * Iterate range.
1045
+ */
1046
+ _iterRange(range, cb) {
1047
+ const to = range.to ?? range.from;
1048
+ const rowRange = [
1049
+ Math.min(range.from.row, to.row),
1050
+ Math.max(range.from.row, to.row)
1051
+ ];
1052
+ const columnRange = [
1053
+ Math.min(range.from.col, to.col),
1054
+ Math.max(range.from.col, to.col)
1055
+ ];
1056
+ const rows = [];
1057
+ for (let row = rowRange[0]; row <= rowRange[1]; row++) {
1058
+ const rowCells = [];
1059
+ for (let column = columnRange[0]; column <= columnRange[1]; column++) {
1060
+ const value = cb({
1061
+ row,
1062
+ col: column
1063
+ });
1064
+ if (value !== void 0) {
1065
+ rowCells.push(value);
1066
+ }
1067
+ }
1068
+ rows.push(rowCells);
1069
+ }
1070
+ return rows;
1071
+ }
1072
+ // TODO(burdon): Delete index.
1073
+ _deleteIndices(indices, i, n) {
1074
+ throw new Error("Not implemented");
1075
+ }
1076
+ // TODO(burdon): Move. Cannot use fractional without changing. Switch back to using unique IDs?
1077
+ _moveIndices(indices, i, j, n) {
1078
+ throw new Error("Not implemented");
1079
+ }
1080
+ //
1081
+ // Indices.
1082
+ //
1083
+ /**
1084
+ * Map from A1 notation to indices.
1085
+ */
1086
+ mapFormulaRefsToIndices(formula) {
1087
+ invariant2(formula.charAt(0) === "=", void 0, {
1088
+ F: __dxlog_file2,
1089
+ L: 372,
1090
+ S: this,
1091
+ A: [
1092
+ "formula.charAt(0) === '='",
1093
+ ""
1094
+ ]
1095
+ });
1096
+ return formula.replace(/([a-zA-Z]+)([0-9]+)/g, (match) => {
1097
+ return addressToIndex(this._sheet, addressFromA1Notation(match));
1098
+ });
1099
+ }
1100
+ /**
1101
+ * Map from indices to A1 notation.
1102
+ */
1103
+ mapFormulaIndicesToRefs(formula) {
1104
+ invariant2(formula.charAt(0) === "=", void 0, {
1105
+ F: __dxlog_file2,
1106
+ L: 382,
1107
+ S: this,
1108
+ A: [
1109
+ "formula.charAt(0) === '='",
1110
+ ""
1111
+ ]
1112
+ });
1113
+ return formula.replace(/([a-zA-Z0-9]+)@([a-zA-Z0-9]+)/g, (idx) => {
1114
+ return addressToA1Notation(addressFromIndex(this._sheet, idx));
1115
+ });
1116
+ }
1117
+ //
1118
+ // Values
1119
+ //
1120
+ /**
1121
+ * https://hyperformula.handsontable.com/guide/date-and-time-handling.html#example
1122
+ * https://hyperformula.handsontable.com/api/interfaces/configparams.html#nulldate
1123
+ * NOTE: TODAY() is number of FULL days since nullDate. It will typically be -1 days from NOW().
1124
+ */
1125
+ toLocalDate(num) {
1126
+ const { year, month, day, hours, minutes, seconds } = this.toDateTime(num);
1127
+ return new Date(year, month - 1, day, hours, minutes, seconds);
1128
+ }
1129
+ toDateTime(num) {
1130
+ invariant2(this._node, void 0, {
1131
+ F: __dxlog_file2,
1132
+ L: 403,
1133
+ S: this,
1134
+ A: [
1135
+ "this._node",
1136
+ ""
1137
+ ]
1138
+ });
1139
+ return this._node.graph.hf.numberToDateTime(num);
1140
+ }
1141
+ toDate(num) {
1142
+ invariant2(this._node, void 0, {
1143
+ F: __dxlog_file2,
1144
+ L: 408,
1145
+ S: this,
1146
+ A: [
1147
+ "this._node",
1148
+ ""
1149
+ ]
1150
+ });
1151
+ return this._node.graph.hf.numberToDate(num);
1152
+ }
1153
+ toTime(num) {
1154
+ invariant2(this._node, void 0, {
1155
+ F: __dxlog_file2,
1156
+ L: 413,
1157
+ S: this,
1158
+ A: [
1159
+ "this._node",
1160
+ ""
1161
+ ]
1162
+ });
1163
+ return this._node.graph.hf.numberToTime(num);
1164
+ }
1165
+ };
1166
+
1167
+ // packages/plugins/plugin-sheet/src/hooks/useFormattingModel.ts
1168
+ var useFormattingModel = (model) => {
1169
+ return useMemo(() => model && new FormattingModel(model), [
1170
+ model
1171
+ ]);
1172
+ };
1173
+
1174
+ // packages/plugins/plugin-sheet/src/hooks/useSheetModel.ts
1175
+ import { useEffect as useEffect2, useState as useState3 } from "react";
1176
+ var useSheetModel = (graph, sheet, { readonly } = {}) => {
1177
+ const [model, setModel] = useState3();
1178
+ useEffect2(() => {
1179
+ if (!graph || !sheet) {
1180
+ return;
1181
+ }
1182
+ let model2;
1183
+ const t = setTimeout(async () => {
1184
+ model2 = new SheetModel(graph, sheet, {
1185
+ readonly
1186
+ });
1187
+ await model2.open();
1188
+ setModel(model2);
1189
+ });
1190
+ return () => {
1191
+ clearTimeout(t);
1192
+ void model2?.close();
1193
+ };
1194
+ }, [
1195
+ graph,
1196
+ sheet,
1197
+ readonly
1198
+ ]);
1199
+ return model;
1200
+ };
1201
+
1202
+ // packages/plugins/plugin-sheet/src/components/Sheet/sheet-context.tsx
1203
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/plugins/plugin-sheet/src/components/Sheet/sheet-context.tsx";
1204
+ var SheetContext = /* @__PURE__ */ createContext2(null);
1205
+ var useSheetContext = () => {
1206
+ const context = useContext2(SheetContext);
1207
+ invariant3(context, void 0, {
1208
+ F: __dxlog_file3,
1209
+ L: 43,
1210
+ S: void 0,
1211
+ A: [
1212
+ "context",
1213
+ ""
1214
+ ]
1215
+ });
1216
+ return context;
1217
+ };
1218
+ var SheetContextProvider = ({ children, graph, sheet, readonly, onInfo }) => {
1219
+ const model = useSheetModel(graph, sheet, {
1220
+ readonly
1221
+ });
1222
+ const formatting = useFormattingModel(model);
1223
+ const [cursor, setCursor] = useState4();
1224
+ const [range, setRange] = useState4();
1225
+ const [editing, setEditing] = useState4(false);
1226
+ const decorations = useMemo2(() => createDecorations(), []);
1227
+ return !model || !formatting ? null : /* @__PURE__ */ React3.createElement(SheetContext.Provider, {
1228
+ value: {
1229
+ model,
1230
+ formatting,
1231
+ cursor,
1232
+ setCursor,
1233
+ range,
1234
+ setRange,
1235
+ editing,
1236
+ setEditing,
1237
+ // TODO(burdon): Change to event.
1238
+ onInfo,
1239
+ decorations
1240
+ }
1241
+ }, children);
1242
+ };
1243
+
1244
+ // packages/plugins/plugin-sheet/src/components/Sheet/threads.tsx
1245
+ import { effect } from "@preact/signals-core";
1246
+ import React4, { useCallback, useEffect as useEffect3, useMemo as useMemo3 } from "react";
1247
+ import { LayoutAction, useIntentDispatcher, useIntentResolver } from "@dxos/app-framework";
1248
+ import { debounce } from "@dxos/async";
1249
+ import { fullyQualifiedId } from "@dxos/react-client/echo";
1250
+ import { Icon, useTranslation } from "@dxos/react-ui";
1251
+ var CommentIndicator = () => {
1252
+ return /* @__PURE__ */ React4.createElement("div", {
1253
+ role: "none",
1254
+ className: "absolute top-0 right-0 w-0 h-0 border-t-8 border-l-8 border-t-cmCommentSurface border-l-transparent"
1255
+ });
1256
+ };
1257
+ var ThreadedCellWrapper = ({ children }) => {
1258
+ const dispatch = useIntentDispatcher();
1259
+ const [isHovered, setIsHovered] = React4.useState(false);
1260
+ const { t } = useTranslation(SHEET_PLUGIN);
1261
+ const handleClick = React4.useCallback((_event) => {
1262
+ void dispatch({
1263
+ action: LayoutAction.SET_LAYOUT,
1264
+ data: {
1265
+ element: "complementary",
1266
+ state: true
1267
+ }
1268
+ });
1269
+ }, [
1270
+ dispatch
1271
+ ]);
1272
+ return /* @__PURE__ */ React4.createElement("div", {
1273
+ role: "none",
1274
+ className: "relative h-full is-full",
1275
+ onMouseEnter: () => {
1276
+ setIsHovered(true);
1277
+ },
1278
+ onMouseLeave: () => {
1279
+ setIsHovered(false);
1280
+ }
1281
+ }, /* @__PURE__ */ React4.createElement(CommentIndicator, null), isHovered && /* @__PURE__ */ React4.createElement("div", {
1282
+ className: "absolute inset-0 flex items-center justify-end pr-1"
1283
+ }, /* @__PURE__ */ React4.createElement("button", {
1284
+ className: "ch-button text-xs min-bs-0 p-1",
1285
+ onClick: handleClick,
1286
+ "aria-label": t("open comment for sheet cell")
1287
+ }, /* @__PURE__ */ React4.createElement(Icon, {
1288
+ icon: "ph--chat--regular",
1289
+ "aria-hidden": true
1290
+ }))), children);
1291
+ };
1292
+ var createThreadDecoration = (cellIndex, threadId, sheetId) => {
1293
+ return {
1294
+ type: "comment",
1295
+ cellIndex,
1296
+ decorate: (props) => /* @__PURE__ */ React4.createElement(ThreadedCellWrapper, props)
1297
+ };
1298
+ };
1299
+ var useUpdateCursorOnThreadSelection = () => {
1300
+ const { setCursor, model } = useSheetContext();
1301
+ const handleScrollIntoView = useCallback(({ action, data }) => {
1302
+ switch (action) {
1303
+ case LayoutAction.SCROLL_INTO_VIEW: {
1304
+ if (!data?.id || data?.cursor === void 0 || data?.id !== fullyQualifiedId(model.sheet)) {
1305
+ return;
1306
+ }
1307
+ const cellAddress = addressFromIndex(model.sheet, data.cursor);
1308
+ setCursor(cellAddress);
1309
+ }
1310
+ }
1311
+ }, [
1312
+ model.sheet,
1313
+ setCursor
1314
+ ]);
1315
+ useIntentResolver(SHEET_PLUGIN, handleScrollIntoView);
1316
+ };
1317
+ var useSelectThreadOnCursorChange = () => {
1318
+ const { cursor, model } = useSheetContext();
1319
+ const dispatch = useIntentDispatcher();
1320
+ const activeThreads = useMemo3(() => model.sheet.threads?.filter((thread) => !!thread && thread.status === "active") ?? [], [
1321
+ JSON.stringify(model.sheet.threads)
1322
+ ]);
1323
+ const activeThreadAddresses = useMemo3(() => activeThreads.map((thread) => thread.anchor).filter((anchor) => anchor !== void 0).map((anchor) => addressFromIndex(model.sheet, anchor)), [
1324
+ activeThreads,
1325
+ model.sheet
1326
+ ]);
1327
+ const selectClosestThread = useCallback((cellAddress) => {
1328
+ if (!cellAddress || !activeThreads) {
1329
+ return;
1330
+ }
1331
+ const closestThreadAnchor = closest(cellAddress, activeThreadAddresses);
1332
+ if (closestThreadAnchor) {
1333
+ const closestThread = activeThreads.find((thread) => thread && thread.anchor === addressToIndex(model.sheet, closestThreadAnchor));
1334
+ if (closestThread) {
1335
+ void dispatch([
1336
+ {
1337
+ action: "dxos.org/plugin/thread/action/select",
1338
+ data: {
1339
+ current: fullyQualifiedId(closestThread)
1340
+ }
1341
+ }
1342
+ ]);
1343
+ }
1344
+ }
1345
+ }, [
1346
+ dispatch,
1347
+ activeThreads,
1348
+ activeThreadAddresses,
1349
+ model.sheet
1350
+ ]);
1351
+ const debounced = useMemo3(() => {
1352
+ return debounce((cursor2) => requestAnimationFrame(() => selectClosestThread(cursor2)), 50);
1353
+ }, [
1354
+ selectClosestThread
1355
+ ]);
1356
+ useEffect3(() => {
1357
+ if (!cursor) {
1358
+ return;
1359
+ }
1360
+ debounced(cursor);
1361
+ }, [
1362
+ cursor,
1363
+ selectClosestThread
1364
+ ]);
1365
+ };
1366
+ var useThreadDecorations = () => {
1367
+ const { decorations, model } = useSheetContext();
1368
+ const sheet = useMemo3(() => model.sheet, [
1369
+ model.sheet
1370
+ ]);
1371
+ const sheetId = useMemo3(() => fullyQualifiedId(sheet), [
1372
+ sheet
1373
+ ]);
1374
+ useEffect3(() => {
1375
+ const unsubscribe = effect(() => {
1376
+ const activeThreadAnchors = /* @__PURE__ */ new Set();
1377
+ if (!sheet.threads) {
1378
+ return;
1379
+ }
1380
+ for (const thread of sheet.threads) {
1381
+ if (!thread || thread.anchor === void 0 || thread.status === "resolved") {
1382
+ continue;
1383
+ }
1384
+ activeThreadAnchors.add(thread.anchor);
1385
+ const index = thread.anchor;
1386
+ const existingDecorations = decorations.getDecorationsForCell(index);
1387
+ if (!existingDecorations || !existingDecorations.some((d) => d.type === "comment")) {
1388
+ decorations.addDecoration(index, createThreadDecoration(index, thread.id, sheetId));
1389
+ }
1390
+ }
1391
+ for (const decoration of decorations.getAllDecorations()) {
1392
+ if (decoration.type !== "comment") {
1393
+ continue;
1394
+ }
1395
+ if (!activeThreadAnchors.has(decoration.cellIndex)) {
1396
+ decorations.removeDecoration(decoration.cellIndex, "comment");
1397
+ }
1398
+ }
1399
+ });
1400
+ return () => unsubscribe();
1401
+ });
1402
+ };
1403
+ var useThreads = () => {
1404
+ useUpdateCursorOnThreadSelection();
1405
+ useSelectThreadOnCursorChange();
1406
+ useThreadDecorations();
1407
+ };
1408
+
1409
+ // packages/plugins/plugin-sheet/src/components/Sheet/util.ts
1410
+ var getRelativeClientRect = (root, element) => {
1411
+ const rootRect = root.getBoundingClientRect();
1412
+ const elementRect = element.getBoundingClientRect();
1413
+ return new DOMRect(elementRect.left - rootRect.left + root.scrollLeft, elementRect.top - rootRect.top + root.scrollTop, elementRect.width, elementRect.height);
1414
+ };
1415
+ var getRectUnion = (b1, b2) => {
1416
+ return {
1417
+ left: Math.min(b1.left, b2.left),
1418
+ top: Math.min(b1.top, b2.top),
1419
+ width: Math.abs(b1.left - b2.left) + (b1.left > b2.left ? b1.width : b2.width),
1420
+ height: Math.abs(b1.top - b2.top) + (b1.height > b2.height ? b1.height : b2.height)
1421
+ };
1422
+ };
1423
+ var scrollIntoView = (scrollContainer, el) => {
1424
+ el.scrollIntoView({
1425
+ block: "nearest",
1426
+ inline: "nearest"
1427
+ });
1428
+ const cellBounds = el.getBoundingClientRect();
1429
+ const scrollerBounds = scrollContainer.getBoundingClientRect();
1430
+ if (cellBounds.top < scrollerBounds.top) {
1431
+ scrollContainer.scrollTop -= scrollerBounds.top - cellBounds.top;
1432
+ } else if (cellBounds.bottom >= scrollerBounds.bottom - 1) {
1433
+ scrollContainer.scrollTop += 2 + scrollerBounds.bottom - cellBounds.bottom;
1434
+ }
1435
+ if (cellBounds.left < scrollerBounds.left) {
1436
+ scrollContainer.scrollLeft -= scrollerBounds.left - cellBounds.left;
1437
+ } else if (cellBounds.right >= scrollerBounds.right) {
1438
+ scrollContainer.scrollLeft += 2 + scrollerBounds.right - cellBounds.right;
1439
+ }
1440
+ };
1441
+
1442
+ // packages/plugins/plugin-sheet/src/components/CellEditor/CellEditor.tsx
1443
+ import { EditorView, keymap } from "@codemirror/view";
1444
+ import React5 from "react";
1445
+ import { useThemeContext } from "@dxos/react-ui";
1446
+ import { createBasicExtensions, createThemeExtensions, preventNewline, useTextEditor } from "@dxos/react-ui-editor";
1447
+ var editorKeys = ({ onNav, onClose }) => {
1448
+ return keymap.of([
1449
+ {
1450
+ key: "ArrowUp",
1451
+ run: (editor) => {
1452
+ const value = editor.state.doc.toString();
1453
+ onNav?.(value, {
1454
+ key: "ArrowUp"
1455
+ });
1456
+ return !!onNav;
1457
+ }
1458
+ },
1459
+ {
1460
+ key: "ArrowDown",
1461
+ run: (editor) => {
1462
+ const value = editor.state.doc.toString();
1463
+ onNav?.(value, {
1464
+ key: "ArrowDown"
1465
+ });
1466
+ return !!onNav;
1467
+ }
1468
+ },
1469
+ {
1470
+ key: "ArrowLeft",
1471
+ run: (editor) => {
1472
+ const value = editor.state.doc.toString();
1473
+ onNav?.(value, {
1474
+ key: "ArrowLeft"
1475
+ });
1476
+ return !!onNav;
1477
+ }
1478
+ },
1479
+ {
1480
+ key: "ArrowRight",
1481
+ run: (editor) => {
1482
+ const value = editor.state.doc.toString();
1483
+ onNav?.(value, {
1484
+ key: "ArrowRight"
1485
+ });
1486
+ return !!onNav;
1487
+ }
1488
+ },
1489
+ {
1490
+ key: "Enter",
1491
+ run: (editor) => {
1492
+ onClose(editor.state.doc.toString(), {
1493
+ key: "Enter"
1494
+ });
1495
+ return true;
1496
+ },
1497
+ shift: (editor) => {
1498
+ onClose(editor.state.doc.toString(), {
1499
+ key: "Enter",
1500
+ shift: true
1501
+ });
1502
+ return true;
1503
+ }
1504
+ },
1505
+ {
1506
+ key: "Tab",
1507
+ run: (editor) => {
1508
+ onClose(editor.state.doc.toString(), {
1509
+ key: "Tab"
1510
+ });
1511
+ return true;
1512
+ },
1513
+ shift: (editor) => {
1514
+ onClose(editor.state.doc.toString(), {
1515
+ key: "Tab",
1516
+ shift: true
1517
+ });
1518
+ return true;
1519
+ }
1520
+ },
1521
+ {
1522
+ key: "Escape",
1523
+ run: () => {
1524
+ onClose(void 0, {
1525
+ key: "Escape"
1526
+ });
1527
+ return true;
1528
+ }
1529
+ }
1530
+ ]);
1531
+ };
1532
+ var editorVariants = {
1533
+ // TODO(thure): remove when legacy is no longer used.
1534
+ legacy: {
1535
+ root: "flex w-full",
1536
+ editor: "flex w-full [&>.cm-scroller]:scrollbar-none",
1537
+ content: "!px-2 !py-1"
1538
+ },
1539
+ grid: {
1540
+ root: "absolute z-[1]",
1541
+ editor: "[&>.cm-scroller]:scrollbar-none tabular-nums",
1542
+ content: "!border !border-transparent !p-0.5"
1543
+ }
1544
+ };
1545
+ var CellEditor = ({ value, extension, autoFocus, onBlur, variant = "legacy", box, gridId }) => {
1546
+ const { themeMode } = useThemeContext();
1547
+ const { parentRef } = useTextEditor(() => {
1548
+ return {
1549
+ autoFocus,
1550
+ initialValue: value,
1551
+ selection: {
1552
+ anchor: value?.length ?? 0
1553
+ },
1554
+ extensions: [
1555
+ extension ?? [],
1556
+ preventNewline,
1557
+ EditorView.focusChangeEffect.of((_, focusing) => {
1558
+ if (!focusing) {
1559
+ onBlur?.({
1560
+ type: "blur"
1561
+ });
1562
+ }
1563
+ return null;
1564
+ }),
1565
+ createBasicExtensions({
1566
+ lineWrapping: false
1567
+ }),
1568
+ createThemeExtensions({
1569
+ themeMode,
1570
+ slots: {
1571
+ editor: {
1572
+ className: editorVariants[variant].editor
1573
+ },
1574
+ content: {
1575
+ className: editorVariants[variant].content
1576
+ }
1577
+ }
1578
+ })
1579
+ ]
1580
+ };
1581
+ }, [
1582
+ extension,
1583
+ autoFocus,
1584
+ value,
1585
+ variant,
1586
+ onBlur
1587
+ ]);
1588
+ return /* @__PURE__ */ React5.createElement("div", {
1589
+ ref: parentRef,
1590
+ className: editorVariants[variant].root,
1591
+ style: box,
1592
+ ...gridId && {
1593
+ "data-grid": gridId
1594
+ }
1595
+ });
1596
+ };
1597
+
1598
+ // packages/plugins/plugin-sheet/src/components/CellEditor/extension.ts
1599
+ import { acceptCompletion, autocompletion, completionStatus, startCompletion } from "@codemirror/autocomplete";
1600
+ import { HighlightStyle, syntaxHighlighting } from "@codemirror/language";
1601
+ import { ViewPlugin, keymap as keymap2 } from "@codemirror/view";
1602
+ import { tags } from "@lezer/highlight";
1603
+ import { spreadsheet } from "codemirror-lang-spreadsheet";
1604
+ import { singleValueFacet } from "@dxos/react-ui-editor/state";
1605
+ import { mx } from "@dxos/react-ui-theme";
1606
+ var highlightStyles = HighlightStyle.define([
1607
+ // Function.
1608
+ {
1609
+ tag: tags.name,
1610
+ class: "text-accentText"
1611
+ },
1612
+ // Range.
1613
+ {
1614
+ tag: tags.tagName,
1615
+ class: "text-pinkText"
1616
+ },
1617
+ // Values.
1618
+ {
1619
+ tag: tags.number,
1620
+ class: "text-tealText"
1621
+ },
1622
+ {
1623
+ tag: tags.bool,
1624
+ class: "text-tealText"
1625
+ },
1626
+ {
1627
+ tag: tags.string,
1628
+ class: "text-tealText"
1629
+ },
1630
+ // Error.
1631
+ {
1632
+ tag: tags.invalid,
1633
+ class: "text-unAccent"
1634
+ }
1635
+ ]);
1636
+ var languageFacet = singleValueFacet();
1637
+ var sheetExtension = ({ functions = [] }) => {
1638
+ const { extension, language } = spreadsheet({
1639
+ idiom: "en-US",
1640
+ decimalSeparator: "."
1641
+ });
1642
+ const createCompletion = (name) => {
1643
+ const { section = "Custom", description, syntax } = functions.find((value) => value.name === name) ?? {};
1644
+ return {
1645
+ section,
1646
+ label: name,
1647
+ info: () => {
1648
+ if (!description && !syntax) {
1649
+ return null;
1650
+ }
1651
+ const root = document.createElement("div");
1652
+ root.className = "flex flex-col gap-2 text-sm";
1653
+ const title = document.createElement("h2");
1654
+ title.innerText = name;
1655
+ title.className = "text-lg font-mono text-accentText";
1656
+ root.appendChild(title);
1657
+ if (description) {
1658
+ const info = document.createElement("p");
1659
+ info.innerText = description;
1660
+ info.className = "text-subdued";
1661
+ root.appendChild(info);
1662
+ }
1663
+ if (syntax) {
1664
+ const detail = document.createElement("pre");
1665
+ detail.innerText = syntax;
1666
+ detail.className = "whitespace-pre-wrap text-greenText";
1667
+ root.appendChild(detail);
1668
+ }
1669
+ return root;
1670
+ },
1671
+ apply: (view, completion, from, to) => {
1672
+ const insertParens = to === view.state.doc.toString().length;
1673
+ view.dispatch(view.state.update({
1674
+ changes: {
1675
+ from,
1676
+ to,
1677
+ insert: completion.label + (insertParens ? "()" : "")
1678
+ },
1679
+ selection: {
1680
+ anchor: from + completion.label.length + 1
1681
+ }
1682
+ }));
1683
+ }
1684
+ };
1685
+ };
1686
+ return [
1687
+ extension,
1688
+ languageFacet.of(language),
1689
+ language.data.of({
1690
+ autocomplete: (context) => {
1691
+ if (context.state.doc.toString()[0] !== "=") {
1692
+ return null;
1693
+ }
1694
+ const match = context.matchBefore(/\w*/);
1695
+ if (!match || match.from === match.to) {
1696
+ return null;
1697
+ }
1698
+ const text = match.text.toUpperCase();
1699
+ if (!context.explicit && match.text.length < 2) {
1700
+ return null;
1701
+ }
1702
+ return {
1703
+ from: match.from,
1704
+ options: functions?.filter(({ name }) => name.startsWith(text)).map(({ name }) => createCompletion(name)) ?? []
1705
+ };
1706
+ }
1707
+ }),
1708
+ syntaxHighlighting(highlightStyles),
1709
+ autocompletion({
1710
+ aboveCursor: false,
1711
+ defaultKeymap: true,
1712
+ activateOnTyping: true,
1713
+ // NOTE: Useful for debugging.
1714
+ closeOnBlur: false,
1715
+ icons: false,
1716
+ tooltipClass: () => mx("!-left-[1px] !top-[33px] !-m-0 border !border-t-0 [&>ul]:!min-w-[198px]", "[&>ul>li[aria-selected]]:!bg-accentSurface", "border-separator")
1717
+ }),
1718
+ keymap2.of([
1719
+ {
1720
+ key: "Tab",
1721
+ run: (view) => {
1722
+ return completionStatus(view.state) === "active" ? acceptCompletion(view) : startCompletion(view);
1723
+ }
1724
+ }
1725
+ ])
1726
+ ];
1727
+ };
1728
+ var rangeExtension = (onInit) => {
1729
+ let view;
1730
+ let activeRange;
1731
+ const provider = (range) => {
1732
+ if (activeRange) {
1733
+ view.dispatch(view.state.update({
1734
+ changes: {
1735
+ ...activeRange,
1736
+ insert: range.toString()
1737
+ },
1738
+ selection: {
1739
+ anchor: activeRange.from + range.length
1740
+ }
1741
+ }));
1742
+ }
1743
+ view.focus();
1744
+ };
1745
+ return ViewPlugin.fromClass(class {
1746
+ constructor(_view) {
1747
+ view = _view;
1748
+ onInit(provider);
1749
+ }
1750
+ update(view2) {
1751
+ const { anchor } = view2.state.selection.ranges[0];
1752
+ activeRange = void 0;
1753
+ const language = view2.state.facet(languageFacet);
1754
+ const { topNode } = language.parser.parse(view2.state.doc.toString());
1755
+ visitTree(topNode, ({ type, from, to }) => {
1756
+ if (from <= anchor && to >= anchor) {
1757
+ switch (type.name) {
1758
+ case "Function": {
1759
+ activeRange = {
1760
+ from: to,
1761
+ to
1762
+ };
1763
+ break;
1764
+ }
1765
+ case "CloseParen": {
1766
+ activeRange = {
1767
+ from,
1768
+ to: from
1769
+ };
1770
+ break;
1771
+ }
1772
+ case "RangeToken":
1773
+ case "CellToken":
1774
+ activeRange = {
1775
+ from,
1776
+ to
1777
+ };
1778
+ return true;
1779
+ }
1780
+ }
1781
+ return false;
1782
+ });
1783
+ if (!activeRange && view2.state.doc.toString()[0] === "=") {
1784
+ activeRange = {
1785
+ from: 1,
1786
+ to: view2.state.doc.toString().length
1787
+ };
1788
+ }
1789
+ }
1790
+ });
1791
+ };
1792
+ var visitTree = (node, callback) => {
1793
+ if (callback(node)) {
1794
+ return true;
1795
+ }
1796
+ for (let child = node.firstChild; child !== null; child = child.nextSibling) {
1797
+ if (visitTree(child, callback)) {
1798
+ return true;
1799
+ }
1800
+ }
1801
+ return false;
1802
+ };
1803
+
1804
+ // packages/plugins/plugin-sheet/src/components/Sheet/Sheet.tsx
1805
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/plugins/plugin-sheet/src/components/Sheet/Sheet.tsx";
1806
+ var fragments = {
1807
+ axis: "bg-axisSurface text-axisText text-xs select-none",
1808
+ axisSelected: "bg-attention text-baseText",
1809
+ cell: "bg-gridCell",
1810
+ cellSelected: "bg-gridCellSelected text-baseText border !border-accentSurface"
1811
+ };
1812
+ var SheetRoot = ({ children, ...props }) => {
1813
+ return /* @__PURE__ */ React6.createElement(SheetContextProvider, props, children);
1814
+ };
1815
+ var SheetMain = /* @__PURE__ */ forwardRef(({ classNames, numRows, numCols }, forwardRef2) => {
1816
+ const { model, cursor, setCursor, setRange, setEditing } = useSheetContext();
1817
+ const { rowsRef, columnsRef, contentRef } = useScrollHandlers();
1818
+ useThreads();
1819
+ const [rows, setRows] = useState5([
1820
+ ...model.sheet.rows
1821
+ ]);
1822
+ const [columns, setColumns] = useState5([
1823
+ ...model.sheet.columns
1824
+ ]);
1825
+ useEffect4(() => {
1826
+ const rowsAccessor = createDocAccessor(model.sheet, [
1827
+ "rows"
1828
+ ]);
1829
+ const columnsAccessor = createDocAccessor(model.sheet, [
1830
+ "columns"
1831
+ ]);
1832
+ const handleUpdate = debounce2(() => {
1833
+ setRows([
1834
+ ...model.sheet.rows
1835
+ ]);
1836
+ setColumns([
1837
+ ...model.sheet.columns
1838
+ ]);
1839
+ }, 100);
1840
+ rowsAccessor.handle.addListener("change", handleUpdate);
1841
+ columnsAccessor.handle.addListener("change", handleUpdate);
1842
+ handleUpdate();
1843
+ return () => {
1844
+ rowsAccessor.handle.removeListener("change", handleUpdate);
1845
+ columnsAccessor.handle.removeListener("change", handleUpdate);
1846
+ };
1847
+ }, [
1848
+ model
1849
+ ]);
1850
+ useEffect4(() => {
1851
+ model.reset();
1852
+ }, [
1853
+ rows,
1854
+ columns
1855
+ ]);
1856
+ const handleMoveRows = (from, to, num = 1) => {
1857
+ const cursorIdx = cursor ? addressToIndex(model.sheet, cursor) : void 0;
1858
+ const [rows2] = model.sheet.rows.splice(from, num);
1859
+ model.sheet.rows.splice(to, 0, rows2);
1860
+ if (cursorIdx) {
1861
+ setCursor(addressFromIndex(model.sheet, cursorIdx));
1862
+ }
1863
+ setRows([
1864
+ ...model.sheet.rows
1865
+ ]);
1866
+ };
1867
+ const handleMoveColumns = (from, to, num = 1) => {
1868
+ const cursorIdx = cursor ? addressToIndex(model.sheet, cursor) : void 0;
1869
+ const columns2 = model.sheet.columns.splice(from, num);
1870
+ model.sheet.columns.splice(to, 0, ...columns2);
1871
+ if (cursorIdx) {
1872
+ setCursor(addressFromIndex(model.sheet, cursorIdx));
1873
+ }
1874
+ setColumns([
1875
+ ...model.sheet.columns
1876
+ ]);
1877
+ };
1878
+ const [rowSizes, setRowSizes] = useState5();
1879
+ const [columnSizes, setColumnSizes] = useState5();
1880
+ useEffect4(() => {
1881
+ const rowAccessor = createDocAccessor(model.sheet, [
1882
+ "rowMeta"
1883
+ ]);
1884
+ const columnAccessor = createDocAccessor(model.sheet, [
1885
+ "columnMeta"
1886
+ ]);
1887
+ const handleUpdate = debounce2(() => {
1888
+ const mapSizes = (values) => values.reduce((map, [idx, meta]) => {
1889
+ if (meta.size) {
1890
+ map[idx] = meta.size;
1891
+ }
1892
+ return map;
1893
+ }, {});
1894
+ setRowSizes(mapSizes(Object.entries(model.sheet.rowMeta)));
1895
+ setColumnSizes(mapSizes(Object.entries(model.sheet.columnMeta)));
1896
+ }, 100);
1897
+ rowAccessor.handle.addListener("change", handleUpdate);
1898
+ columnAccessor.handle.addListener("change", handleUpdate);
1899
+ handleUpdate();
1900
+ return () => {
1901
+ rowAccessor.handle.removeListener("change", handleUpdate);
1902
+ columnAccessor.handle.removeListener("change", handleUpdate);
1903
+ };
1904
+ }, [
1905
+ model
1906
+ ]);
1907
+ const handleResizeRow = (idx, size, save) => {
1908
+ if (save) {
1909
+ model.sheet.rowMeta[idx] ??= {};
1910
+ model.sheet.rowMeta[idx].size = size;
1911
+ } else {
1912
+ setRowSizes((sizes) => ({
1913
+ ...sizes,
1914
+ [idx]: size
1915
+ }));
1916
+ }
1917
+ };
1918
+ const handleResizeColumn = (idx, size, save) => {
1919
+ if (save) {
1920
+ model.sheet.columnMeta[idx] ??= {};
1921
+ model.sheet.columnMeta[idx].size = size;
1922
+ } else {
1923
+ setColumnSizes((sizes) => ({
1924
+ ...sizes,
1925
+ [idx]: size
1926
+ }));
1927
+ }
1928
+ };
1929
+ return /* @__PURE__ */ React6.createElement("div", {
1930
+ role: "none",
1931
+ className: mx2("grid grid-cols-[calc(var(--rail-size)-2px)_1fr] grid-rows-[32px_1fr_32px] bs-full is-full overflow-hidden", classNames)
1932
+ }, /* @__PURE__ */ React6.createElement(GridCorner, {
1933
+ onClick: () => {
1934
+ setCursor(void 0);
1935
+ setRange(void 0);
1936
+ setEditing(false);
1937
+ }
1938
+ }), /* @__PURE__ */ React6.createElement(SheetColumns, {
1939
+ ref: columnsRef,
1940
+ columns,
1941
+ sizes: columnSizes,
1942
+ selected: cursor?.col,
1943
+ onSelect: (col) => setCursor(cursor?.col === col ? void 0 : {
1944
+ row: -1,
1945
+ col
1946
+ }),
1947
+ onResize: handleResizeColumn,
1948
+ onMove: handleMoveColumns
1949
+ }), /* @__PURE__ */ React6.createElement(SheetRows, {
1950
+ ref: rowsRef,
1951
+ rows,
1952
+ sizes: rowSizes,
1953
+ selected: cursor?.row,
1954
+ onSelect: (row) => setCursor(cursor?.row === row ? void 0 : {
1955
+ row,
1956
+ col: -1
1957
+ }),
1958
+ onResize: handleResizeRow,
1959
+ onMove: handleMoveRows
1960
+ }), /* @__PURE__ */ React6.createElement(SheetGrid, {
1961
+ ref: contentRef,
1962
+ size: {
1963
+ numRows: numRows ?? rows.length,
1964
+ numCols: numCols ?? columns.length
1965
+ },
1966
+ rows,
1967
+ columns,
1968
+ rowSizes,
1969
+ columnSizes
1970
+ }), /* @__PURE__ */ React6.createElement(GridCorner, null), /* @__PURE__ */ React6.createElement(SheetStatusBar, null));
1971
+ });
1972
+ var useScrollHandlers = () => {
1973
+ const rowsRef = useRef(null);
1974
+ const columnsRef = useRef(null);
1975
+ const contentRef = useRef(null);
1976
+ useEffect4(() => {
1977
+ const handleRowsScroll = (ev) => {
1978
+ const { scrollTop } = ev.target;
1979
+ if (!rowsRef.current.dataset.locked) {
1980
+ contentRef.current.scrollTop = scrollTop;
1981
+ }
1982
+ };
1983
+ const handleColumnsScroll = (ev) => {
1984
+ const { scrollLeft } = ev.target;
1985
+ if (!columnsRef.current.dataset.locked) {
1986
+ contentRef.current.scrollLeft = scrollLeft;
1987
+ }
1988
+ };
1989
+ const handleContentScroll = (ev) => {
1990
+ const { scrollTop, scrollLeft } = ev.target;
1991
+ rowsRef.current.scrollTop = scrollTop;
1992
+ columnsRef.current.scrollLeft = scrollLeft;
1993
+ };
1994
+ const rows = rowsRef.current;
1995
+ const columns = columnsRef.current;
1996
+ const content = contentRef.current;
1997
+ rows.addEventListener("scroll", handleRowsScroll);
1998
+ columns.addEventListener("scroll", handleColumnsScroll);
1999
+ content.addEventListener("scroll", handleContentScroll);
2000
+ return () => {
2001
+ rows.removeEventListener("scroll", handleRowsScroll);
2002
+ columns.removeEventListener("scroll", handleColumnsScroll);
2003
+ content.removeEventListener("scroll", handleContentScroll);
2004
+ };
2005
+ }, []);
2006
+ return {
2007
+ rowsRef,
2008
+ columnsRef,
2009
+ contentRef
2010
+ };
2011
+ };
2012
+ var GridCorner = (props) => {
2013
+ return /* @__PURE__ */ React6.createElement("div", {
2014
+ className: fragments.axis,
2015
+ ...props
2016
+ });
2017
+ };
2018
+ var MovingOverlay = ({ label }) => {
2019
+ return /* @__PURE__ */ React6.createElement("div", {
2020
+ className: "flex w-full h-full justify-center items-center text-sm p-1 bg-gridOverlay cursor-pointer"
2021
+ }, label);
2022
+ };
2023
+ var mouseConstraints = {
2024
+ distance: 10
2025
+ };
2026
+ var touchConstraints = {
2027
+ delay: 250,
2028
+ tolerance: 5
2029
+ };
2030
+ var SheetRows = /* @__PURE__ */ forwardRef(({ rows, sizes, selected, onSelect, onResize, onMove }, forwardRef2) => {
2031
+ const mouseSensor = useSensor(MouseSensor, {
2032
+ activationConstraint: mouseConstraints
2033
+ });
2034
+ const touchSensor = useSensor(TouchSensor, {
2035
+ activationConstraint: touchConstraints
2036
+ });
2037
+ const keyboardSensor = useSensor(KeyboardSensor, {});
2038
+ const sensors = useSensors(mouseSensor, touchSensor, keyboardSensor);
2039
+ const [active, setActive] = useState5(null);
2040
+ const handleDragStart = ({ active: active2 }) => {
2041
+ setActive(active2);
2042
+ };
2043
+ const handleDragEnd = ({ over, active: active2 }) => {
2044
+ if (over && over.id !== active2.id) {
2045
+ setActive(null);
2046
+ onMove?.(active2.data.current.index, over.data.current.index);
2047
+ }
2048
+ };
2049
+ const snapToCenter = ({ activatorEvent, draggingNodeRect, transform }) => {
2050
+ if (draggingNodeRect && activatorEvent) {
2051
+ const activatorCoordinates = getEventCoordinates(activatorEvent);
2052
+ if (!activatorCoordinates) {
2053
+ return transform;
2054
+ }
2055
+ const offset = activatorCoordinates.y - draggingNodeRect.top;
2056
+ return {
2057
+ ...transform,
2058
+ y: transform.y + offset - draggingNodeRect.height / 2
2059
+ };
2060
+ }
2061
+ return transform;
2062
+ };
2063
+ return /* @__PURE__ */ React6.createElement("div", {
2064
+ className: "relative flex grow overflow-hidden"
2065
+ }, /* @__PURE__ */ React6.createElement("div", {
2066
+ className: mx2("z-20 absolute inset-0 border-y border-gridLine pointer-events-none"),
2067
+ style: {
2068
+ width: axisWidth
2069
+ }
2070
+ }), /* @__PURE__ */ React6.createElement("div", {
2071
+ ref: forwardRef2,
2072
+ role: "rowheader",
2073
+ className: "grow overflow-y-auto scrollbar-none"
2074
+ }, /* @__PURE__ */ React6.createElement(DndContext, {
2075
+ sensors,
2076
+ modifiers: [
2077
+ restrictToVerticalAxis,
2078
+ snapToCenter
2079
+ ],
2080
+ onDragStart: handleDragStart,
2081
+ onDragEnd: handleDragEnd
2082
+ }, /* @__PURE__ */ React6.createElement("div", {
2083
+ className: "flex flex-col",
2084
+ style: {
2085
+ width: axisWidth
2086
+ }
2087
+ }, rows.map((idx, index) => /* @__PURE__ */ React6.createElement(GridRowCell, {
2088
+ key: idx,
2089
+ idx,
2090
+ index,
2091
+ label: String(index + 1),
2092
+ size: sizes?.[idx] ?? defaultHeight,
2093
+ resize: index < rows.length - 1,
2094
+ selected: selected === index,
2095
+ onResize,
2096
+ onSelect
2097
+ }))), /* @__PURE__ */ createPortal(/* @__PURE__ */ React6.createElement(DragOverlay, null, active && /* @__PURE__ */ React6.createElement(MovingOverlay, {
2098
+ label: String(active.data.current.index + 1)
2099
+ })), document.body))));
2100
+ });
2101
+ var GridRowCell = ({ idx, index, label, size, resize, selected, onSelect, onResize }) => {
2102
+ const { setNodeRef: setDroppableNodeRef } = useDroppable({
2103
+ id: idx,
2104
+ data: {
2105
+ index
2106
+ }
2107
+ });
2108
+ const { setNodeRef: setDraggableNodeRef, attributes, listeners, isDragging, over } = useDraggable({
2109
+ id: idx,
2110
+ data: {
2111
+ index
2112
+ }
2113
+ });
2114
+ const setNodeRef = useCombinedRefs(setDroppableNodeRef, setDraggableNodeRef);
2115
+ const [initialSize, setInitialSize] = useState5(size);
2116
+ const [resizing, setResizing] = useState5(false);
2117
+ const scrollHandler = useRef();
2118
+ const handleResizeStart = (_ev, _dir, elementRef) => {
2119
+ const scrollContainer = elementRef.closest('[role="rowheader"]');
2120
+ const scrollTop = scrollContainer.scrollTop;
2121
+ scrollHandler.current = (ev) => ev.target.scrollTop = scrollTop;
2122
+ scrollContainer.addEventListener("scroll", scrollHandler.current);
2123
+ scrollContainer.dataset.locked = "true";
2124
+ setResizing(true);
2125
+ };
2126
+ const handleResize = (_ev, _dir, _elementRef, { height }) => {
2127
+ onResize?.(idx, initialSize + height);
2128
+ };
2129
+ const handleResizeStop = (_ev, _dir, elementRef, { height }) => {
2130
+ const scrollContainer = elementRef.closest('[role="rowheader"]');
2131
+ scrollContainer.removeEventListener("scroll", scrollHandler.current);
2132
+ delete scrollContainer.dataset.locked;
2133
+ scrollHandler.current = void 0;
2134
+ setInitialSize(initialSize + height);
2135
+ onResize?.(idx, initialSize + height, true);
2136
+ setResizing(false);
2137
+ };
2138
+ return /* @__PURE__ */ React6.createElement(Resizable, {
2139
+ enable: {
2140
+ bottom: resize
2141
+ },
2142
+ size: {
2143
+ height: size - 1
2144
+ },
2145
+ minHeight: minHeight - 1,
2146
+ maxHeight,
2147
+ onResizeStart: handleResizeStart,
2148
+ onResize: handleResize,
2149
+ onResizeStop: handleResizeStop
2150
+ }, /* @__PURE__ */ React6.createElement("div", {
2151
+ ref: setNodeRef,
2152
+ ...attributes,
2153
+ ...listeners,
2154
+ className: mx2("flex h-full items-center justify-center cursor-pointer", "border-t border-gridLine focus-visible:outline-none", fragments.axis, selected && fragments.axisSelected, isDragging && fragments.axisSelected),
2155
+ onClick: () => onSelect?.(index)
2156
+ }, /* @__PURE__ */ React6.createElement("span", {
2157
+ className: "flex w-full justify-center"
2158
+ }, label), over?.id === idx && !isDragging && /* @__PURE__ */ React6.createElement("div", {
2159
+ className: "z-20 absolute top-0 w-full min-h-[4px] border-b-4 border-accentSurface"
2160
+ }), resizing && /* @__PURE__ */ React6.createElement("div", {
2161
+ className: "z-20 absolute bottom-0 w-full min-h-[4px] border-b-4 border-accentSurface"
2162
+ })));
2163
+ };
2164
+ var SheetColumns = /* @__PURE__ */ forwardRef(({ columns, sizes, selected, onSelect, onResize, onMove }, forwardRef2) => {
2165
+ const mouseSensor = useSensor(MouseSensor, {
2166
+ activationConstraint: mouseConstraints
2167
+ });
2168
+ const touchSensor = useSensor(TouchSensor, {
2169
+ activationConstraint: touchConstraints
2170
+ });
2171
+ const keyboardSensor = useSensor(KeyboardSensor, {});
2172
+ const sensors = useSensors(mouseSensor, touchSensor, keyboardSensor);
2173
+ const [active, setActive] = useState5(null);
2174
+ const handleDragStart = ({ active: active2 }) => {
2175
+ setActive(active2);
2176
+ };
2177
+ const handleDragEnd = ({ active: active2, over }) => {
2178
+ if (over && over.id !== active2.id) {
2179
+ setActive(null);
2180
+ onMove?.(active2.data.current.index, over.data.current.index);
2181
+ }
2182
+ };
2183
+ const snapToCenter = ({ activatorEvent, draggingNodeRect, transform }) => {
2184
+ if (draggingNodeRect && activatorEvent) {
2185
+ const activatorCoordinates = getEventCoordinates(activatorEvent);
2186
+ if (!activatorCoordinates) {
2187
+ return transform;
2188
+ }
2189
+ const offset = activatorCoordinates.x - draggingNodeRect.left;
2190
+ return {
2191
+ ...transform,
2192
+ x: transform.x + offset - draggingNodeRect.width / 2
2193
+ };
2194
+ }
2195
+ return transform;
2196
+ };
2197
+ return /* @__PURE__ */ React6.createElement("div", {
2198
+ className: "relative flex grow overflow-hidden"
2199
+ }, /* @__PURE__ */ React6.createElement("div", {
2200
+ className: mx2("z-20 absolute inset-0 border-x border-gridLine pointer-events-none"),
2201
+ style: {
2202
+ height: axisHeight
2203
+ }
2204
+ }), /* @__PURE__ */ React6.createElement("div", {
2205
+ ref: forwardRef2,
2206
+ role: "columnheader",
2207
+ className: "grow overflow-x-auto scrollbar-none"
2208
+ }, /* @__PURE__ */ React6.createElement(DndContext, {
2209
+ autoScroll: {
2210
+ enabled: true
2211
+ },
2212
+ sensors,
2213
+ modifiers: [
2214
+ restrictToHorizontalAxis,
2215
+ snapToCenter
2216
+ ],
2217
+ onDragStart: handleDragStart,
2218
+ onDragEnd: handleDragEnd
2219
+ }, /* @__PURE__ */ React6.createElement("div", {
2220
+ className: "flex h-full",
2221
+ style: {
2222
+ height: axisHeight
2223
+ }
2224
+ }, columns.map((idx, index) => /* @__PURE__ */ React6.createElement(GridColumnCell, {
2225
+ key: idx,
2226
+ idx,
2227
+ index,
2228
+ label: columnLetter(index),
2229
+ size: sizes?.[idx] ?? defaultWidth,
2230
+ resize: index < columns.length - 1,
2231
+ selected: selected === index,
2232
+ onResize,
2233
+ onSelect
2234
+ }))), /* @__PURE__ */ createPortal(/* @__PURE__ */ React6.createElement(DragOverlay, null, active && /* @__PURE__ */ React6.createElement(MovingOverlay, {
2235
+ label: columnLetter(active.data.current.index)
2236
+ })), document.body))));
2237
+ });
2238
+ var GridColumnCell = ({ idx, index, label, size, resize, selected, onSelect, onResize }) => {
2239
+ const { setNodeRef: setDroppableNodeRef } = useDroppable({
2240
+ id: idx,
2241
+ data: {
2242
+ index
2243
+ }
2244
+ });
2245
+ const { setNodeRef: setDraggableNodeRef, attributes, listeners, over, isDragging } = useDraggable({
2246
+ id: idx,
2247
+ data: {
2248
+ index
2249
+ }
2250
+ });
2251
+ const setNodeRef = useCombinedRefs(setDroppableNodeRef, setDraggableNodeRef);
2252
+ const [initialSize, setInitialSize] = useState5(size);
2253
+ const [resizing, setResizing] = useState5(false);
2254
+ const scrollHandler = useRef();
2255
+ const handleResizeStart = (_ev, _dir, elementRef) => {
2256
+ const scrollContainer = elementRef.closest('[role="columnheader"]');
2257
+ const scrollLeft = scrollContainer.scrollLeft;
2258
+ scrollHandler.current = (ev) => ev.target.scrollLeft = scrollLeft;
2259
+ scrollContainer.addEventListener("scroll", scrollHandler.current);
2260
+ scrollContainer.dataset.locked = "true";
2261
+ setResizing(true);
2262
+ };
2263
+ const handleResize = (_ev, _dir, _elementRef, { width }) => {
2264
+ onResize?.(idx, initialSize + width);
2265
+ };
2266
+ const handleResizeStop = (_ev, _dir, elementRef, { width }) => {
2267
+ const scrollContainer = elementRef.closest('[role="columnheader"]');
2268
+ scrollContainer.removeEventListener("scroll", scrollHandler.current);
2269
+ delete scrollContainer.dataset.locked;
2270
+ scrollHandler.current = void 0;
2271
+ setInitialSize(initialSize + width);
2272
+ onResize?.(idx, initialSize + width, true);
2273
+ setResizing(false);
2274
+ };
2275
+ return /* @__PURE__ */ React6.createElement(Resizable, {
2276
+ enable: {
2277
+ right: resize
2278
+ },
2279
+ size: {
2280
+ width: size - 1
2281
+ },
2282
+ minWidth: minWidth - 1,
2283
+ maxWidth,
2284
+ onResizeStart: handleResizeStart,
2285
+ onResize: handleResize,
2286
+ onResizeStop: handleResizeStop
2287
+ }, /* @__PURE__ */ React6.createElement("div", {
2288
+ ref: setNodeRef,
2289
+ ...attributes,
2290
+ ...listeners,
2291
+ className: mx2("flex h-full items-center justify-center cursor-pointer", "border-l border-gridLine focus-visible:outline-none", fragments.axis, selected && fragments.axisSelected, isDragging && fragments.axisSelected),
2292
+ onClick: () => onSelect?.(index)
2293
+ }, /* @__PURE__ */ React6.createElement("span", {
2294
+ className: "flex w-full justify-center"
2295
+ }, label), over?.id === idx && !isDragging && /* @__PURE__ */ React6.createElement("div", {
2296
+ className: "z-20 absolute left-0 h-full min-w-[4px] border-l-4 border-accentSurface"
2297
+ }), resizing && /* @__PURE__ */ React6.createElement("div", {
2298
+ className: "z-20 absolute right-0 h-full min-h-[4px] border-l-4 border-accentSurface"
2299
+ })));
2300
+ };
2301
+ var SheetGrid = /* @__PURE__ */ forwardRef(({ size, rows, columns, rowSizes, columnSizes }, forwardRef2) => {
2302
+ const { ref: containerRef, width: containerWidth = 0, height: containerHeight = 0 } = useResizeDetector({
2303
+ refreshRate: 200
2304
+ });
2305
+ const scrollerRef = useRef(null);
2306
+ useImperativeHandle(forwardRef2, () => scrollerRef.current);
2307
+ const { model, cursor, range, editing, setCursor, setRange, setEditing, onInfo } = useSheetContext();
2308
+ const initialText = useRef();
2309
+ const quickEdit = useRef(false);
2310
+ const [, forceUpdate] = useState5({});
2311
+ useEffect4(() => {
2312
+ const unsubscribe = model.update.on(() => {
2313
+ log2("updated", {
2314
+ id: model.id
2315
+ }, {
2316
+ F: __dxlog_file4,
2317
+ L: 738,
2318
+ S: void 0,
2319
+ C: (f, a) => f(...a)
2320
+ });
2321
+ forceUpdate({});
2322
+ });
2323
+ return () => {
2324
+ unsubscribe();
2325
+ };
2326
+ }, [
2327
+ model
2328
+ ]);
2329
+ const inputRef = useRef(null);
2330
+ const handleKeyDown = (ev) => {
2331
+ const isMacOS = /Mac|iPhone|iPod|iPad/.test(navigator.userAgent);
2332
+ if (cursor && (isMacOS && ev.metaKey || ev.ctrlKey)) {
2333
+ switch (ev.key) {
2334
+ case "x": {
2335
+ model.cut(range ?? {
2336
+ from: cursor
2337
+ });
2338
+ return;
2339
+ }
2340
+ case "c": {
2341
+ model.copy(range ?? {
2342
+ from: cursor
2343
+ });
2344
+ return;
2345
+ }
2346
+ case "v": {
2347
+ model.paste(cursor);
2348
+ return;
2349
+ }
2350
+ case "z": {
2351
+ if (ev.shiftKey) {
2352
+ model.redo();
2353
+ } else {
2354
+ model.undo();
2355
+ }
2356
+ return;
2357
+ }
2358
+ }
2359
+ }
2360
+ switch (ev.key) {
2361
+ case "ArrowUp":
2362
+ case "ArrowDown":
2363
+ case "ArrowLeft":
2364
+ case "ArrowRight":
2365
+ case "Home":
2366
+ case "End": {
2367
+ const next = handleNav(ev, cursor, range, size);
2368
+ setRange(next.range);
2369
+ if (next.cursor) {
2370
+ setCursor(next.cursor);
2371
+ const element = getCellElement(scrollerRef.current, next.cursor);
2372
+ if (element) {
2373
+ scrollIntoView(scrollerRef.current, element);
2374
+ }
2375
+ }
2376
+ break;
2377
+ }
2378
+ case "Backspace": {
2379
+ if (cursor) {
2380
+ if (range) {
2381
+ model.clear(range);
2382
+ } else {
2383
+ model.setValue(cursor, null);
2384
+ }
2385
+ }
2386
+ break;
2387
+ }
2388
+ case "Escape": {
2389
+ setRange(void 0);
2390
+ break;
2391
+ }
2392
+ case "Enter": {
2393
+ ev.stopPropagation();
2394
+ if (cursor) {
2395
+ setEditing(true);
2396
+ }
2397
+ break;
2398
+ }
2399
+ case "?": {
2400
+ onInfo?.();
2401
+ break;
2402
+ }
2403
+ default: {
2404
+ if (ev.key.length === 1) {
2405
+ initialText.current = ev.key;
2406
+ quickEdit.current = true;
2407
+ setEditing(true);
2408
+ }
2409
+ }
2410
+ }
2411
+ };
2412
+ const { handlers } = useRangeSelect((event, range2) => {
2413
+ switch (event) {
2414
+ case "start": {
2415
+ setRange(void 0);
2416
+ break;
2417
+ }
2418
+ default: {
2419
+ setRange(range2);
2420
+ }
2421
+ }
2422
+ });
2423
+ const { width, height, rowRange, columnRange } = useGridLayout({
2424
+ scroller: scrollerRef.current,
2425
+ size: {
2426
+ width: containerWidth,
2427
+ height: containerHeight
2428
+ },
2429
+ rows,
2430
+ columns,
2431
+ rowSizes,
2432
+ columnSizes
2433
+ });
2434
+ const id = fullyQualifiedId2(model.sheet);
2435
+ const { hasAttention } = useAttention(id);
2436
+ return /* @__PURE__ */ React6.createElement("div", {
2437
+ ref: containerRef,
2438
+ role: "grid",
2439
+ className: "relative flex grow overflow-hidden"
2440
+ }, /* @__PURE__ */ React6.createElement("div", {
2441
+ className: mx2("z-20 absolute inset-0 border border-gridLine pointer-events-none")
2442
+ }), /* @__PURE__ */ React6.createElement("div", {
2443
+ ref: scrollerRef,
2444
+ className: mx2("grow", hasAttention && "overflow-auto scrollbar-thin")
2445
+ }, /* @__PURE__ */ React6.createElement("div", {
2446
+ className: "relative select-none",
2447
+ style: {
2448
+ width,
2449
+ height
2450
+ },
2451
+ onClick: () => inputRef.current?.focus(),
2452
+ ...handlers
2453
+ }, scrollerRef.current && /* @__PURE__ */ React6.createElement(SelectionOverlay, {
2454
+ root: scrollerRef.current
2455
+ }), rowRange.map(({ row, top, height: height2 }) => {
2456
+ return columnRange.map(({ col, left, width: width2 }) => {
2457
+ const style = {
2458
+ position: "absolute",
2459
+ top,
2460
+ left,
2461
+ width: width2,
2462
+ height: height2
2463
+ };
2464
+ const cell = {
2465
+ row,
2466
+ col
2467
+ };
2468
+ const id2 = addressToA1Notation(cell);
2469
+ const idx = addressToIndex(model.sheet, cell);
2470
+ const active = posEquals(cursor, cell);
2471
+ if (active && editing) {
2472
+ const value = initialText.current ?? model.getCellText(cell) ?? "";
2473
+ const handleClose = (value2) => {
2474
+ initialText.current = void 0;
2475
+ quickEdit.current = false;
2476
+ if (value2 !== void 0) {
2477
+ model.setValue(cell, value2);
2478
+ const next = handleArrowNav({
2479
+ key: "ArrowDown",
2480
+ metaKey: false
2481
+ }, cursor, size);
2482
+ if (next) {
2483
+ setCursor(next);
2484
+ }
2485
+ }
2486
+ inputRef.current?.focus();
2487
+ setEditing(false);
2488
+ };
2489
+ const handleNav2 = (value2, { key }) => {
2490
+ initialText.current = void 0;
2491
+ model.setValue(cell, value2 ?? null);
2492
+ const next = handleArrowNav({
2493
+ key,
2494
+ metaKey: false
2495
+ }, cursor, size);
2496
+ if (next) {
2497
+ setCursor(next);
2498
+ }
2499
+ inputRef.current?.focus();
2500
+ setEditing(false);
2501
+ };
2502
+ return /* @__PURE__ */ React6.createElement(GridCellEditor, {
2503
+ key: idx,
2504
+ value,
2505
+ style,
2506
+ onNav: quickEdit.current ? handleNav2 : void 0,
2507
+ onClose: handleClose
2508
+ });
2509
+ }
2510
+ return /* @__PURE__ */ React6.createElement(SheetCell, {
2511
+ key: id2,
2512
+ id: id2,
2513
+ cell,
2514
+ active,
2515
+ style,
2516
+ onSelect: (cell2, edit) => {
2517
+ setEditing(edit);
2518
+ setCursor(cell2);
2519
+ }
2520
+ });
2521
+ });
2522
+ }))), /* @__PURE__ */ createPortal(/* @__PURE__ */ React6.createElement(SheetInput, {
2523
+ ref: inputRef,
2524
+ id,
2525
+ onKeyDown: handleKeyDown
2526
+ }), document.body));
2527
+ });
2528
+ var SheetInput = /* @__PURE__ */ forwardRef(({ id, onKeyDown }, forwardedRef) => {
2529
+ const path = useAttentionPath();
2530
+ const attendableAttrs = useAttendableAttributes(id);
2531
+ return path.toReversed().reduce((acc, part) => {
2532
+ return /* @__PURE__ */ React6.createElement("div", {
2533
+ [ATTENABLE_ATTRIBUTE]: part
2534
+ }, acc);
2535
+ }, /* @__PURE__ */ React6.createElement("input", {
2536
+ ref: forwardedRef,
2537
+ className: "absolute w-[1px] h-[1px] bg-transparent outline-none border-none caret-transparent",
2538
+ onKeyDown,
2539
+ ...attendableAttrs
2540
+ }));
2541
+ });
2542
+ var SelectionOverlay = ({ root }) => {
2543
+ const { range } = useSheetContext();
2544
+ if (!range) {
2545
+ return null;
2546
+ }
2547
+ const c1 = getCellElement(root, range.from);
2548
+ const c2 = range.to ? getCellElement(root, range.to) : c1;
2549
+ if (!c1 || !c2) {
2550
+ return null;
2551
+ }
2552
+ const b1 = getRelativeClientRect(root, c1);
2553
+ const b2 = getRelativeClientRect(root, c2);
2554
+ const bounds = getRectUnion(b1, b2);
2555
+ return /* @__PURE__ */ React6.createElement("div", {
2556
+ role: "none",
2557
+ style: bounds,
2558
+ className: "z-10 absolute pointer-events-none bg-gridSelectionOverlay border border-gridOverlay"
2559
+ });
2560
+ };
2561
+ var SheetCell = ({ id, cell, style, active, onSelect }) => {
2562
+ const { formatting, editing, setRange, decorations, model: { sheet } } = useSheetContext();
2563
+ const { value, classNames } = formatting.getFormatting(cell);
2564
+ const decorationsForCell = decorations.getDecorationsForCell(addressToIndex(sheet, cell)) ?? [];
2565
+ const decorationAddedClasses = useMemo4(() => decorationsForCell.flatMap((d) => d.classNames ?? []), [
2566
+ decorationsForCell
2567
+ ]);
2568
+ const decoratedContent = decorationsForCell.reduce((children, { decorate }) => {
2569
+ if (!decorate) {
2570
+ return children;
2571
+ }
2572
+ const DecoratorComponent = decorate;
2573
+ return /* @__PURE__ */ React6.createElement(DecoratorComponent, null, children);
2574
+ }, /* @__PURE__ */ React6.createElement("div", {
2575
+ role: "none",
2576
+ className: mx2("flex flex-grow bs-full is-full px-2 items-center truncate cursor-pointer", ...decorationAddedClasses)
2577
+ }, value));
2578
+ return /* @__PURE__ */ React6.createElement("div", {
2579
+ [`data-${CELL_DATA_KEY}`]: id,
2580
+ role: "cell",
2581
+ style,
2582
+ className: mx2("border border-gridLine cursor-pointer", fragments.cell, active && [
2583
+ "z-20",
2584
+ fragments.cellSelected
2585
+ ], classNames),
2586
+ onClick: () => {
2587
+ if (editing) {
2588
+ setRange?.({
2589
+ from: cell
2590
+ });
2591
+ } else {
2592
+ onSelect?.(cell, false);
2593
+ }
2594
+ },
2595
+ onDoubleClick: () => onSelect?.(cell, true)
2596
+ }, decoratedContent);
2597
+ };
2598
+ var GridCellEditor = ({ style, value, onNav, onClose }) => {
2599
+ const { model, range } = useSheetContext();
2600
+ const notifier = useRef();
2601
+ useEffect4(() => {
2602
+ if (range) {
2603
+ notifier.current?.(rangeToA1Notation(range));
2604
+ }
2605
+ }, [
2606
+ range
2607
+ ]);
2608
+ const extension = useMemo4(() => [
2609
+ editorKeys({
2610
+ onNav,
2611
+ onClose
2612
+ }),
2613
+ sheetExtension({
2614
+ functions: model.graph.getFunctions()
2615
+ }),
2616
+ rangeExtension((fn) => notifier.current = fn)
2617
+ ], [
2618
+ model
2619
+ ]);
2620
+ return /* @__PURE__ */ React6.createElement("div", {
2621
+ role: "cell",
2622
+ style,
2623
+ className: mx2("z-20 flex", fragments.cellSelected),
2624
+ onClick: (ev) => ev.stopPropagation()
2625
+ }, /* @__PURE__ */ React6.createElement(CellEditor, {
2626
+ autoFocus: true,
2627
+ value,
2628
+ extension
2629
+ }));
2630
+ };
2631
+ var SheetStatusBar = () => {
2632
+ const { model, cursor, range } = useSheetContext();
2633
+ let value;
2634
+ let isFormula = false;
2635
+ if (cursor) {
2636
+ value = model.getCellValue(cursor);
2637
+ if (typeof value === "string" && value.charAt(0) === "=") {
2638
+ value = model.graph.mapFunctionBindingFromId(model.mapFormulaIndicesToRefs(value));
2639
+ isFormula = true;
2640
+ } else if (value != null) {
2641
+ value = String(value);
2642
+ }
2643
+ }
2644
+ return /* @__PURE__ */ React6.createElement("div", {
2645
+ className: mx2("flex shrink-0 justify-between items-center px-4 py-1 text-sm border-x border-gridLine")
2646
+ }, /* @__PURE__ */ React6.createElement("div", {
2647
+ className: "flex gap-4 items-center"
2648
+ }, /* @__PURE__ */ React6.createElement("div", {
2649
+ className: "flex w-16 items-center font-mono"
2650
+ }, range && rangeToA1Notation(range) || cursor && addressToA1Notation(cursor)), /* @__PURE__ */ React6.createElement("div", {
2651
+ className: "flex gap-2 items-center"
2652
+ }, /* @__PURE__ */ React6.createElement(FunctionIcon, {
2653
+ className: mx2("text-greenText", isFormula ? "visible" : "invisible")
2654
+ }), /* @__PURE__ */ React6.createElement("span", {
2655
+ className: "font-mono"
2656
+ }, value))));
2657
+ };
2658
+ var SheetDebug = () => {
2659
+ const { model, cursor, range } = useSheetContext();
2660
+ const [, forceUpdate] = useState5({});
2661
+ useEffect4(() => {
2662
+ const accessor = createDocAccessor(model.sheet, []);
2663
+ const handleUpdate = () => forceUpdate({});
2664
+ accessor.handle.addListener("change", handleUpdate);
2665
+ handleUpdate();
2666
+ return () => {
2667
+ accessor.handle.removeListener("change", handleUpdate);
2668
+ };
2669
+ }, [
2670
+ model
2671
+ ]);
2672
+ return /* @__PURE__ */ React6.createElement("div", {
2673
+ className: mx2("z-20 absolute right-0 top-20 bottom-20 w-[30rem] overflow-auto scrollbar-thin", "border border-gridLine text-xs bg-neutral-50 dark:bg-black text-cyan-500 font-mono p-1 opacity-80")
2674
+ }, /* @__PURE__ */ React6.createElement("pre", {
2675
+ className: "whitespace-pre-wrap"
2676
+ }, JSON.stringify({
2677
+ cursor,
2678
+ range,
2679
+ cells: model.sheet.cells,
2680
+ rowMeta: model.sheet.rowMeta,
2681
+ columnMeta: model.sheet.columnMeta,
2682
+ formatting: model.sheet.formatting
2683
+ }, void 0, 2)));
2684
+ };
2685
+ var Sheet = {
2686
+ Root: SheetRoot,
2687
+ Main: SheetMain,
2688
+ Rows: SheetRows,
2689
+ Columns: SheetColumns,
2690
+ Grid: SheetGrid,
2691
+ Cell: SheetCell,
2692
+ StatusBar: SheetStatusBar,
2693
+ Debug: SheetDebug
2694
+ };
2695
+
2696
+ export {
2697
+ ComputeGraphContextProvider,
2698
+ createSheet,
2699
+ addressToIndex,
2700
+ compareIndexPositions,
2701
+ useComputeGraph,
2702
+ useSheetContext,
2703
+ Sheet,
2704
+ SheetContainer
2705
+ };
2706
+ //# sourceMappingURL=chunk-QEUCIHIN.mjs.map