@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,461 @@
1
+ import { BasePlugin } from "../base_plugin";
2
+ import { clip, toXC } from "../helpers/index";
3
+ import { Mode } from "../model";
4
+ import { _lt } from "../translation";
5
+ import {
6
+ CancelledReason,
7
+ Cell,
8
+ Command,
9
+ CommandResult,
10
+ GridRenderingContext,
11
+ LAYERS,
12
+ Zone,
13
+ } from "../types/index";
14
+
15
+ interface ClipboardCell {
16
+ cell: Cell | null;
17
+ col: number;
18
+ row: number;
19
+ }
20
+
21
+ /**
22
+ * Clipboard Plugin
23
+ *
24
+ * This clipboard manages all cut/copy/paste interactions internal to the
25
+ * application, and with the OS clipboard as well.
26
+ */
27
+ export class ClipboardPlugin extends BasePlugin {
28
+ static layers = [LAYERS.Clipboard];
29
+ static getters = ["getClipboardContent", "isPaintingFormat", "getPasteZones"];
30
+ static modes: Mode[] = ["normal", "readonly"];
31
+
32
+ private status: "empty" | "visible" | "invisible" = "empty";
33
+ private shouldCut?: boolean;
34
+ private zones: Zone[] = [];
35
+ private cells?: ClipboardCell[][];
36
+ private originSheet: string = this.workbook.activeSheet.id;
37
+ private _isPaintingFormat: boolean = false;
38
+ private pasteOnlyValue: boolean = false;
39
+ private pasteOnlyFormat: boolean = false;
40
+
41
+ // ---------------------------------------------------------------------------
42
+ // Command Handling
43
+ // ---------------------------------------------------------------------------
44
+
45
+ allowDispatch(cmd: Command): CommandResult {
46
+ if (cmd.type === "PASTE") {
47
+ return this.isPasteAllowed(cmd.target);
48
+ }
49
+ return {
50
+ status: "SUCCESS",
51
+ };
52
+ }
53
+
54
+ handle(cmd: Command) {
55
+ switch (cmd.type) {
56
+ case "COPY":
57
+ this.cutOrCopy(cmd.target, false);
58
+ break;
59
+ case "CUT":
60
+ this.cutOrCopy(cmd.target, true);
61
+ break;
62
+ case "PASTE":
63
+ this.pasteOnlyValue = "onlyValue" in cmd && !!cmd.onlyValue;
64
+ const onlyFormat = "onlyFormat" in cmd ? !!cmd.onlyFormat : this._isPaintingFormat;
65
+ this._isPaintingFormat = false;
66
+ this.pasteOnlyFormat = !this.pasteOnlyValue && onlyFormat;
67
+ if (cmd.interactive) {
68
+ this.interactivePaste(cmd.target);
69
+ } else {
70
+ this.pasteFromModel(cmd.target);
71
+ }
72
+ break;
73
+ case "PASTE_CELL":
74
+ this.pasteCell(cmd.origin, cmd.col, cmd.row, cmd.onlyValue, cmd.onlyFormat);
75
+ break;
76
+ case "PASTE_FROM_OS_CLIPBOARD":
77
+ this.pasteFromClipboard(cmd.target, cmd.text);
78
+ break;
79
+ case "ACTIVATE_PAINT_FORMAT":
80
+ this._isPaintingFormat = true;
81
+ this.cutOrCopy(cmd.target, false);
82
+ break;
83
+ }
84
+ }
85
+
86
+ // ---------------------------------------------------------------------------
87
+ // Getters
88
+ // ---------------------------------------------------------------------------
89
+
90
+ /**
91
+ * Format the current clipboard to a string suitable for being pasted in other
92
+ * programs.
93
+ *
94
+ * - add a tab character between each concecutive cells
95
+ * - add a newline character between each line
96
+ *
97
+ * Note that it returns \t if the clipboard is empty. This is necessary for the
98
+ * clipboard copy event to add it as data, otherwise an empty string is not
99
+ * considered as a copy content.
100
+ */
101
+ getClipboardContent(): string {
102
+ if (!this.cells) {
103
+ return "\t";
104
+ }
105
+ return (
106
+ this.cells
107
+ .map((cells) => {
108
+ return cells.map((c) => (c.cell ? this.getters.getCellText(c.cell) : "")).join("\t");
109
+ })
110
+ .join("\n") || "\t"
111
+ );
112
+ }
113
+
114
+ getPasteZones(target: Zone[]): Zone[] {
115
+ if (!this.cells) {
116
+ return target;
117
+ }
118
+ const height = this.cells.length;
119
+ const width = this.cells[0].length;
120
+ const selection = target[target.length - 1];
121
+ const pasteZones: Zone[] = [];
122
+ let col = selection.left;
123
+ let row = selection.top;
124
+ const repX = Math.max(1, Math.floor((selection.right + 1 - selection.left) / width));
125
+ const repY = Math.max(1, Math.floor((selection.bottom + 1 - selection.top) / height));
126
+ for (let x = 1; x <= repX; x++) {
127
+ for (let y = 1; y <= repY; y++) {
128
+ pasteZones.push({
129
+ left: col,
130
+ top: row,
131
+ right: col - 1 + x * width,
132
+ bottom: row - 1 + y * height,
133
+ });
134
+ }
135
+ }
136
+ return pasteZones;
137
+ }
138
+
139
+ isPaintingFormat(): boolean {
140
+ return this._isPaintingFormat;
141
+ }
142
+
143
+ // ---------------------------------------------------------------------------
144
+ // Private methods
145
+ // ---------------------------------------------------------------------------
146
+
147
+ private cutOrCopy(zones: Zone[], cut: boolean) {
148
+ const tops = new Set(zones.map((z) => z.top));
149
+ const bottoms = new Set(zones.map((z) => z.bottom));
150
+ const areZonesCompatible = tops.size === 1 && bottoms.size === 1;
151
+ let clippedZones = areZonesCompatible ? zones : [zones[zones.length - 1]];
152
+ clippedZones = clippedZones.map((z) => Object.assign({}, z));
153
+
154
+ const cells: ClipboardCell[][] = [];
155
+ let { top, bottom } = clippedZones[0];
156
+ for (let r = top; r <= bottom; r++) {
157
+ const row: ClipboardCell[] = [];
158
+ cells.push(row);
159
+ for (let zone of clippedZones) {
160
+ let { left, right } = zone;
161
+ for (let c = left; c <= right; c++) {
162
+ const cell = this.getters.getCell(c, r);
163
+ row.push({
164
+ cell: cell ? Object.assign({}, cell) : null,
165
+ col: c,
166
+ row: r,
167
+ });
168
+ }
169
+ }
170
+ }
171
+ this.status = "visible";
172
+ this.shouldCut = cut;
173
+ this.zones = clippedZones;
174
+ this.cells = cells;
175
+ this.originSheet = this.workbook.activeSheet.id;
176
+ }
177
+
178
+ private pasteFromClipboard(target: Zone[], content: string) {
179
+ this.status = "invisible";
180
+ const values = content
181
+ .replace(/\r/g, "")
182
+ .split("\n")
183
+ .map((vals) => vals.split("\t"));
184
+ const { left: activeCol, top: activeRow } = target[0];
185
+ const width = Math.max.apply(
186
+ Math,
187
+ values.map((a) => a.length)
188
+ );
189
+ const height = values.length;
190
+ this.addMissingDimensions(width, height, activeCol, activeRow);
191
+ for (let i = 0; i < values.length; i++) {
192
+ for (let j = 0; j < values[i].length; j++) {
193
+ const xc = toXC(activeCol + j, activeRow + i);
194
+ this.dispatch("SET_VALUE", { xc, text: values[i][j] });
195
+ }
196
+ }
197
+ this.dispatch("SET_SELECTION", {
198
+ anchor: [activeCol, activeRow],
199
+ zones: [
200
+ {
201
+ left: activeCol,
202
+ top: activeRow,
203
+ right: activeCol + width - 1,
204
+ bottom: activeRow + height - 1,
205
+ },
206
+ ],
207
+ });
208
+ }
209
+
210
+ private isPasteAllowed(target: Zone[]): CommandResult {
211
+ const { zones, cells, status } = this;
212
+ // cannot paste if we have a clipped zone larger than a cell and multiple
213
+ // zones selected
214
+ if (!zones || !cells || status === "empty") {
215
+ return { status: "CANCELLED", reason: CancelledReason.EmptyClipboard };
216
+ } else if (target.length > 1 && (cells.length > 1 || cells[0].length > 1)) {
217
+ return { status: "CANCELLED", reason: CancelledReason.WrongPasteSelection };
218
+ }
219
+ return { status: "SUCCESS" };
220
+ }
221
+
222
+ private pasteFromModel(target: Zone[]) {
223
+ const { cells, shouldCut } = this;
224
+ if (!cells) {
225
+ return;
226
+ }
227
+ this.status = shouldCut ? "empty" : "invisible";
228
+ if (shouldCut) {
229
+ this.clearCutZone();
230
+ }
231
+ const height = cells.length;
232
+ const width = cells[0].length;
233
+ if (target.length > 1) {
234
+ for (let zone of target) {
235
+ for (let i = zone.left; i <= zone.right; i++) {
236
+ for (let j = zone.top; j <= zone.bottom; j++) {
237
+ this.pasteZone(width, height, i, j);
238
+ }
239
+ }
240
+ }
241
+ return;
242
+ }
243
+ const selection = target[target.length - 1];
244
+ let col = selection.left;
245
+ let row = selection.top;
246
+ const repX = Math.max(1, Math.floor((selection.right + 1 - selection.left) / width));
247
+ const repY = Math.max(1, Math.floor((selection.bottom + 1 - selection.top) / height));
248
+ for (let x = 0; x < repX; x++) {
249
+ for (let y = 0; y < repY; y++) {
250
+ this.pasteZone(width, height, col + x * width, row + y * height);
251
+ }
252
+ }
253
+
254
+ if (height > 1 || width > 1) {
255
+ const newSelection = {
256
+ left: col,
257
+ top: row,
258
+ right: col + repX * width - 1,
259
+ bottom: row + repY * height - 1,
260
+ };
261
+ const [anchorCol, anchorRow] = this.getters.getSelection().anchor;
262
+ const newCol = clip(anchorCol, col, col + repX * width - 1);
263
+ const newRow = clip(anchorRow, row, row + repY * height - 1);
264
+ this.dispatch("SET_SELECTION", {
265
+ anchor: [newCol, newRow],
266
+ zones: [newSelection],
267
+ });
268
+ }
269
+ }
270
+
271
+ private clearCutZone() {
272
+ for (let row of this.cells!) {
273
+ for (let cell of row) {
274
+ if (cell) {
275
+ this.dispatch("CLEAR_CELL", {
276
+ sheet: this.originSheet,
277
+ col: cell.col,
278
+ row: cell.row,
279
+ });
280
+ }
281
+ }
282
+ }
283
+ }
284
+
285
+ private pasteZone(width: number, height: number, col: number, row: number) {
286
+ // first, add missing cols/rows if needed
287
+ this.addMissingDimensions(width, height, col, row);
288
+ // then, perform the actual paste operation
289
+ for (let r = 0; r < height; r++) {
290
+ const rowCells = this.cells![r];
291
+ for (let c = 0; c < width; c++) {
292
+ const originCell = rowCells[c];
293
+ this.dispatch("PASTE_CELL", {
294
+ origin: originCell.cell,
295
+ originCol: originCell.col,
296
+ originRow: originCell.row,
297
+ col: col + c,
298
+ row: row + r,
299
+ sheet: this.originSheet,
300
+ cut: this.shouldCut,
301
+ onlyValue: this.pasteOnlyValue,
302
+ onlyFormat: this.pasteOnlyFormat,
303
+ });
304
+ }
305
+ }
306
+ }
307
+
308
+ private addMissingDimensions(width, height, col, row) {
309
+ const { cols, rows } = this.workbook.activeSheet;
310
+ const missingRows = height + row - rows.length;
311
+ if (missingRows > 0) {
312
+ this.dispatch("ADD_ROWS", {
313
+ row: rows.length - 1,
314
+ sheet: this.workbook.activeSheet.id,
315
+ quantity: missingRows,
316
+ position: "after",
317
+ });
318
+ }
319
+ const missingCols = width + col - cols.length;
320
+ if (missingCols > 0) {
321
+ this.dispatch("ADD_COLUMNS", {
322
+ column: cols.length - 1,
323
+ sheet: this.workbook.activeSheet.id,
324
+ quantity: missingCols,
325
+ position: "after",
326
+ });
327
+ }
328
+ }
329
+
330
+ pasteCell(
331
+ origin: Cell | null,
332
+ col: number,
333
+ row: number,
334
+ onlyValue: boolean,
335
+ onlyFormat: boolean
336
+ ) {
337
+ const targetCell = this.getters.getCell(col, row);
338
+ if (origin) {
339
+ let style = origin.style;
340
+ let border = origin.border;
341
+ let format = origin.format;
342
+ let content: string | undefined = origin.content || "";
343
+
344
+ if (onlyValue) {
345
+ style = targetCell ? targetCell.style : undefined;
346
+ border = targetCell ? targetCell.border : undefined;
347
+ format = targetCell ? targetCell.format : undefined;
348
+
349
+ if (targetCell) {
350
+ if (targetCell.type === "date") {
351
+ format = targetCell.value.format;
352
+ }
353
+ }
354
+ if (origin.type === "formula" || origin.type === "date") {
355
+ content = this.valueToContent(origin.value);
356
+ }
357
+ } else if (onlyFormat) {
358
+ content = targetCell ? targetCell.content : "";
359
+ } else if (origin.type === "formula") {
360
+ const offsetX = col - origin.col;
361
+ const offsetY = row - origin.row;
362
+ content = this.getters.applyOffset(content, offsetX, offsetY);
363
+ }
364
+
365
+ let newCell = {
366
+ style: style,
367
+ border: border,
368
+ format: format,
369
+ sheet: this.workbook.activeSheet.id,
370
+ col: col,
371
+ row: row,
372
+ content: content,
373
+ };
374
+
375
+ this.dispatch("UPDATE_CELL", newCell);
376
+ }
377
+ if (!origin && targetCell) {
378
+ if (onlyValue) {
379
+ if (targetCell.content) {
380
+ //this.history.updateCell(targetCell, "content", undefined);
381
+ this.history.updateCell(targetCell, "content", "");
382
+ this.history.updateCell(targetCell, "value", "");
383
+ }
384
+ } else if (onlyFormat) {
385
+ if (targetCell.style) {
386
+ this.history.updateCell(targetCell, "style", undefined);
387
+ }
388
+ if (targetCell.border) {
389
+ this.history.updateCell(targetCell, "border", undefined);
390
+ }
391
+ if (targetCell.format) {
392
+ this.history.updateCell(targetCell, "format", undefined);
393
+ }
394
+ } else {
395
+ this.dispatch("CLEAR_CELL", {
396
+ sheet: this.workbook.activeSheet.id,
397
+ col: col,
398
+ row: row,
399
+ });
400
+ }
401
+ }
402
+ }
403
+
404
+ private valueToContent(cellValue: any): string {
405
+ switch (typeof cellValue) {
406
+ case "number":
407
+ return cellValue.toString();
408
+ case "string":
409
+ return cellValue;
410
+ case "boolean":
411
+ return cellValue ? "TRUE" : "FALSE";
412
+ case "object":
413
+ return cellValue.value.toString();
414
+ default:
415
+ return "";
416
+ }
417
+ }
418
+
419
+ interactivePaste(target: Zone[]) {
420
+ const result = this.dispatch("PASTE", { target, onlyFormat: false });
421
+
422
+ if (result.status === "CANCELLED") {
423
+ if (result.reason === CancelledReason.WrongPasteSelection) {
424
+ this.ui.notifyUser(_lt("This operation is not allowed with multiple selections."));
425
+ }
426
+ if (result.reason === CancelledReason.WillRemoveExistingMerge) {
427
+ this.ui.askConfirmation(
428
+ _lt("Pasting here will remove existing merge(s). Paste anyway?"),
429
+ () => this.dispatch("PASTE", { target, onlyFormat: false, force: true })
430
+ );
431
+ }
432
+ }
433
+ }
434
+
435
+ // ---------------------------------------------------------------------------
436
+ // Grid rendering
437
+ // ---------------------------------------------------------------------------
438
+
439
+ drawGrid(renderingContext: GridRenderingContext) {
440
+ const { viewport, ctx, thinLineWidth } = renderingContext;
441
+ const zones = this.zones;
442
+ if (
443
+ this.status !== "visible" ||
444
+ !zones.length ||
445
+ this.originSheet !== this.getters.getActiveSheet()
446
+ ) {
447
+ return;
448
+ }
449
+ ctx.save();
450
+ ctx.setLineDash([8, 5]);
451
+ ctx.strokeStyle = "#3266ca";
452
+ ctx.lineWidth = 3.3 * thinLineWidth;
453
+ for (const zone of zones) {
454
+ const [x, y, width, height] = this.getters.getRect(zone, viewport);
455
+ if (width > 0 && height > 0) {
456
+ ctx.strokeRect(x, y, width, height);
457
+ }
458
+ }
459
+ ctx.restore();
460
+ }
461
+ }