@dxos/plugin-sheet 0.6.11 → 0.6.12-main.15a606f

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 (260) hide show
  1. package/dist/lib/browser/SheetContainer-V4GCCZTX.mjs +261 -0
  2. package/dist/lib/browser/SheetContainer-V4GCCZTX.mjs.map +7 -0
  3. package/dist/lib/browser/{chunk-D5AGLXJP.mjs → chunk-6ZMQVB4Z.mjs} +359 -671
  4. package/dist/lib/browser/chunk-6ZMQVB4Z.mjs.map +7 -0
  5. package/dist/lib/browser/{chunk-JRL5LGCE.mjs → chunk-QILRZNE5.mjs} +2 -5
  6. package/dist/lib/browser/chunk-QILRZNE5.mjs.map +7 -0
  7. package/dist/lib/browser/{chunk-FUAGSXA4.mjs → chunk-T3NJFTD4.mjs} +8 -15
  8. package/dist/lib/browser/chunk-T3NJFTD4.mjs.map +7 -0
  9. package/dist/lib/browser/{SheetContainer-U4H5D34A.mjs → chunk-U2JHW3L6.mjs} +1020 -240
  10. package/dist/lib/browser/chunk-U2JHW3L6.mjs.map +7 -0
  11. package/dist/lib/browser/graph-T27BOBOV.mjs +21 -0
  12. package/dist/lib/browser/graph-T27BOBOV.mjs.map +7 -0
  13. package/dist/lib/browser/index.mjs +68 -56
  14. package/dist/lib/browser/index.mjs.map +3 -3
  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-3ZY7MPWJ.cjs +279 -0
  19. package/dist/lib/node/SheetContainer-3ZY7MPWJ.cjs.map +7 -0
  20. package/dist/lib/node/{chunk-BJ6ZD7MN.cjs → chunk-BNARJ5GM.cjs} +5 -18
  21. package/dist/lib/node/chunk-BNARJ5GM.cjs.map +7 -0
  22. package/dist/lib/node/{chunk-5KKJ4NPP.cjs → chunk-DD6FIXWC.cjs} +360 -667
  23. package/dist/lib/node/chunk-DD6FIXWC.cjs.map +7 -0
  24. package/dist/lib/node/{SheetContainer-AXQV3ZT5.cjs → chunk-OTTD7FBK.cjs} +1050 -279
  25. package/dist/lib/node/chunk-OTTD7FBK.cjs.map +7 -0
  26. package/dist/lib/node/{chunk-DSYKOI4E.cjs → chunk-Q3HBHPRL.cjs} +12 -20
  27. package/dist/lib/node/chunk-Q3HBHPRL.cjs.map +7 -0
  28. package/dist/lib/node/graph-SPKGX7W4.cjs +43 -0
  29. package/dist/lib/node/graph-SPKGX7W4.cjs.map +7 -0
  30. package/dist/lib/node/index.cjs +83 -64
  31. package/dist/lib/node/index.cjs.map +3 -3
  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-PXSJX6XK.mjs +262 -0
  38. package/dist/lib/node-esm/SheetContainer-PXSJX6XK.mjs.map +7 -0
  39. package/dist/lib/node-esm/chunk-7HVSOTGA.mjs +2553 -0
  40. package/dist/lib/node-esm/chunk-7HVSOTGA.mjs.map +7 -0
  41. package/dist/lib/node-esm/chunk-BMNA27EX.mjs +76 -0
  42. package/dist/lib/node-esm/chunk-BMNA27EX.mjs.map +7 -0
  43. package/dist/lib/node-esm/chunk-D6KU5MI7.mjs +2925 -0
  44. package/dist/lib/node-esm/chunk-D6KU5MI7.mjs.map +7 -0
  45. package/dist/lib/node-esm/chunk-IU2L277A.mjs +17 -0
  46. package/dist/lib/node-esm/chunk-IU2L277A.mjs.map +7 -0
  47. package/dist/lib/node-esm/graph-U67IO4UC.mjs +22 -0
  48. package/dist/lib/node-esm/graph-U67IO4UC.mjs.map +7 -0
  49. package/dist/lib/node-esm/index.mjs +261 -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 +6 -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 +6 -5
  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 +5 -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/{components/ComputeGraph → graph}/async-function.d.ts +1 -1
  109. package/dist/types/src/graph/async-function.d.ts.map +1 -0
  110. package/dist/types/src/graph/compute-graph.browser.test.d.ts +2 -0
  111. package/dist/types/src/graph/compute-graph.browser.test.d.ts.map +1 -0
  112. package/dist/types/src/graph/compute-graph.d.ts +81 -0
  113. package/dist/types/src/graph/compute-graph.d.ts.map +1 -0
  114. package/dist/types/src/graph/compute-graph.stories.d.ts +10 -0
  115. package/dist/types/src/graph/compute-graph.stories.d.ts.map +1 -0
  116. package/dist/types/src/graph/compute-node.d.ts +19 -0
  117. package/dist/types/src/graph/compute-node.d.ts.map +1 -0
  118. package/dist/types/src/{components/ComputeGraph/custom.d.ts → graph/custom-function.d.ts} +1 -1
  119. package/dist/types/src/graph/custom-function.d.ts.map +1 -0
  120. package/dist/types/src/graph/edge-function.d.ts.map +1 -0
  121. package/dist/types/src/{model/functions.d.ts → graph/function-defs.d.ts} +1 -1
  122. package/dist/types/src/graph/function-defs.d.ts.map +1 -0
  123. package/dist/types/src/graph/hyperformula.test.d.ts +2 -0
  124. package/dist/types/src/graph/hyperformula.test.d.ts.map +1 -0
  125. package/dist/types/src/graph/index.d.ts +4 -0
  126. package/dist/types/src/graph/index.d.ts.map +1 -0
  127. package/dist/types/src/graph/util.d.ts +2 -0
  128. package/dist/types/src/graph/util.d.ts.map +1 -0
  129. package/dist/types/src/hooks/hooks.stories.d.ts +11 -0
  130. package/dist/types/src/hooks/hooks.stories.d.ts.map +1 -0
  131. package/dist/types/src/hooks/index.d.ts +4 -0
  132. package/dist/types/src/hooks/index.d.ts.map +1 -0
  133. package/dist/types/src/hooks/useComputeGraph.d.ts +7 -0
  134. package/dist/types/src/hooks/useComputeGraph.d.ts.map +1 -0
  135. package/dist/types/src/hooks/useFormattingModel.d.ts +3 -0
  136. package/dist/types/src/hooks/useFormattingModel.d.ts.map +1 -0
  137. package/dist/types/src/hooks/useSheetModel.d.ts +8 -0
  138. package/dist/types/src/hooks/useSheetModel.d.ts.map +1 -0
  139. package/dist/types/src/meta.d.ts +1 -4
  140. package/dist/types/src/meta.d.ts.map +1 -1
  141. package/dist/types/src/model/formatting-model.d.ts +16 -0
  142. package/dist/types/src/model/formatting-model.d.ts.map +1 -0
  143. package/dist/types/src/model/index.d.ts +2 -3
  144. package/dist/types/src/model/index.d.ts.map +1 -1
  145. package/dist/types/src/model/{model.d.ts → sheet-model.d.ts} +9 -64
  146. package/dist/types/src/model/sheet-model.d.ts.map +1 -0
  147. package/dist/types/src/sanity.test.d.ts +2 -0
  148. package/dist/types/src/sanity.test.d.ts.map +1 -0
  149. package/dist/types/src/testing/index.d.ts +2 -0
  150. package/dist/types/src/testing/index.d.ts.map +1 -0
  151. package/dist/types/src/testing/testing.d.ts +9 -0
  152. package/dist/types/src/testing/testing.d.ts.map +1 -0
  153. package/dist/types/src/translations.d.ts +17 -12
  154. package/dist/types/src/translations.d.ts.map +1 -1
  155. package/dist/types/src/types.d.ts +83 -3
  156. package/dist/types/src/types.d.ts.map +1 -1
  157. package/dist/vendor/hyperformula.mjs +37145 -0
  158. package/package.json +48 -41
  159. package/src/SheetPlugin.tsx +43 -70
  160. package/src/components/CellEditor/CellEditor.stories.tsx +4 -3
  161. package/src/components/CellEditor/CellEditor.tsx +59 -9
  162. package/src/components/CellEditor/extension.test.ts +4 -5
  163. package/src/components/CellEditor/extension.ts +1 -3
  164. package/src/components/ComputeGraph/ComputeGraphContextProvider.tsx +20 -0
  165. package/src/components/ComputeGraph/index.ts +1 -3
  166. package/src/components/GridSheet/GridSheet.stories.tsx +35 -0
  167. package/src/components/GridSheet/GridSheet.tsx +153 -0
  168. package/src/components/GridSheet/util.ts +89 -0
  169. package/src/components/Sheet/Sheet.stories.tsx +45 -82
  170. package/src/components/Sheet/Sheet.tsx +57 -18
  171. package/src/components/Sheet/decorations.ts +62 -0
  172. package/src/components/Sheet/grid.ts +3 -3
  173. package/src/components/Sheet/nav.ts +19 -19
  174. package/src/components/Sheet/sheet-context.tsx +16 -78
  175. package/src/components/Sheet/threads.tsx +205 -0
  176. package/src/components/SheetContainer.tsx +73 -19
  177. package/src/components/Toolbar/Toolbar.tsx +53 -12
  178. package/src/components/index.ts +1 -0
  179. package/src/defs/index.ts +6 -0
  180. package/src/{model → defs}/types.test.ts +8 -9
  181. package/src/{model → defs}/types.ts +23 -14
  182. package/src/defs/util.ts +135 -0
  183. package/src/extensions/compute.stories.tsx +151 -0
  184. package/src/extensions/compute.ts +98 -0
  185. package/src/extensions/index.ts +5 -0
  186. package/src/{components/ComputeGraph → graph}/async-function.ts +3 -1
  187. package/src/graph/compute-graph.browser.test.ts +104 -0
  188. package/src/graph/compute-graph.stories.tsx +92 -0
  189. package/src/graph/compute-graph.ts +290 -0
  190. package/src/graph/compute-node.ts +51 -0
  191. package/src/{components/ComputeGraph/custom.ts → graph/custom-function.ts} +2 -6
  192. package/src/{components/ComputeGraph → graph}/edge-function.ts +2 -1
  193. package/src/graph/hyperformula.test.ts +15 -0
  194. package/src/graph/index.ts +7 -0
  195. package/src/graph/util.ts +8 -0
  196. package/src/hooks/hooks.stories.tsx +50 -0
  197. package/src/hooks/index.ts +7 -0
  198. package/src/hooks/useComputeGraph.ts +20 -0
  199. package/src/hooks/useFormattingModel.ts +11 -0
  200. package/src/hooks/useSheetModel.ts +43 -0
  201. package/src/meta.tsx +1 -5
  202. package/src/{components/Sheet/formatting.ts → model/formatting-model.ts} +20 -13
  203. package/src/model/index.ts +2 -3
  204. package/src/model/sheet-model.ts +399 -0
  205. package/src/sanity.test.ts +40 -0
  206. package/src/testing/index.ts +5 -0
  207. package/src/testing/testing.tsx +66 -0
  208. package/src/translations.ts +6 -1
  209. package/src/types.ts +30 -5
  210. package/dist/lib/browser/SheetContainer-U4H5D34A.mjs.map +0 -7
  211. package/dist/lib/browser/chunk-APHOLYUB.mjs +0 -175
  212. package/dist/lib/browser/chunk-APHOLYUB.mjs.map +0 -7
  213. package/dist/lib/browser/chunk-D5AGLXJP.mjs.map +0 -7
  214. package/dist/lib/browser/chunk-FUAGSXA4.mjs.map +0 -7
  215. package/dist/lib/browser/chunk-JRL5LGCE.mjs.map +0 -7
  216. package/dist/lib/browser/chunk-NU4PBN33.mjs +0 -8
  217. package/dist/lib/browser/chunk-NU4PBN33.mjs.map +0 -7
  218. package/dist/lib/browser/testing.mjs +0 -92
  219. package/dist/lib/browser/testing.mjs.map +0 -7
  220. package/dist/lib/node/SheetContainer-AXQV3ZT5.cjs.map +0 -7
  221. package/dist/lib/node/chunk-5KKJ4NPP.cjs.map +0 -7
  222. package/dist/lib/node/chunk-BJ6ZD7MN.cjs.map +0 -7
  223. package/dist/lib/node/chunk-CN3RPESU.cjs +0 -202
  224. package/dist/lib/node/chunk-CN3RPESU.cjs.map +0 -7
  225. package/dist/lib/node/chunk-DSYKOI4E.cjs.map +0 -7
  226. package/dist/lib/node/chunk-PYXHNAAK.cjs +0 -40
  227. package/dist/lib/node/chunk-PYXHNAAK.cjs.map +0 -7
  228. package/dist/lib/node/testing.cjs +0 -111
  229. package/dist/lib/node/testing.cjs.map +0 -7
  230. package/dist/types/src/components/ComputeGraph/async-function.d.ts.map +0 -1
  231. package/dist/types/src/components/ComputeGraph/custom.d.ts.map +0 -1
  232. package/dist/types/src/components/ComputeGraph/edge-function.d.ts.map +0 -1
  233. package/dist/types/src/components/ComputeGraph/graph-context.d.ts +0 -12
  234. package/dist/types/src/components/ComputeGraph/graph-context.d.ts.map +0 -1
  235. package/dist/types/src/components/ComputeGraph/graph.browser.test.d.ts +0 -2
  236. package/dist/types/src/components/ComputeGraph/graph.browser.test.d.ts.map +0 -1
  237. package/dist/types/src/components/ComputeGraph/graph.d.ts +0 -26
  238. package/dist/types/src/components/ComputeGraph/graph.d.ts.map +0 -1
  239. package/dist/types/src/components/Sheet/formatting.d.ts +0 -14
  240. package/dist/types/src/components/Sheet/formatting.d.ts.map +0 -1
  241. package/dist/types/src/model/functions.d.ts.map +0 -1
  242. package/dist/types/src/model/model.browser.test.d.ts +0 -2
  243. package/dist/types/src/model/model.browser.test.d.ts.map +0 -1
  244. package/dist/types/src/model/model.d.ts.map +0 -1
  245. package/dist/types/src/model/types.d.ts.map +0 -1
  246. package/dist/types/src/model/types.test.d.ts.map +0 -1
  247. package/dist/types/src/model/util.d.ts +0 -15
  248. package/dist/types/src/model/util.d.ts.map +0 -1
  249. package/dist/types/src/testing.d.ts +0 -9
  250. package/dist/types/src/testing.d.ts.map +0 -1
  251. package/src/components/ComputeGraph/graph-context.tsx +0 -50
  252. package/src/components/ComputeGraph/graph.browser.test.ts +0 -50
  253. package/src/components/ComputeGraph/graph.ts +0 -62
  254. package/src/model/model.browser.test.ts +0 -100
  255. package/src/model/model.ts +0 -550
  256. package/src/model/util.ts +0 -36
  257. package/src/testing.ts +0 -50
  258. /package/dist/types/src/{model → defs}/types.test.d.ts +0 -0
  259. /package/dist/types/src/{components/ComputeGraph → graph}/edge-function.d.ts +0 -0
  260. /package/src/{model/functions.ts → graph/function-defs.ts} +0 -0
@@ -0,0 +1,20 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { useContext } from 'react';
6
+
7
+ import { raise } from '@dxos/debug';
8
+ import { type Space } from '@dxos/react-client/echo';
9
+ import { useAsyncState } from '@dxos/react-hooks';
10
+
11
+ import { ComputeGraphContext } from '../components';
12
+ import { type ComputeGraph } from '../graph';
13
+
14
+ /**
15
+ * Get existing or create new compute graph for the given space.
16
+ */
17
+ export const useComputeGraph = (space?: Space): ComputeGraph | undefined => {
18
+ const { registry } = useContext(ComputeGraphContext) ?? raise(new Error('Missing ComputeGraphContext'));
19
+ return useAsyncState(async () => space && registry.getOrCreateGraph(space), [space, registry]);
20
+ };
@@ -0,0 +1,11 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { useMemo } from 'react';
6
+
7
+ import { type SheetModel, FormattingModel } from '../model';
8
+
9
+ export const useFormattingModel = (model: SheetModel | undefined): FormattingModel | undefined => {
10
+ return useMemo(() => model && new FormattingModel(model), [model]);
11
+ };
@@ -0,0 +1,43 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { useEffect, useState } from 'react';
6
+
7
+ import { type Space } from '@dxos/react-client/echo';
8
+
9
+ import { useComputeGraph } from './useComputeGraph';
10
+ import { SheetModel } from '../model';
11
+ import { type SheetType } from '../types';
12
+
13
+ export type UseSheetModelOptions = {
14
+ readonly?: boolean;
15
+ };
16
+
17
+ export const useSheetModel = (
18
+ space?: Space,
19
+ sheet?: SheetType,
20
+ { readonly }: UseSheetModelOptions = {},
21
+ ): SheetModel | undefined => {
22
+ const graph = useComputeGraph(space);
23
+ const [model, setModel] = useState<SheetModel>();
24
+ useEffect(() => {
25
+ if (!space || !graph || !sheet) {
26
+ return;
27
+ }
28
+
29
+ let model: SheetModel | undefined;
30
+ const t = setTimeout(async () => {
31
+ model = new SheetModel(graph, sheet, { readonly });
32
+ await model.open();
33
+ setModel(model);
34
+ });
35
+
36
+ return () => {
37
+ clearTimeout(t);
38
+ void model?.close();
39
+ };
40
+ }, [space, sheet, graph, readonly]);
41
+
42
+ return model;
43
+ };
package/src/meta.tsx CHANGED
@@ -2,9 +2,6 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { GridNine, type IconProps } from '@phosphor-icons/react';
6
- import React from 'react';
7
-
8
5
  import { pluginMeta } from '@dxos/app-framework';
9
6
 
10
7
  export const SHEET_PLUGIN = 'dxos.org/plugin/sheet';
@@ -13,6 +10,5 @@ export default pluginMeta({
13
10
  id: SHEET_PLUGIN,
14
11
  name: 'Sheet',
15
12
  description: 'A simple spreadsheet plugin.',
16
- iconComponent: (props: IconProps) => <GridNine {...props} />,
17
- iconSymbol: 'ph--grid-nine--regular',
13
+ icon: 'ph--grid-nine--regular',
18
14
  });
@@ -4,17 +4,24 @@
4
4
 
5
5
  import { type ClassNameValue } from '@dxos/react-ui-types';
6
6
 
7
- import { type SheetModel, type CellAddress, inRange } from '../../model';
8
- import { ValueTypeEnum } from '../../types';
7
+ import { type SheetModel } from './sheet-model';
8
+ import { type CellAddress, inRange } from '../defs';
9
+ import { addressToIndex, rangeFromIndex } from '../defs';
10
+ import { ValueTypeEnum } from '../types';
11
+
12
+ export type CellFormat = {
13
+ value?: string;
14
+ classNames?: ClassNameValue;
15
+ };
9
16
 
10
17
  export class FormattingModel {
11
- constructor(private readonly model: SheetModel) {}
18
+ constructor(private readonly _model: SheetModel) {}
12
19
 
13
20
  /**
14
21
  * Get formatted string value and className for cell.
15
22
  */
16
- getFormatting(cell: CellAddress): { value?: string; classNames?: ClassNameValue } {
17
- const value = this.model.getValue(cell);
23
+ getFormatting(cell: CellAddress): CellFormat {
24
+ const value = this._model.getValue(cell);
18
25
  if (value === undefined || value === null) {
19
26
  return {};
20
27
  }
@@ -23,15 +30,15 @@ export class FormattingModel {
23
30
  const locales = undefined;
24
31
 
25
32
  // Cell-specific formatting.
26
- const idx = this.model.addressToIndex(cell);
27
- let formatting = this.model.sheet.formatting?.[idx] ?? {};
33
+ const idx = addressToIndex(this._model.sheet, cell);
34
+ let formatting = this._model.sheet.formatting?.[idx] ?? {};
28
35
  const classNames = [...(formatting?.classNames ?? [])];
29
36
 
30
37
  // Range formatting.
31
38
  // TODO(burdon): NOTE: D0 means the D column.
32
39
  // TODO(burdon): Cache model formatting (e.g., for ranges). Create class out of this function.
33
- for (const [idx, _formatting] of Object.entries(this.model.sheet.formatting)) {
34
- const range = this.model.rangeFromIndex(idx);
40
+ for (const [idx, _formatting] of Object.entries(this._model.sheet.formatting)) {
41
+ const range = rangeFromIndex(this._model.sheet, idx);
35
42
  if (inRange(range, cell)) {
36
43
  if (_formatting.classNames) {
37
44
  classNames.push(..._formatting.classNames);
@@ -46,7 +53,7 @@ export class FormattingModel {
46
53
 
47
54
  const defaultNumber = 'justify-end font-mono';
48
55
 
49
- const type = formatting?.type ?? this.model.getValueType(cell);
56
+ const type = formatting?.type ?? this._model.getValueType(cell);
50
57
  switch (type) {
51
58
  case ValueTypeEnum.Boolean: {
52
59
  return {
@@ -84,17 +91,17 @@ export class FormattingModel {
84
91
  //
85
92
 
86
93
  case ValueTypeEnum.DateTime: {
87
- const date = this.model.toLocalDate(value as number);
94
+ const date = this._model.toLocalDate(value as number);
88
95
  return { value: date.toLocaleString(locales), classNames };
89
96
  }
90
97
 
91
98
  case ValueTypeEnum.Date: {
92
- const date = this.model.toLocalDate(value as number);
99
+ const date = this._model.toLocalDate(value as number);
93
100
  return { value: date.toLocaleDateString(locales), classNames };
94
101
  }
95
102
 
96
103
  case ValueTypeEnum.Time: {
97
- const date = this.model.toLocalDate(value as number);
104
+ const date = this._model.toLocalDate(value as number);
98
105
  return { value: date.toLocaleTimeString(locales), classNames };
99
106
  }
100
107
 
@@ -2,6 +2,5 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- export * from './functions';
6
- export * from './model';
7
- export * from './types';
5
+ export * from './formatting-model';
6
+ export * from './sheet-model';
@@ -0,0 +1,399 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { type SimpleCellRange } from 'hyperformula/typings/AbsoluteCellRange';
6
+ import { type SimpleCellAddress } from 'hyperformula/typings/Cell';
7
+ import { type SimpleDate, type SimpleDateTime } from 'hyperformula/typings/DateTimeHelper';
8
+
9
+ import { Event } from '@dxos/async';
10
+ import { Resource } from '@dxos/context';
11
+ import { invariant } from '@dxos/invariant';
12
+ import { PublicKey } from '@dxos/keys';
13
+ import { log } from '@dxos/log';
14
+
15
+ import { DetailedCellError, ExportedCellChange } from '#hyperformula';
16
+ import {
17
+ addressFromA1Notation,
18
+ addressToA1Notation,
19
+ type CellAddress,
20
+ type CellRange,
21
+ MAX_COLUMNS,
22
+ MAX_ROWS,
23
+ } from '../defs';
24
+ import { addressFromIndex, addressToIndex, initialize, insertIndices, ReadonlyException } from '../defs';
25
+ import { type ComputeNode, type ComputeGraph, createSheetName } from '../graph';
26
+ import { type CellScalarValue, type CellValue, type SheetType, ValueTypeEnum } from '../types';
27
+
28
+ const typeMap: Record<string, ValueTypeEnum> = {
29
+ BOOLEAN: ValueTypeEnum.Boolean,
30
+ NUMBER_RAW: ValueTypeEnum.Number,
31
+ NUMBER_PERCENT: ValueTypeEnum.Percent,
32
+ NUMBER_CURRENCY: ValueTypeEnum.Currency,
33
+ NUMBER_DATETIME: ValueTypeEnum.DateTime,
34
+ NUMBER_DATE: ValueTypeEnum.Date,
35
+ NUMBER_TIME: ValueTypeEnum.Time,
36
+ };
37
+
38
+ const getTopLeft = (range: CellRange): CellAddress => {
39
+ const to = range.to ?? range.from;
40
+ return { row: Math.min(range.from.row, to.row), col: Math.min(range.from.col, to.col) };
41
+ };
42
+
43
+ const toSimpleCellAddress = (sheet: number, cell: CellAddress): SimpleCellAddress => ({
44
+ sheet,
45
+ row: cell.row,
46
+ col: cell.col,
47
+ });
48
+
49
+ const toModelRange = (sheet: number, range: CellRange): SimpleCellRange => ({
50
+ start: toSimpleCellAddress(sheet, range.from),
51
+ end: toSimpleCellAddress(sheet, range.to ?? range.from),
52
+ });
53
+
54
+ export type SheetModelOptions = {
55
+ readonly?: boolean;
56
+ };
57
+
58
+ /**
59
+ * Spreadsheet data model.
60
+ *
61
+ * [ComputeGraphContext] > [SheetContext]:[SheetModel] > [Sheet.Root]
62
+ */
63
+ // TODO(burdon): Factor out commonality with ComputeNode.
64
+ export class SheetModel extends Resource {
65
+ public readonly id = `model-${PublicKey.random().truncate()}`;
66
+
67
+ public readonly update = new Event();
68
+
69
+ private readonly _node: ComputeNode;
70
+
71
+ constructor(
72
+ private readonly _graph: ComputeGraph,
73
+ private readonly _sheet: SheetType,
74
+ private readonly _options: SheetModelOptions = {},
75
+ ) {
76
+ super();
77
+ // TODO(burdon): SheetModel should extend ComputeNode and be constructed via the graph.
78
+ this._node = this._graph.getOrCreateNode(createSheetName(this._sheet.id));
79
+ this.reset();
80
+ }
81
+
82
+ get graph() {
83
+ return this._graph;
84
+ }
85
+
86
+ get sheet() {
87
+ return this._sheet;
88
+ }
89
+
90
+ get readonly() {
91
+ return this._options.readonly;
92
+ }
93
+
94
+ get bounds() {
95
+ return {
96
+ rows: this._sheet.rows.length,
97
+ columns: this._sheet.columns.length,
98
+ };
99
+ }
100
+
101
+ /**
102
+ * Initialize sheet and engine.
103
+ */
104
+ protected override async _open() {
105
+ log('initialize', { id: this.id });
106
+ initialize(this._sheet);
107
+ this.reset();
108
+
109
+ // TODO(burdon): Event hierarchy?
110
+ // Listen for model updates (e.g., async calculations).
111
+ const unsubscribe = this._graph.update.on(() => this.update.emit());
112
+ this._ctx.onDispose(unsubscribe);
113
+ }
114
+
115
+ /**
116
+ * Update engine.
117
+ * NOTE: This resets the undo history.
118
+ * @deprecated
119
+ */
120
+ reset() {
121
+ this._node.hf.clearSheet(this._node.sheetId);
122
+ Object.entries(this._sheet.cells).forEach(([key, { value }]) => {
123
+ const { col, row } = addressFromIndex(this._sheet, key);
124
+ if (typeof value === 'string' && value.charAt(0) === '=') {
125
+ value = this._graph.mapFormulaToNative(
126
+ this._graph.mapFunctionBindingFromId(this.mapFormulaIndicesToRefs(value)),
127
+ );
128
+ }
129
+
130
+ this._node.hf.setCellContents({ sheet: this._node.sheetId, row, col }, value);
131
+ });
132
+ }
133
+
134
+ /**
135
+ * Recalculate formulas.
136
+ * NOTE: This resets the undo history.
137
+ * https://hyperformula.handsontable.com/guide/volatile-functions.html#volatile-actions
138
+ * @deprecated
139
+ */
140
+ // TODO(burdon): Remove.
141
+ recalculate() {
142
+ this._node.hf.rebuildAndRecalculate();
143
+ }
144
+
145
+ insertRows(i: number, n = 1) {
146
+ insertIndices(this._sheet.rows, i, n, MAX_ROWS);
147
+ this.reset();
148
+ }
149
+
150
+ insertColumns(i: number, n = 1) {
151
+ insertIndices(this._sheet.columns, i, n, MAX_COLUMNS);
152
+ this.reset();
153
+ }
154
+
155
+ //
156
+ // Undoable actions.
157
+ // TODO(burdon): Group undoable methods; consistently update hf/sheet.
158
+ //
159
+
160
+ /**
161
+ * Clear range of values.
162
+ */
163
+ clear(range: CellRange) {
164
+ const topLeft = getTopLeft(range);
165
+ const values = this._iterRange(range, () => null);
166
+ this._node.hf.setCellContents(toSimpleCellAddress(this._node.sheetId, topLeft), values);
167
+ this._iterRange(range, (cell) => {
168
+ const idx = addressToIndex(this._sheet, cell);
169
+ delete this._sheet.cells[idx];
170
+ });
171
+ }
172
+
173
+ cut(range: CellRange) {
174
+ this._node.hf.cut(toModelRange(this._node.sheetId, range));
175
+ this._iterRange(range, (cell) => {
176
+ const idx = addressToIndex(this._sheet, cell);
177
+ delete this._sheet.cells[idx];
178
+ });
179
+ }
180
+
181
+ copy(range: CellRange) {
182
+ this._node.hf.copy(toModelRange(this._node.sheetId, range));
183
+ }
184
+
185
+ paste(cell: CellAddress) {
186
+ if (!this._node.hf.isClipboardEmpty()) {
187
+ const changes = this._node.hf.paste(toSimpleCellAddress(this._node.sheetId, cell));
188
+ for (const change of changes) {
189
+ if (change instanceof ExportedCellChange) {
190
+ const { address, newValue } = change;
191
+ const idx = addressToIndex(this._sheet, { row: address.row, col: address.col });
192
+ this._sheet.cells[idx] = { value: newValue };
193
+ }
194
+ }
195
+ }
196
+ }
197
+
198
+ // TODO(burdon): Display undo/redo state.
199
+ undo() {
200
+ if (this._node.hf.isThereSomethingToUndo()) {
201
+ this._node.hf.undo();
202
+ this.update.emit();
203
+ }
204
+ }
205
+
206
+ redo() {
207
+ if (this._node.hf.isThereSomethingToRedo()) {
208
+ this._node.hf.redo();
209
+ this.update.emit();
210
+ }
211
+ }
212
+
213
+ /**
214
+ * Get value from sheet.
215
+ */
216
+ getCellValue(cell: CellAddress): CellScalarValue {
217
+ const idx = addressToIndex(this._sheet, cell);
218
+ return this._sheet.cells[idx]?.value ?? null;
219
+ }
220
+
221
+ /**
222
+ * Get value as a string for editing.
223
+ */
224
+ getCellText(cell: CellAddress): string | undefined {
225
+ const value = this.getCellValue(cell);
226
+ if (value == null) {
227
+ return undefined;
228
+ }
229
+
230
+ if (typeof value === 'string' && value.charAt(0) === '=') {
231
+ return this._graph.mapFunctionBindingFromId(this.mapFormulaIndicesToRefs(value));
232
+ } else {
233
+ return String(value);
234
+ }
235
+ }
236
+
237
+ /**
238
+ * Get array of raw values from sheet.
239
+ */
240
+ getCellValues(range: CellRange): CellScalarValue[][] {
241
+ return this._iterRange(range, (cell) => this.getCellValue(cell));
242
+ }
243
+
244
+ /**
245
+ * Gets the regular or computed value from the engine.
246
+ */
247
+ getValue(cell: CellAddress): CellScalarValue {
248
+ // Applies rounding and post-processing.
249
+ const value = this._node.hf.getCellValue(toSimpleCellAddress(this._node.sheetId, cell));
250
+ if (value instanceof DetailedCellError) {
251
+ return value.toString();
252
+ }
253
+
254
+ return value;
255
+ }
256
+
257
+ /**
258
+ * Get value type.
259
+ */
260
+ getValueType(cell: CellAddress): ValueTypeEnum {
261
+ const addr = toSimpleCellAddress(this._node.sheetId, cell);
262
+ const type = this._node.hf.getCellValueDetailedType(addr);
263
+ return typeMap[type];
264
+ }
265
+
266
+ /**
267
+ * Sets the value, updating the sheet and engine.
268
+ */
269
+ setValue(cell: CellAddress, value: CellScalarValue) {
270
+ if (this._options.readonly) {
271
+ throw new ReadonlyException();
272
+ }
273
+
274
+ // Reallocate if > current bounds.
275
+ let refresh = false;
276
+ if (cell.row >= this._sheet.rows.length) {
277
+ insertIndices(this._sheet.rows, cell.row, 1, MAX_ROWS);
278
+ refresh = true;
279
+ }
280
+ if (cell.col >= this._sheet.columns.length) {
281
+ insertIndices(this._sheet.columns, cell.col, 1, MAX_COLUMNS);
282
+ refresh = true;
283
+ }
284
+
285
+ if (refresh) {
286
+ // TODO(burdon): Remove.
287
+ this.reset();
288
+ }
289
+
290
+ // Insert into engine.
291
+ this._node.hf.setCellContents({ sheet: this._node.sheetId, row: cell.row, col: cell.col }, [
292
+ [typeof value === 'string' && value.charAt(0) === '=' ? this._graph.mapFormulaToNative(value) : value],
293
+ ]);
294
+
295
+ // Insert into sheet.
296
+ const idx = addressToIndex(this._sheet, cell);
297
+ if (value === undefined || value === null) {
298
+ delete this._sheet.cells[idx];
299
+ } else {
300
+ if (typeof value === 'string' && value.charAt(0) === '=') {
301
+ value = this._graph.mapFunctionBindingToId(this.mapFormulaRefsToIndices(value));
302
+ }
303
+
304
+ this._sheet.cells[idx] = { value };
305
+ }
306
+ }
307
+
308
+ /**
309
+ * Sets values from a simple map.
310
+ */
311
+ setValues(values: Record<string, CellValue>) {
312
+ Object.entries(values).forEach(([key, { value }]) => {
313
+ this.setValue(addressFromA1Notation(key), value);
314
+ });
315
+ }
316
+
317
+ /**
318
+ * Iterate range.
319
+ */
320
+ private _iterRange(range: CellRange, cb: (cell: CellAddress) => CellScalarValue | void): CellScalarValue[][] {
321
+ const to = range.to ?? range.from;
322
+ const rowRange = [Math.min(range.from.row, to.row), Math.max(range.from.row, to.row)];
323
+ const columnRange = [Math.min(range.from.col, to.col), Math.max(range.from.col, to.col)];
324
+ const rows: CellScalarValue[][] = [];
325
+ for (let row = rowRange[0]; row <= rowRange[1]; row++) {
326
+ const rowCells: CellScalarValue[] = [];
327
+ for (let column = columnRange[0]; column <= columnRange[1]; column++) {
328
+ const value = cb({ row, col: column });
329
+ if (value !== undefined) {
330
+ rowCells.push(value);
331
+ }
332
+ }
333
+
334
+ rows.push(rowCells);
335
+ }
336
+
337
+ return rows;
338
+ }
339
+
340
+ // TODO(burdon): Delete index.
341
+ private _deleteIndices(indices: string[], i: number, n: number) {
342
+ throw new Error('Not implemented');
343
+ }
344
+
345
+ // TODO(burdon): Move. Cannot use fractional without changing. Switch back to using unique IDs?
346
+ private _moveIndices(indices: string[], i: number, j: number, n: number) {
347
+ throw new Error('Not implemented');
348
+ }
349
+
350
+ //
351
+ // Indices.
352
+ //
353
+
354
+ /**
355
+ * Map from A1 notation to indices.
356
+ */
357
+ mapFormulaRefsToIndices(formula: string): string {
358
+ invariant(formula.charAt(0) === '=');
359
+ return formula.replace(/([a-zA-Z]+)([0-9]+)/g, (match) => {
360
+ return addressToIndex(this._sheet, addressFromA1Notation(match));
361
+ });
362
+ }
363
+
364
+ /**
365
+ * Map from indices to A1 notation.
366
+ */
367
+ mapFormulaIndicesToRefs(formula: string): string {
368
+ invariant(formula.charAt(0) === '=');
369
+ return formula.replace(/([a-zA-Z0-9]+)@([a-zA-Z0-9]+)/g, (idx) => {
370
+ return addressToA1Notation(addressFromIndex(this._sheet, idx));
371
+ });
372
+ }
373
+
374
+ //
375
+ // Values
376
+ //
377
+
378
+ /**
379
+ * https://hyperformula.handsontable.com/guide/date-and-time-handling.html#example
380
+ * https://hyperformula.handsontable.com/api/interfaces/configparams.html#nulldate
381
+ * NOTE: TODAY() is number of FULL days since nullDate. It will typically be -1 days from NOW().
382
+ */
383
+ toLocalDate(num: number): Date {
384
+ const { year, month, day, hours, minutes, seconds } = this.toDateTime(num);
385
+ return new Date(year, month - 1, day, hours, minutes, seconds);
386
+ }
387
+
388
+ toDateTime(num: number): SimpleDateTime {
389
+ return this._node.hf.numberToDateTime(num) as SimpleDateTime;
390
+ }
391
+
392
+ toDate(num: number): SimpleDate {
393
+ return this._node.hf.numberToDate(num) as SimpleDate;
394
+ }
395
+
396
+ toTime(num: number): SimpleDate {
397
+ return this._node.hf.numberToTime(num) as SimpleDate;
398
+ }
399
+ }
@@ -0,0 +1,40 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { describe, test, expect } from 'vitest';
6
+
7
+ // Part 2.
8
+ // TODO(burdon): Cannot test outside of browser.
9
+ // - Cannot test Hyperformula
10
+ // - throws "Cannot convert undefined or null to object" in vitest (no browser).
11
+ // - throws "process.nextTick is not a function" (if browser)
12
+
13
+ import { Client } from '@dxos/client';
14
+ import { create } from '@dxos/client/echo';
15
+ import { FunctionType } from '@dxos/plugin-script/types';
16
+
17
+ // TODO(burdon): Fix test infrastructure:
18
+ // - Need docs? esp. needed for config. need pristine example package?
19
+ // - NOTE for non browser tests, import types from x-plugin/types (otherwise will bring in react deps).
20
+ // - Can't add flags to our tools?
21
+ // - .only / .skip ignored (have to comment out tests)
22
+
23
+ describe('test', () => {
24
+ test('test', async () => {
25
+ const client = new Client();
26
+ client.addTypes([FunctionType]);
27
+ await client.initialize();
28
+ await client.halo.createIdentity();
29
+
30
+ // Part 1.
31
+ // Create script.
32
+ // VITEST_ENV=chromium p test
33
+ // TODO(burdon): Test after initialize.
34
+ // - ERROR "process.nextTick is not a function"
35
+ // - ERROR "Identifier 'Buffer' has already been declared" if { nodeExternal: true }
36
+ const space = await client.spaces.create();
37
+ const fn = space.db.add(create(FunctionType, { version: 1, binding: 'HELLO' }));
38
+ expect(fn).to.exist;
39
+ });
40
+ });
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ export * from './testing';
@@ -0,0 +1,66 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import type { Decorator } from '@storybook/react';
6
+ import React, { useState } from 'react';
7
+
8
+ import { type Space } from '@dxos/react-client/echo';
9
+ import { useAsyncState } from '@dxos/react-hooks';
10
+
11
+ import { ComputeGraphContextProvider } from '../components';
12
+ import { createSheet } from '../defs';
13
+ import { type ComputeGraph, ComputeGraphRegistry } from '../graph';
14
+ import { type CellValue, type CreateSheetOptions } from '../types';
15
+
16
+ export const testSheetName = 'test';
17
+
18
+ // TODO(thure): Remove this from the `/testing` entrypoint.
19
+ export const createCells = (): Record<string, CellValue> => ({
20
+ B1: { value: 'Qty' },
21
+ B3: { value: 1 },
22
+ B4: { value: 2 },
23
+ B5: { value: 3 },
24
+ B7: { value: 'Total' },
25
+
26
+ C1: { value: 'Price' },
27
+ C3: { value: 2_000 },
28
+ C4: { value: 2_500 },
29
+ C5: { value: 3_000 },
30
+ C7: { value: '=SUMPRODUCT(B2:B6, C2:C6)' },
31
+ // C8: { value: '=C7*CRYPTO(D7)' },
32
+ C8: { value: '=C7*TEST()' },
33
+
34
+ D7: { value: 'USD' },
35
+ D8: { value: 'BTC' },
36
+
37
+ E3: { value: '=TODAY()' },
38
+ E4: { value: '=NOW()' },
39
+
40
+ F1: { value: `=${testSheetName}!A1` }, // Ref test sheet.
41
+ F3: { value: true },
42
+ F4: { value: false },
43
+ F5: { value: '8%' },
44
+ F6: { value: '$10000' },
45
+ });
46
+
47
+ export const useTestSheet = (space?: Space, graph?: ComputeGraph, options?: CreateSheetOptions) => {
48
+ return useAsyncState(async () => {
49
+ if (!space || !graph) {
50
+ return;
51
+ }
52
+
53
+ const sheet = createSheet(options);
54
+ space.db.add(sheet);
55
+ return sheet;
56
+ }, [space, graph]);
57
+ };
58
+
59
+ export const withGraphDecorator: Decorator = (Story) => {
60
+ const [registry] = useState(new ComputeGraphRegistry());
61
+ return (
62
+ <ComputeGraphContextProvider registry={registry}>
63
+ <Story />
64
+ </ComputeGraphContextProvider>
65
+ );
66
+ };