@odoo/o-spreadsheet 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +20 -0
- package/.husky/post-checkout +9 -0
- package/.husky/pre-commit +39 -0
- package/.prettierignore +1 -0
- package/astToFormula.patch +0 -0
- package/demo/data.js +255 -0
- package/demo/index.html +9 -0
- package/demo/libs/Chart.bundle.js +20755 -0
- package/demo/main.js +101 -0
- package/doc/readme.md +46 -0
- package/package.json +78 -0
- package/readme.md +39 -0
- package/rollup.config.js +23 -0
- package/src/base_plugin.ts +102 -0
- package/src/components/autofill.ts +146 -0
- package/src/components/bottom_bar.ts +199 -0
- package/src/components/color_picker.ts +150 -0
- package/src/components/composer/autocomplete_dropdown.ts +135 -0
- package/src/components/composer/composer.ts +487 -0
- package/src/components/composer/content_editable_helper.ts +146 -0
- package/src/components/figures/chart.ts +70 -0
- package/src/components/figures/container.ts +268 -0
- package/src/components/figures/text_figure.ts +36 -0
- package/src/components/grid.ts +635 -0
- package/src/components/helpers/dom_helpers.ts +7 -0
- package/src/components/helpers/drag_and_drop.ts +17 -0
- package/src/components/icons.ts +36 -0
- package/src/components/index.ts +1 -0
- package/src/components/menu.ts +288 -0
- package/src/components/overlay.ts +457 -0
- package/src/components/scrollbar.ts +22 -0
- package/src/components/selection_input.ts +168 -0
- package/src/components/side_panel/cell_is_rule_editor.ts +169 -0
- package/src/components/side_panel/chart_panel.ts +104 -0
- package/src/components/side_panel/color_scale_rule_editor.ts +145 -0
- package/src/components/side_panel/conditional_formatting.ts +396 -0
- package/src/components/side_panel/side_panel.ts +127 -0
- package/src/components/side_panel/translations_terms.ts +45 -0
- package/src/components/spreadsheet.ts +198 -0
- package/src/components/top_bar.ts +535 -0
- package/src/constants.ts +26 -0
- package/src/data.ts +149 -0
- package/src/fonts.ts +41 -0
- package/src/formulas/compiler.ts +258 -0
- package/src/formulas/composer_tokenizer.ts +70 -0
- package/src/formulas/index.ts +15 -0
- package/src/formulas/normalize.ts +52 -0
- package/src/formulas/parser.ts +280 -0
- package/src/formulas/range_tokenizer.ts +97 -0
- package/src/formulas/tokenizer.ts +242 -0
- package/src/functions/arguments.ts +114 -0
- package/src/functions/dates.ts +302 -0
- package/src/functions/helpers.ts +592 -0
- package/src/functions/index.ts +52 -0
- package/src/functions/module_database.ts +282 -0
- package/src/functions/module_date.ts +743 -0
- package/src/functions/module_info.ts +69 -0
- package/src/functions/module_logical.ts +208 -0
- package/src/functions/module_lookup.ts +185 -0
- package/src/functions/module_math.ts +1252 -0
- package/src/functions/module_operators.ts +291 -0
- package/src/functions/module_statistical.ts +882 -0
- package/src/functions/module_text.ts +370 -0
- package/src/helpers/color.ts +30 -0
- package/src/helpers/coordinates.ts +71 -0
- package/src/helpers/grid_manipulation.ts +68 -0
- package/src/helpers/index.ts +7 -0
- package/src/helpers/misc.ts +76 -0
- package/src/helpers/numbers.ts +117 -0
- package/src/helpers/uuid.ts +19 -0
- package/src/helpers/zones.ts +263 -0
- package/src/history.ts +186 -0
- package/src/index.ts +84 -0
- package/src/model.ts +261 -0
- package/src/plugins/autofill.ts +412 -0
- package/src/plugins/chart.ts +404 -0
- package/src/plugins/clipboard.ts +461 -0
- package/src/plugins/conditional_format.ts +445 -0
- package/src/plugins/core.ts +1502 -0
- package/src/plugins/edition.ts +157 -0
- package/src/plugins/evaluation.ts +428 -0
- package/src/plugins/figures.ts +123 -0
- package/src/plugins/formatting.ts +630 -0
- package/src/plugins/highlight.ts +187 -0
- package/src/plugins/index.ts +32 -0
- package/src/plugins/merge.ts +524 -0
- package/src/plugins/renderer.ts +571 -0
- package/src/plugins/selection.ts +562 -0
- package/src/plugins/selection_inputs.ts +326 -0
- package/src/registries/autofill_modifiers.ts +76 -0
- package/src/registries/autofill_rules.ts +98 -0
- package/src/registries/figure_registry.ts +30 -0
- package/src/registries/index.ts +7 -0
- package/src/registries/menu_items_registry.ts +110 -0
- package/src/registries/menus/cell_menu_registry.ts +79 -0
- package/src/registries/menus/col_menu_registry.ts +63 -0
- package/src/registries/menus/index.ts +6 -0
- package/src/registries/menus/menu_items_actions.ts +425 -0
- package/src/registries/menus/row_menu_registry.ts +63 -0
- package/src/registries/menus/sheet_menu_registry.ts +75 -0
- package/src/registries/menus/topbar_menu_registry.ts +204 -0
- package/src/registries/side_panel_registry.ts +26 -0
- package/src/registries/topbar_component_registry.ts +12 -0
- package/src/registry.ts +51 -0
- package/src/translation.ts +44 -0
- package/src/types/autofill.ts +48 -0
- package/src/types/chart.ts +26 -0
- package/src/types/commands.ts +716 -0
- package/src/types/conditional_formatting.ts +124 -0
- package/src/types/env.ts +13 -0
- package/src/types/functions.ts +35 -0
- package/src/types/getters.ts +103 -0
- package/src/types/index.ts +20 -0
- package/src/types/misc.ts +143 -0
- package/src/types/rendering.ts +41 -0
- package/src/types/workbook_data.ts +62 -0
- package/tests/__mocks__/uuid.ts +7 -0
- package/tests/canvas.mock.ts +34 -0
- package/tests/components/__mocks__/content_editable_helper.ts +79 -0
- package/tests/components/__mocks__/scrollbar.ts +18 -0
- package/tests/components/__snapshots__/autocomplete_dropdown_test.ts.snap +29 -0
- package/tests/components/__snapshots__/bottom_bar_test.ts.snap +75 -0
- package/tests/components/__snapshots__/context_menu_test.ts.snap +88 -0
- package/tests/components/__snapshots__/grid_test.ts.snap +60 -0
- package/tests/components/__snapshots__/spreadsheet_test.ts.snap +455 -0
- package/tests/components/__snapshots__/top_bar_test.ts.snap +705 -0
- package/tests/components/autocomplete_dropdown_test.ts +315 -0
- package/tests/components/autofill_test.ts +167 -0
- package/tests/components/bottom_bar_test.ts +262 -0
- package/tests/components/chart_side_panel_test.ts +123 -0
- package/tests/components/composer_test.ts +400 -0
- package/tests/components/context_menu_test.ts +825 -0
- package/tests/components/grid_manipulation_test.ts +163 -0
- package/tests/components/grid_test.ts +581 -0
- package/tests/components/overlay_test.ts +541 -0
- package/tests/components/selection_input_test.ts +259 -0
- package/tests/components/side_panel_test.ts +148 -0
- package/tests/components/spreadsheet_test.ts +194 -0
- package/tests/components/top_bar_test.ts +427 -0
- package/tests/data_test.ts +175 -0
- package/tests/dom_helper.ts +42 -0
- package/tests/formulas/__snapshots__/compiler_test.ts.snap +302 -0
- package/tests/formulas/compiler_test.ts +266 -0
- package/tests/formulas/composer_tokenizer_test.ts +137 -0
- package/tests/formulas/expressions_test.ts +57 -0
- package/tests/formulas/formulas_test.ts +80 -0
- package/tests/formulas/normalize_test.ts +82 -0
- package/tests/formulas/parser_test.ts +224 -0
- package/tests/formulas/range_tokenizer_test.ts +187 -0
- package/tests/formulas/tokenizer_test.ts +164 -0
- package/tests/functions/arguments_test.ts +141 -0
- package/tests/functions/dates_test.ts +1081 -0
- package/tests/functions/functions_test.ts +53 -0
- package/tests/functions/helper_test.ts +98 -0
- package/tests/functions/module_database_test.ts +311 -0
- package/tests/functions/module_date_test.ts +941 -0
- package/tests/functions/module_info_test.ts +139 -0
- package/tests/functions/module_logical_test.ts +369 -0
- package/tests/functions/module_lookup_test.ts +541 -0
- package/tests/functions/module_math_test.ts +2482 -0
- package/tests/functions/module_operators_test.ts +955 -0
- package/tests/functions/module_statistical_test.ts +2014 -0
- package/tests/functions/module_text_test.ts +667 -0
- package/tests/helpers/coordinates_test.ts +25 -0
- package/tests/helpers/misc_test.ts +95 -0
- package/tests/helpers/numbers_test.ts +196 -0
- package/tests/helpers/zones_test.ts +97 -0
- package/tests/helpers.ts +384 -0
- package/tests/menu_items_registry_test.ts +540 -0
- package/tests/model/plugins/__snapshots__/chart_test.ts.snap +90 -0
- package/tests/model/plugins/chart_test.ts +562 -0
- package/tests/model_test.ts +82 -0
- package/tests/plugins/__snapshots__/conditional_formatting_test.ts.snap +131 -0
- package/tests/plugins/__snapshots__/renderer_test.ts.snap +371 -0
- package/tests/plugins/autofill_test.ts +457 -0
- package/tests/plugins/borders_test.ts +295 -0
- package/tests/plugins/clipboard_test.ts +1180 -0
- package/tests/plugins/conditional_formatting_test.ts +1253 -0
- package/tests/plugins/core_test.ts +614 -0
- package/tests/plugins/edition_test.ts +100 -0
- package/tests/plugins/evaluation_async_test.ts +332 -0
- package/tests/plugins/evaluation_dates_test.ts +79 -0
- package/tests/plugins/evaluation_test.ts +901 -0
- package/tests/plugins/figures_test.ts +327 -0
- package/tests/plugins/formatting_test.ts +391 -0
- package/tests/plugins/grid_manipulation_test.ts +1366 -0
- package/tests/plugins/highlight_test.ts +413 -0
- package/tests/plugins/history_test.ts +142 -0
- package/tests/plugins/import_export_test.ts +276 -0
- package/tests/plugins/merges_test.ts +490 -0
- package/tests/plugins/navigation_test.ts +181 -0
- package/tests/plugins/plugins/evaluation_test.ts +111 -0
- package/tests/plugins/renderer_test.ts +598 -0
- package/tests/plugins/resizing_test.ts +152 -0
- package/tests/plugins/selection_input_test.ts +409 -0
- package/tests/plugins/selection_test.ts +442 -0
- package/tests/plugins/sheets_test.ts +643 -0
- package/tests/plugins/style_test.ts +90 -0
- package/tsconfig.json +22 -0
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
import { BasePlugin } from "../base_plugin";
|
|
2
|
+
import {
|
|
3
|
+
updateAddColumns,
|
|
4
|
+
updateAddRows,
|
|
5
|
+
updateRemoveColumns,
|
|
6
|
+
updateRemoveRows,
|
|
7
|
+
} from "../helpers/grid_manipulation";
|
|
8
|
+
import { clip, isEqual, overlap, toCartesian, toXC, union } from "../helpers/index";
|
|
9
|
+
import { _lt } from "../translation";
|
|
10
|
+
import { CancelledReason, Command, CommandResult, Merge, WorkbookData, Zone } from "../types/index";
|
|
11
|
+
|
|
12
|
+
interface PendingMerges {
|
|
13
|
+
sheet: string;
|
|
14
|
+
merges: string[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class MergePlugin extends BasePlugin {
|
|
18
|
+
static getters = [
|
|
19
|
+
"isMergeDestructive",
|
|
20
|
+
"isInMerge",
|
|
21
|
+
"getMainCell",
|
|
22
|
+
"expandZone",
|
|
23
|
+
"doesIntersectMerge",
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
private nextId: number = 1;
|
|
27
|
+
private pending: PendingMerges | null = null;
|
|
28
|
+
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
// Command Handling
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
allowDispatch(cmd: Command): CommandResult {
|
|
33
|
+
const force = "force" in cmd ? !!cmd.force : false;
|
|
34
|
+
|
|
35
|
+
switch (cmd.type) {
|
|
36
|
+
case "PASTE":
|
|
37
|
+
return this.isPasteAllowed(cmd.target, force);
|
|
38
|
+
case "ADD_MERGE":
|
|
39
|
+
return this.isMergeAllowed(cmd.zone, force);
|
|
40
|
+
default:
|
|
41
|
+
return { status: "SUCCESS" };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
beforeHandle(cmd: Command) {
|
|
46
|
+
switch (cmd.type) {
|
|
47
|
+
case "REMOVE_COLUMNS":
|
|
48
|
+
this.exportAndRemoveMerges(
|
|
49
|
+
cmd.sheet,
|
|
50
|
+
(range) => updateRemoveColumns(range, cmd.columns),
|
|
51
|
+
true
|
|
52
|
+
);
|
|
53
|
+
break;
|
|
54
|
+
case "REMOVE_ROWS":
|
|
55
|
+
this.exportAndRemoveMerges(cmd.sheet, (range) => updateRemoveRows(range, cmd.rows), false);
|
|
56
|
+
break;
|
|
57
|
+
case "ADD_COLUMNS":
|
|
58
|
+
const col = cmd.position === "before" ? cmd.column : cmd.column + 1;
|
|
59
|
+
this.exportAndRemoveMerges(
|
|
60
|
+
cmd.sheet,
|
|
61
|
+
(range) => updateAddColumns(range, col, cmd.quantity),
|
|
62
|
+
true
|
|
63
|
+
);
|
|
64
|
+
break;
|
|
65
|
+
case "ADD_ROWS":
|
|
66
|
+
const row = cmd.position === "before" ? cmd.row : cmd.row + 1;
|
|
67
|
+
this.exportAndRemoveMerges(
|
|
68
|
+
cmd.sheet,
|
|
69
|
+
(range) => updateAddRows(range, row, cmd.quantity),
|
|
70
|
+
false
|
|
71
|
+
);
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
handle(cmd: Command) {
|
|
77
|
+
switch (cmd.type) {
|
|
78
|
+
case "ADD_MERGE":
|
|
79
|
+
if (cmd.interactive) {
|
|
80
|
+
this.interactiveMerge(cmd.sheet, cmd.zone);
|
|
81
|
+
} else {
|
|
82
|
+
this.addMerge(cmd.sheet, cmd.zone);
|
|
83
|
+
}
|
|
84
|
+
break;
|
|
85
|
+
case "REMOVE_MERGE":
|
|
86
|
+
this.removeMerge(cmd.sheet, cmd.zone);
|
|
87
|
+
break;
|
|
88
|
+
case "AUTOFILL_CELL":
|
|
89
|
+
this.autoFillMerge(cmd.originCol, cmd.originRow, cmd.col, cmd.row);
|
|
90
|
+
break;
|
|
91
|
+
case "PASTE_CELL":
|
|
92
|
+
const xc = toXC(cmd.originCol, cmd.originRow);
|
|
93
|
+
if (this.isMainCell(xc, cmd.sheet)) {
|
|
94
|
+
this.duplicateMerge(xc, cmd.col, cmd.row, cmd.sheet, cmd.cut);
|
|
95
|
+
}
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
if (this.pending) {
|
|
99
|
+
this.importMerges(this.pending.sheet, this.pending.merges);
|
|
100
|
+
this.history.updateLocalState(["pending"], null);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ---------------------------------------------------------------------------
|
|
105
|
+
// Getters
|
|
106
|
+
// ---------------------------------------------------------------------------
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Return true if the current selection requires losing state if it is merged.
|
|
110
|
+
* This happens when there is some textual content in other cells than the
|
|
111
|
+
* top left.
|
|
112
|
+
*/
|
|
113
|
+
isMergeDestructive(zone: Zone): boolean {
|
|
114
|
+
const { left, right, top, bottom } = zone;
|
|
115
|
+
for (let row = top; row <= bottom; row++) {
|
|
116
|
+
const actualRow = this.workbook.activeSheet.rows[row];
|
|
117
|
+
for (let col = left; col <= right; col++) {
|
|
118
|
+
if (col !== left || row !== top) {
|
|
119
|
+
const cell = actualRow.cells[col];
|
|
120
|
+
if (cell && cell.content) {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Return true if the zone intersects an existing merge:
|
|
130
|
+
* if they have at least a common cell
|
|
131
|
+
*/
|
|
132
|
+
doesIntersectMerge(zone: Zone): boolean {
|
|
133
|
+
const { left, right, top, bottom } = zone;
|
|
134
|
+
for (let row = top; row <= bottom; row++) {
|
|
135
|
+
for (let col = left; col <= right; col++) {
|
|
136
|
+
const cellXc = toXC(col, row);
|
|
137
|
+
if (this.workbook.activeSheet.mergeCellMap[cellXc]) {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Add all necessary merge to the current selection to make it valid
|
|
147
|
+
*/
|
|
148
|
+
expandZone(zone: Zone): Zone {
|
|
149
|
+
let { left, right, top, bottom } = zone;
|
|
150
|
+
const sheet = this.workbook.activeSheet;
|
|
151
|
+
let result: Zone = { left, right, top, bottom };
|
|
152
|
+
|
|
153
|
+
for (let id in sheet.merges) {
|
|
154
|
+
const merge = sheet.merges[id];
|
|
155
|
+
if (overlap(merge, result)) {
|
|
156
|
+
result = union(merge, result);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return isEqual(result, zone) ? result : this.expandZone(result);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
isInMerge(xc: string): boolean {
|
|
163
|
+
return xc in this.workbook.activeSheet.mergeCellMap;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
isMainCell(xc: string, sheetId: string): boolean {
|
|
167
|
+
const merges = this.workbook.sheets[sheetId].merges;
|
|
168
|
+
for (let key in merges) {
|
|
169
|
+
if (merges[key].topLeft === xc) {
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
getMainCell(xc: string): string {
|
|
177
|
+
if (!this.isInMerge(xc)) {
|
|
178
|
+
return xc;
|
|
179
|
+
}
|
|
180
|
+
const sheet = this.workbook.activeSheet;
|
|
181
|
+
const merge = sheet.mergeCellMap[xc];
|
|
182
|
+
return sheet.merges[merge].topLeft;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// ---------------------------------------------------------------------------
|
|
186
|
+
// Merges
|
|
187
|
+
// ---------------------------------------------------------------------------
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Verify that we can merge without losing content of other cells or
|
|
191
|
+
* because the user gave his permission
|
|
192
|
+
*/
|
|
193
|
+
private isMergeAllowed(zone: Zone, force: boolean): CommandResult {
|
|
194
|
+
if (!force) {
|
|
195
|
+
if (this.isMergeDestructive(zone)) {
|
|
196
|
+
return {
|
|
197
|
+
status: "CANCELLED",
|
|
198
|
+
reason: CancelledReason.MergeIsDestructive,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return {
|
|
203
|
+
status: "SUCCESS",
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Merge the current selection. Note that:
|
|
209
|
+
* - it assumes that we have a valid selection (no intersection with other
|
|
210
|
+
* merges)
|
|
211
|
+
* - it does nothing if the merge is trivial: A1:A1
|
|
212
|
+
*/
|
|
213
|
+
private addMerge(sheetId: string, zone: Zone) {
|
|
214
|
+
const sheet = this.workbook.sheets[sheetId];
|
|
215
|
+
const { left, right, top, bottom } = zone;
|
|
216
|
+
const tl = toXC(left, top);
|
|
217
|
+
const br = toXC(right, bottom);
|
|
218
|
+
if (tl === br) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const topLeft = this.getters.getCell(left, top);
|
|
222
|
+
|
|
223
|
+
let id = this.nextId++;
|
|
224
|
+
this.history.updateState(["sheets", sheetId, "merges", id], {
|
|
225
|
+
id,
|
|
226
|
+
left,
|
|
227
|
+
top,
|
|
228
|
+
right,
|
|
229
|
+
bottom,
|
|
230
|
+
topLeft: tl,
|
|
231
|
+
});
|
|
232
|
+
let previousMerges: Set<number> = new Set();
|
|
233
|
+
for (let row = top; row <= bottom; row++) {
|
|
234
|
+
for (let col = left; col <= right; col++) {
|
|
235
|
+
const xc = toXC(col, row);
|
|
236
|
+
if (col !== left || row !== top) {
|
|
237
|
+
this.dispatch("UPDATE_CELL", {
|
|
238
|
+
sheet: sheetId,
|
|
239
|
+
col,
|
|
240
|
+
row,
|
|
241
|
+
style: topLeft ? topLeft.style : undefined,
|
|
242
|
+
content: undefined,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
if (sheet.mergeCellMap[xc]) {
|
|
246
|
+
previousMerges.add(sheet.mergeCellMap[xc]);
|
|
247
|
+
}
|
|
248
|
+
this.history.updateState(["sheets", sheetId, "mergeCellMap", xc], id);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
this.applyBorderMerge(zone, sheetId);
|
|
252
|
+
for (let m of previousMerges) {
|
|
253
|
+
const { top, bottom, left, right } = sheet.merges[m];
|
|
254
|
+
for (let r = top; r <= bottom; r++) {
|
|
255
|
+
for (let c = left; c <= right; c++) {
|
|
256
|
+
const xc = toXC(c, r);
|
|
257
|
+
if (sheet.mergeCellMap[xc] !== id) {
|
|
258
|
+
this.history.updateState(["sheets", sheetId, "mergeCellMap", xc], undefined);
|
|
259
|
+
this.dispatch("CLEAR_CELL", {
|
|
260
|
+
sheet: sheetId,
|
|
261
|
+
col: c,
|
|
262
|
+
row: r,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
this.history.updateState(["sheets", sheetId, "merges", m], undefined);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
private applyBorderMerge(zone: Zone, sheet: string) {
|
|
272
|
+
const { left, right, top, bottom } = zone;
|
|
273
|
+
const topLeft = this.getters.getCell(left, top);
|
|
274
|
+
const bottomRight = this.getters.getCell(right, bottom) || topLeft;
|
|
275
|
+
const bordersTopLeft = topLeft ? this.getters.getCellBorder(topLeft) : null;
|
|
276
|
+
const bordersBottomRight =
|
|
277
|
+
(bottomRight ? this.getters.getCellBorder(bottomRight) : null) || bordersTopLeft;
|
|
278
|
+
this.dispatch("SET_FORMATTING", {
|
|
279
|
+
sheet,
|
|
280
|
+
target: [{ left, right, top, bottom }],
|
|
281
|
+
border: "clear",
|
|
282
|
+
});
|
|
283
|
+
if (bordersBottomRight && bordersBottomRight.right) {
|
|
284
|
+
const zone = [{ left: right, right, top, bottom }];
|
|
285
|
+
this.dispatch("SET_FORMATTING", {
|
|
286
|
+
sheet,
|
|
287
|
+
target: zone,
|
|
288
|
+
border: "right",
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
if (bordersTopLeft && bordersTopLeft.left) {
|
|
292
|
+
const zone = [{ left, right: left, top, bottom }];
|
|
293
|
+
this.dispatch("SET_FORMATTING", {
|
|
294
|
+
sheet,
|
|
295
|
+
target: zone,
|
|
296
|
+
border: "left",
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
if (bordersTopLeft && bordersTopLeft.top) {
|
|
300
|
+
const zone = [{ left, right, top, bottom: top }];
|
|
301
|
+
this.dispatch("SET_FORMATTING", {
|
|
302
|
+
sheet,
|
|
303
|
+
target: zone,
|
|
304
|
+
border: "top",
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
if (bordersBottomRight && bordersBottomRight.bottom) {
|
|
308
|
+
const zone = [{ left, right, top: bottom, bottom }];
|
|
309
|
+
this.dispatch("SET_FORMATTING", {
|
|
310
|
+
sheet,
|
|
311
|
+
target: zone,
|
|
312
|
+
border: "bottom",
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
private removeMerge(sheetId: string, zone: Zone) {
|
|
318
|
+
const { left, top, bottom, right } = zone;
|
|
319
|
+
let tl = toXC(left, top);
|
|
320
|
+
const mergeId = this.workbook.sheets[sheetId].mergeCellMap[tl];
|
|
321
|
+
const mergeZone = this.workbook.sheets[sheetId].merges[mergeId];
|
|
322
|
+
if (!isEqual(zone, mergeZone)) {
|
|
323
|
+
throw new Error(_lt("Invalid merge zone"));
|
|
324
|
+
}
|
|
325
|
+
this.history.updateState(["sheets", sheetId, "merges", mergeId], undefined);
|
|
326
|
+
for (let r = top; r <= bottom; r++) {
|
|
327
|
+
for (let c = left; c <= right; c++) {
|
|
328
|
+
const xc = toXC(c, r);
|
|
329
|
+
this.history.updateState(["sheets", sheetId, "mergeCellMap", xc], undefined);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
private interactiveMerge(sheet: string, zone: Zone) {
|
|
335
|
+
const result = this.dispatch("ADD_MERGE", { sheet, zone });
|
|
336
|
+
|
|
337
|
+
if (result.status === "CANCELLED") {
|
|
338
|
+
if (result.reason === CancelledReason.MergeIsDestructive) {
|
|
339
|
+
this.ui.askConfirmation(
|
|
340
|
+
_lt("Merging these cells will only preserve the top-leftmost value. Merge anyway?"),
|
|
341
|
+
() => {
|
|
342
|
+
this.dispatch("ADD_MERGE", { sheet, zone, force: true });
|
|
343
|
+
}
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
private duplicateMerge(xc: string, col: number, row: number, sheetId: string, cut?: boolean) {
|
|
350
|
+
const mergeId = this.workbook.sheets[sheetId].mergeCellMap[xc];
|
|
351
|
+
const merge = this.workbook.sheets[sheetId].merges[mergeId];
|
|
352
|
+
const colNumber = this.getters.getNumberCols(sheetId) - 1;
|
|
353
|
+
const rowNumber = this.getters.getNumberRows(sheetId) - 1;
|
|
354
|
+
const newMerge = {
|
|
355
|
+
left: col,
|
|
356
|
+
top: row,
|
|
357
|
+
right: clip(col + merge.right - merge.left, 0, colNumber),
|
|
358
|
+
bottom: clip(row + merge.bottom - merge.top, 0, rowNumber),
|
|
359
|
+
};
|
|
360
|
+
if (cut) {
|
|
361
|
+
this.dispatch("REMOVE_MERGE", {
|
|
362
|
+
sheet: sheetId,
|
|
363
|
+
zone: merge,
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
this.dispatch("ADD_MERGE", {
|
|
367
|
+
sheet: this.workbook.activeSheet.id,
|
|
368
|
+
zone: newMerge,
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// ---------------------------------------------------------------------------
|
|
373
|
+
// Add/Remove columns
|
|
374
|
+
// ---------------------------------------------------------------------------
|
|
375
|
+
|
|
376
|
+
private removeAllMerges(sheetId: string) {
|
|
377
|
+
const sheet = this.workbook.sheets[sheetId];
|
|
378
|
+
for (let id in sheet.merges) {
|
|
379
|
+
this.history.updateState(["sheets", sheetId, "merges", id], undefined);
|
|
380
|
+
}
|
|
381
|
+
for (let id in sheet.mergeCellMap) {
|
|
382
|
+
this.history.updateState(["sheets", sheetId, "mergeCellMap", id], undefined);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
private exportAndRemoveMerges(
|
|
387
|
+
sheetId: string,
|
|
388
|
+
updater: (s: string) => string | null,
|
|
389
|
+
isCol: boolean
|
|
390
|
+
) {
|
|
391
|
+
const sheet = this.workbook.sheets[sheetId];
|
|
392
|
+
const merges = exportMerges(sheet.merges);
|
|
393
|
+
const updatedMerges: string[] = [];
|
|
394
|
+
for (let m of merges) {
|
|
395
|
+
const update = updater(m);
|
|
396
|
+
if (update) {
|
|
397
|
+
const [tl, br] = update.split(":");
|
|
398
|
+
if (tl !== br) {
|
|
399
|
+
updatedMerges.push(update);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
this.updateMergesStyles(sheetId, isCol);
|
|
404
|
+
this.removeAllMerges(sheetId);
|
|
405
|
+
this.history.updateLocalState(["pending"], { sheet: sheetId, merges: updatedMerges });
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
private updateMergesStyles(sheetId: string, isColumn: boolean) {
|
|
409
|
+
const sheet = this.workbook.sheets[sheetId];
|
|
410
|
+
for (let merge of Object.values(sheet.merges)) {
|
|
411
|
+
const xc = merge.topLeft;
|
|
412
|
+
const topLeft = sheet.cells[xc];
|
|
413
|
+
if (!topLeft) {
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
let [x, y] = toCartesian(xc);
|
|
417
|
+
if (isColumn && merge.left !== merge.right) {
|
|
418
|
+
x += 1;
|
|
419
|
+
}
|
|
420
|
+
if (!isColumn && merge.top !== merge.bottom) {
|
|
421
|
+
y += 1;
|
|
422
|
+
}
|
|
423
|
+
this.dispatch("UPDATE_CELL", {
|
|
424
|
+
sheet: sheet.id,
|
|
425
|
+
col: x,
|
|
426
|
+
row: y,
|
|
427
|
+
style: topLeft.style,
|
|
428
|
+
border: topLeft.border,
|
|
429
|
+
format: topLeft.format,
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// ---------------------------------------------------------------------------
|
|
435
|
+
// Copy/Cut/Paste and Merge
|
|
436
|
+
// ---------------------------------------------------------------------------
|
|
437
|
+
|
|
438
|
+
private isPasteAllowed(target: Zone[], force: boolean): CommandResult {
|
|
439
|
+
if (!force) {
|
|
440
|
+
const pasteZones = this.getters.getPasteZones(target);
|
|
441
|
+
for (let zone of pasteZones) {
|
|
442
|
+
if (this.doesIntersectMerge(zone)) {
|
|
443
|
+
return {
|
|
444
|
+
status: "CANCELLED",
|
|
445
|
+
reason: CancelledReason.WillRemoveExistingMerge,
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
return {
|
|
451
|
+
status: "SUCCESS",
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// ---------------------------------------------------------------------------
|
|
456
|
+
// Autofill
|
|
457
|
+
// ---------------------------------------------------------------------------
|
|
458
|
+
|
|
459
|
+
private autoFillMerge(originCol: number, originRow: number, col: number, row: number) {
|
|
460
|
+
const xcOrigin = toXC(originCol, originRow);
|
|
461
|
+
const xcTarget = toXC(col, row);
|
|
462
|
+
const sheet = this.getters.getActiveSheet();
|
|
463
|
+
if (this.isInMerge(xcTarget) && !this.isInMerge(xcOrigin)) {
|
|
464
|
+
const mergeId = this.workbook.sheets[sheet].mergeCellMap[xcTarget];
|
|
465
|
+
const zone = this.workbook.sheets[sheet].merges[mergeId];
|
|
466
|
+
this.dispatch("REMOVE_MERGE", {
|
|
467
|
+
sheet,
|
|
468
|
+
zone,
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
if (this.isMainCell(xcOrigin, sheet)) {
|
|
472
|
+
this.duplicateMerge(xcOrigin, col, row, sheet);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// ---------------------------------------------------------------------------
|
|
477
|
+
// Import/Export
|
|
478
|
+
// ---------------------------------------------------------------------------
|
|
479
|
+
|
|
480
|
+
import(data: WorkbookData) {
|
|
481
|
+
const sheets = data.sheets || [];
|
|
482
|
+
for (let sheetData of sheets) {
|
|
483
|
+
const sheet = this.workbook.sheets[sheetData.id];
|
|
484
|
+
if (sheet && sheetData.merges) {
|
|
485
|
+
this.importMerges(sheet.id, sheetData.merges);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
private importMerges(sheetId: string, merges: string[]) {
|
|
491
|
+
for (let m of merges) {
|
|
492
|
+
let id = this.nextId++;
|
|
493
|
+
const [tl, br] = m.split(":");
|
|
494
|
+
const [left, top] = toCartesian(tl);
|
|
495
|
+
const [right, bottom] = toCartesian(br);
|
|
496
|
+
this.history.updateState(["sheets", sheetId, "merges", id], {
|
|
497
|
+
id,
|
|
498
|
+
left,
|
|
499
|
+
top,
|
|
500
|
+
right,
|
|
501
|
+
bottom,
|
|
502
|
+
topLeft: tl,
|
|
503
|
+
});
|
|
504
|
+
for (let row = top; row <= bottom; row++) {
|
|
505
|
+
for (let col = left; col <= right; col++) {
|
|
506
|
+
const xc = toXC(col, row);
|
|
507
|
+
this.history.updateState(["sheets", sheetId, "mergeCellMap", xc], id);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
export(data: WorkbookData) {
|
|
513
|
+
for (let sheetData of data.sheets) {
|
|
514
|
+
const sheet = this.workbook.sheets[sheetData.id];
|
|
515
|
+
sheetData.merges.push(...exportMerges(sheet.merges));
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function exportMerges(merges: { [key: number]: Merge }): string[] {
|
|
521
|
+
return Object.values(merges).map(
|
|
522
|
+
(merge) => toXC(merge.left, merge.top) + ":" + toXC(merge.right, merge.bottom)
|
|
523
|
+
);
|
|
524
|
+
}
|