@veltra/sheet 4.1.1 → 4.1.3

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 (34) hide show
  1. package/dist/components/sheet/formula-bar.js +95 -11
  2. package/dist/components/sheet/sheet.js +5 -3
  3. package/dist/components/sheet/use-formula-ref-select.js +11 -3
  4. package/dist/components/sheet/use-sheet-grid.js +11 -3
  5. package/dist/components/sheet/use-sheet-state.js +1 -1
  6. package/dist/index.js +1 -2
  7. package/dist/tools/builtin.js +307 -305
  8. package/dist/tools/registry.d.ts +3 -1
  9. package/dist/tools/registry.js +7 -2
  10. package/package.json +4 -8
  11. package/src/components/sheet/__test__/canvas-mock.ts +3 -1
  12. package/src/components/sheet/__test__/find-shortcut-anchor.test.ts +2 -0
  13. package/src/components/sheet/__test__/formula-bar.test.ts +205 -0
  14. package/src/components/sheet/__test__/formula-capability.test.ts +2 -0
  15. package/src/components/sheet/__test__/functions-popup.test.ts +2 -0
  16. package/src/components/sheet/__test__/hyperlink-popup.test.ts +2 -0
  17. package/src/components/sheet/__test__/import-file.test.ts +2 -0
  18. package/src/components/sheet/__test__/insert-count-menu-item.test.ts +2 -0
  19. package/src/components/sheet/__test__/insert-image-popup.test.ts +2 -0
  20. package/src/components/sheet/__test__/insert-image.test.ts +2 -0
  21. package/src/components/sheet/__test__/sheet-component.test.ts +4 -1
  22. package/src/components/sheet/__test__/sheet-grow-on-scroll.test.ts +2 -0
  23. package/src/components/sheet/__test__/sheet-scrollbar.test.ts +2 -0
  24. package/src/components/sheet/__test__/sheet-theme.test.ts +2 -0
  25. package/src/components/sheet/__test__/use-formula-ref-select.test.ts +12 -1
  26. package/src/components/sheet/formula-bar.vue +106 -14
  27. package/src/components/sheet/sheet.vue +28 -26
  28. package/src/components/sheet/use-formula-ref-select.ts +13 -0
  29. package/src/components/sheet/use-sheet-grid.ts +26 -7
  30. package/src/index.ts +1 -3
  31. package/src/tools/__test__/builtin.test.ts +2 -2
  32. package/src/tools/builtin.ts +345 -344
  33. package/src/tools/registry.ts +8 -2
  34. package/src/tools/index.ts +0 -3
@@ -1,11 +1,12 @@
1
1
  import _sfc_main$1 from "./formula-suggest-list.js";
2
2
  import { applySuggest, filterFormulaSuggestions, getSuggestContext, moveSuggestIndex } from "./use-formula-suggest.js";
3
3
  import _sfc_main$2 from "./popups/functions-popup.js";
4
- import { insertRefText, isRefSelectContext } from "./use-formula-ref-select.js";
4
+ import { REF_HIGHLIGHT_COLORS, insertRefText, isRefSelectContext, replaceRefText } from "./use-formula-ref-select.js";
5
5
  import { formatAddress, formatRange, parseRange } from "infinitable/sheet";
6
6
  import { computed, createCommentVNode, createElementBlock, createElementVNode, createVNode, defineComponent, nextTick, normalizeClass, onBeforeUnmount, onMounted, openBlock, ref, shallowRef, unref, useTemplateRef, vModelText, watch, withCtx, withDirectives, withModifiers } from "vue";
7
7
  import { UDropdown, message } from "@veltra/desktop";
8
8
  import { bem } from "@veltra/utils";
9
+ import { scanFormulaReferences } from "infinitable";
9
10
  //#region src/components/sheet/formula-bar.vue
10
11
  const _hoisted_1 = ["disabled"];
11
12
  const _hoisted_2 = ["disabled", "readonly"];
@@ -15,7 +16,8 @@ const _sfc_main = /*@__PURE__*/ defineComponent({
15
16
  props: {
16
17
  sheet: {},
17
18
  context: {},
18
- setSelectionAnchor: { type: Function }
19
+ setSelectionAnchor: { type: Function },
20
+ setRefHighlights: { type: Function }
19
21
  },
20
22
  setup(__props, { expose: __expose }) {
21
23
  /**
@@ -60,28 +62,102 @@ const _sfc_main = /*@__PURE__*/ defineComponent({
60
62
  function isRefSelecting() {
61
63
  return editing.value && !mirroring.value && isRefSelectContext(fxDraft.value, fxCursor.value);
62
64
  }
63
- /** 网格 pointerdown(capture)→ 挂起即将到来的 blur 提交 */
65
+ /** 上次引用插入的文本区间(fxDraft 内 [start, end)):插入引用后光标离开触发位、
66
+ * isRefSelectContext 判定失效,拾取会话延续依赖它维持 blur 抑制与替换/扩展语义 */
67
+ let lastRefInsert = null;
68
+ function clearRefInsert() {
69
+ lastRefInsert = null;
70
+ }
71
+ /** 引用拾取延续中(上次插入的引用可被后续点选替换/扩展,点击画布不退出编辑) */
72
+ function hasRefInsert() {
73
+ return lastRefInsert !== null;
74
+ }
75
+ /** 网格 pointerdown(capture)→ 挂起即将到来的 blur 提交;拾取会话延续中
76
+ * (上次插入的引用未被打断)同样挂起——任意点击都消费为引用替换/扩展 */
64
77
  function beginBlurSuppress() {
65
- if (!isRefSelecting()) return;
78
+ if (!isRefSelecting() && !lastRefInsert) return;
66
79
  suppressBlurCommit = true;
67
80
  }
68
- /** 选区拦截回调:插入 A1 / A1:B2 到光标处(仅引用选择模式;弹框打开但非引用上下文时不插入) */
81
+ /** 选区拦截回调:插入 A1 / A1:B2 到光标处(仅引用选择模式;弹框打开但非引用上下文时不插入)。
82
+ * 拾取会话延续(lastRefInsert 存在):替换上次插入的引用——shift 扩展回交(Excel 锚点
83
+ * 恒定连续扩展)与普通点选(Point mode 换目标)同走此路径,编辑不中断 */
69
84
  function handleRefSelect(range) {
70
- if (!isRefSelecting()) return;
85
+ if (!isRefSelecting() && !lastRefInsert) return;
86
+ if (editAddr && range.start.row === range.end.row && range.start.col === range.end.col && range.start.row === editAddr.row && range.start.col === editAddr.col) return;
71
87
  const rangeText = formatRange(range);
72
- const selEnd = fxRef.value?.selectionEnd ?? fxCursor.value;
73
- const result = insertRefText(fxDraft.value, fxCursor.value, rangeText, selEnd);
74
- fxDraft.value = result.text;
75
- fxCursor.value = result.cursor;
88
+ if (lastRefInsert) {
89
+ const { start } = lastRefInsert;
90
+ const result = replaceRefText(fxDraft.value, start, lastRefInsert.end, rangeText);
91
+ fxDraft.value = result.text;
92
+ fxCursor.value = result.cursor;
93
+ lastRefInsert = {
94
+ start,
95
+ end: result.cursor
96
+ };
97
+ } else {
98
+ const selEnd = fxRef.value?.selectionEnd ?? fxCursor.value;
99
+ const result = insertRefText(fxDraft.value, fxCursor.value, rangeText, selEnd);
100
+ fxDraft.value = result.text;
101
+ fxCursor.value = result.cursor;
102
+ lastRefInsert = {
103
+ start: result.cursor - rangeText.length,
104
+ end: result.cursor
105
+ };
106
+ }
76
107
  closeSuggest();
77
108
  nextTick(() => {
78
109
  const input = fxRef.value;
79
110
  if (!input) return;
80
111
  input.focus();
81
- input.setSelectionRange(result.cursor, result.cursor);
112
+ input.setSelectionRange(fxCursor.value, fxCursor.value);
82
113
  autosizeFx();
83
114
  });
84
115
  }
116
+ /** 公式文本扫描 → 画布染色框:编辑中且 `=` 开头时逐引用上框(颜色按出现顺序循环),
117
+ * 手输/点选/扩展一视同仁(对齐 Excel:编辑公式即染色全部引用);其余状态清除 */
118
+ function syncRefHighlights() {
119
+ if (!editing.value || mirroring.value || !fxDraft.value.startsWith("=")) {
120
+ props.setRefHighlights?.([]);
121
+ return;
122
+ }
123
+ props.setRefHighlights?.(scanFormulaReferences(fxDraft.value).map(refToHighlight));
124
+ }
125
+ /** 扫描结果 → 染色框条目(RangeRef 展开为区域,单格引用同形) */
126
+ function refToHighlight(scanned, index) {
127
+ const color = REF_HIGHLIGHT_COLORS[index % REF_HIGHLIGHT_COLORS.length];
128
+ const ref = scanned.ref;
129
+ if (scanned.isRange) return {
130
+ range: {
131
+ start: {
132
+ row: ref.startRow,
133
+ col: ref.startCol
134
+ },
135
+ end: {
136
+ row: ref.endRow,
137
+ col: ref.endCol
138
+ }
139
+ },
140
+ color
141
+ };
142
+ return {
143
+ range: {
144
+ start: {
145
+ row: ref.row,
146
+ col: ref.col
147
+ },
148
+ end: {
149
+ row: ref.row,
150
+ col: ref.col
151
+ }
152
+ },
153
+ color
154
+ };
155
+ }
156
+ watch([
157
+ fxDraft,
158
+ editing,
159
+ mirroring
160
+ ], syncRefHighlights);
85
161
  function nameText(state) {
86
162
  const range = state.ranges[0];
87
163
  if (range) {
@@ -164,6 +240,7 @@ const _sfc_main = /*@__PURE__*/ defineComponent({
164
240
  refreshSuggest();
165
241
  }
166
242
  function handleFxInput() {
243
+ clearRefInsert();
167
244
  syncFxCursor();
168
245
  autosizeFx();
169
246
  }
@@ -193,6 +270,7 @@ const _sfc_main = /*@__PURE__*/ defineComponent({
193
270
  const result = applySuggest(fxDraft.value, ctx.start, ctx.end, pick.name);
194
271
  fxDraft.value = result.text;
195
272
  fxCursor.value = result.cursor;
273
+ clearRefInsert();
196
274
  closeSuggest();
197
275
  nextTick(() => {
198
276
  const el = fxRef.value;
@@ -293,6 +371,7 @@ const _sfc_main = /*@__PURE__*/ defineComponent({
293
371
  editAddr = null;
294
372
  suppressBlurCommit = false;
295
373
  props.setSelectionAnchor?.(null);
374
+ clearRefInsert();
296
375
  closeSuggest();
297
376
  if (addr) {
298
377
  props.context.setCellValue(addr, fxDraft.value);
@@ -307,6 +386,7 @@ const _sfc_main = /*@__PURE__*/ defineComponent({
307
386
  editAddr = null;
308
387
  suppressBlurCommit = false;
309
388
  props.setSelectionAnchor?.(null);
389
+ clearRefInsert();
310
390
  closeSuggest();
311
391
  refreshFx();
312
392
  }
@@ -337,6 +417,7 @@ const _sfc_main = /*@__PURE__*/ defineComponent({
337
417
  mirroring.value = true;
338
418
  mirrorAddr = addr;
339
419
  fxDraft.value = cellText(addr);
420
+ clearRefInsert();
340
421
  closeSuggest();
341
422
  }
342
423
  function exitMirror(addr) {
@@ -398,6 +479,7 @@ const _sfc_main = /*@__PURE__*/ defineComponent({
398
479
  fxDraft.value = text;
399
480
  fxCursor.value = cursor;
400
481
  suppressBlurCommit = false;
482
+ clearRefInsert();
401
483
  props.setSelectionAnchor?.(addr);
402
484
  closeSuggest();
403
485
  nextTick(() => {
@@ -414,6 +496,7 @@ const _sfc_main = /*@__PURE__*/ defineComponent({
414
496
  mirrorGridEdit,
415
497
  exitMirror,
416
498
  isRefSelecting,
499
+ hasRefInsert,
417
500
  isFunctionPopupOpen: () => functionsOpen.value,
418
501
  beginBlurSuppress,
419
502
  handleRefSelect,
@@ -431,6 +514,7 @@ const _sfc_main = /*@__PURE__*/ defineComponent({
431
514
  mirroring.value = false;
432
515
  mirrorAddr = null;
433
516
  suppressBlurCommit = false;
517
+ clearRefInsert();
434
518
  functionsOpen.value = false;
435
519
  closeSuggest();
436
520
  bindSheet(sheet);
@@ -202,7 +202,7 @@ const _sfc_main = /*@__PURE__*/ defineComponent({
202
202
  }
203
203
  const gridRef = useTemplateRef("gridRef");
204
204
  const formulaBarRef = useTemplateRef("formulaBarRef");
205
- const { rebuildGrid, activateGrid, pruneCache, getGrid, setSelectionAnchor } = useSheetGrid({
205
+ const { rebuildGrid, activateGrid, pruneCache, getGrid, setSelectionAnchor, setRefHighlights } = useSheetGrid({
206
206
  props,
207
207
  gridRef,
208
208
  getActiveSheet: () => activeSheet.value,
@@ -301,11 +301,13 @@ const _sfc_main = /*@__PURE__*/ defineComponent({
301
301
  ref: formulaBarRef,
302
302
  sheet: unref(activeSheet),
303
303
  context: unref(context),
304
- "set-selection-anchor": unref(setSelectionAnchor)
304
+ "set-selection-anchor": unref(setSelectionAnchor),
305
+ "set-ref-highlights": unref(setRefHighlights)
305
306
  }, null, 8, [
306
307
  "sheet",
307
308
  "context",
308
- "set-selection-anchor"
309
+ "set-selection-anchor",
310
+ "set-ref-highlights"
309
311
  ])) : createCommentVNode("v-if", true),
310
312
  createElementVNode("div", {
311
313
  ref_key: "gridRef",
@@ -13,6 +13,12 @@ const REF_TRIGGER_CHARS = /* @__PURE__ */ new Set([
13
13
  "<",
14
14
  ">"
15
15
  ]);
16
+ /** 引用拾取染色框循环色板(对齐 Excel 蓝/绿/红循环惯例),按引用在公式中的出现顺序取色 */
17
+ const REF_HIGHLIGHT_COLORS = [
18
+ "#2563eb",
19
+ "#16a34a",
20
+ "#dc2626"
21
+ ];
16
22
  /**
17
23
  * 判定是否处于「可插入引用」位置(无状态:每次按文本 + 光标重算)。
18
24
  * `-` 的一元/二元歧义基础版不区分(`A1-` 后框选 → `A1-B2`,合法且误判无害)。
@@ -26,12 +32,14 @@ function isRefSelectContext(text, cursor) {
26
32
  }
27
33
  /** 在光标处插入引用文本(替换空选区;有选区时覆盖选区) */
28
34
  function insertRefText(text, cursor, rangeText, selectionEnd = cursor) {
29
- const start = Math.min(cursor, selectionEnd);
30
- const end = Math.max(cursor, selectionEnd);
35
+ return replaceRefText(text, Math.min(cursor, selectionEnd), Math.max(cursor, selectionEnd), rangeText);
36
+ }
37
+ /** 替换 [start, end) 区间为引用文本(shift+点选扩展:扩展上次插入的引用) */
38
+ function replaceRefText(text, start, end, rangeText) {
31
39
  return {
32
40
  text: text.slice(0, start) + rangeText + text.slice(end),
33
41
  cursor: start + rangeText.length
34
42
  };
35
43
  }
36
44
  //#endregion
37
- export { insertRefText, isRefSelectContext };
45
+ export { REF_HIGHLIGHT_COLORS, insertRefText, isRefSelectContext, replaceRefText };
@@ -124,7 +124,7 @@ function useSheetGrid(options) {
124
124
  onEditStart: (addr) => formulaBarRef.value?.mirrorGridEdit(addr),
125
125
  onEditEnd: (addr) => formulaBarRef.value?.exitMirror(addr),
126
126
  onColResizeEnd: options.onColResizeEnd,
127
- interceptSelection: () => (formulaBarRef.value?.isRefSelecting() ?? false) || (formulaBarRef.value?.isFunctionPopupOpen() ?? false) || (options.isFunctionPopupOpen?.() ?? false),
127
+ interceptSelection: () => (formulaBarRef.value?.isRefSelecting() ?? false) || (formulaBarRef.value?.hasRefInsert() ?? false) || (formulaBarRef.value?.isFunctionPopupOpen() ?? false) || (options.isFunctionPopupOpen?.() ?? false),
128
128
  onSelectionIntercept: (range) => formulaBarRef.value?.handleRefSelect(range)
129
129
  });
130
130
  let dirty = false;
@@ -212,7 +212,10 @@ function useSheetGrid(options) {
212
212
  function activateGrid() {
213
213
  const sheet = getActiveSheet();
214
214
  if (!gridRef.value) return;
215
- if (active && active.sheet !== sheet) active.grid.setSelectionAnchor(null);
215
+ if (active && active.sheet !== sheet) {
216
+ active.grid.setSelectionAnchor(null);
217
+ active.grid.setHighlightRanges([]);
218
+ }
216
219
  const cached = cache.get(sheet);
217
220
  if (cached && !cached.dirty) {
218
221
  cached.lastUsed = ++seq;
@@ -284,12 +287,17 @@ function useSheetGrid(options) {
284
287
  function setSelectionAnchor(addr) {
285
288
  active?.grid.setSelectionAnchor(addr);
286
289
  }
290
+ /** 公式引用染色框同步(公式栏 → 活动实例透传;空数组清除;无活动实例时静默) */
291
+ function setRefHighlights(items) {
292
+ active?.grid.setHighlightRanges(items);
293
+ }
287
294
  return {
288
295
  rebuildGrid,
289
296
  activateGrid,
290
297
  pruneCache,
291
298
  getGrid: () => active?.grid,
292
- setSelectionAnchor
299
+ setSelectionAnchor,
300
+ setRefHighlights
293
301
  };
294
302
  }
295
303
  //#endregion
@@ -1,5 +1,5 @@
1
- import { defaultToolRegistry } from "../../tools/registry.js";
2
1
  import { createSheetContext } from "../../tools/context.js";
2
+ import { defaultToolRegistry } from "../../tools/registry.js";
3
3
  import { resolveRenderSize } from "./sheet-context-menu.js";
4
4
  import { Workbook } from "infinitable/sheet";
5
5
  import { computed, onBeforeUnmount, onMounted, ref, shallowRef, watch } from "vue";
package/dist/index.js CHANGED
@@ -1,5 +1,4 @@
1
- import { defaultToolRegistry, registerTool, unregisterTool } from "./tools/registry.js";
2
- import "./tools/builtin.js";
3
1
  import { createSheetContext } from "./tools/context.js";
2
+ import { defaultToolRegistry, registerTool, unregisterTool } from "./tools/registry.js";
4
3
  import _sfc_main from "./components/sheet/sheet.js";
5
4
  export { _sfc_main as USheet, createSheetContext, defaultToolRegistry, registerTool, unregisterTool };