@dxos/plugin-sheet 0.6.12-main.ed7cda7 → 0.6.12-staging.0b4bb48

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/{chunk-U2JHW3L6.mjs → SheetContainer-U4H5D34A.mjs} +240 -1020
  2. package/dist/lib/browser/SheetContainer-U4H5D34A.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-APHOLYUB.mjs +175 -0
  4. package/dist/lib/browser/chunk-APHOLYUB.mjs.map +7 -0
  5. package/dist/lib/{node-esm/chunk-D6KU5MI7.mjs → browser/chunk-D5AGLXJP.mjs} +670 -360
  6. package/dist/lib/browser/chunk-D5AGLXJP.mjs.map +7 -0
  7. package/dist/lib/browser/{chunk-T3NJFTD4.mjs → chunk-FUAGSXA4.mjs} +15 -8
  8. package/dist/lib/browser/chunk-FUAGSXA4.mjs.map +7 -0
  9. package/dist/lib/browser/{chunk-QILRZNE5.mjs → chunk-JRL5LGCE.mjs} +5 -2
  10. package/dist/lib/browser/chunk-JRL5LGCE.mjs.map +7 -0
  11. package/dist/lib/browser/chunk-NU4PBN33.mjs +8 -0
  12. package/dist/lib/browser/chunk-NU4PBN33.mjs.map +7 -0
  13. package/dist/lib/browser/index.mjs +56 -68
  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/testing.mjs +92 -0
  18. package/dist/lib/browser/testing.mjs.map +7 -0
  19. package/dist/lib/browser/types.mjs +6 -4
  20. package/dist/lib/node/{chunk-OTTD7FBK.cjs → SheetContainer-AXQV3ZT5.cjs} +279 -1050
  21. package/dist/lib/node/SheetContainer-AXQV3ZT5.cjs.map +7 -0
  22. package/dist/lib/node/{chunk-DD6FIXWC.cjs → chunk-5KKJ4NPP.cjs} +667 -360
  23. package/dist/lib/node/chunk-5KKJ4NPP.cjs.map +7 -0
  24. package/dist/lib/node/{chunk-BNARJ5GM.cjs → chunk-BJ6ZD7MN.cjs} +18 -5
  25. package/dist/lib/node/chunk-BJ6ZD7MN.cjs.map +7 -0
  26. package/dist/lib/node/chunk-CN3RPESU.cjs +202 -0
  27. package/dist/lib/node/chunk-CN3RPESU.cjs.map +7 -0
  28. package/dist/lib/node/{chunk-Q3HBHPRL.cjs → chunk-DSYKOI4E.cjs} +20 -12
  29. package/dist/lib/node/chunk-DSYKOI4E.cjs.map +7 -0
  30. package/dist/lib/node/chunk-PYXHNAAK.cjs +40 -0
  31. package/dist/lib/node/chunk-PYXHNAAK.cjs.map +7 -0
  32. package/dist/lib/node/index.cjs +64 -83
  33. package/dist/lib/node/index.cjs.map +3 -3
  34. package/dist/lib/node/meta.cjs +3 -3
  35. package/dist/lib/node/meta.cjs.map +1 -1
  36. package/dist/lib/node/meta.json +1 -1
  37. package/dist/lib/node/testing.cjs +111 -0
  38. package/dist/lib/node/testing.cjs.map +7 -0
  39. package/dist/lib/node/types.cjs +12 -10
  40. package/dist/lib/node/types.cjs.map +2 -2
  41. package/dist/types/src/SheetPlugin.d.ts.map +1 -1
  42. package/dist/types/src/components/CellEditor/CellEditor.d.ts +3 -23
  43. package/dist/types/src/components/CellEditor/CellEditor.d.ts.map +1 -1
  44. package/dist/types/src/components/CellEditor/CellEditor.stories.d.ts +2 -2
  45. package/dist/types/src/components/CellEditor/CellEditor.stories.d.ts.map +1 -1
  46. package/dist/types/src/components/CellEditor/extension.d.ts +1 -1
  47. package/dist/types/src/components/CellEditor/extension.d.ts.map +1 -1
  48. package/dist/types/src/{graph → components/ComputeGraph}/async-function.d.ts +1 -1
  49. package/dist/types/src/components/ComputeGraph/async-function.d.ts.map +1 -0
  50. package/dist/types/src/{graph/custom-function.d.ts → components/ComputeGraph/custom.d.ts} +1 -1
  51. package/dist/types/src/components/ComputeGraph/custom.d.ts.map +1 -0
  52. package/dist/types/src/components/ComputeGraph/edge-function.d.ts.map +1 -0
  53. package/dist/types/src/components/ComputeGraph/graph-context.d.ts +12 -0
  54. package/dist/types/src/components/ComputeGraph/graph-context.d.ts.map +1 -0
  55. package/dist/types/src/components/ComputeGraph/graph.browser.test.d.ts +2 -0
  56. package/dist/types/src/components/ComputeGraph/graph.browser.test.d.ts.map +1 -0
  57. package/dist/types/src/components/ComputeGraph/graph.d.ts +26 -0
  58. package/dist/types/src/components/ComputeGraph/graph.d.ts.map +1 -0
  59. package/dist/types/src/components/ComputeGraph/index.d.ts +3 -1
  60. package/dist/types/src/components/ComputeGraph/index.d.ts.map +1 -1
  61. package/dist/types/src/components/Sheet/Sheet.d.ts +1 -1
  62. package/dist/types/src/components/Sheet/Sheet.d.ts.map +1 -1
  63. package/dist/types/src/components/Sheet/Sheet.stories.d.ts +6 -5
  64. package/dist/types/src/components/Sheet/Sheet.stories.d.ts.map +1 -1
  65. package/dist/types/src/components/Sheet/formatting.d.ts +14 -0
  66. package/dist/types/src/components/Sheet/formatting.d.ts.map +1 -0
  67. package/dist/types/src/components/Sheet/grid.d.ts +2 -2
  68. package/dist/types/src/components/Sheet/grid.d.ts.map +1 -1
  69. package/dist/types/src/components/Sheet/nav.d.ts +3 -3
  70. package/dist/types/src/components/Sheet/nav.d.ts.map +1 -1
  71. package/dist/types/src/components/Sheet/sheet-context.d.ts +5 -6
  72. package/dist/types/src/components/Sheet/sheet-context.d.ts.map +1 -1
  73. package/dist/types/src/components/SheetContainer.d.ts +3 -2
  74. package/dist/types/src/components/SheetContainer.d.ts.map +1 -1
  75. package/dist/types/src/components/Toolbar/Toolbar.d.ts +3 -19
  76. package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -1
  77. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +13 -18
  78. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -1
  79. package/dist/types/src/components/index.d.ts +2 -2
  80. package/dist/types/src/components/index.d.ts.map +1 -1
  81. package/dist/types/src/meta.d.ts +4 -1
  82. package/dist/types/src/meta.d.ts.map +1 -1
  83. package/dist/types/src/{graph/function-defs.d.ts → model/functions.d.ts} +1 -1
  84. package/dist/types/src/model/functions.d.ts.map +1 -0
  85. package/dist/types/src/model/index.d.ts +3 -2
  86. package/dist/types/src/model/index.d.ts.map +1 -1
  87. package/dist/types/src/model/model.browser.test.d.ts +2 -0
  88. package/dist/types/src/model/model.browser.test.d.ts.map +1 -0
  89. package/dist/types/src/model/{sheet-model.d.ts → model.d.ts} +64 -9
  90. package/dist/types/src/model/model.d.ts.map +1 -0
  91. package/dist/types/src/{defs → model}/types.d.ts +3 -8
  92. package/dist/types/src/model/types.d.ts.map +1 -0
  93. package/dist/types/src/model/types.test.d.ts.map +1 -0
  94. package/dist/types/src/model/util.d.ts +15 -0
  95. package/dist/types/src/model/util.d.ts.map +1 -0
  96. package/dist/types/src/testing.d.ts +9 -0
  97. package/dist/types/src/testing.d.ts.map +1 -0
  98. package/dist/types/src/translations.d.ts +12 -17
  99. package/dist/types/src/translations.d.ts.map +1 -1
  100. package/dist/types/src/types.d.ts +3 -83
  101. package/dist/types/src/types.d.ts.map +1 -1
  102. package/package.json +41 -48
  103. package/src/SheetPlugin.tsx +70 -43
  104. package/src/components/CellEditor/CellEditor.stories.tsx +3 -4
  105. package/src/components/CellEditor/CellEditor.tsx +9 -59
  106. package/src/components/CellEditor/extension.test.ts +5 -4
  107. package/src/components/CellEditor/extension.ts +3 -1
  108. package/src/{graph → components/ComputeGraph}/async-function.ts +1 -3
  109. package/src/{graph/custom-function.ts → components/ComputeGraph/custom.ts} +6 -2
  110. package/src/{graph → components/ComputeGraph}/edge-function.ts +1 -2
  111. package/src/components/ComputeGraph/graph-context.tsx +50 -0
  112. package/src/components/ComputeGraph/graph.browser.test.ts +50 -0
  113. package/src/components/ComputeGraph/graph.ts +62 -0
  114. package/src/components/ComputeGraph/index.ts +3 -1
  115. package/src/components/Sheet/Sheet.stories.tsx +82 -45
  116. package/src/components/Sheet/Sheet.tsx +18 -57
  117. package/src/{model/formatting-model.ts → components/Sheet/formatting.ts} +13 -20
  118. package/src/components/Sheet/grid.ts +3 -3
  119. package/src/components/Sheet/nav.ts +19 -19
  120. package/src/components/Sheet/sheet-context.tsx +78 -16
  121. package/src/components/SheetContainer.tsx +19 -73
  122. package/src/components/Toolbar/Toolbar.tsx +12 -53
  123. package/src/components/index.ts +0 -1
  124. package/src/meta.tsx +5 -1
  125. package/src/model/index.ts +3 -2
  126. package/src/model/model.browser.test.ts +100 -0
  127. package/src/model/model.ts +550 -0
  128. package/src/{defs → model}/types.test.ts +9 -8
  129. package/src/{defs → model}/types.ts +14 -23
  130. package/src/model/util.ts +36 -0
  131. package/src/testing.ts +50 -0
  132. package/src/translations.ts +1 -6
  133. package/src/types.ts +5 -30
  134. package/dist/lib/browser/SheetContainer-V4GCCZTX.mjs +0 -261
  135. package/dist/lib/browser/SheetContainer-V4GCCZTX.mjs.map +0 -7
  136. package/dist/lib/browser/chunk-6ZMQVB4Z.mjs +0 -2923
  137. package/dist/lib/browser/chunk-6ZMQVB4Z.mjs.map +0 -7
  138. package/dist/lib/browser/chunk-QILRZNE5.mjs.map +0 -7
  139. package/dist/lib/browser/chunk-T3NJFTD4.mjs.map +0 -7
  140. package/dist/lib/browser/chunk-U2JHW3L6.mjs.map +0 -7
  141. package/dist/lib/browser/graph-T27BOBOV.mjs +0 -21
  142. package/dist/lib/browser/graph-T27BOBOV.mjs.map +0 -7
  143. package/dist/lib/node/SheetContainer-3ZY7MPWJ.cjs +0 -279
  144. package/dist/lib/node/SheetContainer-3ZY7MPWJ.cjs.map +0 -7
  145. package/dist/lib/node/chunk-BNARJ5GM.cjs.map +0 -7
  146. package/dist/lib/node/chunk-DD6FIXWC.cjs.map +0 -7
  147. package/dist/lib/node/chunk-OTTD7FBK.cjs.map +0 -7
  148. package/dist/lib/node/chunk-Q3HBHPRL.cjs.map +0 -7
  149. package/dist/lib/node/graph-SPKGX7W4.cjs +0 -43
  150. package/dist/lib/node/graph-SPKGX7W4.cjs.map +0 -7
  151. package/dist/lib/node-esm/SheetContainer-PXSJX6XK.mjs +0 -262
  152. package/dist/lib/node-esm/SheetContainer-PXSJX6XK.mjs.map +0 -7
  153. package/dist/lib/node-esm/chunk-7HVSOTGA.mjs +0 -2553
  154. package/dist/lib/node-esm/chunk-7HVSOTGA.mjs.map +0 -7
  155. package/dist/lib/node-esm/chunk-BMNA27EX.mjs +0 -76
  156. package/dist/lib/node-esm/chunk-BMNA27EX.mjs.map +0 -7
  157. package/dist/lib/node-esm/chunk-D6KU5MI7.mjs.map +0 -7
  158. package/dist/lib/node-esm/chunk-IU2L277A.mjs +0 -17
  159. package/dist/lib/node-esm/chunk-IU2L277A.mjs.map +0 -7
  160. package/dist/lib/node-esm/graph-U67IO4UC.mjs +0 -22
  161. package/dist/lib/node-esm/graph-U67IO4UC.mjs.map +0 -7
  162. package/dist/lib/node-esm/index.mjs +0 -261
  163. package/dist/lib/node-esm/index.mjs.map +0 -7
  164. package/dist/lib/node-esm/meta.json +0 -1
  165. package/dist/lib/node-esm/meta.mjs +0 -10
  166. package/dist/lib/node-esm/meta.mjs.map +0 -7
  167. package/dist/lib/node-esm/types.mjs +0 -21
  168. package/dist/lib/node-esm/types.mjs.map +0 -7
  169. package/dist/types/src/components/ComputeGraph/ComputeGraphContextProvider.d.ts +0 -11
  170. package/dist/types/src/components/ComputeGraph/ComputeGraphContextProvider.d.ts.map +0 -1
  171. package/dist/types/src/components/GridSheet/GridSheet.d.ts +0 -10
  172. package/dist/types/src/components/GridSheet/GridSheet.d.ts.map +0 -1
  173. package/dist/types/src/components/GridSheet/GridSheet.stories.d.ts +0 -9
  174. package/dist/types/src/components/GridSheet/GridSheet.stories.d.ts.map +0 -1
  175. package/dist/types/src/components/GridSheet/util.d.ts +0 -7
  176. package/dist/types/src/components/GridSheet/util.d.ts.map +0 -1
  177. package/dist/types/src/components/Sheet/decorations.d.ts +0 -24
  178. package/dist/types/src/components/Sheet/decorations.d.ts.map +0 -1
  179. package/dist/types/src/components/Sheet/threads.d.ts +0 -2
  180. package/dist/types/src/components/Sheet/threads.d.ts.map +0 -1
  181. package/dist/types/src/defs/index.d.ts +0 -3
  182. package/dist/types/src/defs/index.d.ts.map +0 -1
  183. package/dist/types/src/defs/types.d.ts.map +0 -1
  184. package/dist/types/src/defs/types.test.d.ts.map +0 -1
  185. package/dist/types/src/defs/util.d.ts +0 -43
  186. package/dist/types/src/defs/util.d.ts.map +0 -1
  187. package/dist/types/src/extensions/compute.d.ts +0 -5
  188. package/dist/types/src/extensions/compute.d.ts.map +0 -1
  189. package/dist/types/src/extensions/compute.stories.d.ts +0 -26
  190. package/dist/types/src/extensions/compute.stories.d.ts.map +0 -1
  191. package/dist/types/src/extensions/index.d.ts +0 -2
  192. package/dist/types/src/extensions/index.d.ts.map +0 -1
  193. package/dist/types/src/graph/async-function.d.ts.map +0 -1
  194. package/dist/types/src/graph/compute-graph.browser.test.d.ts +0 -2
  195. package/dist/types/src/graph/compute-graph.browser.test.d.ts.map +0 -1
  196. package/dist/types/src/graph/compute-graph.d.ts +0 -81
  197. package/dist/types/src/graph/compute-graph.d.ts.map +0 -1
  198. package/dist/types/src/graph/compute-graph.stories.d.ts +0 -10
  199. package/dist/types/src/graph/compute-graph.stories.d.ts.map +0 -1
  200. package/dist/types/src/graph/compute-node.d.ts +0 -19
  201. package/dist/types/src/graph/compute-node.d.ts.map +0 -1
  202. package/dist/types/src/graph/custom-function.d.ts.map +0 -1
  203. package/dist/types/src/graph/edge-function.d.ts.map +0 -1
  204. package/dist/types/src/graph/function-defs.d.ts.map +0 -1
  205. package/dist/types/src/graph/hyperformula.test.d.ts +0 -2
  206. package/dist/types/src/graph/hyperformula.test.d.ts.map +0 -1
  207. package/dist/types/src/graph/index.d.ts +0 -4
  208. package/dist/types/src/graph/index.d.ts.map +0 -1
  209. package/dist/types/src/graph/util.d.ts +0 -2
  210. package/dist/types/src/graph/util.d.ts.map +0 -1
  211. package/dist/types/src/hooks/hooks.stories.d.ts +0 -11
  212. package/dist/types/src/hooks/hooks.stories.d.ts.map +0 -1
  213. package/dist/types/src/hooks/index.d.ts +0 -4
  214. package/dist/types/src/hooks/index.d.ts.map +0 -1
  215. package/dist/types/src/hooks/useComputeGraph.d.ts +0 -7
  216. package/dist/types/src/hooks/useComputeGraph.d.ts.map +0 -1
  217. package/dist/types/src/hooks/useFormattingModel.d.ts +0 -3
  218. package/dist/types/src/hooks/useFormattingModel.d.ts.map +0 -1
  219. package/dist/types/src/hooks/useSheetModel.d.ts +0 -8
  220. package/dist/types/src/hooks/useSheetModel.d.ts.map +0 -1
  221. package/dist/types/src/model/formatting-model.d.ts +0 -16
  222. package/dist/types/src/model/formatting-model.d.ts.map +0 -1
  223. package/dist/types/src/model/sheet-model.d.ts.map +0 -1
  224. package/dist/types/src/sanity.test.d.ts +0 -2
  225. package/dist/types/src/sanity.test.d.ts.map +0 -1
  226. package/dist/types/src/testing/index.d.ts +0 -2
  227. package/dist/types/src/testing/index.d.ts.map +0 -1
  228. package/dist/types/src/testing/testing.d.ts +0 -9
  229. package/dist/types/src/testing/testing.d.ts.map +0 -1
  230. package/dist/vendor/hyperformula.mjs +0 -37145
  231. package/src/components/ComputeGraph/ComputeGraphContextProvider.tsx +0 -20
  232. package/src/components/GridSheet/GridSheet.stories.tsx +0 -35
  233. package/src/components/GridSheet/GridSheet.tsx +0 -153
  234. package/src/components/GridSheet/util.ts +0 -108
  235. package/src/components/Sheet/decorations.ts +0 -62
  236. package/src/components/Sheet/threads.tsx +0 -205
  237. package/src/defs/index.ts +0 -6
  238. package/src/defs/util.ts +0 -135
  239. package/src/extensions/compute.stories.tsx +0 -151
  240. package/src/extensions/compute.ts +0 -98
  241. package/src/extensions/index.ts +0 -5
  242. package/src/graph/compute-graph.browser.test.ts +0 -104
  243. package/src/graph/compute-graph.stories.tsx +0 -92
  244. package/src/graph/compute-graph.ts +0 -290
  245. package/src/graph/compute-node.ts +0 -51
  246. package/src/graph/hyperformula.test.ts +0 -15
  247. package/src/graph/index.ts +0 -7
  248. package/src/graph/util.ts +0 -8
  249. package/src/hooks/hooks.stories.tsx +0 -50
  250. package/src/hooks/index.ts +0 -7
  251. package/src/hooks/useComputeGraph.ts +0 -20
  252. package/src/hooks/useFormattingModel.ts +0 -11
  253. package/src/hooks/useSheetModel.ts +0 -43
  254. package/src/model/sheet-model.ts +0 -399
  255. package/src/sanity.test.ts +0 -40
  256. package/src/testing/index.ts +0 -5
  257. package/src/testing/testing.tsx +0 -66
  258. /package/dist/types/src/{graph → components/ComputeGraph}/edge-function.d.ts +0 -0
  259. /package/dist/types/src/{defs → model}/types.test.d.ts +0 -0
  260. /package/src/{graph/function-defs.ts → model/functions.ts} +0 -0
package/src/defs/util.ts DELETED
@@ -1,135 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { randomBytes } from '@dxos/crypto';
6
- import { create } from '@dxos/echo-schema';
7
-
8
- import { type CellAddress, type CellRange, DEFAULT_COLUMNS, DEFAULT_ROWS, MAX_COLUMNS, MAX_ROWS } from './types';
9
- import { type CreateSheetOptions, type SheetSize, SheetType } from '../types';
10
-
11
- // TODO(burdon): Factor out from dxos/protocols to new common package.
12
- export class ApiError extends Error {}
13
-
14
- export class ReadonlyException extends ApiError {}
15
-
16
- export class RangeException extends ApiError {
17
- constructor(n: number) {
18
- super();
19
- }
20
- }
21
-
22
- /**
23
- * With a string length of 8, the chance of a collision is 0.02% for a sheet with 10,000 strings.
24
- */
25
- export const createIndex = (length = 8): string => {
26
- const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
27
- const charactersLength = characters.length;
28
- const randomBuffer = randomBytes(length);
29
- return Array.from(randomBuffer, (byte) => characters[byte % charactersLength]).join('');
30
- };
31
-
32
- export const createIndices = (length: number): string[] => Array.from({ length }).map(() => createIndex());
33
-
34
- export const insertIndices = (indices: string[], i: number, n: number, max: number) => {
35
- if (i + n > max) {
36
- throw new RangeException(i + n);
37
- }
38
-
39
- const idx = createIndices(n);
40
- indices.splice(i, 0, ...idx);
41
- };
42
-
43
- export const initialize = (
44
- sheet: SheetType,
45
- { rows = DEFAULT_ROWS, columns = DEFAULT_COLUMNS }: Partial<SheetSize> = {},
46
- ) => {
47
- if (!sheet.rows.length) {
48
- insertIndices(sheet.rows, 0, rows, MAX_ROWS);
49
- }
50
- if (!sheet.columns.length) {
51
- insertIndices(sheet.columns, 0, columns, MAX_COLUMNS);
52
- }
53
- };
54
-
55
- export const createSheet = ({ title, ...size }: CreateSheetOptions = {}): SheetType => {
56
- const sheet = create(SheetType, {
57
- title,
58
- cells: {},
59
- rows: [],
60
- columns: [],
61
- rowMeta: {},
62
- columnMeta: {},
63
- formatting: {},
64
- });
65
-
66
- initialize(sheet, size);
67
- return sheet;
68
- };
69
-
70
- /**
71
- * E.g., "A1" => "CA2@CB3".
72
- */
73
- export const addressToIndex = (sheet: SheetType, cell: CellAddress): string => {
74
- return `${sheet.columns[cell.col]}@${sheet.rows[cell.row]}`;
75
- };
76
-
77
- /**
78
- * E.g., "CA2@CB3" => "A1".
79
- */
80
- export const addressFromIndex = (sheet: SheetType, idx: string): CellAddress => {
81
- const [column, row] = idx.split('@');
82
- return {
83
- col: sheet.columns.indexOf(column),
84
- row: sheet.rows.indexOf(row),
85
- };
86
- };
87
-
88
- /**
89
- * E.g., "A1:B2" => "CA2@CB3:CC4@CD5".
90
- */
91
- export const rangeToIndex = (sheet: SheetType, range: CellRange): string => {
92
- return [range.from, range.to ?? range.from].map((cell) => addressToIndex(sheet, cell)).join(':');
93
- };
94
-
95
- /**
96
- * E.g., "CA2@CB3:CC4@CD5" => "A1:B2".
97
- */
98
- export const rangeFromIndex = (sheet: SheetType, idx: string): CellRange => {
99
- const [from, to] = idx.split(':').map((index) => addressFromIndex(sheet, index));
100
- return { from, to };
101
- };
102
-
103
- /**
104
- * Find closest cell to cursor.
105
- */
106
- export const closest = (cursor: CellAddress, cells: CellAddress[]): CellAddress | undefined => {
107
- let closestCell: CellAddress | undefined;
108
- let closestDistance = Number.MAX_SAFE_INTEGER;
109
-
110
- for (const cell of cells) {
111
- const distance = Math.abs(cell.row - cursor.row) + Math.abs(cell.col - cursor.col);
112
- if (distance < closestDistance) {
113
- closestCell = cell;
114
- closestDistance = distance;
115
- }
116
- }
117
-
118
- return closestCell;
119
- };
120
-
121
- /**
122
- * Compares the positions of two cell indexes in a sheet.
123
- * Sorts primarily by row, then by column if rows are equal.
124
- */
125
- export const compareIndexPositions = (sheet: SheetType, indexA: string, indexB: string): number => {
126
- const { row: rowA, col: columnA } = addressFromIndex(sheet, indexA);
127
- const { row: rowB, col: columnB } = addressFromIndex(sheet, indexB);
128
-
129
- // Sort by row first, then by column.
130
- if (rowA !== rowB) {
131
- return rowA - rowB;
132
- } else {
133
- return columnA - columnB;
134
- }
135
- };
@@ -1,151 +0,0 @@
1
- //
2
- // Copyright 2023 DXOS.org
3
- //
4
-
5
- import '@dxos-theme';
6
- import React, { useEffect, useState } from 'react';
7
-
8
- import { useSpace } from '@dxos/react-client/echo';
9
- import { withClientProvider } from '@dxos/react-client/testing';
10
- import { useThemeContext } from '@dxos/react-ui';
11
- import {
12
- createBasicExtensions,
13
- createMarkdownExtensions,
14
- createThemeExtensions,
15
- decorateMarkdown,
16
- useTextEditor,
17
- } from '@dxos/react-ui-editor';
18
- import { withTheme, withLayout } from '@dxos/storybook-utils';
19
- import { nonNullable } from '@dxos/util';
20
-
21
- import { compute } from './compute';
22
- import { Sheet } from '../components';
23
- import { type ComputeNode } from '../graph';
24
- import { useComputeGraph, useSheetModel } from '../hooks';
25
- import { useTestSheet, withGraphDecorator } from '../testing';
26
- import { SheetType } from '../types';
27
-
28
- const str = (...lines: string[]) => lines.join('\n');
29
-
30
- type EditorProps = {
31
- text?: string;
32
- };
33
-
34
- // TODO(burdon): Implement named expressions.
35
- // https://hyperformula.handsontable.com/guide/cell-references.html
36
-
37
- const DOC_NAME = 'Test Doc';
38
- const SHEET_NAME = 'Test Sheet';
39
-
40
- const Editor = ({ text }: EditorProps) => {
41
- const { themeMode } = useThemeContext();
42
- const space = useSpace();
43
- const graph = useComputeGraph(space);
44
- const [node, setNode] = useState<ComputeNode>();
45
- // TODO(burdon): Virtualize SheetModel.
46
- useEffect(() => {
47
- if (graph) {
48
- setNode(graph.getOrCreateNode(DOC_NAME));
49
- }
50
- }, [graph]);
51
- const { parentRef, focusAttributes } = useTextEditor(
52
- () => ({
53
- initialValue: text,
54
- extensions: [
55
- createBasicExtensions(),
56
- createMarkdownExtensions({ themeMode }),
57
- createThemeExtensions({ themeMode, syntaxHighlighting: true }),
58
- node && compute(node),
59
- decorateMarkdown(),
60
- ].filter(nonNullable),
61
- }),
62
- [node, themeMode],
63
- );
64
-
65
- return <div className='w-[40rem] overflow-hidden' ref={parentRef} {...focusAttributes} />;
66
- };
67
-
68
- const Grid = () => {
69
- const space = useSpace();
70
- const graph = useComputeGraph(space);
71
- const sheet = useTestSheet(space, graph, { title: SHEET_NAME });
72
- const model = useSheetModel(space, sheet);
73
- useEffect(() => {
74
- if (model) {
75
- model.setValues({ A1: { value: 100 }, A2: { value: 200 }, A3: { value: 300 }, A5: { value: '=SUM(A1:A3)' } });
76
- }
77
- }, [model]);
78
-
79
- if (!space || !sheet) {
80
- return null;
81
- }
82
-
83
- return (
84
- <div className='flex w-[40rem] overflow-hidden'>
85
- <Sheet.Root space={space} sheet={sheet}>
86
- <Sheet.Main classNames='border border-separator' />
87
- </Sheet.Root>
88
- </div>
89
- );
90
- };
91
-
92
- const Story = (props: EditorProps) => {
93
- return (
94
- <div className='grid grid-rows-2'>
95
- <Editor {...props} />
96
- <Grid />
97
- </div>
98
- );
99
- };
100
-
101
- export default {
102
- title: 'plugin-sheet/extensions',
103
- decorators: [
104
- withClientProvider({ types: [SheetType], createIdentity: true, createSpace: true }),
105
- withGraphDecorator,
106
- withTheme,
107
- withLayout({ fullscreen: true, classNames: 'justify-center' }),
108
- ],
109
- parameters: { layout: 'fullscreen' },
110
- };
111
-
112
- export const Default = {
113
- render: Editor,
114
- args: {
115
- text: str(
116
- //
117
- '# Compute Graph',
118
- '',
119
- 'This is a compute expression:',
120
- '',
121
- '```dx',
122
- '=SUM(1, 2)',
123
- '```',
124
- '',
125
- 'It should change in realtime.',
126
- '',
127
- '```dx',
128
- '=SUM(3, 5)',
129
- '```',
130
- '',
131
- '',
132
- ),
133
- },
134
- };
135
-
136
- export const Graph = {
137
- render: Story,
138
- args: {
139
- text: str(
140
- //
141
- '# Compute Graph',
142
- '',
143
- 'The total projected cost is:',
144
- '',
145
- '```dx',
146
- `="${SHEET_NAME}"!A5`,
147
- '```',
148
- '',
149
- ),
150
- },
151
- };
@@ -1,98 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { syntaxTree } from '@codemirror/language';
6
- import {
7
- type EditorState,
8
- type Extension,
9
- type RangeSet,
10
- RangeSetBuilder,
11
- StateEffect,
12
- StateField,
13
- type Transaction,
14
- } from '@codemirror/state';
15
- import { Decoration, EditorView, ViewPlugin, WidgetType } from '@codemirror/view';
16
-
17
- import { type ComputeNode } from '../graph';
18
-
19
- const LANGUAGE_TAG = 'dx';
20
-
21
- // TODO(burdon): Create marker just for our decorator?
22
- const updateAllDecorations = StateEffect.define<void>();
23
-
24
- export type ComputeOptions = {};
25
-
26
- export const compute = (computeNode: ComputeNode, options: ComputeOptions = {}): Extension => {
27
- const update = (state: EditorState) => {
28
- const builder = new RangeSetBuilder();
29
- syntaxTree(state).iterate({
30
- enter: (node) => {
31
- if (node.name === 'FencedCode') {
32
- const cursor = state.selection.main.head;
33
- if (state.readOnly || cursor < node.from || cursor > node.to) {
34
- const info = node.node.getChild('CodeInfo');
35
- if (info) {
36
- const type = state.sliceDoc(info.from, info.to);
37
- const text = node.node.getChild('CodeText');
38
- if (type === LANGUAGE_TAG && text) {
39
- const content = state.sliceDoc(text.from, text.to);
40
- // TODO(burdon): Map unique reference onto cell; e.g., track ordered list?
41
- computeNode.setValue({ col: 0, row: 0 }, content);
42
- const value = computeNode.getValue({ col: 0, row: 0 });
43
- builder.add(
44
- node.from,
45
- node.to,
46
- Decoration.replace({
47
- widget: new DxWidget(String(value)),
48
- }),
49
- );
50
- }
51
- }
52
- }
53
- }
54
- },
55
- });
56
-
57
- return builder.finish();
58
- };
59
-
60
- return [
61
- // Graph subscription.
62
- ViewPlugin.fromClass(
63
- class {
64
- private readonly _subscription: any;
65
- constructor(view: EditorView) {
66
- this._subscription = computeNode.graph.update.on(() => {
67
- view.dispatch({
68
- effects: updateAllDecorations.of(),
69
- });
70
- });
71
- }
72
-
73
- destroy() {
74
- this._subscription();
75
- }
76
- },
77
- ),
78
-
79
- // Decorations.
80
- StateField.define<RangeSet<any>>({
81
- create: (state) => update(state),
82
- update: (_: RangeSet<any>, tr: Transaction) => update(tr.state),
83
- provide: (field) => EditorView.decorations.from(field),
84
- }),
85
- ];
86
- };
87
-
88
- class DxWidget extends WidgetType {
89
- constructor(private readonly value: string) {
90
- super();
91
- }
92
-
93
- override toDOM(view: EditorView) {
94
- const div = document.createElement('div');
95
- div.innerText = this.value;
96
- return div;
97
- }
98
- }
@@ -1,5 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- export * from './compute';
@@ -1,104 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { describe, expect, test } from 'vitest';
6
-
7
- import { Trigger } from '@dxos/async';
8
- import { Client } from '@dxos/client';
9
- import { create } from '@dxos/client/echo';
10
- import { Context } from '@dxos/context';
11
- import { type S } from '@dxos/echo-schema';
12
- import { FunctionType } from '@dxos/plugin-script/types';
13
-
14
- import { createComputeGraphRegistry } from './compute-graph';
15
- import { addressFromA1Notation, createSheet } from '../defs';
16
- import { SheetModel } from '../model';
17
- import { type CellScalarValue } from '../types';
18
-
19
- // TODO(burdon): Vitest issues:
20
- // - Cannot test Hyperformula
21
- // - throws "Cannot convert undefined or null to object" in vitest (without browser).
22
- // - throws "process.nextTick is not a function" (with browser)
23
- // - throws "Buffer already defined" (if nodeExternal: true in config)
24
- // - Need better docs; esp. vitest config.
25
- // - NOTE: For non-browser tests, import types from x-plugin/types (otherwise will bring in react deps).
26
- // - Can't add flags to our tools?
27
- // - test.only / test.skip ignored?
28
-
29
- /**
30
- * NOTE: Browser test required for hyperformula due to raw translation files.
31
- */
32
- describe('compute graph', () => {
33
- // TODO(burdon): Replace with builder.
34
- const createModel = async (types?: S.Schema<any>[]) => {
35
- const ctx = new Context();
36
- const client = new Client();
37
- if (types) {
38
- client.addTypes(types);
39
- }
40
- await client.initialize();
41
- await client.halo.createIdentity();
42
- const space = await client.spaces.create();
43
- ctx.onDispose(() => client.destroy());
44
-
45
- const registry = createComputeGraphRegistry();
46
- await registry.open(ctx);
47
-
48
- const graph = await registry.createGraph(space);
49
- await graph.open(ctx);
50
-
51
- const sheet = createSheet({ rows: 5, columns: 5 });
52
- const model = new SheetModel(graph, sheet);
53
- await model.open(ctx);
54
-
55
- // TODO(burdon): Move event propagation into graph.
56
- graph.update.on(() => model.update.emit());
57
-
58
- return { space, graph, model };
59
- };
60
-
61
- test('map functions', async () => {
62
- const { space, graph } = await createModel([FunctionType]);
63
-
64
- // Create script.
65
- const fn = space.db.add(create(FunctionType, { version: 1, binding: 'TEST' }));
66
- const id = graph.mapFunctionBindingToId('TEST()');
67
- expect(id).to.eq(`${fn.id}()`);
68
- });
69
-
70
- test('cross-node references', async () => {
71
- const { graph } = await createModel();
72
-
73
- // Create ndoes.
74
- const node1 = graph.getOrCreateNode('node-1');
75
- const node2 = graph.getOrCreateNode('node-2');
76
- node1.hf.setCellContents({ sheet: node1.sheetId, row: 1, col: 1 }, 100);
77
- node2.hf.setCellContents({ sheet: node2.sheetId, row: 1, col: 1 }, `=${node1.sheetId}!A1`);
78
- const value1 = node1.hf.getCellValue({ sheet: node1.sheetId, col: 1, row: 1 });
79
- const value2 = node1.hf.getCellValue({ sheet: node2.sheetId, col: 1, row: 1 });
80
- expect(value1).to.eq(value2);
81
- });
82
-
83
- test('async function', async () => {
84
- const { graph, model } = await createModel();
85
-
86
- // Triggers function.
87
- model.setValue(addressFromA1Notation('A1'), '=TEST()');
88
- const trigger = new Trigger<CellScalarValue>();
89
- model.update.on(() => {
90
- const value = model.getValue(addressFromA1Notation('A1'));
91
- trigger.wake(value);
92
- });
93
-
94
- // Get initial value (i.e., null).
95
- const v1 = model.getValue(addressFromA1Notation('A1'));
96
- expect(v1).to.be.null;
97
- expect(graph.context.info.invocations.TEST).to.eq(undefined);
98
-
99
- // Wait until async update triggered.
100
- const v2 = await trigger.wait();
101
- expect(v2).not.to.be.null;
102
- expect(graph.context.info.invocations.TEST).to.eq(1);
103
- });
104
- });
@@ -1,92 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import '@dxos-theme';
6
-
7
- import React, { useEffect, useRef, useState } from 'react';
8
-
9
- import { FunctionType } from '@dxos/plugin-script/types';
10
- import { create, useSpace, Filter } from '@dxos/react-client/echo';
11
- import { withClientProvider } from '@dxos/react-client/testing';
12
- import { Toolbar, Button, Input } from '@dxos/react-ui';
13
- import { SyntaxHighlighter } from '@dxos/react-ui-syntax-highlighter';
14
- import { withTheme } from '@dxos/storybook-utils';
15
-
16
- import { createSheet } from '../defs';
17
- import { useComputeGraph, useSheetModel } from '../hooks';
18
- import { withGraphDecorator } from '../testing';
19
- import { SheetType } from '../types';
20
-
21
- const FUNCTION_NAME = 'TEST';
22
-
23
- const Story = () => {
24
- const space = useSpace();
25
- const graph = useComputeGraph(space);
26
- const [sheet, setSheet] = useState<SheetType>();
27
- const [text, setText] = useState(`${FUNCTION_NAME}(100)`);
28
- const [result, setResult] = useState<any>();
29
- const model = useSheetModel(space, sheet);
30
- useEffect(() => {
31
- if (space) {
32
- const sheet = space.db.add(createSheet());
33
- setSheet(sheet);
34
- }
35
- }, [space]);
36
-
37
- useEffect(() => {
38
- if (space && graph) {
39
- graph.update.on(() => {
40
- const f1 = graph.getFunctions({ standard: true, echo: false });
41
- const f2 = graph.getFunctions({ standard: false, echo: true });
42
- setResult({ functions: { standard: f1.length, echo: f2.length } });
43
- });
44
-
45
- space.db.add(create(FunctionType, { version: 1, binding: FUNCTION_NAME }));
46
- }
47
- }, [space, graph]);
48
-
49
- const inputRef = useRef<HTMLInputElement | null>(null);
50
- const handleTest = async () => {
51
- if (space && graph) {
52
- const { objects } = await space.db.query(Filter.schema(FunctionType)).run();
53
- const mapped = graph.mapFunctionBindingToId(text);
54
- const unmapped = graph.mapFunctionBindingFromId(mapped);
55
- const internal = graph.mapFormulaToNative(text);
56
- setResult({ mapped, unmapped, internal, functions: objects.map((object) => object.id) });
57
- }
58
-
59
- inputRef.current?.focus();
60
- };
61
-
62
- return (
63
- <div className='flex flex-col gap-2 '>
64
- <Toolbar.Root>
65
- <Input.Root>
66
- <Input.TextInput
67
- ref={inputRef}
68
- placeholder='Formula'
69
- value={text}
70
- onChange={(ev) => setText(ev.target.value)}
71
- />
72
- </Input.Root>
73
- <Button onClick={handleTest}>Test</Button>
74
- </Toolbar.Root>
75
- <SyntaxHighlighter language='json'>
76
- {JSON.stringify({ space: space?.id, graph: graph?.id, sheet: sheet?.id, model: model?.id, result }, null, 2)}
77
- </SyntaxHighlighter>
78
- </div>
79
- );
80
- };
81
-
82
- export default {
83
- title: 'plugin-sheet/functions',
84
- decorators: [
85
- withClientProvider({ types: [FunctionType, SheetType], createIdentity: true, createSpace: true }),
86
- withGraphDecorator,
87
- withTheme,
88
- ],
89
- render: (args: any) => <Story {...args} />,
90
- };
91
-
92
- export const Default = {};