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