@visactor/vtable-sheet 1.20.0-alpha.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 (231) hide show
  1. package/cjs/components/vtable-sheet.d.ts +78 -0
  2. package/cjs/components/vtable-sheet.js +474 -0
  3. package/cjs/components/vtable-sheet.js.map +1 -0
  4. package/cjs/core/WorkSheet.d.ts +69 -0
  5. package/cjs/core/WorkSheet.js +298 -0
  6. package/cjs/core/WorkSheet.js.map +1 -0
  7. package/cjs/core/table-plugins.d.ts +3 -0
  8. package/cjs/core/table-plugins.js +171 -0
  9. package/cjs/core/table-plugins.js.map +1 -0
  10. package/cjs/event/event-manager.d.ts +29 -0
  11. package/cjs/event/event-manager.js +71 -0
  12. package/cjs/event/event-manager.js.map +1 -0
  13. package/cjs/event/event-target.d.ts +12 -0
  14. package/cjs/event/event-target.js +50 -0
  15. package/cjs/event/event-target.js.map +1 -0
  16. package/cjs/formula/cell-highlight-manager.d.ts +29 -0
  17. package/cjs/formula/cell-highlight-manager.js +141 -0
  18. package/cjs/formula/cell-highlight-manager.js.map +1 -0
  19. package/cjs/formula/formula-autocomplete.d.ts +39 -0
  20. package/cjs/formula/formula-autocomplete.js +239 -0
  21. package/cjs/formula/formula-autocomplete.js.map +1 -0
  22. package/cjs/formula/formula-editor.d.ts +21 -0
  23. package/cjs/formula/formula-editor.js +162 -0
  24. package/cjs/formula/formula-editor.js.map +1 -0
  25. package/cjs/formula/formula-helper.d.ts +8 -0
  26. package/cjs/formula/formula-helper.js +66 -0
  27. package/cjs/formula/formula-helper.js.map +1 -0
  28. package/cjs/formula/formula-range-selector.d.ts +19 -0
  29. package/cjs/formula/formula-range-selector.js +201 -0
  30. package/cjs/formula/formula-range-selector.js.map +1 -0
  31. package/cjs/formula/formula-throttle.d.ts +10 -0
  32. package/cjs/formula/formula-throttle.js +39 -0
  33. package/cjs/formula/formula-throttle.js.map +1 -0
  34. package/cjs/formula/formula-ui-manager.d.ts +19 -0
  35. package/cjs/formula/formula-ui-manager.js +226 -0
  36. package/cjs/formula/formula-ui-manager.js.map +1 -0
  37. package/cjs/formula/index.d.ts +5 -0
  38. package/cjs/formula/index.js +23 -0
  39. package/cjs/formula/index.js.map +1 -0
  40. package/cjs/index.d.ts +6 -0
  41. package/cjs/index.js +50 -0
  42. package/cjs/index.js.map +1 -0
  43. package/cjs/managers/formula-manager.d.ts +82 -0
  44. package/cjs/managers/formula-manager.js +445 -0
  45. package/cjs/managers/formula-manager.js.map +1 -0
  46. package/cjs/managers/menu-manager.d.ts +12 -0
  47. package/cjs/managers/menu-manager.js +89 -0
  48. package/cjs/managers/menu-manager.js.map +1 -0
  49. package/cjs/managers/sheet-manager.d.ts +21 -0
  50. package/cjs/managers/sheet-manager.js +74 -0
  51. package/cjs/managers/sheet-manager.js.map +1 -0
  52. package/cjs/managers/tab-drag-manager.d.ts +24 -0
  53. package/cjs/managers/tab-drag-manager.js +121 -0
  54. package/cjs/managers/tab-drag-manager.js.map +1 -0
  55. package/cjs/sheet-helper.d.ts +16 -0
  56. package/cjs/sheet-helper.js +64 -0
  57. package/cjs/sheet-helper.js.map +1 -0
  58. package/cjs/styles/common.d.ts +1 -0
  59. package/cjs/styles/common.js +17 -0
  60. package/cjs/styles/common.js.map +1 -0
  61. package/cjs/styles/formula-autocomplete.d.ts +1 -0
  62. package/cjs/styles/formula-autocomplete.js +17 -0
  63. package/cjs/styles/formula-autocomplete.js.map +1 -0
  64. package/cjs/styles/formula-bar.d.ts +1 -0
  65. package/cjs/styles/formula-bar.js +17 -0
  66. package/cjs/styles/formula-bar.js.map +1 -0
  67. package/cjs/styles/menu.d.ts +1 -0
  68. package/cjs/styles/menu.js +17 -0
  69. package/cjs/styles/menu.js.map +1 -0
  70. package/cjs/styles/sheet-tab.d.ts +1 -0
  71. package/cjs/styles/sheet-tab.js +17 -0
  72. package/cjs/styles/sheet-tab.js.map +1 -0
  73. package/cjs/styles/sheet.d.ts +1 -0
  74. package/cjs/styles/sheet.js +17 -0
  75. package/cjs/styles/sheet.js.map +1 -0
  76. package/cjs/styles/style-manager.d.ts +1 -0
  77. package/cjs/styles/style-manager.js +15 -0
  78. package/cjs/styles/style-manager.js.map +1 -0
  79. package/cjs/test/formula-complete.test.d.ts +1 -0
  80. package/cjs/test/formula-complete.test.js +42 -0
  81. package/cjs/test/formula-complete.test.js.map +1 -0
  82. package/cjs/tools/env.d.ts +20 -0
  83. package/cjs/tools/env.js +59 -0
  84. package/cjs/tools/env.js.map +1 -0
  85. package/cjs/tools/index.d.ts +3 -0
  86. package/cjs/tools/index.js +16 -0
  87. package/cjs/tools/index.js.map +1 -0
  88. package/cjs/tools/ui/snackbar.d.ts +1 -0
  89. package/cjs/tools/ui/snackbar.js +20 -0
  90. package/cjs/tools/ui/snackbar.js.map +1 -0
  91. package/cjs/ts-types/base.d.ts +44 -0
  92. package/cjs/ts-types/base.js +14 -0
  93. package/cjs/ts-types/base.js.map +1 -0
  94. package/cjs/ts-types/event.d.ts +79 -0
  95. package/cjs/ts-types/event.js +6 -0
  96. package/cjs/ts-types/event.js.map +1 -0
  97. package/cjs/ts-types/events.d.ts +1 -0
  98. package/cjs/ts-types/events.js +3 -0
  99. package/cjs/ts-types/events.js.map +1 -0
  100. package/cjs/ts-types/filter.d.ts +58 -0
  101. package/cjs/ts-types/filter.js +16 -0
  102. package/cjs/ts-types/filter.js.map +1 -0
  103. package/cjs/ts-types/formula.d.ts +51 -0
  104. package/cjs/ts-types/formula.js +6 -0
  105. package/cjs/ts-types/formula.js.map +1 -0
  106. package/cjs/ts-types/index.d.ts +67 -0
  107. package/cjs/ts-types/index.js +37 -0
  108. package/cjs/ts-types/index.js.map +1 -0
  109. package/cjs/ts-types/sheet.d.ts +45 -0
  110. package/cjs/ts-types/sheet.js +6 -0
  111. package/cjs/ts-types/sheet.js.map +1 -0
  112. package/cjs/vtable.d.ts +1 -0
  113. package/cjs/vtable.js +35 -0
  114. package/cjs/vtable.js.map +1 -0
  115. package/dist/vtable-sheet.js +114494 -0
  116. package/dist/vtable-sheet.min.js +853 -0
  117. package/es/components/vtable-sheet.d.ts +78 -0
  118. package/es/components/vtable-sheet.js +462 -0
  119. package/es/components/vtable-sheet.js.map +1 -0
  120. package/es/core/WorkSheet.d.ts +69 -0
  121. package/es/core/WorkSheet.js +298 -0
  122. package/es/core/WorkSheet.js.map +1 -0
  123. package/es/core/table-plugins.d.ts +3 -0
  124. package/es/core/table-plugins.js +164 -0
  125. package/es/core/table-plugins.js.map +1 -0
  126. package/es/event/event-manager.d.ts +29 -0
  127. package/es/event/event-manager.js +63 -0
  128. package/es/event/event-manager.js.map +1 -0
  129. package/es/event/event-target.d.ts +12 -0
  130. package/es/event/event-target.js +42 -0
  131. package/es/event/event-target.js.map +1 -0
  132. package/es/formula/cell-highlight-manager.d.ts +29 -0
  133. package/es/formula/cell-highlight-manager.js +133 -0
  134. package/es/formula/cell-highlight-manager.js.map +1 -0
  135. package/es/formula/formula-autocomplete.d.ts +39 -0
  136. package/es/formula/formula-autocomplete.js +231 -0
  137. package/es/formula/formula-autocomplete.js.map +1 -0
  138. package/es/formula/formula-editor.d.ts +21 -0
  139. package/es/formula/formula-editor.js +135 -0
  140. package/es/formula/formula-editor.js.map +1 -0
  141. package/es/formula/formula-helper.d.ts +8 -0
  142. package/es/formula/formula-helper.js +60 -0
  143. package/es/formula/formula-helper.js.map +1 -0
  144. package/es/formula/formula-range-selector.d.ts +19 -0
  145. package/es/formula/formula-range-selector.js +195 -0
  146. package/es/formula/formula-range-selector.js.map +1 -0
  147. package/es/formula/formula-throttle.d.ts +10 -0
  148. package/es/formula/formula-throttle.js +31 -0
  149. package/es/formula/formula-throttle.js.map +1 -0
  150. package/es/formula/formula-ui-manager.d.ts +19 -0
  151. package/es/formula/formula-ui-manager.js +218 -0
  152. package/es/formula/formula-ui-manager.js.map +1 -0
  153. package/es/formula/index.d.ts +5 -0
  154. package/es/formula/index.js +10 -0
  155. package/es/formula/index.js.map +1 -0
  156. package/es/index.d.ts +6 -0
  157. package/es/index.js +13 -0
  158. package/es/index.js.map +1 -0
  159. package/es/managers/formula-manager.d.ts +82 -0
  160. package/es/managers/formula-manager.js +441 -0
  161. package/es/managers/formula-manager.js.map +1 -0
  162. package/es/managers/menu-manager.d.ts +12 -0
  163. package/es/managers/menu-manager.js +81 -0
  164. package/es/managers/menu-manager.js.map +1 -0
  165. package/es/managers/sheet-manager.d.ts +21 -0
  166. package/es/managers/sheet-manager.js +66 -0
  167. package/es/managers/sheet-manager.js.map +1 -0
  168. package/es/managers/tab-drag-manager.d.ts +24 -0
  169. package/es/managers/tab-drag-manager.js +113 -0
  170. package/es/managers/tab-drag-manager.js.map +1 -0
  171. package/es/sheet-helper.d.ts +16 -0
  172. package/es/sheet-helper.js +54 -0
  173. package/es/sheet-helper.js.map +1 -0
  174. package/es/styles/common.d.ts +1 -0
  175. package/es/styles/common.js +9 -0
  176. package/es/styles/common.js.map +1 -0
  177. package/es/styles/formula-autocomplete.d.ts +1 -0
  178. package/es/styles/formula-autocomplete.js +9 -0
  179. package/es/styles/formula-autocomplete.js.map +1 -0
  180. package/es/styles/formula-bar.d.ts +1 -0
  181. package/es/styles/formula-bar.js +9 -0
  182. package/es/styles/formula-bar.js.map +1 -0
  183. package/es/styles/menu.d.ts +1 -0
  184. package/es/styles/menu.js +9 -0
  185. package/es/styles/menu.js.map +1 -0
  186. package/es/styles/sheet-tab.d.ts +1 -0
  187. package/es/styles/sheet-tab.js +9 -0
  188. package/es/styles/sheet-tab.js.map +1 -0
  189. package/es/styles/sheet.d.ts +1 -0
  190. package/es/styles/sheet.js +9 -0
  191. package/es/styles/sheet.js.map +1 -0
  192. package/es/styles/style-manager.d.ts +1 -0
  193. package/es/styles/style-manager.js +17 -0
  194. package/es/styles/style-manager.js.map +1 -0
  195. package/es/test/formula-complete.test.d.ts +1 -0
  196. package/es/test/formula-complete.test.js +36 -0
  197. package/es/test/formula-complete.test.js.map +1 -0
  198. package/es/tools/env.d.ts +20 -0
  199. package/es/tools/env.js +53 -0
  200. package/es/tools/env.js.map +1 -0
  201. package/es/tools/index.d.ts +3 -0
  202. package/es/tools/index.js +9 -0
  203. package/es/tools/index.js.map +1 -0
  204. package/es/tools/ui/snackbar.d.ts +1 -0
  205. package/es/tools/ui/snackbar.js +14 -0
  206. package/es/tools/ui/snackbar.js.map +1 -0
  207. package/es/ts-types/base.d.ts +44 -0
  208. package/es/ts-types/base.js +14 -0
  209. package/es/ts-types/base.js.map +1 -0
  210. package/es/ts-types/event.d.ts +79 -0
  211. package/es/ts-types/event.js +2 -0
  212. package/es/ts-types/event.js.map +1 -0
  213. package/es/ts-types/events.d.ts +1 -0
  214. package/es/ts-types/events.js +3 -0
  215. package/es/ts-types/events.js.map +1 -0
  216. package/es/ts-types/filter.d.ts +58 -0
  217. package/es/ts-types/filter.js +16 -0
  218. package/es/ts-types/filter.js.map +1 -0
  219. package/es/ts-types/formula.d.ts +51 -0
  220. package/es/ts-types/formula.js +2 -0
  221. package/es/ts-types/formula.js.map +1 -0
  222. package/es/ts-types/index.d.ts +67 -0
  223. package/es/ts-types/index.js +14 -0
  224. package/es/ts-types/index.js.map +1 -0
  225. package/es/ts-types/sheet.d.ts +45 -0
  226. package/es/ts-types/sheet.js +2 -0
  227. package/es/ts-types/sheet.js.map +1 -0
  228. package/es/vtable.d.ts +1 -0
  229. package/es/vtable.js +2 -0
  230. package/es/vtable.js.map +1 -0
  231. package/package.json +120 -0
@@ -0,0 +1,39 @@
1
+ import type VTableSheet from '../components/vtable-sheet';
2
+ interface AutocompleteItem {
3
+ type: 'function' | 'cell' | 'range';
4
+ value: string;
5
+ label: string;
6
+ description?: string;
7
+ signature?: string;
8
+ }
9
+ export declare class FormulaAutocomplete {
10
+ private container;
11
+ private dropdown;
12
+ private items;
13
+ private selectedIndex;
14
+ private isVisible;
15
+ private inputElement;
16
+ private sheet;
17
+ private onSelectCallback?;
18
+ private customPositioning?;
19
+ constructor(container: HTMLElement, sheet: VTableSheet);
20
+ private createDropdown;
21
+ attachTo(input: HTMLInputElement, onSelect?: (item: AutocompleteItem) => void, customPositioning?: () => void): void;
22
+ private handleInput;
23
+ private parseInputContext;
24
+ private updateSuggestions;
25
+ private generateCellSuggestions;
26
+ private getFunctionDescription;
27
+ private getFunctionSignature;
28
+ private renderDropdown;
29
+ private groupItems;
30
+ private getGroupTitle;
31
+ private getTypeLabel;
32
+ private positionDropdown;
33
+ private handleKeydown;
34
+ private selectItem;
35
+ private show;
36
+ private hide;
37
+ release(): void;
38
+ }
39
+ export {};
@@ -0,0 +1,239 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: !0
5
+ }), exports.FormulaAutocomplete = void 0;
6
+
7
+ class FormulaAutocomplete {
8
+ constructor(container, sheet) {
9
+ this.dropdown = null, this.items = [], this.selectedIndex = -1, this.isVisible = !1,
10
+ this.inputElement = null, this.container = container, this.sheet = sheet, this.createDropdown();
11
+ }
12
+ createDropdown() {
13
+ this.dropdown = document.createElement("div"), this.dropdown.className = "vtable-formula-autocomplete",
14
+ this.dropdown.style.display = "none", this.container.appendChild(this.dropdown);
15
+ }
16
+ attachTo(input, onSelect, customPositioning) {
17
+ this.inputElement = input, this.onSelectCallback = onSelect, this.customPositioning = customPositioning,
18
+ input.addEventListener("input", this.handleInput.bind(this)), input.addEventListener("keydown", this.handleKeydown.bind(this)),
19
+ input.addEventListener("blur", (() => {
20
+ setTimeout((() => this.hide()), 200);
21
+ }));
22
+ }
23
+ handleInput(event) {
24
+ const input = event.target, value = input.value, cursorPos = input.selectionStart || 0, context = this.parseInputContext(value, cursorPos);
25
+ context.shouldShowAutocomplete ? (this.updateSuggestions(context), this.show()) : this.hide();
26
+ }
27
+ parseInputContext(value, cursorPos) {
28
+ if (!value.startsWith("=")) return {
29
+ shouldShowAutocomplete: !1,
30
+ searchTerm: "",
31
+ type: "all",
32
+ insertPosition: 0
33
+ };
34
+ const textBeforeCursor = value.substring(1, cursorPos), lastOperatorMatch = textBeforeCursor.match(/[\+\-\*\/\(\,\s]([A-Za-z0-9]*)$/);
35
+ return lastOperatorMatch && "" !== lastOperatorMatch[1] ? {
36
+ shouldShowAutocomplete: !0,
37
+ searchTerm: lastOperatorMatch[1],
38
+ type: /^[A-Za-z]/.test(lastOperatorMatch[1]) ? "function" : "cell",
39
+ insertPosition: cursorPos - lastOperatorMatch[1].length
40
+ } : textBeforeCursor.match(/^[A-Za-z0-9]*$/) ? {
41
+ shouldShowAutocomplete: !0,
42
+ searchTerm: textBeforeCursor,
43
+ type: "all",
44
+ insertPosition: 1
45
+ } : {
46
+ shouldShowAutocomplete: !1,
47
+ searchTerm: "",
48
+ type: "all",
49
+ insertPosition: 0
50
+ };
51
+ }
52
+ updateSuggestions(context) {
53
+ const {searchTerm: searchTerm, type: type} = context;
54
+ if (this.items = [], "function" === type || "all" === type) {
55
+ const functionItems = this.sheet.formulaManager.getAvailableFunctions().filter((fn => fn.toUpperCase().startsWith(searchTerm.toUpperCase()))).slice(0, 10).map((fn => ({
56
+ type: "function",
57
+ value: fn,
58
+ label: fn,
59
+ description: this.getFunctionDescription(fn),
60
+ signature: this.getFunctionSignature(fn)
61
+ })));
62
+ this.items.push(...functionItems);
63
+ }
64
+ if ("cell" === type || "all" === type) {
65
+ const cellSuggestions = this.generateCellSuggestions(searchTerm);
66
+ this.items.push(...cellSuggestions);
67
+ }
68
+ this.renderDropdown();
69
+ }
70
+ generateCellSuggestions(searchTerm) {
71
+ const suggestions = [], cellMatch = searchTerm.match(/^([A-Za-z]*)([0-9]*)$/);
72
+ if (cellMatch) {
73
+ const [, colPart, rowPart] = cellMatch;
74
+ if (colPart && !rowPart) for (let row = 1; row <= 5; row++) suggestions.push({
75
+ type: "cell",
76
+ value: `${colPart.toUpperCase()}${row}`,
77
+ label: `${colPart.toUpperCase()}${row}`,
78
+ description: "单元格引用"
79
+ });
80
+ if (colPart && rowPart) {
81
+ const address = `${colPart.toUpperCase()}${rowPart}`;
82
+ suggestions.push({
83
+ type: "cell",
84
+ value: address,
85
+ label: address,
86
+ description: "单元格引用"
87
+ }), suggestions.push({
88
+ type: "range",
89
+ value: `${address}:`,
90
+ label: `${address}:...`,
91
+ description: "开始选择范围"
92
+ });
93
+ }
94
+ }
95
+ return suggestions.slice(0, 5);
96
+ }
97
+ getFunctionDescription(functionName) {
98
+ return {
99
+ SUM: "计算数值总和",
100
+ AVERAGE: "计算平均值",
101
+ COUNT: "计数数值单元格",
102
+ MAX: "返回最大值",
103
+ MIN: "返回最小值",
104
+ IF: "条件判断",
105
+ VLOOKUP: "垂直查找",
106
+ CONCATENATE: "连接文本"
107
+ }[functionName] || "";
108
+ }
109
+ getFunctionSignature(functionName) {
110
+ return {
111
+ SUM: "(number1, [number2], ...)",
112
+ AVERAGE: "(number1, [number2], ...)",
113
+ COUNT: "(value1, [value2], ...)",
114
+ MAX: "(number1, [number2], ...)",
115
+ MIN: "(number1, [number2], ...)",
116
+ IF: "(logical_test, value_if_true, value_if_false)",
117
+ VLOOKUP: "(lookup_value, table_array, col_index_num, [range_lookup])",
118
+ CONCATENATE: "(text1, [text2], ...)"
119
+ }[functionName] || "()";
120
+ }
121
+ renderDropdown() {
122
+ if (!this.dropdown) return;
123
+ if (this.dropdown.innerHTML = "", 0 === this.items.length) {
124
+ const emptyEl = document.createElement("div");
125
+ return emptyEl.className = "vtable-formula-autocomplete-empty", emptyEl.textContent = "没有匹配的函数",
126
+ void this.dropdown.appendChild(emptyEl);
127
+ }
128
+ const groups = this.groupItems(this.items);
129
+ groups.forEach((group => {
130
+ if (groups.length > 1) {
131
+ const groupEl = document.createElement("div");
132
+ groupEl.className = "vtable-formula-autocomplete-group", groupEl.textContent = this.getGroupTitle(group.type),
133
+ this.dropdown.appendChild(groupEl);
134
+ }
135
+ group.items.forEach(((item, index) => {
136
+ const globalIndex = this.items.indexOf(item), itemEl = document.createElement("div");
137
+ itemEl.className = "vtable-formula-autocomplete-item", globalIndex === this.selectedIndex && itemEl.classList.add("selected");
138
+ const nameEl = document.createElement("div");
139
+ nameEl.className = "item-name";
140
+ const labelEl = document.createElement("span");
141
+ labelEl.textContent = item.label, nameEl.appendChild(labelEl);
142
+ const typeEl = document.createElement("span");
143
+ if (typeEl.className = `item-type ${item.type}`, typeEl.textContent = this.getTypeLabel(item.type),
144
+ nameEl.appendChild(typeEl), itemEl.appendChild(nameEl), item.signature) {
145
+ const signatureEl = document.createElement("div");
146
+ signatureEl.className = "item-signature", signatureEl.textContent = item.signature,
147
+ itemEl.appendChild(signatureEl);
148
+ }
149
+ if (item.description) {
150
+ const descEl = document.createElement("div");
151
+ descEl.className = "item-description", descEl.textContent = item.description, itemEl.appendChild(descEl);
152
+ }
153
+ itemEl.addEventListener("click", (() => this.selectItem(globalIndex))), itemEl.addEventListener("mouseenter", (() => {
154
+ this.selectedIndex = globalIndex, this.renderDropdown();
155
+ })), this.dropdown.appendChild(itemEl);
156
+ }));
157
+ }));
158
+ const footerEl = document.createElement("div");
159
+ footerEl.className = "vtable-formula-autocomplete-footer", footerEl.innerHTML = '\n <div class="shortcut">\n <span class="key">↑↓</span>\n <span>选择</span>\n </div>\n <div class="shortcut">\n <span class="key">Enter</span>\n <span>确认</span>\n </div>\n <div class="shortcut">\n <span class="key">Esc</span>\n <span>取消</span>\n </div>\n ',
160
+ this.dropdown.appendChild(footerEl), this.positionDropdown();
161
+ }
162
+ groupItems(items) {
163
+ const groups = {};
164
+ items.forEach((item => {
165
+ groups[item.type] || (groups[item.type] = []), groups[item.type].push(item);
166
+ }));
167
+ return [ "function", "cell", "range" ].filter((type => groups[type])).map((type => ({
168
+ type: type,
169
+ items: groups[type]
170
+ })));
171
+ }
172
+ getGroupTitle(type) {
173
+ return {
174
+ function: "函数",
175
+ cell: "单元格",
176
+ range: "范围"
177
+ }[type] || type;
178
+ }
179
+ getTypeLabel(type) {
180
+ return {
181
+ function: "fx",
182
+ cell: "A1",
183
+ range: "A:B"
184
+ }[type] || type;
185
+ }
186
+ positionDropdown() {
187
+ if (this.customPositioning) return void this.customPositioning();
188
+ if (!this.dropdown || !this.inputElement) return;
189
+ const inputRect = this.inputElement.getBoundingClientRect(), containerRect = this.container.getBoundingClientRect();
190
+ this.dropdown.style.position = "absolute", this.dropdown.style.left = inputRect.left - containerRect.left + "px",
191
+ this.dropdown.style.top = inputRect.bottom - containerRect.top + 2 + "px", this.dropdown.style.width = `${Math.max(300, inputRect.width)}px`;
192
+ }
193
+ handleKeydown(event) {
194
+ if (this.isVisible) switch (event.key) {
195
+ case "ArrowDown":
196
+ event.preventDefault(), this.selectedIndex = Math.min(this.selectedIndex + 1, this.items.length - 1),
197
+ this.renderDropdown();
198
+ break;
199
+
200
+ case "ArrowUp":
201
+ event.preventDefault(), this.selectedIndex = Math.max(this.selectedIndex - 1, 0),
202
+ this.renderDropdown();
203
+ break;
204
+
205
+ case "Enter":
206
+ case "Tab":
207
+ this.selectedIndex >= 0 && (event.preventDefault(), this.selectItem(this.selectedIndex));
208
+ break;
209
+
210
+ case "Escape":
211
+ this.hide();
212
+ }
213
+ }
214
+ selectItem(index) {
215
+ const item = this.items[index];
216
+ if (item && this.inputElement) {
217
+ if (this.onSelectCallback) this.onSelectCallback(item); else {
218
+ const value = this.inputElement.value, cursorPos = this.inputElement.selectionStart || 0, context = this.parseInputContext(value, cursorPos), newValue = value.substring(0, context.insertPosition) + item.value + ("function" === item.type ? "(" : "") + value.substring(cursorPos);
219
+ this.inputElement.value = newValue;
220
+ const newCursorPos = context.insertPosition + item.value.length + ("function" === item.type ? 1 : 0);
221
+ this.inputElement.setSelectionRange(newCursorPos, newCursorPos);
222
+ }
223
+ this.hide();
224
+ }
225
+ }
226
+ show() {
227
+ this.dropdown && this.items.length > 0 && (this.dropdown.style.display = "block",
228
+ this.isVisible = !0, this.selectedIndex = 0, this.renderDropdown());
229
+ }
230
+ hide() {
231
+ this.dropdown && (this.dropdown.style.display = "none", this.isVisible = !1, this.selectedIndex = -1);
232
+ }
233
+ release() {
234
+ this.dropdown && this.dropdown.parentNode && this.dropdown.parentNode.removeChild(this.dropdown);
235
+ }
236
+ }
237
+
238
+ exports.FormulaAutocomplete = FormulaAutocomplete;
239
+ //# sourceMappingURL=formula-autocomplete.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/formula/formula-autocomplete.ts"],"names":[],"mappings":";;;AAUA,MAAa,mBAAmB;IAY9B,YAAY,SAAsB,EAAE,KAAkB;QAV9C,aAAQ,GAAuB,IAAI,CAAC;QACpC,UAAK,GAAuB,EAAE,CAAC;QAC/B,kBAAa,GAAW,CAAC,CAAC,CAAC;QAC3B,cAAS,GAAY,KAAK,CAAC;QAC3B,iBAAY,GAA4B,IAAI,CAAC;QAOnD,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,cAAc,EAAE,CAAC;IACxB,CAAC;IAKO,cAAc;QACpB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;QAC9C,IAAI,CAAC,QAAQ,CAAC,SAAS,GAAG,6BAA6B,CAAC;QACxD,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC;QACrC,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC5C,CAAC;IAKD,QAAQ,CAAC,KAAuB,EAAE,QAA2C,EAAE,iBAA8B;QAC3G,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;QAC1B,IAAI,CAAC,gBAAgB,GAAG,QAAQ,CAAC;QACjC,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAC;QAG3C,KAAK,CAAC,gBAAgB,CAAC,OAAO,EAAE,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QAC7D,KAAK,CAAC,gBAAgB,CAAC,SAAS,EAAE,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QACjE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C,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;SACrD;IACH,CAAC;CACF;AA3eD,kDA2eC","file":"formula-autocomplete.js","sourcesContent":["import type VTableSheet from '../components/vtable-sheet';\n\ninterface AutocompleteItem {\n type: 'function' | 'cell' | 'range';\n value: string;\n label: string;\n description?: string;\n signature?: string;\n}\n\nexport class FormulaAutocomplete {\n private container: HTMLElement;\n private dropdown: HTMLElement | null = null;\n private items: AutocompleteItem[] = [];\n private selectedIndex: number = -1;\n private isVisible: boolean = false;\n private inputElement: HTMLInputElement | null = null;\n private sheet: VTableSheet;\n private onSelectCallback?: (item: AutocompleteItem) => void;\n\n private customPositioning?: () => void;\n\n constructor(container: HTMLElement, sheet: VTableSheet) {\n this.container = container;\n this.sheet = sheet;\n this.createDropdown();\n }\n\n /**\n * 创建下拉框元素\n */\n private createDropdown(): void {\n this.dropdown = document.createElement('div');\n this.dropdown.className = 'vtable-formula-autocomplete';\n this.dropdown.style.display = 'none';\n this.container.appendChild(this.dropdown);\n }\n\n /**\n * 绑定到输入框\n */\n attachTo(input: HTMLInputElement, onSelect?: (item: AutocompleteItem) => void, customPositioning?: () => void): void {\n this.inputElement = input;\n this.onSelectCallback = onSelect;\n this.customPositioning = customPositioning;\n\n // 绑定事件\n input.addEventListener('input', this.handleInput.bind(this));\n input.addEventListener('keydown', this.handleKeydown.bind(this));\n input.addEventListener('blur', () => {\n setTimeout(() => this.hide(), 200);\n });\n }\n\n /**\n * 处理输入事件\n */\n private handleInput(event: Event): void {\n const input = event.target as HTMLInputElement;\n const value = input.value;\n const cursorPos = input.selectionStart || 0;\n\n // 解析当前输入上下文\n const context = this.parseInputContext(value, cursorPos);\n\n if (context.shouldShowAutocomplete) {\n this.updateSuggestions(context);\n this.show();\n } else {\n this.hide();\n }\n }\n\n /**\n * 解析输入上下文\n */\n private parseInputContext(\n value: string,\n cursorPos: number\n ): {\n shouldShowAutocomplete: boolean;\n searchTerm: string;\n type: 'function' | 'cell' | 'all';\n insertPosition: number;\n } {\n // 如果不是公式,不显示\n if (!value.startsWith('=')) {\n return { shouldShowAutocomplete: false, searchTerm: '', type: 'all', insertPosition: 0 };\n }\n\n // 获取光标前的文本\n const textBeforeCursor = value.substring(1, cursorPos); // 去掉 '='\n\n // 查找最后一个函数或操作符\n const lastOperatorMatch = textBeforeCursor.match(/[\\+\\-\\*\\/\\(\\,\\s]([A-Za-z0-9]*)$/);\n\n if (lastOperatorMatch && lastOperatorMatch[1] !== '') {\n return {\n shouldShowAutocomplete: true,\n searchTerm: lastOperatorMatch[1],\n type: /^[A-Za-z]/.test(lastOperatorMatch[1]) ? 'function' : 'cell',\n insertPosition: cursorPos - lastOperatorMatch[1].length\n };\n }\n\n // 公式开始位置\n if (textBeforeCursor.match(/^[A-Za-z0-9]*$/)) {\n return {\n shouldShowAutocomplete: true,\n searchTerm: textBeforeCursor,\n type: 'all',\n insertPosition: 1\n };\n }\n\n return { shouldShowAutocomplete: false, searchTerm: '', type: 'all', insertPosition: 0 };\n }\n\n /**\n * 更新建议列表\n */\n private updateSuggestions(context: any): void {\n const { searchTerm, type } = context;\n this.items = [];\n\n // 获取函数列表\n if (type === 'function' || type === 'all') {\n const functions = this.sheet.formulaManager.getAvailableFunctions();\n const functionItems = functions\n .filter(fn => fn.toUpperCase().startsWith(searchTerm.toUpperCase()))\n .slice(0, 10) // 限制数量\n .map(fn => ({\n type: 'function' as const,\n value: fn,\n label: fn,\n description: this.getFunctionDescription(fn),\n signature: this.getFunctionSignature(fn)\n }));\n this.items.push(...functionItems);\n }\n\n // 添加单元格引用建议\n if (type === 'cell' || type === 'all') {\n const cellSuggestions = this.generateCellSuggestions(searchTerm);\n this.items.push(...cellSuggestions);\n }\n console.log('updateSuggestions', context, this.items);\n this.renderDropdown();\n }\n\n /**\n * 生成单元格引用建议\n */\n private generateCellSuggestions(searchTerm: string): AutocompleteItem[] {\n const suggestions: AutocompleteItem[] = [];\n\n // 如果是有效的部分单元格地址\n const cellMatch = searchTerm.match(/^([A-Za-z]*)([0-9]*)$/);\n if (cellMatch) {\n const [, colPart, rowPart] = cellMatch;\n\n // 生成列建议\n if (colPart && !rowPart) {\n // A -> A1, A2, A3...\n for (let row = 1; row <= 5; row++) {\n suggestions.push({\n type: 'cell',\n value: `${colPart.toUpperCase()}${row}`,\n label: `${colPart.toUpperCase()}${row}`,\n description: '单元格引用'\n });\n }\n }\n\n // 生成完整单元格建议\n if (colPart && rowPart) {\n const address = `${colPart.toUpperCase()}${rowPart}`;\n suggestions.push({\n type: 'cell',\n value: address,\n label: address,\n description: '单元格引用'\n });\n\n // 添加范围建议\n suggestions.push({\n type: 'range',\n value: `${address}:`,\n label: `${address}:...`,\n description: '开始选择范围'\n });\n }\n }\n\n return suggestions.slice(0, 5); // 限制建议数量\n }\n\n /**\n * 获取函数描述\n */\n private getFunctionDescription(functionName: string): string {\n const descriptions: Record<string, string> = {\n SUM: '计算数值总和',\n AVERAGE: '计算平均值',\n COUNT: '计数数值单元格',\n MAX: '返回最大值',\n MIN: '返回最小值',\n IF: '条件判断',\n VLOOKUP: '垂直查找',\n CONCATENATE: '连接文本'\n // ... 更多函数描述\n };\n return descriptions[functionName] || '';\n }\n\n /**\n * 获取函数签名\n */\n private getFunctionSignature(functionName: string): string {\n const signatures: Record<string, string> = {\n SUM: '(number1, [number2], ...)',\n AVERAGE: '(number1, [number2], ...)',\n COUNT: '(value1, [value2], ...)',\n MAX: '(number1, [number2], ...)',\n MIN: '(number1, [number2], ...)',\n IF: '(logical_test, value_if_true, value_if_false)',\n VLOOKUP: '(lookup_value, table_array, col_index_num, [range_lookup])',\n CONCATENATE: '(text1, [text2], ...)'\n // ... 更多函数签名\n };\n return signatures[functionName] || '()';\n }\n\n /**\n * 渲染下拉框\n */\n private renderDropdown(): void {\n if (!this.dropdown) {\n return;\n }\n\n this.dropdown.innerHTML = '';\n\n if (this.items.length === 0) {\n // 无结果提示\n const emptyEl = document.createElement('div');\n emptyEl.className = 'vtable-formula-autocomplete-empty';\n emptyEl.textContent = '没有匹配的函数';\n this.dropdown.appendChild(emptyEl);\n return;\n }\n\n // 按类型分组\n const groups = this.groupItems(this.items);\n\n groups.forEach(group => {\n // 添加分组标题\n if (groups.length > 1) {\n const groupEl = document.createElement('div');\n groupEl.className = 'vtable-formula-autocomplete-group';\n groupEl.textContent = this.getGroupTitle(group.type);\n this.dropdown.appendChild(groupEl);\n }\n\n // 渲染组内项目\n group.items.forEach((item, index) => {\n const globalIndex = this.items.indexOf(item);\n const itemEl = document.createElement('div');\n itemEl.className = 'vtable-formula-autocomplete-item';\n\n if (globalIndex === this.selectedIndex) {\n itemEl.classList.add('selected');\n }\n\n // 函数名和类型标签\n const nameEl = document.createElement('div');\n nameEl.className = 'item-name';\n\n const labelEl = document.createElement('span');\n labelEl.textContent = item.label;\n nameEl.appendChild(labelEl);\n\n const typeEl = document.createElement('span');\n typeEl.className = `item-type ${item.type}`;\n typeEl.textContent = this.getTypeLabel(item.type);\n nameEl.appendChild(typeEl);\n\n itemEl.appendChild(nameEl);\n\n // 函数签名\n if (item.signature) {\n const signatureEl = document.createElement('div');\n signatureEl.className = 'item-signature';\n signatureEl.textContent = item.signature;\n itemEl.appendChild(signatureEl);\n }\n\n // 描述\n if (item.description) {\n const descEl = document.createElement('div');\n descEl.className = 'item-description';\n descEl.textContent = item.description;\n itemEl.appendChild(descEl);\n }\n\n itemEl.addEventListener('click', () => this.selectItem(globalIndex));\n itemEl.addEventListener('mouseenter', () => {\n this.selectedIndex = globalIndex;\n this.renderDropdown();\n });\n\n this.dropdown.appendChild(itemEl);\n });\n });\n\n // 添加快捷键提示\n const footerEl = document.createElement('div');\n footerEl.className = 'vtable-formula-autocomplete-footer';\n footerEl.innerHTML = `\n <div class=\"shortcut\">\n <span class=\"key\">↑↓</span>\n <span>选择</span>\n </div>\n <div class=\"shortcut\">\n <span class=\"key\">Enter</span>\n <span>确认</span>\n </div>\n <div class=\"shortcut\">\n <span class=\"key\">Esc</span>\n <span>取消</span>\n </div>\n `;\n this.dropdown.appendChild(footerEl);\n\n this.positionDropdown();\n }\n\n /**\n * 对项目进行分组\n */\n private groupItems(items: AutocompleteItem[]): Array<{ type: string; items: AutocompleteItem[] }> {\n const groups: Record<string, AutocompleteItem[]> = {};\n\n items.forEach(item => {\n if (!groups[item.type]) {\n groups[item.type] = [];\n }\n groups[item.type].push(item);\n });\n\n // 按照优先级排序:function > cell > range\n const priority = ['function', 'cell', 'range'];\n return priority.filter(type => groups[type]).map(type => ({ type, items: groups[type] }));\n }\n\n /**\n * 获取分组标题\n */\n private getGroupTitle(type: string): string {\n const titles: Record<string, string> = {\n function: '函数',\n cell: '单元格',\n range: '范围'\n };\n return titles[type] || type;\n }\n\n /**\n * 获取类型标签\n */\n private getTypeLabel(type: string): string {\n const labels: Record<string, string> = {\n function: 'fx',\n cell: 'A1',\n range: 'A:B'\n };\n return labels[type] || type;\n }\n\n /**\n * 定位下拉框\n */\n private positionDropdown(): void {\n if (this.customPositioning) {\n this.customPositioning();\n return;\n }\n if (!this.dropdown || !this.inputElement) {\n return;\n }\n\n const inputRect = this.inputElement.getBoundingClientRect();\n const containerRect = this.container.getBoundingClientRect();\n\n this.dropdown.style.position = 'absolute';\n this.dropdown.style.left = `${inputRect.left - containerRect.left}px`;\n this.dropdown.style.top = `${inputRect.bottom - containerRect.top + 2}px`;\n this.dropdown.style.width = `${Math.max(300, inputRect.width)}px`;\n }\n\n /**\n * 处理键盘事件\n */\n private handleKeydown(event: KeyboardEvent): void {\n if (!this.isVisible) {\n return;\n }\n\n switch (event.key) {\n case 'ArrowDown':\n event.preventDefault();\n this.selectedIndex = Math.min(this.selectedIndex + 1, this.items.length - 1);\n this.renderDropdown();\n break;\n case 'ArrowUp':\n event.preventDefault();\n this.selectedIndex = Math.max(this.selectedIndex - 1, 0);\n this.renderDropdown();\n break;\n case 'Enter':\n if (this.selectedIndex >= 0) {\n event.preventDefault();\n this.selectItem(this.selectedIndex);\n }\n break;\n case 'Escape':\n this.hide();\n break;\n case 'Tab':\n if (this.selectedIndex >= 0) {\n event.preventDefault();\n this.selectItem(this.selectedIndex);\n }\n break;\n }\n }\n\n /**\n * 选择项目\n */\n private selectItem(index: number): void {\n const item = this.items[index];\n if (!item || !this.inputElement) {\n return;\n }\n\n if (this.onSelectCallback) {\n this.onSelectCallback(item);\n } else {\n // 默认插入行为\n const value = this.inputElement.value;\n const cursorPos = this.inputElement.selectionStart || 0;\n const context = this.parseInputContext(value, cursorPos);\n\n const newValue =\n value.substring(0, context.insertPosition) +\n item.value +\n (item.type === 'function' ? '(' : '') +\n value.substring(cursorPos);\n\n this.inputElement.value = newValue;\n\n // 设置光标位置\n const newCursorPos = context.insertPosition + item.value.length + (item.type === 'function' ? 1 : 0);\n this.inputElement.setSelectionRange(newCursorPos, newCursorPos);\n }\n\n this.hide();\n }\n\n /**\n * 显示下拉框\n */\n private show(): void {\n if (this.dropdown && this.items.length > 0) {\n this.dropdown.style.display = 'block';\n this.isVisible = true;\n this.selectedIndex = 0;\n this.renderDropdown();\n }\n }\n\n /**\n * 隐藏下拉框\n */\n private hide(): void {\n if (this.dropdown) {\n this.dropdown.style.display = 'none';\n this.isVisible = false;\n this.selectedIndex = -1;\n }\n }\n\n /**\n * 销毁组件\n */\n release(): void {\n if (this.dropdown && this.dropdown.parentNode) {\n this.dropdown.parentNode.removeChild(this.dropdown);\n }\n }\n}\n"]}
@@ -0,0 +1,21 @@
1
+ import * as VTable_editors from '@visactor/vtable-editors';
2
+ import type VTableSheet from '../components/vtable-sheet';
3
+ import type { EditContext } from '@visactor/vtable-editors';
4
+ export declare class FormulaInputEditor extends VTable_editors.InputEditor {
5
+ private formulaAutocomplete;
6
+ private sheet;
7
+ private eventHandlers;
8
+ setSheet(sheet: VTableSheet): void;
9
+ getInputElement(): HTMLElement;
10
+ targetIsOnEditor(target: HTMLElement): boolean;
11
+ createElement(): void;
12
+ private handleFormulaInput;
13
+ private initAutocomplete;
14
+ onStart(context: EditContext<string>): void;
15
+ beforeEnd(): void;
16
+ onEnd(): void;
17
+ getValue(): string;
18
+ validateValue(newValue?: any, oldValue?: any, position?: VTable_editors.CellAddress, table?: any, isClickOnTable?: boolean): boolean | VTable_editors.ValidateEnum;
19
+ private getCellRangeString;
20
+ }
21
+ export declare const formulaEditor: FormulaInputEditor;
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+
3
+ var __createBinding = this && this.__createBinding || (Object.create ? function(o, m, k, k2) {
4
+ void 0 === k2 && (k2 = k);
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ desc && !("get" in desc ? !m.__esModule : desc.writable || desc.configurable) || (desc = {
7
+ enumerable: !0,
8
+ get: function() {
9
+ return m[k];
10
+ }
11
+ }), Object.defineProperty(o, k2, desc);
12
+ } : function(o, m, k, k2) {
13
+ void 0 === k2 && (k2 = k), o[k2] = m[k];
14
+ }), __setModuleDefault = this && this.__setModuleDefault || (Object.create ? function(o, v) {
15
+ Object.defineProperty(o, "default", {
16
+ enumerable: !0,
17
+ value: v
18
+ });
19
+ } : function(o, v) {
20
+ o.default = v;
21
+ }), __importStar = this && this.__importStar || function(mod) {
22
+ if (mod && mod.__esModule) return mod;
23
+ var result = {};
24
+ if (null != mod) for (var k in mod) "default" !== k && Object.prototype.hasOwnProperty.call(mod, k) && __createBinding(result, mod, k);
25
+ return __setModuleDefault(result, mod), result;
26
+ };
27
+
28
+ Object.defineProperty(exports, "__esModule", {
29
+ value: !0
30
+ }), exports.formulaEditor = exports.FormulaInputEditor = void 0;
31
+
32
+ const VTable_editors = __importStar(require("@visactor/vtable-editors")), formula_autocomplete_1 = require("./formula-autocomplete"), formula_helper_1 = require("./formula-helper");
33
+
34
+ class FormulaInputEditor extends VTable_editors.InputEditor {
35
+ constructor() {
36
+ super(...arguments), this.formulaAutocomplete = null, this.sheet = null, this.eventHandlers = [];
37
+ }
38
+ setSheet(sheet) {
39
+ this.sheet = sheet;
40
+ }
41
+ getInputElement() {
42
+ return this.element;
43
+ }
44
+ targetIsOnEditor(target) {
45
+ return target === this.element || target === this.sheet.formulaUIManager.formulaInput;
46
+ }
47
+ createElement() {
48
+ if (super.createElement(), this.element && this.sheet) {
49
+ [ "click", "mouseup", "keyup", "select", "input", "focus" ].forEach((eventType => {
50
+ const handler = e => {
51
+ const cursorPos = this.element.selectionStart;
52
+ null != cursorPos && (this.sheet.formulaManager.lastKnownCursorPosInFormulaInput = cursorPos,
53
+ this.sheet.formulaManager.inputingElement = this.element);
54
+ };
55
+ this.element.addEventListener(eventType, handler), this.eventHandlers.push({
56
+ type: eventType,
57
+ handler: handler
58
+ });
59
+ }));
60
+ const inputHandler = e => {
61
+ this.handleFormulaInput(e);
62
+ };
63
+ this.element.addEventListener("input", inputHandler), this.eventHandlers.push({
64
+ type: "input",
65
+ handler: inputHandler
66
+ }), setTimeout((() => {
67
+ this.initAutocomplete();
68
+ }), 50);
69
+ }
70
+ }
71
+ handleFormulaInput(event) {
72
+ if (!this.sheet || !this.element) return;
73
+ const value = this.element.value;
74
+ this.sheet.formulaUIManager.formulaInput.value = value;
75
+ const highlightManager = this.sheet.formulaManager.cellHighlightManager;
76
+ highlightManager && (value.startsWith("=") ? highlightManager.highlightFormulaCells(value) : highlightManager.clearHighlights());
77
+ }
78
+ initAutocomplete() {
79
+ if (!this.sheet || !this.element) return;
80
+ this.formulaAutocomplete && (this.formulaAutocomplete.release(), this.formulaAutocomplete = null);
81
+ const tableContainer = this.sheet.getContentElement();
82
+ this.formulaAutocomplete = new formula_autocomplete_1.FormulaAutocomplete(tableContainer, this.sheet);
83
+ this.formulaAutocomplete.attachTo(this.element, void 0, (() => {
84
+ if (!this.formulaAutocomplete || !this.element) return;
85
+ const inputRect = this.element.getBoundingClientRect(), containerRect = tableContainer.getBoundingClientRect(), windowHeight = window.innerHeight, relativeLeft = inputRect.left - containerRect.left, idealWidth = Math.max(300, inputRect.width);
86
+ let relativeTop = inputRect.bottom - containerRect.top + 2;
87
+ const spaceBelow = windowHeight - inputRect.bottom, spaceAbove = inputRect.top;
88
+ spaceBelow < 250 && spaceAbove > spaceBelow && (relativeTop = inputRect.top - containerRect.top - 250 - 2);
89
+ const bottomOverflow = relativeTop + 250 - containerRect.height;
90
+ bottomOverflow > 0 && (relativeTop = Math.max(0, relativeTop - bottomOverflow)),
91
+ relativeTop < 0 && (relativeTop = 0);
92
+ const dropdown = this.formulaAutocomplete.dropdown;
93
+ dropdown && (dropdown.style.position = "absolute", dropdown.style.left = `${Math.max(0, relativeLeft)}px`,
94
+ dropdown.style.top = `${relativeTop}px`, dropdown.style.width = `${idealWidth}px`,
95
+ dropdown.style.maxHeight = "250px", dropdown.style.overflowY = "auto", dropdown.style.zIndex = "10000",
96
+ dropdown.style.boxShadow = "0 2px 8px rgba(0, 0, 0, 0.15)");
97
+ }));
98
+ }
99
+ onStart(context) {
100
+ var _a, _b;
101
+ const formula = this.sheet.formulaManager.getCellFormula({
102
+ sheet: (null === (_a = this.sheet.getActiveSheet()) || void 0 === _a ? void 0 : _a.getKey()) || "",
103
+ row: context.row,
104
+ col: context.col
105
+ });
106
+ if (formula && (context.value = formula), super.onStart(context), this.sheet.formulaManager.inputingElement = this.element,
107
+ this.sheet && "string" == typeof context.value) {
108
+ const formula = this.sheet.formulaManager.getCellFormula({
109
+ sheet: (null === (_b = this.sheet.getActiveSheet()) || void 0 === _b ? void 0 : _b.getKey()) || "",
110
+ row: context.row,
111
+ col: context.col
112
+ });
113
+ if (formula) {
114
+ this.setValue(formula);
115
+ const highlightManager = this.sheet.formulaManager.cellHighlightManager;
116
+ highlightManager && highlightManager.highlightFormulaCells(formula);
117
+ }
118
+ }
119
+ }
120
+ beforeEnd() {
121
+ this.sheet.formulaManager.formulaWorkingOnCell = null;
122
+ }
123
+ onEnd() {
124
+ if (this.formulaAutocomplete && (this.formulaAutocomplete.release(), this.formulaAutocomplete = null),
125
+ this.sheet) {
126
+ const highlightManager = this.sheet.formulaManager.cellHighlightManager;
127
+ highlightManager && highlightManager.clearHighlights();
128
+ }
129
+ this.eventHandlers.forEach((({type: type, handler: handler}) => {
130
+ this.element.removeEventListener(type, handler);
131
+ })), super.onEnd();
132
+ }
133
+ getValue() {
134
+ var _a;
135
+ return (null === (_a = this.element) || void 0 === _a ? void 0 : _a.value) || "";
136
+ }
137
+ validateValue(newValue, oldValue, position, table, isClickOnTable) {
138
+ var _a;
139
+ if (isClickOnTable && newValue && newValue.startsWith("=")) {
140
+ if (this.sheet) {
141
+ const formulaInput = this.element;
142
+ return this.sheet.formulaManager.inputIsParamMode = (0, formula_helper_1.detectFunctionParameterPosition)(formulaInput.value, this.sheet.formulaManager.lastKnownCursorPosInFormulaInput),
143
+ this.sheet.formulaManager.inputIsParamMode.inParamMode ? (this.sheet.formulaManager.formulaWorkingOnCell = null === (_a = this.sheet.getActiveSheet()) || void 0 === _a ? void 0 : _a.editingCell,
144
+ VTable_editors.ValidateEnum.validateNotExit) : VTable_editors.ValidateEnum.validateExit;
145
+ }
146
+ if ((newValue.match(/\(/g) || []).length !== (newValue.match(/\)/g) || []).length) return VTable_editors.ValidateEnum.validateNotExit;
147
+ }
148
+ return !0;
149
+ }
150
+ getCellRangeString(range) {
151
+ if (!range || !range.start || !range.end) return "";
152
+ const colToLetter = col => {
153
+ let letter = "";
154
+ for (;col >= 0; ) letter = String.fromCharCode(65 + col % 26) + letter, col = Math.floor(col / 26) - 1;
155
+ return letter;
156
+ }, startCol = colToLetter(range.start.col), startRow = range.start.row + 1, endCol = colToLetter(range.end.col), endRow = range.end.row + 1;
157
+ return startCol === endCol && startRow === endRow ? `${startCol}${startRow}` : `${startCol}${startRow}:${endCol}${endRow}`;
158
+ }
159
+ }
160
+
161
+ exports.FormulaInputEditor = FormulaInputEditor, exports.formulaEditor = new FormulaInputEditor;
162
+ //# sourceMappingURL=formula-editor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/formula/formula-editor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yEAA2D;AAC3D,iEAA6D;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GjC,IAAI,QAAQ,KAAK,MAAM,IAAI,QAAQ,KAAK,MAAM,EAAE;YAC9C,OAAO,GAAG,QAAQ,GAAG,QAAQ,EAAE,CAAC;SACjC;QAGD,OAAO,GAAG,QAAQ,GAAG,QAAQ,IAAI,MAAM,GAAG,MAAM,EAAE,CAAC;IACrD,CAAC;CACF;AAjTD,gDAiTC;AAEY,QAAA,aAAa,GAAG,IAAI,kBAAkB,EAAE,CAAC","file":"formula-editor.js","sourcesContent":["import * as VTable_editors from '@visactor/vtable-editors';\nimport { FormulaAutocomplete } from './formula-autocomplete';\nimport type VTableSheet from '../components/vtable-sheet';\nimport type { EditContext } from '@visactor/vtable-editors';\nimport { detectFunctionParameterPosition } from './formula-helper';\n\nexport class FormulaInputEditor extends VTable_editors.InputEditor {\n private formulaAutocomplete: FormulaAutocomplete | null = null;\n private sheet: VTableSheet | null = null;\n // 定义存储事件处理函数的数组\n private eventHandlers: Array<{ type: string; handler: EventListener }> = [];\n /**\n * 设置 Sheet 实例\n */\n setSheet(sheet: VTableSheet): void {\n this.sheet = sheet;\n }\n getInputElement(): HTMLElement {\n return this.element;\n }\n targetIsOnEditor(target: HTMLElement): boolean {\n return target === this.element || target === this.sheet.formulaUIManager.formulaInput;\n }\n /**\n * 创建编辑器元素\n * 重写父类方法以添加自动补全功能\n */\n createElement(): void {\n super.createElement();\n\n if (this.element && this.sheet) {\n const events = ['click', 'mouseup', 'keyup', 'select', 'input', 'focus'];\n events.forEach(eventType => {\n const handler = (e: Event) => {\n const cursorPos = this.element.selectionStart;\n if (cursorPos !== null && cursorPos !== undefined) {\n console.log(\n 'event',\n e,\n 'old',\n this.sheet.formulaManager.lastKnownCursorPosInFormulaInput,\n 'new',\n cursorPos\n );\n this.sheet.formulaManager.lastKnownCursorPosInFormulaInput = cursorPos;\n this.sheet.formulaManager.inputingElement = this.element;\n }\n };\n\n this.element.addEventListener(eventType, handler);\n this.eventHandlers.push({ type: eventType, handler });\n });\n\n // 添加输入事件监听\n const inputHandler = (e: Event) => {\n this.handleFormulaInput(e);\n };\n this.element.addEventListener('input', inputHandler);\n this.eventHandlers.push({ type: 'input', handler: inputHandler });\n\n // 延迟初始化自动补全\n setTimeout(() => {\n this.initAutocomplete();\n }, 50);\n }\n }\n\n /**\n * 处理公式输入\n */\n private handleFormulaInput(event: Event): void {\n if (!this.sheet || !this.element) {\n return;\n }\n\n const value = this.element.value;\n console.log('handleFormulaInput', value);\n // 同步内容到顶部输入栏\n this.sheet.formulaUIManager.formulaInput.value = value;\n // const inputEvent = new Event('input', { bubbles: true });\n // Object.defineProperty(inputEvent, 'isFormulaInsertion', { value: true });\n // this.sheet.formulaUIManager.formulaInput.dispatchEvent(inputEvent);\n // 获取高亮管理器\n const highlightManager = this.sheet.formulaManager.cellHighlightManager;\n\n if (highlightManager) {\n if (value.startsWith('=')) {\n highlightManager.highlightFormulaCells(value);\n } else {\n highlightManager.clearHighlights();\n }\n }\n }\n\n /**\n * 初始化自动补全\n */\n private initAutocomplete(): void {\n if (!this.sheet || !this.element) {\n return;\n }\n\n // 清理之前的实例\n if (this.formulaAutocomplete) {\n this.formulaAutocomplete.release();\n this.formulaAutocomplete = null;\n }\n\n const tableContainer = this.sheet.getContentElement();\n\n this.formulaAutocomplete = new FormulaAutocomplete(tableContainer, this.sheet);\n\n // 自定义定位逻辑\n const customPositioning = () => {\n if (!this.formulaAutocomplete || !this.element) {\n return;\n }\n\n // 获取编辑器元素和容器的位置信息\n const inputRect = this.element.getBoundingClientRect();\n const containerRect = tableContainer.getBoundingClientRect();\n const windowHeight = window.innerHeight;\n\n // 计算相对于表格容器的位置\n const relativeLeft = inputRect.left - containerRect.left;\n\n // 计算下拉框的最大高度和理想宽度\n const maxDropdownHeight = 250; // 最大高度限制\n const idealWidth = Math.max(300, inputRect.width); // 理想宽度\n\n // 计算下拉框在下方显示时的顶部位置\n let relativeTop = inputRect.bottom - containerRect.top + 2;\n\n // 检查是否有足够空间在下方显示下拉框\n const spaceBelow = windowHeight - inputRect.bottom;\n const spaceAbove = inputRect.top;\n\n // 判断应该将下拉框放在输入框的上方还是下方\n if (spaceBelow < maxDropdownHeight && spaceAbove > spaceBelow) {\n // 空间不足且上方空间更大,则放在上方\n relativeTop = inputRect.top - containerRect.top - maxDropdownHeight - 2;\n }\n\n // 确保下拉框不会超出容器底部\n const bottomOverflow = relativeTop + maxDropdownHeight - containerRect.height;\n if (bottomOverflow > 0) {\n relativeTop = Math.max(0, relativeTop - bottomOverflow);\n }\n\n // 确保下拉框不会超出容器顶部\n if (relativeTop < 0) {\n relativeTop = 0;\n }\n\n // 设置下拉框位置和样式\n const dropdown = (this.formulaAutocomplete as any).dropdown;\n if (dropdown) {\n dropdown.style.position = 'absolute';\n dropdown.style.left = `${Math.max(0, relativeLeft)}px`;\n dropdown.style.top = `${relativeTop}px`;\n dropdown.style.width = `${idealWidth}px`;\n dropdown.style.maxHeight = `${maxDropdownHeight}px`;\n dropdown.style.overflowY = 'auto';\n dropdown.style.zIndex = '10000';\n dropdown.style.boxShadow = '0 2px 8px rgba(0, 0, 0, 0.15)';\n }\n };\n\n // 附加自动补全到输入框\n this.formulaAutocomplete.attachTo(this.element, undefined, customPositioning);\n }\n\n /**\n * 开始编辑\n * 重写父类方法\n */\n onStart(context: EditContext<string>): void {\n // 获取公式\n const formula = this.sheet.formulaManager.getCellFormula({\n sheet: this.sheet.getActiveSheet()?.getKey() || '',\n row: context.row,\n col: context.col\n });\n if (formula) {\n context.value = formula;\n }\n super.onStart(context);\n this.sheet.formulaManager.inputingElement = this.element;\n // 如果是公式,显示公式而不是计算结果\n if (this.sheet && typeof context.value === 'string') {\n const formula = this.sheet.formulaManager.getCellFormula({\n sheet: this.sheet.getActiveSheet()?.getKey() || '',\n row: context.row,\n col: context.col\n });\n\n if (formula) {\n this.setValue(formula);\n // 触发高亮\n const highlightManager = this.sheet.formulaManager.cellHighlightManager;\n if (highlightManager) {\n highlightManager.highlightFormulaCells(formula);\n }\n }\n }\n }\n beforeEnd(): void {\n this.sheet.formulaManager.formulaWorkingOnCell = null;\n }\n /**\n * 结束编辑\n * 重写父类方法以清理自动补全\n */\n onEnd(): void {\n if (this.formulaAutocomplete) {\n this.formulaAutocomplete.release();\n this.formulaAutocomplete = null;\n }\n if (this.sheet) {\n const highlightManager = this.sheet.formulaManager.cellHighlightManager;\n if (highlightManager) {\n highlightManager.clearHighlights();\n }\n }\n //解绑所有事件\n // 解绑事件(在需要解绑的地方)\n this.eventHandlers.forEach(({ type, handler }) => {\n this.element.removeEventListener(type, handler);\n });\n super.onEnd();\n }\n\n /**\n * 获取编辑器的值\n */\n getValue(): string {\n return this.element?.value || '';\n }\n\n validateValue(\n newValue?: any,\n oldValue?: any,\n position?: VTable_editors.CellAddress,\n table?: any,\n isClickOnTable?: boolean\n ): boolean | VTable_editors.ValidateEnum {\n // 判断点击到表格其他单元格,且输入了公式,则检查公式是否完整\n if (isClickOnTable && newValue && newValue.startsWith('=')) {\n // 如果有sheet实例,使用FormulaManager检查公式完整性\n if (this.sheet) {\n const formulaInput = this.element;\n this.sheet.formulaManager.inputIsParamMode = detectFunctionParameterPosition(\n formulaInput.value,\n this.sheet.formulaManager.lastKnownCursorPosInFormulaInput\n );\n console.log('test formula complete', newValue, this.sheet.formulaManager.inputIsParamMode);\n if (this.sheet.formulaManager.inputIsParamMode.inParamMode) {\n // // 如果公式不完整,不退出编辑状态 TODO 这里不应该只判断完整性,如这种情况下按住ctrl连续点选 =SUM(H5) 这种情况应该允许继续编辑输入点选单元格范围\n // if (!formulaManager.isFormulaComplete(newValue)) {\n\n this.sheet.formulaManager.formulaWorkingOnCell = this.sheet.getActiveSheet()?.editingCell;\n\n return VTable_editors.ValidateEnum.validateNotExit;\n }\n return VTable_editors.ValidateEnum.validateExit;\n }\n // 如果没有sheet实例,使用简单的括号匹配检查\n const openParenCount = (newValue.match(/\\(/g) || []).length;\n const closeParenCount = (newValue.match(/\\)/g) || []).length;\n if (openParenCount !== closeParenCount) {\n return VTable_editors.ValidateEnum.validateNotExit;\n }\n }\n return true;\n }\n\n /**\n * 将选中的单元格范围转换为公式引用字符串\n * 例如:A1:B3\n */\n private getCellRangeString(range: {\n start: { col: number; row: number };\n end: { col: number; row: number };\n }): string {\n if (!range || !range.start || !range.end) {\n return '';\n }\n\n // 将列索引转换为字母表示(0->A, 1->B, 等)\n const colToLetter = (col: number): string => {\n let letter = '';\n while (col >= 0) {\n letter = String.fromCharCode(65 + (col % 26)) + letter;\n col = Math.floor(col / 26) - 1;\n }\n return letter;\n };\n\n const startCol = colToLetter(range.start.col);\n const startRow = range.start.row + 1; // 行索引从1开始\n const endCol = colToLetter(range.end.col);\n const endRow = range.end.row + 1;\n\n // 如果是单个单元格\n if (startCol === endCol && startRow === endRow) {\n return `${startCol}${startRow}`;\n }\n\n // 如果是范围\n return `${startCol}${startRow}:${endCol}${endRow}`;\n }\n}\n\nexport const formulaEditor = new FormulaInputEditor();\n"]}
@@ -0,0 +1,8 @@
1
+ export declare const supportedFunctions: string[];
2
+ export declare function detectFunctionParameterPosition(formula: string, cursorPosition: number): {
3
+ inParamMode: boolean;
4
+ functionParamPosition: {
5
+ start: number;
6
+ end: number;
7
+ } | null;
8
+ };
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+
3
+ function findMatchingCloseParen(formula, openParenIndex) {
4
+ let depth = 1;
5
+ for (let i = openParenIndex + 1; i < formula.length; i++) if ("(" === formula[i]) depth++; else if (")" === formula[i] && (depth--,
6
+ 0 === depth)) return i;
7
+ return -1;
8
+ }
9
+
10
+ function detectFunctionParameterPosition(formula, cursorPosition) {
11
+ if (!formula.startsWith("=")) return {
12
+ inParamMode: !1,
13
+ functionParamPosition: null
14
+ };
15
+ const functionRegex = /([A-Za-z]+)\s*\(/g;
16
+ let match, positionBeforeCursor = -1;
17
+ for (;null !== (match = functionRegex.exec(formula)); ) {
18
+ match[1].toUpperCase();
19
+ const functionStart = match.index, openParenIndex = formula.indexOf("(", functionStart);
20
+ if (openParenIndex < cursorPosition && openParenIndex > positionBeforeCursor) {
21
+ const closeParenIndex = findMatchingCloseParen(formula, openParenIndex);
22
+ if (cursorPosition > openParenIndex && (-1 === closeParenIndex || cursorPosition <= closeParenIndex)) return {
23
+ inParamMode: !0,
24
+ functionParamPosition: {
25
+ start: openParenIndex + 1,
26
+ end: cursorPosition
27
+ }
28
+ };
29
+ if (cursorPosition === openParenIndex + 1) return {
30
+ inParamMode: !0,
31
+ functionParamPosition: {
32
+ start: openParenIndex + 1,
33
+ end: cursorPosition
34
+ }
35
+ };
36
+ positionBeforeCursor = openParenIndex;
37
+ }
38
+ }
39
+ if (cursorPosition > 1 && cursorPosition <= formula.length) {
40
+ const prevChar = formula[cursorPosition - 1];
41
+ if ([ "+", "-", "*", "/", "=", ">", "<", "&", "|", "^", "(", "," ].includes(prevChar)) return {
42
+ inParamMode: !0,
43
+ functionParamPosition: {
44
+ start: cursorPosition,
45
+ end: cursorPosition
46
+ }
47
+ };
48
+ }
49
+ return 1 === cursorPosition && "=" === formula[0] ? {
50
+ inParamMode: !0,
51
+ functionParamPosition: {
52
+ start: cursorPosition,
53
+ end: cursorPosition
54
+ }
55
+ } : {
56
+ inParamMode: !1,
57
+ functionParamPosition: null
58
+ };
59
+ }
60
+
61
+ Object.defineProperty(exports, "__esModule", {
62
+ value: !0
63
+ }), exports.detectFunctionParameterPosition = exports.supportedFunctions = void 0,
64
+ exports.supportedFunctions = [ "SUM", "AVERAGE", "MAX", "MIN", "COUNT", "COUNTA", "IF", "AND", "OR", "NOT", "ROUND", "FLOOR", "CEILING", "ABS", "SQRT", "POWER", "MOD", "INT", "RAND", "TODAY", "NOW", "YEAR", "MONTH", "DAY", "HOUR", "MINUTE", "SECOND" ],
65
+ exports.detectFunctionParameterPosition = detectFunctionParameterPosition;
66
+ //# sourceMappingURL=formula-helper.js.map