@dxos/plugin-sheet 0.6.12-main.5cc132e → 0.6.12-main.7907542

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 (273) 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-WUPTZUTX.mjs → chunk-5ZMVZYGB.mjs} +21 -19
  4. package/dist/lib/browser/chunk-5ZMVZYGB.mjs.map +7 -0
  5. package/dist/lib/browser/{chunk-GNNVBNCX.mjs → chunk-GSV5QNLD.mjs} +409 -686
  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-Y7ZMFBAP.mjs → chunk-ZL2V5UJR.mjs} +982 -508
  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 +86 -59
  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-KEOKUKAQ.cjs → chunk-2K53Z2TU.cjs} +1036 -558
  21. package/dist/lib/node/chunk-2K53Z2TU.cjs.map +7 -0
  22. package/dist/lib/node/{chunk-ZRQZFV5T.cjs → chunk-5XPK2V4A.cjs} +418 -691
  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-VJU3NPUJ.cjs → chunk-STAVQ2JE.cjs} +25 -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 +98 -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-WUPTZUTX.mjs → chunk-2HAM45RC.mjs} +22 -19
  40. package/dist/lib/node-esm/chunk-2HAM45RC.mjs.map +7 -0
  41. package/dist/lib/node-esm/{chunk-GNNVBNCX.mjs → chunk-5WPZCXNS.mjs} +411 -686
  42. package/dist/lib/node-esm/chunk-5WPZCXNS.mjs.map +7 -0
  43. package/dist/lib/node-esm/{chunk-JRL5LGCE.mjs → chunk-IU2L277A.mjs} +4 -5
  44. package/dist/lib/node-esm/chunk-IU2L277A.mjs.map +7 -0
  45. package/dist/lib/node-esm/{SheetContainer-Y7ZMFBAP.mjs → chunk-QEUCIHIN.mjs} +983 -508
  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 +87 -59
  50. package/dist/lib/node-esm/index.mjs.map +4 -4
  51. package/dist/lib/node-esm/meta.json +1 -1
  52. package/dist/lib/node-esm/meta.mjs +2 -1
  53. package/dist/lib/node-esm/types.mjs +5 -6
  54. package/dist/types/src/SheetPlugin.d.ts.map +1 -1
  55. package/dist/types/src/components/CellEditor/CellEditor.d.ts +23 -3
  56. package/dist/types/src/components/CellEditor/CellEditor.d.ts.map +1 -1
  57. package/dist/types/src/components/CellEditor/CellEditor.stories.d.ts +2 -2
  58. package/dist/types/src/components/CellEditor/CellEditor.stories.d.ts.map +1 -1
  59. package/dist/types/src/components/CellEditor/extension.d.ts +1 -1
  60. package/dist/types/src/components/CellEditor/extension.d.ts.map +1 -1
  61. package/dist/types/src/components/ComputeGraph/ComputeGraphContextProvider.d.ts +11 -0
  62. package/dist/types/src/components/ComputeGraph/ComputeGraphContextProvider.d.ts.map +1 -0
  63. package/dist/types/src/components/ComputeGraph/index.d.ts +1 -3
  64. package/dist/types/src/components/ComputeGraph/index.d.ts.map +1 -1
  65. package/dist/types/src/components/GridSheet/GridSheet.d.ts +10 -0
  66. package/dist/types/src/components/GridSheet/GridSheet.d.ts.map +1 -0
  67. package/dist/types/src/components/GridSheet/GridSheet.stories.d.ts +9 -0
  68. package/dist/types/src/components/GridSheet/GridSheet.stories.d.ts.map +1 -0
  69. package/dist/types/src/components/GridSheet/util.d.ts +16 -0
  70. package/dist/types/src/components/GridSheet/util.d.ts.map +1 -0
  71. package/dist/types/src/components/Sheet/Sheet.d.ts +1 -1
  72. package/dist/types/src/components/Sheet/Sheet.d.ts.map +1 -1
  73. package/dist/types/src/components/Sheet/Sheet.stories.d.ts +5 -6
  74. package/dist/types/src/components/Sheet/Sheet.stories.d.ts.map +1 -1
  75. package/dist/types/src/components/Sheet/grid.d.ts +2 -2
  76. package/dist/types/src/components/Sheet/grid.d.ts.map +1 -1
  77. package/dist/types/src/components/Sheet/nav.d.ts +3 -3
  78. package/dist/types/src/components/Sheet/nav.d.ts.map +1 -1
  79. package/dist/types/src/components/Sheet/sheet-context.d.ts +6 -7
  80. package/dist/types/src/components/Sheet/sheet-context.d.ts.map +1 -1
  81. package/dist/types/src/components/Sheet/threads.d.ts.map +1 -1
  82. package/dist/types/src/components/SheetContainer.d.ts +1 -1
  83. package/dist/types/src/components/SheetContainer.d.ts.map +1 -1
  84. package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -1
  85. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +1 -1
  86. package/dist/types/src/components/index.d.ts +2 -1
  87. package/dist/types/src/components/index.d.ts.map +1 -1
  88. package/dist/types/src/defs/index.d.ts +3 -0
  89. package/dist/types/src/defs/index.d.ts.map +1 -0
  90. package/dist/types/src/{model → defs}/types.d.ts +8 -3
  91. package/dist/types/src/defs/types.d.ts.map +1 -0
  92. package/dist/types/src/defs/types.test.d.ts.map +1 -0
  93. package/dist/types/src/{model → defs}/util.d.ts +8 -4
  94. package/dist/types/src/defs/util.d.ts.map +1 -0
  95. package/dist/types/src/extensions/compute.d.ts +6 -0
  96. package/dist/types/src/extensions/compute.d.ts.map +1 -0
  97. package/dist/types/src/extensions/compute.stories.d.ts +26 -0
  98. package/dist/types/src/extensions/compute.stories.d.ts.map +1 -0
  99. package/dist/types/src/extensions/index.d.ts +2 -0
  100. package/dist/types/src/extensions/index.d.ts.map +1 -0
  101. package/dist/types/src/graph/compute-graph-registry.d.ts +34 -0
  102. package/dist/types/src/graph/compute-graph-registry.d.ts.map +1 -0
  103. package/dist/types/src/graph/compute-graph.d.ts +64 -0
  104. package/dist/types/src/graph/compute-graph.d.ts.map +1 -0
  105. package/dist/types/src/graph/compute-graph.stories.d.ts +10 -0
  106. package/dist/types/src/graph/compute-graph.stories.d.ts.map +1 -0
  107. package/dist/types/src/graph/compute-graph.test.d.ts +2 -0
  108. package/dist/types/src/graph/compute-graph.test.d.ts.map +1 -0
  109. package/dist/types/src/graph/compute-node.d.ts +26 -0
  110. package/dist/types/src/graph/compute-node.d.ts.map +1 -0
  111. package/dist/types/src/{components/ComputeGraph → graph/functions}/async-function.d.ts +14 -5
  112. package/dist/types/src/graph/functions/async-function.d.ts.map +1 -0
  113. package/dist/types/src/graph/functions/edge-function.d.ts +21 -0
  114. package/dist/types/src/graph/functions/edge-function.d.ts.map +1 -0
  115. package/dist/types/src/{model/functions.d.ts → graph/functions/function-defs.d.ts} +1 -1
  116. package/dist/types/src/graph/functions/function-defs.d.ts.map +1 -0
  117. package/dist/types/src/graph/functions/index.d.ts +4 -0
  118. package/dist/types/src/graph/functions/index.d.ts.map +1 -0
  119. package/dist/types/src/graph/hyperformula.test.d.ts +2 -0
  120. package/dist/types/src/graph/hyperformula.test.d.ts.map +1 -0
  121. package/dist/types/src/graph/index.d.ts +5 -0
  122. package/dist/types/src/graph/index.d.ts.map +1 -0
  123. package/dist/types/src/graph/testing/index.d.ts +3 -0
  124. package/dist/types/src/graph/testing/index.d.ts.map +1 -0
  125. package/dist/types/src/graph/testing/test-builder.d.ts +15 -0
  126. package/dist/types/src/graph/testing/test-builder.d.ts.map +1 -0
  127. package/dist/types/src/graph/testing/test-plugin.d.ts +36 -0
  128. package/dist/types/src/graph/testing/test-plugin.d.ts.map +1 -0
  129. package/dist/types/src/graph/util.d.ts +2 -0
  130. package/dist/types/src/graph/util.d.ts.map +1 -0
  131. package/dist/types/src/hooks/hooks.stories.d.ts +11 -0
  132. package/dist/types/src/hooks/hooks.stories.d.ts.map +1 -0
  133. package/dist/types/src/hooks/index.d.ts +4 -0
  134. package/dist/types/src/hooks/index.d.ts.map +1 -0
  135. package/dist/types/src/hooks/useComputeGraph.d.ts +7 -0
  136. package/dist/types/src/hooks/useComputeGraph.d.ts.map +1 -0
  137. package/dist/types/src/hooks/useFormattingModel.d.ts +3 -0
  138. package/dist/types/src/hooks/useFormattingModel.d.ts.map +1 -0
  139. package/dist/types/src/hooks/useSheetModel.d.ts +8 -0
  140. package/dist/types/src/hooks/useSheetModel.d.ts.map +1 -0
  141. package/dist/types/src/meta.d.ts +1 -4
  142. package/dist/types/src/meta.d.ts.map +1 -1
  143. package/dist/types/src/model/formatting-model.d.ts +16 -0
  144. package/dist/types/src/model/formatting-model.d.ts.map +1 -0
  145. package/dist/types/src/model/index.d.ts +2 -4
  146. package/dist/types/src/model/index.d.ts.map +1 -1
  147. package/dist/types/src/model/{model.d.ts → sheet-model.d.ts} +10 -49
  148. package/dist/types/src/model/sheet-model.d.ts.map +1 -0
  149. package/dist/types/src/model/sheet-model.test.d.ts +2 -0
  150. package/dist/types/src/model/sheet-model.test.d.ts.map +1 -0
  151. package/dist/types/src/sanity.test.d.ts +2 -0
  152. package/dist/types/src/sanity.test.d.ts.map +1 -0
  153. package/dist/types/src/testing/index.d.ts +2 -0
  154. package/dist/types/src/testing/index.d.ts.map +1 -0
  155. package/dist/types/src/testing/testing.d.ts +8 -0
  156. package/dist/types/src/testing/testing.d.ts.map +1 -0
  157. package/dist/types/src/types.d.ts +15 -4
  158. package/dist/types/src/types.d.ts.map +1 -1
  159. package/dist/vendor/hyperformula.mjs +37145 -0
  160. package/package.json +48 -44
  161. package/src/SheetPlugin.tsx +46 -62
  162. package/src/components/CellEditor/CellEditor.stories.tsx +6 -6
  163. package/src/components/CellEditor/CellEditor.tsx +59 -9
  164. package/src/components/CellEditor/extension.test.ts +3 -4
  165. package/src/components/CellEditor/extension.ts +5 -6
  166. package/src/components/ComputeGraph/ComputeGraphContextProvider.tsx +20 -0
  167. package/src/components/ComputeGraph/index.ts +1 -3
  168. package/src/components/GridSheet/GridSheet.stories.tsx +36 -0
  169. package/src/components/GridSheet/GridSheet.tsx +171 -0
  170. package/src/components/GridSheet/util.ts +148 -0
  171. package/src/components/Sheet/Sheet.stories.tsx +48 -88
  172. package/src/components/Sheet/Sheet.tsx +42 -24
  173. package/src/components/Sheet/grid.ts +3 -3
  174. package/src/components/Sheet/nav.ts +19 -19
  175. package/src/components/Sheet/sheet-context.tsx +12 -82
  176. package/src/components/Sheet/threads.tsx +10 -6
  177. package/src/components/SheetContainer.tsx +13 -15
  178. package/src/components/Toolbar/Toolbar.tsx +1 -2
  179. package/src/components/index.ts +1 -0
  180. package/src/defs/index.ts +6 -0
  181. package/src/{model → defs}/types.test.ts +7 -7
  182. package/src/{model → defs}/types.ts +24 -14
  183. package/src/{model → defs}/util.ts +65 -17
  184. package/src/extensions/compute.stories.tsx +151 -0
  185. package/src/extensions/compute.ts +147 -0
  186. package/src/extensions/index.ts +5 -0
  187. package/src/graph/compute-graph-registry.ts +90 -0
  188. package/src/graph/compute-graph.stories.tsx +93 -0
  189. package/src/graph/compute-graph.test.ts +87 -0
  190. package/src/graph/compute-graph.ts +242 -0
  191. package/src/graph/compute-node.ts +63 -0
  192. package/src/{components/ComputeGraph → graph/functions}/async-function.ts +25 -15
  193. package/src/{components/ComputeGraph → graph/functions}/edge-function.ts +16 -14
  194. package/src/graph/functions/index.ts +7 -0
  195. package/src/graph/hyperformula.test.ts +14 -0
  196. package/src/graph/index.ts +8 -0
  197. package/src/graph/testing/index.ts +6 -0
  198. package/src/graph/testing/test-builder.ts +54 -0
  199. package/src/{components/ComputeGraph/custom.ts → graph/testing/test-plugin.ts} +44 -14
  200. package/src/graph/util.ts +8 -0
  201. package/src/hooks/hooks.stories.tsx +50 -0
  202. package/src/hooks/index.ts +7 -0
  203. package/src/hooks/useComputeGraph.ts +28 -0
  204. package/src/hooks/useFormattingModel.ts +11 -0
  205. package/src/hooks/useSheetModel.ts +40 -0
  206. package/src/meta.tsx +1 -5
  207. package/src/{components/Sheet/formatting.ts → model/formatting-model.ts} +20 -13
  208. package/src/model/index.ts +2 -4
  209. package/src/model/sheet-model.test.ts +57 -0
  210. package/src/model/{model.ts → sheet-model.ts} +88 -188
  211. package/src/sanity.test.ts +40 -0
  212. package/src/testing/index.ts +5 -0
  213. package/src/testing/testing.tsx +68 -0
  214. package/src/types.ts +19 -17
  215. package/dist/lib/browser/SheetContainer-Y7ZMFBAP.mjs.map +0 -7
  216. package/dist/lib/browser/chunk-GNNVBNCX.mjs.map +0 -7
  217. package/dist/lib/browser/chunk-JRL5LGCE.mjs.map +0 -7
  218. package/dist/lib/browser/chunk-PGKZPKUD.mjs +0 -175
  219. package/dist/lib/browser/chunk-PGKZPKUD.mjs.map +0 -7
  220. package/dist/lib/browser/chunk-VBF7YENS.mjs +0 -8
  221. package/dist/lib/browser/chunk-VBF7YENS.mjs.map +0 -7
  222. package/dist/lib/browser/chunk-WUPTZUTX.mjs.map +0 -7
  223. package/dist/lib/browser/testing.mjs +0 -92
  224. package/dist/lib/browser/testing.mjs.map +0 -7
  225. package/dist/lib/node/SheetContainer-KEOKUKAQ.cjs.map +0 -7
  226. package/dist/lib/node/chunk-57PB2HPY.cjs +0 -40
  227. package/dist/lib/node/chunk-57PB2HPY.cjs.map +0 -7
  228. package/dist/lib/node/chunk-6LWBQAQZ.cjs +0 -202
  229. package/dist/lib/node/chunk-6LWBQAQZ.cjs.map +0 -7
  230. package/dist/lib/node/chunk-BJ6ZD7MN.cjs.map +0 -7
  231. package/dist/lib/node/chunk-VJU3NPUJ.cjs.map +0 -7
  232. package/dist/lib/node/chunk-ZRQZFV5T.cjs.map +0 -7
  233. package/dist/lib/node/testing.cjs +0 -111
  234. package/dist/lib/node/testing.cjs.map +0 -7
  235. package/dist/lib/node-esm/SheetContainer-Y7ZMFBAP.mjs.map +0 -7
  236. package/dist/lib/node-esm/chunk-GNNVBNCX.mjs.map +0 -7
  237. package/dist/lib/node-esm/chunk-JRL5LGCE.mjs.map +0 -7
  238. package/dist/lib/node-esm/chunk-PGKZPKUD.mjs +0 -175
  239. package/dist/lib/node-esm/chunk-PGKZPKUD.mjs.map +0 -7
  240. package/dist/lib/node-esm/chunk-VBF7YENS.mjs +0 -8
  241. package/dist/lib/node-esm/chunk-VBF7YENS.mjs.map +0 -7
  242. package/dist/lib/node-esm/chunk-WUPTZUTX.mjs.map +0 -7
  243. package/dist/lib/node-esm/testing.mjs +0 -92
  244. package/dist/lib/node-esm/testing.mjs.map +0 -7
  245. package/dist/types/src/components/ComputeGraph/async-function.d.ts.map +0 -1
  246. package/dist/types/src/components/ComputeGraph/custom.d.ts +0 -21
  247. package/dist/types/src/components/ComputeGraph/custom.d.ts.map +0 -1
  248. package/dist/types/src/components/ComputeGraph/edge-function.d.ts +0 -20
  249. package/dist/types/src/components/ComputeGraph/edge-function.d.ts.map +0 -1
  250. package/dist/types/src/components/ComputeGraph/graph-context.d.ts +0 -12
  251. package/dist/types/src/components/ComputeGraph/graph-context.d.ts.map +0 -1
  252. package/dist/types/src/components/ComputeGraph/graph.browser.test.d.ts +0 -2
  253. package/dist/types/src/components/ComputeGraph/graph.browser.test.d.ts.map +0 -1
  254. package/dist/types/src/components/ComputeGraph/graph.d.ts +0 -26
  255. package/dist/types/src/components/ComputeGraph/graph.d.ts.map +0 -1
  256. package/dist/types/src/components/Sheet/formatting.d.ts +0 -14
  257. package/dist/types/src/components/Sheet/formatting.d.ts.map +0 -1
  258. package/dist/types/src/model/functions.d.ts.map +0 -1
  259. package/dist/types/src/model/model.browser.test.d.ts +0 -2
  260. package/dist/types/src/model/model.browser.test.d.ts.map +0 -1
  261. package/dist/types/src/model/model.d.ts.map +0 -1
  262. package/dist/types/src/model/types.d.ts.map +0 -1
  263. package/dist/types/src/model/types.test.d.ts.map +0 -1
  264. package/dist/types/src/model/util.d.ts.map +0 -1
  265. package/dist/types/src/testing.d.ts +0 -9
  266. package/dist/types/src/testing.d.ts.map +0 -1
  267. package/src/components/ComputeGraph/graph-context.tsx +0 -50
  268. package/src/components/ComputeGraph/graph.browser.test.ts +0 -49
  269. package/src/components/ComputeGraph/graph.ts +0 -62
  270. package/src/model/model.browser.test.ts +0 -99
  271. package/src/testing.ts +0 -50
  272. /package/dist/types/src/{model → defs}/types.test.d.ts +0 -0
  273. /package/src/{model/functions.ts → graph/functions/function-defs.ts} +0 -0
@@ -1,22 +1,69 @@
1
- import {
2
- ValueTypeEnum
3
- } from "./chunk-WUPTZUTX.mjs";
1
+ // packages/plugins/plugin-sheet/src/graph/compute-graph.ts
2
+ import { Event as Event2 } from "@dxos/async";
3
+ import { Filter as Filter2, fullyQualifiedId } from "@dxos/client/echo";
4
+ import { Resource as Resource2 } from "@dxos/context";
5
+ import { getTypename } from "@dxos/echo-schema";
6
+ import { invariant } from "@dxos/invariant";
7
+ import { PublicKey } from "@dxos/keys";
8
+ import { log as log3 } from "@dxos/log";
9
+ import { FunctionType as FunctionType2 } from "@dxos/plugin-script/types";
10
+ import { nonNullable as nonNullable2 } from "@dxos/util";
11
+ import { ExportedCellChange } from "#hyperformula";
4
12
 
5
- // packages/plugins/plugin-sheet/src/components/ComputeGraph/graph.ts
6
- import { HyperFormula } from "hyperformula";
13
+ // packages/plugins/plugin-sheet/src/graph/compute-node.ts
7
14
  import { Event } from "@dxos/async";
8
- import { PublicKey } from "@dxos/keys";
9
- import { log as log2 } from "@dxos/log";
15
+ import { Resource } from "@dxos/context";
16
+ import { DetailedCellError } from "#hyperformula";
17
+ var ComputeNode = class extends Resource {
18
+ constructor(_graph, sheetId) {
19
+ super();
20
+ this._graph = _graph;
21
+ this.sheetId = sheetId;
22
+ this.update = new Event();
23
+ }
24
+ get graph() {
25
+ return this._graph;
26
+ }
27
+ clear() {
28
+ this._graph.hf.clearSheet(this.sheetId);
29
+ }
30
+ getValue(cell) {
31
+ const value = this._graph.hf.getCellValue({
32
+ sheet: this.sheetId,
33
+ row: cell.row,
34
+ col: cell.col
35
+ });
36
+ if (value instanceof DetailedCellError) {
37
+ return null;
38
+ }
39
+ return value;
40
+ }
41
+ setValue(cell, value) {
42
+ const mappedValue = typeof value === "string" && value.charAt(0) === "=" ? this._graph.mapFormulaToNative(value) : value;
43
+ this._graph.hf.setCellContents({
44
+ sheet: this.sheetId,
45
+ row: cell.row,
46
+ col: cell.col
47
+ }, [
48
+ [
49
+ mappedValue
50
+ ]
51
+ ]);
52
+ }
53
+ // TODO(burdon): Load data into sheet.
54
+ async _open() {
55
+ }
56
+ };
10
57
 
11
- // packages/plugins/plugin-sheet/src/components/ComputeGraph/async-function.ts
12
- import { CellError, ErrorType, EmptyValue, FunctionPlugin } from "hyperformula";
58
+ // packages/plugins/plugin-sheet/src/graph/functions/async-function.ts
13
59
  import defaultsDeep from "lodash.defaultsdeep";
14
60
  import { debounce } from "@dxos/async";
15
61
  import { log } from "@dxos/log";
16
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/plugins/plugin-sheet/src/components/ComputeGraph/async-function.ts";
62
+ import { CellError, ErrorType, EmptyValue, FunctionPlugin } from "#hyperformula";
63
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/plugins/plugin-sheet/src/graph/functions/async-function.ts";
17
64
  var defaultFunctionContextOptions = {
18
65
  defaultTtl: 5e3,
19
- recalculationDelay: 200,
66
+ debounceDelay: 200,
20
67
  remoteFunctionUrl: "https://edge.dxos.workers.dev/functions"
21
68
  };
22
69
  var FunctionContext = class _FunctionContext {
@@ -27,7 +74,7 @@ var FunctionContext = class _FunctionContext {
27
74
  ...args
28
75
  });
29
76
  }
30
- constructor(_hf, _space, onUpdate, _options) {
77
+ constructor(_hf, _space, _options) {
31
78
  this._hf = _hf;
32
79
  this._space = _space;
33
80
  this._cache = /* @__PURE__ */ new Map();
@@ -35,10 +82,10 @@ var FunctionContext = class _FunctionContext {
35
82
  this._subscriptions = /* @__PURE__ */ new Map();
36
83
  this._invocations = {};
37
84
  this._options = defaultsDeep(_options ?? {}, defaultFunctionContextOptions);
38
- this._onUpdate = debounce(() => {
85
+ this._onUpdate = debounce((update) => {
39
86
  this._hf.resumeEvaluation();
40
- onUpdate(this);
41
- }, this._options.recalculationDelay);
87
+ this._options.onUpdate?.(update);
88
+ }, this._options.debounceDelay);
42
89
  }
43
90
  get space() {
44
91
  return this._space;
@@ -88,18 +135,21 @@ var FunctionContext = class _FunctionContext {
88
135
  value: value2
89
136
  }, {
90
137
  F: __dxlog_file,
91
- L: 136,
138
+ L: 143,
92
139
  S: this,
93
140
  C: (f, a) => f(...a)
94
141
  });
95
- this._onUpdate();
142
+ this._onUpdate({
143
+ name,
144
+ cell
145
+ });
96
146
  } catch (err) {
97
147
  log.warn("failed", {
98
148
  cell,
99
149
  err
100
150
  }, {
101
151
  F: __dxlog_file,
102
- L: 140,
152
+ L: 147,
103
153
  S: this,
104
154
  C: (f, a) => f(...a)
105
155
  });
@@ -119,17 +169,20 @@ var FunctionContext = class _FunctionContext {
119
169
  cache: value
120
170
  }, {
121
171
  F: __dxlog_file,
122
- L: 148,
172
+ L: 155,
123
173
  S: this,
124
174
  C: (f, a) => f(...a)
125
175
  });
126
176
  return value;
127
177
  }
128
178
  };
129
- var FunctionPluginAsync = class extends FunctionPlugin {
179
+ var AsyncFunctionPlugin = class extends FunctionPlugin {
130
180
  get context() {
131
181
  return this.config.context;
132
182
  }
183
+ /**
184
+ * Immediately returns cached value then runs the async function.
185
+ */
133
186
  runAsyncFunction(ast, state, cb, options) {
134
187
  const { procedureName } = ast;
135
188
  const metadata = this.metadata(procedureName);
@@ -139,53 +192,127 @@ var FunctionPluginAsync = class extends FunctionPlugin {
139
192
  }
140
193
  };
141
194
 
142
- // packages/plugins/plugin-sheet/src/components/ComputeGraph/graph.ts
143
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/plugins/plugin-sheet/src/components/ComputeGraph/graph.ts";
144
- var createComputeGraph = (functionPlugins = [], space, options) => {
145
- functionPlugins.forEach(({ plugin, translations }) => {
146
- HyperFormula.registerFunctionPlugin(plugin, translations);
147
- });
148
- const hf = HyperFormula.buildEmpty({
149
- licenseKey: "gpl-v3"
150
- });
151
- return new ComputeGraph(hf, space, options);
152
- };
153
- var ComputeGraph = class {
154
- constructor(hf, _space, _options) {
155
- this.hf = hf;
156
- this._space = _space;
157
- this._options = _options;
158
- this.id = `graph-${PublicKey.random().truncate()}`;
159
- this.update = new Event();
160
- this.context = new FunctionContext(this.hf, this._space, () => {
161
- this.refresh();
162
- }, this._options);
163
- this.hf.updateConfig({
164
- context: this.context
195
+ // packages/plugins/plugin-sheet/src/graph/functions/edge-function.ts
196
+ import { effect } from "@preact/signals-core";
197
+ import { Filter, getMeta } from "@dxos/client/echo";
198
+ import { log as log2 } from "@dxos/log";
199
+ import { getUserFunctionUrlInMetadata } from "@dxos/plugin-script/edge";
200
+ import { FunctionType } from "@dxos/plugin-script/types";
201
+ import { nonNullable } from "@dxos/util";
202
+ import { CellError as CellError2, ErrorType as ErrorType2, FunctionArgumentType } from "#hyperformula";
203
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/plugins/plugin-sheet/src/graph/functions/edge-function.ts";
204
+ var EDGE_FUNCTION_NAME = "DX";
205
+ var FUNCTION_TTL = 1e4;
206
+ var EdgeFunctionPlugin = class extends AsyncFunctionPlugin {
207
+ dx(ast, state) {
208
+ const handler = (subscribe = false) => async (binding, ...args) => {
209
+ const space = this.context.space;
210
+ if (!space) {
211
+ return new CellError2(ErrorType2.REF, "Missing space");
212
+ }
213
+ const { objects: [fn] } = await space.db.query(Filter.schema(FunctionType, {
214
+ binding
215
+ })).run();
216
+ if (!fn) {
217
+ log2.info("Function not found", {
218
+ binding
219
+ }, {
220
+ F: __dxlog_file2,
221
+ L: 41,
222
+ S: this,
223
+ C: (f, a) => f(...a)
224
+ });
225
+ return new CellError2(ErrorType2.REF, "Function not found");
226
+ }
227
+ if (subscribe) {
228
+ const unsubscribe = effect(() => {
229
+ log2.info("function changed", {
230
+ fn
231
+ }, {
232
+ F: __dxlog_file2,
233
+ L: 47,
234
+ S: this,
235
+ C: (f, a) => f(...a)
236
+ });
237
+ const _ = fn?.version;
238
+ this.runAsyncFunction(ast, state, handler(false), {
239
+ ttl: FUNCTION_TTL
240
+ });
241
+ });
242
+ this.context.createSubscription(ast.procedureName, unsubscribe);
243
+ }
244
+ const path = getUserFunctionUrlInMetadata(getMeta(fn));
245
+ const result = await fetch(`${this.context.remoteFunctionUrl}${path}`, {
246
+ method: "POST",
247
+ headers: {
248
+ "Content-Type": "application/json"
249
+ },
250
+ body: JSON.stringify({
251
+ args: args.filter(nonNullable)
252
+ })
253
+ });
254
+ return await result.text();
255
+ };
256
+ return this.runAsyncFunction(ast, state, handler(true), {
257
+ ttl: FUNCTION_TTL
165
258
  });
166
259
  }
167
- refresh() {
168
- log2("refresh", {
169
- id: this.id
170
- }, {
171
- F: __dxlog_file2,
172
- L: 59,
173
- S: this,
174
- C: (f, a) => f(...a)
175
- });
176
- this.update.emit();
260
+ };
261
+ EdgeFunctionPlugin.implementedFunctions = {
262
+ [EDGE_FUNCTION_NAME]: {
263
+ method: "dx",
264
+ parameters: [
265
+ // Binding
266
+ {
267
+ argumentType: FunctionArgumentType.STRING
268
+ },
269
+ // Remote function arguments (currently supporting up to 8).
270
+ {
271
+ argumentType: FunctionArgumentType.ANY,
272
+ optionalArg: true
273
+ },
274
+ {
275
+ argumentType: FunctionArgumentType.ANY,
276
+ optionalArg: true
277
+ },
278
+ {
279
+ argumentType: FunctionArgumentType.ANY,
280
+ optionalArg: true
281
+ },
282
+ {
283
+ argumentType: FunctionArgumentType.ANY,
284
+ optionalArg: true
285
+ },
286
+ {
287
+ argumentType: FunctionArgumentType.ANY,
288
+ optionalArg: true
289
+ },
290
+ {
291
+ argumentType: FunctionArgumentType.ANY,
292
+ optionalArg: true
293
+ },
294
+ {
295
+ argumentType: FunctionArgumentType.ANY,
296
+ optionalArg: true
297
+ },
298
+ {
299
+ argumentType: FunctionArgumentType.ANY,
300
+ optionalArg: true
301
+ }
302
+ ],
303
+ isVolatile: true
304
+ }
305
+ };
306
+ var EdgeFunctionPluginTranslations = {
307
+ enGB: {
308
+ [EDGE_FUNCTION_NAME]: "Remote function"
309
+ },
310
+ enUS: {
311
+ [EDGE_FUNCTION_NAME]: "Remote function"
177
312
  }
178
313
  };
179
314
 
180
- // packages/plugins/plugin-sheet/src/model/model.ts
181
- import { DetailedCellError, ExportedCellChange } from "hyperformula";
182
- import { Event as Event2 } from "@dxos/async";
183
- import { Context } from "@dxos/context";
184
- import { invariant as invariant2 } from "@dxos/invariant";
185
- import { PublicKey as PublicKey2 } from "@dxos/keys";
186
- import { log as log3 } from "@dxos/log";
187
-
188
- // packages/plugins/plugin-sheet/src/model/functions.ts
315
+ // packages/plugins/plugin-sheet/src/graph/functions/function-defs.ts
189
316
  var defaultFunctions = [
190
317
  // Array
191
318
  {
@@ -2559,685 +2686,281 @@ var defaultFunctions = [
2559
2686
  }
2560
2687
  ];
2561
2688
 
2562
- // packages/plugins/plugin-sheet/src/model/types.ts
2563
- import { invariant } from "@dxos/invariant";
2564
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/plugins/plugin-sheet/src/model/types.ts";
2565
- var MAX_COLUMNS = 26 * 26;
2566
- var posEquals = (a, b) => {
2567
- return a?.column === b?.column && a?.row === b?.row;
2568
- };
2569
- var columnLetter = (column) => {
2570
- invariant(column < MAX_COLUMNS, `Invalid column: ${column}`, {
2571
- F: __dxlog_file3,
2572
- L: 17,
2573
- S: void 0,
2574
- A: [
2575
- "column < MAX_COLUMNS",
2576
- "`Invalid column: ${column}`"
2577
- ]
2578
- });
2579
- return (column >= 26 ? String.fromCharCode("A".charCodeAt(0) + Math.floor(column / 26) - 1) : "") + String.fromCharCode("A".charCodeAt(0) + column % 26);
2580
- };
2581
- var addressToA1Notation = ({ column, row }) => {
2582
- return `${columnLetter(column)}${row + 1}`;
2583
- };
2584
- var addressFromA1Notation = (ref) => {
2585
- const match = ref.match(/([A-Z]+)(\d+)/);
2586
- invariant(match, `Invalid notation: ${ref}`, {
2587
- F: __dxlog_file3,
2588
- L: 30,
2589
- S: void 0,
2590
- A: [
2591
- "match",
2592
- "`Invalid notation: ${ref}`"
2593
- ]
2594
- });
2595
- return {
2596
- row: parseInt(match[2], 10) - 1,
2597
- column: match[1].split("").reduce((acc, c) => acc * 26 + c.charCodeAt(0) - "A".charCodeAt(0) + 1, 0) - 1
2689
+ // packages/plugins/plugin-sheet/src/graph/compute-graph.ts
2690
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/plugins/plugin-sheet/src/graph/compute-graph.ts";
2691
+ var OBJECT_ID_LENGTH = 60;
2692
+ var createSheetName = ({ type, id }) => `${type}@${id}`;
2693
+ var parseSheetName = (name) => {
2694
+ const [type, id] = name.split("@");
2695
+ return id ? {
2696
+ type,
2697
+ id
2698
+ } : {
2699
+ id: type
2598
2700
  };
2599
2701
  };
2600
- var rangeToA1Notation = (range) => {
2601
- return [
2602
- range?.from && addressToA1Notation(range?.from),
2603
- range?.to && addressToA1Notation(range?.to)
2604
- ].filter(Boolean).join(":");
2605
- };
2606
- var inRange = (range, cell) => {
2607
- if (!range) {
2608
- return false;
2609
- }
2610
- const { from, to } = range;
2611
- if (from && posEquals(from, cell) || to && posEquals(to, cell)) {
2612
- return true;
2613
- }
2614
- if (!from || !to) {
2615
- return false;
2616
- }
2617
- const { column: c1, row: r1 } = from;
2618
- const { column: c2, row: r2 } = to;
2619
- const cMin = Math.min(c1, c2);
2620
- const cMax = Math.max(c1, c2);
2621
- const rMin = Math.min(r1, r2);
2622
- const rMax = Math.max(r1, r2);
2623
- const { column, row } = cell;
2624
- return column >= cMin && column <= cMax && row >= rMin && row <= rMax;
2625
- };
2626
-
2627
- // packages/plugins/plugin-sheet/src/model/util.ts
2628
- import { randomBytes } from "@dxos/crypto";
2629
- var ApiError = class extends Error {
2630
- };
2631
- var ReadonlyException = class extends ApiError {
2632
- };
2633
- var RangeException = class extends ApiError {
2634
- constructor(n) {
2702
+ var ComputeGraph = class extends Resource2 {
2703
+ constructor(_hf, _space, _options) {
2635
2704
  super();
2636
- }
2637
- };
2638
- var createIndex = (length = 8) => {
2639
- const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
2640
- const charactersLength = characters.length;
2641
- const randomBuffer = randomBytes(length);
2642
- return Array.from(randomBuffer, (byte) => characters[byte % charactersLength]).join("");
2643
- };
2644
- var createIndices = (length) => Array.from({
2645
- length
2646
- }).map(() => createIndex());
2647
- var addressToIndex = (sheet, cell) => {
2648
- return `${sheet.columns[cell.column]}@${sheet.rows[cell.row]}`;
2649
- };
2650
- var addressFromIndex = (sheet, idx) => {
2651
- const [column, row] = idx.split("@");
2652
- return {
2653
- column: sheet.columns.indexOf(column),
2654
- row: sheet.rows.indexOf(row)
2655
- };
2656
- };
2657
- var rangeFromIndex = (sheet, idx) => {
2658
- const [from, to] = idx.split(":").map((index) => addressFromIndex(sheet, index));
2659
- return {
2660
- from,
2661
- to
2662
- };
2663
- };
2664
- var closest = (cursor, cells) => {
2665
- let closestCell;
2666
- let closestDistance = Number.MAX_SAFE_INTEGER;
2667
- for (const cell of cells) {
2668
- const distance = Math.abs(cell.row - cursor.row) + Math.abs(cell.column - cursor.column);
2669
- if (distance < closestDistance) {
2670
- closestCell = cell;
2671
- closestDistance = distance;
2672
- }
2673
- }
2674
- return closestCell;
2675
- };
2676
- var compareIndexPositions = (sheet, indexA, indexB) => {
2677
- const { row: rowA, column: columnA } = addressFromIndex(sheet, indexA);
2678
- const { row: rowB, column: columnB } = addressFromIndex(sheet, indexB);
2679
- if (rowA !== rowB) {
2680
- return rowA - rowB;
2681
- } else {
2682
- return columnA - columnB;
2683
- }
2684
- };
2685
-
2686
- // packages/plugins/plugin-sheet/src/model/model.ts
2687
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/plugins/plugin-sheet/src/model/model.ts";
2688
- var DEFAULT_ROWS = 100;
2689
- var DEFAULT_COLUMNS = 26;
2690
- var typeMap = {
2691
- BOOLEAN: ValueTypeEnum.Boolean,
2692
- NUMBER_RAW: ValueTypeEnum.Number,
2693
- NUMBER_PERCENT: ValueTypeEnum.Percent,
2694
- NUMBER_CURRENCY: ValueTypeEnum.Currency,
2695
- NUMBER_DATETIME: ValueTypeEnum.DateTime,
2696
- NUMBER_DATE: ValueTypeEnum.Date,
2697
- NUMBER_TIME: ValueTypeEnum.Time
2698
- };
2699
- var defaultOptions = {
2700
- rows: 50,
2701
- columns: 26,
2702
- mapFormulaBindingFromId: () => (formula) => formula,
2703
- mapFormulaBindingToId: () => (formula) => formula
2704
- };
2705
- var getTopLeft = (range) => {
2706
- const to = range.to ?? range.from;
2707
- return {
2708
- row: Math.min(range.from.row, to.row),
2709
- column: Math.min(range.from.column, to.column)
2710
- };
2711
- };
2712
- var toSimpleCellAddress = (sheet, cell) => ({
2713
- sheet,
2714
- row: cell.row,
2715
- col: cell.column
2716
- });
2717
- var toModelRange = (sheet, range) => ({
2718
- start: toSimpleCellAddress(sheet, range.from),
2719
- end: toSimpleCellAddress(sheet, range.to ?? range.from)
2720
- });
2721
- var SheetModel = class {
2722
- constructor(_graph, _sheet, _space, options = {}) {
2723
- this._graph = _graph;
2724
- this._sheet = _sheet;
2705
+ this._hf = _hf;
2725
2706
  this._space = _space;
2726
- this.id = `model-${PublicKey2.random().truncate()}`;
2727
- this._ctx = void 0;
2728
- this._functions = [];
2707
+ this._options = _options;
2708
+ this.id = `graph-${PublicKey.random().truncate()}`;
2709
+ this._nodes = /* @__PURE__ */ new Map();
2710
+ this._remoteFunctions = [];
2729
2711
  this.update = new Event2();
2730
- const name = this._sheet.id;
2731
- if (!this._graph.hf.doesSheetExist(name)) {
2732
- this._graph.hf.addSheet(name);
2733
- }
2734
- this._sheetId = this._graph.hf.getSheetId(name);
2735
- this._options = {
2736
- ...defaultOptions,
2737
- ...options
2712
+ this.context = new FunctionContext(this._hf, this._space, this._options);
2713
+ this._hf.updateConfig({
2714
+ context: this.context
2715
+ });
2716
+ const onValuesUpdate = (changes) => {
2717
+ for (const change of changes) {
2718
+ if (change instanceof ExportedCellChange) {
2719
+ const { sheet } = change;
2720
+ const node = this._nodes.get(sheet);
2721
+ if (node) {
2722
+ node.update.emit({
2723
+ type: "valuesUpdated",
2724
+ change
2725
+ });
2726
+ }
2727
+ }
2728
+ }
2738
2729
  };
2739
- this.reset();
2740
- }
2741
- get graph() {
2742
- return this._graph;
2743
- }
2744
- get sheet() {
2745
- return this._sheet;
2746
- }
2747
- get readonly() {
2748
- return this._options.readonly;
2730
+ this._hf.on("valuesUpdated", onValuesUpdate);
2731
+ this._ctx.onDispose(() => this._hf.off("valuesUpdated", onValuesUpdate));
2749
2732
  }
2750
- get bounds() {
2751
- return {
2752
- rows: this._sheet.rows.length,
2753
- columns: this._sheet.columns.length
2754
- };
2733
+ get hf() {
2734
+ return this._hf;
2755
2735
  }
2756
- get functions() {
2757
- const hfFunctions = this._graph.hf.getRegisteredFunctionNames().map((name) => defaultFunctions.find((fn) => fn.name === name) ?? {
2758
- name
2759
- });
2760
- const echoFunctions = this._functions.map((fn) => ({
2761
- name: fn.binding
2762
- }));
2736
+ getFunctions({ standard, echo } = {
2737
+ standard: true,
2738
+ echo: true
2739
+ }) {
2763
2740
  return [
2764
- ...hfFunctions,
2765
- ...echoFunctions
2741
+ ...standard ? this._hf.getRegisteredFunctionNames().map((name) => defaultFunctions.find((fn) => fn.name === name) ?? {
2742
+ name
2743
+ }) : [],
2744
+ ...echo ? this._remoteFunctions.map((fn) => ({
2745
+ name: fn.binding
2746
+ })) : []
2766
2747
  ];
2767
2748
  }
2768
- get initialized() {
2769
- return !!this._ctx;
2770
- }
2771
2749
  /**
2772
- * Initialize sheet and engine.
2750
+ * Get or create cell representing a sheet.
2773
2751
  */
2774
- async initialize() {
2775
- log3("initialize", {
2776
- id: this.id
2777
- }, {
2778
- F: __dxlog_file4,
2779
- L: 142,
2780
- S: this,
2781
- C: (f, a) => f(...a)
2782
- });
2783
- invariant2(!this.initialized, "Already initialized.", {
2784
- F: __dxlog_file4,
2785
- L: 143,
2752
+ // TODO(burdon): Async (open node).
2753
+ // The graph should be an extensible factory that plugins extend with model constructors.
2754
+ // This would enable on-the-fly instantiation of new models when then are referenced.
2755
+ // E.g., Cross-object reference would be stored as "ObjectId!A1"
2756
+ // The graph would then load the object and create a ComputeNode (model) of the appropriate type.
2757
+ getOrCreateNode(name) {
2758
+ invariant(name.length, void 0, {
2759
+ F: __dxlog_file3,
2760
+ L: 117,
2786
2761
  S: this,
2787
2762
  A: [
2788
- "!this.initialized",
2789
- "'Already initialized.'"
2763
+ "name.length",
2764
+ ""
2790
2765
  ]
2791
2766
  });
2792
- this._ctx = new Context(void 0, {
2793
- F: __dxlog_file4,
2794
- L: 144
2795
- });
2796
- if (!this._sheet.rows.length) {
2797
- this._insertIndices(this._sheet.rows, 0, this._options.rows, DEFAULT_ROWS);
2798
- }
2799
- if (!this._sheet.columns.length) {
2800
- this._insertIndices(this._sheet.columns, 0, this._options.columns, DEFAULT_COLUMNS);
2801
- }
2802
- this.reset();
2803
- const unsubscribe = this._graph.update.on(() => this.update.emit());
2804
- this._ctx.onDispose(unsubscribe);
2805
- if (this._space) {
2806
- const { Filter } = await import("@dxos/client/echo");
2807
- const { FunctionType } = await import("@dxos/plugin-script/types");
2808
- const query = this._space?.db.query(Filter.schema(FunctionType));
2809
- const unsubscribe2 = query.subscribe(({ objects }) => {
2810
- this._functions = objects.filter((fn) => fn.binding);
2811
- this.update.emit();
2767
+ if (!this._hf.doesSheetExist(name)) {
2768
+ log3.info("created node", {
2769
+ space: this._space?.id,
2770
+ sheet: name
2771
+ }, {
2772
+ F: __dxlog_file3,
2773
+ L: 119,
2774
+ S: this,
2775
+ C: (f, a) => f(...a)
2812
2776
  });
2813
- this._ctx.onDispose(unsubscribe2);
2777
+ this._hf.addSheet(name);
2814
2778
  }
2815
- return this;
2816
- }
2817
- async destroy() {
2818
- log3("destroy", {
2819
- id: this.id
2820
- }, {
2821
- F: __dxlog_file4,
2822
- L: 174,
2779
+ const sheetId = this._hf.getSheetId(name);
2780
+ invariant(sheetId !== void 0, void 0, {
2781
+ F: __dxlog_file3,
2782
+ L: 124,
2823
2783
  S: this,
2824
- C: (f, a) => f(...a)
2825
- });
2826
- if (this._ctx) {
2827
- await this._ctx.dispose();
2828
- this._ctx = void 0;
2829
- }
2830
- }
2831
- /**
2832
- * Update engine.
2833
- * NOTE: This resets the undo history.
2834
- * @deprecated
2835
- */
2836
- reset() {
2837
- this._graph.hf.clearSheet(this._sheetId);
2838
- Object.entries(this._sheet.cells).forEach(([key, { value }]) => {
2839
- const { column, row } = addressFromIndex(this._sheet, key);
2840
- if (typeof value === "string" && value.charAt(0) === "=") {
2841
- value = this.mapFormulaBindingToFormula(this.mapFormulaBindingFromId(this.mapFormulaIndicesToRefs(value)));
2842
- }
2843
- this._graph.hf.setCellContents({
2844
- sheet: this._sheetId,
2845
- row,
2846
- col: column
2847
- }, value);
2848
- });
2849
- }
2850
- /**
2851
- * Recalculate formulas.
2852
- * NOTE: This resets the undo history.
2853
- * https://hyperformula.handsontable.com/guide/volatile-functions.html#volatile-actions
2854
- * @deprecated
2855
- */
2856
- // TODO(burdon): Remove.
2857
- recalculate() {
2858
- this._graph.hf.rebuildAndRecalculate();
2859
- }
2860
- insertRows(i, n = 1) {
2861
- this._insertIndices(this._sheet.rows, i, n, DEFAULT_ROWS);
2862
- this.reset();
2863
- }
2864
- insertColumns(i, n = 1) {
2865
- this._insertIndices(this._sheet.columns, i, n, DEFAULT_COLUMNS);
2866
- this.reset();
2867
- }
2868
- //
2869
- // Undoable actions.
2870
- // TODO(burdon): Group undoable methods; consistently update hf/sheet.
2871
- //
2872
- /**
2873
- * Clear range of values.
2874
- */
2875
- clear(range) {
2876
- const topLeft = getTopLeft(range);
2877
- const values = this._iterRange(range, () => null);
2878
- this._graph.hf.setCellContents(toSimpleCellAddress(this._sheetId, topLeft), values);
2879
- this._iterRange(range, (cell) => {
2880
- const idx = addressToIndex(this._sheet, cell);
2881
- delete this._sheet.cells[idx];
2882
- });
2883
- }
2884
- cut(range) {
2885
- this._graph.hf.cut(toModelRange(this._sheetId, range));
2886
- this._iterRange(range, (cell) => {
2887
- const idx = addressToIndex(this._sheet, cell);
2888
- delete this._sheet.cells[idx];
2889
- });
2890
- }
2891
- copy(range) {
2892
- this._graph.hf.copy(toModelRange(this._sheetId, range));
2893
- }
2894
- paste(cell) {
2895
- if (!this._graph.hf.isClipboardEmpty()) {
2896
- const changes = this._graph.hf.paste(toSimpleCellAddress(this._sheetId, cell));
2897
- for (const change of changes) {
2898
- if (change instanceof ExportedCellChange) {
2899
- const { address, newValue } = change;
2900
- const idx = addressToIndex(this._sheet, {
2901
- row: address.row,
2902
- column: address.col
2903
- });
2904
- this._sheet.cells[idx] = {
2905
- value: newValue
2906
- };
2907
- }
2908
- }
2909
- }
2910
- }
2911
- // TODO(burdon): Display undo/redo state.
2912
- undo() {
2913
- if (this._graph.hf.isThereSomethingToUndo()) {
2914
- this._graph.hf.undo();
2915
- this.update.emit();
2916
- }
2917
- }
2918
- redo() {
2919
- if (this._graph.hf.isThereSomethingToRedo()) {
2920
- this._graph.hf.redo();
2921
- this.update.emit();
2922
- }
2923
- }
2924
- /**
2925
- * Get value from sheet.
2926
- */
2927
- getCellValue(cell) {
2928
- const idx = addressToIndex(this._sheet, cell);
2929
- return this._sheet.cells[idx]?.value ?? null;
2930
- }
2931
- /**
2932
- * Get value as a string for editing.
2933
- */
2934
- getCellText(cell) {
2935
- const value = this.getCellValue(cell);
2936
- if (value == null) {
2937
- return void 0;
2938
- }
2939
- if (typeof value === "string" && value.charAt(0) === "=") {
2940
- return this.mapFormulaBindingFromId(this.mapFormulaIndicesToRefs(value));
2941
- } else {
2942
- return String(value);
2943
- }
2944
- }
2945
- /**
2946
- * Get array of raw values from sheet.
2947
- */
2948
- getCellValues(range) {
2949
- return this._iterRange(range, (cell) => this.getCellValue(cell));
2950
- }
2951
- /**
2952
- * Gets the regular or computed value from the engine.
2953
- */
2954
- getValue(cell) {
2955
- const value = this._graph.hf.getCellValue(toSimpleCellAddress(this._sheetId, cell));
2956
- if (value instanceof DetailedCellError) {
2957
- return value.toString();
2958
- }
2959
- return value;
2960
- }
2961
- /**
2962
- * Get value type.
2963
- */
2964
- getValueType(cell) {
2965
- const addr = toSimpleCellAddress(this._sheetId, cell);
2966
- const type = this._graph.hf.getCellValueDetailedType(addr);
2967
- return typeMap[type];
2968
- }
2969
- /**
2970
- * Sets the value, updating the sheet and engine.
2971
- */
2972
- setValue(cell, value) {
2973
- if (this._options.readonly) {
2974
- throw new ReadonlyException();
2975
- }
2976
- let refresh = false;
2977
- if (cell.row >= this._sheet.rows.length) {
2978
- this._insertIndices(this._sheet.rows, cell.row, 1, DEFAULT_ROWS);
2979
- refresh = true;
2980
- }
2981
- if (cell.column >= this._sheet.columns.length) {
2982
- this._insertIndices(this._sheet.columns, cell.column, 1, DEFAULT_COLUMNS);
2983
- refresh = true;
2984
- }
2985
- if (refresh) {
2986
- this.reset();
2987
- }
2988
- this._graph.hf.setCellContents({
2989
- sheet: this._sheetId,
2990
- row: cell.row,
2991
- col: cell.column
2992
- }, [
2993
- [
2994
- typeof value === "string" && value.charAt(0) === "=" ? this.mapFormulaBindingToFormula(value) : value
2784
+ A: [
2785
+ "sheetId !== undefined",
2786
+ ""
2995
2787
  ]
2996
- ]);
2997
- const idx = addressToIndex(this._sheet, cell);
2998
- if (value === void 0 || value === null) {
2999
- delete this._sheet.cells[idx];
3000
- } else {
3001
- if (typeof value === "string" && value.charAt(0) === "=") {
3002
- value = this.mapFormulaBindingToId(this.mapFormulaRefsToIndices(value));
3003
- }
3004
- this._sheet.cells[idx] = {
3005
- value
3006
- };
3007
- }
3008
- }
3009
- /**
3010
- * Sets values from a simple map.
3011
- */
3012
- setValues(values) {
3013
- Object.entries(values).forEach(([key, { value }]) => {
3014
- this.setValue(addressFromA1Notation(key), value);
3015
2788
  });
2789
+ const node = new ComputeNode(this, sheetId);
2790
+ this._nodes.set(sheetId, node);
2791
+ return node;
3016
2792
  }
3017
2793
  /**
3018
- * Iterate range.
2794
+ * Map bound value to custom function invocation.
2795
+ * E.g., "HELLO(...args)" => "EDGE("HELLO", ...args)".
3019
2796
  */
3020
- _iterRange(range, cb) {
3021
- const to = range.to ?? range.from;
3022
- const rowRange = [
3023
- Math.min(range.from.row, to.row),
3024
- Math.max(range.from.row, to.row)
3025
- ];
3026
- const columnRange = [
3027
- Math.min(range.from.column, to.column),
3028
- Math.max(range.from.column, to.column)
3029
- ];
3030
- const rows = [];
3031
- for (let row = rowRange[0]; row <= rowRange[1]; row++) {
3032
- const rowCells = [];
3033
- for (let column = columnRange[0]; column <= columnRange[1]; column++) {
3034
- const value = cb({
3035
- row,
3036
- column
3037
- });
3038
- if (value !== void 0) {
3039
- rowCells.push(value);
2797
+ mapFormulaToNative(formula) {
2798
+ return formula.replace(/['"]?([ \w]+)['"]?!/, (_match, name) => {
2799
+ if (name) {
2800
+ const objects = this._hf.getSheetNames().map((name2) => {
2801
+ const { type, id } = parseSheetName(name2);
2802
+ return type && id ? this._space?.db.getObjectById(id) : void 0;
2803
+ }).filter(nonNullable2);
2804
+ for (const obj of objects) {
2805
+ if (obj.name === name) {
2806
+ const type = getTypename(obj);
2807
+ return `'${createSheetName({
2808
+ type,
2809
+ id: obj.id
2810
+ })}'!`;
2811
+ }
3040
2812
  }
3041
2813
  }
3042
- rows.push(rowCells);
3043
- }
3044
- return rows;
3045
- }
3046
- /**
3047
- *
3048
- */
3049
- // TODO(burdon): Insert indices into sheet.
3050
- _insertIndices(indices, i, n, max) {
3051
- if (i + n > max) {
3052
- throw new RangeException(i + n);
3053
- }
3054
- const idx = createIndices(n);
3055
- indices.splice(i, 0, ...idx);
3056
- }
3057
- // TODO(burdon): Delete index.
3058
- _deleteIndices(indices, i, n) {
3059
- throw new Error("Not implemented");
3060
- }
3061
- // TODO(burdon): Move. Cannot use fractional without changing. Switch back to using unique IDs?
3062
- _moveIndices(indices, i, j, n) {
3063
- throw new Error("Not implemented");
3064
- }
3065
- //
3066
- // Indices.
3067
- //
3068
- /**
3069
- * E.g., "HELLO()" => "EDGE("HELLO")".
3070
- */
3071
- mapFormulaBindingToFormula(formula) {
3072
- return formula.replace(/([a-zA-Z0-9]+)\((.*)\)/g, (match, binding, args) => {
3073
- const fn = this._functions.find((fn2) => fn2.binding === binding);
2814
+ return `${name}!`;
2815
+ }).replace(/(\w+)\((.*)\)/g, (match, binding, args) => {
2816
+ const fn = this._remoteFunctions.find((fn2) => fn2.binding === binding);
3074
2817
  if (!fn) {
3075
2818
  return match;
3076
2819
  }
3077
2820
  if (args.trim() === "") {
3078
- return `EDGE("${binding}")`;
2821
+ return `${EDGE_FUNCTION_NAME}("${binding}")`;
2822
+ } else {
2823
+ return `${EDGE_FUNCTION_NAME}("${binding}", ${args})`;
3079
2824
  }
3080
- return `EDGE("${binding}", ${args})`;
3081
2825
  });
3082
2826
  }
3083
2827
  /**
3084
- * E.g., "EDGE("HELLO")" => "HELLO()".
2828
+ * Map from binding to fully qualified ECHO ID (to store).
2829
+ * E.g., HELLO() => spaceId:objectId()
3085
2830
  */
3086
- mapFormulaBindingFromFormula(formula) {
3087
- return formula.replace(/EDGE\("([a-zA-Z0-9]+)"(.*)\)/, (_match, binding, args) => {
3088
- if (args.trim() === "") {
3089
- return `${binding}()`;
2831
+ mapFunctionBindingToId(formula) {
2832
+ return formula.replace(/(\w+)\((.*)\)/g, (match, binding, args) => {
2833
+ if (binding === EDGE_FUNCTION_NAME || defaultFunctions.find((fn2) => fn2.name === binding)) {
2834
+ return match;
2835
+ }
2836
+ const fn = this._remoteFunctions.find((fn2) => fn2.binding === binding);
2837
+ if (fn) {
2838
+ const id = fullyQualifiedId(fn);
2839
+ return `${id}(${args})`;
2840
+ } else {
2841
+ return match;
3090
2842
  }
3091
- return `${binding}(${args.slice(2)})`;
3092
- });
3093
- }
3094
- /**
3095
- * Map from binding to fully qualified ECHO ID.
3096
- */
3097
- mapFormulaBindingToId(formula) {
3098
- return this._options.mapFormulaBindingToId(this._functions)(formula);
3099
- }
3100
- /**
3101
- * Map from fully qualified ECHO ID to binding.
3102
- */
3103
- mapFormulaBindingFromId(formula) {
3104
- return this._options.mapFormulaBindingFromId(this._functions)(formula);
3105
- }
3106
- /**
3107
- * Map from A1 notation to indices.
3108
- */
3109
- mapFormulaRefsToIndices(formula) {
3110
- invariant2(formula.charAt(0) === "=", void 0, {
3111
- F: __dxlog_file4,
3112
- L: 475,
3113
- S: this,
3114
- A: [
3115
- "formula.charAt(0) === '='",
3116
- ""
3117
- ]
3118
- });
3119
- return formula.replace(/([a-zA-Z]+)([0-9]+)/g, (match) => {
3120
- return addressToIndex(this._sheet, addressFromA1Notation(match));
3121
2843
  });
3122
2844
  }
3123
2845
  /**
3124
- * Map from indices to A1 notation.
2846
+ * Map from fully qualified ECHO ID to binding (from store).
2847
+ * E.g., spaceId:objectId() => HELLO()
3125
2848
  */
3126
- mapFormulaIndicesToRefs(formula) {
3127
- invariant2(formula.charAt(0) === "=", void 0, {
3128
- F: __dxlog_file4,
3129
- L: 485,
3130
- S: this,
3131
- A: [
3132
- "formula.charAt(0) === '='",
3133
- ""
3134
- ]
3135
- });
3136
- return formula.replace(/([a-zA-Z0-9]+)@([a-zA-Z0-9]+)/g, (idx) => {
3137
- return addressToA1Notation(addressFromIndex(this._sheet, idx));
2849
+ mapFunctionBindingFromId(formula) {
2850
+ return formula.replace(/(\w+):([a-zA-Z0-9]+)\((.*)\)/g, (match, spaceId, objectId, args) => {
2851
+ const id = `${spaceId}:${objectId}`;
2852
+ if (id.length !== OBJECT_ID_LENGTH) {
2853
+ return match;
2854
+ }
2855
+ const fn = this._remoteFunctions.find((fn2) => fullyQualifiedId(fn2) === id);
2856
+ if (fn?.binding) {
2857
+ return `${fn.binding}(${args})`;
2858
+ } else {
2859
+ return match;
2860
+ }
3138
2861
  });
3139
2862
  }
3140
- //
3141
- // Values
3142
- //
3143
- /**
3144
- * https://hyperformula.handsontable.com/guide/date-and-time-handling.html#example
3145
- * https://hyperformula.handsontable.com/api/interfaces/configparams.html#nulldate
3146
- * NOTE: TODAY() is number of FULL days since nullDate. It will typically be -1 days from NOW().
3147
- */
3148
- toLocalDate(num) {
3149
- const { year, month, day, hours, minutes, seconds } = this.toDateTime(num);
3150
- return new Date(year, month - 1, day, hours, minutes, seconds);
3151
- }
3152
- toDateTime(num) {
3153
- return this._graph.hf.numberToDateTime(num);
3154
- }
3155
- toDate(num) {
3156
- return this._graph.hf.numberToDate(num);
2863
+ async _open() {
2864
+ if (this._space) {
2865
+ const query = this._space.db.query(Filter2.schema(FunctionType2));
2866
+ const unsubscribe = query.subscribe(({ objects }) => {
2867
+ this._remoteFunctions = objects.filter(({ binding }) => binding);
2868
+ this.update.emit({
2869
+ type: "functionsUpdated"
2870
+ });
2871
+ });
2872
+ this._ctx.onDispose(unsubscribe);
2873
+ }
3157
2874
  }
3158
- toTime(num) {
3159
- return this._graph.hf.numberToTime(num);
2875
+ async _close() {
2876
+ for (const node of this._nodes.values()) {
2877
+ await node.close();
2878
+ }
3160
2879
  }
3161
2880
  };
3162
2881
 
3163
- // packages/plugins/plugin-sheet/src/components/ComputeGraph/custom.ts
3164
- import { FunctionArgumentType } from "hyperformula";
3165
- import { getDeep } from "@dxos/util";
3166
- var parseNumberString = (str) => {
3167
- return parseFloat(str.replace(/[^\d.]/g, ""));
2882
+ // packages/plugins/plugin-sheet/src/graph/compute-graph-registry.ts
2883
+ import defaultsDeep2 from "lodash.defaultsdeep";
2884
+ import { Resource as Resource3 } from "@dxos/context";
2885
+ import { invariant as invariant2 } from "@dxos/invariant";
2886
+ import { log as log4 } from "@dxos/log";
2887
+ import { HyperFormula } from "#hyperformula";
2888
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/plugins/plugin-sheet/src/graph/compute-graph-registry.ts";
2889
+ var defaultOptions = {
2890
+ licenseKey: "gpl-v3"
3168
2891
  };
3169
- var CustomPlugin = class extends FunctionPluginAsync {
3170
- test(ast, state) {
3171
- const handler = async () => {
3172
- return Math.random();
3173
- };
3174
- return this.runAsyncFunction(ast, state, handler);
2892
+ var defaultPlugins = [
2893
+ {
2894
+ plugin: EdgeFunctionPlugin,
2895
+ translations: EdgeFunctionPluginTranslations
3175
2896
  }
3176
- crypto(ast, state) {
3177
- const handler = async (_currency) => {
3178
- const currency = (_currency || "USD").toUpperCase();
3179
- const result = await fetch(`https://api.coindesk.com/v1/bpi/currentprice/${currency}.json`);
3180
- const data = await result.json();
3181
- const rate = getDeep(data, [
3182
- "bpi",
3183
- currency,
3184
- "rate"
3185
- ]);
3186
- if (!rate) {
3187
- return NaN;
3188
- }
3189
- return parseNumberString(rate);
3190
- };
3191
- return this.runAsyncFunction(ast, state, handler, {
3192
- ttl: 1e4
2897
+ ];
2898
+ var ComputeGraphRegistry = class extends Resource3 {
2899
+ constructor(options = {
2900
+ plugins: defaultPlugins
2901
+ }) {
2902
+ super();
2903
+ this._graphs = /* @__PURE__ */ new Map();
2904
+ this._options = defaultsDeep2({}, options, defaultOptions);
2905
+ this._options.plugins?.forEach(({ plugin, translations }) => {
2906
+ HyperFormula.registerFunctionPlugin(plugin, translations);
3193
2907
  });
3194
2908
  }
3195
- };
3196
- CustomPlugin.implementedFunctions = {
3197
- TEST: {
3198
- method: "test",
3199
- parameters: [],
3200
- isVolatile: true
3201
- },
3202
- CRYPTO: {
3203
- method: "crypto",
3204
- parameters: [
3205
- {
3206
- argumentType: FunctionArgumentType.STRING,
3207
- optionalArg: true
3208
- }
3209
- ],
3210
- isVolatile: true
2909
+ getGraph(spaceId) {
2910
+ return this._graphs.get(spaceId);
3211
2911
  }
3212
- };
3213
- var CustomPluginTranslations = {
3214
- enGB: {
3215
- TEST: "TEST",
3216
- CRYPTO: "CRYPTO"
3217
- },
3218
- enUS: {
3219
- TEST: "TEST",
3220
- CRYPTO: "CRYPTO"
2912
+ getOrCreateGraph(space) {
2913
+ let graph = this._graphs.get(space.id);
2914
+ if (!graph) {
2915
+ log4("create graph", {
2916
+ space: space.id
2917
+ }, {
2918
+ F: __dxlog_file4,
2919
+ L: 69,
2920
+ S: this,
2921
+ C: (f, a) => f(...a)
2922
+ });
2923
+ graph = this.createGraph(space);
2924
+ }
2925
+ return graph;
2926
+ }
2927
+ createGraph(space) {
2928
+ invariant2(!this._graphs.has(space.id), `ComputeGraph already exists for space: ${space.id}`, {
2929
+ F: __dxlog_file4,
2930
+ L: 77,
2931
+ S: this,
2932
+ A: [
2933
+ "!this._graphs.has(space.id)",
2934
+ "`ComputeGraph already exists for space: ${space.id}`"
2935
+ ]
2936
+ });
2937
+ const hf = HyperFormula.buildEmpty(this._options);
2938
+ const graph = new ComputeGraph(hf, space, this._options);
2939
+ this._graphs.set(space.id, graph);
2940
+ return graph;
2941
+ }
2942
+ async _close() {
2943
+ for (const graph of this._graphs.values()) {
2944
+ await graph.close();
2945
+ }
2946
+ this._graphs.clear();
3221
2947
  }
3222
2948
  };
3223
2949
 
3224
2950
  export {
3225
- FunctionPluginAsync,
3226
- CustomPlugin,
3227
- CustomPluginTranslations,
3228
- createComputeGraph,
2951
+ ComputeNode,
2952
+ defaultFunctionContextOptions,
2953
+ FunctionContext,
2954
+ AsyncFunctionPlugin,
2955
+ EDGE_FUNCTION_NAME,
2956
+ EdgeFunctionPlugin,
2957
+ EdgeFunctionPluginTranslations,
3229
2958
  defaultFunctions,
3230
- posEquals,
3231
- columnLetter,
3232
- addressToA1Notation,
3233
- addressFromA1Notation,
3234
- rangeToA1Notation,
3235
- inRange,
3236
- addressToIndex,
3237
- addressFromIndex,
3238
- rangeFromIndex,
3239
- closest,
3240
- compareIndexPositions,
3241
- SheetModel
2959
+ createSheetName,
2960
+ parseSheetName,
2961
+ ComputeGraph,
2962
+ defaultOptions,
2963
+ defaultPlugins,
2964
+ ComputeGraphRegistry
3242
2965
  };
3243
- //# sourceMappingURL=chunk-GNNVBNCX.mjs.map
2966
+ //# sourceMappingURL=chunk-GSV5QNLD.mjs.map