@odoo/o-spreadsheet 14.0.1 → 15.0.0

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 (203) hide show
  1. package/dist/o_spreadsheet.cjs.js +31302 -0
  2. package/dist/o_spreadsheet.d.ts +6604 -0
  3. package/dist/o_spreadsheet.esm.js +31257 -0
  4. package/dist/o_spreadsheet.iife.js +31304 -0
  5. package/dist/o_spreadsheet.iife.min.js +3071 -0
  6. package/package.json +57 -15
  7. package/.eslintrc.json +0 -20
  8. package/.husky/post-checkout +0 -9
  9. package/.husky/pre-commit +0 -39
  10. package/.prettierignore +0 -1
  11. package/astToFormula.patch +0 -0
  12. package/demo/data.js +0 -255
  13. package/demo/index.html +0 -9
  14. package/demo/libs/Chart.bundle.js +0 -20755
  15. package/demo/main.js +0 -101
  16. package/doc/readme.md +0 -46
  17. package/rollup.config.js +0 -23
  18. package/src/base_plugin.ts +0 -102
  19. package/src/components/autofill.ts +0 -146
  20. package/src/components/bottom_bar.ts +0 -199
  21. package/src/components/color_picker.ts +0 -150
  22. package/src/components/composer/autocomplete_dropdown.ts +0 -135
  23. package/src/components/composer/composer.ts +0 -487
  24. package/src/components/composer/content_editable_helper.ts +0 -146
  25. package/src/components/figures/chart.ts +0 -70
  26. package/src/components/figures/container.ts +0 -268
  27. package/src/components/figures/text_figure.ts +0 -36
  28. package/src/components/grid.ts +0 -635
  29. package/src/components/helpers/dom_helpers.ts +0 -7
  30. package/src/components/helpers/drag_and_drop.ts +0 -17
  31. package/src/components/icons.ts +0 -36
  32. package/src/components/index.ts +0 -1
  33. package/src/components/menu.ts +0 -288
  34. package/src/components/overlay.ts +0 -457
  35. package/src/components/scrollbar.ts +0 -22
  36. package/src/components/selection_input.ts +0 -168
  37. package/src/components/side_panel/cell_is_rule_editor.ts +0 -169
  38. package/src/components/side_panel/chart_panel.ts +0 -104
  39. package/src/components/side_panel/color_scale_rule_editor.ts +0 -145
  40. package/src/components/side_panel/conditional_formatting.ts +0 -396
  41. package/src/components/side_panel/side_panel.ts +0 -127
  42. package/src/components/side_panel/translations_terms.ts +0 -45
  43. package/src/components/spreadsheet.ts +0 -198
  44. package/src/components/top_bar.ts +0 -535
  45. package/src/constants.ts +0 -26
  46. package/src/data.ts +0 -149
  47. package/src/fonts.ts +0 -41
  48. package/src/formulas/compiler.ts +0 -258
  49. package/src/formulas/composer_tokenizer.ts +0 -70
  50. package/src/formulas/index.ts +0 -15
  51. package/src/formulas/normalize.ts +0 -52
  52. package/src/formulas/parser.ts +0 -280
  53. package/src/formulas/range_tokenizer.ts +0 -97
  54. package/src/formulas/tokenizer.ts +0 -242
  55. package/src/functions/arguments.ts +0 -114
  56. package/src/functions/dates.ts +0 -302
  57. package/src/functions/helpers.ts +0 -592
  58. package/src/functions/index.ts +0 -52
  59. package/src/functions/module_database.ts +0 -282
  60. package/src/functions/module_date.ts +0 -743
  61. package/src/functions/module_info.ts +0 -69
  62. package/src/functions/module_logical.ts +0 -208
  63. package/src/functions/module_lookup.ts +0 -185
  64. package/src/functions/module_math.ts +0 -1252
  65. package/src/functions/module_operators.ts +0 -291
  66. package/src/functions/module_statistical.ts +0 -882
  67. package/src/functions/module_text.ts +0 -370
  68. package/src/helpers/color.ts +0 -30
  69. package/src/helpers/coordinates.ts +0 -71
  70. package/src/helpers/grid_manipulation.ts +0 -68
  71. package/src/helpers/index.ts +0 -7
  72. package/src/helpers/misc.ts +0 -76
  73. package/src/helpers/numbers.ts +0 -117
  74. package/src/helpers/uuid.ts +0 -19
  75. package/src/helpers/zones.ts +0 -263
  76. package/src/history.ts +0 -186
  77. package/src/index.ts +0 -84
  78. package/src/model.ts +0 -261
  79. package/src/plugins/autofill.ts +0 -412
  80. package/src/plugins/chart.ts +0 -404
  81. package/src/plugins/clipboard.ts +0 -461
  82. package/src/plugins/conditional_format.ts +0 -445
  83. package/src/plugins/core.ts +0 -1502
  84. package/src/plugins/edition.ts +0 -157
  85. package/src/plugins/evaluation.ts +0 -428
  86. package/src/plugins/figures.ts +0 -123
  87. package/src/plugins/formatting.ts +0 -630
  88. package/src/plugins/highlight.ts +0 -187
  89. package/src/plugins/index.ts +0 -32
  90. package/src/plugins/merge.ts +0 -524
  91. package/src/plugins/renderer.ts +0 -571
  92. package/src/plugins/selection.ts +0 -562
  93. package/src/plugins/selection_inputs.ts +0 -326
  94. package/src/registries/autofill_modifiers.ts +0 -76
  95. package/src/registries/autofill_rules.ts +0 -98
  96. package/src/registries/figure_registry.ts +0 -30
  97. package/src/registries/index.ts +0 -7
  98. package/src/registries/menu_items_registry.ts +0 -110
  99. package/src/registries/menus/cell_menu_registry.ts +0 -79
  100. package/src/registries/menus/col_menu_registry.ts +0 -63
  101. package/src/registries/menus/index.ts +0 -6
  102. package/src/registries/menus/menu_items_actions.ts +0 -425
  103. package/src/registries/menus/row_menu_registry.ts +0 -63
  104. package/src/registries/menus/sheet_menu_registry.ts +0 -75
  105. package/src/registries/menus/topbar_menu_registry.ts +0 -204
  106. package/src/registries/side_panel_registry.ts +0 -26
  107. package/src/registries/topbar_component_registry.ts +0 -12
  108. package/src/registry.ts +0 -51
  109. package/src/translation.ts +0 -44
  110. package/src/types/autofill.ts +0 -48
  111. package/src/types/chart.ts +0 -26
  112. package/src/types/commands.ts +0 -716
  113. package/src/types/conditional_formatting.ts +0 -124
  114. package/src/types/env.ts +0 -13
  115. package/src/types/functions.ts +0 -35
  116. package/src/types/getters.ts +0 -103
  117. package/src/types/index.ts +0 -20
  118. package/src/types/misc.ts +0 -143
  119. package/src/types/rendering.ts +0 -41
  120. package/src/types/workbook_data.ts +0 -62
  121. package/tests/__mocks__/uuid.ts +0 -7
  122. package/tests/canvas.mock.ts +0 -34
  123. package/tests/components/__mocks__/content_editable_helper.ts +0 -79
  124. package/tests/components/__mocks__/scrollbar.ts +0 -18
  125. package/tests/components/__snapshots__/autocomplete_dropdown_test.ts.snap +0 -29
  126. package/tests/components/__snapshots__/bottom_bar_test.ts.snap +0 -75
  127. package/tests/components/__snapshots__/context_menu_test.ts.snap +0 -88
  128. package/tests/components/__snapshots__/grid_test.ts.snap +0 -60
  129. package/tests/components/__snapshots__/spreadsheet_test.ts.snap +0 -455
  130. package/tests/components/__snapshots__/top_bar_test.ts.snap +0 -705
  131. package/tests/components/autocomplete_dropdown_test.ts +0 -315
  132. package/tests/components/autofill_test.ts +0 -167
  133. package/tests/components/bottom_bar_test.ts +0 -262
  134. package/tests/components/chart_side_panel_test.ts +0 -123
  135. package/tests/components/composer_test.ts +0 -400
  136. package/tests/components/context_menu_test.ts +0 -825
  137. package/tests/components/grid_manipulation_test.ts +0 -163
  138. package/tests/components/grid_test.ts +0 -581
  139. package/tests/components/overlay_test.ts +0 -541
  140. package/tests/components/selection_input_test.ts +0 -259
  141. package/tests/components/side_panel_test.ts +0 -148
  142. package/tests/components/spreadsheet_test.ts +0 -194
  143. package/tests/components/top_bar_test.ts +0 -427
  144. package/tests/data_test.ts +0 -175
  145. package/tests/dom_helper.ts +0 -42
  146. package/tests/formulas/__snapshots__/compiler_test.ts.snap +0 -302
  147. package/tests/formulas/compiler_test.ts +0 -266
  148. package/tests/formulas/composer_tokenizer_test.ts +0 -137
  149. package/tests/formulas/expressions_test.ts +0 -57
  150. package/tests/formulas/formulas_test.ts +0 -80
  151. package/tests/formulas/normalize_test.ts +0 -82
  152. package/tests/formulas/parser_test.ts +0 -224
  153. package/tests/formulas/range_tokenizer_test.ts +0 -187
  154. package/tests/formulas/tokenizer_test.ts +0 -164
  155. package/tests/functions/arguments_test.ts +0 -141
  156. package/tests/functions/dates_test.ts +0 -1081
  157. package/tests/functions/functions_test.ts +0 -53
  158. package/tests/functions/helper_test.ts +0 -98
  159. package/tests/functions/module_database_test.ts +0 -311
  160. package/tests/functions/module_date_test.ts +0 -941
  161. package/tests/functions/module_info_test.ts +0 -139
  162. package/tests/functions/module_logical_test.ts +0 -369
  163. package/tests/functions/module_lookup_test.ts +0 -541
  164. package/tests/functions/module_math_test.ts +0 -2482
  165. package/tests/functions/module_operators_test.ts +0 -955
  166. package/tests/functions/module_statistical_test.ts +0 -2014
  167. package/tests/functions/module_text_test.ts +0 -667
  168. package/tests/helpers/coordinates_test.ts +0 -25
  169. package/tests/helpers/misc_test.ts +0 -95
  170. package/tests/helpers/numbers_test.ts +0 -196
  171. package/tests/helpers/zones_test.ts +0 -97
  172. package/tests/helpers.ts +0 -384
  173. package/tests/menu_items_registry_test.ts +0 -540
  174. package/tests/model/plugins/__snapshots__/chart_test.ts.snap +0 -90
  175. package/tests/model/plugins/chart_test.ts +0 -562
  176. package/tests/model_test.ts +0 -82
  177. package/tests/plugins/__snapshots__/conditional_formatting_test.ts.snap +0 -131
  178. package/tests/plugins/__snapshots__/renderer_test.ts.snap +0 -371
  179. package/tests/plugins/autofill_test.ts +0 -457
  180. package/tests/plugins/borders_test.ts +0 -295
  181. package/tests/plugins/clipboard_test.ts +0 -1180
  182. package/tests/plugins/conditional_formatting_test.ts +0 -1253
  183. package/tests/plugins/core_test.ts +0 -614
  184. package/tests/plugins/edition_test.ts +0 -100
  185. package/tests/plugins/evaluation_async_test.ts +0 -332
  186. package/tests/plugins/evaluation_dates_test.ts +0 -79
  187. package/tests/plugins/evaluation_test.ts +0 -901
  188. package/tests/plugins/figures_test.ts +0 -327
  189. package/tests/plugins/formatting_test.ts +0 -391
  190. package/tests/plugins/grid_manipulation_test.ts +0 -1366
  191. package/tests/plugins/highlight_test.ts +0 -413
  192. package/tests/plugins/history_test.ts +0 -142
  193. package/tests/plugins/import_export_test.ts +0 -276
  194. package/tests/plugins/merges_test.ts +0 -490
  195. package/tests/plugins/navigation_test.ts +0 -181
  196. package/tests/plugins/plugins/evaluation_test.ts +0 -111
  197. package/tests/plugins/renderer_test.ts +0 -598
  198. package/tests/plugins/resizing_test.ts +0 -152
  199. package/tests/plugins/selection_input_test.ts +0 -409
  200. package/tests/plugins/selection_test.ts +0 -442
  201. package/tests/plugins/sheets_test.ts +0 -643
  202. package/tests/plugins/style_test.ts +0 -90
  203. package/tsconfig.json +0 -22
@@ -1,562 +0,0 @@
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
- }