@odoo/o-spreadsheet 14.0.0 → 14.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.
Files changed (198) hide show
  1. package/.eslintrc.json +20 -0
  2. package/.husky/post-checkout +9 -0
  3. package/.husky/pre-commit +39 -0
  4. package/.prettierignore +1 -0
  5. package/astToFormula.patch +0 -0
  6. package/demo/data.js +255 -0
  7. package/demo/index.html +9 -0
  8. package/demo/libs/Chart.bundle.js +20755 -0
  9. package/demo/main.js +101 -0
  10. package/doc/readme.md +46 -0
  11. package/package.json +3 -17
  12. package/rollup.config.js +23 -0
  13. package/src/base_plugin.ts +102 -0
  14. package/src/components/autofill.ts +146 -0
  15. package/src/components/bottom_bar.ts +199 -0
  16. package/src/components/color_picker.ts +150 -0
  17. package/src/components/composer/autocomplete_dropdown.ts +135 -0
  18. package/src/components/composer/composer.ts +487 -0
  19. package/src/components/composer/content_editable_helper.ts +146 -0
  20. package/src/components/figures/chart.ts +70 -0
  21. package/src/components/figures/container.ts +268 -0
  22. package/src/components/figures/text_figure.ts +36 -0
  23. package/src/components/grid.ts +635 -0
  24. package/src/components/helpers/dom_helpers.ts +7 -0
  25. package/src/components/helpers/drag_and_drop.ts +17 -0
  26. package/src/components/icons.ts +36 -0
  27. package/src/components/index.ts +1 -0
  28. package/src/components/menu.ts +288 -0
  29. package/src/components/overlay.ts +457 -0
  30. package/src/components/scrollbar.ts +22 -0
  31. package/src/components/selection_input.ts +168 -0
  32. package/src/components/side_panel/cell_is_rule_editor.ts +169 -0
  33. package/src/components/side_panel/chart_panel.ts +104 -0
  34. package/src/components/side_panel/color_scale_rule_editor.ts +145 -0
  35. package/src/components/side_panel/conditional_formatting.ts +396 -0
  36. package/src/components/side_panel/side_panel.ts +127 -0
  37. package/src/components/side_panel/translations_terms.ts +45 -0
  38. package/src/components/spreadsheet.ts +198 -0
  39. package/src/components/top_bar.ts +535 -0
  40. package/src/constants.ts +26 -0
  41. package/src/data.ts +149 -0
  42. package/src/fonts.ts +41 -0
  43. package/src/formulas/compiler.ts +258 -0
  44. package/src/formulas/composer_tokenizer.ts +70 -0
  45. package/src/formulas/index.ts +15 -0
  46. package/src/formulas/normalize.ts +52 -0
  47. package/src/formulas/parser.ts +280 -0
  48. package/src/formulas/range_tokenizer.ts +97 -0
  49. package/src/formulas/tokenizer.ts +242 -0
  50. package/src/functions/arguments.ts +114 -0
  51. package/src/functions/dates.ts +302 -0
  52. package/src/functions/helpers.ts +592 -0
  53. package/src/functions/index.ts +52 -0
  54. package/src/functions/module_database.ts +282 -0
  55. package/src/functions/module_date.ts +743 -0
  56. package/src/functions/module_info.ts +69 -0
  57. package/src/functions/module_logical.ts +208 -0
  58. package/src/functions/module_lookup.ts +185 -0
  59. package/src/functions/module_math.ts +1252 -0
  60. package/src/functions/module_operators.ts +291 -0
  61. package/src/functions/module_statistical.ts +882 -0
  62. package/src/functions/module_text.ts +370 -0
  63. package/src/helpers/color.ts +30 -0
  64. package/src/helpers/coordinates.ts +71 -0
  65. package/src/helpers/grid_manipulation.ts +68 -0
  66. package/src/helpers/index.ts +7 -0
  67. package/src/helpers/misc.ts +76 -0
  68. package/src/helpers/numbers.ts +117 -0
  69. package/src/helpers/uuid.ts +19 -0
  70. package/src/helpers/zones.ts +263 -0
  71. package/src/history.ts +186 -0
  72. package/src/index.ts +84 -0
  73. package/src/model.ts +261 -0
  74. package/src/plugins/autofill.ts +412 -0
  75. package/src/plugins/chart.ts +404 -0
  76. package/src/plugins/clipboard.ts +461 -0
  77. package/src/plugins/conditional_format.ts +445 -0
  78. package/src/plugins/core.ts +1502 -0
  79. package/src/plugins/edition.ts +157 -0
  80. package/src/plugins/evaluation.ts +428 -0
  81. package/src/plugins/figures.ts +123 -0
  82. package/src/plugins/formatting.ts +630 -0
  83. package/src/plugins/highlight.ts +187 -0
  84. package/src/plugins/index.ts +32 -0
  85. package/src/plugins/merge.ts +524 -0
  86. package/src/plugins/renderer.ts +571 -0
  87. package/src/plugins/selection.ts +562 -0
  88. package/src/plugins/selection_inputs.ts +326 -0
  89. package/src/registries/autofill_modifiers.ts +76 -0
  90. package/src/registries/autofill_rules.ts +98 -0
  91. package/src/registries/figure_registry.ts +30 -0
  92. package/src/registries/index.ts +7 -0
  93. package/src/registries/menu_items_registry.ts +110 -0
  94. package/src/registries/menus/cell_menu_registry.ts +79 -0
  95. package/src/registries/menus/col_menu_registry.ts +63 -0
  96. package/src/registries/menus/index.ts +6 -0
  97. package/src/registries/menus/menu_items_actions.ts +425 -0
  98. package/src/registries/menus/row_menu_registry.ts +63 -0
  99. package/src/registries/menus/sheet_menu_registry.ts +75 -0
  100. package/src/registries/menus/topbar_menu_registry.ts +204 -0
  101. package/src/registries/side_panel_registry.ts +26 -0
  102. package/src/registries/topbar_component_registry.ts +12 -0
  103. package/src/registry.ts +51 -0
  104. package/src/translation.ts +44 -0
  105. package/src/types/autofill.ts +48 -0
  106. package/src/types/chart.ts +26 -0
  107. package/src/types/commands.ts +716 -0
  108. package/src/types/conditional_formatting.ts +124 -0
  109. package/src/types/env.ts +13 -0
  110. package/src/types/functions.ts +35 -0
  111. package/src/types/getters.ts +103 -0
  112. package/src/types/index.ts +20 -0
  113. package/src/types/misc.ts +143 -0
  114. package/src/types/rendering.ts +41 -0
  115. package/src/types/workbook_data.ts +62 -0
  116. package/tests/__mocks__/uuid.ts +7 -0
  117. package/tests/canvas.mock.ts +34 -0
  118. package/tests/components/__mocks__/content_editable_helper.ts +79 -0
  119. package/tests/components/__mocks__/scrollbar.ts +18 -0
  120. package/tests/components/__snapshots__/autocomplete_dropdown_test.ts.snap +29 -0
  121. package/tests/components/__snapshots__/bottom_bar_test.ts.snap +75 -0
  122. package/tests/components/__snapshots__/context_menu_test.ts.snap +88 -0
  123. package/tests/components/__snapshots__/grid_test.ts.snap +60 -0
  124. package/tests/components/__snapshots__/spreadsheet_test.ts.snap +455 -0
  125. package/tests/components/__snapshots__/top_bar_test.ts.snap +705 -0
  126. package/tests/components/autocomplete_dropdown_test.ts +315 -0
  127. package/tests/components/autofill_test.ts +167 -0
  128. package/tests/components/bottom_bar_test.ts +262 -0
  129. package/tests/components/chart_side_panel_test.ts +123 -0
  130. package/tests/components/composer_test.ts +400 -0
  131. package/tests/components/context_menu_test.ts +825 -0
  132. package/tests/components/grid_manipulation_test.ts +163 -0
  133. package/tests/components/grid_test.ts +581 -0
  134. package/tests/components/overlay_test.ts +541 -0
  135. package/tests/components/selection_input_test.ts +259 -0
  136. package/tests/components/side_panel_test.ts +148 -0
  137. package/tests/components/spreadsheet_test.ts +194 -0
  138. package/tests/components/top_bar_test.ts +427 -0
  139. package/tests/data_test.ts +175 -0
  140. package/tests/dom_helper.ts +42 -0
  141. package/tests/formulas/__snapshots__/compiler_test.ts.snap +302 -0
  142. package/tests/formulas/compiler_test.ts +266 -0
  143. package/tests/formulas/composer_tokenizer_test.ts +137 -0
  144. package/tests/formulas/expressions_test.ts +57 -0
  145. package/tests/formulas/formulas_test.ts +80 -0
  146. package/tests/formulas/normalize_test.ts +82 -0
  147. package/tests/formulas/parser_test.ts +224 -0
  148. package/tests/formulas/range_tokenizer_test.ts +187 -0
  149. package/tests/formulas/tokenizer_test.ts +164 -0
  150. package/tests/functions/arguments_test.ts +141 -0
  151. package/tests/functions/dates_test.ts +1081 -0
  152. package/tests/functions/functions_test.ts +53 -0
  153. package/tests/functions/helper_test.ts +98 -0
  154. package/tests/functions/module_database_test.ts +311 -0
  155. package/tests/functions/module_date_test.ts +941 -0
  156. package/tests/functions/module_info_test.ts +139 -0
  157. package/tests/functions/module_logical_test.ts +369 -0
  158. package/tests/functions/module_lookup_test.ts +541 -0
  159. package/tests/functions/module_math_test.ts +2482 -0
  160. package/tests/functions/module_operators_test.ts +955 -0
  161. package/tests/functions/module_statistical_test.ts +2014 -0
  162. package/tests/functions/module_text_test.ts +667 -0
  163. package/tests/helpers/coordinates_test.ts +25 -0
  164. package/tests/helpers/misc_test.ts +95 -0
  165. package/tests/helpers/numbers_test.ts +196 -0
  166. package/tests/helpers/zones_test.ts +97 -0
  167. package/tests/helpers.ts +384 -0
  168. package/tests/menu_items_registry_test.ts +540 -0
  169. package/tests/model/plugins/__snapshots__/chart_test.ts.snap +90 -0
  170. package/tests/model/plugins/chart_test.ts +562 -0
  171. package/tests/model_test.ts +82 -0
  172. package/tests/plugins/__snapshots__/conditional_formatting_test.ts.snap +131 -0
  173. package/tests/plugins/__snapshots__/renderer_test.ts.snap +371 -0
  174. package/tests/plugins/autofill_test.ts +457 -0
  175. package/tests/plugins/borders_test.ts +295 -0
  176. package/tests/plugins/clipboard_test.ts +1180 -0
  177. package/tests/plugins/conditional_formatting_test.ts +1253 -0
  178. package/tests/plugins/core_test.ts +614 -0
  179. package/tests/plugins/edition_test.ts +100 -0
  180. package/tests/plugins/evaluation_async_test.ts +332 -0
  181. package/tests/plugins/evaluation_dates_test.ts +79 -0
  182. package/tests/plugins/evaluation_test.ts +901 -0
  183. package/tests/plugins/figures_test.ts +327 -0
  184. package/tests/plugins/formatting_test.ts +391 -0
  185. package/tests/plugins/grid_manipulation_test.ts +1366 -0
  186. package/tests/plugins/highlight_test.ts +413 -0
  187. package/tests/plugins/history_test.ts +142 -0
  188. package/tests/plugins/import_export_test.ts +276 -0
  189. package/tests/plugins/merges_test.ts +490 -0
  190. package/tests/plugins/navigation_test.ts +181 -0
  191. package/tests/plugins/plugins/evaluation_test.ts +111 -0
  192. package/tests/plugins/renderer_test.ts +598 -0
  193. package/tests/plugins/resizing_test.ts +152 -0
  194. package/tests/plugins/selection_input_test.ts +409 -0
  195. package/tests/plugins/selection_test.ts +442 -0
  196. package/tests/plugins/sheets_test.ts +643 -0
  197. package/tests/plugins/style_test.ts +90 -0
  198. package/tsconfig.json +22 -0
@@ -0,0 +1,562 @@
1
+ import { BasePlugin } from "../base_plugin";
2
+ import { SELECTION_BORDER_COLOR } from "../constants";
3
+ import { clip, formatStandardNumber, isEqual, toXC, union } from "../helpers/index";
4
+ import { Mode } from "../model";
5
+ import {
6
+ CancelledReason,
7
+ Cell,
8
+ Command,
9
+ CommandResult,
10
+ GridRenderingContext,
11
+ LAYERS,
12
+ Zone,
13
+ } from "../types/index";
14
+
15
+ export interface Selection {
16
+ anchor: [number, number];
17
+ zones: Zone[];
18
+ }
19
+
20
+ interface SheetInfo {
21
+ selection: Selection;
22
+ activeCol: number;
23
+ activeRow: number;
24
+ activeXc: string;
25
+ }
26
+
27
+ export enum SelectionMode {
28
+ idle,
29
+ selecting,
30
+ readyToExpand, //The next selection will add another zone to the current selection
31
+ expanding,
32
+ }
33
+
34
+ /**
35
+ * SelectionPlugin
36
+ */
37
+ export class SelectionPlugin extends BasePlugin {
38
+ static layers = [LAYERS.Selection];
39
+ static modes: Mode[] = ["normal", "readonly"];
40
+ static getters = [
41
+ "getActiveCell",
42
+ "getActiveCols",
43
+ "getActiveRows",
44
+ "getSelectedZones",
45
+ "getSelectedZone",
46
+ "getAggregate",
47
+ "getSelection",
48
+ "getPosition",
49
+ "getSelectionMode",
50
+ "isSelected",
51
+ ];
52
+
53
+ private selection: Selection = {
54
+ zones: [{ top: 0, left: 0, bottom: 0, right: 0 }],
55
+ anchor: [0, 0],
56
+ };
57
+ private activeCol: number = 0;
58
+ private activeRow: number = 0;
59
+ private activeXc: string = "A1";
60
+ private mode = SelectionMode.idle;
61
+ private sheetsData: { [sheet: string]: SheetInfo } = {};
62
+
63
+ // ---------------------------------------------------------------------------
64
+ // Command Handling
65
+ // ---------------------------------------------------------------------------
66
+
67
+ allowDispatch(cmd: Command): CommandResult {
68
+ switch (cmd.type) {
69
+ case "MOVE_POSITION": {
70
+ const [refCol, refRow] = this.getReferenceCoords();
71
+ const { cols, rows } = this.workbook.activeSheet;
72
+ const outOfBound =
73
+ (cmd.deltaY < 0 && refRow === 0) ||
74
+ (cmd.deltaY > 0 && refRow === rows.length - 1) ||
75
+ (cmd.deltaX < 0 && refCol === 0) ||
76
+ (cmd.deltaX > 0 && refCol === cols.length - 1);
77
+ if (outOfBound) {
78
+ return {
79
+ status: "CANCELLED",
80
+ reason: CancelledReason.SelectionOutOfBound,
81
+ };
82
+ }
83
+ break;
84
+ }
85
+ case "SELECT_COLUMN": {
86
+ const { index } = cmd;
87
+ if (index < 0 || index >= this.workbook.activeSheet.cols.length) {
88
+ return {
89
+ status: "CANCELLED",
90
+ reason: CancelledReason.SelectionOutOfBound,
91
+ };
92
+ }
93
+ break;
94
+ }
95
+ case "SELECT_ROW": {
96
+ const { index } = cmd;
97
+ if (index < 0 || index >= this.workbook.activeSheet.rows.length) {
98
+ return {
99
+ status: "CANCELLED",
100
+ reason: CancelledReason.SelectionOutOfBound,
101
+ };
102
+ }
103
+ break;
104
+ }
105
+ }
106
+ return {
107
+ status: "SUCCESS",
108
+ };
109
+ }
110
+ handle(cmd: Command) {
111
+ switch (cmd.type) {
112
+ case "START":
113
+ this.selectCell(0, 0);
114
+ break;
115
+ case "ACTIVATE_SHEET":
116
+ this.sheetsData[cmd.from] = {
117
+ selection: JSON.parse(JSON.stringify(this.selection)),
118
+ activeCol: this.activeCol,
119
+ activeRow: this.activeRow,
120
+ activeXc: this.activeXc,
121
+ };
122
+ if (cmd.to in this.sheetsData) {
123
+ Object.assign(this, this.sheetsData[cmd.to]);
124
+ } else {
125
+ this.selectCell(0, 0);
126
+ }
127
+ break;
128
+ case "SET_SELECTION":
129
+ this.setSelection(cmd.anchor, cmd.zones, cmd.strict);
130
+ break;
131
+ case "START_SELECTION":
132
+ this.mode = SelectionMode.selecting;
133
+ break;
134
+ case "PREPARE_SELECTION_EXPANSION":
135
+ this.mode = SelectionMode.readyToExpand;
136
+ break;
137
+ case "START_SELECTION_EXPANSION":
138
+ this.mode = SelectionMode.expanding;
139
+ break;
140
+ case "STOP_SELECTION":
141
+ this.mode = SelectionMode.idle;
142
+ break;
143
+ case "MOVE_POSITION":
144
+ this.movePosition(cmd.deltaX, cmd.deltaY);
145
+ break;
146
+ case "SELECT_CELL":
147
+ this.selectCell(cmd.col, cmd.row);
148
+ break;
149
+ case "SELECT_COLUMN":
150
+ this.selectColumn(cmd.index, cmd.createRange || false, cmd.updateRange || false);
151
+ break;
152
+ case "SELECT_ROW":
153
+ this.selectRow(cmd.index, cmd.createRange || false, cmd.updateRange || false);
154
+ break;
155
+ case "SELECT_ALL":
156
+ this.selectAll();
157
+ break;
158
+ case "ALTER_SELECTION":
159
+ if (cmd.delta) {
160
+ this.moveSelection(cmd.delta[0], cmd.delta[1]);
161
+ }
162
+ if (cmd.cell) {
163
+ this.addCellToSelection(...cmd.cell);
164
+ }
165
+ break;
166
+ case "UNDO":
167
+ case "REDO":
168
+ case "REMOVE_COLUMNS":
169
+ case "REMOVE_ROWS":
170
+ this.ensureSelectionValidity();
171
+ break;
172
+ case "ADD_COLUMNS":
173
+ if (cmd.position === "before") {
174
+ this.onAddColumns(cmd.quantity);
175
+ }
176
+ break;
177
+ case "ADD_ROWS":
178
+ if (cmd.position === "before") {
179
+ this.onAddRows(cmd.quantity);
180
+ }
181
+ break;
182
+ }
183
+ }
184
+
185
+ // ---------------------------------------------------------------------------
186
+ // Getters
187
+ // ---------------------------------------------------------------------------
188
+
189
+ getActiveCell(): Cell | null {
190
+ const sheet = this.workbook.activeSheet;
191
+ let mergeId = sheet.mergeCellMap[this.activeXc];
192
+ if (mergeId) {
193
+ return sheet.cells[sheet.merges[mergeId].topLeft];
194
+ } else {
195
+ return this.getters.getCell(this.activeCol, this.activeRow);
196
+ }
197
+ }
198
+
199
+ getActiveCols(): Set<number> {
200
+ const activeCols = new Set<number>();
201
+ for (let zone of this.selection.zones) {
202
+ if (zone.top === 0 && zone.bottom === this.workbook.activeSheet.rows.length - 1) {
203
+ for (let i = zone.left; i <= zone.right; i++) {
204
+ activeCols.add(i);
205
+ }
206
+ }
207
+ }
208
+ return activeCols;
209
+ }
210
+
211
+ getActiveRows(): Set<number> {
212
+ const activeRows = new Set<number>();
213
+ for (let zone of this.selection.zones) {
214
+ if (zone.left === 0 && zone.right === this.workbook.activeSheet.cols.length - 1) {
215
+ for (let i = zone.top; i <= zone.bottom; i++) {
216
+ activeRows.add(i);
217
+ }
218
+ }
219
+ }
220
+ return activeRows;
221
+ }
222
+
223
+ getSelectedZones(): Zone[] {
224
+ return this.selection.zones;
225
+ }
226
+
227
+ getSelectedZone(): Zone {
228
+ return this.selection.zones[this.selection.zones.length - 1];
229
+ }
230
+
231
+ getSelection(): Selection {
232
+ return this.selection;
233
+ }
234
+
235
+ getPosition(): [number, number] {
236
+ return [this.activeCol, this.activeRow];
237
+ }
238
+
239
+ getAggregate(): string | null {
240
+ let aggregate = 0;
241
+ let n = 0;
242
+ for (let zone of this.selection.zones) {
243
+ for (let row = zone.top; row <= zone.bottom; row++) {
244
+ const r = this.workbook.activeSheet.rows[row];
245
+ for (let col = zone.left; col <= zone.right; col++) {
246
+ const cell = r.cells[col];
247
+ if (cell && cell.type !== "text" && !cell.error && typeof cell.value === "number") {
248
+ n++;
249
+ aggregate += cell.value;
250
+ }
251
+ }
252
+ }
253
+ }
254
+ return n < 2 ? null : formatStandardNumber(aggregate);
255
+ }
256
+
257
+ getSelectionMode(): SelectionMode {
258
+ return this.mode;
259
+ }
260
+
261
+ isSelected(zone: Zone): boolean {
262
+ return !!this.getters.getSelectedZones().find((z) => isEqual(z, zone));
263
+ }
264
+
265
+ // ---------------------------------------------------------------------------
266
+ // Other
267
+ // ---------------------------------------------------------------------------
268
+
269
+ /**
270
+ * Return [col, row]
271
+ */
272
+ private getReferenceCoords(): [number, number] {
273
+ const isSelectingRange = this.getters.getEditionMode() === "selecting";
274
+ return isSelectingRange ? this.selection.anchor : [this.activeCol, this.activeRow];
275
+ }
276
+
277
+ private selectColumn(index: number, createRange: boolean, updateRange: boolean) {
278
+ const bottom = this.workbook.activeSheet.rows.length - 1;
279
+ const zone = { left: index, right: index, top: 0, bottom };
280
+ const current = this.selection.zones;
281
+ let zones: Zone[], anchor: [number, number];
282
+ if (updateRange) {
283
+ const [col, row] = this.selection.anchor;
284
+ const updatedZone = union(zone, { left: col, right: col, top: 0, bottom });
285
+ zones = current.slice(0, -1).concat(updatedZone);
286
+ anchor = [col, row];
287
+ } else {
288
+ zones = createRange ? current.concat(zone) : [zone];
289
+ anchor = [index, 0];
290
+ }
291
+ this.dispatch("SET_SELECTION", { zones, anchor, strict: true });
292
+ }
293
+
294
+ private selectRow(index: number, createRange: boolean, updateRange: boolean) {
295
+ const right = this.workbook.activeSheet.cols.length - 1;
296
+ const zone = { top: index, bottom: index, left: 0, right };
297
+ const current = this.selection.zones;
298
+ let zones: Zone[], anchor: [number, number];
299
+ if (updateRange) {
300
+ const [col, row] = this.selection.anchor;
301
+ const updatedZone = union(zone, { left: 0, right, top: row, bottom: row });
302
+ zones = current.slice(0, -1).concat(updatedZone);
303
+ anchor = [col, row];
304
+ } else {
305
+ zones = createRange ? current.concat(zone) : [zone];
306
+ anchor = [0, index];
307
+ }
308
+ this.dispatch("SET_SELECTION", { zones, anchor, strict: true });
309
+ }
310
+
311
+ private selectAll() {
312
+ const bottom = this.workbook.activeSheet.rows.length - 1;
313
+ const right = this.workbook.activeSheet.cols.length - 1;
314
+ const zone = { left: 0, top: 0, bottom, right };
315
+ this.dispatch("SET_SELECTION", { zones: [zone], anchor: [0, 0] });
316
+ }
317
+
318
+ /**
319
+ * Change the anchor of the selection active cell to an absolute col and row inded.
320
+ *
321
+ * This is a non trivial task. We need to stop the editing process and update
322
+ * properly the current selection. Also, this method can optionally create a new
323
+ * range in the selection.
324
+ */
325
+ private selectCell(col: number, row: number) {
326
+ const xc = toXC(col, row);
327
+ let zone = this.getters.expandZone({ left: col, right: col, top: row, bottom: row });
328
+
329
+ if (this.mode === SelectionMode.expanding) {
330
+ this.selection.zones.push(zone);
331
+ } else {
332
+ this.selection.zones = [zone];
333
+ }
334
+ this.selection.anchor = [col, row];
335
+ if (this.getters.getEditionMode() !== "selecting") {
336
+ this.activeCol = col;
337
+ this.activeRow = row;
338
+ this.activeXc = xc;
339
+ }
340
+ }
341
+
342
+ /**
343
+ * Moves the position of either the active cell of the anchor of the current selection by a number of rows / cols delta
344
+ */
345
+ movePosition(deltaX: number, deltaY: number) {
346
+ const [refCol, refRow] = this.getReferenceCoords();
347
+ const activeReference = toXC(refCol, refRow);
348
+
349
+ const sheet = this.workbook.activeSheet;
350
+ let mergeId = sheet.mergeCellMap[activeReference];
351
+ if (mergeId) {
352
+ let targetCol = refCol;
353
+ let targetRow = refRow;
354
+ while (sheet.mergeCellMap[toXC(targetCol, targetRow)] === mergeId) {
355
+ targetCol += deltaX;
356
+ targetRow += deltaY;
357
+ }
358
+ if (targetCol >= 0 && targetRow >= 0) {
359
+ this.selectCell(targetCol, targetRow);
360
+ }
361
+ } else {
362
+ this.selectCell(refCol + deltaX, refRow + deltaY);
363
+ }
364
+ }
365
+
366
+ setSelection(anchor: [number, number], zones: Zone[], strict: boolean = false) {
367
+ this.selectCell(...anchor);
368
+ if (strict) {
369
+ this.selection.zones = zones;
370
+ } else {
371
+ this.selection.zones = zones.map(this.getters.expandZone);
372
+ }
373
+ this.selection.anchor = anchor;
374
+ }
375
+
376
+ private moveSelection(deltaX: number, deltaY: number) {
377
+ const selection = this.selection;
378
+ const zones = selection.zones.slice();
379
+ const lastZone = zones[selection.zones.length - 1];
380
+ const [anchorCol, anchorRow] = selection.anchor;
381
+ const { left, right, top, bottom } = lastZone;
382
+ let result: Zone | null = lastZone;
383
+ const expand = (z: Zone) => {
384
+ const { left, right, top, bottom } = this.getters.expandZone(z);
385
+ return {
386
+ left: Math.max(0, left),
387
+ right: Math.min(this.workbook.activeSheet.cols.length - 1, right),
388
+ top: Math.max(0, top),
389
+ bottom: Math.min(this.workbook.activeSheet.rows.length - 1, bottom),
390
+ };
391
+ };
392
+
393
+ // check if we can shrink selection
394
+ let n = 0;
395
+ while (result !== null) {
396
+ n++;
397
+ if (deltaX < 0) {
398
+ result = anchorCol <= right - n ? expand({ top, left, bottom, right: right - n }) : null;
399
+ }
400
+ if (deltaX > 0) {
401
+ result = left + n <= anchorCol ? expand({ top, left: left + n, bottom, right }) : null;
402
+ }
403
+ if (deltaY < 0) {
404
+ result = anchorRow <= bottom - n ? expand({ top, left, bottom: bottom - n, right }) : null;
405
+ }
406
+ if (deltaY > 0) {
407
+ result = top + n <= anchorRow ? expand({ top: top + n, left, bottom, right }) : null;
408
+ }
409
+ if (result && !isEqual(result, lastZone)) {
410
+ zones[zones.length - 1] = result;
411
+ this.dispatch("SET_SELECTION", { zones, anchor: [anchorCol, anchorRow] });
412
+ return;
413
+ }
414
+ }
415
+ const currentZone = { top: anchorRow, bottom: anchorRow, left: anchorCol, right: anchorCol };
416
+ const zoneWithDelta = {
417
+ top: top + deltaY,
418
+ left: left + deltaX,
419
+ bottom: bottom + deltaY,
420
+ right: right + deltaX,
421
+ };
422
+ result = expand(union(currentZone, zoneWithDelta));
423
+ if (!isEqual(result, lastZone)) {
424
+ zones[zones.length - 1] = result;
425
+ this.dispatch("SET_SELECTION", { zones, anchor: [anchorCol, anchorRow] });
426
+ }
427
+ }
428
+
429
+ private addCellToSelection(col: number, row: number) {
430
+ const selection = this.selection;
431
+ const [anchorCol, anchorRow] = selection.anchor;
432
+ const zone: Zone = {
433
+ left: Math.min(anchorCol, col),
434
+ top: Math.min(anchorRow, row),
435
+ right: Math.max(anchorCol, col),
436
+ bottom: Math.max(anchorRow, row),
437
+ };
438
+ const zones = selection.zones.slice(0, -1).concat(zone);
439
+ this.dispatch("SET_SELECTION", { zones, anchor: [anchorCol, anchorRow] });
440
+ }
441
+
442
+ /**
443
+ * Ensure selections are not outside sheet boundaries.
444
+ * They are clipped to fit inside the sheet if needed.
445
+ */
446
+ private ensureSelectionValidity() {
447
+ const sheets = this.getters.getSheets();
448
+ const sheetIds = sheets.map((sheet) => sheet.id);
449
+ if (!sheetIds.includes(this.getters.getActiveSheet())) {
450
+ this.dispatch("ACTIVATE_SHEET", { from: this.getters.getActiveSheet(), to: sheetIds[0] });
451
+ } else {
452
+ const { anchor, zones } = this.clipSelection(this.getters.getActiveSheet(), this.selection);
453
+ this.setSelection(anchor, zones);
454
+ }
455
+ const deletedSheetIds = Object.keys(this.sheetsData).filter(
456
+ (sheetId) => !sheetIds.includes(sheetId)
457
+ );
458
+ for (const sheetId of deletedSheetIds) {
459
+ delete this.sheetsData[sheetId];
460
+ }
461
+ for (const sheetId in this.sheetsData) {
462
+ const { anchor, zones } = this.clipSelection(sheetId, this.sheetsData[sheetId].selection);
463
+ this.sheetsData[sheetId] = {
464
+ selection: { anchor, zones },
465
+ activeCol: anchor[0],
466
+ activeRow: anchor[1],
467
+ activeXc: toXC(...anchor),
468
+ };
469
+ }
470
+ }
471
+
472
+ /**
473
+ * Clip the selection if it spans outside the sheet
474
+ */
475
+ private clipSelection(sheetId: string, selection: Selection): Selection {
476
+ const cols = this.getters.getNumberCols(sheetId) - 1;
477
+ const rows = this.getters.getNumberRows(sheetId) - 1;
478
+ const zones = selection.zones.map((z) => ({
479
+ left: clip(z.left, 0, cols),
480
+ right: clip(z.right, 0, cols),
481
+ top: clip(z.top, 0, rows),
482
+ bottom: clip(z.bottom, 0, rows),
483
+ }));
484
+ const anchorCol = zones[zones.length - 1].left;
485
+ const anchorRow = zones[zones.length - 1].top;
486
+ return {
487
+ anchor: [anchorCol, anchorRow],
488
+ zones,
489
+ };
490
+ }
491
+
492
+ private onAddColumns(quantity: number) {
493
+ const selection = this.getSelectedZone();
494
+ const zone = {
495
+ left: selection.left + quantity,
496
+ right: selection.right + quantity,
497
+ top: selection.top,
498
+ bottom: selection.bottom,
499
+ };
500
+ this.dispatch("SET_SELECTION", { zones: [zone], anchor: [zone.left, zone.top], strict: true });
501
+ }
502
+
503
+ private onAddRows(quantity: number) {
504
+ const selection = this.getSelectedZone();
505
+ const zone = {
506
+ left: selection.left,
507
+ right: selection.right,
508
+ top: selection.top + quantity,
509
+ bottom: selection.bottom + quantity,
510
+ };
511
+ this.dispatch("SET_SELECTION", { zones: [zone], anchor: [zone.left, zone.top], strict: true });
512
+ }
513
+
514
+ // ---------------------------------------------------------------------------
515
+ // Grid rendering
516
+ // ---------------------------------------------------------------------------
517
+
518
+ drawGrid(renderingContext: GridRenderingContext) {
519
+ const { viewport, ctx, thinLineWidth } = renderingContext;
520
+
521
+ // selection
522
+ const zones = this.getSelectedZones();
523
+ ctx.fillStyle = "#f3f7fe";
524
+ const onlyOneCell =
525
+ zones.length === 1 && zones[0].left === zones[0].right && zones[0].top === zones[0].bottom;
526
+ ctx.fillStyle = onlyOneCell ? "#f3f7fe" : "#e9f0ff";
527
+ ctx.strokeStyle = SELECTION_BORDER_COLOR;
528
+ ctx.lineWidth = 1.5 * thinLineWidth;
529
+ ctx.globalCompositeOperation = "multiply";
530
+ for (const zone of zones) {
531
+ const [x, y, width, height] = this.getters.getRect(zone, viewport);
532
+ if (width > 0 && height > 0) {
533
+ ctx.fillRect(x, y, width, height);
534
+ ctx.strokeRect(x, y, width, height);
535
+ }
536
+ }
537
+ ctx.globalCompositeOperation = "source-over";
538
+
539
+ // active zone
540
+ const { mergeCellMap, merges } = this.workbook.activeSheet;
541
+ const [col, row] = this.getPosition();
542
+ const activeXc = toXC(col, row);
543
+
544
+ ctx.strokeStyle = "#3266ca";
545
+ ctx.lineWidth = 3 * thinLineWidth;
546
+ let zone: Zone;
547
+ if (activeXc in mergeCellMap) {
548
+ zone = merges[mergeCellMap[activeXc]];
549
+ } else {
550
+ zone = {
551
+ top: row,
552
+ bottom: row,
553
+ left: col,
554
+ right: col,
555
+ };
556
+ }
557
+ const [x, y, width, height] = this.getters.getRect(zone, viewport);
558
+ if (width > 0 && height > 0) {
559
+ ctx.strokeRect(x, y, width, height);
560
+ }
561
+ }
562
+ }