@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,571 +0,0 @@
1
- import { BasePlugin } from "../base_plugin";
2
- import {
3
- BACKGROUND_HEADER_ACTIVE_COLOR,
4
- BACKGROUND_HEADER_COLOR,
5
- BACKGROUND_HEADER_SELECTED_COLOR,
6
- CELL_BORDER_COLOR,
7
- DEFAULT_FONT,
8
- DEFAULT_FONT_SIZE,
9
- DEFAULT_FONT_WEIGHT,
10
- HEADER_BORDER_COLOR,
11
- HEADER_FONT_SIZE,
12
- HEADER_HEIGHT,
13
- HEADER_WIDTH,
14
- TEXT_HEADER_COLOR,
15
- } from "../constants";
16
- import { fontSizeMap } from "../fonts";
17
- import { overlap, toXC } from "../helpers/index";
18
- import { Mode } from "../model";
19
- import {
20
- Box,
21
- Cell,
22
- GridRenderingContext,
23
- Header,
24
- LAYERS,
25
- Rect,
26
- Viewport,
27
- Zone,
28
- } from "../types/index";
29
-
30
- // -----------------------------------------------------------------------------
31
- // Constants, types, helpers, ...
32
- // -----------------------------------------------------------------------------
33
-
34
- function computeAlign(cell: Cell, isShowingFormulas: boolean): "right" | "center" | "left" {
35
- if (cell.type === "formula" && isShowingFormulas) {
36
- return "left";
37
- } else if (cell.error || cell.pending) {
38
- return "center";
39
- }
40
- switch (typeof cell.value) {
41
- case "object":
42
- case "number":
43
- return "right";
44
- case "boolean":
45
- return "center";
46
- default:
47
- return "left";
48
- }
49
- }
50
-
51
- function searchIndex(headers: Header[], offset: number): number {
52
- let left = 0;
53
- let right = headers.length - 1;
54
- while (left <= right) {
55
- const index = Math.floor((left + right) / 2);
56
- const header = headers[index];
57
- if (offset < header.start) {
58
- right = index - 1;
59
- } else if (offset > header.end) {
60
- left = index + 1;
61
- } else {
62
- return index;
63
- }
64
- }
65
- return -1;
66
- }
67
-
68
- export class RendererPlugin extends BasePlugin {
69
- static layers = [LAYERS.Background, LAYERS.Headers];
70
- static getters = [
71
- "getColIndex",
72
- "getRowIndex",
73
- "getRect",
74
- "snapViewportToCell",
75
- "adjustViewportPosition",
76
- "adjustViewportZone",
77
- ];
78
- static modes: Mode[] = ["normal", "readonly"];
79
-
80
- private boxes: Box[] = [];
81
-
82
- // ---------------------------------------------------------------------------
83
- // Getters
84
- // ---------------------------------------------------------------------------
85
-
86
- /**
87
- * Return the index of a column given an offset x and a visible left col index.
88
- * It returns -1 if no column is found.
89
- */
90
- getColIndex(x: number, left: number): number {
91
- if (x < HEADER_WIDTH) {
92
- return -1;
93
- }
94
- const cols = this.workbook.activeSheet.cols;
95
- const adjustedX = x - HEADER_WIDTH + cols[left].start + 1;
96
- return searchIndex(cols, adjustedX);
97
- }
98
-
99
- getRowIndex(y: number, top: number): number {
100
- if (y < HEADER_HEIGHT) {
101
- return -1;
102
- }
103
- const rows = this.workbook.activeSheet.rows;
104
- const adjustedY = y - HEADER_HEIGHT + rows[top].start + 1;
105
- return searchIndex(rows, adjustedY);
106
- }
107
-
108
- getRect(zone: Zone, viewport: Viewport): Rect {
109
- const { left, top, right, bottom } = zone;
110
- let { offsetY, offsetX } = viewport;
111
- offsetX -= HEADER_WIDTH;
112
- offsetY -= HEADER_HEIGHT;
113
- const { cols, rows } = this.workbook.activeSheet;
114
- const x = Math.max(cols[left].start - offsetX, HEADER_WIDTH);
115
- const width = cols[right].end - offsetX - x;
116
- const y = Math.max(rows[top].start - offsetY, HEADER_HEIGHT);
117
- const height = rows[bottom].end - offsetY - y;
118
- return [x, y, width, height];
119
- }
120
-
121
- /**
122
- * Snap a viewport boundaries to exactly match the start of a cell.
123
- * @param viewport
124
- */
125
- snapViewportToCell(viewport: Viewport): Viewport {
126
- const { cols, rows } = this.workbook.activeSheet;
127
- const adjustedViewport = Object.assign({}, viewport);
128
- adjustedViewport.offsetX = cols[viewport.left].start;
129
- adjustedViewport.offsetY = rows[viewport.top].start;
130
- return adjustedViewport;
131
- }
132
-
133
- /**
134
- * Adjust the viewport until the active cell is completely visible inside it.
135
- * @param viewport the viewport that will be adjusted
136
- */
137
- adjustViewportPosition(viewport: Viewport): Viewport {
138
- const adjustedViewport = Object.assign({}, viewport);
139
- const { cols, rows } = this.workbook.activeSheet;
140
- const [col, row] = this.getters.getPosition();
141
- while (col >= adjustedViewport.right && col !== cols.length - 1) {
142
- adjustedViewport.offsetX = cols[adjustedViewport.left].end;
143
- this.adjustViewportZoneX(adjustedViewport);
144
- }
145
- while (col < adjustedViewport.left) {
146
- adjustedViewport.offsetX = cols[adjustedViewport.left - 1].start;
147
- this.adjustViewportZoneX(adjustedViewport);
148
- }
149
- while (row >= adjustedViewport.bottom && row !== rows.length - 1) {
150
- adjustedViewport.offsetY = rows[adjustedViewport.top].end;
151
- this.adjustViewportZoneY(adjustedViewport);
152
- }
153
- while (row < adjustedViewport.top) {
154
- adjustedViewport.offsetY = rows[adjustedViewport.top - 1].start;
155
- this.adjustViewportZoneY(adjustedViewport);
156
- }
157
- return adjustedViewport;
158
- }
159
-
160
- adjustViewportZone(viewport: Viewport): Viewport {
161
- const adjustedViewport = Object.assign({}, viewport);
162
- this.adjustViewportZoneX(adjustedViewport);
163
- this.adjustViewportZoneY(adjustedViewport);
164
- return adjustedViewport;
165
- }
166
-
167
- private adjustViewportZoneX(viewport: Viewport) {
168
- const { cols } = this.workbook.activeSheet;
169
- const { width, offsetX } = viewport;
170
- viewport.left = this.getColIndex(offsetX + HEADER_WIDTH, 0);
171
- const x = width + offsetX - HEADER_WIDTH;
172
- viewport.right = cols.length - 1;
173
- for (let i = viewport.left; i < cols.length; i++) {
174
- if (x < cols[i].end) {
175
- viewport.right = i;
176
- break;
177
- }
178
- }
179
- }
180
-
181
- private adjustViewportZoneY(viewport: Viewport) {
182
- const { rows } = this.workbook.activeSheet;
183
- const { height, offsetY } = viewport;
184
- viewport.top = this.getRowIndex(offsetY + HEADER_HEIGHT, 0);
185
-
186
- let y = height + offsetY - HEADER_HEIGHT;
187
- viewport.bottom = rows.length - 1;
188
- for (let i = viewport.top; i < rows.length; i++) {
189
- if (y < rows[i].end) {
190
- viewport.bottom = i;
191
- break;
192
- }
193
- }
194
- }
195
-
196
- // ---------------------------------------------------------------------------
197
- // Grid rendering
198
- // ---------------------------------------------------------------------------
199
-
200
- drawGrid(renderingContext: GridRenderingContext, layer: LAYERS) {
201
- switch (layer) {
202
- case LAYERS.Background:
203
- this.boxes = this.getGridBoxes(renderingContext);
204
- this.drawBackground(renderingContext);
205
- this.drawCellBackground(renderingContext);
206
- this.drawBorders(renderingContext);
207
- this.drawTexts(renderingContext);
208
- break;
209
- case LAYERS.Headers:
210
- this.drawHeaders(renderingContext);
211
- break;
212
- }
213
- }
214
-
215
- private drawBackground(renderingContext: GridRenderingContext) {
216
- const { ctx, viewport, thinLineWidth } = renderingContext;
217
- let { width, height, offsetX, offsetY, top, left, bottom, right } = viewport;
218
- const { rows, cols } = this.workbook.activeSheet;
219
-
220
- // white background
221
- ctx.fillStyle = "white";
222
- ctx.fillRect(0, 0, viewport.width, viewport.height);
223
-
224
- // background grid
225
- offsetX -= HEADER_WIDTH;
226
- offsetY -= HEADER_HEIGHT;
227
-
228
- ctx.lineWidth = 2 * thinLineWidth;
229
- ctx.strokeStyle = CELL_BORDER_COLOR;
230
- ctx.beginPath();
231
-
232
- // vertical lines
233
- const lineHeight = Math.min(height, rows[bottom].end - offsetY);
234
- for (let i = left; i <= right; i++) {
235
- const x = cols[i].end - offsetX;
236
- ctx.moveTo(x, 0);
237
- ctx.lineTo(x, lineHeight);
238
- }
239
-
240
- // horizontal lines
241
- const lineWidth = Math.min(width, cols[right].end - offsetX);
242
- for (let i = top; i <= bottom; i++) {
243
- const y = rows[i].end - offsetY;
244
- ctx.moveTo(0, y);
245
- ctx.lineTo(lineWidth, y);
246
- }
247
- ctx.stroke();
248
- }
249
-
250
- private drawCellBackground(renderingContext: GridRenderingContext) {
251
- const { ctx, thinLineWidth } = renderingContext;
252
- ctx.lineWidth = 0.3 * thinLineWidth;
253
- const inset = 0.1 * thinLineWidth;
254
- ctx.strokeStyle = "#111";
255
- for (let box of this.boxes) {
256
- // fill color
257
- let style = box.style;
258
- if (style && style.fillColor && style.fillColor !== "#ffffff") {
259
- ctx.fillStyle = style.fillColor;
260
- ctx.fillRect(box.x, box.y, box.width, box.height);
261
- ctx.strokeRect(box.x + inset, box.y + inset, box.width - 2 * inset, box.height - 2 * inset);
262
- }
263
- if (box.error) {
264
- ctx.fillStyle = "red";
265
- ctx.beginPath();
266
- ctx.moveTo(box.x + box.width - 5, box.y);
267
- ctx.lineTo(box.x + box.width, box.y);
268
- ctx.lineTo(box.x + box.width, box.y + 5);
269
- ctx.fill();
270
- }
271
- }
272
- }
273
-
274
- private drawBorders(renderingContext: GridRenderingContext) {
275
- const { ctx, thinLineWidth } = renderingContext;
276
- for (let box of this.boxes) {
277
- // fill color
278
- let border = box.border;
279
- if (border) {
280
- const { x, y, width, height } = box;
281
- if (border.left) {
282
- drawBorder(border.left, x, y, x, y + height);
283
- }
284
- if (border.top) {
285
- drawBorder(border.top, x, y, x + width, y);
286
- }
287
- if (border.right) {
288
- drawBorder(border.right, x + width, y, x + width, y + height);
289
- }
290
- if (border.bottom) {
291
- drawBorder(border.bottom, x, y + height, x + width, y + height);
292
- }
293
- }
294
- }
295
- function drawBorder([style, color], x1, y1, x2, y2) {
296
- ctx.strokeStyle = color;
297
- ctx.lineWidth = (style === "thin" ? 2 : 3) * thinLineWidth;
298
- ctx.beginPath();
299
- ctx.moveTo(x1, y1);
300
- ctx.lineTo(x2, y2);
301
- ctx.stroke();
302
- }
303
- }
304
-
305
- private drawTexts(renderingContext: GridRenderingContext) {
306
- const { ctx, thinLineWidth } = renderingContext;
307
- ctx.textBaseline = "middle";
308
- let currentFont;
309
- for (let box of this.boxes) {
310
- if (box.text) {
311
- const style = box.style || {};
312
- const align = box.align!;
313
- const italic = style.italic ? "italic " : "";
314
- const weight = style.bold ? "bold" : DEFAULT_FONT_WEIGHT;
315
- const sizeInPt = style.fontSize || DEFAULT_FONT_SIZE;
316
- const size = fontSizeMap[sizeInPt];
317
- const font = `${italic}${weight} ${size}px ${DEFAULT_FONT}`;
318
- if (font !== currentFont) {
319
- currentFont = font;
320
- ctx.font = font;
321
- }
322
- ctx.fillStyle = style.textColor || "#000";
323
- let x: number;
324
- let y = box.y + box.height / 2 + 1;
325
- if (align === "left") {
326
- x = box.x + 3;
327
- } else if (align === "right") {
328
- x = box.x + box.width - 3;
329
- } else {
330
- x = box.x + box.width / 2;
331
- }
332
- ctx.textAlign = align;
333
- if (box.clipRect) {
334
- ctx.save();
335
- ctx.beginPath();
336
- ctx.rect(...box.clipRect);
337
- ctx.clip();
338
- }
339
- ctx.fillText(box.text, Math.round(x), Math.round(y));
340
- if (style.strikethrough) {
341
- if (align === "right") {
342
- x = x - box.textWidth;
343
- } else if (align === "center") {
344
- x = x - box.textWidth / 2;
345
- }
346
- ctx.fillRect(x, y, box.textWidth, 2.6 * thinLineWidth);
347
- }
348
- if (box.clipRect) {
349
- ctx.restore();
350
- }
351
- }
352
- }
353
- }
354
-
355
- private drawHeaders(renderingContext: GridRenderingContext) {
356
- const { ctx, viewport, thinLineWidth } = renderingContext;
357
- let { width, height, offsetX, offsetY, left, top, right, bottom } = viewport;
358
- offsetX -= HEADER_WIDTH;
359
- offsetY -= HEADER_HEIGHT;
360
- const selection = this.getters.getSelectedZones();
361
- const { cols, rows } = this.workbook.activeSheet;
362
- const activeCols = this.getters.getActiveCols();
363
- const activeRows = this.getters.getActiveRows();
364
-
365
- ctx.fillStyle = BACKGROUND_HEADER_COLOR;
366
- ctx.font = `400 ${HEADER_FONT_SIZE}px ${DEFAULT_FONT}`;
367
- ctx.textAlign = "center";
368
- ctx.textBaseline = "middle";
369
- ctx.lineWidth = thinLineWidth;
370
- ctx.strokeStyle = "#333";
371
-
372
- // background
373
- ctx.fillRect(0, 0, width, HEADER_HEIGHT);
374
- ctx.fillRect(0, 0, HEADER_WIDTH, height);
375
- // selection background
376
- ctx.fillStyle = BACKGROUND_HEADER_SELECTED_COLOR;
377
- for (let zone of selection) {
378
- const x1 = Math.max(HEADER_WIDTH, cols[zone.left].start - offsetX);
379
- const x2 = Math.max(HEADER_WIDTH, cols[zone.right].end - offsetX);
380
- const y1 = Math.max(HEADER_HEIGHT, rows[zone.top].start - offsetY);
381
- const y2 = Math.max(HEADER_HEIGHT, rows[zone.bottom].end - offsetY);
382
- ctx.fillStyle = activeCols.has(zone.left)
383
- ? BACKGROUND_HEADER_ACTIVE_COLOR
384
- : BACKGROUND_HEADER_SELECTED_COLOR;
385
- ctx.fillRect(x1, 0, x2 - x1, HEADER_HEIGHT);
386
- ctx.fillStyle = activeRows.has(zone.top)
387
- ? BACKGROUND_HEADER_ACTIVE_COLOR
388
- : BACKGROUND_HEADER_SELECTED_COLOR;
389
- ctx.fillRect(0, y1, HEADER_WIDTH, y2 - y1);
390
- }
391
-
392
- // 2 main lines
393
- ctx.beginPath();
394
- ctx.moveTo(HEADER_WIDTH, 0);
395
- ctx.lineTo(HEADER_WIDTH, height);
396
- ctx.moveTo(0, HEADER_HEIGHT);
397
- ctx.lineTo(width, HEADER_HEIGHT);
398
- ctx.strokeStyle = HEADER_BORDER_COLOR;
399
- ctx.stroke();
400
-
401
- ctx.beginPath();
402
- // column text + separator
403
- for (let i = left; i <= right; i++) {
404
- const col = cols[i];
405
- ctx.fillStyle = activeCols.has(i) ? "#fff" : TEXT_HEADER_COLOR;
406
- ctx.fillText(col.name, (col.start + col.end) / 2 - offsetX, HEADER_HEIGHT / 2);
407
- ctx.moveTo(col.end - offsetX, 0);
408
- ctx.lineTo(col.end - offsetX, HEADER_HEIGHT);
409
- }
410
- // row text + separator
411
- for (let i = top; i <= bottom; i++) {
412
- const row = rows[i];
413
- ctx.fillStyle = activeRows.has(i) ? "#fff" : TEXT_HEADER_COLOR;
414
-
415
- ctx.fillText(row.name, HEADER_WIDTH / 2, (row.start + row.end) / 2 - offsetY);
416
- ctx.moveTo(0, row.end - offsetY);
417
- ctx.lineTo(HEADER_WIDTH, row.end - offsetY);
418
- }
419
-
420
- ctx.stroke();
421
- }
422
-
423
- private hasContent(col: number, row: number): boolean {
424
- const { cells, mergeCellMap } = this.workbook.activeSheet;
425
- const xc = toXC(col, row);
426
- const cell = cells[xc];
427
- return (cell && cell.content) || ((xc in mergeCellMap) as any);
428
- }
429
-
430
- private getGridBoxes(renderingContext: GridRenderingContext): Box[] {
431
- const { viewport } = renderingContext;
432
- let { right, left, top, bottom, offsetX, offsetY } = viewport;
433
- offsetX -= HEADER_WIDTH;
434
- offsetY -= HEADER_HEIGHT;
435
-
436
- const result: Box[] = [];
437
- const { cols, rows, mergeCellMap, cells, merges } = this.workbook.activeSheet;
438
- // process all visible cells
439
- for (let rowNumber = top; rowNumber <= bottom; rowNumber++) {
440
- let row = rows[rowNumber];
441
- for (let colNumber = left; colNumber <= right; colNumber++) {
442
- let cell = row.cells[colNumber];
443
- if (cell && !(cell.xc in mergeCellMap)) {
444
- let col = cols[colNumber];
445
- const text = this.getters.getCellText(cell);
446
- const textWidth = this.getters.getCellWidth(cell);
447
- let style = this.getters.getCellStyle(cell);
448
- const conditionalStyle = this.getters.getConditionalStyle(cell.xc);
449
- if (conditionalStyle) {
450
- style = Object.assign({}, style, conditionalStyle);
451
- }
452
- const align = text
453
- ? (style && style.align) || computeAlign(cell, this.getters.shouldShowFormulas())
454
- : undefined;
455
- let clipRect: Rect | null = null;
456
- if (text && textWidth > cols[cell.col].size) {
457
- let c: number;
458
- let width: number;
459
- switch (align) {
460
- case "left":
461
- c = cell.col;
462
- while (c < right && !this.hasContent(c + 1, cell.row)) {
463
- c++;
464
- }
465
- width = cols[c].end - col.start;
466
- if (width < textWidth) {
467
- clipRect = [col.start - offsetX, row.start - offsetY, width, row.size];
468
- }
469
- break;
470
- case "right":
471
- c = cell.col;
472
- while (c > left && !this.hasContent(c - 1, cell.row)) {
473
- c--;
474
- }
475
- width = col.end - cols[c].start;
476
- if (width < textWidth) {
477
- clipRect = [cols[c].start - offsetX, row.start - offsetY, width, row.size];
478
- }
479
- break;
480
- case "center":
481
- let c1 = colNumber;
482
- while (c1 > left && !this.hasContent(c1 - 1, rowNumber)) {
483
- c1--;
484
- }
485
- let c2 = colNumber;
486
- while (c2 < right && !this.hasContent(c2 + 1, rowNumber)) {
487
- c2++;
488
- }
489
- const colLeft = Math.min(c1, colNumber);
490
- const colRight = Math.max(c2, colNumber);
491
- width = cols[colRight].end - cols[colLeft].start;
492
- if (width < textWidth || colLeft === colNumber || colRight === colNumber) {
493
- clipRect = [cols[colLeft].start - offsetX, row.start - offsetY, width, row.size];
494
- }
495
- break;
496
- }
497
- }
498
-
499
- result.push({
500
- x: col.start - offsetX,
501
- y: row.start - offsetY,
502
- width: col.size,
503
- height: row.size,
504
- text,
505
- textWidth,
506
- border: this.getters.getCellBorder(cell),
507
- style,
508
- align,
509
- clipRect,
510
- error: cell.error,
511
- });
512
- }
513
- }
514
- }
515
-
516
- // process all visible merges
517
- for (let id in merges) {
518
- let merge = merges[id];
519
- if (overlap(merge, viewport)) {
520
- const refCell = cells[merge.topLeft];
521
- const bottomRight = cells[toXC(merge.right, merge.bottom)];
522
- const width = cols[merge.right].end - cols[merge.left].start;
523
- let text, textWidth, style, align, border;
524
- if (refCell || bottomRight) {
525
- text = refCell ? this.getters.getCellText(refCell) : "";
526
- textWidth = refCell ? this.getters.getCellWidth(refCell) : null;
527
- const cfStyle = this.getters.getConditionalStyle(merge.topLeft);
528
- style = cfStyle ? cfStyle : refCell ? this.getters.getCellStyle(refCell) : null;
529
- align = text
530
- ? (style && style.align) || computeAlign(refCell, this.getters.shouldShowFormulas())
531
- : null;
532
- const borderTopLeft = refCell ? this.getters.getCellBorder(refCell) : null;
533
- const borderBottomRight = bottomRight ? this.getters.getCellBorder(bottomRight) : null;
534
- border = {
535
- bottom: borderBottomRight ? borderBottomRight.bottom : null,
536
- left: borderTopLeft ? borderTopLeft.left : null,
537
- right: borderBottomRight ? borderBottomRight.right : null,
538
- top: borderTopLeft ? borderTopLeft.top : null,
539
- };
540
- }
541
- style = style || {};
542
- // Small trick: the code that draw the background color skips the color
543
- // #ffffff. But for merges, we actually need to draw the background,
544
- // otherwise the grid is visible. So, we change the #ffffff color to the
545
- // color #fff, which is actually the same.
546
- if (!style.fillColor || style.fillColor === "#ffffff") {
547
- style = Object.create(style);
548
- style.fillColor = "#fff";
549
- }
550
-
551
- const x = cols[merge.left].start - offsetX;
552
- const y = rows[merge.top].start - offsetY;
553
- const height = rows[merge.bottom].end - rows[merge.top].start;
554
- result.push({
555
- x: x,
556
- y: y,
557
- width,
558
- height,
559
- text,
560
- textWidth,
561
- border,
562
- style,
563
- align,
564
- clipRect: [x, y, width, height],
565
- error: refCell ? refCell.error : undefined,
566
- });
567
- }
568
- }
569
- return result;
570
- }
571
- }