@dxos/plugin-sheet 0.7.4 → 0.7.5-main.937ce75

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 (188) hide show
  1. package/dist/lib/browser/{SheetContainer-KCLT6PEO.mjs → SheetContainer-S4NCLUYL.mjs} +16 -22
  2. package/dist/lib/browser/SheetContainer-S4NCLUYL.mjs.map +7 -0
  3. package/dist/lib/browser/{chunk-E5WQ7U7G.mjs → chunk-A374JPWV.mjs} +72 -705
  4. package/dist/lib/browser/chunk-A374JPWV.mjs.map +7 -0
  5. package/dist/lib/browser/chunk-Q4XS4YWF.mjs +900 -0
  6. package/dist/lib/browser/chunk-Q4XS4YWF.mjs.map +7 -0
  7. package/dist/lib/browser/index.mjs +74 -180
  8. package/dist/lib/browser/index.mjs.map +3 -3
  9. package/dist/lib/browser/meta.json +1 -1
  10. package/dist/lib/browser/types/index.mjs +62 -0
  11. package/dist/lib/node/{SheetContainer-VVVRYTQG.cjs → SheetContainer-TP4GYXZB.cjs} +41 -43
  12. package/dist/lib/node/SheetContainer-TP4GYXZB.cjs.map +7 -0
  13. package/dist/lib/node/{chunk-45YW2DX2.cjs → chunk-FDEQ2PGJ.cjs} +115 -726
  14. package/dist/lib/node/chunk-FDEQ2PGJ.cjs.map +7 -0
  15. package/dist/lib/node/chunk-TQOJ7DG2.cjs +935 -0
  16. package/dist/lib/node/chunk-TQOJ7DG2.cjs.map +7 -0
  17. package/dist/lib/node/index.cjs +104 -207
  18. package/dist/lib/node/index.cjs.map +3 -3
  19. package/dist/lib/node/meta.json +1 -1
  20. package/dist/lib/node/types/index.cjs +84 -0
  21. package/dist/lib/node/types/index.cjs.map +7 -0
  22. package/dist/lib/node-esm/{SheetContainer-LSBE6Q4X.mjs → SheetContainer-YB3JBVPZ.mjs} +16 -22
  23. package/dist/lib/node-esm/SheetContainer-YB3JBVPZ.mjs.map +7 -0
  24. package/dist/lib/node-esm/{chunk-NYDNXI7L.mjs → chunk-L5PQHVTX.mjs} +72 -705
  25. package/dist/lib/node-esm/chunk-L5PQHVTX.mjs.map +7 -0
  26. package/dist/lib/node-esm/chunk-NYYIDVR7.mjs +901 -0
  27. package/dist/lib/node-esm/chunk-NYYIDVR7.mjs.map +7 -0
  28. package/dist/lib/node-esm/index.mjs +74 -180
  29. package/dist/lib/node-esm/index.mjs.map +3 -3
  30. package/dist/lib/node-esm/meta.json +1 -1
  31. package/dist/lib/node-esm/types/index.mjs +63 -0
  32. package/dist/types/src/SheetPlugin.d.ts.map +1 -1
  33. package/dist/types/src/components/ComputeGraph/ComputeGraphContextProvider.d.ts +1 -1
  34. package/dist/types/src/components/ComputeGraph/ComputeGraphContextProvider.d.ts.map +1 -1
  35. package/dist/types/src/components/ComputeGraph/compute-graph.stories.d.ts.map +1 -0
  36. package/dist/types/src/components/FunctionEditor/FunctionEditor.d.ts.map +1 -1
  37. package/dist/types/src/components/GridSheet/GridSheet.d.ts.map +1 -1
  38. package/dist/types/src/components/GridSheet/SheetCellEditor.stories.d.ts.map +1 -1
  39. package/dist/types/src/components/GridSheet/util.d.ts.map +1 -1
  40. package/dist/types/src/components/RangeList/RangeList.d.ts.map +1 -1
  41. package/dist/types/src/components/SheetContainer/SheetContainer.stories.d.ts.map +1 -1
  42. package/dist/types/src/components/SheetContext/SheetContext.d.ts +1 -2
  43. package/dist/types/src/components/SheetContext/SheetContext.d.ts.map +1 -1
  44. package/dist/types/src/components/Toolbar/Toolbar.d.ts +1 -1
  45. package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -1
  46. package/dist/types/src/components/index.d.ts +1 -1
  47. package/dist/types/src/extensions/compute.d.ts +1 -1
  48. package/dist/types/src/extensions/compute.d.ts.map +1 -1
  49. package/dist/types/src/extensions/editor/extension.d.ts +1 -1
  50. package/dist/types/src/extensions/editor/extension.d.ts.map +1 -1
  51. package/dist/types/src/index.d.ts +1 -1
  52. package/dist/types/src/index.d.ts.map +1 -1
  53. package/dist/types/src/integrations/thread-ranges.d.ts +1 -1
  54. package/dist/types/src/integrations/thread-ranges.d.ts.map +1 -1
  55. package/dist/types/src/model/sheet-model.d.ts +6 -8
  56. package/dist/types/src/model/sheet-model.d.ts.map +1 -1
  57. package/dist/types/src/model/testing.d.ts +1 -1
  58. package/dist/types/src/model/testing.d.ts.map +1 -1
  59. package/dist/types/src/model/useSheetModel.d.ts +1 -1
  60. package/dist/types/src/model/useSheetModel.d.ts.map +1 -1
  61. package/dist/types/src/testing/testing.d.ts +2 -2
  62. package/dist/types/src/testing/testing.d.ts.map +1 -1
  63. package/dist/types/src/{defs → types}/index.d.ts +1 -0
  64. package/dist/types/src/types/index.d.ts.map +1 -0
  65. package/dist/types/src/types/schema.d.ts +105 -0
  66. package/dist/types/src/types/schema.d.ts.map +1 -0
  67. package/dist/types/src/types/sheet-range-types.d.ts.map +1 -0
  68. package/dist/types/src/types/types.d.ts +82 -0
  69. package/dist/types/src/types/types.d.ts.map +1 -0
  70. package/dist/types/src/{defs → types}/util.d.ts +5 -1
  71. package/dist/types/src/types/util.d.ts.map +1 -0
  72. package/dist/types/tsconfig.tsbuildinfo +1 -0
  73. package/package.json +46 -53
  74. package/src/SheetPlugin.tsx +44 -115
  75. package/src/components/ComputeGraph/ComputeGraphContextProvider.tsx +1 -2
  76. package/src/{compute-graph → components/ComputeGraph}/compute-graph.stories.tsx +6 -7
  77. package/src/components/FunctionEditor/FunctionEditor.tsx +2 -1
  78. package/src/components/GridSheet/GridSheet.tsx +15 -18
  79. package/src/components/GridSheet/SheetCellEditor.stories.tsx +6 -7
  80. package/src/components/GridSheet/util.ts +3 -2
  81. package/src/components/RangeList/RangeList.tsx +2 -1
  82. package/src/components/SheetContainer/SheetContainer.stories.tsx +11 -12
  83. package/src/components/SheetContext/SheetContext.tsx +1 -2
  84. package/src/components/Toolbar/Toolbar.tsx +18 -20
  85. package/src/extensions/compute.ts +7 -4
  86. package/src/extensions/editor/extension.test.ts +2 -1
  87. package/src/extensions/editor/extension.ts +2 -3
  88. package/src/index.ts +1 -3
  89. package/src/integrations/thread-ranges.ts +28 -24
  90. package/src/model/sheet-model.test.ts +4 -10
  91. package/src/model/sheet-model.ts +30 -22
  92. package/src/model/testing.ts +3 -2
  93. package/src/model/useSheetModel.ts +2 -1
  94. package/src/sanity.test.ts +1 -1
  95. package/src/testing/testing.tsx +2 -3
  96. package/src/{defs → types}/index.ts +1 -0
  97. package/src/types/schema.ts +56 -0
  98. package/src/types/types.ts +93 -0
  99. package/src/{defs → types}/util.ts +13 -13
  100. package/dist/lib/browser/SheetContainer-KCLT6PEO.mjs.map +0 -7
  101. package/dist/lib/browser/chunk-E5WQ7U7G.mjs.map +0 -7
  102. package/dist/lib/browser/chunk-F3HE6D3J.mjs +0 -3269
  103. package/dist/lib/browser/chunk-F3HE6D3J.mjs.map +0 -7
  104. package/dist/lib/browser/chunk-JXFPOYNA.mjs +0 -67
  105. package/dist/lib/browser/chunk-JXFPOYNA.mjs.map +0 -7
  106. package/dist/lib/browser/compute-graph-SNUS7HOH.mjs +0 -35
  107. package/dist/lib/browser/types.mjs +0 -16
  108. package/dist/lib/node/SheetContainer-VVVRYTQG.cjs.map +0 -7
  109. package/dist/lib/node/chunk-45YW2DX2.cjs.map +0 -7
  110. package/dist/lib/node/chunk-KSEEI5VC.cjs +0 -3323
  111. package/dist/lib/node/chunk-KSEEI5VC.cjs.map +0 -7
  112. package/dist/lib/node/chunk-OWH2EUHZ.cjs +0 -90
  113. package/dist/lib/node/chunk-OWH2EUHZ.cjs.map +0 -7
  114. package/dist/lib/node/compute-graph-WILPHO4A.cjs +0 -57
  115. package/dist/lib/node/compute-graph-WILPHO4A.cjs.map +0 -7
  116. package/dist/lib/node/types.cjs +0 -38
  117. package/dist/lib/node/types.cjs.map +0 -7
  118. package/dist/lib/node-esm/SheetContainer-LSBE6Q4X.mjs.map +0 -7
  119. package/dist/lib/node-esm/chunk-6JF2AHKO.mjs +0 -3270
  120. package/dist/lib/node-esm/chunk-6JF2AHKO.mjs.map +0 -7
  121. package/dist/lib/node-esm/chunk-BVS2IQRO.mjs +0 -68
  122. package/dist/lib/node-esm/chunk-BVS2IQRO.mjs.map +0 -7
  123. package/dist/lib/node-esm/chunk-NYDNXI7L.mjs.map +0 -7
  124. package/dist/lib/node-esm/compute-graph-S6CVN7RS.mjs +0 -36
  125. package/dist/lib/node-esm/compute-graph-S6CVN7RS.mjs.map +0 -7
  126. package/dist/lib/node-esm/types.mjs +0 -17
  127. package/dist/lib/node-esm/types.mjs.map +0 -7
  128. package/dist/types/src/compute-graph/compute-graph-registry.d.ts +0 -34
  129. package/dist/types/src/compute-graph/compute-graph-registry.d.ts.map +0 -1
  130. package/dist/types/src/compute-graph/compute-graph.d.ts +0 -64
  131. package/dist/types/src/compute-graph/compute-graph.d.ts.map +0 -1
  132. package/dist/types/src/compute-graph/compute-graph.stories.d.ts.map +0 -1
  133. package/dist/types/src/compute-graph/compute-graph.test.d.ts +0 -2
  134. package/dist/types/src/compute-graph/compute-graph.test.d.ts.map +0 -1
  135. package/dist/types/src/compute-graph/compute-node.d.ts +0 -26
  136. package/dist/types/src/compute-graph/compute-node.d.ts.map +0 -1
  137. package/dist/types/src/compute-graph/functions/async-function.d.ts +0 -66
  138. package/dist/types/src/compute-graph/functions/async-function.d.ts.map +0 -1
  139. package/dist/types/src/compute-graph/functions/edge-function.d.ts +0 -21
  140. package/dist/types/src/compute-graph/functions/edge-function.d.ts.map +0 -1
  141. package/dist/types/src/compute-graph/functions/function-defs.d.ts +0 -11
  142. package/dist/types/src/compute-graph/functions/function-defs.d.ts.map +0 -1
  143. package/dist/types/src/compute-graph/functions/index.d.ts +0 -4
  144. package/dist/types/src/compute-graph/functions/index.d.ts.map +0 -1
  145. package/dist/types/src/compute-graph/hyperformula.test.d.ts +0 -2
  146. package/dist/types/src/compute-graph/hyperformula.test.d.ts.map +0 -1
  147. package/dist/types/src/compute-graph/index.d.ts +0 -5
  148. package/dist/types/src/compute-graph/index.d.ts.map +0 -1
  149. package/dist/types/src/compute-graph/testing/index.d.ts +0 -3
  150. package/dist/types/src/compute-graph/testing/index.d.ts.map +0 -1
  151. package/dist/types/src/compute-graph/testing/test-builder.d.ts +0 -15
  152. package/dist/types/src/compute-graph/testing/test-builder.d.ts.map +0 -1
  153. package/dist/types/src/compute-graph/testing/test-plugin.d.ts +0 -36
  154. package/dist/types/src/compute-graph/testing/test-plugin.d.ts.map +0 -1
  155. package/dist/types/src/compute-graph/util.d.ts +0 -2
  156. package/dist/types/src/compute-graph/util.d.ts.map +0 -1
  157. package/dist/types/src/defs/index.d.ts.map +0 -1
  158. package/dist/types/src/defs/sheet-range-types.d.ts.map +0 -1
  159. package/dist/types/src/defs/types.d.ts +0 -26
  160. package/dist/types/src/defs/types.d.ts.map +0 -1
  161. package/dist/types/src/defs/types.test.d.ts +0 -2
  162. package/dist/types/src/defs/types.test.d.ts.map +0 -1
  163. package/dist/types/src/defs/util.d.ts.map +0 -1
  164. package/dist/types/src/types.d.ts +0 -182
  165. package/dist/types/src/types.d.ts.map +0 -1
  166. package/dist/vendor/hyperformula.mjs +0 -37145
  167. package/src/compute-graph/compute-graph-registry.ts +0 -90
  168. package/src/compute-graph/compute-graph.test.ts +0 -87
  169. package/src/compute-graph/compute-graph.ts +0 -260
  170. package/src/compute-graph/compute-node.ts +0 -62
  171. package/src/compute-graph/functions/async-function.ts +0 -179
  172. package/src/compute-graph/functions/edge-function.ts +0 -102
  173. package/src/compute-graph/functions/function-defs.ts +0 -2427
  174. package/src/compute-graph/functions/index.ts +0 -7
  175. package/src/compute-graph/hyperformula.test.ts +0 -14
  176. package/src/compute-graph/index.ts +0 -8
  177. package/src/compute-graph/testing/index.ts +0 -6
  178. package/src/compute-graph/testing/test-builder.ts +0 -54
  179. package/src/compute-graph/testing/test-plugin.ts +0 -100
  180. package/src/compute-graph/util.ts +0 -8
  181. package/src/defs/types.test.ts +0 -91
  182. package/src/defs/types.ts +0 -88
  183. package/src/types.ts +0 -125
  184. /package/dist/lib/browser/{compute-graph-SNUS7HOH.mjs.map → types/index.mjs.map} +0 -0
  185. /package/dist/lib/{browser/types.mjs.map → node-esm/types/index.mjs.map} +0 -0
  186. /package/dist/types/src/{compute-graph → components/ComputeGraph}/compute-graph.stories.d.ts +0 -0
  187. /package/dist/types/src/{defs → types}/sheet-range-types.d.ts +0 -0
  188. /package/src/{defs → types}/sheet-range-types.ts +0 -0
@@ -0,0 +1,901 @@
1
+ import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
+ import {
3
+ SHEET_PLUGIN
4
+ } from "./chunk-BM2Q3FFC.mjs";
5
+
6
+ // packages/plugins/plugin-sheet/src/types/schema.ts
7
+ import { Ref, S, TypedObject } from "@dxos/echo-schema";
8
+ import { ThreadType } from "@dxos/plugin-space/types";
9
+ var CellValue = S.Struct({
10
+ // TODO(burdon): How to store dates (datetime, date, time), percentages, etc.
11
+ // Consider import/export; natural access for other plugins. Special handling for currency (precision).
12
+ // TODO(burdon): Automerge (long string) or short string or number.
13
+ value: S.Any
14
+ });
15
+ var Range = S.Struct({
16
+ range: S.String,
17
+ key: S.String,
18
+ value: S.String
19
+ });
20
+ var RowColumnMeta = S.Struct({
21
+ size: S.optional(S.Number)
22
+ });
23
+ var SheetType = class extends TypedObject({
24
+ typename: "dxos.org/type/Sheet",
25
+ version: "0.1.0"
26
+ })({
27
+ name: S.optional(S.String),
28
+ // Sparse map of cells referenced by index.
29
+ cells: S.mutable(S.Record({
30
+ key: S.String,
31
+ value: S.mutable(CellValue)
32
+ })),
33
+ // Ordered row indices.
34
+ rows: S.mutable(S.Array(S.String)),
35
+ // Ordered column indices.
36
+ columns: S.mutable(S.Array(S.String)),
37
+ // Row metadata referenced by index.
38
+ rowMeta: S.mutable(S.Record({
39
+ key: S.String,
40
+ value: S.mutable(RowColumnMeta)
41
+ })),
42
+ // Column metadata referenced by index.
43
+ columnMeta: S.mutable(S.Record({
44
+ key: S.String,
45
+ value: S.mutable(RowColumnMeta)
46
+ })),
47
+ // Cell formatting referenced by indexed range.
48
+ ranges: S.mutable(S.Array(Range)),
49
+ // Threads associated with the sheet
50
+ threads: S.optional(S.mutable(S.Array(Ref(ThreadType))))
51
+ }) {
52
+ };
53
+
54
+ // packages/plugins/plugin-sheet/src/types/sheet-range-types.ts
55
+ var alignKey = "alignment";
56
+ var commentKey = "comment";
57
+ var styleKey = "style";
58
+ var cellClassNameForRange = ({ key, value }) => {
59
+ switch (key) {
60
+ case alignKey:
61
+ switch (value) {
62
+ case "start":
63
+ return "text-start";
64
+ case "center":
65
+ return "text-center";
66
+ case "end":
67
+ return "text-end";
68
+ default:
69
+ return void 0;
70
+ }
71
+ case commentKey:
72
+ return "bg-gridComment";
73
+ case styleKey:
74
+ switch (value) {
75
+ case "highlight":
76
+ return "!bg-gridHighlight";
77
+ case "softwrap":
78
+ return "!whitespace-normal";
79
+ default:
80
+ return void 0;
81
+ }
82
+ default:
83
+ return void 0;
84
+ }
85
+ };
86
+
87
+ // packages/plugins/plugin-sheet/src/types/types.ts
88
+ import { S as S2 } from "@dxos/echo-schema";
89
+
90
+ // packages/plugins/plugin-sheet/src/model/sheet-model.ts
91
+ import { Event } from "@dxos/async";
92
+ import { addressFromA1Notation, addressToA1Notation, createSheetName, isFormula, DetailedCellError, ExportedCellChange } from "@dxos/compute";
93
+ import { Resource } from "@dxos/context";
94
+ import { getTypename, FormatEnum, TypeEnum } from "@dxos/echo-schema";
95
+ import { invariant } from "@dxos/invariant";
96
+ import { PublicKey } from "@dxos/keys";
97
+ import { log } from "@dxos/log";
98
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/plugins/plugin-sheet/src/model/sheet-model.ts";
99
+ var typeMap = {
100
+ BOOLEAN: {
101
+ type: TypeEnum.Boolean
102
+ },
103
+ NUMBER_RAW: {
104
+ type: TypeEnum.Number
105
+ },
106
+ NUMBER_PERCENT: {
107
+ type: TypeEnum.Number,
108
+ format: FormatEnum.Percent
109
+ },
110
+ NUMBER_CURRENCY: {
111
+ type: TypeEnum.Number,
112
+ format: FormatEnum.Currency
113
+ },
114
+ NUMBER_DATETIME: {
115
+ type: TypeEnum.String,
116
+ format: FormatEnum.DateTime
117
+ },
118
+ NUMBER_DATE: {
119
+ type: TypeEnum.String,
120
+ format: FormatEnum.Date
121
+ },
122
+ NUMBER_TIME: {
123
+ type: TypeEnum.String,
124
+ format: FormatEnum.Time
125
+ }
126
+ };
127
+ var getTopLeft = (range) => {
128
+ const to = range.to ?? range.from;
129
+ return {
130
+ row: Math.min(range.from.row, to.row),
131
+ col: Math.min(range.from.col, to.col)
132
+ };
133
+ };
134
+ var toSimpleCellAddress = (sheet, cell) => ({
135
+ sheet,
136
+ row: cell.row,
137
+ col: cell.col
138
+ });
139
+ var toModelRange = (sheet, range) => ({
140
+ start: toSimpleCellAddress(sheet, range.from),
141
+ end: toSimpleCellAddress(sheet, range.to ?? range.from)
142
+ });
143
+ var SheetModel = class extends Resource {
144
+ constructor(_graph, _sheet, _options = {}) {
145
+ super();
146
+ this._graph = _graph;
147
+ this._sheet = _sheet;
148
+ this._options = _options;
149
+ this.id = `model-${PublicKey.random().truncate()}`;
150
+ this.update = new Event();
151
+ }
152
+ get graph() {
153
+ return this._graph;
154
+ }
155
+ get sheet() {
156
+ return this._sheet;
157
+ }
158
+ get readonly() {
159
+ return this._options.readonly;
160
+ }
161
+ get bounds() {
162
+ return {
163
+ rows: this._sheet.rows.length,
164
+ columns: this._sheet.columns.length
165
+ };
166
+ }
167
+ /**
168
+ * Initialize sheet and engine.
169
+ */
170
+ async _open() {
171
+ log("initialize", {
172
+ id: this.id
173
+ }, {
174
+ F: __dxlog_file,
175
+ L: 123,
176
+ S: this,
177
+ C: (f, a) => f(...a)
178
+ });
179
+ initialize(this._sheet);
180
+ this._graph.update.on((event) => {
181
+ if (event.type === "functionsUpdated") {
182
+ this.reset();
183
+ }
184
+ });
185
+ this._node = this._graph.getOrCreateNode(createSheetName({
186
+ type: getTypename(this._sheet),
187
+ id: this._sheet.id
188
+ }));
189
+ await this._node.open();
190
+ const unsubscribe = this._node.update.on((event) => this.update.emit(event));
191
+ this._ctx.onDispose(unsubscribe);
192
+ this.reset();
193
+ }
194
+ /**
195
+ * Update engine.
196
+ * NOTE: This resets the undo history.
197
+ * @deprecated
198
+ */
199
+ reset() {
200
+ invariant(this._node, void 0, {
201
+ F: __dxlog_file,
202
+ L: 149,
203
+ S: this,
204
+ A: [
205
+ "this._node",
206
+ ""
207
+ ]
208
+ });
209
+ this._node.graph.hf.clearSheet(this._node.sheetId);
210
+ Object.entries(this._sheet.cells).forEach(([key, { value }]) => {
211
+ invariant(this._node, void 0, {
212
+ F: __dxlog_file,
213
+ L: 152,
214
+ S: this,
215
+ A: [
216
+ "this._node",
217
+ ""
218
+ ]
219
+ });
220
+ const { col, row } = addressFromIndex(this._sheet, key);
221
+ if (isFormula(value)) {
222
+ const binding = this._graph.mapFunctionBindingFromId(mapFormulaIndicesToRefs(this._sheet, value));
223
+ if (binding) {
224
+ value = this._graph.mapFormulaToNative(binding);
225
+ } else {
226
+ value = "";
227
+ }
228
+ }
229
+ this._node.graph.hf.setCellContents({
230
+ sheet: this._node.sheetId,
231
+ row,
232
+ col
233
+ }, value);
234
+ });
235
+ }
236
+ /**
237
+ * Recalculate formulas.
238
+ * NOTE: This resets the undo history.
239
+ * https://hyperformula.handsontable.com/guide/volatile-functions.html#volatile-actions
240
+ * @deprecated
241
+ */
242
+ // TODO(burdon): Remove.
243
+ recalculate() {
244
+ this._node?.graph.hf.rebuildAndRecalculate();
245
+ }
246
+ insertRows(i, n = 1) {
247
+ const idx = insertIndices(this._sheet.rows, i, n, MAX_ROWS);
248
+ this.reset();
249
+ return idx;
250
+ }
251
+ insertColumns(i, n = 1) {
252
+ const idx = insertIndices(this._sheet.columns, i, n, MAX_COLS);
253
+ this.reset();
254
+ return idx;
255
+ }
256
+ dropRow(rowIndex) {
257
+ const range = {
258
+ from: addressFromIndex(this._sheet, `${this._sheet.columns[0]}@${rowIndex}`),
259
+ to: addressFromIndex(this._sheet, `${this._sheet.columns[this._sheet.columns.length - 1]}@${rowIndex}`)
260
+ };
261
+ const values = this.getCellValues(range).flat();
262
+ const index = this._sheet.rows.indexOf(rowIndex);
263
+ this.clear(range);
264
+ this._sheet.rows.splice(index, 1);
265
+ delete this._sheet.rowMeta[rowIndex];
266
+ this.reset();
267
+ return {
268
+ axis: "row",
269
+ index,
270
+ axisIndex: rowIndex,
271
+ axisMeta: this._sheet.rowMeta[rowIndex],
272
+ values
273
+ };
274
+ }
275
+ dropColumn(colIndex) {
276
+ const range = {
277
+ from: addressFromIndex(this._sheet, `${colIndex}@${this._sheet.rows[0]}`),
278
+ to: addressFromIndex(this._sheet, `${colIndex}@${this._sheet.rows[this._sheet.rows.length - 1]}`)
279
+ };
280
+ const values = this.getCellValues(range).flat();
281
+ const index = this._sheet.columns.indexOf(colIndex);
282
+ this.clear(range);
283
+ this._sheet.columns.splice(index, 1);
284
+ delete this._sheet.columnMeta[colIndex];
285
+ this.reset();
286
+ return {
287
+ axis: "col",
288
+ index,
289
+ axisIndex: colIndex,
290
+ axisMeta: this._sheet.rowMeta[colIndex],
291
+ values
292
+ };
293
+ }
294
+ restoreRow({ index, axisIndex, axisMeta, values }) {
295
+ this._sheet.rows.splice(index, 0, axisIndex);
296
+ values.forEach((value, col) => {
297
+ if (value) {
298
+ this._sheet.cells[`${this._sheet.columns[col]}@${axisIndex}`] = {
299
+ value
300
+ };
301
+ }
302
+ });
303
+ if (axisMeta) {
304
+ this._sheet.rowMeta[axisIndex] = axisMeta;
305
+ }
306
+ this.reset();
307
+ }
308
+ restoreColumn({ index, axisIndex, axisMeta, values }) {
309
+ this._sheet.columns.splice(index, 0, axisIndex);
310
+ values.forEach((value, row) => {
311
+ if (value) {
312
+ this._sheet.cells[`${axisIndex}@${this._sheet.rows[row]}`] = {
313
+ value
314
+ };
315
+ }
316
+ });
317
+ if (axisMeta) {
318
+ this._sheet.columnMeta[axisIndex] = axisMeta;
319
+ }
320
+ this.reset();
321
+ }
322
+ //
323
+ // Undoable actions.
324
+ // TODO(burdon): Group undoable methods; consistently update hf/sheet.
325
+ //
326
+ /**
327
+ * Clear range of values.
328
+ */
329
+ clear(range) {
330
+ invariant(this._node, void 0, {
331
+ F: __dxlog_file,
332
+ L: 255,
333
+ S: this,
334
+ A: [
335
+ "this._node",
336
+ ""
337
+ ]
338
+ });
339
+ const topLeft = getTopLeft(range);
340
+ const values = this._iterRange(range, () => null);
341
+ this._node.graph.hf.setCellContents(toSimpleCellAddress(this._node.sheetId, topLeft), values);
342
+ this._iterRange(range, (cell) => {
343
+ const idx = addressToIndex(this._sheet, cell);
344
+ delete this._sheet.cells[idx];
345
+ });
346
+ }
347
+ cut(range) {
348
+ invariant(this._node, void 0, {
349
+ F: __dxlog_file,
350
+ L: 266,
351
+ S: this,
352
+ A: [
353
+ "this._node",
354
+ ""
355
+ ]
356
+ });
357
+ this._node.graph.hf.cut(toModelRange(this._node.sheetId, range));
358
+ this._iterRange(range, (cell) => {
359
+ const idx = addressToIndex(this._sheet, cell);
360
+ delete this._sheet.cells[idx];
361
+ });
362
+ }
363
+ copy(range) {
364
+ invariant(this._node, void 0, {
365
+ F: __dxlog_file,
366
+ L: 275,
367
+ S: this,
368
+ A: [
369
+ "this._node",
370
+ ""
371
+ ]
372
+ });
373
+ this._node.graph.hf.copy(toModelRange(this._node.sheetId, range));
374
+ }
375
+ paste(cell) {
376
+ invariant(this._node, void 0, {
377
+ F: __dxlog_file,
378
+ L: 280,
379
+ S: this,
380
+ A: [
381
+ "this._node",
382
+ ""
383
+ ]
384
+ });
385
+ if (!this._node.graph.hf.isClipboardEmpty()) {
386
+ const changes = this._node.graph.hf.paste(toSimpleCellAddress(this._node.sheetId, cell));
387
+ for (const change of changes) {
388
+ if (change instanceof ExportedCellChange) {
389
+ const { address, newValue } = change;
390
+ const idx = addressToIndex(this._sheet, {
391
+ row: address.row,
392
+ col: address.col
393
+ });
394
+ this._sheet.cells[idx] = {
395
+ value: newValue
396
+ };
397
+ }
398
+ }
399
+ }
400
+ }
401
+ // TODO(burdon): Display undo/redo state.
402
+ undo() {
403
+ invariant(this._node, void 0, {
404
+ F: __dxlog_file,
405
+ L: 295,
406
+ S: this,
407
+ A: [
408
+ "this._node",
409
+ ""
410
+ ]
411
+ });
412
+ if (this._node.graph.hf.isThereSomethingToUndo()) {
413
+ this._node.graph.hf.undo();
414
+ }
415
+ }
416
+ redo() {
417
+ invariant(this._node, void 0, {
418
+ F: __dxlog_file,
419
+ L: 303,
420
+ S: this,
421
+ A: [
422
+ "this._node",
423
+ ""
424
+ ]
425
+ });
426
+ if (this._node.graph.hf.isThereSomethingToRedo()) {
427
+ this._node.graph.hf.redo();
428
+ }
429
+ }
430
+ /**
431
+ * Get value from sheet.
432
+ */
433
+ getCellValue(cell) {
434
+ const idx = addressToIndex(this._sheet, cell);
435
+ return this._sheet.cells[idx]?.value ?? null;
436
+ }
437
+ /**
438
+ * Get value as a string for editing.
439
+ */
440
+ getCellText(cell) {
441
+ const value = this.getCellValue(cell);
442
+ if (value == null) {
443
+ return void 0;
444
+ }
445
+ if (isFormula(value)) {
446
+ return this._graph.mapFunctionBindingFromId(mapFormulaIndicesToRefs(this._sheet, value));
447
+ } else {
448
+ return String(value);
449
+ }
450
+ }
451
+ /**
452
+ * Get array of raw values from sheet.
453
+ */
454
+ getCellValues(range) {
455
+ return this._iterRange(range, (cell) => this.getCellValue(cell));
456
+ }
457
+ /**
458
+ * Gets the regular or computed value from the engine.
459
+ */
460
+ getValue(cell) {
461
+ invariant(this._node, void 0, {
462
+ F: __dxlog_file,
463
+ L: 346,
464
+ S: this,
465
+ A: [
466
+ "this._node",
467
+ ""
468
+ ]
469
+ });
470
+ const address = toSimpleCellAddress(this._node.sheetId, cell);
471
+ const value = this._node.graph.hf.getCellValue(address);
472
+ if (value instanceof DetailedCellError) {
473
+ log.info("cell error", {
474
+ cell,
475
+ error: value
476
+ }, {
477
+ F: __dxlog_file,
478
+ L: 351,
479
+ S: this,
480
+ C: (f, a) => f(...a)
481
+ });
482
+ return value.toString();
483
+ }
484
+ return value;
485
+ }
486
+ /**
487
+ * Get value type.
488
+ */
489
+ getValueDescription(cell) {
490
+ invariant(this._node, void 0, {
491
+ F: __dxlog_file,
492
+ L: 362,
493
+ S: this,
494
+ A: [
495
+ "this._node",
496
+ ""
497
+ ]
498
+ });
499
+ const addr = toSimpleCellAddress(this._node.sheetId, cell);
500
+ const type = this._node.graph.hf.getCellValueDetailedType(addr);
501
+ return typeMap[type];
502
+ }
503
+ /**
504
+ * Sets the value, updating the sheet and engine.
505
+ */
506
+ setValue(cell, value) {
507
+ invariant(this._node, void 0, {
508
+ F: __dxlog_file,
509
+ L: 372,
510
+ S: this,
511
+ A: [
512
+ "this._node",
513
+ ""
514
+ ]
515
+ });
516
+ if (this._options.readonly) {
517
+ throw new ReadonlyException();
518
+ }
519
+ let refresh = false;
520
+ if (cell.row >= this._sheet.rows.length) {
521
+ insertIndices(this._sheet.rows, cell.row, 1, MAX_ROWS);
522
+ refresh = true;
523
+ }
524
+ if (cell.col >= this._sheet.columns.length) {
525
+ insertIndices(this._sheet.columns, cell.col, 1, MAX_COLS);
526
+ refresh = true;
527
+ }
528
+ if (refresh) {
529
+ this.reset();
530
+ }
531
+ this._node.graph.hf.setCellContents({
532
+ sheet: this._node.sheetId,
533
+ row: cell.row,
534
+ col: cell.col
535
+ }, [
536
+ [
537
+ isFormula(value) ? this._graph.mapFormulaToNative(value) : value
538
+ ]
539
+ ]);
540
+ const idx = addressToIndex(this._sheet, cell);
541
+ if (value === void 0 || value === null) {
542
+ delete this._sheet.cells[idx];
543
+ } else {
544
+ if (isFormula(value)) {
545
+ value = this._graph.mapFunctionBindingToId(mapFormulaRefsToIndices(this._sheet, value));
546
+ }
547
+ this._sheet.cells[idx] = {
548
+ value
549
+ };
550
+ }
551
+ }
552
+ /**
553
+ * Sets values from a simple map.
554
+ */
555
+ setValues(values) {
556
+ Object.entries(values).forEach(([key, { value }]) => {
557
+ this.setValue(addressFromA1Notation(key), value);
558
+ });
559
+ }
560
+ /**
561
+ * Iterate range.
562
+ */
563
+ _iterRange(range, cb) {
564
+ const to = range.to ?? range.from;
565
+ const rowRange = [
566
+ Math.min(range.from.row, to.row),
567
+ Math.max(range.from.row, to.row)
568
+ ];
569
+ const columnRange = [
570
+ Math.min(range.from.col, to.col),
571
+ Math.max(range.from.col, to.col)
572
+ ];
573
+ const rows = [];
574
+ for (let row = rowRange[0]; row <= rowRange[1]; row++) {
575
+ const rowCells = [];
576
+ for (let column = columnRange[0]; column <= columnRange[1]; column++) {
577
+ const value = cb({
578
+ row,
579
+ col: column
580
+ });
581
+ if (value !== void 0) {
582
+ rowCells.push(value);
583
+ }
584
+ }
585
+ rows.push(rowCells);
586
+ }
587
+ return rows;
588
+ }
589
+ // TODO(burdon): Delete index.
590
+ _deleteIndices(indices, i, n) {
591
+ throw new Error("Not implemented");
592
+ }
593
+ // TODO(burdon): Move. Cannot use fractional without changing. Switch back to using unique IDs?
594
+ _moveIndices(indices, i, j, n) {
595
+ throw new Error("Not implemented");
596
+ }
597
+ /**
598
+ * Map from indices to A1 notation.
599
+ */
600
+ mapFormulaIndicesToRefs(formula) {
601
+ invariant(isFormula(formula), void 0, {
602
+ F: __dxlog_file,
603
+ L: 457,
604
+ S: this,
605
+ A: [
606
+ "isFormula(formula)",
607
+ ""
608
+ ]
609
+ });
610
+ return formula.replace(/([a-zA-Z0-9]+)@([a-zA-Z0-9]+)/g, (idx) => {
611
+ return addressToA1Notation(addressFromIndex(this._sheet, idx));
612
+ });
613
+ }
614
+ //
615
+ // Values
616
+ //
617
+ /**
618
+ * https://hyperformula.handsontable.com/guide/date-and-time-handling.html#example
619
+ * https://hyperformula.handsontable.com/api/interfaces/configparams.html#nulldate
620
+ * NOTE: TODAY() is number of FULL days since nullDate. It will typically be -1 days from NOW().
621
+ */
622
+ toLocalDate(num) {
623
+ const { year, month, day, hours, minutes, seconds } = this.toDateTime(num);
624
+ return new Date(year, month - 1, day, hours, minutes, seconds);
625
+ }
626
+ toDateTime(num) {
627
+ invariant(this._node, void 0, {
628
+ F: __dxlog_file,
629
+ L: 478,
630
+ S: this,
631
+ A: [
632
+ "this._node",
633
+ ""
634
+ ]
635
+ });
636
+ return this._node.graph.hf.numberToDateTime(num);
637
+ }
638
+ toDate(num) {
639
+ invariant(this._node, void 0, {
640
+ F: __dxlog_file,
641
+ L: 483,
642
+ S: this,
643
+ A: [
644
+ "this._node",
645
+ ""
646
+ ]
647
+ });
648
+ return this._node.graph.hf.numberToDate(num);
649
+ }
650
+ toTime(num) {
651
+ invariant(this._node, void 0, {
652
+ F: __dxlog_file,
653
+ L: 488,
654
+ S: this,
655
+ A: [
656
+ "this._node",
657
+ ""
658
+ ]
659
+ });
660
+ return this._node.graph.hf.numberToTime(num);
661
+ }
662
+ };
663
+
664
+ // packages/plugins/plugin-sheet/src/model/useSheetModel.ts
665
+ import { useEffect, useState } from "react";
666
+ var useSheetModel = (graph, sheet, { readonly } = {}) => {
667
+ const [model, setModel] = useState();
668
+ useEffect(() => {
669
+ if (!graph || !sheet) {
670
+ return;
671
+ }
672
+ let model2;
673
+ const t = setTimeout(async () => {
674
+ model2 = new SheetModel(graph, sheet, {
675
+ readonly
676
+ });
677
+ await model2.open();
678
+ setModel(model2);
679
+ });
680
+ return () => {
681
+ clearTimeout(t);
682
+ void model2?.close();
683
+ };
684
+ }, [
685
+ graph,
686
+ sheet,
687
+ readonly
688
+ ]);
689
+ return model;
690
+ };
691
+
692
+ // packages/plugins/plugin-sheet/src/types/types.ts
693
+ var SheetAction;
694
+ (function(SheetAction2) {
695
+ const SHEET_ACTION = `${SHEET_PLUGIN}/action`;
696
+ class Create extends S2.TaggedClass()(`${SHEET_ACTION}/create`, {
697
+ input: S2.Struct({
698
+ name: S2.optional(S2.String)
699
+ }),
700
+ output: S2.Struct({
701
+ object: SheetType
702
+ })
703
+ }) {
704
+ }
705
+ SheetAction2.Create = Create;
706
+ const Axis = S2.Union(S2.Literal("row"), S2.Literal("col"));
707
+ class InsertAxis extends S2.TaggedClass()(`${SHEET_ACTION}/axis-insert`, {
708
+ input: S2.Struct({
709
+ // TODO(wittjosiah): S.instanceOf(SheetModel) throws when running tests.
710
+ model: S2.Any.pipe(S2.filter((model) => model instanceof SheetModel)),
711
+ axis: Axis,
712
+ index: S2.Number,
713
+ count: S2.optional(S2.Number)
714
+ }),
715
+ output: S2.Void
716
+ }) {
717
+ }
718
+ SheetAction2.InsertAxis = InsertAxis;
719
+ SheetAction2.RestoreAxis = S2.Struct({
720
+ axis: Axis,
721
+ axisIndex: S2.String,
722
+ index: S2.Number,
723
+ axisMeta: RowColumnMeta,
724
+ values: S2.Array(S2.Any)
725
+ });
726
+ class DropAxis extends S2.TaggedClass()(`${SHEET_ACTION}/axis-drop`, {
727
+ input: S2.Struct({
728
+ // TODO(wittjosiah): S.instanceOf(SheetModel) throws when running tests.
729
+ model: S2.Any.pipe(S2.filter((model) => model instanceof SheetModel)),
730
+ axis: Axis,
731
+ axisIndex: S2.String,
732
+ deletionData: S2.optional(SheetAction2.RestoreAxis)
733
+ }),
734
+ output: S2.Void
735
+ }) {
736
+ }
737
+ SheetAction2.DropAxis = DropAxis;
738
+ })(SheetAction || (SheetAction = {}));
739
+
740
+ // packages/plugins/plugin-sheet/src/types/util.ts
741
+ import { addressFromA1Notation as addressFromA1Notation2, addressToA1Notation as addressToA1Notation2, isFormula as isFormula2 } from "@dxos/compute";
742
+ import { randomBytes } from "@dxos/crypto";
743
+ import { invariant as invariant2 } from "@dxos/invariant";
744
+ import { create } from "@dxos/live-object";
745
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/plugins/plugin-sheet/src/types/util.ts";
746
+ var MAX_ROWS = 500;
747
+ var MAX_COLS = 676;
748
+ var DEFAULT_ROWS = 50;
749
+ var DEFAULT_COLS = 26;
750
+ var ApiError = class extends Error {
751
+ };
752
+ var ReadonlyException = class extends ApiError {
753
+ };
754
+ var RangeException = class extends ApiError {
755
+ constructor(n) {
756
+ super();
757
+ }
758
+ };
759
+ var createIndex = (length = 8) => {
760
+ const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
761
+ const charactersLength = characters.length;
762
+ const randomBuffer = randomBytes(length);
763
+ return Array.from(randomBuffer, (byte) => characters[byte % charactersLength]).join("");
764
+ };
765
+ var createIndices = (length) => Array.from({
766
+ length
767
+ }).map(() => createIndex());
768
+ var insertIndices = (indices, i, n, max) => {
769
+ if (i + n > max) {
770
+ throw new RangeException(i + n);
771
+ }
772
+ const idx = createIndices(n);
773
+ indices.splice(i, 0, ...idx);
774
+ return idx;
775
+ };
776
+ var initialize = (sheet, { rows = DEFAULT_ROWS, columns = DEFAULT_COLS } = {}) => {
777
+ if (!sheet.rows.length) {
778
+ insertIndices(sheet.rows, 0, rows, MAX_ROWS);
779
+ }
780
+ if (!sheet.columns.length) {
781
+ insertIndices(sheet.columns, 0, columns, MAX_COLS);
782
+ }
783
+ };
784
+ var createSheet = ({ name, cells, ...size } = {}) => {
785
+ const sheet = create(SheetType, {
786
+ name,
787
+ cells: {},
788
+ rows: [],
789
+ columns: [],
790
+ rowMeta: {},
791
+ columnMeta: {},
792
+ ranges: [],
793
+ threads: []
794
+ });
795
+ initialize(sheet, size);
796
+ if (cells) {
797
+ Object.entries(cells).forEach(([key, { value }]) => {
798
+ const idx = addressToIndex(sheet, addressFromA1Notation2(key));
799
+ if (isFormula2(value)) {
800
+ value = mapFormulaRefsToIndices(sheet, value);
801
+ }
802
+ sheet.cells[idx] = {
803
+ value
804
+ };
805
+ });
806
+ }
807
+ return sheet;
808
+ };
809
+ var addressToIndex = (sheet, cell) => {
810
+ return `${sheet.columns[cell.col]}@${sheet.rows[cell.row]}`;
811
+ };
812
+ var addressFromIndex = (sheet, idx) => {
813
+ const [column, row] = idx.split("@");
814
+ return {
815
+ col: sheet.columns.indexOf(column),
816
+ row: sheet.rows.indexOf(row)
817
+ };
818
+ };
819
+ var rangeToIndex = (sheet, range) => {
820
+ return [
821
+ range.from,
822
+ range.to ?? range.from
823
+ ].map((cell) => addressToIndex(sheet, cell)).join(":");
824
+ };
825
+ var rangeFromIndex = (sheet, idx) => {
826
+ const [from, to] = idx.split(":").map((index) => addressFromIndex(sheet, index));
827
+ return {
828
+ from,
829
+ to
830
+ };
831
+ };
832
+ var compareIndexPositions = (sheet, indexA, indexB) => {
833
+ const { row: rowA, col: columnA } = addressFromIndex(sheet, indexA);
834
+ const { row: rowB, col: columnB } = addressFromIndex(sheet, indexB);
835
+ if (rowA !== rowB) {
836
+ return rowA - rowB;
837
+ } else {
838
+ return columnA - columnB;
839
+ }
840
+ };
841
+ var mapFormulaRefsToIndices = (sheet, formula) => {
842
+ invariant2(isFormula2(formula), void 0, {
843
+ F: __dxlog_file2,
844
+ L: 153,
845
+ S: void 0,
846
+ A: [
847
+ "isFormula(formula)",
848
+ ""
849
+ ]
850
+ });
851
+ return formula.replace(/([a-zA-Z]+)([0-9]+)/g, (match) => {
852
+ return addressToIndex(sheet, addressFromA1Notation2(match));
853
+ });
854
+ };
855
+ var mapFormulaIndicesToRefs = (sheet, formula) => {
856
+ invariant2(isFormula2(formula), void 0, {
857
+ F: __dxlog_file2,
858
+ L: 163,
859
+ S: void 0,
860
+ A: [
861
+ "isFormula(formula)",
862
+ ""
863
+ ]
864
+ });
865
+ return formula.replace(/([a-zA-Z0-9]+)@([a-zA-Z0-9]+)/g, (idx) => {
866
+ return addressToA1Notation2(addressFromIndex(sheet, idx));
867
+ });
868
+ };
869
+
870
+ export {
871
+ CellValue,
872
+ Range,
873
+ RowColumnMeta,
874
+ SheetType,
875
+ alignKey,
876
+ commentKey,
877
+ styleKey,
878
+ cellClassNameForRange,
879
+ useSheetModel,
880
+ SheetAction,
881
+ MAX_ROWS,
882
+ MAX_COLS,
883
+ DEFAULT_ROWS,
884
+ DEFAULT_COLS,
885
+ ApiError,
886
+ ReadonlyException,
887
+ RangeException,
888
+ createIndex,
889
+ createIndices,
890
+ insertIndices,
891
+ initialize,
892
+ createSheet,
893
+ addressToIndex,
894
+ addressFromIndex,
895
+ rangeToIndex,
896
+ rangeFromIndex,
897
+ compareIndexPositions,
898
+ mapFormulaRefsToIndices,
899
+ mapFormulaIndicesToRefs
900
+ };
901
+ //# sourceMappingURL=chunk-NYYIDVR7.mjs.map