@dxos/plugin-sheet 0.6.13-main.ed424a1 → 0.6.13
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.
- package/dist/lib/browser/SheetContainer-U4H5D34A.mjs +1772 -0
- package/dist/lib/browser/SheetContainer-U4H5D34A.mjs.map +7 -0
- package/dist/lib/browser/chunk-APHOLYUB.mjs +175 -0
- package/dist/lib/browser/chunk-APHOLYUB.mjs.map +7 -0
- package/dist/lib/{node-esm/chunk-5WPZCXNS.mjs → browser/chunk-D5AGLXJP.mjs} +678 -411
- package/dist/lib/browser/chunk-D5AGLXJP.mjs.map +7 -0
- package/dist/lib/browser/chunk-FUAGSXA4.mjs +82 -0
- package/dist/lib/browser/chunk-FUAGSXA4.mjs.map +7 -0
- package/dist/lib/browser/chunk-JRL5LGCE.mjs +18 -0
- package/dist/lib/browser/chunk-JRL5LGCE.mjs.map +7 -0
- package/dist/lib/browser/chunk-NU4PBN33.mjs +8 -0
- package/dist/lib/browser/chunk-NU4PBN33.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +62 -93
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/meta.mjs +1 -1
- package/dist/lib/browser/testing.mjs +92 -0
- package/dist/lib/browser/testing.mjs.map +7 -0
- package/dist/lib/browser/types.mjs +12 -6
- package/dist/lib/node/SheetContainer-AXQV3ZT5.cjs +1765 -0
- package/dist/lib/node/SheetContainer-AXQV3ZT5.cjs.map +7 -0
- package/dist/lib/node/{chunk-5XPK2V4A.cjs → chunk-5KKJ4NPP.cjs} +678 -418
- package/dist/lib/node/chunk-5KKJ4NPP.cjs.map +7 -0
- package/dist/lib/node/chunk-BJ6ZD7MN.cjs +51 -0
- package/dist/lib/node/chunk-BJ6ZD7MN.cjs.map +7 -0
- package/dist/lib/node/chunk-CN3RPESU.cjs +202 -0
- package/dist/lib/node/chunk-CN3RPESU.cjs.map +7 -0
- package/dist/lib/node/{chunk-NZARD7UP.cjs → chunk-DSYKOI4E.cjs} +51 -40
- package/dist/lib/node/chunk-DSYKOI4E.cjs.map +7 -0
- package/dist/lib/node/chunk-PYXHNAAK.cjs +40 -0
- package/dist/lib/node/chunk-PYXHNAAK.cjs.map +7 -0
- package/dist/lib/node/index.cjs +69 -105
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.cjs +3 -3
- package/dist/lib/node/meta.cjs.map +1 -1
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing.cjs +111 -0
- package/dist/lib/node/testing.cjs.map +7 -0
- package/dist/lib/node/types.cjs +15 -9
- package/dist/lib/node/types.cjs.map +2 -2
- package/dist/types/src/SheetPlugin.d.ts.map +1 -1
- package/dist/types/src/components/CellEditor/CellEditor.d.ts +14 -0
- package/dist/types/src/components/CellEditor/CellEditor.d.ts.map +1 -0
- package/dist/types/src/components/{GridSheet/SheetCellEditor.stories.d.ts → CellEditor/CellEditor.stories.d.ts} +4 -4
- package/dist/types/src/components/CellEditor/CellEditor.stories.d.ts.map +1 -0
- package/dist/types/src/{extensions/editor → components/CellEditor}/extension.d.ts +1 -1
- package/dist/types/src/components/CellEditor/extension.d.ts.map +1 -0
- package/dist/types/src/components/CellEditor/extension.test.d.ts.map +1 -0
- package/dist/types/src/components/CellEditor/index.d.ts +3 -0
- package/dist/types/src/components/CellEditor/index.d.ts.map +1 -0
- package/dist/types/src/{graph/functions → components/ComputeGraph}/async-function.d.ts +5 -14
- package/dist/types/src/components/ComputeGraph/async-function.d.ts.map +1 -0
- package/dist/types/src/components/ComputeGraph/custom.d.ts +21 -0
- package/dist/types/src/components/ComputeGraph/custom.d.ts.map +1 -0
- package/dist/types/src/components/ComputeGraph/edge-function.d.ts +20 -0
- package/dist/types/src/components/ComputeGraph/edge-function.d.ts.map +1 -0
- package/dist/types/src/components/ComputeGraph/graph-context.d.ts +12 -0
- package/dist/types/src/components/ComputeGraph/graph-context.d.ts.map +1 -0
- package/dist/types/src/components/ComputeGraph/graph.browser.test.d.ts +2 -0
- package/dist/types/src/components/ComputeGraph/graph.browser.test.d.ts.map +1 -0
- package/dist/types/src/components/ComputeGraph/graph.d.ts +26 -0
- package/dist/types/src/components/ComputeGraph/graph.d.ts.map +1 -0
- package/dist/types/src/components/ComputeGraph/index.d.ts +3 -1
- package/dist/types/src/components/ComputeGraph/index.d.ts.map +1 -1
- package/dist/types/src/components/Sheet/Sheet.d.ts +55 -0
- package/dist/types/src/components/Sheet/Sheet.d.ts.map +1 -0
- package/dist/types/src/components/Sheet/Sheet.stories.d.ts +54 -0
- package/dist/types/src/components/Sheet/Sheet.stories.d.ts.map +1 -0
- package/dist/types/src/components/Sheet/formatting.d.ts +14 -0
- package/dist/types/src/components/Sheet/formatting.d.ts.map +1 -0
- package/dist/types/src/components/Sheet/grid.d.ts +52 -0
- package/dist/types/src/components/Sheet/grid.d.ts.map +1 -0
- package/dist/types/src/components/Sheet/index.d.ts +2 -0
- package/dist/types/src/components/Sheet/index.d.ts.map +1 -0
- package/dist/types/src/components/Sheet/nav.d.ts +29 -0
- package/dist/types/src/components/Sheet/nav.d.ts.map +1 -0
- package/dist/types/src/components/Sheet/sheet-context.d.ts +25 -0
- package/dist/types/src/components/Sheet/sheet-context.d.ts.map +1 -0
- package/dist/types/src/components/Sheet/util.d.ts +18 -0
- package/dist/types/src/components/Sheet/util.d.ts.map +1 -0
- package/dist/types/src/components/SheetContainer.d.ts +9 -0
- package/dist/types/src/components/SheetContainer.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/Toolbar.d.ts +9 -33
- package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -1
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +15 -19
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -1
- package/dist/types/src/components/Toolbar/common.d.ts +20 -0
- package/dist/types/src/components/Toolbar/common.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +2 -3
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/meta.d.ts +9 -3
- package/dist/types/src/meta.d.ts.map +1 -1
- package/dist/types/src/{graph/functions/function-defs.d.ts → model/functions.d.ts} +1 -1
- package/dist/types/src/model/functions.d.ts.map +1 -0
- package/dist/types/src/model/index.d.ts +3 -2
- package/dist/types/src/model/index.d.ts.map +1 -1
- package/dist/types/src/model/model.browser.test.d.ts +2 -0
- package/dist/types/src/model/model.browser.test.d.ts.map +1 -0
- package/dist/types/src/model/{sheet-model.d.ts → model.d.ts} +67 -13
- package/dist/types/src/model/model.d.ts.map +1 -0
- package/dist/types/src/{defs → model}/types.d.ts +3 -8
- package/dist/types/src/model/types.d.ts.map +1 -0
- package/dist/types/src/model/types.test.d.ts.map +1 -0
- package/dist/types/src/model/util.d.ts +15 -0
- package/dist/types/src/model/util.d.ts.map +1 -0
- package/dist/types/src/testing.d.ts +9 -0
- package/dist/types/src/testing.d.ts.map +1 -0
- package/dist/types/src/translations.d.ts +12 -17
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +40 -125
- package/dist/types/src/types.d.ts.map +1 -1
- package/package.json +52 -58
- package/src/SheetPlugin.tsx +74 -52
- package/src/components/{GridSheet/SheetCellEditor.stories.tsx → CellEditor/CellEditor.stories.tsx} +8 -8
- package/src/components/CellEditor/CellEditor.tsx +113 -0
- package/src/{extensions/editor → components/CellEditor}/extension.test.ts +6 -4
- package/src/{extensions/editor → components/CellEditor}/extension.ts +6 -5
- package/src/{extensions/editor → components/CellEditor}/index.ts +1 -0
- package/src/{graph/functions → components/ComputeGraph}/async-function.ts +15 -25
- package/src/{graph/testing/test-plugin.ts → components/ComputeGraph/custom.ts} +14 -44
- package/src/{graph/functions → components/ComputeGraph}/edge-function.ts +14 -16
- package/src/components/ComputeGraph/graph-context.tsx +50 -0
- package/src/components/ComputeGraph/graph.browser.test.ts +50 -0
- package/src/components/ComputeGraph/graph.ts +62 -0
- package/src/components/ComputeGraph/index.ts +3 -1
- package/src/components/Sheet/Sheet.stories.tsx +287 -0
- package/src/components/Sheet/Sheet.tsx +1160 -0
- package/src/components/Sheet/formatting.ts +106 -0
- package/src/components/Sheet/grid.ts +191 -0
- package/src/{testing → components/Sheet}/index.ts +1 -1
- package/src/components/Sheet/nav.ts +157 -0
- package/src/components/Sheet/sheet-context.tsx +150 -0
- package/src/components/Sheet/util.ts +56 -0
- package/src/components/SheetContainer.tsx +34 -0
- package/src/components/Toolbar/Toolbar.tsx +112 -242
- package/src/components/Toolbar/common.tsx +72 -0
- package/src/components/index.ts +0 -2
- package/src/meta.tsx +18 -0
- package/src/model/index.ts +3 -2
- package/src/model/model.browser.test.ts +100 -0
- package/src/model/model.ts +550 -0
- package/src/{defs → model}/types.test.ts +9 -8
- package/src/{defs → model}/types.ts +14 -24
- package/src/model/util.ts +36 -0
- package/src/testing.ts +50 -0
- package/src/translations.ts +1 -6
- package/src/types.ts +47 -40
- package/dist/lib/browser/SheetContainer-RVRACGCZ.mjs +0 -290
- package/dist/lib/browser/SheetContainer-RVRACGCZ.mjs.map +0 -7
- package/dist/lib/browser/chunk-BWN5DZWZ.mjs +0 -74
- package/dist/lib/browser/chunk-BWN5DZWZ.mjs.map +0 -7
- package/dist/lib/browser/chunk-D3QTX46O.mjs +0 -14
- package/dist/lib/browser/chunk-D3QTX46O.mjs.map +0 -7
- package/dist/lib/browser/chunk-GSV5QNLD.mjs +0 -2966
- package/dist/lib/browser/chunk-GSV5QNLD.mjs.map +0 -7
- package/dist/lib/browser/chunk-HOG37RYS.mjs +0 -1528
- package/dist/lib/browser/chunk-HOG37RYS.mjs.map +0 -7
- package/dist/lib/browser/graph-M4IQ76QX.mjs +0 -33
- package/dist/lib/browser/graph-M4IQ76QX.mjs.map +0 -7
- package/dist/lib/node/SheetContainer-J7XROAMN.cjs +0 -300
- package/dist/lib/node/SheetContainer-J7XROAMN.cjs.map +0 -7
- package/dist/lib/node/chunk-5XPK2V4A.cjs.map +0 -7
- package/dist/lib/node/chunk-AXQZA2YS.cjs +0 -1527
- package/dist/lib/node/chunk-AXQZA2YS.cjs.map +0 -7
- package/dist/lib/node/chunk-NZARD7UP.cjs.map +0 -7
- package/dist/lib/node/chunk-QIFIGEKV.cjs +0 -37
- package/dist/lib/node/chunk-QIFIGEKV.cjs.map +0 -7
- package/dist/lib/node/graph-Q3N2X26H.cjs +0 -55
- package/dist/lib/node/graph-Q3N2X26H.cjs.map +0 -7
- package/dist/lib/node-esm/SheetContainer-YWQVKHQB.mjs +0 -291
- package/dist/lib/node-esm/SheetContainer-YWQVKHQB.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-5WPZCXNS.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-BPXXIAOQ.mjs +0 -1529
- package/dist/lib/node-esm/chunk-BPXXIAOQ.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-VCYJWE3O.mjs +0 -16
- package/dist/lib/node-esm/chunk-VCYJWE3O.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-WFDTY3IC.mjs +0 -75
- package/dist/lib/node-esm/chunk-WFDTY3IC.mjs.map +0 -7
- package/dist/lib/node-esm/graph-SMPUMOV2.mjs +0 -34
- package/dist/lib/node-esm/graph-SMPUMOV2.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -280
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/meta.mjs +0 -10
- package/dist/lib/node-esm/meta.mjs.map +0 -7
- package/dist/lib/node-esm/types.mjs +0 -17
- package/dist/lib/node-esm/types.mjs.map +0 -7
- package/dist/types/src/components/ComputeGraph/ComputeGraphContextProvider.d.ts +0 -11
- package/dist/types/src/components/ComputeGraph/ComputeGraphContextProvider.d.ts.map +0 -1
- package/dist/types/src/components/FunctionEditor/FunctionEditor.d.ts +0 -3
- package/dist/types/src/components/FunctionEditor/FunctionEditor.d.ts.map +0 -1
- package/dist/types/src/components/FunctionEditor/index.d.ts +0 -2
- package/dist/types/src/components/FunctionEditor/index.d.ts.map +0 -1
- package/dist/types/src/components/GridSheet/GridSheet.d.ts +0 -3
- package/dist/types/src/components/GridSheet/GridSheet.d.ts.map +0 -1
- package/dist/types/src/components/GridSheet/GridSheet.stories.d.ts +0 -9
- package/dist/types/src/components/GridSheet/GridSheet.stories.d.ts.map +0 -1
- package/dist/types/src/components/GridSheet/SheetCellEditor.stories.d.ts.map +0 -1
- package/dist/types/src/components/GridSheet/index.d.ts +0 -2
- package/dist/types/src/components/GridSheet/index.d.ts.map +0 -1
- package/dist/types/src/components/GridSheet/util.d.ts +0 -16
- package/dist/types/src/components/GridSheet/util.d.ts.map +0 -1
- package/dist/types/src/components/SheetContainer/SheetContainer.d.ts +0 -6
- package/dist/types/src/components/SheetContainer/SheetContainer.d.ts.map +0 -1
- package/dist/types/src/components/SheetContainer/SheetContainer.stories.d.ts +0 -11
- package/dist/types/src/components/SheetContainer/SheetContainer.stories.d.ts.map +0 -1
- package/dist/types/src/components/SheetContainer/index.d.ts +0 -3
- package/dist/types/src/components/SheetContainer/index.d.ts.map +0 -1
- package/dist/types/src/components/SheetContext/SheetContext.d.ts +0 -26
- package/dist/types/src/components/SheetContext/SheetContext.d.ts.map +0 -1
- package/dist/types/src/components/SheetContext/index.d.ts +0 -2
- package/dist/types/src/components/SheetContext/index.d.ts.map +0 -1
- package/dist/types/src/defs/index.d.ts +0 -4
- package/dist/types/src/defs/index.d.ts.map +0 -1
- package/dist/types/src/defs/sheet-range-types.d.ts +0 -13
- package/dist/types/src/defs/sheet-range-types.d.ts.map +0 -1
- package/dist/types/src/defs/types.d.ts.map +0 -1
- package/dist/types/src/defs/types.test.d.ts.map +0 -1
- package/dist/types/src/defs/util.d.ts +0 -43
- package/dist/types/src/defs/util.d.ts.map +0 -1
- package/dist/types/src/extensions/compute.d.ts +0 -6
- package/dist/types/src/extensions/compute.d.ts.map +0 -1
- package/dist/types/src/extensions/compute.stories.d.ts +0 -26
- package/dist/types/src/extensions/compute.stories.d.ts.map +0 -1
- package/dist/types/src/extensions/editor/extension.d.ts.map +0 -1
- package/dist/types/src/extensions/editor/extension.test.d.ts.map +0 -1
- package/dist/types/src/extensions/editor/index.d.ts +0 -2
- package/dist/types/src/extensions/editor/index.d.ts.map +0 -1
- package/dist/types/src/extensions/index.d.ts +0 -3
- package/dist/types/src/extensions/index.d.ts.map +0 -1
- package/dist/types/src/graph/compute-graph-registry.d.ts +0 -34
- package/dist/types/src/graph/compute-graph-registry.d.ts.map +0 -1
- package/dist/types/src/graph/compute-graph.d.ts +0 -64
- package/dist/types/src/graph/compute-graph.d.ts.map +0 -1
- package/dist/types/src/graph/compute-graph.stories.d.ts +0 -10
- package/dist/types/src/graph/compute-graph.stories.d.ts.map +0 -1
- package/dist/types/src/graph/compute-graph.test.d.ts +0 -2
- package/dist/types/src/graph/compute-graph.test.d.ts.map +0 -1
- package/dist/types/src/graph/compute-node.d.ts +0 -26
- package/dist/types/src/graph/compute-node.d.ts.map +0 -1
- package/dist/types/src/graph/functions/async-function.d.ts.map +0 -1
- package/dist/types/src/graph/functions/edge-function.d.ts +0 -21
- package/dist/types/src/graph/functions/edge-function.d.ts.map +0 -1
- package/dist/types/src/graph/functions/function-defs.d.ts.map +0 -1
- package/dist/types/src/graph/functions/index.d.ts +0 -4
- package/dist/types/src/graph/functions/index.d.ts.map +0 -1
- package/dist/types/src/graph/hyperformula.test.d.ts +0 -2
- package/dist/types/src/graph/hyperformula.test.d.ts.map +0 -1
- package/dist/types/src/graph/index.d.ts +0 -5
- package/dist/types/src/graph/index.d.ts.map +0 -1
- package/dist/types/src/graph/testing/index.d.ts +0 -3
- package/dist/types/src/graph/testing/index.d.ts.map +0 -1
- package/dist/types/src/graph/testing/test-builder.d.ts +0 -15
- package/dist/types/src/graph/testing/test-builder.d.ts.map +0 -1
- package/dist/types/src/graph/testing/test-plugin.d.ts +0 -36
- package/dist/types/src/graph/testing/test-plugin.d.ts.map +0 -1
- package/dist/types/src/graph/util.d.ts +0 -2
- package/dist/types/src/graph/util.d.ts.map +0 -1
- package/dist/types/src/hooks/hooks.stories.d.ts +0 -11
- package/dist/types/src/hooks/hooks.stories.d.ts.map +0 -1
- package/dist/types/src/hooks/index.d.ts +0 -4
- package/dist/types/src/hooks/index.d.ts.map +0 -1
- package/dist/types/src/hooks/threads.d.ts +0 -8
- package/dist/types/src/hooks/threads.d.ts.map +0 -1
- package/dist/types/src/hooks/useComputeGraph.d.ts +0 -7
- package/dist/types/src/hooks/useComputeGraph.d.ts.map +0 -1
- package/dist/types/src/hooks/useSheetModel.d.ts +0 -8
- package/dist/types/src/hooks/useSheetModel.d.ts.map +0 -1
- package/dist/types/src/model/decorations.d.ts +0 -25
- package/dist/types/src/model/decorations.d.ts.map +0 -1
- package/dist/types/src/model/sheet-model.d.ts.map +0 -1
- package/dist/types/src/model/sheet-model.test.d.ts +0 -2
- package/dist/types/src/model/sheet-model.test.d.ts.map +0 -1
- package/dist/types/src/sanity.test.d.ts +0 -2
- package/dist/types/src/sanity.test.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -2
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/testing.d.ts +0 -8
- package/dist/types/src/testing/testing.d.ts.map +0 -1
- package/dist/vendor/hyperformula.mjs +0 -37145
- package/src/components/ComputeGraph/ComputeGraphContextProvider.tsx +0 -20
- package/src/components/FunctionEditor/FunctionEditor.tsx +0 -45
- package/src/components/FunctionEditor/index.ts +0 -5
- package/src/components/GridSheet/GridSheet.stories.tsx +0 -41
- package/src/components/GridSheet/GridSheet.tsx +0 -163
- package/src/components/GridSheet/index.ts +0 -5
- package/src/components/GridSheet/util.ts +0 -158
- package/src/components/SheetContainer/SheetContainer.stories.tsx +0 -40
- package/src/components/SheetContainer/SheetContainer.tsx +0 -27
- package/src/components/SheetContainer/index.ts +0 -7
- package/src/components/SheetContext/SheetContext.tsx +0 -104
- package/src/components/SheetContext/index.ts +0 -5
- package/src/defs/index.ts +0 -7
- package/src/defs/sheet-range-types.ts +0 -46
- package/src/defs/util.ts +0 -151
- package/src/extensions/compute.stories.tsx +0 -151
- package/src/extensions/compute.ts +0 -147
- package/src/extensions/index.ts +0 -6
- package/src/graph/compute-graph-registry.ts +0 -90
- package/src/graph/compute-graph.stories.tsx +0 -93
- package/src/graph/compute-graph.test.ts +0 -87
- package/src/graph/compute-graph.ts +0 -242
- package/src/graph/compute-node.ts +0 -63
- package/src/graph/functions/index.ts +0 -7
- package/src/graph/hyperformula.test.ts +0 -14
- package/src/graph/index.ts +0 -8
- package/src/graph/testing/index.ts +0 -6
- package/src/graph/testing/test-builder.ts +0 -54
- package/src/graph/util.ts +0 -8
- package/src/hooks/hooks.stories.tsx +0 -50
- package/src/hooks/index.ts +0 -7
- package/src/hooks/threads.ts +0 -147
- package/src/hooks/useComputeGraph.ts +0 -28
- package/src/hooks/useSheetModel.ts +0 -40
- package/src/meta.ts +0 -14
- package/src/model/decorations.ts +0 -64
- package/src/model/sheet-model.test.ts +0 -57
- package/src/model/sheet-model.ts +0 -418
- package/src/sanity.test.ts +0 -40
- package/src/testing/testing.tsx +0 -68
- /package/dist/types/src/{extensions/editor → components/CellEditor}/extension.test.d.ts +0 -0
- /package/dist/types/src/{defs → model}/types.test.d.ts +0 -0
- /package/src/{graph/functions/function-defs.ts → model/functions.ts} +0 -0
|
@@ -0,0 +1,550 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { DetailedCellError, ExportedCellChange } from 'hyperformula';
|
|
6
|
+
import { type SimpleCellRange } from 'hyperformula/typings/AbsoluteCellRange';
|
|
7
|
+
import { type SimpleCellAddress } from 'hyperformula/typings/Cell';
|
|
8
|
+
import { type SimpleDate, type SimpleDateTime } from 'hyperformula/typings/DateTimeHelper';
|
|
9
|
+
|
|
10
|
+
import { Event } from '@dxos/async';
|
|
11
|
+
import { type Space } from '@dxos/client/echo';
|
|
12
|
+
import { Context } from '@dxos/context';
|
|
13
|
+
import { invariant } from '@dxos/invariant';
|
|
14
|
+
import { PublicKey } from '@dxos/keys';
|
|
15
|
+
import { log } from '@dxos/log';
|
|
16
|
+
import { type FunctionType } from '@dxos/plugin-script/types';
|
|
17
|
+
|
|
18
|
+
import { defaultFunctions, type FunctionDefinition } from './functions';
|
|
19
|
+
import { addressFromA1Notation, addressToA1Notation, type CellAddress, type CellRange } from './types';
|
|
20
|
+
import { createIndices, RangeException, ReadonlyException } from './util';
|
|
21
|
+
import { type ComputeGraph } from '../components';
|
|
22
|
+
import { type CellScalarValue, type CellValue, type SheetType, ValueTypeEnum } from '../types';
|
|
23
|
+
|
|
24
|
+
const DEFAULT_ROWS = 100;
|
|
25
|
+
const DEFAULT_COLUMNS = 26;
|
|
26
|
+
|
|
27
|
+
export type CellIndex = string;
|
|
28
|
+
|
|
29
|
+
export type CellContentValue = number | string | boolean | null;
|
|
30
|
+
|
|
31
|
+
export type SheetModelOptions = {
|
|
32
|
+
readonly?: boolean;
|
|
33
|
+
rows: number;
|
|
34
|
+
columns: number;
|
|
35
|
+
mapFormulaBindingToId: (functions: FunctionType[]) => (formula: string) => string;
|
|
36
|
+
mapFormulaBindingFromId: (functions: FunctionType[]) => (formula: string) => string;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const typeMap: Record<string, ValueTypeEnum> = {
|
|
40
|
+
BOOLEAN: ValueTypeEnum.Boolean,
|
|
41
|
+
NUMBER_RAW: ValueTypeEnum.Number,
|
|
42
|
+
NUMBER_PERCENT: ValueTypeEnum.Percent,
|
|
43
|
+
NUMBER_CURRENCY: ValueTypeEnum.Currency,
|
|
44
|
+
NUMBER_DATETIME: ValueTypeEnum.DateTime,
|
|
45
|
+
NUMBER_DATE: ValueTypeEnum.Date,
|
|
46
|
+
NUMBER_TIME: ValueTypeEnum.Time,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const defaultOptions: SheetModelOptions = {
|
|
50
|
+
rows: 50,
|
|
51
|
+
columns: 26,
|
|
52
|
+
mapFormulaBindingFromId: () => (formula) => formula,
|
|
53
|
+
mapFormulaBindingToId: () => (formula) => formula,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const getTopLeft = (range: CellRange) => {
|
|
57
|
+
const to = range.to ?? range.from;
|
|
58
|
+
return { row: Math.min(range.from.row, to.row), column: Math.min(range.from.column, to.column) };
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const toSimpleCellAddress = (sheet: number, cell: CellAddress): SimpleCellAddress => ({
|
|
62
|
+
sheet,
|
|
63
|
+
row: cell.row,
|
|
64
|
+
col: cell.column,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const toModelRange = (sheet: number, range: CellRange): SimpleCellRange => ({
|
|
68
|
+
start: toSimpleCellAddress(sheet, range.from),
|
|
69
|
+
end: toSimpleCellAddress(sheet, range.to ?? range.from),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Spreadsheet data model.
|
|
74
|
+
*
|
|
75
|
+
* [ComputeGraphContext] > [SheetContext]:[SheetModel] > [Sheet.Root]
|
|
76
|
+
*/
|
|
77
|
+
export class SheetModel {
|
|
78
|
+
public readonly id = `model-${PublicKey.random().truncate()}`;
|
|
79
|
+
private _ctx?: Context = undefined;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Formula engine.
|
|
83
|
+
* Acts as a write through cache for scalar and computed values.
|
|
84
|
+
*/
|
|
85
|
+
private readonly _sheetId: number;
|
|
86
|
+
private readonly _options: SheetModelOptions;
|
|
87
|
+
private _functions: FunctionType[] = [];
|
|
88
|
+
|
|
89
|
+
public readonly update = new Event();
|
|
90
|
+
|
|
91
|
+
constructor(
|
|
92
|
+
private readonly _graph: ComputeGraph,
|
|
93
|
+
private readonly _sheet: SheetType,
|
|
94
|
+
private readonly _space?: Space,
|
|
95
|
+
options: Partial<SheetModelOptions> = {},
|
|
96
|
+
) {
|
|
97
|
+
// Sheet for this object.
|
|
98
|
+
const name = this._sheet.id;
|
|
99
|
+
if (!this._graph.hf.doesSheetExist(name)) {
|
|
100
|
+
this._graph.hf.addSheet(name);
|
|
101
|
+
}
|
|
102
|
+
this._sheetId = this._graph.hf.getSheetId(name)!;
|
|
103
|
+
this._options = { ...defaultOptions, ...options };
|
|
104
|
+
this.reset();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
get graph() {
|
|
108
|
+
return this._graph;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
get sheet() {
|
|
112
|
+
return this._sheet;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
get readonly() {
|
|
116
|
+
return this._options.readonly;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
get bounds() {
|
|
120
|
+
return {
|
|
121
|
+
rows: this._sheet.rows.length,
|
|
122
|
+
columns: this._sheet.columns.length,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
get functions(): FunctionDefinition[] {
|
|
127
|
+
const hfFunctions = this._graph.hf
|
|
128
|
+
.getRegisteredFunctionNames()
|
|
129
|
+
.map((name) => defaultFunctions.find((fn) => fn.name === name) ?? { name });
|
|
130
|
+
const echoFunctions = this._functions.map((fn) => ({ name: fn.binding! }));
|
|
131
|
+
return [...hfFunctions, ...echoFunctions];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
get initialized(): boolean {
|
|
135
|
+
return !!this._ctx;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Initialize sheet and engine.
|
|
140
|
+
*/
|
|
141
|
+
async initialize() {
|
|
142
|
+
log('initialize', { id: this.id });
|
|
143
|
+
invariant(!this.initialized, 'Already initialized.');
|
|
144
|
+
this._ctx = new Context();
|
|
145
|
+
if (!this._sheet.rows.length) {
|
|
146
|
+
this._insertIndices(this._sheet.rows, 0, this._options.rows, DEFAULT_ROWS);
|
|
147
|
+
}
|
|
148
|
+
if (!this._sheet.columns.length) {
|
|
149
|
+
this._insertIndices(this._sheet.columns, 0, this._options.columns, DEFAULT_COLUMNS);
|
|
150
|
+
}
|
|
151
|
+
this.reset();
|
|
152
|
+
|
|
153
|
+
// Listen for model updates (e.g., async calculations).
|
|
154
|
+
const unsubscribe = this._graph.update.on(() => this.update.emit());
|
|
155
|
+
this._ctx.onDispose(unsubscribe);
|
|
156
|
+
|
|
157
|
+
if (this._space) {
|
|
158
|
+
const { Filter } = await import('@dxos/client/echo');
|
|
159
|
+
const { FunctionType } = await import('@dxos/plugin-script/types');
|
|
160
|
+
|
|
161
|
+
// Listen for function changes.
|
|
162
|
+
const query = this._space?.db.query(Filter.schema(FunctionType));
|
|
163
|
+
const unsubscribe = query.subscribe(({ objects }) => {
|
|
164
|
+
this._functions = objects.filter((fn) => fn.binding);
|
|
165
|
+
this.update.emit();
|
|
166
|
+
});
|
|
167
|
+
this._ctx.onDispose(unsubscribe);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return this;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async destroy() {
|
|
174
|
+
log('destroy', { id: this.id });
|
|
175
|
+
if (this._ctx) {
|
|
176
|
+
await this._ctx.dispose();
|
|
177
|
+
this._ctx = undefined;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Update engine.
|
|
183
|
+
* NOTE: This resets the undo history.
|
|
184
|
+
* @deprecated
|
|
185
|
+
*/
|
|
186
|
+
reset() {
|
|
187
|
+
this._graph.hf.clearSheet(this._sheetId);
|
|
188
|
+
Object.entries(this._sheet.cells).forEach(([key, { value }]) => {
|
|
189
|
+
const { column, row } = this.addressFromIndex(key);
|
|
190
|
+
if (typeof value === 'string' && value.charAt(0) === '=') {
|
|
191
|
+
value = this.mapFormulaBindingToFormula(this.mapFormulaBindingFromId(this.mapFormulaIndicesToRefs(value)));
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
this._graph.hf.setCellContents({ sheet: this._sheetId, row, col: column }, value);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Recalculate formulas.
|
|
200
|
+
* NOTE: This resets the undo history.
|
|
201
|
+
* https://hyperformula.handsontable.com/guide/volatile-functions.html#volatile-actions
|
|
202
|
+
* @deprecated
|
|
203
|
+
*/
|
|
204
|
+
// TODO(burdon): Remove.
|
|
205
|
+
recalculate() {
|
|
206
|
+
this._graph.hf.rebuildAndRecalculate();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
insertRows(i: number, n = 1) {
|
|
210
|
+
this._insertIndices(this._sheet.rows, i, n, DEFAULT_ROWS);
|
|
211
|
+
this.reset();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
insertColumns(i: number, n = 1) {
|
|
215
|
+
this._insertIndices(this._sheet.columns, i, n, DEFAULT_COLUMNS);
|
|
216
|
+
this.reset();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
//
|
|
220
|
+
// Undoable actions.
|
|
221
|
+
// TODO(burdon): Group undoable methods; consistently update hf/sheet.
|
|
222
|
+
//
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Clear range of values.
|
|
226
|
+
*/
|
|
227
|
+
clear(range: CellRange) {
|
|
228
|
+
const topLeft = getTopLeft(range);
|
|
229
|
+
const values = this._iterRange(range, () => null);
|
|
230
|
+
this._graph.hf.setCellContents(toSimpleCellAddress(this._sheetId, topLeft), values);
|
|
231
|
+
this._iterRange(range, (cell) => {
|
|
232
|
+
const idx = this.addressToIndex(cell);
|
|
233
|
+
delete this._sheet.cells[idx];
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
cut(range: CellRange) {
|
|
238
|
+
this._graph.hf.cut(toModelRange(this._sheetId, range));
|
|
239
|
+
this._iterRange(range, (cell) => {
|
|
240
|
+
const idx = this.addressToIndex(cell);
|
|
241
|
+
delete this._sheet.cells[idx];
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
copy(range: CellRange) {
|
|
246
|
+
this._graph.hf.copy(toModelRange(this._sheetId, range));
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
paste(cell: CellAddress) {
|
|
250
|
+
if (!this._graph.hf.isClipboardEmpty()) {
|
|
251
|
+
const changes = this._graph.hf.paste(toSimpleCellAddress(this._sheetId, cell));
|
|
252
|
+
for (const change of changes) {
|
|
253
|
+
if (change instanceof ExportedCellChange) {
|
|
254
|
+
const { address, newValue } = change;
|
|
255
|
+
const idx = this.addressToIndex({ row: address.row, column: address.col });
|
|
256
|
+
this._sheet.cells[idx] = { value: newValue };
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// TODO(burdon): Display undo/redo state.
|
|
263
|
+
undo() {
|
|
264
|
+
if (this._graph.hf.isThereSomethingToUndo()) {
|
|
265
|
+
this._graph.hf.undo();
|
|
266
|
+
this.update.emit();
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
redo() {
|
|
271
|
+
if (this._graph.hf.isThereSomethingToRedo()) {
|
|
272
|
+
this._graph.hf.redo();
|
|
273
|
+
this.update.emit();
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Get value from sheet.
|
|
279
|
+
*/
|
|
280
|
+
getCellValue(cell: CellAddress): CellScalarValue {
|
|
281
|
+
const idx = this.addressToIndex(cell);
|
|
282
|
+
return this._sheet.cells[idx]?.value ?? null;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Get value as a string for editing.
|
|
287
|
+
*/
|
|
288
|
+
getCellText(cell: CellAddress): string | undefined {
|
|
289
|
+
const value = this.getCellValue(cell);
|
|
290
|
+
if (value == null) {
|
|
291
|
+
return undefined;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (typeof value === 'string' && value.charAt(0) === '=') {
|
|
295
|
+
return this.mapFormulaBindingFromId(this.mapFormulaIndicesToRefs(value));
|
|
296
|
+
} else {
|
|
297
|
+
return String(value);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Get array of raw values from sheet.
|
|
303
|
+
*/
|
|
304
|
+
getCellValues(range: CellRange): CellScalarValue[][] {
|
|
305
|
+
return this._iterRange(range, (cell) => this.getCellValue(cell));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Gets the regular or computed value from the engine.
|
|
310
|
+
*/
|
|
311
|
+
getValue(cell: CellAddress): CellScalarValue {
|
|
312
|
+
// Applies rounding and post-processing.
|
|
313
|
+
const value = this._graph.hf.getCellValue(toSimpleCellAddress(this._sheetId, cell));
|
|
314
|
+
if (value instanceof DetailedCellError) {
|
|
315
|
+
return value.toString();
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
return value;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Get value type.
|
|
323
|
+
*/
|
|
324
|
+
getValueType(cell: CellAddress): ValueTypeEnum {
|
|
325
|
+
const addr = toSimpleCellAddress(this._sheetId, cell);
|
|
326
|
+
const type = this._graph.hf.getCellValueDetailedType(addr);
|
|
327
|
+
return typeMap[type];
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Sets the value, updating the sheet and engine.
|
|
332
|
+
*/
|
|
333
|
+
setValue(cell: CellAddress, value: CellScalarValue) {
|
|
334
|
+
if (this._options.readonly) {
|
|
335
|
+
throw new ReadonlyException();
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// Reallocate if > current bounds.
|
|
339
|
+
let refresh = false;
|
|
340
|
+
if (cell.row >= this._sheet.rows.length) {
|
|
341
|
+
this._insertIndices(this._sheet.rows, cell.row, 1, DEFAULT_ROWS);
|
|
342
|
+
refresh = true;
|
|
343
|
+
}
|
|
344
|
+
if (cell.column >= this._sheet.columns.length) {
|
|
345
|
+
this._insertIndices(this._sheet.columns, cell.column, 1, DEFAULT_COLUMNS);
|
|
346
|
+
refresh = true;
|
|
347
|
+
}
|
|
348
|
+
if (refresh) {
|
|
349
|
+
// TODO(burdon): Remove.
|
|
350
|
+
this.reset();
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// Insert into engine.
|
|
354
|
+
this._graph.hf.setCellContents({ sheet: this._sheetId, row: cell.row, col: cell.column }, [
|
|
355
|
+
[typeof value === 'string' && value.charAt(0) === '=' ? this.mapFormulaBindingToFormula(value) : value],
|
|
356
|
+
]);
|
|
357
|
+
|
|
358
|
+
// Insert into sheet.
|
|
359
|
+
const idx = this.addressToIndex(cell);
|
|
360
|
+
if (value === undefined || value === null) {
|
|
361
|
+
delete this._sheet.cells[idx];
|
|
362
|
+
} else {
|
|
363
|
+
if (typeof value === 'string' && value.charAt(0) === '=') {
|
|
364
|
+
value = this.mapFormulaBindingToId(this.mapFormulaRefsToIndices(value));
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
this._sheet.cells[idx] = { value };
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Sets values from a simple map.
|
|
373
|
+
*/
|
|
374
|
+
setValues(values: Record<string, CellValue>) {
|
|
375
|
+
Object.entries(values).forEach(([key, { value }]) => {
|
|
376
|
+
this.setValue(addressFromA1Notation(key), value);
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Iterate range.
|
|
382
|
+
*/
|
|
383
|
+
private _iterRange(range: CellRange, cb: (cell: CellAddress) => CellScalarValue | void): CellScalarValue[][] {
|
|
384
|
+
const to = range.to ?? range.from;
|
|
385
|
+
const rowRange = [Math.min(range.from.row, to.row), Math.max(range.from.row, to.row)];
|
|
386
|
+
const columnRange = [Math.min(range.from.column, to.column), Math.max(range.from.column, to.column)];
|
|
387
|
+
const rows: CellScalarValue[][] = [];
|
|
388
|
+
for (let row = rowRange[0]; row <= rowRange[1]; row++) {
|
|
389
|
+
const rowCells: CellScalarValue[] = [];
|
|
390
|
+
for (let column = columnRange[0]; column <= columnRange[1]; column++) {
|
|
391
|
+
const value = cb({ row, column });
|
|
392
|
+
if (value !== undefined) {
|
|
393
|
+
rowCells.push(value);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
rows.push(rowCells);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
return rows;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
*
|
|
404
|
+
*/
|
|
405
|
+
// TODO(burdon): Insert indices into sheet.
|
|
406
|
+
private _insertIndices(indices: string[], i: number, n: number, max: number) {
|
|
407
|
+
if (i + n > max) {
|
|
408
|
+
throw new RangeException(i + n);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
const idx = createIndices(n);
|
|
412
|
+
indices.splice(i, 0, ...idx);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// TODO(burdon): Delete index.
|
|
416
|
+
private _deleteIndices(indices: string[], i: number, n: number) {
|
|
417
|
+
throw new Error('Not implemented');
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// TODO(burdon): Move. Cannot use fractional without changing. Switch back to using unique IDs?
|
|
421
|
+
private _moveIndices(indices: string[], i: number, j: number, n: number) {
|
|
422
|
+
throw new Error('Not implemented');
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
//
|
|
426
|
+
// Indices.
|
|
427
|
+
//
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* E.g., "A1" => "x1@y1".
|
|
431
|
+
*/
|
|
432
|
+
addressToIndex(cell: CellAddress): CellIndex {
|
|
433
|
+
return `${this._sheet.columns[cell.column]}@${this._sheet.rows[cell.row]}`;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* E.g., "x1@y1" => "A1".
|
|
438
|
+
*/
|
|
439
|
+
addressFromIndex(idx: CellIndex): CellAddress {
|
|
440
|
+
const [column, row] = idx.split('@');
|
|
441
|
+
return {
|
|
442
|
+
column: this._sheet.columns.indexOf(column),
|
|
443
|
+
row: this._sheet.rows.indexOf(row),
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* E.g., "A1:B2" => "x1@y1:x2@y2".
|
|
449
|
+
*/
|
|
450
|
+
rangeToIndex(range: CellRange): string {
|
|
451
|
+
return [range.from, range.to ?? range.from].map((cell) => this.addressToIndex(cell)).join(':');
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* E.g., "x1@y1:x2@y2" => "A1:B2".
|
|
456
|
+
*/
|
|
457
|
+
rangeFromIndex(idx: string): CellRange {
|
|
458
|
+
const [from, to] = idx.split(':').map((idx) => this.addressFromIndex(idx));
|
|
459
|
+
return { from, to };
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* E.g., "HELLO()" => "EDGE("HELLO")".
|
|
464
|
+
*/
|
|
465
|
+
mapFormulaBindingToFormula(formula: string): string {
|
|
466
|
+
return formula.replace(/([a-zA-Z0-9]+)\((.*)\)/g, (match, binding, args) => {
|
|
467
|
+
const fn = this._functions.find((fn) => fn.binding === binding);
|
|
468
|
+
if (!fn) {
|
|
469
|
+
return match;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (args.trim() === '') {
|
|
473
|
+
return `EDGE("${binding}")`;
|
|
474
|
+
}
|
|
475
|
+
return `EDGE("${binding}", ${args})`;
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* E.g., "EDGE("HELLO")" => "HELLO()".
|
|
481
|
+
*/
|
|
482
|
+
mapFormulaBindingFromFormula(formula: string): string {
|
|
483
|
+
return formula.replace(/EDGE\("([a-zA-Z0-9]+)"(.*)\)/, (_match, binding, args) => {
|
|
484
|
+
if (args.trim() === '') {
|
|
485
|
+
return `${binding}()`;
|
|
486
|
+
}
|
|
487
|
+
return `${binding}(${args.slice(2)})`;
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Map from binding to fully qualified ECHO ID.
|
|
493
|
+
*/
|
|
494
|
+
mapFormulaBindingToId(formula: string): string {
|
|
495
|
+
return this._options.mapFormulaBindingToId(this._functions)(formula);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Map from fully qualified ECHO ID to binding.
|
|
500
|
+
*/
|
|
501
|
+
mapFormulaBindingFromId(formula: string): string {
|
|
502
|
+
return this._options.mapFormulaBindingFromId(this._functions)(formula);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Map from A1 notation to indices.
|
|
507
|
+
*/
|
|
508
|
+
mapFormulaRefsToIndices(formula: string): string {
|
|
509
|
+
invariant(formula.charAt(0) === '=');
|
|
510
|
+
return formula.replace(/([a-zA-Z]+)([0-9]+)/g, (match) => {
|
|
511
|
+
return this.addressToIndex(addressFromA1Notation(match));
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Map from indices to A1 notation.
|
|
517
|
+
*/
|
|
518
|
+
mapFormulaIndicesToRefs(formula: string): string {
|
|
519
|
+
invariant(formula.charAt(0) === '=');
|
|
520
|
+
return formula.replace(/([a-zA-Z0-9]+)@([a-zA-Z0-9]+)/g, (idx) => {
|
|
521
|
+
return addressToA1Notation(this.addressFromIndex(idx));
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
//
|
|
526
|
+
// Values
|
|
527
|
+
//
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* https://hyperformula.handsontable.com/guide/date-and-time-handling.html#example
|
|
531
|
+
* https://hyperformula.handsontable.com/api/interfaces/configparams.html#nulldate
|
|
532
|
+
* NOTE: TODAY() is number of FULL days since nullDate. It will typically be -1 days from NOW().
|
|
533
|
+
*/
|
|
534
|
+
toLocalDate(num: number): Date {
|
|
535
|
+
const { year, month, day, hours, minutes, seconds } = this.toDateTime(num);
|
|
536
|
+
return new Date(year, month - 1, day, hours, minutes, seconds);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
toDateTime(num: number): SimpleDateTime {
|
|
540
|
+
return this._graph.hf.numberToDateTime(num) as SimpleDateTime;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
toDate(num: number): SimpleDate {
|
|
544
|
+
return this._graph.hf.numberToDate(num) as SimpleDate;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
toTime(num: number): SimpleDate {
|
|
548
|
+
return this._graph.hf.numberToTime(num) as SimpleDate;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
@@ -3,14 +3,15 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { getIndices, sortByIndex, getIndicesBelow, getIndicesAbove, getIndicesBetween } from '@tldraw/indices';
|
|
6
|
-
import {
|
|
6
|
+
import { expect } from 'chai';
|
|
7
|
+
import { describe, test } from 'vitest';
|
|
7
8
|
|
|
8
9
|
import { inRange, addressFromA1Notation, addressToA1Notation, rangeFromA1Notation, rangeToA1Notation } from './types';
|
|
9
10
|
|
|
10
11
|
describe('cell', () => {
|
|
11
12
|
test('posToA1Notation', () => {
|
|
12
|
-
expect(addressToA1Notation({
|
|
13
|
-
expect(addressFromA1Notation('C2')).to.deep.eq({
|
|
13
|
+
expect(addressToA1Notation({ column: 0, row: 0 })).to.eq('A1');
|
|
14
|
+
expect(addressFromA1Notation('C2')).to.deep.eq({ column: 2, row: 1 });
|
|
14
15
|
});
|
|
15
16
|
|
|
16
17
|
test('rangeToA1Notation', () => {
|
|
@@ -55,19 +56,19 @@ describe('cell', () => {
|
|
|
55
56
|
// Values.
|
|
56
57
|
const cells: Record<string, any> = {};
|
|
57
58
|
const setCell = (cell: string, value: any) => {
|
|
58
|
-
const {
|
|
59
|
+
const { column, row } = addressFromA1Notation(cell);
|
|
59
60
|
// Reallocate if > current bounds.
|
|
60
|
-
if (
|
|
61
|
-
insertIndex(columns,
|
|
61
|
+
if (column >= columns.length) {
|
|
62
|
+
insertIndex(columns, column);
|
|
62
63
|
}
|
|
63
64
|
if (row >= rows.length) {
|
|
64
65
|
insertIndex(rows, row);
|
|
65
66
|
}
|
|
66
|
-
const index = `${columns[
|
|
67
|
+
const index = `${columns[column]}@${rows[row]}`;
|
|
67
68
|
cells[index] = value;
|
|
68
69
|
};
|
|
69
70
|
|
|
70
|
-
expect(addressFromA1Notation('A1')).to.deep.eq({
|
|
71
|
+
expect(addressFromA1Notation('A1')).to.deep.eq({ column: 0, row: 0 });
|
|
71
72
|
|
|
72
73
|
expect(columns).to.deep.eq(['a1', 'a2', 'a3', 'a4', 'a5']);
|
|
73
74
|
insertIndex(columns, 7);
|
|
@@ -4,43 +4,33 @@
|
|
|
4
4
|
|
|
5
5
|
import { invariant } from '@dxos/invariant';
|
|
6
6
|
|
|
7
|
-
export const
|
|
8
|
-
export const DEFAULT_COLUMNS = 26;
|
|
9
|
-
|
|
10
|
-
export const MAX_ROWS = 500;
|
|
11
|
-
export const MAX_COLUMNS = 26 * 2;
|
|
12
|
-
|
|
13
|
-
export type CellAddress = { col: number; row: number };
|
|
7
|
+
export const MAX_COLUMNS = 26 * 26;
|
|
14
8
|
|
|
9
|
+
export type CellAddress = { column: number; row: number };
|
|
15
10
|
export type CellRange = { from: CellAddress; to?: CellAddress };
|
|
16
11
|
|
|
17
|
-
export type CellIndex = string;
|
|
18
|
-
|
|
19
|
-
export type CellContentValue = number | string | boolean | null;
|
|
20
|
-
|
|
21
12
|
export const posEquals = (a: CellAddress | undefined, b: CellAddress | undefined) => {
|
|
22
|
-
return a?.
|
|
13
|
+
return a?.column === b?.column && a?.row === b?.row;
|
|
23
14
|
};
|
|
24
15
|
|
|
25
|
-
export const columnLetter = (
|
|
26
|
-
invariant(
|
|
16
|
+
export const columnLetter = (column: number): string => {
|
|
17
|
+
invariant(column < MAX_COLUMNS, `Invalid column: ${column}`);
|
|
27
18
|
return (
|
|
28
|
-
(
|
|
29
|
-
String.fromCharCode('A'.charCodeAt(0) + (
|
|
19
|
+
(column >= 26 ? String.fromCharCode('A'.charCodeAt(0) + Math.floor(column / 26) - 1) : '') +
|
|
20
|
+
String.fromCharCode('A'.charCodeAt(0) + (column % 26))
|
|
30
21
|
);
|
|
31
22
|
};
|
|
32
23
|
|
|
33
|
-
export const addressToA1Notation = ({
|
|
34
|
-
return `${columnLetter(
|
|
24
|
+
export const addressToA1Notation = ({ column, row }: CellAddress): string => {
|
|
25
|
+
return `${columnLetter(column)}${row + 1}`;
|
|
35
26
|
};
|
|
36
27
|
|
|
37
|
-
// TODO(burdon): See simpleCellAddressFromString
|
|
38
28
|
export const addressFromA1Notation = (ref: string): CellAddress => {
|
|
39
29
|
const match = ref.match(/([A-Z]+)(\d+)/);
|
|
40
30
|
invariant(match, `Invalid notation: ${ref}`);
|
|
41
31
|
return {
|
|
42
32
|
row: parseInt(match[2], 10) - 1,
|
|
43
|
-
|
|
33
|
+
column: match[1].split('').reduce((acc, c) => acc * 26 + c.charCodeAt(0) - 'A'.charCodeAt(0) + 1, 0) - 1,
|
|
44
34
|
};
|
|
45
35
|
};
|
|
46
36
|
|
|
@@ -69,13 +59,13 @@ export const inRange = (range: CellRange | undefined, cell: CellAddress): boolea
|
|
|
69
59
|
return false;
|
|
70
60
|
}
|
|
71
61
|
|
|
72
|
-
const {
|
|
73
|
-
const {
|
|
62
|
+
const { column: c1, row: r1 } = from;
|
|
63
|
+
const { column: c2, row: r2 } = to;
|
|
74
64
|
const cMin = Math.min(c1, c2);
|
|
75
65
|
const cMax = Math.max(c1, c2);
|
|
76
66
|
const rMin = Math.min(r1, r2);
|
|
77
67
|
const rMax = Math.max(r1, r2);
|
|
78
68
|
|
|
79
|
-
const {
|
|
80
|
-
return
|
|
69
|
+
const { column, row } = cell;
|
|
70
|
+
return column >= cMin && column <= cMax && row >= rMin && row <= rMax;
|
|
81
71
|
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { randomBytes } from '@dxos/crypto';
|
|
6
|
+
|
|
7
|
+
// TODO(burdon): Factor out from dxos/protocols to new common package.
|
|
8
|
+
export class ApiError extends Error {}
|
|
9
|
+
export class ReadonlyException extends ApiError {}
|
|
10
|
+
export class RangeException extends ApiError {
|
|
11
|
+
constructor(n: number) {
|
|
12
|
+
super();
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* With a string length of 8, the chance of a collision is 0.02% for a sheet with 10,000 strings.
|
|
18
|
+
*/
|
|
19
|
+
export const createIndex = (length = 8): string => {
|
|
20
|
+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
21
|
+
const charactersLength = characters.length;
|
|
22
|
+
const randomBuffer = randomBytes(length);
|
|
23
|
+
return Array.from(randomBuffer, (byte) => characters[byte % charactersLength]).join('');
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const createIndices = (length: number): string[] => Array.from({ length }).map(() => createIndex());
|
|
27
|
+
|
|
28
|
+
// TODO(burdon): Factor out.
|
|
29
|
+
export const pickOne = <T>(values: T[]): T => values[Math.floor(Math.random() * values.length)];
|
|
30
|
+
export const pickSome = <T>(values: T[], n = 1): T[] => {
|
|
31
|
+
const result = new Set<T>();
|
|
32
|
+
while (result.size < n) {
|
|
33
|
+
result.add(pickOne(values));
|
|
34
|
+
}
|
|
35
|
+
return Array.from(result.values());
|
|
36
|
+
};
|