@ricsam/formula-engine 0.2.7 → 0.2.9

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 (101) hide show
  1. package/README.md +1 -3
  2. package/dist/cjs/core/engine.cjs +518 -149
  3. package/dist/cjs/core/engine.cjs.map +3 -3
  4. package/dist/cjs/core/managers/dependency-manager.cjs.map +2 -2
  5. package/dist/cjs/core/managers/evaluation-manager.cjs.map +1 -1
  6. package/dist/cjs/core/mutation-invalidation.cjs +241 -0
  7. package/dist/cjs/core/mutation-invalidation.cjs.map +10 -0
  8. package/dist/cjs/lib.cjs +2 -13
  9. package/dist/cjs/lib.cjs.map +3 -3
  10. package/dist/cjs/package.json +1 -1
  11. package/dist/mjs/core/engine.mjs +532 -190
  12. package/dist/mjs/core/engine.mjs.map +3 -3
  13. package/dist/mjs/core/managers/dependency-manager.mjs.map +2 -2
  14. package/dist/mjs/core/managers/evaluation-manager.mjs.map +1 -1
  15. package/dist/mjs/core/mutation-invalidation.mjs +201 -0
  16. package/dist/mjs/core/mutation-invalidation.mjs.map +10 -0
  17. package/dist/mjs/lib.mjs +2 -13
  18. package/dist/mjs/lib.mjs.map +3 -3
  19. package/dist/mjs/package.json +1 -1
  20. package/dist/types/core/engine.d.ts +17 -116
  21. package/dist/types/core/managers/dependency-manager.d.ts +1 -1
  22. package/dist/types/core/managers/evaluation-manager.d.ts +1 -1
  23. package/dist/types/core/mutation-invalidation.d.ts +46 -0
  24. package/dist/types/lib.d.ts +0 -8
  25. package/package.json +1 -1
  26. package/dist/cjs/core/commands/command-executor.cjs +0 -149
  27. package/dist/cjs/core/commands/command-executor.cjs.map +0 -10
  28. package/dist/cjs/core/commands/content-commands.cjs +0 -857
  29. package/dist/cjs/core/commands/content-commands.cjs.map +0 -10
  30. package/dist/cjs/core/commands/metadata-commands.cjs +0 -160
  31. package/dist/cjs/core/commands/metadata-commands.cjs.map +0 -10
  32. package/dist/cjs/core/commands/named-expression-commands.cjs +0 -363
  33. package/dist/cjs/core/commands/named-expression-commands.cjs.map +0 -10
  34. package/dist/cjs/core/commands/structure-commands.cjs +0 -696
  35. package/dist/cjs/core/commands/structure-commands.cjs.map +0 -10
  36. package/dist/cjs/core/commands/style-commands.cjs +0 -229
  37. package/dist/cjs/core/commands/style-commands.cjs.map +0 -10
  38. package/dist/cjs/core/commands/table-commands.cjs +0 -495
  39. package/dist/cjs/core/commands/table-commands.cjs.map +0 -10
  40. package/dist/cjs/core/commands/types.cjs +0 -99
  41. package/dist/cjs/core/commands/types.cjs.map +0 -10
  42. package/dist/cjs/core/managers/schema-manager.cjs +0 -374
  43. package/dist/cjs/core/managers/schema-manager.cjs.map +0 -10
  44. package/dist/cjs/core/schema/cell-orm.cjs +0 -76
  45. package/dist/cjs/core/schema/cell-orm.cjs.map +0 -10
  46. package/dist/cjs/core/schema/grid-orm.cjs +0 -125
  47. package/dist/cjs/core/schema/grid-orm.cjs.map +0 -10
  48. package/dist/cjs/core/schema/schema-builder.cjs +0 -74
  49. package/dist/cjs/core/schema/schema-builder.cjs.map +0 -10
  50. package/dist/cjs/core/schema/schema-helpers.cjs +0 -200
  51. package/dist/cjs/core/schema/schema-helpers.cjs.map +0 -10
  52. package/dist/cjs/core/schema/schema.cjs +0 -91
  53. package/dist/cjs/core/schema/schema.cjs.map +0 -10
  54. package/dist/cjs/core/schema/table-orm.cjs +0 -189
  55. package/dist/cjs/core/schema/table-orm.cjs.map +0 -10
  56. package/dist/mjs/core/commands/command-executor.mjs +0 -109
  57. package/dist/mjs/core/commands/command-executor.mjs.map +0 -10
  58. package/dist/mjs/core/commands/content-commands.mjs +0 -821
  59. package/dist/mjs/core/commands/content-commands.mjs.map +0 -10
  60. package/dist/mjs/core/commands/metadata-commands.mjs +0 -120
  61. package/dist/mjs/core/commands/metadata-commands.mjs.map +0 -10
  62. package/dist/mjs/core/commands/named-expression-commands.mjs +0 -323
  63. package/dist/mjs/core/commands/named-expression-commands.mjs.map +0 -10
  64. package/dist/mjs/core/commands/structure-commands.mjs +0 -661
  65. package/dist/mjs/core/commands/structure-commands.mjs.map +0 -10
  66. package/dist/mjs/core/commands/style-commands.mjs +0 -189
  67. package/dist/mjs/core/commands/style-commands.mjs.map +0 -10
  68. package/dist/mjs/core/commands/table-commands.mjs +0 -459
  69. package/dist/mjs/core/commands/table-commands.mjs.map +0 -10
  70. package/dist/mjs/core/commands/types.mjs +0 -59
  71. package/dist/mjs/core/commands/types.mjs.map +0 -10
  72. package/dist/mjs/core/managers/schema-manager.mjs +0 -334
  73. package/dist/mjs/core/managers/schema-manager.mjs.map +0 -10
  74. package/dist/mjs/core/schema/cell-orm.mjs +0 -35
  75. package/dist/mjs/core/schema/cell-orm.mjs.map +0 -10
  76. package/dist/mjs/core/schema/grid-orm.mjs +0 -84
  77. package/dist/mjs/core/schema/grid-orm.mjs.map +0 -10
  78. package/dist/mjs/core/schema/schema-builder.mjs +0 -34
  79. package/dist/mjs/core/schema/schema-builder.mjs.map +0 -10
  80. package/dist/mjs/core/schema/schema-helpers.mjs +0 -160
  81. package/dist/mjs/core/schema/schema-helpers.mjs.map +0 -10
  82. package/dist/mjs/core/schema/schema.mjs +0 -51
  83. package/dist/mjs/core/schema/schema.mjs.map +0 -10
  84. package/dist/mjs/core/schema/table-orm.mjs +0 -156
  85. package/dist/mjs/core/schema/table-orm.mjs.map +0 -10
  86. package/dist/types/core/commands/command-executor.d.ts +0 -81
  87. package/dist/types/core/commands/content-commands.d.ts +0 -172
  88. package/dist/types/core/commands/metadata-commands.d.ts +0 -55
  89. package/dist/types/core/commands/named-expression-commands.d.ts +0 -130
  90. package/dist/types/core/commands/structure-commands.d.ts +0 -155
  91. package/dist/types/core/commands/style-commands.d.ts +0 -76
  92. package/dist/types/core/commands/table-commands.d.ts +0 -119
  93. package/dist/types/core/commands/types.d.ts +0 -143
  94. package/dist/types/core/managers/schema-manager.d.ts +0 -142
  95. package/dist/types/core/schema/cell-orm.d.ts +0 -33
  96. package/dist/types/core/schema/grid-orm.d.ts +0 -63
  97. package/dist/types/core/schema/schema-builder.d.ts +0 -16
  98. package/dist/types/core/schema/schema-helpers.d.ts +0 -49
  99. package/dist/types/core/schema/schema.d.ts +0 -167
  100. package/dist/types/core/schema/schema.type-test.d.ts +0 -1
  101. package/dist/types/core/schema/table-orm.d.ts +0 -46
@@ -1,661 +0,0 @@
1
- // src/core/commands/structure-commands.ts
2
- import { ActionTypes, emptyMutationInvalidation } from "./types.mjs";
3
- import {
4
- getNamedExpressionResourceKey,
5
- getSheetResourceKey,
6
- getTableResourceKey,
7
- getWorkbookResourceKey
8
- } from "../resource-keys.mjs";
9
-
10
- class AddWorkbookCommand {
11
- deps;
12
- workbookName;
13
- requiresReevaluation = true;
14
- executeFootprint = emptyMutationInvalidation();
15
- undoFootprint = emptyMutationInvalidation();
16
- constructor(deps, workbookName) {
17
- this.deps = deps;
18
- this.workbookName = workbookName;
19
- }
20
- execute() {
21
- this.deps.workbookManager.addWorkbook(this.workbookName);
22
- this.deps.namedExpressionManager.addWorkbook(this.workbookName);
23
- this.deps.tableManager.addWorkbook(this.workbookName);
24
- this.executeFootprint = {
25
- touchedCells: [],
26
- resourceKeys: [getWorkbookResourceKey(this.workbookName)]
27
- };
28
- this.undoFootprint = {
29
- touchedCells: [],
30
- resourceKeys: [getWorkbookResourceKey(this.workbookName)],
31
- removedScopes: [{ type: "workbook", workbookName: this.workbookName }]
32
- };
33
- }
34
- undo() {
35
- this.deps.workbookManager.removeWorkbook(this.workbookName);
36
- this.deps.namedExpressionManager.removeWorkbook(this.workbookName);
37
- this.deps.tableManager.removeWorkbook(this.workbookName);
38
- }
39
- getInvalidationFootprint(phase) {
40
- return phase === "execute" ? this.executeFootprint : this.undoFootprint;
41
- }
42
- toAction() {
43
- return {
44
- type: ActionTypes.ADD_WORKBOOK,
45
- payload: { workbookName: this.workbookName }
46
- };
47
- }
48
- }
49
- function captureWorkbookSnapshot(deps, workbookName) {
50
- const workbook = deps.workbookManager.getWorkbooks().get(workbookName);
51
- if (!workbook)
52
- return;
53
- const clonedSheets = new Map;
54
- for (const [name, sheet] of workbook.sheets) {
55
- clonedSheets.set(name, {
56
- name: sheet.name,
57
- index: sheet.index,
58
- content: new Map(sheet.content),
59
- metadata: new Map(sheet.metadata),
60
- sheetMetadata: sheet.sheetMetadata
61
- });
62
- }
63
- const namedExpressions = deps.namedExpressionManager.getNamedExpressions();
64
- const snapshot = {
65
- workbook: {
66
- name: workbook.name,
67
- sheets: clonedSheets,
68
- workbookMetadata: workbook.workbookMetadata
69
- },
70
- namedExpressions: {
71
- workbookLevel: new Map(namedExpressions.workbookExpressions.get(workbookName) || []),
72
- sheetLevel: new Map
73
- },
74
- tables: new Map(deps.tableManager.getTables(workbookName)),
75
- conditionalStyles: deps.styleManager.getAllConditionalStyles().filter((s) => s.areas.some((a) => a.workbookName === workbookName)),
76
- cellStyles: deps.styleManager.getAllCellStyles().filter((s) => s.areas.some((a) => a.workbookName === workbookName))
77
- };
78
- const sheetExpressions = namedExpressions.sheetExpressions.get(workbookName);
79
- if (sheetExpressions) {
80
- for (const [sheetName, expressions] of sheetExpressions) {
81
- snapshot.namedExpressions.sheetLevel.set(sheetName, new Map(expressions));
82
- }
83
- }
84
- return snapshot;
85
- }
86
- function removeWorkbook(deps, workbookName) {
87
- deps.workbookManager.removeWorkbook(workbookName);
88
- deps.namedExpressionManager.removeWorkbook(workbookName);
89
- deps.tableManager.removeWorkbook(workbookName);
90
- deps.styleManager.removeWorkbookStyles(workbookName);
91
- deps.referenceManager.invalidateWorkbook(workbookName);
92
- }
93
- function renameWorkbookAcrossManagers(deps, oldName, newName) {
94
- deps.workbookManager.renameWorkbook({
95
- workbookName: oldName,
96
- newWorkbookName: newName
97
- });
98
- deps.namedExpressionManager.renameWorkbook({
99
- workbookName: oldName,
100
- newWorkbookName: newName
101
- });
102
- deps.tableManager.updateTablesForWorkbookRename({
103
- workbookName: oldName,
104
- newWorkbookName: newName
105
- });
106
- deps.styleManager.updateWorkbookName(oldName, newName);
107
- const changedCells = deps.workbookManager.updateAllFormulas((formula) => deps.renameWorkbookInFormula({
108
- formula,
109
- oldWorkbookName: oldName,
110
- newWorkbookName: newName
111
- }));
112
- deps.referenceManager.updateWorkbookName(oldName, newName);
113
- deps.apiSchemaManager.updateForWorkbookRename(oldName, newName);
114
- return changedCells;
115
- }
116
- function renameSheetAcrossManagers(deps, opts) {
117
- deps.workbookManager.renameSheet(opts);
118
- deps.namedExpressionManager.renameSheet(opts);
119
- deps.tableManager.updateTablesForSheetRename(opts);
120
- deps.styleManager.updateSheetName(opts.workbookName, opts.sheetName, opts.newSheetName);
121
- const changedCells = deps.workbookManager.updateAllFormulas((formula) => deps.renameSheetInFormula({
122
- formula,
123
- oldSheetName: opts.sheetName,
124
- newSheetName: opts.newSheetName
125
- }));
126
- deps.referenceManager.updateSheetName(opts.workbookName, opts.sheetName, opts.newSheetName);
127
- deps.apiSchemaManager.updateForSheetRename(opts.workbookName, opts.sheetName, opts.newSheetName);
128
- return changedCells;
129
- }
130
- function getWorkbookSnapshotResourceKeys(workbookName, snapshot) {
131
- const resourceKeys = new Set([getWorkbookResourceKey(workbookName)]);
132
- if (!snapshot) {
133
- return Array.from(resourceKeys);
134
- }
135
- for (const tableName of snapshot.tables.keys()) {
136
- resourceKeys.add(getTableResourceKey({ workbookName, tableName }));
137
- }
138
- for (const name of snapshot.namedExpressions.workbookLevel.keys()) {
139
- resourceKeys.add(getNamedExpressionResourceKey({ expressionName: name, workbookName }));
140
- }
141
- for (const [sheetName, expressions] of snapshot.namedExpressions.sheetLevel) {
142
- resourceKeys.add(getSheetResourceKey({ workbookName, sheetName }));
143
- for (const name of expressions.keys()) {
144
- resourceKeys.add(getNamedExpressionResourceKey({
145
- expressionName: name,
146
- workbookName,
147
- sheetName
148
- }));
149
- }
150
- }
151
- return Array.from(resourceKeys);
152
- }
153
- function getSheetSnapshotResourceKeys(workbookName, sheetName, snapshot) {
154
- const resourceKeys = new Set([
155
- getWorkbookResourceKey(workbookName),
156
- getSheetResourceKey({ workbookName, sheetName })
157
- ]);
158
- if (!snapshot) {
159
- return Array.from(resourceKeys);
160
- }
161
- for (const tableName of snapshot.tables.keys()) {
162
- resourceKeys.add(getTableResourceKey({ workbookName, tableName }));
163
- }
164
- for (const name of snapshot.namedExpressions.keys()) {
165
- resourceKeys.add(getNamedExpressionResourceKey({
166
- expressionName: name,
167
- workbookName,
168
- sheetName
169
- }));
170
- }
171
- return Array.from(resourceKeys);
172
- }
173
- function restoreTables(tableManager, tables, workbookName) {
174
- for (const [tableName, table] of tables) {
175
- tableManager.addTable({
176
- workbookName,
177
- tableName,
178
- sheetName: table.sheetName,
179
- start: `${String.fromCharCode(65 + table.start.colIndex)}${table.start.rowIndex + 1}`,
180
- numRows: table.endRow,
181
- numCols: table.headers.size,
182
- getCellValue: () => {
183
- return;
184
- }
185
- });
186
- }
187
- }
188
- function restoreStyles(styleManager, conditionalStyles, cellStyles) {
189
- for (const style of conditionalStyles) {
190
- styleManager.addConditionalStyle(style);
191
- }
192
- for (const style of cellStyles) {
193
- styleManager.addCellStyle(style);
194
- }
195
- }
196
-
197
- class RemoveWorkbookCommand {
198
- deps;
199
- workbookName;
200
- requiresReevaluation = true;
201
- snapshot;
202
- executeFootprint = emptyMutationInvalidation();
203
- undoFootprint = emptyMutationInvalidation();
204
- constructor(deps, workbookName) {
205
- this.deps = deps;
206
- this.workbookName = workbookName;
207
- }
208
- execute() {
209
- this.snapshot = captureWorkbookSnapshot(this.deps, this.workbookName);
210
- removeWorkbook(this.deps, this.workbookName);
211
- const resourceKeys = getWorkbookSnapshotResourceKeys(this.workbookName, this.snapshot);
212
- this.executeFootprint = {
213
- touchedCells: [],
214
- resourceKeys,
215
- removedScopes: [{ type: "workbook", workbookName: this.workbookName }]
216
- };
217
- this.undoFootprint = {
218
- touchedCells: [],
219
- resourceKeys
220
- };
221
- }
222
- undo() {
223
- if (!this.snapshot)
224
- return;
225
- this.deps.workbookManager.addWorkbook(this.workbookName);
226
- this.deps.namedExpressionManager.addWorkbook(this.workbookName);
227
- this.deps.tableManager.addWorkbook(this.workbookName);
228
- for (const [sheetName, sheet] of this.snapshot.workbook.sheets) {
229
- this.deps.workbookManager.addSheet({
230
- workbookName: this.workbookName,
231
- sheetName
232
- });
233
- this.deps.namedExpressionManager.addSheet({
234
- workbookName: this.workbookName,
235
- sheetName
236
- });
237
- this.deps.workbookManager.setSheetContent({ workbookName: this.workbookName, sheetName }, sheet.content);
238
- }
239
- for (const [name, expr] of this.snapshot.namedExpressions.workbookLevel) {
240
- this.deps.namedExpressionManager.addNamedExpression({
241
- expressionName: name,
242
- expression: expr.expression,
243
- workbookName: this.workbookName
244
- });
245
- }
246
- for (const [sheetName, expressions] of this.snapshot.namedExpressions.sheetLevel) {
247
- for (const [name, expr] of expressions) {
248
- this.deps.namedExpressionManager.addNamedExpression({
249
- expressionName: name,
250
- expression: expr.expression,
251
- workbookName: this.workbookName,
252
- sheetName
253
- });
254
- }
255
- }
256
- restoreTables(this.deps.tableManager, this.snapshot.tables, this.workbookName);
257
- restoreStyles(this.deps.styleManager, this.snapshot.conditionalStyles, this.snapshot.cellStyles);
258
- }
259
- getInvalidationFootprint(phase) {
260
- return phase === "execute" ? this.executeFootprint : this.undoFootprint;
261
- }
262
- toAction() {
263
- return {
264
- type: ActionTypes.REMOVE_WORKBOOK,
265
- payload: { workbookName: this.workbookName }
266
- };
267
- }
268
- }
269
-
270
- class RenameWorkbookCommand {
271
- deps;
272
- workbookName;
273
- newWorkbookName;
274
- requiresReevaluation = true;
275
- snapshot;
276
- executeFootprint = emptyMutationInvalidation();
277
- undoFootprint = emptyMutationInvalidation();
278
- constructor(deps, workbookName, newWorkbookName) {
279
- this.deps = deps;
280
- this.workbookName = workbookName;
281
- this.newWorkbookName = newWorkbookName;
282
- }
283
- execute() {
284
- this.snapshot = captureWorkbookSnapshot(this.deps, this.workbookName);
285
- const changedCells = renameWorkbookAcrossManagers(this.deps, this.workbookName, this.newWorkbookName);
286
- this.executeFootprint = {
287
- touchedCells: changedCells.map((address) => ({
288
- address,
289
- beforeKind: "formula",
290
- afterKind: "formula"
291
- })),
292
- resourceKeys: Array.from(new Set([
293
- ...getWorkbookSnapshotResourceKeys(this.workbookName, this.snapshot),
294
- ...getWorkbookSnapshotResourceKeys(this.newWorkbookName, this.snapshot)
295
- ]))
296
- };
297
- }
298
- undo() {
299
- const changedCells = renameWorkbookAcrossManagers(this.deps, this.newWorkbookName, this.workbookName);
300
- this.undoFootprint = {
301
- touchedCells: changedCells.map((address) => ({
302
- address,
303
- beforeKind: "formula",
304
- afterKind: "formula"
305
- })),
306
- resourceKeys: Array.from(new Set([
307
- ...getWorkbookSnapshotResourceKeys(this.workbookName, this.snapshot),
308
- ...getWorkbookSnapshotResourceKeys(this.newWorkbookName, this.snapshot)
309
- ]))
310
- };
311
- }
312
- getInvalidationFootprint(phase) {
313
- return phase === "execute" ? this.executeFootprint : this.undoFootprint;
314
- }
315
- toAction() {
316
- return {
317
- type: ActionTypes.RENAME_WORKBOOK,
318
- payload: {
319
- workbookName: this.workbookName,
320
- newWorkbookName: this.newWorkbookName
321
- }
322
- };
323
- }
324
- }
325
-
326
- class CloneWorkbookCommand {
327
- deps;
328
- fromWorkbookName;
329
- toWorkbookName;
330
- requiresReevaluation = true;
331
- executeFootprint = emptyMutationInvalidation();
332
- undoFootprint = emptyMutationInvalidation();
333
- constructor(deps, fromWorkbookName, toWorkbookName) {
334
- this.deps = deps;
335
- this.fromWorkbookName = fromWorkbookName;
336
- this.toWorkbookName = toWorkbookName;
337
- }
338
- execute() {
339
- const {
340
- workbookManager,
341
- namedExpressionManager,
342
- tableManager,
343
- styleManager,
344
- renameWorkbookInFormula
345
- } = this.deps;
346
- const sourceWorkbook = workbookManager.getWorkbooks().get(this.fromWorkbookName);
347
- if (!sourceWorkbook) {
348
- throw new Error(`Source workbook "${this.fromWorkbookName}" not found`);
349
- }
350
- if (workbookManager.getWorkbooks().has(this.toWorkbookName)) {
351
- throw new Error(`Target workbook "${this.toWorkbookName}" already exists`);
352
- }
353
- workbookManager.addWorkbook(this.toWorkbookName);
354
- namedExpressionManager.addWorkbook(this.toWorkbookName);
355
- tableManager.addWorkbook(this.toWorkbookName);
356
- for (const [sheetName, sheet] of sourceWorkbook.sheets) {
357
- workbookManager.addSheet({
358
- workbookName: this.toWorkbookName,
359
- sheetName
360
- });
361
- namedExpressionManager.addSheet({
362
- workbookName: this.toWorkbookName,
363
- sheetName
364
- });
365
- workbookManager.setSheetContent({ workbookName: this.toWorkbookName, sheetName }, new Map(sheet.content));
366
- const targetSheet = workbookManager.getSheet({
367
- workbookName: this.toWorkbookName,
368
- sheetName
369
- });
370
- if (targetSheet) {
371
- targetSheet.metadata = new Map(sheet.metadata);
372
- if (sheet.sheetMetadata !== undefined) {
373
- targetSheet.sheetMetadata = structuredClone(sheet.sheetMetadata);
374
- }
375
- }
376
- }
377
- const targetWorkbook = workbookManager.getWorkbooks().get(this.toWorkbookName);
378
- if (targetWorkbook && sourceWorkbook.workbookMetadata !== undefined) {
379
- targetWorkbook.workbookMetadata = structuredClone(sourceWorkbook.workbookMetadata);
380
- }
381
- const allExpressions = namedExpressionManager.getNamedExpressions();
382
- const sourceWorkbookExpressions = allExpressions.workbookExpressions.get(this.fromWorkbookName);
383
- if (sourceWorkbookExpressions) {
384
- for (const [expressionName, expression] of sourceWorkbookExpressions) {
385
- namedExpressionManager.addNamedExpression({
386
- expressionName,
387
- expression: expression.expression,
388
- workbookName: this.toWorkbookName
389
- });
390
- }
391
- }
392
- const sourceSheetExpressions = allExpressions.sheetExpressions.get(this.fromWorkbookName);
393
- if (sourceSheetExpressions) {
394
- for (const [sheetName, sheetExpressions] of sourceSheetExpressions) {
395
- for (const [expressionName, expression] of sheetExpressions) {
396
- namedExpressionManager.addNamedExpression({
397
- expressionName,
398
- expression: expression.expression,
399
- workbookName: this.toWorkbookName,
400
- sheetName
401
- });
402
- }
403
- }
404
- }
405
- const sourceTables = tableManager.tables.get(this.fromWorkbookName);
406
- if (sourceTables) {
407
- for (const [tableName] of sourceTables) {
408
- tableManager.copyTable({ workbookName: this.fromWorkbookName, tableName }, { workbookName: this.toWorkbookName, tableName });
409
- }
410
- }
411
- const allConditionalStyles = styleManager.getAllConditionalStyles();
412
- for (const style of allConditionalStyles) {
413
- if (style.areas.some((area) => area.workbookName === this.fromWorkbookName)) {
414
- const newStyle = {
415
- ...style,
416
- areas: style.areas.map((area) => area.workbookName === this.fromWorkbookName ? { ...area, workbookName: this.toWorkbookName } : area)
417
- };
418
- styleManager.addConditionalStyle(newStyle);
419
- }
420
- }
421
- const allCellStyles = styleManager.getAllCellStyles();
422
- for (const style of allCellStyles) {
423
- if (style.areas.some((area) => area.workbookName === this.fromWorkbookName)) {
424
- const newStyle = {
425
- ...style,
426
- areas: style.areas.map((area) => area.workbookName === this.fromWorkbookName ? { ...area, workbookName: this.toWorkbookName } : area)
427
- };
428
- styleManager.addCellStyle(newStyle);
429
- }
430
- }
431
- workbookManager.updateFormulasForWorkbook(this.toWorkbookName, (formula) => renameWorkbookInFormula({
432
- formula,
433
- oldWorkbookName: this.fromWorkbookName,
434
- newWorkbookName: this.toWorkbookName
435
- }));
436
- this.executeFootprint = {
437
- touchedCells: [],
438
- resourceKeys: [getWorkbookResourceKey(this.toWorkbookName)]
439
- };
440
- this.undoFootprint = {
441
- touchedCells: [],
442
- resourceKeys: [getWorkbookResourceKey(this.toWorkbookName)],
443
- removedScopes: [{ type: "workbook", workbookName: this.toWorkbookName }]
444
- };
445
- }
446
- undo() {
447
- removeWorkbook(this.deps, this.toWorkbookName);
448
- }
449
- getInvalidationFootprint(phase) {
450
- return phase === "execute" ? this.executeFootprint : this.undoFootprint;
451
- }
452
- toAction() {
453
- return {
454
- type: ActionTypes.CLONE_WORKBOOK,
455
- payload: {
456
- fromWorkbookName: this.fromWorkbookName,
457
- toWorkbookName: this.toWorkbookName
458
- }
459
- };
460
- }
461
- }
462
-
463
- class AddSheetCommand {
464
- deps;
465
- opts;
466
- requiresReevaluation = true;
467
- executeFootprint = emptyMutationInvalidation();
468
- undoFootprint = emptyMutationInvalidation();
469
- constructor(deps, opts) {
470
- this.deps = deps;
471
- this.opts = opts;
472
- }
473
- execute() {
474
- this.deps.workbookManager.addSheet(this.opts);
475
- this.deps.namedExpressionManager.addSheet(this.opts);
476
- const resourceKey = getSheetResourceKey(this.opts);
477
- this.executeFootprint = {
478
- touchedCells: [],
479
- resourceKeys: [resourceKey]
480
- };
481
- this.undoFootprint = {
482
- touchedCells: [],
483
- resourceKeys: [resourceKey],
484
- removedScopes: [{ type: "sheet", ...this.opts }]
485
- };
486
- }
487
- undo() {
488
- this.deps.workbookManager.removeSheet(this.opts);
489
- this.deps.namedExpressionManager.removeSheet(this.opts);
490
- }
491
- getInvalidationFootprint(phase) {
492
- return phase === "execute" ? this.executeFootprint : this.undoFootprint;
493
- }
494
- toAction() {
495
- return {
496
- type: ActionTypes.ADD_SHEET,
497
- payload: this.opts
498
- };
499
- }
500
- }
501
-
502
- class RemoveSheetCommand {
503
- deps;
504
- opts;
505
- requiresReevaluation = true;
506
- snapshot;
507
- executeFootprint = emptyMutationInvalidation();
508
- undoFootprint = emptyMutationInvalidation();
509
- constructor(deps, opts) {
510
- this.deps = deps;
511
- this.opts = opts;
512
- }
513
- execute() {
514
- const sheet = this.deps.workbookManager.getSheet(this.opts);
515
- if (sheet) {
516
- const namedExpressions = this.deps.namedExpressionManager.getNamedExpressions();
517
- const sheetExpressions = namedExpressions.sheetExpressions.get(this.opts.workbookName)?.get(this.opts.sheetName) || new Map;
518
- const allTables = this.deps.tableManager.getTables(this.opts.workbookName);
519
- const sheetTables = new Map;
520
- for (const [name, table] of allTables) {
521
- if (table.sheetName === this.opts.sheetName) {
522
- sheetTables.set(name, table);
523
- }
524
- }
525
- this.snapshot = {
526
- sheet: {
527
- name: sheet.name,
528
- index: sheet.index,
529
- content: new Map(sheet.content),
530
- metadata: new Map(sheet.metadata),
531
- sheetMetadata: sheet.sheetMetadata
532
- },
533
- namedExpressions: new Map(sheetExpressions),
534
- tables: sheetTables,
535
- conditionalStyles: this.deps.styleManager.getAllConditionalStyles().filter((s) => s.areas.some((a) => a.workbookName === this.opts.workbookName && a.sheetName === this.opts.sheetName)),
536
- cellStyles: this.deps.styleManager.getAllCellStyles().filter((s) => s.areas.some((a) => a.workbookName === this.opts.workbookName && a.sheetName === this.opts.sheetName))
537
- };
538
- }
539
- this.deps.workbookManager.removeSheet(this.opts);
540
- this.deps.namedExpressionManager.removeSheet(this.opts);
541
- this.deps.tableManager.removeSheet(this.opts);
542
- this.deps.styleManager.removeSheetStyles(this.opts.workbookName, this.opts.sheetName);
543
- this.deps.referenceManager.invalidateSheet(this.opts.workbookName, this.opts.sheetName);
544
- const resourceKeys = getSheetSnapshotResourceKeys(this.opts.workbookName, this.opts.sheetName, this.snapshot);
545
- this.executeFootprint = {
546
- touchedCells: [],
547
- resourceKeys,
548
- removedScopes: [{ type: "sheet", ...this.opts }]
549
- };
550
- this.undoFootprint = {
551
- touchedCells: [],
552
- resourceKeys
553
- };
554
- }
555
- undo() {
556
- if (!this.snapshot)
557
- return;
558
- this.deps.workbookManager.addSheet(this.opts);
559
- this.deps.namedExpressionManager.addSheet(this.opts);
560
- this.deps.workbookManager.setSheetContent(this.opts, this.snapshot.sheet.content);
561
- for (const [name, expr] of this.snapshot.namedExpressions) {
562
- this.deps.namedExpressionManager.addNamedExpression({
563
- expressionName: name,
564
- expression: expr.expression,
565
- workbookName: this.opts.workbookName,
566
- sheetName: this.opts.sheetName
567
- });
568
- }
569
- restoreTables(this.deps.tableManager, this.snapshot.tables, this.opts.workbookName);
570
- restoreStyles(this.deps.styleManager, this.snapshot.conditionalStyles, this.snapshot.cellStyles);
571
- }
572
- getInvalidationFootprint(phase) {
573
- return phase === "execute" ? this.executeFootprint : this.undoFootprint;
574
- }
575
- toAction() {
576
- return {
577
- type: ActionTypes.REMOVE_SHEET,
578
- payload: this.opts
579
- };
580
- }
581
- }
582
-
583
- class RenameSheetCommand {
584
- deps;
585
- opts;
586
- requiresReevaluation = true;
587
- snapshot;
588
- executeFootprint = emptyMutationInvalidation();
589
- undoFootprint = emptyMutationInvalidation();
590
- constructor(deps, opts) {
591
- this.deps = deps;
592
- this.opts = opts;
593
- }
594
- execute() {
595
- const namedExpressions = this.deps.namedExpressionManager.getNamedExpressions().sheetExpressions.get(this.opts.workbookName)?.get(this.opts.sheetName) || new Map;
596
- const tables = new Map;
597
- for (const [name, table] of this.deps.tableManager.getTables(this.opts.workbookName)) {
598
- if (table.sheetName === this.opts.sheetName) {
599
- tables.set(name, table);
600
- }
601
- }
602
- const sheet = this.deps.workbookManager.getSheet(this.opts);
603
- this.snapshot = {
604
- sheet,
605
- namedExpressions: new Map(namedExpressions),
606
- tables,
607
- conditionalStyles: [],
608
- cellStyles: []
609
- };
610
- const changedCells = renameSheetAcrossManagers(this.deps, this.opts);
611
- this.executeFootprint = {
612
- touchedCells: changedCells.map((address) => ({
613
- address,
614
- beforeKind: "formula",
615
- afterKind: "formula"
616
- })),
617
- resourceKeys: Array.from(new Set([
618
- ...getSheetSnapshotResourceKeys(this.opts.workbookName, this.opts.sheetName, this.snapshot),
619
- ...getSheetSnapshotResourceKeys(this.opts.workbookName, this.opts.newSheetName, this.snapshot)
620
- ]))
621
- };
622
- }
623
- undo() {
624
- const changedCells = renameSheetAcrossManagers(this.deps, {
625
- workbookName: this.opts.workbookName,
626
- sheetName: this.opts.newSheetName,
627
- newSheetName: this.opts.sheetName
628
- });
629
- this.undoFootprint = {
630
- touchedCells: changedCells.map((address) => ({
631
- address,
632
- beforeKind: "formula",
633
- afterKind: "formula"
634
- })),
635
- resourceKeys: Array.from(new Set([
636
- ...getSheetSnapshotResourceKeys(this.opts.workbookName, this.opts.sheetName, this.snapshot),
637
- ...getSheetSnapshotResourceKeys(this.opts.workbookName, this.opts.newSheetName, this.snapshot)
638
- ]))
639
- };
640
- }
641
- getInvalidationFootprint(phase) {
642
- return phase === "execute" ? this.executeFootprint : this.undoFootprint;
643
- }
644
- toAction() {
645
- return {
646
- type: ActionTypes.RENAME_SHEET,
647
- payload: this.opts
648
- };
649
- }
650
- }
651
- export {
652
- RenameWorkbookCommand,
653
- RenameSheetCommand,
654
- RemoveWorkbookCommand,
655
- RemoveSheetCommand,
656
- CloneWorkbookCommand,
657
- AddWorkbookCommand,
658
- AddSheetCommand
659
- };
660
-
661
- //# debugId=981C2E76E1D1CC3964756E2164756E21