@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,326 +0,0 @@
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
- }
@@ -1,76 +0,0 @@
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
- });
@@ -1,98 +0,0 @@
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
- });
@@ -1,30 +0,0 @@
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" });
@@ -1,7 +0,0 @@
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";
@@ -1,110 +0,0 @@
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
- }