@veltra/sheet 3.0.0 → 4.1.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.
- package/dist/components/sheet/float-image-menu.js +59 -0
- package/dist/components/sheet/import-file.js +3 -1
- package/dist/components/sheet/popup-helpers.js +18 -2
- package/dist/components/sheet/popups/hyperlink-popup.js +127 -0
- package/dist/components/sheet/popups/import.worker.d.ts +4 -0
- package/dist/components/sheet/popups/import.worker.js +3 -1
- package/dist/components/sheet/sheet-context-menu.js +81 -7
- package/dist/components/sheet/sheet.js +112 -17
- package/dist/components/sheet/style2.css +62 -0
- package/dist/components/sheet/use-cell-link-tooltip.js +33 -0
- package/dist/components/sheet/use-sheet-grid.js +82 -2
- package/dist/components/sheet/use-sheet-state.js +1 -0
- package/dist/tools/context.d.ts +20 -2
- package/dist/tools/context.js +9 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/sheet.d.ts +54 -7
- package/package.json +2 -2
- package/src/components/sheet/__test__/float-image-menu.test.ts +130 -0
- package/src/components/sheet/__test__/grid-test-utils.ts +2 -3
- package/src/components/sheet/__test__/hyperlink-popup.test.ts +176 -0
- package/src/components/sheet/__test__/import-file.test.ts +33 -2
- package/src/components/sheet/__test__/sheet-component.test.ts +476 -18
- package/src/components/sheet/__test__/sheet-context-menu.test.ts +255 -2
- package/src/components/sheet/__test__/sheet-scrollbar.test.ts +23 -73
- package/src/components/sheet/__test__/sheet-theme.test.ts +212 -0
- package/src/components/sheet/float-image-menu.ts +78 -0
- package/src/components/sheet/import-file.ts +19 -2
- package/src/components/sheet/popup-helpers.ts +68 -2
- package/src/components/sheet/popups/hyperlink-popup.vue +112 -0
- package/src/components/sheet/popups/import.worker.ts +18 -4
- package/src/components/sheet/sheet-context-menu.ts +100 -7
- package/src/components/sheet/sheet.vue +120 -7
- package/src/components/sheet/style.scss +66 -0
- package/src/components/sheet/use-cell-link-tooltip.ts +42 -0
- package/src/components/sheet/use-sheet-grid.ts +111 -4
- package/src/components/sheet/use-sheet-state.ts +8 -2
- package/src/tools/__test__/context.test.ts +89 -0
- package/src/tools/context.ts +33 -2
- package/src/types/sheet.ts +59 -7
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { shallowRef } from "vue";
|
|
2
|
+
//#region src/components/sheet/use-cell-link-tooltip.ts
|
|
3
|
+
/**
|
|
4
|
+
* tooltip 相对悬停落点的偏移:右下错开,浮层不遮指针与命中的链接文本。
|
|
5
|
+
*/
|
|
6
|
+
const TOOLTIP_OFFSET_X = 8;
|
|
7
|
+
const TOOLTIP_OFFSET_Y = 20;
|
|
8
|
+
/**
|
|
9
|
+
* 单元格超链接悬停 tooltip(引擎 hover/leave 事件驱动):
|
|
10
|
+
* hover 时记录命中的层坐标 + URL(层坐标相对 grid 实例容器 = grid 区原点,
|
|
11
|
+
* 渲染层直接在 grid 区内绝对定位挂载浮层);leave 时卸载(置 null)。
|
|
12
|
+
* 事件源接线在 use-sheet-grid(与 cell-link-hover/leave 组件 emit 同源)。
|
|
13
|
+
*/
|
|
14
|
+
function useCellLinkTooltip() {
|
|
15
|
+
const tooltip = shallowRef(null);
|
|
16
|
+
function onCellLinkHover(event) {
|
|
17
|
+
tooltip.value = {
|
|
18
|
+
url: event.url,
|
|
19
|
+
left: event.x + TOOLTIP_OFFSET_X,
|
|
20
|
+
top: event.y + TOOLTIP_OFFSET_Y
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function onCellLinkLeave() {
|
|
24
|
+
tooltip.value = null;
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
tooltip,
|
|
28
|
+
onCellLinkHover,
|
|
29
|
+
onCellLinkLeave
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
export { useCellLinkTooltip };
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { popFloatImageMenu } from "./float-image-menu.js";
|
|
1
2
|
import { buildBodyMenus, buildColHeaderMenus, buildRowHeaderMenus, ensureContextMenuSelection, resolveRenderSize } from "./sheet-context-menu.js";
|
|
2
3
|
import { SheetGrid } from "infinitable/sheet";
|
|
3
4
|
import { onBeforeUnmount, onMounted, watch } from "vue";
|
|
4
5
|
import { contextmenu } from "@veltra/desktop";
|
|
6
|
+
import { currentTheme } from "@veltra/styles/theme";
|
|
5
7
|
//#region src/components/sheet/use-sheet-grid.ts
|
|
6
8
|
/**
|
|
7
9
|
* 缓存容量:最近 N 个 sheet 的 SheetGrid 实例常驻(tab 来回切换零重建)。
|
|
@@ -11,6 +13,38 @@ import { contextmenu } from "@veltra/desktop";
|
|
|
11
13
|
*/
|
|
12
14
|
const GRID_CACHE_CAPACITY = 3;
|
|
13
15
|
/**
|
|
16
|
+
* 画布主题映射:读 grid 宿主元素上生效的 `--u-*` CSS 变量(loadTheme 注入 html、
|
|
17
|
+
* 继承到宿主),换算为引擎 `ThemeOverride`——画布底色 / 表头 / 文字 / 网格线 /
|
|
18
|
+
* 冻结分隔线跟随 UI 主题。rowHeader / corner 由引擎随生效 header 派生,无需单写。
|
|
19
|
+
*
|
|
20
|
+
* 任一变量为空(未 loadTheme)返回 `undefined`:SheetGrid 不传 theme、保持引擎
|
|
21
|
+
* 缺省白底,不破坏无主题环境。
|
|
22
|
+
*/
|
|
23
|
+
function resolveCanvasThemeOverride(el) {
|
|
24
|
+
const style = getComputedStyle(el);
|
|
25
|
+
const bgColorTop = style.getPropertyValue("--u-bg-color-top").trim();
|
|
26
|
+
const bgColorBottom = style.getPropertyValue("--u-bg-color-bottom").trim();
|
|
27
|
+
const textColorMain = style.getPropertyValue("--u-text-color-main").trim();
|
|
28
|
+
const borderColor = style.getPropertyValue("--u-border-color").trim();
|
|
29
|
+
const borderMutedColor = style.getPropertyValue("--u-border-muted-color").trim();
|
|
30
|
+
if (!bgColorTop || !bgColorBottom || !textColorMain || !borderColor || !borderMutedColor) return;
|
|
31
|
+
return {
|
|
32
|
+
body: {
|
|
33
|
+
background: bgColorTop,
|
|
34
|
+
color: textColorMain,
|
|
35
|
+
borderColor
|
|
36
|
+
},
|
|
37
|
+
header: {
|
|
38
|
+
background: bgColorBottom,
|
|
39
|
+
color: textColorMain,
|
|
40
|
+
borderColor
|
|
41
|
+
},
|
|
42
|
+
underlayBackgroundColor: bgColorTop,
|
|
43
|
+
interaction: { freezeDividerColor: borderMutedColor },
|
|
44
|
+
frameStyle: { color: borderColor }
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
14
48
|
* SheetGrid 生命周期与网格右键菜单(LRU 实例缓存):
|
|
15
49
|
* - activateGrid:tab 切换入口——命中缓存 → 显示 + 模型同步(零重建);
|
|
16
50
|
* 未命中 → 新建实例入缓存,超容量淘汰最久未用(release + 移除容器)
|
|
@@ -29,7 +63,17 @@ function useSheetGrid(options) {
|
|
|
29
63
|
const sheet = getActiveSheet();
|
|
30
64
|
const renderSize = resolveRenderSize(props.rows, props.cols, sheet.rows, sheet.cols);
|
|
31
65
|
ensureContextMenuSelection(context, info, renderSize);
|
|
32
|
-
const menus = info.kind === "row-header" ? buildRowHeaderMenus(context, {
|
|
66
|
+
const menus = info.kind === "row-header" ? buildRowHeaderMenus(context, {
|
|
67
|
+
readonly: props.readonly,
|
|
68
|
+
row: info.row
|
|
69
|
+
}) : info.kind === "col-header" ? buildColHeaderMenus(context, {
|
|
70
|
+
readonly: props.readonly,
|
|
71
|
+
col: info.col
|
|
72
|
+
}) : buildBodyMenus(context, {
|
|
73
|
+
readonly: props.readonly,
|
|
74
|
+
openHyperlinkPopup: options.openHyperlinkPopup,
|
|
75
|
+
openImagePreview: options.openImagePreview
|
|
76
|
+
});
|
|
33
77
|
contextmenu.pop({
|
|
34
78
|
mousePosition: {
|
|
35
79
|
x: info.x,
|
|
@@ -74,6 +118,7 @@ function useSheetGrid(options) {
|
|
|
74
118
|
colResize: props.colResize,
|
|
75
119
|
scrollbar: props.scrollbar,
|
|
76
120
|
growOnScroll: props.growOnScroll,
|
|
121
|
+
theme: resolveCanvasThemeOverride(host),
|
|
77
122
|
onContextMenu: handleContextMenu,
|
|
78
123
|
onEditStart: (addr) => formulaBarRef.value?.mirrorGridEdit(addr),
|
|
79
124
|
onEditEnd: (addr) => formulaBarRef.value?.exitMirror(addr),
|
|
@@ -85,8 +130,34 @@ function useSheetGrid(options) {
|
|
|
85
130
|
const offStructureChange = sheet.on("structure-change", () => {
|
|
86
131
|
dirty = true;
|
|
87
132
|
});
|
|
133
|
+
const table = grid.getTable();
|
|
134
|
+
const offEngineEvents = [
|
|
135
|
+
table.onCellLinkHover((event) => options.onCellLinkHover?.(event)),
|
|
136
|
+
table.onCellLinkLeave((event) => options.onCellLinkLeave?.(event)),
|
|
137
|
+
table.onCellLinkClick((event) => options.onCellLinkClick?.(event)),
|
|
138
|
+
table.onImageLinkClick((event) => options.onImageLinkClick?.(event)),
|
|
139
|
+
table.floatObjects.onContextMenu((event) => {
|
|
140
|
+
const rect = el.getBoundingClientRect();
|
|
141
|
+
const payload = {
|
|
142
|
+
...event,
|
|
143
|
+
clientX: rect.left + event.x,
|
|
144
|
+
clientY: rect.top + event.y
|
|
145
|
+
};
|
|
146
|
+
options.onFloatContextMenu?.(payload);
|
|
147
|
+
popFloatImageMenu({
|
|
148
|
+
ctx: context,
|
|
149
|
+
event: payload,
|
|
150
|
+
handles: {
|
|
151
|
+
openImagePreview: options.openImagePreview,
|
|
152
|
+
openHyperlinkPopup: options.openHyperlinkPopup
|
|
153
|
+
},
|
|
154
|
+
readonly: props.readonly
|
|
155
|
+
});
|
|
156
|
+
})
|
|
157
|
+
];
|
|
88
158
|
const release = () => {
|
|
89
159
|
offStructureChange();
|
|
160
|
+
for (const off of offEngineEvents) off();
|
|
90
161
|
grid.release();
|
|
91
162
|
el.remove();
|
|
92
163
|
};
|
|
@@ -199,6 +270,15 @@ function useSheetGrid(options) {
|
|
|
199
270
|
onBeforeUnmount(() => {
|
|
200
271
|
invalidateAll();
|
|
201
272
|
});
|
|
273
|
+
watch([() => currentTheme.value?.theme, currentTheme], () => {
|
|
274
|
+
const host = gridRef.value;
|
|
275
|
+
const override = host ? resolveCanvasThemeOverride(host) : void 0;
|
|
276
|
+
if (!override) return;
|
|
277
|
+
for (const item of cache.values()) item.grid.updateTheme(override);
|
|
278
|
+
}, {
|
|
279
|
+
deep: true,
|
|
280
|
+
flush: "post"
|
|
281
|
+
});
|
|
202
282
|
/** fx 引用拾取会话置/清选区锚点(公式栏 → 活动实例透传;无活动实例时静默) */
|
|
203
283
|
function setSelectionAnchor(addr) {
|
|
204
284
|
active?.grid.setSelectionAnchor(addr);
|
|
@@ -212,4 +292,4 @@ function useSheetGrid(options) {
|
|
|
212
292
|
};
|
|
213
293
|
}
|
|
214
294
|
//#endregion
|
|
215
|
-
export { useSheetGrid };
|
|
295
|
+
export { resolveCanvasThemeOverride, useSheetGrid };
|
|
@@ -49,6 +49,7 @@ function useSheetState(props, emit, hooks) {
|
|
|
49
49
|
sheet.on("cell-change", bump),
|
|
50
50
|
sheet.on("merge-change", bump),
|
|
51
51
|
sheet.on("frozen-change", bump),
|
|
52
|
+
sheet.on("cell-image-change", (payload) => emit("cell-image-change", payload)),
|
|
52
53
|
sheet.on("content-reset", scheduleBump),
|
|
53
54
|
sheet.on("structure-change", hooks.rebuildGrid)
|
|
54
55
|
];
|
package/dist/tools/context.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CellAddress, CellData, CellInfo, CellRange, CellStyle, CellStylePatch, CellValue, FrozenState, HistoryState, ImageInput, ImageUpdateFields, SelectionState, SetCellValueItem, Sheet, SheetImage, Workbook } from "infinitable/sheet";
|
|
1
|
+
import { CellAddress, CellData, CellImagePayload, CellInfo, CellLinkPayload, CellRange, CellStyle, CellStylePatch, CellValue, FrozenState, HistoryState, ImageInput, ImageUpdateFields, SelectionState, SetCellValueItem, Sheet, SheetImage, Workbook } from "infinitable/sheet";
|
|
2
2
|
//#region src/tools/context.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* SheetContext:工具的唯一操作门面。
|
|
@@ -93,10 +93,24 @@ export interface SheetContext {
|
|
|
93
93
|
insertImage(input: ImageInput): string;
|
|
94
94
|
/** 删除浮动图片;不存在则无操作 */
|
|
95
95
|
removeImage(id: string): void;
|
|
96
|
-
/**
|
|
96
|
+
/** 更新浮动图片锚点/尺寸/文案/链接(link 传 null 清除);不存在或无变更则无操作 */
|
|
97
97
|
updateImage(id: string, patch: ImageUpdateFields): void;
|
|
98
98
|
/** 只读图片列表(快照副本) */
|
|
99
99
|
getImages(): readonly SheetImage[];
|
|
100
|
+
/** 写入单元格超链接;link 为 null 时清除该格链接(合并格解析锚点) */
|
|
101
|
+
setCellLink(addr: CellAddress, link: CellLinkPayload | null): void;
|
|
102
|
+
/** 读取单元格超链接载荷(副本);无链接返回 undefined(合并格解析锚点) */
|
|
103
|
+
getCellLink(addr: CellAddress): CellLinkPayload | undefined;
|
|
104
|
+
/** 清除单元格超链接;无链接则无操作(合并格解析锚点) */
|
|
105
|
+
clearCellLink(addr: CellAddress): void;
|
|
106
|
+
/** 插入(或覆盖)按格嵌入图;相同载荷视为无操作(合并格解析锚点) */
|
|
107
|
+
insertCellImage(addr: CellAddress, image: CellImagePayload): void;
|
|
108
|
+
/** 移除按格嵌入图;无图时无操作(合并格解析锚点) */
|
|
109
|
+
removeCellImage(addr: CellAddress): void;
|
|
110
|
+
/** 读取按格嵌入图载荷(副本);无图返回 undefined(合并格解析锚点) */
|
|
111
|
+
getCellImage(addr: CellAddress): CellImagePayload | undefined;
|
|
112
|
+
/** 该格是否有嵌入图(供右键菜单等判定;合并格解析锚点) */
|
|
113
|
+
hasCellImage(addr: CellAddress): boolean;
|
|
100
114
|
onSelectionChange(handler: (state: SelectionState) => void): () => void;
|
|
101
115
|
onHistoryChange(handler: (state: HistoryState) => void): () => void;
|
|
102
116
|
onFrozenChange(handler: (state: FrozenState) => void): () => void;
|
|
@@ -104,6 +118,10 @@ export interface SheetContext {
|
|
|
104
118
|
onImageChange(handler: (payload: {
|
|
105
119
|
id?: string;
|
|
106
120
|
}) => void): () => void;
|
|
121
|
+
/** 按格嵌入图变化(单格带 addr;结构平移/整表替换时缺省) */
|
|
122
|
+
onCellImageChange(handler: (payload: {
|
|
123
|
+
addr?: CellAddress;
|
|
124
|
+
}) => void): () => void;
|
|
107
125
|
}
|
|
108
126
|
/** createSheetContext 可选配置 */
|
|
109
127
|
export interface SheetContextOptions {
|
package/dist/tools/context.js
CHANGED
|
@@ -103,6 +103,13 @@ function createSheetContext(resolveSheet, workbook, options) {
|
|
|
103
103
|
removeImage: (id) => sheet().removeImage(id),
|
|
104
104
|
updateImage: (id, patch) => sheet().updateImage(id, patch),
|
|
105
105
|
getImages: () => sheet().getImages(),
|
|
106
|
+
setCellLink: (addr, link) => sheet().setCellLink(addr, link),
|
|
107
|
+
getCellLink: (addr) => sheet().getCellLink(addr),
|
|
108
|
+
clearCellLink: (addr) => sheet().clearCellLink(addr),
|
|
109
|
+
insertCellImage: (addr, image) => sheet().insertCellImage(addr, image),
|
|
110
|
+
removeCellImage: (addr) => sheet().removeCellImage(addr),
|
|
111
|
+
getCellImage: (addr) => sheet().getCellImage(addr),
|
|
112
|
+
hasCellImage: (addr) => sheet().hasCellImage(addr),
|
|
106
113
|
beginTransaction: () => sheet().beginTransaction(),
|
|
107
114
|
commit: () => sheet().commit(),
|
|
108
115
|
rollback: () => sheet().rollback(),
|
|
@@ -117,7 +124,8 @@ function createSheetContext(resolveSheet, workbook, options) {
|
|
|
117
124
|
onSelectionChange: (handler) => sheet().on("selection-change", handler),
|
|
118
125
|
onHistoryChange: (handler) => sheet().on("history-change", handler),
|
|
119
126
|
onFrozenChange: (handler) => sheet().on("frozen-change", handler),
|
|
120
|
-
onImageChange: (handler) => sheet().on("image-change", handler)
|
|
127
|
+
onImageChange: (handler) => sheet().on("image-change", handler),
|
|
128
|
+
onCellImageChange: (handler) => sheet().on("cell-image-change", handler)
|
|
121
129
|
};
|
|
122
130
|
}
|
|
123
131
|
//#endregion
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { SheetEmits, SheetExposed, SheetProps, _SheetExposed } from "./sheet.js";
|
|
2
|
-
export { SheetEmits, SheetExposed, SheetProps, _SheetExposed };
|
|
1
|
+
import { FloatContextMenuPayload, SheetEmits, SheetExposed, SheetProps, _SheetExposed } from "./sheet.js";
|
|
2
|
+
export { FloatContextMenuPayload, SheetEmits, SheetExposed, SheetProps, _SheetExposed };
|
package/dist/types/sheet.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { SheetContext } from "../tools/context.js";
|
|
2
|
-
import { ResolveCellRenderer, ResolveCellStyleHook, ResolveDisplayValue,
|
|
2
|
+
import { CellAddress, ResolveCellRenderer, ResolveCellStyleHook, ResolveDisplayValue, Sheet, SheetGrid, SheetGridEditorsOptions, SheetGridHeaderOptions, Workbook } from "infinitable/sheet";
|
|
3
3
|
import { ComputedRef } from "vue";
|
|
4
4
|
import { DeconstructValue } from "@veltra/utils";
|
|
5
|
+
import { CellLinkClickEvent, CellLinkHoverEvent, CellLinkLeaveEvent, FloatContextMenuEvent, ImageLinkClickEvent } from "infinitable";
|
|
5
6
|
//#region src/types/sheet.d.ts
|
|
6
7
|
/** 电子表格组件属性 */
|
|
7
8
|
export interface SheetProps {
|
|
@@ -54,13 +55,11 @@ export interface SheetProps {
|
|
|
54
55
|
colResize?: boolean;
|
|
55
56
|
/**
|
|
56
57
|
* 滚动条(透传 SheetGrid → 引擎内建,构造期选项):false 整体关闭(不显示任何
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* canvas 档显示策略)。对象形态引用更替触发网格重建——沿用 header / editors
|
|
61
|
-
* 的稳定引用约定,宿主勿在模板内联对象字面量
|
|
58
|
+
* 滚动条);缺省 true 画布常驻档——引擎归一化 canvas always + reserve,滚动条
|
|
59
|
+
* 常驻画在画布内(reserve 只在画布内预留轨道,不占页面布局、不遮挡单元格)。
|
|
60
|
+
* 变化触发网格重建
|
|
62
61
|
*/
|
|
63
|
-
scrollbar?: boolean
|
|
62
|
+
scrollbar?: boolean;
|
|
64
63
|
/**
|
|
65
64
|
* 滚动近端动态增长(透传 SheetGrid,构造期选项):滚动缓冲末端触到当前行列数
|
|
66
65
|
* 时模型与引擎同步扩容(引擎缺省 true);false 为固定尺寸网格——内容定尺寸的
|
|
@@ -68,6 +67,24 @@ export interface SheetProps {
|
|
|
68
67
|
*/
|
|
69
68
|
growOnScroll?: boolean;
|
|
70
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* 浮动图右键事件载荷:引擎 `FloatContextMenuEvent` 关键字段 + 页面坐标。
|
|
72
|
+
*
|
|
73
|
+
* 右键命中浮动图时引擎不穿透到单元格右键(body 菜单不弹),宿主经
|
|
74
|
+
* `float-context-menu` 自行弹浮动图菜单(预览 / 超链接 / 删除等)。
|
|
75
|
+
*/
|
|
76
|
+
export interface FloatContextMenuPayload {
|
|
77
|
+
/** 命中的浮动对象 id */
|
|
78
|
+
id: string;
|
|
79
|
+
/** 层坐标(CSS 像素,相对 grid 实例容器) */
|
|
80
|
+
x: number;
|
|
81
|
+
y: number;
|
|
82
|
+
/** 页面视口坐标(层坐标 + grid 容器偏移;与 body 右键菜单同口径,供菜单定位) */
|
|
83
|
+
clientX: number;
|
|
84
|
+
clientY: number;
|
|
85
|
+
/** 引擎原始右键事件(最小 DOM 事件结构) */
|
|
86
|
+
originalEvent: FloatContextMenuEvent['originalEvent'];
|
|
87
|
+
}
|
|
71
88
|
export interface SheetEmits {
|
|
72
89
|
/** 激活 sheet 切换(点击 tab 或宿主调用 workbook.activateSheet) */
|
|
73
90
|
(name: 'active-sheet-change', payload: {
|
|
@@ -79,6 +96,36 @@ export interface SheetEmits {
|
|
|
79
96
|
col: number;
|
|
80
97
|
width: number;
|
|
81
98
|
}): void;
|
|
99
|
+
/**
|
|
100
|
+
* 单元格超链接悬停(引擎 `CellLinkHoverEvent` 形态:格坐标 + URL/显示文本 +
|
|
101
|
+
* 落点层坐标);tooltip 由宿主渲染,ultra-ui 不接管
|
|
102
|
+
*/
|
|
103
|
+
(name: 'cell-link-hover', payload: CellLinkHoverEvent): void;
|
|
104
|
+
/** 单元格超链接离开(引擎 `CellLinkLeaveEvent` 形态:刚离开的链接格坐标) */
|
|
105
|
+
(name: 'cell-link-leave', payload: CellLinkLeaveEvent): void;
|
|
106
|
+
/**
|
|
107
|
+
* 单元格超链接单击(引擎 `CellLinkClickEvent` 形态:真实指针 down+up 命中
|
|
108
|
+
* 同一链接格;框选拖拽/键盘选中不触发)。单击打开仍由引擎
|
|
109
|
+
* `openCellLinkOnClick` 默认处理,ultra-ui 不接管
|
|
110
|
+
*/
|
|
111
|
+
(name: 'cell-link-click', payload: CellLinkClickEvent): void;
|
|
112
|
+
/**
|
|
113
|
+
* 图片链接单击(引擎 `ImageLinkClickEvent`,`kind` 区分 `cell` 嵌入图 /
|
|
114
|
+
* `float` 浮动图);默认打开行为同上由引擎处理
|
|
115
|
+
*/
|
|
116
|
+
(name: 'image-link-click', payload: ImageLinkClickEvent): void;
|
|
117
|
+
/**
|
|
118
|
+
* 按格嵌入图变化(`{ addr?: CellAddress }` 定向语义:单格变更带 addr,
|
|
119
|
+
* 结构平移/整表替换时缺省);仅当前活动 sheet 的事件被透传
|
|
120
|
+
*/
|
|
121
|
+
(name: 'cell-image-change', payload: {
|
|
122
|
+
addr?: CellAddress;
|
|
123
|
+
}): void;
|
|
124
|
+
/**
|
|
125
|
+
* 浮动图右键(载荷见 {@link FloatContextMenuPayload};右键不穿透到
|
|
126
|
+
* 单元格 body 菜单,宿主据此弹浮动图独立菜单)
|
|
127
|
+
*/
|
|
128
|
+
(name: 'float-context-menu', payload: FloatContextMenuPayload): void;
|
|
82
129
|
}
|
|
83
130
|
/** 在组件内部引用 */
|
|
84
131
|
export interface _SheetExposed {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veltra/sheet",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"homepage": "https://github.com/cabinet-fe/ultra-ui#readme",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/cabinet-fe/ultra-ui/issues"
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
}
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"infinitable": "^0.1.
|
|
37
|
+
"infinitable": "^0.1.7"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
40
|
"@cat-kit/core": ">=1.2.1",
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { Sheet } from 'infinitable/sheet'
|
|
2
|
+
import { describe, expect, it } from 'vite-plus/test'
|
|
3
|
+
|
|
4
|
+
import { createSheetContext } from '../../../tools/context'
|
|
5
|
+
import { buildFloatImageMenus } from '../float-image-menu'
|
|
6
|
+
import type { ImagePreviewSource } from '../popup-helpers'
|
|
7
|
+
|
|
8
|
+
/** 插入一张浮动图(data 字节 + 可选 src/link),返回 id */
|
|
9
|
+
function insertFloatImage(sheet: Sheet, input: { src?: string; link?: string } = {}): string {
|
|
10
|
+
return sheet.insertImage({
|
|
11
|
+
data: new Uint8Array([1, 2, 3]),
|
|
12
|
+
type: 'png',
|
|
13
|
+
anchor: { from: { row: 0, col: 0 } },
|
|
14
|
+
src: input.src,
|
|
15
|
+
link: input.link
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** 句柄 spy:记录预览源与弹窗调用(mode + 回填 + 目标) */
|
|
20
|
+
function handlesSpy() {
|
|
21
|
+
const previews: ImagePreviewSource[] = []
|
|
22
|
+
const popups: Array<{
|
|
23
|
+
mode: 'image'
|
|
24
|
+
initial?: { url: string }
|
|
25
|
+
target?: { kind: 'float'; id: string }
|
|
26
|
+
}> = []
|
|
27
|
+
return {
|
|
28
|
+
previews,
|
|
29
|
+
popups,
|
|
30
|
+
handles: {
|
|
31
|
+
openImagePreview: (source: ImagePreviewSource): void => {
|
|
32
|
+
previews.push(source)
|
|
33
|
+
},
|
|
34
|
+
openHyperlinkPopup: (
|
|
35
|
+
mode: 'image',
|
|
36
|
+
initial?: { url: string },
|
|
37
|
+
target?: { kind: 'float'; id: string }
|
|
38
|
+
): void => {
|
|
39
|
+
popups.push({ mode, initial, target })
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe('buildFloatImageMenus(浮动图右键独立菜单)', () => {
|
|
46
|
+
it('无链接浮动图:预览 / 设置超链接 / 删除图片(无取消超链接);预览源 src 优先', () => {
|
|
47
|
+
const sheet = new Sheet()
|
|
48
|
+
const ctx = createSheetContext(sheet)
|
|
49
|
+
const id = insertFloatImage(sheet, { src: 'https://example.com/a.png' })
|
|
50
|
+
const spy = handlesSpy()
|
|
51
|
+
|
|
52
|
+
const menus = buildFloatImageMenus(ctx, id, spy.handles)
|
|
53
|
+
expect(menus.map((m) => m.label)).toEqual(['预览', '设置超链接', '删除图片'])
|
|
54
|
+
|
|
55
|
+
menus.find((m) => m.label === '预览')!.callback!()
|
|
56
|
+
expect(spy.previews).toEqual([{ src: 'https://example.com/a.png' }])
|
|
57
|
+
|
|
58
|
+
menus.find((m) => m.label === '设置超链接')!.callback!()
|
|
59
|
+
expect(spy.popups).toEqual([
|
|
60
|
+
{ mode: 'image', initial: undefined, target: { kind: 'float', id } }
|
|
61
|
+
])
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('data 字节图预览源:字节 + 引擎 type 映射 MIME', () => {
|
|
65
|
+
const sheet = new Sheet()
|
|
66
|
+
const ctx = createSheetContext(sheet)
|
|
67
|
+
const id = insertFloatImage(sheet)
|
|
68
|
+
const spy = handlesSpy()
|
|
69
|
+
|
|
70
|
+
buildFloatImageMenus(ctx, id, spy.handles).find((m) => m.label === '预览')!.callback!()
|
|
71
|
+
expect(spy.previews).toEqual([{ data: new Uint8Array([1, 2, 3]), mime: 'image/png' }])
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('有链接浮动图:含取消超链接;设置超链接回填原 url;取消清 link 且 undo 还原', () => {
|
|
75
|
+
const sheet = new Sheet()
|
|
76
|
+
const ctx = createSheetContext(sheet)
|
|
77
|
+
const id = insertFloatImage(sheet, { link: 'https://example.com' })
|
|
78
|
+
const spy = handlesSpy()
|
|
79
|
+
|
|
80
|
+
const menus = buildFloatImageMenus(ctx, id, spy.handles)
|
|
81
|
+
expect(menus.map((m) => m.label)).toEqual(['预览', '设置超链接', '取消超链接', '删除图片'])
|
|
82
|
+
|
|
83
|
+
menus.find((m) => m.label === '设置超链接')!.callback!()
|
|
84
|
+
expect(spy.popups).toEqual([
|
|
85
|
+
{ mode: 'image', initial: { url: 'https://example.com' }, target: { kind: 'float', id } }
|
|
86
|
+
])
|
|
87
|
+
|
|
88
|
+
menus.find((m) => m.label === '取消超链接')!.callback!()
|
|
89
|
+
expect(ctx.getImages().find((img) => img.id === id)?.link).toBeUndefined()
|
|
90
|
+
expect(ctx.undo()).toBe(true)
|
|
91
|
+
expect(ctx.getImages().find((img) => img.id === id)?.link).toBe('https://example.com')
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('删除图片:getImages 不含该 id 且 undo 还原', () => {
|
|
95
|
+
const sheet = new Sheet()
|
|
96
|
+
const ctx = createSheetContext(sheet)
|
|
97
|
+
const id = insertFloatImage(sheet)
|
|
98
|
+
|
|
99
|
+
buildFloatImageMenus(ctx, id, handlesSpy().handles).find((m) => m.label === '删除图片')!
|
|
100
|
+
.callback!()
|
|
101
|
+
expect(ctx.getImages().some((img) => img.id === id)).toBe(false)
|
|
102
|
+
expect(ctx.undo()).toBe(true)
|
|
103
|
+
expect(ctx.getImages().some((img) => img.id === id)).toBe(true)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('id 不存在(并发删除):返回空菜单', () => {
|
|
107
|
+
const sheet = new Sheet()
|
|
108
|
+
const ctx = createSheetContext(sheet)
|
|
109
|
+
expect(buildFloatImageMenus(ctx, 'missing', handlesSpy().handles)).toEqual([])
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('readonly:设置/取消/删除禁用、预览可用;句柄缺省:预览/设置禁用', () => {
|
|
113
|
+
const sheet = new Sheet()
|
|
114
|
+
const ctx = createSheetContext(sheet)
|
|
115
|
+
const id = insertFloatImage(sheet, { link: 'https://example.com' })
|
|
116
|
+
|
|
117
|
+
const readonly = buildFloatImageMenus(ctx, id, handlesSpy().handles, { readonly: true })
|
|
118
|
+
expect(readonly.find((m) => m.label === '预览')?.disabled).toBeFalsy()
|
|
119
|
+
expect(readonly.find((m) => m.label === '设置超链接')?.disabled).toBe(true)
|
|
120
|
+
expect(readonly.find((m) => m.label === '取消超链接')?.disabled).toBe(true)
|
|
121
|
+
expect(readonly.find((m) => m.label === '删除图片')?.disabled).toBe(true)
|
|
122
|
+
|
|
123
|
+
const noHandles = buildFloatImageMenus(ctx, id)
|
|
124
|
+
expect(noHandles.find((m) => m.label === '预览')?.disabled).toBe(true)
|
|
125
|
+
expect(noHandles.find((m) => m.label === '设置超链接')?.disabled).toBe(true)
|
|
126
|
+
// 取消/删除只依赖 ctx,句柄缺省仍可用
|
|
127
|
+
expect(noHandles.find((m) => m.label === '取消超链接')?.disabled).toBeFalsy()
|
|
128
|
+
expect(noHandles.find((m) => m.label === '删除图片')?.disabled).toBeFalsy()
|
|
129
|
+
})
|
|
130
|
+
})
|
|
@@ -13,9 +13,8 @@ export const cellX = (col: number) => ROW_HEADER_WIDTH + col * DEFAULT_COL_WIDTH
|
|
|
13
13
|
export const cellY = (row: number) => HEADER_HEIGHT + row * DEFAULT_ROW_HEIGHT + 5
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
* 引擎指针事件派发目标:body
|
|
17
|
-
*
|
|
18
|
-
* 在两档下都是其后代,从它派发(冒泡)对两种形态都到达引擎监听。
|
|
16
|
+
* 引擎指针事件派发目标:body 层画布。引擎事件源是挂载容器,body 画布
|
|
17
|
+
* 是其后代,从它派发(冒泡)到达引擎监听。
|
|
19
18
|
*/
|
|
20
19
|
export function gridEventTarget(root: HTMLElement): HTMLElement {
|
|
21
20
|
return root.querySelector<HTMLElement>(".u-sheet__grid-instance canvas[data-layer-kind='body']")!
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
|
|
2
|
+
import { createApp, h, type App } from 'vue'
|
|
3
|
+
|
|
4
|
+
import type { HyperlinkConfirmPayload } from '../popup-helpers'
|
|
5
|
+
import USheetHyperlinkPopup from '../popups/hyperlink-popup.vue'
|
|
6
|
+
|
|
7
|
+
const mocks = vi.hoisted(() => {
|
|
8
|
+
const message = Object.assign(vi.fn(), {
|
|
9
|
+
success: vi.fn(),
|
|
10
|
+
warn: vi.fn(),
|
|
11
|
+
info: vi.fn(),
|
|
12
|
+
error: vi.fn(),
|
|
13
|
+
default: vi.fn()
|
|
14
|
+
})
|
|
15
|
+
return { message }
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
vi.mock('@veltra/desktop', async () => {
|
|
19
|
+
const { defineComponent, h } = await import('vue')
|
|
20
|
+
return {
|
|
21
|
+
message: mocks.message,
|
|
22
|
+
UInput: defineComponent({
|
|
23
|
+
name: 'UInputStub',
|
|
24
|
+
props: ['modelValue', 'placeholder'],
|
|
25
|
+
emits: ['update:modelValue'],
|
|
26
|
+
setup(props, { emit }) {
|
|
27
|
+
return () =>
|
|
28
|
+
h('input', {
|
|
29
|
+
class: 'hyperlink-input-stub',
|
|
30
|
+
placeholder: props.placeholder as string,
|
|
31
|
+
value: props.modelValue as string,
|
|
32
|
+
onInput: (e: Event) => emit('update:modelValue', (e.target as HTMLInputElement).value)
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
}),
|
|
36
|
+
UButton: defineComponent({
|
|
37
|
+
name: 'UButtonStub',
|
|
38
|
+
props: ['disabled', 'type'],
|
|
39
|
+
emits: ['click'],
|
|
40
|
+
setup(props, { emit, slots }) {
|
|
41
|
+
return () =>
|
|
42
|
+
h(
|
|
43
|
+
'button',
|
|
44
|
+
{
|
|
45
|
+
class: 'hyperlink-btn-stub',
|
|
46
|
+
disabled: props.disabled as boolean,
|
|
47
|
+
onClick: () => emit('click')
|
|
48
|
+
},
|
|
49
|
+
slots.default?.()
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
const apps: App[] = []
|
|
57
|
+
const containers: HTMLElement[] = []
|
|
58
|
+
|
|
59
|
+
interface MountResult {
|
|
60
|
+
confirms: HyperlinkConfirmPayload[]
|
|
61
|
+
closeCount: () => number
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function mountPopup(props: {
|
|
65
|
+
mode: 'cell' | 'image'
|
|
66
|
+
initial?: { url: string; text?: string }
|
|
67
|
+
}): MountResult {
|
|
68
|
+
let closed = 0
|
|
69
|
+
const confirms: HyperlinkConfirmPayload[] = []
|
|
70
|
+
const el = document.createElement('div')
|
|
71
|
+
document.body.appendChild(el)
|
|
72
|
+
containers.push(el)
|
|
73
|
+
const app = createApp({
|
|
74
|
+
setup() {
|
|
75
|
+
return () =>
|
|
76
|
+
h(USheetHyperlinkPopup, {
|
|
77
|
+
mode: props.mode,
|
|
78
|
+
initial: props.initial,
|
|
79
|
+
onConfirm: (payload: HyperlinkConfirmPayload) => confirms.push(payload),
|
|
80
|
+
onClose: () => {
|
|
81
|
+
closed++
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
app.mount(el)
|
|
87
|
+
apps.push(app)
|
|
88
|
+
return { confirms, closeCount: () => closed }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** 弹窗根面板(Teleport 到 body) */
|
|
92
|
+
function dialogEl(): HTMLElement {
|
|
93
|
+
return document.body.querySelector<HTMLElement>('.u-sheet__link-dialog')!
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** 按文案取弹窗内按钮(确定 / 取消) */
|
|
97
|
+
function dialogButton(text: string): HTMLButtonElement {
|
|
98
|
+
return [...dialogEl().querySelectorAll<HTMLButtonElement>('.hyperlink-btn-stub')].find((btn) =>
|
|
99
|
+
btn.textContent?.includes(text)
|
|
100
|
+
)!
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** 第 index 个输入框写入并同步 v-model */
|
|
104
|
+
async function setInput(index: number, value: string): Promise<void> {
|
|
105
|
+
const inputs = [...dialogEl().querySelectorAll<HTMLInputElement>('.hyperlink-input-stub')]
|
|
106
|
+
inputs[index]!.value = value
|
|
107
|
+
inputs[index]!.dispatchEvent(new Event('input', { bubbles: true }))
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
afterEach(() => {
|
|
111
|
+
apps.splice(0).forEach((app) => app.unmount())
|
|
112
|
+
containers.splice(0).forEach((el) => el.remove())
|
|
113
|
+
vi.clearAllMocks()
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
describe('USheetHyperlinkPopup', () => {
|
|
117
|
+
it('空地址提交拦截:message.error 提示、不 confirm 也不关闭', async () => {
|
|
118
|
+
const result = mountPopup({ mode: 'cell' })
|
|
119
|
+
await Promise.resolve()
|
|
120
|
+
|
|
121
|
+
dialogButton('确定').click()
|
|
122
|
+
expect(mocks.message.error).toHaveBeenCalledWith('请输入链接地址')
|
|
123
|
+
expect(result.confirms).toEqual([])
|
|
124
|
+
expect(result.closeCount()).toBe(0)
|
|
125
|
+
|
|
126
|
+
// 填地址后提交放行
|
|
127
|
+
await setInput(0, 'https://example.com')
|
|
128
|
+
dialogButton('确定').click()
|
|
129
|
+
expect(mocks.message.error).toHaveBeenCalledTimes(1)
|
|
130
|
+
expect(result.confirms).toEqual([{ url: 'https://example.com' }])
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it('cell 模式:显示文本留空提交不含 text(沿用单元格内容显示);填写则随载荷写入', async () => {
|
|
134
|
+
const result = mountPopup({
|
|
135
|
+
mode: 'cell',
|
|
136
|
+
initial: { url: 'https://old.example.com', text: '示例' }
|
|
137
|
+
})
|
|
138
|
+
await Promise.resolve()
|
|
139
|
+
|
|
140
|
+
// 编辑回填:改地址、清空显示文本
|
|
141
|
+
await setInput(0, 'https://new.example.com')
|
|
142
|
+
await setInput(1, '')
|
|
143
|
+
dialogButton('确定').click()
|
|
144
|
+
expect(result.confirms).toEqual([{ url: 'https://new.example.com' }])
|
|
145
|
+
expect('text' in result.confirms[0]!).toBe(false)
|
|
146
|
+
|
|
147
|
+
// 显示文本填写时随载荷提交
|
|
148
|
+
await setInput(0, 'https://example.com')
|
|
149
|
+
await setInput(1, ' 官网 ')
|
|
150
|
+
dialogButton('确定').click()
|
|
151
|
+
expect(result.confirms[1]).toEqual({ url: 'https://example.com', text: '官网' })
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
it('image 模式:仅地址一个字段(无显示文本行),标题为设置图片超链接', async () => {
|
|
155
|
+
mountPopup({ mode: 'image' })
|
|
156
|
+
await Promise.resolve()
|
|
157
|
+
|
|
158
|
+
expect(dialogEl().querySelectorAll('.hyperlink-input-stub')).toHaveLength(1)
|
|
159
|
+
expect(dialogEl().textContent).toContain('设置图片超链接')
|
|
160
|
+
expect(
|
|
161
|
+
[...dialogEl().querySelectorAll('.u-sheet__popup-label')].map((l) => l.textContent)
|
|
162
|
+
).toEqual(['地址'])
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it('取消按钮与 Esc 均关闭(emit close,不 confirm)', async () => {
|
|
166
|
+
const result = mountPopup({ mode: 'cell' })
|
|
167
|
+
await Promise.resolve()
|
|
168
|
+
|
|
169
|
+
dialogButton('取消').click()
|
|
170
|
+
expect(result.closeCount()).toBe(1)
|
|
171
|
+
expect(result.confirms).toEqual([])
|
|
172
|
+
|
|
173
|
+
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }))
|
|
174
|
+
expect(result.closeCount()).toBe(2)
|
|
175
|
+
})
|
|
176
|
+
})
|