@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,326 @@
1
+ import { BasePlugin } from "../base_plugin";
2
+ import { rangeReference } from "../formulas/index";
3
+ import { getNextColor, uuidv4 } from "../helpers/index";
4
+ import { Mode } from "../model";
5
+ import { CancelledReason, Command, CommandResult, Highlight, LAYERS } from "../types/index";
6
+ import { SelectionMode } from "./selection";
7
+
8
+ interface RangeInputValue {
9
+ id: string;
10
+ xc: string;
11
+ color?: string | null;
12
+ }
13
+
14
+ /**
15
+ * Selection input Plugin
16
+ *
17
+ * The SelectionInput component input and output are both arrays of strings, but
18
+ * it requires an intermediary internal state to work.
19
+ * This plugin handles this internal state.
20
+ */
21
+ export class SelectionInputPlugin extends BasePlugin {
22
+ static modes: Mode[] = ["normal", "readonly"];
23
+ static layers = [LAYERS.Highlights];
24
+ static getters = ["getSelectionInput", "getSelectionInputValue"];
25
+
26
+ private inputs: {
27
+ [id: string]: RangeInputValue[];
28
+ } = {};
29
+ private inputMaximums: { [id: string]: number } = {};
30
+ private focusedInput: string | null = null;
31
+ private focusedRange: number | null = null;
32
+ private willAddNewRange: boolean = false;
33
+
34
+ // ---------------------------------------------------------------------------
35
+ // Command Handling
36
+ // ---------------------------------------------------------------------------
37
+
38
+ allowDispatch(cmd: Command): CommandResult {
39
+ switch (cmd.type) {
40
+ case "FOCUS_RANGE":
41
+ const index = this.getIndex(cmd.id, cmd.rangeId);
42
+ if (this.focusedInput === cmd.id && this.focusedRange === index) {
43
+ return { status: "CANCELLED", reason: CancelledReason.InputAlreadyFocused };
44
+ }
45
+ break;
46
+ case "ADD_EMPTY_RANGE":
47
+ if (this.inputs[cmd.id].length === this.inputMaximums[cmd.id]) {
48
+ return { status: "CANCELLED", reason: CancelledReason.MaximumRangesReached };
49
+ }
50
+ break;
51
+ }
52
+ return { status: "SUCCESS" };
53
+ }
54
+
55
+ handle(cmd: Command) {
56
+ switch (cmd.type) {
57
+ case "ENABLE_NEW_SELECTION_INPUT":
58
+ this.initInput(cmd.id, cmd.initialRanges || [], cmd.maximumRanges);
59
+ break;
60
+ case "DISABLE_SELECTION_INPUT":
61
+ if (this.focusedInput === cmd.id) {
62
+ this.dispatch("HIGHLIGHT_SELECTION", { enabled: false });
63
+ this.dispatch("REMOVE_ALL_HIGHLIGHTS");
64
+ this.focusedRange = null;
65
+ this.focusedInput = null;
66
+ }
67
+ delete this.inputs[cmd.id];
68
+ delete this.inputMaximums[cmd.id];
69
+ break;
70
+ case "FOCUS_RANGE":
71
+ this.focus(cmd.id, this.getIndex(cmd.id, cmd.rangeId));
72
+ break;
73
+ case "CHANGE_RANGE": {
74
+ const index = this.getIndex(cmd.id, cmd.rangeId);
75
+ if (index !== null) {
76
+ this.changeRange(cmd.id, index, cmd.value);
77
+ }
78
+ break;
79
+ }
80
+ case "ADD_EMPTY_RANGE":
81
+ this.inputs[cmd.id] = [...this.inputs[cmd.id], Object.freeze({ xc: "", id: uuidv4() })];
82
+ this.focusLast(cmd.id);
83
+ break;
84
+ case "REMOVE_RANGE":
85
+ const index = this.getIndex(cmd.id, cmd.rangeId);
86
+ if (index !== null) {
87
+ this.removeRange(cmd.id, index);
88
+ }
89
+ break;
90
+ case "ADD_HIGHLIGHTS":
91
+ const highlights = this.getters.getHighlights();
92
+ this.add(highlights.slice(highlights.length - Object.keys(cmd.ranges).length));
93
+ break;
94
+ case "START_SELECTION_EXPANSION":
95
+ if (this.willAddNewRange) {
96
+ this.dispatch("RESET_PENDING_HIGHLIGHT");
97
+ }
98
+ break;
99
+ case "PREPARE_SELECTION_EXPANSION": {
100
+ const [id, index] = [this.focusedInput, this.focusedRange];
101
+ if (id !== null && index !== null) {
102
+ this.willAddNewRange = this.inputs[id][index].xc.trim() !== "";
103
+ }
104
+ break;
105
+ }
106
+ }
107
+ }
108
+
109
+ // ---------------------------------------------------------------------------
110
+ // Getters
111
+ // ---------------------------------------------------------------------------
112
+
113
+ getSelectionInput(id: string): (RangeInputValue & { isFocused: boolean })[] {
114
+ if (!this.inputs[id]) {
115
+ return [];
116
+ }
117
+ return this.inputs[id].map((input, index) =>
118
+ Object.assign({}, input, {
119
+ color: this.focusedInput === id && this.focusedRange !== null ? input.color : null,
120
+ isFocused: this.focusedInput === id && this.focusedRange === index,
121
+ })
122
+ );
123
+ }
124
+
125
+ getSelectionInputValue(id: string): string[] {
126
+ return this.cleanInputs(this.inputs[id].map((range) => range.xc));
127
+ }
128
+
129
+ // ---------------------------------------------------------------------------
130
+ // Other
131
+ // ---------------------------------------------------------------------------
132
+
133
+ private initInput(id: string, initialRanges: string[], maximumRanges?: number) {
134
+ this.inputs[id] = initialRanges.map((r) =>
135
+ Object.freeze({
136
+ xc: r,
137
+ id: uuidv4(),
138
+ })
139
+ ) as RangeInputValue[];
140
+ if (maximumRanges !== undefined) {
141
+ this.inputMaximums[id] = maximumRanges;
142
+ }
143
+ if (this.inputs[id].length === 0) {
144
+ this.dispatch("ADD_EMPTY_RANGE", { id });
145
+ }
146
+ }
147
+
148
+ /**
149
+ * Focus a given range or remove the focus.
150
+ */
151
+ private focus(id: string, index: number | null) {
152
+ const currentFocusedInput = this.focusedInput;
153
+ const currentFocusedRange = this.focusedInput && this.focusedRange;
154
+ this.focusedInput = id;
155
+
156
+ if (currentFocusedRange !== null && index == null) {
157
+ this.dispatch("HIGHLIGHT_SELECTION", { enabled: false });
158
+ this.removeAllHighlights();
159
+ }
160
+ if (currentFocusedInput !== null && id !== null && currentFocusedInput !== id) {
161
+ this.removeAllHighlights();
162
+ }
163
+ if ((currentFocusedRange === null && index !== null) || currentFocusedInput !== id) {
164
+ this.dispatch("HIGHLIGHT_SELECTION", { enabled: true });
165
+ this.highlightAllRanges(id);
166
+ }
167
+
168
+ this.setPendingRange(id, index);
169
+ if (index !== null) {
170
+ const color = this.inputs[id][index].color || getNextColor();
171
+ this.dispatch("SET_HIGHLIGHT_COLOR", { color });
172
+ }
173
+ this.focusedRange = index;
174
+ }
175
+
176
+ private focusLast(id: string) {
177
+ this.focus(id, this.inputs[id].length - 1);
178
+ }
179
+
180
+ private removeAllHighlights() {
181
+ this.dispatch("REMOVE_ALL_HIGHLIGHTS");
182
+ }
183
+
184
+ /**
185
+ * Highlight all valid ranges.
186
+ */
187
+ private highlightAllRanges(id: string) {
188
+ const inputs = this.inputs[id];
189
+ for (const [index, input] of inputs.entries()) {
190
+ this.focusedRange = index;
191
+ const ranges = this.inputToHighlights(input);
192
+ if (Object.keys(ranges).length > 0) {
193
+ this.dispatch("ADD_HIGHLIGHTS", { ranges });
194
+ }
195
+ }
196
+ }
197
+
198
+ private add(newHighlights: Highlight[]) {
199
+ if (
200
+ this.focusedInput === null ||
201
+ this.focusedRange === null ||
202
+ this.getters.getEditionMode() === "selecting" ||
203
+ newHighlights.length === 0
204
+ ) {
205
+ return;
206
+ }
207
+ const mode = this.getters.getSelectionMode();
208
+ if (mode === SelectionMode.expanding && this.willAddNewRange) {
209
+ this.addNewRange(this.focusedInput, newHighlights);
210
+ this.willAddNewRange = false;
211
+ } else {
212
+ this.setRange(this.focusedInput, this.focusedRange, newHighlights);
213
+ }
214
+ }
215
+
216
+ /**
217
+ * Add a new input at the end and focus it.
218
+ */
219
+ private addNewRange(id: string, highlights: Highlight[]) {
220
+ if (this.inputMaximums[id] < this.inputs[id].length + highlights.length) {
221
+ return;
222
+ }
223
+ this.inputs[id] = this.inputs[id].concat(this.highlightsToInput(highlights));
224
+ this.focusLast(id);
225
+ }
226
+
227
+ private setRange(id: string, index: number, highlights: Highlight[]) {
228
+ let [existingRange, ...newRanges] = this.highlightsToInput(highlights);
229
+ const additionalRanges = this.inputs[id].length + newRanges.length - this.inputMaximums[id];
230
+ if (additionalRanges) {
231
+ newRanges = newRanges.slice(0, newRanges.length - additionalRanges);
232
+ }
233
+ this.inputs[id].splice(index, 1, existingRange, ...newRanges);
234
+ // focus the last newly added range
235
+ if (newRanges.length) {
236
+ this.focus(id, index + newRanges.length);
237
+ }
238
+ }
239
+
240
+ private changeRange(id: string, index: number, value: string) {
241
+ if (this.focusedInput !== id || this.focusedRange !== index) {
242
+ this.dispatch("FOCUS_RANGE", { id, rangeId: this.inputs[id][index].id });
243
+ }
244
+ const input = this.inputs[id][index];
245
+ this.dispatch("REMOVE_HIGHLIGHTS", { ranges: this.inputToHighlights(input) });
246
+ this.dispatch("ADD_HIGHLIGHTS", {
247
+ ranges: this.inputToHighlights({
248
+ color: input.color,
249
+ xc: value,
250
+ }),
251
+ });
252
+ }
253
+
254
+ private removeRange(id: string, index: number) {
255
+ const [removedRange] = this.inputs[id].splice(index, 1);
256
+ if (this.focusedInput === id && this.focusedRange !== null) {
257
+ this.dispatch("REMOVE_HIGHLIGHTS", {
258
+ ranges: this.inputToHighlights(removedRange),
259
+ });
260
+ this.focusLast(id);
261
+ }
262
+ }
263
+
264
+ private setPendingRange(id: string, index: number | null) {
265
+ this.dispatch("RESET_PENDING_HIGHLIGHT");
266
+ if (index !== null && this.inputs[id][index].xc) {
267
+ this.dispatch("ADD_PENDING_HIGHLIGHTS", {
268
+ ranges: this.inputToHighlights(this.inputs[id][index]),
269
+ });
270
+ }
271
+ }
272
+
273
+ /**
274
+ * Convert highlights to the input format
275
+ */
276
+ private highlightsToInput(highlights: Highlight[]): RangeInputValue[] {
277
+ return highlights.map((h) =>
278
+ Object.freeze({
279
+ xc: this.getters.zoneToXC(h.zone),
280
+ id: uuidv4(),
281
+ color: h.color,
282
+ })
283
+ );
284
+ }
285
+
286
+ /**
287
+ * Convert highlights input format to the command format.
288
+ * The first xc in the input range will keep its color.
289
+ */
290
+ private inputToHighlights({ xc, color }: Pick<RangeInputValue, "xc" | "color">): {
291
+ [range: string]: string;
292
+ } {
293
+ const ranges = this.cleanInputs([xc]);
294
+ if (ranges.length === 0) return {};
295
+ const [fromInput, ...otherRanges] = ranges;
296
+ const highlights: { [range: string]: string } = {
297
+ [fromInput]: color || getNextColor(),
298
+ };
299
+ for (const range of otherRanges) {
300
+ highlights[range] = getNextColor();
301
+ }
302
+ return highlights;
303
+ }
304
+
305
+ private isRangeValid(xc: string): boolean {
306
+ return xc.match(rangeReference) !== null;
307
+ }
308
+
309
+ private cleanInputs(ranges: string[]): string[] {
310
+ return ranges
311
+ .map((xc) => xc.split(","))
312
+ .flat()
313
+ .map((xc) => xc.trim())
314
+ .filter((xc) => xc !== "")
315
+ .filter(this.isRangeValid);
316
+ }
317
+
318
+ /**
319
+ * Return the index of a range given its id
320
+ * or `null` if the range is not found.
321
+ */
322
+ private getIndex(id: string, rangeId: string | null): number | null {
323
+ const index = this.inputs[id].findIndex((range) => range.id === rangeId);
324
+ return index >= 0 ? index : null;
325
+ }
326
+ }
@@ -0,0 +1,76 @@
1
+ import { Registry } from "../registry";
2
+ import {
3
+ AutofillModifier,
4
+ CellData,
5
+ CopyModifier,
6
+ DIRECTION,
7
+ FormulaModifier,
8
+ Getters,
9
+ IncrementModifier,
10
+ Tooltip,
11
+ } from "../types/index";
12
+
13
+ /**
14
+ * An AutofillModifierImplementation is used to describe how to handle a
15
+ * AutofillModifier.
16
+ */
17
+ interface AutofillModifierImplementation {
18
+ apply: (
19
+ rule: AutofillModifier,
20
+ data: CellData,
21
+ getters: Getters,
22
+ direction: DIRECTION
23
+ ) => { cellData: CellData; tooltip?: Tooltip };
24
+ }
25
+
26
+ export const autofillModifiersRegistry = new Registry<AutofillModifierImplementation>();
27
+
28
+ autofillModifiersRegistry
29
+ .add("INCREMENT_MODIFIER", {
30
+ apply: (rule: IncrementModifier, data: CellData) => {
31
+ rule.current += rule.increment;
32
+ const content = (parseFloat(data.content!) + rule.current).toString();
33
+ return {
34
+ cellData: Object.assign({}, data, { content }),
35
+ tooltip: content ? { props: { content } } : undefined,
36
+ };
37
+ },
38
+ })
39
+ .add("COPY_MODIFIER", {
40
+ apply: (rule: CopyModifier, data: CellData) => {
41
+ return {
42
+ cellData: data,
43
+ tooltip: data.content ? { props: { content: data.content } } : undefined,
44
+ };
45
+ },
46
+ })
47
+ .add("FORMULA_MODIFIER", {
48
+ apply: (rule: FormulaModifier, data: CellData, getters: Getters, direction: DIRECTION) => {
49
+ rule.current += rule.increment;
50
+ let x = 0;
51
+ let y = 0;
52
+ switch (direction) {
53
+ case DIRECTION.UP:
54
+ x = 0;
55
+ y = -rule.current;
56
+ break;
57
+ case DIRECTION.DOWN:
58
+ x = 0;
59
+ y = rule.current;
60
+ break;
61
+ case DIRECTION.LEFT:
62
+ x = -rule.current;
63
+ y = 0;
64
+ break;
65
+ case DIRECTION.RIGHT:
66
+ x = rule.current;
67
+ y = 0;
68
+ break;
69
+ }
70
+ const content = getters.applyOffset(data.content!, x, y);
71
+ return {
72
+ cellData: Object.assign({}, data, { content }),
73
+ tooltip: content ? { props: { content } } : undefined,
74
+ };
75
+ },
76
+ });
@@ -0,0 +1,98 @@
1
+ import { Registry } from "../registry";
2
+ import { AutofillModifier, Cell } from "../types/index";
3
+
4
+ /**
5
+ * An AutofillRule is used to generate what to do when we need to autofill
6
+ * a cell. (In a AutofillGenerator, see plugins/autofill.ts)
7
+ *
8
+ * When we generate the rules to autofill, we take the first matching rule
9
+ * (ordered by sequence), and we generate the AutofillModifier with generateRule
10
+ */
11
+ interface AutofillRule {
12
+ condition: (cell: Cell, cells: (Cell | null)[]) => boolean;
13
+ generateRule: (cell: Cell, cells: (Cell | null)[]) => AutofillModifier;
14
+ sequence: number;
15
+ }
16
+
17
+ export const autofillRulesRegistry = new Registry<AutofillRule>();
18
+
19
+ /**
20
+ * Get the consecutive xc that are of type "number".
21
+ * Return the one which contains the given cell
22
+ */
23
+ function getGroup(cell: Cell, cells: (Cell | null)[]): string[] {
24
+ let group: string[] = [];
25
+ let found: boolean = false;
26
+ for (let x of cells) {
27
+ if (x === cell) {
28
+ found = true;
29
+ }
30
+ if (!x || x.type !== "number") {
31
+ if (found) {
32
+ return group;
33
+ }
34
+ group = [];
35
+ }
36
+ if (x && x.type === "number" && x.content) {
37
+ group.push(x.content);
38
+ }
39
+ }
40
+ return group;
41
+ }
42
+
43
+ /**
44
+ * Get the average steps between numbers
45
+ */
46
+ function getAverageIncrement(group: string[]) {
47
+ const averages: number[] = [];
48
+ let last = parseFloat(group[0]);
49
+ for (let i = 1; i < group.length; i++) {
50
+ const current = parseFloat(group[i]);
51
+ averages.push(current - last);
52
+ last = current;
53
+ }
54
+ return averages.reduce((a, b) => a + b, 0) / averages.length;
55
+ }
56
+
57
+ autofillRulesRegistry
58
+ .add("simple_value_copy", {
59
+ condition: (cell: Cell, cells: (Cell | null)[]) => {
60
+ return cells.length === 1 && ["text", "date", "number"].includes(cell.type);
61
+ },
62
+ generateRule: () => {
63
+ return { type: "COPY_MODIFIER" };
64
+ },
65
+ sequence: 10,
66
+ })
67
+ .add("copy_text", {
68
+ condition: (cell: Cell) => cell.type === "text",
69
+ generateRule: () => {
70
+ return { type: "COPY_MODIFIER" };
71
+ },
72
+ sequence: 20,
73
+ })
74
+ .add("update_formula", {
75
+ condition: (cell: Cell) => cell.type === "formula",
76
+ generateRule: (_, cells: (Cell | null)[]) => {
77
+ return { type: "FORMULA_MODIFIER", increment: cells.length, current: 0 };
78
+ },
79
+ sequence: 30,
80
+ })
81
+ .add("increment_number", {
82
+ condition: (cell: Cell) => cell.type === "number",
83
+ generateRule: (cell: Cell, cells: (Cell | null)[]) => {
84
+ const group = getGroup(cell, cells);
85
+ let increment: number = 1;
86
+ if (group.length == 2) {
87
+ increment = (parseFloat(group[1]) - parseFloat(group[0])) * 2;
88
+ } else if (group.length > 2) {
89
+ increment = getAverageIncrement(group) * group.length;
90
+ }
91
+ return {
92
+ type: "INCREMENT_MODIFIER",
93
+ increment,
94
+ current: 0,
95
+ };
96
+ },
97
+ sequence: 40,
98
+ });
@@ -0,0 +1,30 @@
1
+ import { ChartFigure } from "../components/figures/chart";
2
+ import { TextFigure } from "../components/figures/text_figure";
3
+ import { Registry } from "../registry";
4
+
5
+ //------------------------------------------------------------------------------
6
+ // Figure Registry
7
+ //------------------------------------------------------------------------------
8
+
9
+ /**
10
+ * This registry is intended to map a type of figure (tag) to a class of
11
+ * component, that will be used in the UI to represent the figure.
12
+ *
13
+ * The most important type of figure will be the Chart
14
+ */
15
+
16
+ export interface FigureContent {
17
+ Component: any;
18
+ SidePanelComponent?: string;
19
+ }
20
+
21
+ export const figureRegistry = new Registry<FigureContent>();
22
+
23
+ // figureRegistry.add("ConditionalFormatting", {
24
+ // title: "Conditional formatting",
25
+ // Body: ConditionalFormattingPanel,
26
+ // });
27
+ //
28
+
29
+ figureRegistry.add("text", { Component: TextFigure });
30
+ figureRegistry.add("chart", { Component: ChartFigure, SidePanelComponent: "ChartPanel" });
@@ -0,0 +1,7 @@
1
+ export * from "./autofill_modifiers";
2
+ export * from "./autofill_rules";
3
+ export * from "./figure_registry";
4
+ export * from "./menus/index";
5
+ export * from "./menu_items_registry";
6
+ export * from "./side_panel_registry";
7
+ export * from "./topbar_component_registry";
@@ -0,0 +1,110 @@
1
+ import { Registry } from "../registry";
2
+ import { SpreadsheetEnv } from "../types/env";
3
+
4
+ //------------------------------------------------------------------------------
5
+ // Menu Item Registry
6
+ //------------------------------------------------------------------------------
7
+
8
+ /**
9
+ * An ActionMenuItem represent a menu item for the menus of the topbar.
10
+ * Later, it will be reused for the context menu items as well.
11
+ *
12
+ * An ActionMenuItem has:
13
+ * - id, used for example to add child
14
+ * - name, which can be a string or a function to compute it
15
+ * - sequence, which represents its position inside the
16
+ * menus (the lower sequence it has, the upper it is in the menu)
17
+ * - isVisible, which can be defined to compute the visibility of the item
18
+ * - action, the action associated to this item
19
+ * - children, subitems associated to this item
20
+ * NB: an item without action or children is not displayed !
21
+ * - separator, whether it should add a separator below the item
22
+ * NB: a separator defined on the last item is not displayed !
23
+ *
24
+ */
25
+ export interface MenuItem {
26
+ name: string | ((env: SpreadsheetEnv) => string);
27
+ sequence: number;
28
+ id?: string;
29
+ isVisible?: (env: SpreadsheetEnv) => boolean;
30
+ isEnabled?: (env: SpreadsheetEnv) => boolean;
31
+ action?: (env: SpreadsheetEnv) => void;
32
+ children?: menuChildren;
33
+ separator?: boolean;
34
+ }
35
+
36
+ export type FullMenuItem = Required<MenuItem>;
37
+
38
+ type menuChildren = FullMenuItem[] | ((env: SpreadsheetEnv) => FullMenuItem[]);
39
+
40
+ const DEFAULT_MENU_ITEM = (key: string) => ({
41
+ isVisible: () => true,
42
+ isEnabled: () => true,
43
+ action: false,
44
+ children: [],
45
+ separator: false,
46
+ id: key,
47
+ });
48
+
49
+ export function createFullMenuItem(key: string, value: MenuItem): FullMenuItem {
50
+ return Object.assign({}, DEFAULT_MENU_ITEM(key), value);
51
+ }
52
+ /**
53
+ * The class Registry is extended in order to add the function addChild
54
+ *
55
+ */
56
+ export class MenuItemRegistry extends Registry<FullMenuItem> {
57
+ /**
58
+ * @override
59
+ */
60
+ add(key: string, value: MenuItem): MenuItemRegistry {
61
+ this.content[key] = createFullMenuItem(key, value);
62
+ return this;
63
+ }
64
+ /**
65
+ * Add a subitem to an existing item
66
+ * @param path Path of items to add this subitem
67
+ * @param value Subitem to add
68
+ */
69
+ addChild(key: string, path: string[], value: MenuItem): MenuItemRegistry {
70
+ const root = path.splice(0, 1)[0];
71
+ let node: FullMenuItem | undefined = this.content[root];
72
+ if (!node) {
73
+ throw new Error(`Path ${root + ":" + path.join(":")} not found`);
74
+ }
75
+ for (let p of path) {
76
+ if (typeof node.children === "function") {
77
+ node = undefined;
78
+ } else {
79
+ node = node.children.find((elt) => elt.id === p);
80
+ }
81
+ if (!node) {
82
+ throw new Error(`Path ${root + ":" + path.join(":")} not found`);
83
+ }
84
+ }
85
+ (node.children as FullMenuItem[]).push(createFullMenuItem(key, value));
86
+ return this;
87
+ }
88
+
89
+ getChildren(node: FullMenuItem, env: SpreadsheetEnv): FullMenuItem[] {
90
+ if (typeof node.children === "function") {
91
+ return node.children(env).sort((a, b) => a.sequence - b.sequence);
92
+ }
93
+ return node.children.sort((a, b) => a.sequence - b.sequence);
94
+ }
95
+
96
+ getName(node: FullMenuItem, env: SpreadsheetEnv): string {
97
+ if (typeof node.name === "function") {
98
+ return node.name(env);
99
+ }
100
+ return node.name;
101
+ }
102
+
103
+ /**
104
+ * Get a list of all elements in the registry, ordered by sequence
105
+ * @override
106
+ */
107
+ getAll(): FullMenuItem[] {
108
+ return super.getAll().sort((a, b) => a.sequence - b.sequence);
109
+ }
110
+ }