@odoo/o-spreadsheet 1.0.1

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