@ricsam/formula-engine 0.2.8 → 0.2.10

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 (130) hide show
  1. package/README.md +41 -3
  2. package/dist/cjs/core/engine.cjs +551 -166
  3. package/dist/cjs/core/engine.cjs.map +3 -3
  4. package/dist/cjs/core/managers/dependency-manager.cjs +151 -67
  5. package/dist/cjs/core/managers/dependency-manager.cjs.map +3 -3
  6. package/dist/cjs/core/managers/evaluation-manager.cjs.map +1 -1
  7. package/dist/cjs/core/managers/workbook-manager.cjs +197 -1
  8. package/dist/cjs/core/managers/workbook-manager.cjs.map +3 -3
  9. package/dist/cjs/core/mutation-invalidation.cjs +241 -0
  10. package/dist/cjs/core/mutation-invalidation.cjs.map +10 -0
  11. package/dist/cjs/core/types.cjs.map +2 -2
  12. package/dist/cjs/core/workbook-renamer.cjs.map +1 -1
  13. package/dist/cjs/evaluator/formula-evaluator.cjs +22 -48
  14. package/dist/cjs/evaluator/formula-evaluator.cjs.map +3 -3
  15. package/dist/cjs/functions/math/sum/summation-utils.cjs +2 -2
  16. package/dist/cjs/functions/math/sum/summation-utils.cjs.map +2 -2
  17. package/dist/cjs/lib.cjs +2 -13
  18. package/dist/cjs/lib.cjs.map +3 -3
  19. package/dist/cjs/package.json +1 -1
  20. package/dist/cjs/parser/formatter.cjs +3 -4
  21. package/dist/cjs/parser/formatter.cjs.map +3 -3
  22. package/dist/cjs/parser/parser.cjs +24 -216
  23. package/dist/cjs/parser/parser.cjs.map +3 -3
  24. package/dist/mjs/core/engine.mjs +565 -207
  25. package/dist/mjs/core/engine.mjs.map +3 -3
  26. package/dist/mjs/core/managers/dependency-manager.mjs +151 -67
  27. package/dist/mjs/core/managers/dependency-manager.mjs.map +3 -3
  28. package/dist/mjs/core/managers/evaluation-manager.mjs.map +1 -1
  29. package/dist/mjs/core/managers/workbook-manager.mjs +197 -1
  30. package/dist/mjs/core/managers/workbook-manager.mjs.map +3 -3
  31. package/dist/mjs/core/mutation-invalidation.mjs +201 -0
  32. package/dist/mjs/core/mutation-invalidation.mjs.map +10 -0
  33. package/dist/mjs/core/types.mjs.map +2 -2
  34. package/dist/mjs/core/workbook-renamer.mjs.map +1 -1
  35. package/dist/mjs/evaluator/formula-evaluator.mjs +22 -48
  36. package/dist/mjs/evaluator/formula-evaluator.mjs.map +3 -3
  37. package/dist/mjs/functions/math/sum/summation-utils.mjs +2 -2
  38. package/dist/mjs/functions/math/sum/summation-utils.mjs.map +2 -2
  39. package/dist/mjs/lib.mjs +2 -13
  40. package/dist/mjs/lib.mjs.map +3 -3
  41. package/dist/mjs/package.json +1 -1
  42. package/dist/mjs/parser/formatter.mjs +3 -4
  43. package/dist/mjs/parser/formatter.mjs.map +3 -3
  44. package/dist/mjs/parser/parser.mjs +24 -216
  45. package/dist/mjs/parser/parser.mjs.map +3 -3
  46. package/dist/types/core/engine.d.ts +31 -135
  47. package/dist/types/core/managers/dependency-manager.d.ts +5 -1
  48. package/dist/types/core/managers/evaluation-manager.d.ts +1 -1
  49. package/dist/types/core/managers/workbook-manager.d.ts +23 -1
  50. package/dist/types/core/mutation-invalidation.d.ts +46 -0
  51. package/dist/types/core/types.d.ts +35 -0
  52. package/dist/types/lib.d.ts +0 -8
  53. package/dist/types/parser/parser.d.ts +4 -6
  54. package/package.json +1 -1
  55. package/dist/cjs/core/commands/command-executor.cjs +0 -149
  56. package/dist/cjs/core/commands/command-executor.cjs.map +0 -10
  57. package/dist/cjs/core/commands/content-commands.cjs +0 -857
  58. package/dist/cjs/core/commands/content-commands.cjs.map +0 -10
  59. package/dist/cjs/core/commands/metadata-commands.cjs +0 -160
  60. package/dist/cjs/core/commands/metadata-commands.cjs.map +0 -10
  61. package/dist/cjs/core/commands/named-expression-commands.cjs +0 -363
  62. package/dist/cjs/core/commands/named-expression-commands.cjs.map +0 -10
  63. package/dist/cjs/core/commands/structure-commands.cjs +0 -696
  64. package/dist/cjs/core/commands/structure-commands.cjs.map +0 -10
  65. package/dist/cjs/core/commands/style-commands.cjs +0 -229
  66. package/dist/cjs/core/commands/style-commands.cjs.map +0 -10
  67. package/dist/cjs/core/commands/table-commands.cjs +0 -495
  68. package/dist/cjs/core/commands/table-commands.cjs.map +0 -10
  69. package/dist/cjs/core/commands/types.cjs +0 -99
  70. package/dist/cjs/core/commands/types.cjs.map +0 -10
  71. package/dist/cjs/core/managers/schema-manager.cjs +0 -374
  72. package/dist/cjs/core/managers/schema-manager.cjs.map +0 -10
  73. package/dist/cjs/core/schema/cell-orm.cjs +0 -76
  74. package/dist/cjs/core/schema/cell-orm.cjs.map +0 -10
  75. package/dist/cjs/core/schema/grid-orm.cjs +0 -125
  76. package/dist/cjs/core/schema/grid-orm.cjs.map +0 -10
  77. package/dist/cjs/core/schema/schema-builder.cjs +0 -74
  78. package/dist/cjs/core/schema/schema-builder.cjs.map +0 -10
  79. package/dist/cjs/core/schema/schema-helpers.cjs +0 -200
  80. package/dist/cjs/core/schema/schema-helpers.cjs.map +0 -10
  81. package/dist/cjs/core/schema/schema.cjs +0 -91
  82. package/dist/cjs/core/schema/schema.cjs.map +0 -10
  83. package/dist/cjs/core/schema/table-orm.cjs +0 -189
  84. package/dist/cjs/core/schema/table-orm.cjs.map +0 -10
  85. package/dist/mjs/core/commands/command-executor.mjs +0 -109
  86. package/dist/mjs/core/commands/command-executor.mjs.map +0 -10
  87. package/dist/mjs/core/commands/content-commands.mjs +0 -821
  88. package/dist/mjs/core/commands/content-commands.mjs.map +0 -10
  89. package/dist/mjs/core/commands/metadata-commands.mjs +0 -120
  90. package/dist/mjs/core/commands/metadata-commands.mjs.map +0 -10
  91. package/dist/mjs/core/commands/named-expression-commands.mjs +0 -323
  92. package/dist/mjs/core/commands/named-expression-commands.mjs.map +0 -10
  93. package/dist/mjs/core/commands/structure-commands.mjs +0 -661
  94. package/dist/mjs/core/commands/structure-commands.mjs.map +0 -10
  95. package/dist/mjs/core/commands/style-commands.mjs +0 -189
  96. package/dist/mjs/core/commands/style-commands.mjs.map +0 -10
  97. package/dist/mjs/core/commands/table-commands.mjs +0 -459
  98. package/dist/mjs/core/commands/table-commands.mjs.map +0 -10
  99. package/dist/mjs/core/commands/types.mjs +0 -59
  100. package/dist/mjs/core/commands/types.mjs.map +0 -10
  101. package/dist/mjs/core/managers/schema-manager.mjs +0 -334
  102. package/dist/mjs/core/managers/schema-manager.mjs.map +0 -10
  103. package/dist/mjs/core/schema/cell-orm.mjs +0 -35
  104. package/dist/mjs/core/schema/cell-orm.mjs.map +0 -10
  105. package/dist/mjs/core/schema/grid-orm.mjs +0 -84
  106. package/dist/mjs/core/schema/grid-orm.mjs.map +0 -10
  107. package/dist/mjs/core/schema/schema-builder.mjs +0 -34
  108. package/dist/mjs/core/schema/schema-builder.mjs.map +0 -10
  109. package/dist/mjs/core/schema/schema-helpers.mjs +0 -160
  110. package/dist/mjs/core/schema/schema-helpers.mjs.map +0 -10
  111. package/dist/mjs/core/schema/schema.mjs +0 -51
  112. package/dist/mjs/core/schema/schema.mjs.map +0 -10
  113. package/dist/mjs/core/schema/table-orm.mjs +0 -156
  114. package/dist/mjs/core/schema/table-orm.mjs.map +0 -10
  115. package/dist/types/core/commands/command-executor.d.ts +0 -81
  116. package/dist/types/core/commands/content-commands.d.ts +0 -172
  117. package/dist/types/core/commands/metadata-commands.d.ts +0 -55
  118. package/dist/types/core/commands/named-expression-commands.d.ts +0 -130
  119. package/dist/types/core/commands/structure-commands.d.ts +0 -155
  120. package/dist/types/core/commands/style-commands.d.ts +0 -76
  121. package/dist/types/core/commands/table-commands.d.ts +0 -119
  122. package/dist/types/core/commands/types.d.ts +0 -143
  123. package/dist/types/core/managers/schema-manager.d.ts +0 -142
  124. package/dist/types/core/schema/cell-orm.d.ts +0 -33
  125. package/dist/types/core/schema/grid-orm.d.ts +0 -63
  126. package/dist/types/core/schema/schema-builder.d.ts +0 -16
  127. package/dist/types/core/schema/schema-helpers.d.ts +0 -49
  128. package/dist/types/core/schema/schema.d.ts +0 -167
  129. package/dist/types/core/schema/schema.type-test.d.ts +0 -1
  130. package/dist/types/core/schema/table-orm.d.ts +0 -46
@@ -1,821 +0,0 @@
1
- // src/core/commands/content-commands.ts
2
- import {
3
- ActionTypes,
4
- emptyMutationInvalidation,
5
- getSerializedCellValueKind
6
- } from "./types.mjs";
7
- import { getCellReference, parseCellReference } from "../utils.mjs";
8
- function buildTouchedCells(cells) {
9
- const deduped = new Map;
10
- for (const cell of cells) {
11
- deduped.set(`${cell.address.workbookName}:${cell.address.sheetName}:${getCellReference(cell.address)}`, {
12
- address: cell.address,
13
- beforeKind: getSerializedCellValueKind(cell.before),
14
- afterKind: getSerializedCellValueKind(cell.after)
15
- });
16
- }
17
- return Array.from(deduped.values());
18
- }
19
- function getAddressKey(address) {
20
- return `${address.workbookName}:${address.sheetName}:${getCellReference(address)}`;
21
- }
22
- function captureCellContents(workbookManager, addresses) {
23
- const contents = new Map;
24
- for (const address of addresses) {
25
- contents.set(getAddressKey(address), workbookManager.getCellContent(address));
26
- }
27
- return contents;
28
- }
29
-
30
- class SetCellContentCommand {
31
- workbookManager;
32
- address;
33
- newContent;
34
- requiresReevaluation = true;
35
- previousValue;
36
- hadPreviousValue = false;
37
- executeFootprint = emptyMutationInvalidation();
38
- undoFootprint = emptyMutationInvalidation();
39
- constructor(workbookManager, address, newContent) {
40
- this.workbookManager = workbookManager;
41
- this.address = address;
42
- this.newContent = newContent;
43
- }
44
- execute() {
45
- const sheet = this.workbookManager.getSheet({
46
- workbookName: this.address.workbookName,
47
- sheetName: this.address.sheetName
48
- });
49
- if (sheet) {
50
- const key = getCellReference(this.address);
51
- this.hadPreviousValue = sheet.content.has(key);
52
- this.previousValue = sheet.content.get(key);
53
- }
54
- this.workbookManager.setCellContent(this.address, this.newContent);
55
- this.executeFootprint = {
56
- touchedCells: buildTouchedCells([
57
- {
58
- address: this.address,
59
- before: this.previousValue,
60
- after: this.newContent
61
- }
62
- ]),
63
- resourceKeys: []
64
- };
65
- this.undoFootprint = {
66
- touchedCells: buildTouchedCells([
67
- {
68
- address: this.address,
69
- before: this.newContent,
70
- after: this.hadPreviousValue ? this.previousValue : undefined
71
- }
72
- ]),
73
- resourceKeys: []
74
- };
75
- }
76
- undo() {
77
- if (this.hadPreviousValue) {
78
- this.workbookManager.setCellContent(this.address, this.previousValue);
79
- } else {
80
- this.workbookManager.setCellContent(this.address, undefined);
81
- }
82
- }
83
- getInvalidationFootprint(phase) {
84
- return phase === "execute" ? this.executeFootprint : this.undoFootprint;
85
- }
86
- toAction() {
87
- return {
88
- type: ActionTypes.SET_CELL_CONTENT,
89
- payload: {
90
- address: this.address,
91
- content: this.newContent
92
- }
93
- };
94
- }
95
- }
96
-
97
- class SetSheetContentCommand {
98
- workbookManager;
99
- opts;
100
- newContent;
101
- requiresReevaluation = true;
102
- previousContent;
103
- executeFootprint = emptyMutationInvalidation();
104
- undoFootprint = emptyMutationInvalidation();
105
- constructor(workbookManager, opts, newContent) {
106
- this.workbookManager = workbookManager;
107
- this.opts = opts;
108
- this.newContent = newContent;
109
- }
110
- execute() {
111
- const sheet = this.workbookManager.getSheet(this.opts);
112
- if (sheet) {
113
- this.previousContent = new Map(sheet.content);
114
- }
115
- this.workbookManager.setSheetContent(this.opts, this.newContent);
116
- const touchedKeys = new Set([
117
- ...Array.from(this.previousContent?.keys() ?? []),
118
- ...Array.from(this.newContent.keys())
119
- ]);
120
- const touchedCells = buildTouchedCells(Array.from(touchedKeys, (key) => ({
121
- address: {
122
- workbookName: this.opts.workbookName,
123
- sheetName: this.opts.sheetName,
124
- ...parseCellReference(key)
125
- },
126
- before: this.previousContent?.get(key),
127
- after: this.newContent.get(key)
128
- })));
129
- this.executeFootprint = {
130
- touchedCells,
131
- resourceKeys: []
132
- };
133
- this.undoFootprint = {
134
- touchedCells: buildTouchedCells(Array.from(touchedKeys, (key) => ({
135
- address: {
136
- workbookName: this.opts.workbookName,
137
- sheetName: this.opts.sheetName,
138
- ...parseCellReference(key)
139
- },
140
- before: this.newContent.get(key),
141
- after: this.previousContent?.get(key)
142
- }))),
143
- resourceKeys: []
144
- };
145
- }
146
- undo() {
147
- if (this.previousContent) {
148
- this.workbookManager.setSheetContent(this.opts, this.previousContent);
149
- }
150
- }
151
- getInvalidationFootprint(phase) {
152
- return phase === "execute" ? this.executeFootprint : this.undoFootprint;
153
- }
154
- toAction() {
155
- return {
156
- type: ActionTypes.SET_SHEET_CONTENT,
157
- payload: {
158
- opts: this.opts,
159
- content: Array.from(this.newContent.entries())
160
- }
161
- };
162
- }
163
- }
164
-
165
- class ClearRangeCommand {
166
- workbookManager;
167
- address;
168
- requiresReevaluation = true;
169
- clearedCells = new Map;
170
- executeFootprint = emptyMutationInvalidation();
171
- undoFootprint = emptyMutationInvalidation();
172
- constructor(workbookManager, address) {
173
- this.workbookManager = workbookManager;
174
- this.address = address;
175
- }
176
- execute() {
177
- this.clearedCells.clear();
178
- try {
179
- for (const cellAddress of this.workbookManager.iterateCellsInRange(this.address)) {
180
- const key = getCellReference(cellAddress);
181
- const value = this.workbookManager.getCellContent(cellAddress);
182
- if (value !== undefined) {
183
- this.clearedCells.set(key, value);
184
- }
185
- }
186
- } catch {}
187
- this.workbookManager.clearSpreadsheetRange(this.address);
188
- this.executeFootprint = {
189
- touchedCells: buildTouchedCells(Array.from(this.clearedCells.entries(), ([key, before]) => ({
190
- address: {
191
- workbookName: this.address.workbookName,
192
- sheetName: this.address.sheetName,
193
- ...parseCellReference(key)
194
- },
195
- before,
196
- after: undefined
197
- }))),
198
- resourceKeys: []
199
- };
200
- this.undoFootprint = {
201
- touchedCells: buildTouchedCells(Array.from(this.clearedCells.entries(), ([key, after]) => ({
202
- address: {
203
- workbookName: this.address.workbookName,
204
- sheetName: this.address.sheetName,
205
- ...parseCellReference(key)
206
- },
207
- before: undefined,
208
- after
209
- }))),
210
- resourceKeys: []
211
- };
212
- }
213
- undo() {
214
- for (const [key, value] of this.clearedCells) {
215
- const { colIndex, rowIndex } = parseCellReference(key);
216
- this.workbookManager.setCellContent({
217
- workbookName: this.address.workbookName,
218
- sheetName: this.address.sheetName,
219
- colIndex,
220
- rowIndex
221
- }, value);
222
- }
223
- }
224
- getInvalidationFootprint(phase) {
225
- return phase === "execute" ? this.executeFootprint : this.undoFootprint;
226
- }
227
- toAction() {
228
- return {
229
- type: ActionTypes.CLEAR_RANGE,
230
- payload: {
231
- address: this.address
232
- }
233
- };
234
- }
235
- }
236
-
237
- class PasteCellsCommand {
238
- workbookManager;
239
- copyManager;
240
- source;
241
- target;
242
- options;
243
- requiresReevaluation = true;
244
- targetSnapshots = [];
245
- sourceSnapshots = [];
246
- executeFootprint = emptyMutationInvalidation();
247
- undoFootprint = emptyMutationInvalidation();
248
- executedContents = new Map;
249
- constructor(workbookManager, copyManager, source, target, options) {
250
- this.workbookManager = workbookManager;
251
- this.copyManager = copyManager;
252
- this.source = source;
253
- this.target = target;
254
- this.options = options;
255
- }
256
- execute() {
257
- this.targetSnapshots = [];
258
- this.sourceSnapshots = [];
259
- this.executedContents.clear();
260
- if (this.source.length > 0) {
261
- const firstSource = this.source[0];
262
- const colOffset = this.target.colIndex - firstSource.colIndex;
263
- const rowOffset = this.target.rowIndex - firstSource.rowIndex;
264
- for (const sourceCell of this.source) {
265
- const targetCell = {
266
- workbookName: this.target.workbookName,
267
- sheetName: this.target.sheetName,
268
- colIndex: sourceCell.colIndex + colOffset,
269
- rowIndex: sourceCell.rowIndex + rowOffset
270
- };
271
- const sheet = this.workbookManager.getSheet({
272
- workbookName: targetCell.workbookName,
273
- sheetName: targetCell.sheetName
274
- });
275
- if (sheet) {
276
- const key = getCellReference(targetCell);
277
- this.targetSnapshots.push({
278
- address: targetCell,
279
- content: sheet.content.get(key),
280
- metadata: sheet.metadata.get(key)
281
- });
282
- }
283
- }
284
- if (this.options.cut) {
285
- for (const sourceCell of this.source) {
286
- const sheet = this.workbookManager.getSheet({
287
- workbookName: sourceCell.workbookName,
288
- sheetName: sourceCell.sheetName
289
- });
290
- if (sheet) {
291
- const key = getCellReference(sourceCell);
292
- this.sourceSnapshots.push({
293
- address: sourceCell,
294
- content: sheet.content.get(key),
295
- metadata: sheet.metadata.get(key)
296
- });
297
- }
298
- }
299
- }
300
- }
301
- this.copyManager.pasteCells(this.source, this.target, this.options);
302
- const touchedAddresses = [
303
- ...this.targetSnapshots.map((snapshot) => snapshot.address),
304
- ...this.sourceSnapshots.map((snapshot) => snapshot.address)
305
- ];
306
- this.executedContents = captureCellContents(this.workbookManager, touchedAddresses);
307
- this.executeFootprint = {
308
- touchedCells: buildTouchedCells(touchedAddresses.map((address) => {
309
- const beforeSnapshot = this.targetSnapshots.find((snapshot) => snapshot.address.workbookName === address.workbookName && snapshot.address.sheetName === address.sheetName && snapshot.address.colIndex === address.colIndex && snapshot.address.rowIndex === address.rowIndex) ?? this.sourceSnapshots.find((snapshot) => snapshot.address.workbookName === address.workbookName && snapshot.address.sheetName === address.sheetName && snapshot.address.colIndex === address.colIndex && snapshot.address.rowIndex === address.rowIndex);
310
- return {
311
- address,
312
- before: beforeSnapshot?.content,
313
- after: this.executedContents.get(getAddressKey(address))
314
- };
315
- })),
316
- resourceKeys: []
317
- };
318
- }
319
- undo() {
320
- for (const snapshot of this.targetSnapshots) {
321
- this.workbookManager.setCellContent(snapshot.address, snapshot.content);
322
- if (snapshot.metadata !== undefined) {
323
- this.workbookManager.setCellMetadata(snapshot.address, snapshot.metadata);
324
- }
325
- }
326
- if (this.options.cut) {
327
- for (const snapshot of this.sourceSnapshots) {
328
- this.workbookManager.setCellContent(snapshot.address, snapshot.content);
329
- if (snapshot.metadata !== undefined) {
330
- this.workbookManager.setCellMetadata(snapshot.address, snapshot.metadata);
331
- }
332
- }
333
- }
334
- const touchedAddresses = [
335
- ...this.targetSnapshots.map((snapshot) => snapshot.address),
336
- ...this.sourceSnapshots.map((snapshot) => snapshot.address)
337
- ];
338
- this.undoFootprint = {
339
- touchedCells: buildTouchedCells(touchedAddresses.map((address) => {
340
- return {
341
- address,
342
- before: this.executedContents.get(getAddressKey(address)),
343
- after: this.workbookManager.getCellContent(address)
344
- };
345
- })),
346
- resourceKeys: []
347
- };
348
- }
349
- getInvalidationFootprint(phase) {
350
- return phase === "execute" ? this.executeFootprint : this.undoFootprint;
351
- }
352
- toAction() {
353
- return {
354
- type: ActionTypes.PASTE_CELLS,
355
- payload: {
356
- source: this.source,
357
- target: this.target,
358
- options: this.options
359
- }
360
- };
361
- }
362
- }
363
-
364
- class FillAreasCommand {
365
- workbookManager;
366
- copyManager;
367
- seedRange;
368
- targetRanges;
369
- options;
370
- requiresReevaluation = true;
371
- targetSnapshots = new Map;
372
- executeFootprint = emptyMutationInvalidation();
373
- undoFootprint = emptyMutationInvalidation();
374
- executedContents = new Map;
375
- constructor(workbookManager, copyManager, seedRange, targetRanges, options) {
376
- this.workbookManager = workbookManager;
377
- this.copyManager = copyManager;
378
- this.seedRange = seedRange;
379
- this.targetRanges = targetRanges;
380
- this.options = options;
381
- }
382
- execute() {
383
- this.targetSnapshots.clear();
384
- this.executedContents.clear();
385
- for (const targetRange of this.targetRanges) {
386
- const sheet = this.workbookManager.getSheet({
387
- workbookName: targetRange.workbookName,
388
- sheetName: targetRange.sheetName
389
- });
390
- if (sheet) {
391
- const { start, end } = targetRange.range;
392
- const endCol = end.col.type === "number" ? end.col.value : start.col + 100;
393
- const endRow = end.row.type === "number" ? end.row.value : start.row + 100;
394
- for (let col = start.col;col <= endCol; col++) {
395
- for (let row = start.row;row <= endRow; row++) {
396
- const address = {
397
- workbookName: targetRange.workbookName,
398
- sheetName: targetRange.sheetName,
399
- colIndex: col,
400
- rowIndex: row
401
- };
402
- const key = getCellReference(address);
403
- const fullKey = `${targetRange.workbookName}:${targetRange.sheetName}:${key}`;
404
- if (!this.targetSnapshots.has(fullKey)) {
405
- this.targetSnapshots.set(fullKey, {
406
- address,
407
- content: sheet.content.get(key),
408
- metadata: sheet.metadata.get(key)
409
- });
410
- }
411
- }
412
- }
413
- }
414
- }
415
- this.copyManager.fillAreas(this.seedRange, this.targetRanges, this.options);
416
- const touchedAddresses = Array.from(this.targetSnapshots.values()).map((snapshot) => snapshot.address);
417
- this.executedContents = captureCellContents(this.workbookManager, touchedAddresses);
418
- this.executeFootprint = {
419
- touchedCells: buildTouchedCells(touchedAddresses.map((address) => ({
420
- address,
421
- before: this.targetSnapshots.get(getAddressKey(address))?.content,
422
- after: this.executedContents.get(getAddressKey(address))
423
- }))),
424
- resourceKeys: []
425
- };
426
- }
427
- undo() {
428
- for (const snapshot of this.targetSnapshots.values()) {
429
- this.workbookManager.setCellContent(snapshot.address, snapshot.content);
430
- if (snapshot.metadata !== undefined) {
431
- this.workbookManager.setCellMetadata(snapshot.address, snapshot.metadata);
432
- }
433
- }
434
- const touchedAddresses = Array.from(this.targetSnapshots.values()).map((snapshot) => snapshot.address);
435
- this.undoFootprint = {
436
- touchedCells: buildTouchedCells(touchedAddresses.map((address) => ({
437
- address,
438
- before: this.executedContents.get(getAddressKey(address)),
439
- after: this.workbookManager.getCellContent(address)
440
- }))),
441
- resourceKeys: []
442
- };
443
- }
444
- getInvalidationFootprint(phase) {
445
- return phase === "execute" ? this.executeFootprint : this.undoFootprint;
446
- }
447
- toAction() {
448
- return {
449
- type: ActionTypes.FILL_AREAS,
450
- payload: {
451
- seedRange: this.seedRange,
452
- targetRanges: this.targetRanges,
453
- options: this.options
454
- }
455
- };
456
- }
457
- }
458
-
459
- class MoveCellCommand {
460
- workbookManager;
461
- copyManager;
462
- source;
463
- target;
464
- requiresReevaluation = true;
465
- sourceSnapshot;
466
- targetSnapshot;
467
- executeFootprint = emptyMutationInvalidation();
468
- undoFootprint = emptyMutationInvalidation();
469
- executedContents = new Map;
470
- constructor(workbookManager, copyManager, source, target) {
471
- this.workbookManager = workbookManager;
472
- this.copyManager = copyManager;
473
- this.source = source;
474
- this.target = target;
475
- }
476
- execute() {
477
- this.sourceSnapshot = undefined;
478
- this.targetSnapshot = undefined;
479
- this.executedContents.clear();
480
- const sourceSheet = this.workbookManager.getSheet({
481
- workbookName: this.source.workbookName,
482
- sheetName: this.source.sheetName
483
- });
484
- if (sourceSheet) {
485
- const key = getCellReference(this.source);
486
- this.sourceSnapshot = {
487
- address: this.source,
488
- content: sourceSheet.content.get(key),
489
- metadata: sourceSheet.metadata.get(key)
490
- };
491
- }
492
- const targetSheet = this.workbookManager.getSheet({
493
- workbookName: this.target.workbookName,
494
- sheetName: this.target.sheetName
495
- });
496
- if (targetSheet) {
497
- const key = getCellReference(this.target);
498
- this.targetSnapshot = {
499
- address: this.target,
500
- content: targetSheet.content.get(key),
501
- metadata: targetSheet.metadata.get(key)
502
- };
503
- }
504
- this.copyManager.pasteCells([this.source], this.target, {
505
- cut: true,
506
- type: "formula",
507
- include: "all"
508
- });
509
- const touchedAddresses = [this.source, this.target];
510
- this.executedContents = captureCellContents(this.workbookManager, touchedAddresses);
511
- this.executeFootprint = {
512
- touchedCells: buildTouchedCells(touchedAddresses.map((address) => ({
513
- address,
514
- before: this.sourceSnapshot?.address === address ? this.sourceSnapshot.content : this.targetSnapshot?.content,
515
- after: this.executedContents.get(getAddressKey(address))
516
- }))),
517
- resourceKeys: []
518
- };
519
- }
520
- undo() {
521
- if (this.sourceSnapshot) {
522
- this.workbookManager.setCellContent(this.sourceSnapshot.address, this.sourceSnapshot.content);
523
- if (this.sourceSnapshot.metadata !== undefined) {
524
- this.workbookManager.setCellMetadata(this.sourceSnapshot.address, this.sourceSnapshot.metadata);
525
- }
526
- }
527
- if (this.targetSnapshot) {
528
- this.workbookManager.setCellContent(this.targetSnapshot.address, this.targetSnapshot.content);
529
- if (this.targetSnapshot.metadata !== undefined) {
530
- this.workbookManager.setCellMetadata(this.targetSnapshot.address, this.targetSnapshot.metadata);
531
- }
532
- }
533
- const touchedAddresses = [this.source, this.target];
534
- this.undoFootprint = {
535
- touchedCells: buildTouchedCells(touchedAddresses.map((address) => ({
536
- address,
537
- before: this.executedContents.get(getAddressKey(address)),
538
- after: this.workbookManager.getCellContent(address)
539
- }))),
540
- resourceKeys: []
541
- };
542
- }
543
- getInvalidationFootprint(phase) {
544
- return phase === "execute" ? this.executeFootprint : this.undoFootprint;
545
- }
546
- toAction() {
547
- return {
548
- type: ActionTypes.MOVE_CELL,
549
- payload: {
550
- source: this.source,
551
- target: this.target
552
- }
553
- };
554
- }
555
- }
556
-
557
- class MoveRangeCommand {
558
- workbookManager;
559
- copyManager;
560
- sourceRange;
561
- target;
562
- requiresReevaluation = true;
563
- sourceSnapshots = [];
564
- targetSnapshots = [];
565
- executeFootprint = emptyMutationInvalidation();
566
- undoFootprint = emptyMutationInvalidation();
567
- executedContents = new Map;
568
- constructor(workbookManager, copyManager, sourceRange, target) {
569
- this.workbookManager = workbookManager;
570
- this.copyManager = copyManager;
571
- this.sourceRange = sourceRange;
572
- this.target = target;
573
- }
574
- execute() {
575
- this.sourceSnapshots = [];
576
- this.targetSnapshots = [];
577
- this.executedContents.clear();
578
- const sourceCells = this.copyManager.expandRangeToCells(this.sourceRange);
579
- for (const cell of sourceCells) {
580
- const sheet = this.workbookManager.getSheet({
581
- workbookName: cell.workbookName,
582
- sheetName: cell.sheetName
583
- });
584
- if (sheet) {
585
- const key = getCellReference(cell);
586
- this.sourceSnapshots.push({
587
- address: cell,
588
- content: sheet.content.get(key),
589
- metadata: sheet.metadata.get(key)
590
- });
591
- }
592
- }
593
- if (sourceCells.length > 0) {
594
- const firstSource = sourceCells[0];
595
- const colOffset = this.target.colIndex - firstSource.colIndex;
596
- const rowOffset = this.target.rowIndex - firstSource.rowIndex;
597
- for (const sourceCell of sourceCells) {
598
- const targetCell = {
599
- workbookName: this.target.workbookName,
600
- sheetName: this.target.sheetName,
601
- colIndex: sourceCell.colIndex + colOffset,
602
- rowIndex: sourceCell.rowIndex + rowOffset
603
- };
604
- const sheet = this.workbookManager.getSheet({
605
- workbookName: targetCell.workbookName,
606
- sheetName: targetCell.sheetName
607
- });
608
- if (sheet) {
609
- const key = getCellReference(targetCell);
610
- this.targetSnapshots.push({
611
- address: targetCell,
612
- content: sheet.content.get(key),
613
- metadata: sheet.metadata.get(key)
614
- });
615
- }
616
- }
617
- }
618
- this.copyManager.pasteCells(sourceCells, this.target, {
619
- cut: true,
620
- type: "formula",
621
- include: "all"
622
- });
623
- const touchedAddresses = [
624
- ...this.sourceSnapshots.map((snapshot) => snapshot.address),
625
- ...this.targetSnapshots.map((snapshot) => snapshot.address)
626
- ];
627
- this.executedContents = captureCellContents(this.workbookManager, touchedAddresses);
628
- this.executeFootprint = {
629
- touchedCells: buildTouchedCells(touchedAddresses.map((address) => {
630
- const snapshot = this.sourceSnapshots.find((candidate) => getAddressKey(candidate.address) === getAddressKey(address)) ?? this.targetSnapshots.find((candidate) => getAddressKey(candidate.address) === getAddressKey(address));
631
- return {
632
- address,
633
- before: snapshot?.content,
634
- after: this.executedContents.get(getAddressKey(address))
635
- };
636
- })),
637
- resourceKeys: []
638
- };
639
- }
640
- undo() {
641
- for (const snapshot of this.sourceSnapshots) {
642
- this.workbookManager.setCellContent(snapshot.address, snapshot.content);
643
- if (snapshot.metadata !== undefined) {
644
- this.workbookManager.setCellMetadata(snapshot.address, snapshot.metadata);
645
- }
646
- }
647
- for (const snapshot of this.targetSnapshots) {
648
- this.workbookManager.setCellContent(snapshot.address, snapshot.content);
649
- if (snapshot.metadata !== undefined) {
650
- this.workbookManager.setCellMetadata(snapshot.address, snapshot.metadata);
651
- }
652
- }
653
- const touchedAddresses = [
654
- ...this.sourceSnapshots.map((snapshot) => snapshot.address),
655
- ...this.targetSnapshots.map((snapshot) => snapshot.address)
656
- ];
657
- this.undoFootprint = {
658
- touchedCells: buildTouchedCells(touchedAddresses.map((address) => ({
659
- address,
660
- before: this.executedContents.get(getAddressKey(address)),
661
- after: this.workbookManager.getCellContent(address)
662
- }))),
663
- resourceKeys: []
664
- };
665
- }
666
- getInvalidationFootprint(phase) {
667
- return phase === "execute" ? this.executeFootprint : this.undoFootprint;
668
- }
669
- toAction() {
670
- return {
671
- type: ActionTypes.MOVE_RANGE,
672
- payload: {
673
- sourceRange: this.sourceRange,
674
- target: this.target
675
- }
676
- };
677
- }
678
- }
679
-
680
- class AutoFillCommand {
681
- workbookManager;
682
- styleManager;
683
- autoFillManager;
684
- opts;
685
- seedRange;
686
- fillRanges;
687
- direction;
688
- requiresReevaluation = true;
689
- previousContent = new Map;
690
- previousMetadata = new Map;
691
- executeFootprint = emptyMutationInvalidation();
692
- undoFootprint = emptyMutationInvalidation();
693
- touchedAddresses = [];
694
- executedContents = new Map;
695
- constructor(workbookManager, styleManager, autoFillManager, opts, seedRange, fillRanges, direction) {
696
- this.workbookManager = workbookManager;
697
- this.styleManager = styleManager;
698
- this.autoFillManager = autoFillManager;
699
- this.opts = opts;
700
- this.seedRange = seedRange;
701
- this.fillRanges = fillRanges;
702
- this.direction = direction;
703
- }
704
- execute() {
705
- this.previousContent.clear();
706
- this.previousMetadata.clear();
707
- this.touchedAddresses = [];
708
- this.executedContents.clear();
709
- for (const fillRange of this.fillRanges) {
710
- if (fillRange.end.col.type === "infinity" || fillRange.end.row.type === "infinity") {
711
- continue;
712
- }
713
- const startCol = fillRange.start.col;
714
- const startRow = fillRange.start.row;
715
- const endCol = fillRange.end.col.value;
716
- const endRow = fillRange.end.row.value;
717
- for (let row = startRow;row <= endRow; row++) {
718
- for (let col = startCol;col <= endCol; col++) {
719
- const key = getCellReference({ colIndex: col, rowIndex: row });
720
- const address = {
721
- workbookName: this.opts.workbookName,
722
- sheetName: this.opts.sheetName,
723
- colIndex: col,
724
- rowIndex: row
725
- };
726
- this.touchedAddresses.push(address);
727
- const content = this.workbookManager.getCellContent(address);
728
- if (content !== undefined) {
729
- this.previousContent.set(key, content);
730
- }
731
- const metadata = this.workbookManager.getCellMetadata(address);
732
- if (metadata !== undefined) {
733
- this.previousMetadata.set(key, metadata);
734
- }
735
- }
736
- }
737
- }
738
- this.autoFillManager.fill(this.opts, this.seedRange, this.fillRanges, this.direction);
739
- this.executedContents = captureCellContents(this.workbookManager, this.touchedAddresses);
740
- this.executeFootprint = {
741
- touchedCells: buildTouchedCells(this.touchedAddresses.map((address) => {
742
- const key = getCellReference(address);
743
- return {
744
- address,
745
- before: this.previousContent.get(key),
746
- after: this.executedContents.get(getAddressKey(address))
747
- };
748
- })),
749
- resourceKeys: []
750
- };
751
- }
752
- undo() {
753
- const currentContent = this.workbookManager.getSheetSerialized(this.opts);
754
- const newContent = new Map(currentContent);
755
- for (const fillRange of this.fillRanges) {
756
- if (fillRange.end.col.type === "infinity" || fillRange.end.row.type === "infinity") {
757
- continue;
758
- }
759
- const startCol = fillRange.start.col;
760
- const startRow = fillRange.start.row;
761
- const endCol = fillRange.end.col.value;
762
- const endRow = fillRange.end.row.value;
763
- for (let row = startRow;row <= endRow; row++) {
764
- for (let col = startCol;col <= endCol; col++) {
765
- const key = getCellReference({ colIndex: col, rowIndex: row });
766
- const address = {
767
- workbookName: this.opts.workbookName,
768
- sheetName: this.opts.sheetName,
769
- colIndex: col,
770
- rowIndex: row
771
- };
772
- if (this.previousContent.has(key)) {
773
- newContent.set(key, this.previousContent.get(key));
774
- } else {
775
- newContent.delete(key);
776
- }
777
- if (this.previousMetadata.has(key)) {
778
- this.workbookManager.setCellMetadata(address, this.previousMetadata.get(key));
779
- } else {
780
- this.workbookManager.setCellMetadata(address, undefined);
781
- }
782
- }
783
- }
784
- }
785
- this.workbookManager.setSheetContent(this.opts, newContent);
786
- this.undoFootprint = {
787
- touchedCells: buildTouchedCells(this.touchedAddresses.map((address) => ({
788
- address,
789
- before: this.executedContents.get(getAddressKey(address)),
790
- after: this.workbookManager.getCellContent(address)
791
- }))),
792
- resourceKeys: []
793
- };
794
- }
795
- getInvalidationFootprint(phase) {
796
- return phase === "execute" ? this.executeFootprint : this.undoFootprint;
797
- }
798
- toAction() {
799
- return {
800
- type: ActionTypes.AUTO_FILL,
801
- payload: {
802
- opts: this.opts,
803
- seedRange: this.seedRange,
804
- fillRanges: this.fillRanges,
805
- direction: this.direction
806
- }
807
- };
808
- }
809
- }
810
- export {
811
- SetSheetContentCommand,
812
- SetCellContentCommand,
813
- PasteCellsCommand,
814
- MoveRangeCommand,
815
- MoveCellCommand,
816
- FillAreasCommand,
817
- ClearRangeCommand,
818
- AutoFillCommand
819
- };
820
-
821
- //# debugId=827AAF04E7067A3964756E2164756E21