@veltra/sheet 4.1.0 → 4.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,10 +2,33 @@ import { imageLinkInitial, imagePreviewSource } from "./popup-helpers.js";
2
2
  import { contextmenu } from "@veltra/desktop";
3
3
  //#region src/components/sheet/float-image-menu.ts
4
4
  /**
5
- * 浮动图右键独立菜单:预览 / 设置超链接 / 取消超链接(无链接不出现)/ 删除图片。
6
- * 设置走 hyperlink 弹窗 image 模式(确认 `updateImage(id, { link })`);取消直写
7
- * `updateImage(id, { link: null })`(null 清除,可 undo);删除走 `removeImage`
8
- * (命令系统,可 undo)。命中 id 不存在(并发删除)返回空数组,调用方不弹菜单。
5
+ * 浮动图转为按格嵌入图(对齐 WPS/Excel「放置在单元格中」):落点为图片锚点格
6
+ * (anchor.from,即图片左上角所在格),src/data/type/link 随载荷迁移。事务内
7
+ * insertCellImage + removeImage——一次转换 = 一个 undo 单元。
8
+ */
9
+ function embedFloatImageToCell(ctx, id) {
10
+ const image = ctx.getImages().find((item) => item.id === id);
11
+ if (!image) return;
12
+ const payload = image.src ? {
13
+ src: image.src,
14
+ type: image.type
15
+ } : {
16
+ data: image.data,
17
+ type: image.type
18
+ };
19
+ if (image.link) payload.link = image.link;
20
+ ctx.beginTransaction();
21
+ ctx.insertCellImage(image.anchor.from, payload);
22
+ ctx.removeImage(id);
23
+ ctx.commit();
24
+ }
25
+ /**
26
+ * 浮动图右键独立菜单:预览 / 设置超链接 / 取消超链接(无链接不出现)/
27
+ * 嵌入单元格 / 删除图片。设置走 hyperlink 弹窗 image 模式(confirm
28
+ * `updateImage(id, { link })`);取消直写 `updateImage(id, { link: null })`
29
+ * (null 清除,可 undo);嵌入单元格走 `embedFloatImageToCell`(事务合并单 undo);
30
+ * 删除走 `removeImage`(命令系统,可 undo)。命中 id 不存在(并发删除)返回空数组,
31
+ * 调用方不弹菜单。
9
32
  */
10
33
  function buildFloatImageMenus(ctx, id, handles, options) {
11
34
  const image = ctx.getImages().find((item) => item.id === id);
@@ -31,6 +54,11 @@ function buildFloatImageMenus(ctx, id, handles, options) {
31
54
  disabled: readonly,
32
55
  callback: () => ctx.updateImage(id, { link: null })
33
56
  }] : [],
57
+ {
58
+ label: "嵌入单元格",
59
+ disabled: readonly,
60
+ callback: () => embedFloatImageToCell(ctx, id)
61
+ },
34
62
  {
35
63
  label: "删除图片",
36
64
  disabled: readonly,
@@ -56,4 +84,4 @@ function popFloatImageMenu(options) {
56
84
  });
57
85
  }
58
86
  //#endregion
59
- export { buildFloatImageMenus, popFloatImageMenu };
87
+ export { buildFloatImageMenus, embedFloatImageToCell, popFloatImageMenu };
@@ -87,9 +87,9 @@ function insertImageFromUrl(ctx, url) {
87
87
  }
88
88
  /**
89
89
  * 拉起系统文件选择框(右键菜单用;不用 UFilePicker——无编程式打开 API)。
90
- * 选中文件后走 insertImageFromFile。
90
+ * 选中文件后交给 onPick;取消选择时清理节点。
91
91
  */
92
- function pickAndInsertImage(ctx) {
92
+ function pickImageFile(onPick) {
93
93
  const input = document.createElement("input");
94
94
  input.type = "file";
95
95
  input.accept = INSERT_IMAGE_ACCEPT;
@@ -100,7 +100,7 @@ function pickAndInsertImage(ctx) {
100
100
  input.addEventListener("change", () => {
101
101
  const file = input.files?.[0];
102
102
  cleanup();
103
- if (file) insertImageFromFile(ctx, file);
103
+ if (file) onPick(file);
104
104
  });
105
105
  window.addEventListener("focus", () => {
106
106
  setTimeout(() => {
@@ -110,5 +110,34 @@ function pickAndInsertImage(ctx) {
110
110
  document.body.appendChild(input);
111
111
  input.click();
112
112
  }
113
+ /** 拉起系统文件选择框,选中后插入浮动图片 */
114
+ function pickAndInsertImage(ctx) {
115
+ pickImageFile((file) => void insertImageFromFile(ctx, file));
116
+ }
117
+ /**
118
+ * 从本地 File 嵌入图片到活动格:读字节 → 映射类型 → ctx.insertCellImage
119
+ * (格内已有嵌入图时整体覆盖)。不支持的格式 / 无活动格时 message.error 提示。
120
+ */
121
+ async function insertCellImageFromFile(ctx, file) {
122
+ const type = resolveImageType(file);
123
+ if (!type) {
124
+ message.error("不支持的图片格式,请选择 png / jpeg / gif / svg / webp");
125
+ return;
126
+ }
127
+ const active = ctx.getSelection().activeCell;
128
+ if (!active) {
129
+ message.error("请先选择单元格");
130
+ return;
131
+ }
132
+ const buffer = await file.arrayBuffer();
133
+ ctx.insertCellImage(active, {
134
+ data: new Uint8Array(buffer),
135
+ type
136
+ });
137
+ }
138
+ /** 拉起系统文件选择框,选中后嵌入图片到活动格(已有嵌入图时覆盖替换) */
139
+ function pickAndEmbedCellImage(ctx) {
140
+ pickImageFile((file) => void insertCellImageFromFile(ctx, file));
141
+ }
113
142
  //#endregion
114
- export { INSERT_IMAGE_ACCEPT, insertImageFromFile, insertImageFromUrl, pickAndInsertImage, resolveImageType };
143
+ export { INSERT_IMAGE_ACCEPT, insertCellImageFromFile, insertImageFromFile, insertImageFromUrl, pickAndEmbedCellImage, pickAndInsertImage, resolveImageType };
@@ -1,6 +1,6 @@
1
1
  import { defaultToolRegistry } from "../../tools/registry.js";
2
2
  import { imageLinkInitial, imagePreviewSource } from "./popup-helpers.js";
3
- import { pickAndInsertImage } from "./insert-image.js";
3
+ import { pickAndEmbedCellImage, pickAndInsertImage } from "./insert-image.js";
4
4
  import _sfc_main from "./insert-count-menu-item.js";
5
5
  import { createRange } from "infinitable/sheet";
6
6
  import { defineComponent, h } from "vue";
@@ -241,11 +241,39 @@ function cellLinkMenuItems(ctx, active, options) {
241
241
  }];
242
242
  }
243
243
  /**
244
- * body 菜单嵌入图项:目标格含嵌入图时附加——预览 / 图片超链接 / 取消超链接
245
- * (按载荷 link 状态出项)/ 移除图片。设置走 hyperlink 弹窗 image 模式(确认后
246
- * 由宿主覆盖写 `insertCellImage(addr, { ...payload, link })`);取消直写覆盖
247
- * `link: undefined`(引擎 CellImagePayload 清除约定,其余字段保留);移除走
248
- * `removeCellImage`(命令系统,可 undo)。无嵌入图返回空数组(不出现图片项)。
244
+ * 按格嵌入图转为浮动图片(对齐 Excel「放置到单元格上」/ WPS 反向切换):浮动图
245
+ * 落在嵌入图锚点格、尺寸取该格宽高(视觉等价还原),fit 缺省 contain(对齐嵌入图
246
+ * 渲染约定,避免浮动图缺省 fill 拉伸变形),src/data/type/link 随载荷迁移。事务内
247
+ * insertImage + removeCellImage——一次转换 = 一个 undo 单元。
248
+ */
249
+ function cellImageToFloat(ctx, addr) {
250
+ const payload = ctx.getCellImage(addr);
251
+ if (!payload) return;
252
+ const anchor = ctx.getCellInfo(addr).anchor;
253
+ const input = {
254
+ data: payload.data ?? /* @__PURE__ */ new Uint8Array(),
255
+ type: payload.type ?? "png",
256
+ anchor: { from: {
257
+ row: anchor.row,
258
+ col: anchor.col
259
+ } },
260
+ width: ctx.getColWidth(anchor.col) ?? 80,
261
+ height: ctx.getRowHeight(anchor.row) ?? 28,
262
+ fit: payload.fit ?? "contain"
263
+ };
264
+ if (payload.src) input.src = payload.src;
265
+ if (payload.link) input.link = payload.link;
266
+ ctx.beginTransaction();
267
+ ctx.insertImage(input);
268
+ ctx.removeCellImage(addr);
269
+ ctx.commit();
270
+ }
271
+ /**
272
+ * body 菜单嵌入图项:预览 / 图片超链接 / 取消超链接(按载荷 link 状态出项)/
273
+ * 转为浮动图片 / 替换图片 / 移除图片。设置走 hyperlink 弹窗 image 模式(确认后由宿主覆盖写
274
+ * `insertCellImage(addr, { ...payload, link })`);取消直写覆盖 `link: undefined`
275
+ * (引擎 CellImagePayload 清除约定,其余字段保留);转换/替换/移除分别走
276
+ * `cellImageToFloat`、`insertCellImage` 覆盖与 `removeCellImage`(命令系统,可 undo)。
249
277
  */
250
278
  function cellImageMenuItems(ctx, active, options) {
251
279
  if (!active || !ctx.hasCellImage(active)) return [];
@@ -274,18 +302,31 @@ function cellImageMenuItems(ctx, active, options) {
274
302
  })
275
303
  });
276
304
  items.push({
305
+ label: "转为浮动图片",
306
+ disabled: readonly,
307
+ callback: () => cellImageToFloat(ctx, active)
308
+ }, {
309
+ label: "替换图片",
310
+ disabled: readonly,
311
+ callback: () => pickAndEmbedCellImage(ctx)
312
+ }, {
277
313
  label: "移除图片",
278
314
  disabled: readonly,
279
315
  callback: () => ctx.removeCellImage(active)
280
316
  });
281
317
  return items;
282
318
  }
283
- /** body 格右键菜单:合并/取消合并 + 设置数据格式 + 超链接 + 插入图片(+ 嵌入图四项;行列插入/删除仅行号/列头菜单) */
319
+ /**
320
+ * body 格右键菜单。目标格含嵌入图时出**纯图片菜单**(预览 / 超链接 / 转浮动 /
321
+ * 替换 / 移除)——图片盖住整格,右键语义是「图片」而非「单元格」,不出合并 /
322
+ * 数据格式 / 超链接等普通格项(对齐 Excel 图片右键约定)。普通格:合并 / 取消合并 +
323
+ * 设置数据格式 + 超链接 + 插入图片(浮动)+ 嵌入图片(入格);行列插入/删除仅行号/列头菜单。
324
+ */
284
325
  function buildBodyMenus(ctx, options) {
326
+ const active = ctx.getSelection().activeCell;
327
+ if (active && ctx.hasCellImage(active)) return cellImageMenuItems(ctx, active, options);
285
328
  const mergeTool = defaultToolRegistry.get("merge");
286
329
  const unmergeTool = defaultToolRegistry.get("unmerge");
287
- const active = ctx.getSelection().activeCell;
288
- const imageItems = cellImageMenuItems(ctx, active, options);
289
330
  return [
290
331
  {
291
332
  label: "合并单元格",
@@ -337,7 +378,11 @@ function buildBodyMenus(ctx, options) {
337
378
  disabled: !active,
338
379
  callback: () => pickAndInsertImage(ctx)
339
380
  },
340
- ...imageItems.length > 0 ? [{ divider: true }, ...imageItems] : []
381
+ {
382
+ label: "嵌入图片",
383
+ disabled: !active,
384
+ callback: () => pickAndEmbedCellImage(ctx)
385
+ }
341
386
  ];
342
387
  }
343
388
  /**
@@ -359,4 +404,4 @@ function ensureContextMenuSelection(ctx, info, renderSize) {
359
404
  }
360
405
  }
361
406
  //#endregion
362
- export { AXIS_SIZE_MAX, INSERT_COUNT_MAX, applyNumFmtToSelection, buildBodyMenus, buildColHeaderMenus, buildRowHeaderMenus, defaultInsertCount, ensureContextMenuSelection, isColInSelection, isRowInSelection, resolveRenderSize, wholeColRange, wholeRowRange };
407
+ export { AXIS_SIZE_MAX, INSERT_COUNT_MAX, applyNumFmtToSelection, buildBodyMenus, buildColHeaderMenus, buildRowHeaderMenus, cellImageToFloat, defaultInsertCount, ensureContextMenuSelection, isColInSelection, isRowInSelection, resolveRenderSize, wholeColRange, wholeRowRange };
@@ -17,7 +17,7 @@ import { useSheetState } from "./use-sheet-state.js";
17
17
  import { useToolGroups } from "./use-tool-groups.js";
18
18
  import { useToolPopup } from "./use-tool-popup.js";
19
19
  import { computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createVNode, defineComponent, normalizeClass, normalizeStyle, openBlock, ref, shallowRef, toDisplayString, unref, useTemplateRef, watch, withCtx, withModifiers } from "vue";
20
- import { UDropdown, UImagePreviewer } from "@veltra/desktop";
20
+ import { UDropdown, UImagePreviewer, messageConfirm } from "@veltra/desktop";
21
21
  import { bem } from "@veltra/utils";
22
22
  //#region src/components/sheet/sheet.vue
23
23
  const _sfc_main = /*@__PURE__*/ defineComponent({
@@ -178,6 +178,28 @@ const _sfc_main = /*@__PURE__*/ defineComponent({
178
178
  function openImagePreview(source) {
179
179
  imagePreview.value = source;
180
180
  }
181
+ /** 新标签打开外链(对齐引擎默认打开语义:noopener) */
182
+ function openExternal(url) {
183
+ window.open(url, "_blank", "noopener,noreferrer");
184
+ }
185
+ /** 链接格单击:emit 后直开(保持引擎原默认行为) */
186
+ function handleCellLinkClick(event) {
187
+ emit("cell-link-click", event);
188
+ openExternal(event.url);
189
+ }
190
+ /** 图片链接单击(浮动图/嵌入图):emit 后确认再开——点击即跳转易误触 */
191
+ function handleImageLinkClick(event) {
192
+ emit("image-link-click", event);
193
+ messageConfirm({
194
+ title: "打开图片链接",
195
+ message: `即将打开图片链接:${event.url}`,
196
+ confirmButtonText: "打开",
197
+ cancelButtonText: "取消",
198
+ onClose: (action) => {
199
+ if (action === "confirm") openExternal(event.url);
200
+ }
201
+ });
202
+ }
181
203
  const gridRef = useTemplateRef("gridRef");
182
204
  const formulaBarRef = useTemplateRef("formulaBarRef");
183
205
  const { rebuildGrid, activateGrid, pruneCache, getGrid, setSelectionAnchor } = useSheetGrid({
@@ -196,8 +218,8 @@ const _sfc_main = /*@__PURE__*/ defineComponent({
196
218
  emit("cell-link-leave", event);
197
219
  hideCellLinkTooltip(event);
198
220
  },
199
- onCellLinkClick: (event) => emit("cell-link-click", event),
200
- onImageLinkClick: (event) => emit("image-link-click", event),
221
+ onCellLinkClick: (event) => handleCellLinkClick(event),
222
+ onImageLinkClick: (event) => handleImageLinkClick(event),
201
223
  onFloatContextMenu: (event) => emit("float-context-menu", event),
202
224
  openHyperlinkPopup,
203
225
  openImagePreview
@@ -6,6 +6,8 @@ import "@veltra/desktop/components/scroll/style";
6
6
  import "@veltra/desktop/components/tip/style";
7
7
  import "@veltra/desktop/components/loading/style";
8
8
  import "@veltra/desktop/components/message/style";
9
+ import "@veltra/desktop/components/message-confirm/style";
10
+ import "@veltra/desktop/components/image-previewer/style";
9
11
  import "@veltra/desktop/components/input/style";
10
12
  import "@veltra/desktop/components/file-picker/style";
11
13
  import "@veltra/desktop/components/button/style";
@@ -7,6 +7,8 @@ import "@veltra/desktop/components/scroll/style";
7
7
  import "@veltra/desktop/components/tip/style";
8
8
  import "@veltra/desktop/components/loading/style";
9
9
  import "@veltra/desktop/components/message/style";
10
+ import "@veltra/desktop/components/message-confirm/style";
11
+ import "@veltra/desktop/components/image-previewer/style";
10
12
  import "@veltra/desktop/components/input/style";
11
13
  import "@veltra/desktop/components/file-picker/style";
12
14
  import "@veltra/desktop/components/button/style";
@@ -119,6 +119,7 @@ function useSheetGrid(options) {
119
119
  scrollbar: props.scrollbar,
120
120
  growOnScroll: props.growOnScroll,
121
121
  theme: resolveCanvasThemeOverride(host),
122
+ openCellLinkOnClick: false,
122
123
  onContextMenu: handleContextMenu,
123
124
  onEditStart: (addr) => formulaBarRef.value?.mirrorGridEdit(addr),
124
125
  onEditEnd: (addr) => formulaBarRef.value?.exitMirror(addr),
@@ -105,13 +105,14 @@ export interface SheetEmits {
105
105
  (name: 'cell-link-leave', payload: CellLinkLeaveEvent): void;
106
106
  /**
107
107
  * 单元格超链接单击(引擎 `CellLinkClickEvent` 形态:真实指针 down+up 命中
108
- * 同一链接格;框选拖拽/键盘选中不触发)。单击打开仍由引擎
109
- * `openCellLinkOnClick` 默认处理,ultra-ui 不接管
108
+ * 同一链接格;框选拖拽/键盘选中不触发)。组件已接管打开:emit 后直接新标签
109
+ * 打开(引擎 `openCellLinkOnClick` 已关闭)
110
110
  */
111
111
  (name: 'cell-link-click', payload: CellLinkClickEvent): void;
112
112
  /**
113
113
  * 图片链接单击(引擎 `ImageLinkClickEvent`,`kind` 区分 `cell` 嵌入图 /
114
- * `float` 浮动图);默认打开行为同上由引擎处理
114
+ * `float` 浮动图)。组件已接管打开:emit 后弹确认框,确认才新标签打开
115
+ * (防误触跳转;引擎 `openCellLinkOnClick` 已关闭)
115
116
  */
116
117
  (name: 'image-link-click', payload: ImageLinkClickEvent): void;
117
118
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veltra/sheet",
3
- "version": "4.1.0",
3
+ "version": "4.1.1",
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.7"
37
+ "infinitable": "^0.1.8"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "@cat-kit/core": ">=1.2.1",
@@ -2,7 +2,7 @@ import { Sheet } from 'infinitable/sheet'
2
2
  import { describe, expect, it } from 'vite-plus/test'
3
3
 
4
4
  import { createSheetContext } from '../../../tools/context'
5
- import { buildFloatImageMenus } from '../float-image-menu'
5
+ import { buildFloatImageMenus, embedFloatImageToCell } from '../float-image-menu'
6
6
  import type { ImagePreviewSource } from '../popup-helpers'
7
7
 
8
8
  /** 插入一张浮动图(data 字节 + 可选 src/link),返回 id */
@@ -43,14 +43,14 @@ function handlesSpy() {
43
43
  }
44
44
 
45
45
  describe('buildFloatImageMenus(浮动图右键独立菜单)', () => {
46
- it('无链接浮动图:预览 / 设置超链接 / 删除图片(无取消超链接);预览源 src 优先', () => {
46
+ it('无链接浮动图:预览 / 设置超链接 / 嵌入单元格 / 删除图片(无取消超链接);预览源 src 优先', () => {
47
47
  const sheet = new Sheet()
48
48
  const ctx = createSheetContext(sheet)
49
49
  const id = insertFloatImage(sheet, { src: 'https://example.com/a.png' })
50
50
  const spy = handlesSpy()
51
51
 
52
52
  const menus = buildFloatImageMenus(ctx, id, spy.handles)
53
- expect(menus.map((m) => m.label)).toEqual(['预览', '设置超链接', '删除图片'])
53
+ expect(menus.map((m) => m.label)).toEqual(['预览', '设置超链接', '嵌入单元格', '删除图片'])
54
54
 
55
55
  menus.find((m) => m.label === '预览')!.callback!()
56
56
  expect(spy.previews).toEqual([{ src: 'https://example.com/a.png' }])
@@ -78,7 +78,13 @@ describe('buildFloatImageMenus(浮动图右键独立菜单)', () => {
78
78
  const spy = handlesSpy()
79
79
 
80
80
  const menus = buildFloatImageMenus(ctx, id, spy.handles)
81
- expect(menus.map((m) => m.label)).toEqual(['预览', '设置超链接', '取消超链接', '删除图片'])
81
+ expect(menus.map((m) => m.label)).toEqual([
82
+ '预览',
83
+ '设置超链接',
84
+ '取消超链接',
85
+ '嵌入单元格',
86
+ '删除图片'
87
+ ])
82
88
 
83
89
  menus.find((m) => m.label === '设置超链接')!.callback!()
84
90
  expect(spy.popups).toEqual([
@@ -109,7 +115,65 @@ describe('buildFloatImageMenus(浮动图右键独立菜单)', () => {
109
115
  expect(buildFloatImageMenus(ctx, 'missing', handlesSpy().handles)).toEqual([])
110
116
  })
111
117
 
112
- it('readonly:设置/取消/删除禁用、预览可用;句柄缺省:预览/设置禁用', () => {
118
+ it('嵌入单元格:浮动图转锚点格嵌入图(src 优先 / data 字节两形态,link 随迁),单 undo 还原', () => {
119
+ const sheet = new Sheet()
120
+ const ctx = createSheetContext(sheet)
121
+ const addr = { row: 2, col: 3 }
122
+ const id = sheet.insertImage({
123
+ data: new Uint8Array([1, 2, 3]),
124
+ type: 'png',
125
+ anchor: { from: addr },
126
+ src: 'https://example.com/a.png',
127
+ link: 'https://example.com'
128
+ })
129
+
130
+ embedFloatImageToCell(ctx, id)
131
+ // 浮动图消失,锚点格出现同源嵌入图(src 优先于字节),link 随迁
132
+ expect(ctx.getImages().some((img) => img.id === id)).toBe(false)
133
+ expect(ctx.getCellImage(addr)).toEqual({
134
+ src: 'https://example.com/a.png',
135
+ type: 'png',
136
+ link: 'https://example.com'
137
+ })
138
+
139
+ // 事务合并:一次 undo 同时还原浮动图、清掉嵌入图
140
+ expect(ctx.undo()).toBe(true)
141
+ expect(ctx.getImages().some((img) => img.id === id)).toBe(true)
142
+ expect(ctx.hasCellImage(addr)).toBe(false)
143
+ })
144
+
145
+ it('嵌入单元格:data 字节图迁字节;目标格已有嵌入图则覆盖;id 不存在无操作', () => {
146
+ const sheet = new Sheet()
147
+ const ctx = createSheetContext(sheet)
148
+ const addr = { row: 1, col: 1 }
149
+ const id = sheet.insertImage({
150
+ data: new Uint8Array([1, 2, 3]),
151
+ type: 'png',
152
+ anchor: { from: addr }
153
+ })
154
+
155
+ embedFloatImageToCell(ctx, id)
156
+ expect(ctx.getCellImage(addr)).toEqual({ data: new Uint8Array([1, 2, 3]), type: 'png' })
157
+
158
+ // 目标格已有嵌入图 → 覆盖(undo 一次仍回到转换前)
159
+ const other = insertFloatImage(sheet)
160
+ sheet.updateImage(other, { anchor: { from: { row: 5, col: 5 } } })
161
+ embedFloatImageToCell(ctx, other)
162
+ expect(ctx.hasCellImage({ row: 5, col: 5 })).toBe(true)
163
+ expect(ctx.getCellImage({ row: 5, col: 5 })).toEqual({
164
+ data: new Uint8Array([1, 2, 3]),
165
+ type: 'png'
166
+ })
167
+ expect(ctx.undo()).toBe(true)
168
+ expect(ctx.hasCellImage({ row: 5, col: 5 })).toBe(false)
169
+ expect(ctx.getImages().some((img) => img.id === other)).toBe(true)
170
+
171
+ // id 不存在(并发删除):无操作
172
+ embedFloatImageToCell(ctx, 'missing')
173
+ expect(ctx.getImages().some((img) => img.id === other)).toBe(true)
174
+ })
175
+
176
+ it('readonly:设置/取消/删除/嵌入单元格禁用、预览可用;句柄缺省:预览/设置禁用', () => {
113
177
  const sheet = new Sheet()
114
178
  const ctx = createSheetContext(sheet)
115
179
  const id = insertFloatImage(sheet, { link: 'https://example.com' })
@@ -118,13 +182,15 @@ describe('buildFloatImageMenus(浮动图右键独立菜单)', () => {
118
182
  expect(readonly.find((m) => m.label === '预览')?.disabled).toBeFalsy()
119
183
  expect(readonly.find((m) => m.label === '设置超链接')?.disabled).toBe(true)
120
184
  expect(readonly.find((m) => m.label === '取消超链接')?.disabled).toBe(true)
185
+ expect(readonly.find((m) => m.label === '嵌入单元格')?.disabled).toBe(true)
121
186
  expect(readonly.find((m) => m.label === '删除图片')?.disabled).toBe(true)
122
187
 
123
188
  const noHandles = buildFloatImageMenus(ctx, id)
124
189
  expect(noHandles.find((m) => m.label === '预览')?.disabled).toBe(true)
125
190
  expect(noHandles.find((m) => m.label === '设置超链接')?.disabled).toBe(true)
126
- // 取消/删除只依赖 ctx,句柄缺省仍可用
191
+ // 取消/嵌入/删除只依赖 ctx,句柄缺省仍可用
127
192
  expect(noHandles.find((m) => m.label === '取消超链接')?.disabled).toBeFalsy()
193
+ expect(noHandles.find((m) => m.label === '嵌入单元格')?.disabled).toBeFalsy()
128
194
  expect(noHandles.find((m) => m.label === '删除图片')?.disabled).toBeFalsy()
129
195
  })
130
196
  })
@@ -4,8 +4,10 @@ import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
4
4
  import { createSheetContext } from '../../../tools/context'
5
5
  import {
6
6
  INSERT_IMAGE_ACCEPT,
7
+ insertCellImageFromFile,
7
8
  insertImageFromFile,
8
9
  insertImageFromUrl,
10
+ pickAndEmbedCellImage,
9
11
  pickAndInsertImage,
10
12
  resolveImageType
11
13
  } from '../insert-image'
@@ -138,6 +140,64 @@ describe('insertImageFromUrl', () => {
138
140
  })
139
141
  })
140
142
 
143
+ describe('insertCellImageFromFile / pickAndEmbedCellImage', () => {
144
+ it('支持格式 → 嵌入活动格(insertCellImage 字节载荷)', async () => {
145
+ const sheet = new Sheet()
146
+ const ctx = createSheetContext(sheet)
147
+ ctx.selectCell({ row: 3, col: 4 })
148
+ const bytes = [9, 8, 7]
149
+ await insertCellImageFromFile(ctx, makeFile('logo.png', 'image/png', bytes))
150
+ expect(ctx.hasCellImage({ row: 3, col: 4 })).toBe(true)
151
+ const payload = ctx.getCellImage({ row: 3, col: 4 })!
152
+ expect(payload.type).toBe('png')
153
+ expect([...payload.data!]).toEqual(bytes)
154
+ expect(mocks.message.error).not.toHaveBeenCalled()
155
+ })
156
+
157
+ it('格内已有嵌入图 → 整体覆盖替换', async () => {
158
+ const sheet = new Sheet()
159
+ const ctx = createSheetContext(sheet)
160
+ const addr = { row: 1, col: 1 }
161
+ ctx.insertCellImage(addr, { src: 'https://example.com/old.png' })
162
+ ctx.selectCell(addr)
163
+ await insertCellImageFromFile(ctx, makeFile('new.gif', 'image/gif', [1, 2]))
164
+ const payload = ctx.getCellImage(addr)!
165
+ expect(payload.src).toBeUndefined()
166
+ expect(payload.type).toBe('gif')
167
+ expect([...payload.data!]).toEqual([1, 2])
168
+ })
169
+
170
+ it('不支持格式 → message.error,不写入', async () => {
171
+ const sheet = new Sheet()
172
+ const ctx = createSheetContext(sheet)
173
+ ctx.selectCell({ row: 0, col: 0 })
174
+ await insertCellImageFromFile(ctx, makeFile('x.bmp', 'image/bmp'))
175
+ expect(ctx.hasCellImage({ row: 0, col: 0 })).toBe(false)
176
+ expect(mocks.message.error).toHaveBeenCalledWith(expect.stringContaining('不支持的图片格式'))
177
+ })
178
+
179
+ it('无活动格 → message.error,不写入', async () => {
180
+ const sheet = new Sheet()
181
+ const ctx = createSheetContext(sheet)
182
+ sheet.selection.clear()
183
+ await insertCellImageFromFile(ctx, makeFile('a.png', 'image/png'))
184
+ expect(mocks.message.error).toHaveBeenCalledWith('请先选择单元格')
185
+ })
186
+
187
+ it('pickAndEmbedCellImage:创建隐藏 file input(同一 accept)并 click', () => {
188
+ const sheet = new Sheet()
189
+ const ctx = createSheetContext(sheet)
190
+ const click = vi.spyOn(HTMLInputElement.prototype, 'click').mockImplementation(() => {})
191
+ pickAndEmbedCellImage(ctx)
192
+ const input = document.body.querySelector<HTMLInputElement>('input[type="file"]')
193
+ expect(input).toBeTruthy()
194
+ expect(input!.accept).toBe(INSERT_IMAGE_ACCEPT)
195
+ expect(input!.hidden).toBe(true)
196
+ expect(click).toHaveBeenCalled()
197
+ click.mockRestore()
198
+ })
199
+ })
200
+
141
201
  describe('pickAndInsertImage', () => {
142
202
  it('创建隐藏 file input(同一 accept)并 click', () => {
143
203
  const sheet = new Sheet()
@@ -1288,6 +1288,8 @@ describe('USheet 引擎链接 / 图片事件透传', () => {
1288
1288
  const leave: unknown[] = []
1289
1289
  const click: unknown[] = []
1290
1290
  const imageClick: unknown[] = []
1291
+ // 链接打开已由组件接管(直开 / 确认后开),stub 掉 window.open 隔离浏览器行为
1292
+ const openSpy = vi.spyOn(window, 'open').mockImplementation(() => null)
1291
1293
  mount(
1292
1294
  () => ({
1293
1295
  workbook,
@@ -1329,6 +1331,8 @@ describe('USheet 引擎链接 / 图片事件透传', () => {
1329
1331
  }
1330
1332
  for (const listener of table.cellLinkClickListeners) listener(clickEvent)
1331
1333
  expect(click).toEqual([clickEvent])
1334
+ // 单元格链接:组件接管直开(保持引擎原默认行为)
1335
+ expect(openSpy).toHaveBeenCalledWith('https://example.com', '_blank', 'noopener,noreferrer')
1332
1336
 
1333
1337
  // 图片链接点击:kind 区分嵌入图(cell)与浮动图(float)
1334
1338
  const cellImageLink = {
@@ -1349,6 +1353,58 @@ describe('USheet 引擎链接 / 图片事件透传', () => {
1349
1353
  listener(floatImageLink)
1350
1354
  }
1351
1355
  expect(imageClick).toEqual([cellImageLink, floatImageLink])
1356
+ openSpy.mockRestore()
1357
+ })
1358
+
1359
+ it('图片链接单击不直接打开:弹确认框,确认后 window.open、取消不打开', async () => {
1360
+ const workbook = createWorkbook()
1361
+ const exposed: { value: SheetExposed | undefined } = { value: undefined }
1362
+ mount(() => ({ workbook, rows: 10, cols: 6 }), exposed)
1363
+ await nextTick()
1364
+
1365
+ const openSpy = vi.spyOn(window, 'open').mockImplementation(() => null)
1366
+ const table = exposed.value!.getGrid()!.getTable()
1367
+ const event = {
1368
+ kind: 'float' as const,
1369
+ id: 'img-1',
1370
+ url: 'https://example.com/float.png',
1371
+ originalEvent: new MouseEvent('pointerup')
1372
+ }
1373
+ // happy-dom 无 transition:关闭动画不结束,旧确认框残留在 body,断言只认最后一份
1374
+ const latestConfirmBox = (): HTMLElement => {
1375
+ const boxes = document.body.querySelectorAll<HTMLElement>('.u-message-confirm')
1376
+ return boxes[boxes.length - 1]!
1377
+ }
1378
+ const confirmButtons = (): HTMLButtonElement[] => [
1379
+ ...latestConfirmBox().querySelectorAll<HTMLButtonElement>('.u-message-confirm__footer button')
1380
+ ]
1381
+
1382
+ // 点击 → 不直接打开,弹确认框
1383
+ for (const listener of table.imageLinkClickListeners) listener(event)
1384
+ await nextTick()
1385
+ expect(openSpy).not.toHaveBeenCalled()
1386
+ expect(latestConfirmBox().textContent).toContain('https://example.com/float.png')
1387
+
1388
+ // 取消 → 不打开
1389
+ confirmButtons()
1390
+ .find((btn) => btn.textContent?.includes('取消'))!
1391
+ .click()
1392
+ await nextTick()
1393
+ expect(openSpy).not.toHaveBeenCalled()
1394
+
1395
+ // 再点 → 确认「打开」→ window.open 新标签
1396
+ for (const listener of table.imageLinkClickListeners) listener(event)
1397
+ await nextTick()
1398
+ confirmButtons()
1399
+ .find((btn) => btn.textContent?.includes('打开'))!
1400
+ .click()
1401
+ await nextTick()
1402
+ expect(openSpy).toHaveBeenCalledWith(
1403
+ 'https://example.com/float.png',
1404
+ '_blank',
1405
+ 'noopener,noreferrer'
1406
+ )
1407
+ openSpy.mockRestore()
1352
1408
  })
1353
1409
 
1354
1410
  it('浮动图右键:引擎 floatObjects.onContextMenu → float-context-menu emit(层坐标 + 页面坐标)', async () => {
@@ -1539,7 +1595,7 @@ describe('USheet 图片 UI(浮动图右键独立菜单 / 嵌入图 body 菜单
1539
1595
  return document.body.querySelector<HTMLElement>('.u-image-previewer')
1540
1596
  }
1541
1597
 
1542
- it('浮动图右键弹独立菜单:预览 / 设置超链接 / 删除图片;无链接无取消项;不弹 body 菜单', async () => {
1598
+ it('浮动图右键弹独立菜单:预览 / 设置超链接 / 嵌入单元格 / 删除图片;无链接无取消项;不弹 body 菜单', async () => {
1543
1599
  const workbook = createWorkbook()
1544
1600
  const exposed: { value: SheetExposed | undefined } = { value: undefined }
1545
1601
  mount(() => ({ workbook, rows: 10, cols: 6 }), exposed)
@@ -1551,6 +1607,7 @@ describe('USheet 图片 UI(浮动图右键独立菜单 / 嵌入图 body 菜单
1551
1607
 
1552
1608
  expect(menuItem('预览')!.classList.contains('is-disabled')).toBe(false)
1553
1609
  expect(menuItem('设置超链接')).toBeTruthy()
1610
+ expect(menuItem('嵌入单元格')).toBeTruthy()
1554
1611
  expect(menuItem('删除图片')).toBeTruthy()
1555
1612
  expect(menuItem('取消超链接')).toBeUndefined()
1556
1613
  // 独立菜单:最新弹层不含 body 菜单项(引擎保证右键不穿透到格)
@@ -1599,7 +1656,7 @@ describe('USheet 图片 UI(浮动图右键独立菜单 / 嵌入图 body 菜单
1599
1656
  expect(ctx.getImages().some((img) => img.id === id)).toBe(true)
1600
1657
  })
1601
1658
 
1602
- it('嵌入图 body 菜单四项:预览 / 图片超链接 / 取消超链接 / 移除图片;移除可 undo、link 覆盖写', async () => {
1659
+ it('嵌入图 body 菜单:纯图片项(预览 / 图片超链接 / 取消超链接 / 转浮动 / 替换 / 移除),无普通格项;移除可 undo、link 覆盖写', async () => {
1603
1660
  const workbook = createWorkbook()
1604
1661
  const exposed: { value: SheetExposed | undefined } = { value: undefined }
1605
1662
  mount(() => ({ workbook, rows: 10, cols: 6 }), exposed)
@@ -1609,13 +1666,21 @@ describe('USheet 图片 UI(浮动图右键独立菜单 / 嵌入图 body 菜单
1609
1666
  ctx.insertCellImage(addr, { src: 'https://example.com/cell.png' })
1610
1667
  ctx.selectCell(addr)
1611
1668
 
1612
- // 无链接:预览 / 图片超链接 / 移除图片,无取消项
1669
+ // 无链接:预览 / 图片超链接 / 替换图片 / 移除图片,无取消项;
1670
+ // 不出合并 / 数据格式 / 超链接 / 插入图片等普通格项(右键语义是图片)
1613
1671
  popBodyContextMenu(exposed, 0, 0)
1614
1672
  await nextTick()
1615
1673
  expect(menuItem('预览')).toBeTruthy()
1616
1674
  expect(menuItem('图片超链接')).toBeTruthy()
1675
+ expect(menuItem('转为浮动图片')).toBeTruthy()
1676
+ expect(menuItem('替换图片')).toBeTruthy()
1617
1677
  expect(menuItem('移除图片')).toBeTruthy()
1618
1678
  expect(menuItem('取消超链接')).toBeUndefined()
1679
+ expect(menuItem('合并单元格')).toBeUndefined()
1680
+ expect(menuItem('设置数据格式')).toBeUndefined()
1681
+ expect(menuItem('插入超链接')).toBeUndefined()
1682
+ expect(menuItem('插入图片')).toBeUndefined()
1683
+ expect(menuItem('嵌入图片')).toBeUndefined()
1619
1684
 
1620
1685
  // 图片超链接 → 弹窗确认 → 覆盖写 link(src 保留)
1621
1686
  menuItem('图片超链接')!.click()
@@ -8,6 +8,7 @@ import {
8
8
  buildBodyMenus,
9
9
  buildColHeaderMenus,
10
10
  buildRowHeaderMenus,
11
+ cellImageToFloat,
11
12
  defaultInsertCount,
12
13
  ensureContextMenuSelection,
13
14
  isColInSelection,
@@ -220,23 +221,27 @@ describe('build*Menus', () => {
220
221
  expect(readonlyCol.render).toBeUndefined()
221
222
  })
222
223
 
223
- it('body 菜单:合并/取消合并 + 设置数据格式子菜单 + 插入超链接 + 插入图片,不含行列插入删除', () => {
224
+ it('body 菜单:合并/取消合并 + 设置数据格式子菜单 + 插入超链接 + 插入/嵌入图片,不含行列插入删除', () => {
224
225
  const sheet = new Sheet()
225
226
  const ctx = createSheetContext(sheet)
226
227
  sheet.selectCell({ row: 0, col: 0 })
227
228
  const menus = buildBodyMenus(ctx)
228
- expect(menus).toHaveLength(6)
229
+ expect(menus).toHaveLength(7)
229
230
  expect(menus.map((m) => m.label ?? (m.divider ? '---' : ''))).toEqual([
230
231
  '合并单元格',
231
232
  '取消合并单元格',
232
233
  '---',
233
234
  '设置数据格式',
234
235
  '插入超链接',
235
- '插入图片'
236
+ '插入图片',
237
+ '嵌入图片'
236
238
  ])
237
239
  const insertImage = menus.find((m) => m.label === '插入图片')!
238
240
  expect(insertImage.disabled).toBeFalsy()
239
241
  expect(insertImage.callback).toBeTruthy()
242
+ const embedImage = menus.find((m) => m.label === '嵌入图片')!
243
+ expect(embedImage.disabled).toBeFalsy()
244
+ expect(embedImage.callback).toBeTruthy()
240
245
  const labels = menus.map((m) => m.label).join()
241
246
  expect(labels).not.toContain('插入行')
242
247
  expect(labels).not.toContain('插入列')
@@ -323,12 +328,13 @@ describe('build*Menus', () => {
323
328
  expect(ctx.canUndo).toBe(false)
324
329
  })
325
330
 
326
- it('body 插入图片 / 设置数据格式:无活动格时禁用', () => {
331
+ it('body 插入图片 / 嵌入图片 / 设置数据格式:无活动格时禁用', () => {
327
332
  const sheet = new Sheet()
328
333
  const ctx = createSheetContext(sheet)
329
334
  sheet.selection.clear()
330
335
  const menus = buildBodyMenus(ctx)
331
336
  expect(menus.find((m) => m.label === '插入图片')?.disabled).toBe(true)
337
+ expect(menus.find((m) => m.label === '嵌入图片')?.disabled).toBe(true)
332
338
  expect(menus.find((m) => m.label === '设置数据格式')?.disabled).toBe(true)
333
339
  // 无活动格:超链接项同样禁用
334
340
  expect(menus.find((m) => m.label === '插入超链接')?.disabled).toBe(true)
@@ -468,18 +474,26 @@ describe('buildBodyMenus 嵌入图项(按目标格嵌入图状态出项)', (
468
474
  return { previews, open: (source: unknown): void => previews.push(source) }
469
475
  }
470
476
 
471
- it('无嵌入图:不出现图片项', () => {
477
+ it('无嵌入图:不出现图片项(普通格菜单 + 嵌入图片入口)', () => {
472
478
  const sheet = new Sheet()
473
479
  const ctx = createSheetContext(sheet)
474
480
  sheet.selectCell(ADDR)
475
481
  const labels = buildBodyMenus(ctx).map((m) => m.label ?? (m.divider ? '---' : ''))
476
- expect(labels).toHaveLength(6)
477
- for (const label of ['预览', '图片超链接', '取消超链接', '移除图片']) {
482
+ expect(labels).toHaveLength(7)
483
+ expect(labels).toContain('嵌入图片')
484
+ for (const label of [
485
+ '预览',
486
+ '图片超链接',
487
+ '取消超链接',
488
+ '转为浮动图片',
489
+ '替换图片',
490
+ '移除图片'
491
+ ]) {
478
492
  expect(labels).not.toContain(label)
479
493
  }
480
494
  })
481
495
 
482
- it('无链接嵌入图:divider + 预览 / 图片超链接 / 移除图片(无取消超链接)', () => {
496
+ it('嵌入图格出纯图片菜单:不含合并/数据格式/超链接/插入图片等普通格项(无链接)', () => {
483
497
  const sheet = new Sheet()
484
498
  const ctx = createSheetContext(sheet)
485
499
  ctx.insertCellImage(ADDR, { src: 'https://example.com/a.png' })
@@ -491,18 +505,23 @@ describe('buildBodyMenus 嵌入图项(按目标格嵌入图状态出项)', (
491
505
  openHyperlinkPopup: popup.open,
492
506
  openImagePreview: preview.open
493
507
  })
494
- expect(menus.map((m) => m.label ?? (m.divider ? '---' : ''))).toEqual([
508
+ expect(menus.map((m) => m.label)).toEqual([
509
+ '预览',
510
+ '图片超链接',
511
+ '转为浮动图片',
512
+ '替换图片',
513
+ '移除图片'
514
+ ])
515
+ for (const label of [
495
516
  '合并单元格',
496
517
  '取消合并单元格',
497
- '---',
498
518
  '设置数据格式',
499
519
  '插入超链接',
500
520
  '插入图片',
501
- '---',
502
- '预览',
503
- '图片超链接',
504
- '移除图片'
505
- ])
521
+ '嵌入图片'
522
+ ]) {
523
+ expect(menus.find((m) => m.label === label)).toBeUndefined()
524
+ }
506
525
 
507
526
  menus.find((m) => m.label === '预览')!.callback!()
508
527
  expect(preview.previews).toEqual([{ src: 'https://example.com/a.png' }])
@@ -520,7 +539,14 @@ describe('buildBodyMenus 嵌入图项(按目标格嵌入图状态出项)', (
520
539
  const popup = popupSpy()
521
540
 
522
541
  const menus = buildBodyMenus(ctx, { openHyperlinkPopup: popup.open })
523
- expect(menus.map((m) => m.label)).toContain('取消超链接')
542
+ expect(menus.map((m) => m.label)).toEqual([
543
+ '预览',
544
+ '图片超链接',
545
+ '取消超链接',
546
+ '转为浮动图片',
547
+ '替换图片',
548
+ '移除图片'
549
+ ])
524
550
 
525
551
  menus.find((m) => m.label === '图片超链接')!.callback!()
526
552
  expect(popup.calls).toEqual([
@@ -552,7 +578,57 @@ describe('buildBodyMenus 嵌入图项(按目标格嵌入图状态出项)', (
552
578
  expect(ctx.hasCellImage(ADDR)).toBe(true)
553
579
  })
554
580
 
555
- it('readonly:图片超链接/取消超链接/移除图片禁用、预览可用;句柄缺省:预览/图片超链接禁用', () => {
581
+ it('转为浮动图片:锚点格落浮动图(格宽高定尺寸、contain 保比例),link 随迁,单 undo 还原', () => {
582
+ const sheet = new Sheet()
583
+ const ctx = createSheetContext(sheet)
584
+ sheet.setRowHeight(ADDR.row, 64)
585
+ sheet.setColWidth(ADDR.col, 120)
586
+ ctx.insertCellImage(ADDR, {
587
+ src: 'https://example.com/a.png',
588
+ type: 'png',
589
+ link: 'https://example.com'
590
+ })
591
+ sheet.selectCell(ADDR)
592
+
593
+ buildBodyMenus(ctx).find((m) => m.label === '转为浮动图片')!.callback!()
594
+ // 嵌入图消失,浮动图落在锚点格:尺寸取格宽高,src/link 随迁,fit 对齐嵌入图缺省 contain
595
+ expect(ctx.hasCellImage(ADDR)).toBe(false)
596
+ const floats = ctx.getImages()
597
+ expect(floats).toHaveLength(1)
598
+ expect(floats[0]?.anchor.from).toEqual(ADDR)
599
+ expect(floats[0]?.width).toBe(120)
600
+ expect(floats[0]?.height).toBe(64)
601
+ expect(floats[0]?.fit).toBe('contain')
602
+ expect(floats[0]?.src).toBe('https://example.com/a.png')
603
+ expect(floats[0]?.link).toBe('https://example.com')
604
+
605
+ // 事务合并:一次 undo 同时还原嵌入图、清掉浮动图
606
+ expect(ctx.undo()).toBe(true)
607
+ expect(ctx.hasCellImage(ADDR)).toBe(true)
608
+ expect(ctx.getImages()).toHaveLength(0)
609
+ })
610
+
611
+ it('转为浮动图片:data 字节图迁字节;无嵌入图无操作', () => {
612
+ const sheet = new Sheet()
613
+ const ctx = createSheetContext(sheet)
614
+ const addr = { row: 3, col: 4 }
615
+ ctx.insertCellImage(addr, { data: new Uint8Array([7, 8]), type: 'gif' })
616
+
617
+ cellImageToFloat(ctx, addr)
618
+ const floats = ctx.getImages()
619
+ expect(floats).toHaveLength(1)
620
+ expect([...floats[0]!.data]).toEqual([7, 8])
621
+ expect(floats[0]!.type).toBe('gif')
622
+ expect(floats[0]!.src).toBeUndefined()
623
+
624
+ // 无嵌入图:无操作(不产生浮动图、不新增 undo 单元)
625
+ const undoSizeBefore = sheet.history.undoSize
626
+ cellImageToFloat(ctx, { row: 0, col: 0 })
627
+ expect(ctx.getImages()).toHaveLength(1)
628
+ expect(sheet.history.undoSize).toBe(undoSizeBefore)
629
+ })
630
+
631
+ it('readonly:图片超链接/取消超链接/转浮动/替换/移除禁用、预览可用;句柄缺省:预览/图片超链接禁用', () => {
556
632
  const sheet = new Sheet()
557
633
  const ctx = createSheetContext(sheet)
558
634
  ctx.insertCellImage(ADDR, { src: 'https://example.com/a.png', link: 'https://e.com' })
@@ -566,11 +642,14 @@ describe('buildBodyMenus 嵌入图项(按目标格嵌入图状态出项)', (
566
642
  expect(readonly.find((m) => m.label === '预览')?.disabled).toBeFalsy()
567
643
  expect(readonly.find((m) => m.label === '图片超链接')?.disabled).toBe(true)
568
644
  expect(readonly.find((m) => m.label === '取消超链接')?.disabled).toBe(true)
645
+ expect(readonly.find((m) => m.label === '转为浮动图片')?.disabled).toBe(true)
646
+ expect(readonly.find((m) => m.label === '替换图片')?.disabled).toBe(true)
569
647
  expect(readonly.find((m) => m.label === '移除图片')?.disabled).toBe(true)
570
648
 
571
649
  const noHandles = buildBodyMenus(ctx)
572
650
  expect(noHandles.find((m) => m.label === '预览')?.disabled).toBe(true)
573
651
  expect(noHandles.find((m) => m.label === '图片超链接')?.disabled).toBe(true)
652
+ expect(noHandles.find((m) => m.label === '转为浮动图片')?.disabled).toBeFalsy()
574
653
  expect(noHandles.find((m) => m.label === '移除图片')?.disabled).toBeFalsy()
575
654
  })
576
655
  })
@@ -1,5 +1,6 @@
1
1
  import type { ContextmenuItem } from '@veltra/desktop'
2
2
  import { contextmenu } from '@veltra/desktop'
3
+ import type { CellImagePayload } from 'infinitable/sheet'
3
4
 
4
5
  import type { SheetContext } from '../../tools/context'
5
6
  import type { FloatContextMenuPayload } from '../../types'
@@ -15,10 +16,30 @@ export interface FloatImageMenuHandles {
15
16
  }
16
17
 
17
18
  /**
18
- * 浮动图右键独立菜单:预览 / 设置超链接 / 取消超链接(无链接不出现)/ 删除图片。
19
- * 设置走 hyperlink 弹窗 image 模式(确认 `updateImage(id, { link })`);取消直写
20
- * `updateImage(id, { link: null })`(null 清除,可 undo);删除走 `removeImage`
21
- * (命令系统,可 undo)。命中 id 不存在(并发删除)返回空数组,调用方不弹菜单。
19
+ * 浮动图转为按格嵌入图(对齐 WPS/Excel「放置在单元格中」):落点为图片锚点格
20
+ * (anchor.from,即图片左上角所在格),src/data/type/link 随载荷迁移。事务内
21
+ * insertCellImage + removeImage——一次转换 = 一个 undo 单元。
22
+ */
23
+ export function embedFloatImageToCell(ctx: SheetContext, id: string): void {
24
+ const image = ctx.getImages().find((item) => item.id === id)
25
+ if (!image) return
26
+ const payload: CellImagePayload = image.src
27
+ ? { src: image.src, type: image.type }
28
+ : { data: image.data, type: image.type }
29
+ if (image.link) payload.link = image.link
30
+ ctx.beginTransaction()
31
+ ctx.insertCellImage(image.anchor.from, payload)
32
+ ctx.removeImage(id)
33
+ ctx.commit()
34
+ }
35
+
36
+ /**
37
+ * 浮动图右键独立菜单:预览 / 设置超链接 / 取消超链接(无链接不出现)/
38
+ * 嵌入单元格 / 删除图片。设置走 hyperlink 弹窗 image 模式(confirm
39
+ * `updateImage(id, { link })`);取消直写 `updateImage(id, { link: null })`
40
+ * (null 清除,可 undo);嵌入单元格走 `embedFloatImageToCell`(事务合并单 undo);
41
+ * 删除走 `removeImage`(命令系统,可 undo)。命中 id 不存在(并发删除)返回空数组,
42
+ * 调用方不弹菜单。
22
43
  */
23
44
  export function buildFloatImageMenus(
24
45
  ctx: SheetContext,
@@ -50,6 +71,7 @@ export function buildFloatImageMenus(
50
71
  }
51
72
  ]
52
73
  : []),
74
+ { label: '嵌入单元格', disabled: readonly, callback: () => embedFloatImageToCell(ctx, id) },
53
75
  { label: '删除图片', disabled: readonly, callback: () => ctx.removeImage(id) }
54
76
  ]
55
77
  return items
@@ -88,9 +88,9 @@ export function insertImageFromUrl(ctx: SheetContext, url: string): string | und
88
88
 
89
89
  /**
90
90
  * 拉起系统文件选择框(右键菜单用;不用 UFilePicker——无编程式打开 API)。
91
- * 选中文件后走 insertImageFromFile。
91
+ * 选中文件后交给 onPick;取消选择时清理节点。
92
92
  */
93
- export function pickAndInsertImage(ctx: SheetContext): void {
93
+ function pickImageFile(onPick: (file: File) => void): void {
94
94
  const input = document.createElement('input')
95
95
  input.type = 'file'
96
96
  input.accept = INSERT_IMAGE_ACCEPT
@@ -101,7 +101,7 @@ export function pickAndInsertImage(ctx: SheetContext): void {
101
101
  input.addEventListener('change', () => {
102
102
  const file = input.files?.[0]
103
103
  cleanup()
104
- if (file) void insertImageFromFile(ctx, file)
104
+ if (file) onPick(file)
105
105
  })
106
106
  // 取消选择时也清理节点(focus 回窗口后延迟判定)
107
107
  window.addEventListener(
@@ -116,3 +116,32 @@ export function pickAndInsertImage(ctx: SheetContext): void {
116
116
  document.body.appendChild(input)
117
117
  input.click()
118
118
  }
119
+
120
+ /** 拉起系统文件选择框,选中后插入浮动图片 */
121
+ export function pickAndInsertImage(ctx: SheetContext): void {
122
+ pickImageFile((file) => void insertImageFromFile(ctx, file))
123
+ }
124
+
125
+ /**
126
+ * 从本地 File 嵌入图片到活动格:读字节 → 映射类型 → ctx.insertCellImage
127
+ * (格内已有嵌入图时整体覆盖)。不支持的格式 / 无活动格时 message.error 提示。
128
+ */
129
+ export async function insertCellImageFromFile(ctx: SheetContext, file: File): Promise<void> {
130
+ const type = resolveImageType(file)
131
+ if (!type) {
132
+ message.error('不支持的图片格式,请选择 png / jpeg / gif / svg / webp')
133
+ return
134
+ }
135
+ const active = ctx.getSelection().activeCell
136
+ if (!active) {
137
+ message.error('请先选择单元格')
138
+ return
139
+ }
140
+ const buffer = await file.arrayBuffer()
141
+ ctx.insertCellImage(active, { data: new Uint8Array(buffer), type })
142
+ }
143
+
144
+ /** 拉起系统文件选择框,选中后嵌入图片到活动格(已有嵌入图时覆盖替换) */
145
+ export function pickAndEmbedCellImage(ctx: SheetContext): void {
146
+ pickImageFile((file) => void insertCellImageFromFile(ctx, file))
147
+ }
@@ -1,13 +1,13 @@
1
1
  import type { ContextmenuItem } from '@veltra/desktop'
2
2
  import { createRange, type CellAddress, type CellRange } from 'infinitable/sheet'
3
- import type { NumFmt } from 'infinitable/sheet'
3
+ import type { ImageInput, NumFmt } from 'infinitable/sheet'
4
4
  import { defineComponent, h } from 'vue'
5
5
 
6
6
  import type { SheetContext } from '../../tools/context'
7
7
  import { MIN_ROW_COL_SIZE } from '../../tools/context'
8
8
  import { defaultToolRegistry } from '../../tools/registry'
9
9
  import InsertCountMenuItem from './insert-count-menu-item.vue'
10
- import { pickAndInsertImage } from './insert-image'
10
+ import { pickAndEmbedCellImage, pickAndInsertImage } from './insert-image'
11
11
  import type { ImagePreviewSource, OpenHyperlinkPopup } from './popup-helpers'
12
12
  import { imageLinkInitial, imagePreviewSource } from './popup-helpers'
13
13
 
@@ -303,11 +303,37 @@ function cellLinkMenuItems(
303
303
  }
304
304
 
305
305
  /**
306
- * body 菜单嵌入图项:目标格含嵌入图时附加——预览 / 图片超链接 / 取消超链接
307
- * (按载荷 link 状态出项)/ 移除图片。设置走 hyperlink 弹窗 image 模式(确认后
308
- * 由宿主覆盖写 `insertCellImage(addr, { ...payload, link })`);取消直写覆盖
309
- * `link: undefined`(引擎 CellImagePayload 清除约定,其余字段保留);移除走
310
- * `removeCellImage`(命令系统,可 undo)。无嵌入图返回空数组(不出现图片项)。
306
+ * 按格嵌入图转为浮动图片(对齐 Excel「放置到单元格上」/ WPS 反向切换):浮动图
307
+ * 落在嵌入图锚点格、尺寸取该格宽高(视觉等价还原),fit 缺省 contain(对齐嵌入图
308
+ * 渲染约定,避免浮动图缺省 fill 拉伸变形),src/data/type/link 随载荷迁移。事务内
309
+ * insertImage + removeCellImage——一次转换 = 一个 undo 单元。
310
+ */
311
+ export function cellImageToFloat(ctx: SheetContext, addr: CellAddress): void {
312
+ const payload = ctx.getCellImage(addr)
313
+ if (!payload) return
314
+ const anchor = ctx.getCellInfo(addr).anchor
315
+ const input: ImageInput = {
316
+ data: payload.data ?? new Uint8Array(),
317
+ type: payload.type ?? 'png',
318
+ anchor: { from: { row: anchor.row, col: anchor.col } },
319
+ width: ctx.getColWidth(anchor.col) ?? FALLBACK_COL_WIDTH,
320
+ height: ctx.getRowHeight(anchor.row) ?? FALLBACK_ROW_HEIGHT,
321
+ fit: payload.fit ?? 'contain'
322
+ }
323
+ if (payload.src) input.src = payload.src
324
+ if (payload.link) input.link = payload.link
325
+ ctx.beginTransaction()
326
+ ctx.insertImage(input)
327
+ ctx.removeCellImage(addr)
328
+ ctx.commit()
329
+ }
330
+
331
+ /**
332
+ * body 菜单嵌入图项:预览 / 图片超链接 / 取消超链接(按载荷 link 状态出项)/
333
+ * 转为浮动图片 / 替换图片 / 移除图片。设置走 hyperlink 弹窗 image 模式(确认后由宿主覆盖写
334
+ * `insertCellImage(addr, { ...payload, link })`);取消直写覆盖 `link: undefined`
335
+ * (引擎 CellImagePayload 清除约定,其余字段保留);转换/替换/移除分别走
336
+ * `cellImageToFloat`、`insertCellImage` 覆盖与 `removeCellImage`(命令系统,可 undo)。
311
337
  */
312
338
  function cellImageMenuItems(
313
339
  ctx: SheetContext,
@@ -339,16 +365,26 @@ function cellImageMenuItems(
339
365
  callback: () => ctx.insertCellImage(active, { ...payload, link: undefined })
340
366
  })
341
367
  }
342
- items.push({ label: '移除图片', disabled: readonly, callback: () => ctx.removeCellImage(active) })
368
+ items.push(
369
+ { label: '转为浮动图片', disabled: readonly, callback: () => cellImageToFloat(ctx, active) },
370
+ { label: '替换图片', disabled: readonly, callback: () => pickAndEmbedCellImage(ctx) },
371
+ { label: '移除图片', disabled: readonly, callback: () => ctx.removeCellImage(active) }
372
+ )
343
373
  return items
344
374
  }
345
375
 
346
- /** body 格右键菜单:合并/取消合并 + 设置数据格式 + 超链接 + 插入图片(+ 嵌入图四项;行列插入/删除仅行号/列头菜单) */
376
+ /**
377
+ * body 格右键菜单。目标格含嵌入图时出**纯图片菜单**(预览 / 超链接 / 转浮动 /
378
+ * 替换 / 移除)——图片盖住整格,右键语义是「图片」而非「单元格」,不出合并 /
379
+ * 数据格式 / 超链接等普通格项(对齐 Excel 图片右键约定)。普通格:合并 / 取消合并 +
380
+ * 设置数据格式 + 超链接 + 插入图片(浮动)+ 嵌入图片(入格);行列插入/删除仅行号/列头菜单。
381
+ */
347
382
  export function buildBodyMenus(ctx: SheetContext, options?: BodyMenuOptions): ContextmenuItem[] {
383
+ const active = ctx.getSelection().activeCell
384
+ if (active && ctx.hasCellImage(active)) return cellImageMenuItems(ctx, active, options)
385
+
348
386
  const mergeTool = defaultToolRegistry.get('merge')
349
387
  const unmergeTool = defaultToolRegistry.get('unmerge')
350
- const active = ctx.getSelection().activeCell
351
- const imageItems = cellImageMenuItems(ctx, active, options)
352
388
 
353
389
  return [
354
390
  {
@@ -385,7 +421,7 @@ export function buildBodyMenus(ctx: SheetContext, options?: BodyMenuOptions): Co
385
421
  },
386
422
  ...cellLinkMenuItems(ctx, active, options),
387
423
  { label: '插入图片', disabled: !active, callback: () => pickAndInsertImage(ctx) },
388
- ...(imageItems.length > 0 ? [{ divider: true } as const, ...imageItems] : [])
424
+ { label: '嵌入图片', disabled: !active, callback: () => pickAndEmbedCellImage(ctx) }
389
425
  ]
390
426
  }
391
427
 
@@ -105,9 +105,14 @@
105
105
  </template>
106
106
 
107
107
  <script lang="ts" setup>
108
- import { UDropdown, UImagePreviewer } from '@veltra/desktop'
108
+ import { UDropdown, UImagePreviewer, messageConfirm } from '@veltra/desktop'
109
109
  import { bem } from '@veltra/utils'
110
- import type { CellAddress, CellLinkPayload } from 'infinitable/sheet'
110
+ import type {
111
+ CellAddress,
112
+ CellLinkClickEvent,
113
+ CellLinkPayload,
114
+ ImageLinkClickEvent
115
+ } from 'infinitable/sheet'
111
116
  import { computed, ref, shallowRef, useTemplateRef, watch } from 'vue'
112
117
 
113
118
  import type { SheetTool } from '../../tools/registry'
@@ -311,6 +316,33 @@ function openImagePreview(source: ImagePreviewSource): void {
311
316
  imagePreview.value = source
312
317
  }
313
318
 
319
+ // ─── 链接打开(引擎 openCellLinkOnClick=false,打开行为由组件接管)────────
320
+
321
+ /** 新标签打开外链(对齐引擎默认打开语义:noopener) */
322
+ function openExternal(url: string): void {
323
+ window.open(url, '_blank', 'noopener,noreferrer')
324
+ }
325
+
326
+ /** 链接格单击:emit 后直开(保持引擎原默认行为) */
327
+ function handleCellLinkClick(event: CellLinkClickEvent): void {
328
+ emit('cell-link-click', event)
329
+ openExternal(event.url)
330
+ }
331
+
332
+ /** 图片链接单击(浮动图/嵌入图):emit 后确认再开——点击即跳转易误触 */
333
+ function handleImageLinkClick(event: ImageLinkClickEvent): void {
334
+ emit('image-link-click', event)
335
+ messageConfirm({
336
+ title: '打开图片链接',
337
+ message: `即将打开图片链接:${event.url}`,
338
+ confirmButtonText: '打开',
339
+ cancelButtonText: '取消',
340
+ onClose: (action) => {
341
+ if (action === 'confirm') openExternal(event.url)
342
+ }
343
+ })
344
+ }
345
+
314
346
  // ─── grid 生命周期(重建 / 右键菜单 / 公式栏编辑镜像)────────────────
315
347
 
316
348
  const gridRef = useTemplateRef<HTMLElement>('gridRef')
@@ -325,7 +357,7 @@ const { rebuildGrid, activateGrid, pruneCache, getGrid, setSelectionAnchor } = u
325
357
  // 工具栏「函数」弹框打开期间与 fx 入口一致:画布点击不清除目标格高亮
326
358
  isFunctionPopupOpen: () => popupTool.value?.popup === 'functions',
327
359
  onColResizeEnd: (event) => emit('col-resize-end', event),
328
- // 引擎链接 / 浮动图事件透传(打开行为仍归引擎 openCellLinkOnClick 默认);
360
+ // 引擎链接 / 浮动图事件透传(openCellLinkOnClick=false:链接格直开、图片链接确认后开);
329
361
  // hover/leave 同时喂给 tooltip 状态机(与组件 emit 同源)
330
362
  onCellLinkHover: (event) => {
331
363
  emit('cell-link-hover', event)
@@ -335,8 +367,8 @@ const { rebuildGrid, activateGrid, pruneCache, getGrid, setSelectionAnchor } = u
335
367
  emit('cell-link-leave', event)
336
368
  hideCellLinkTooltip(event)
337
369
  },
338
- onCellLinkClick: (event) => emit('cell-link-click', event),
339
- onImageLinkClick: (event) => emit('image-link-click', event),
370
+ onCellLinkClick: (event) => handleCellLinkClick(event),
371
+ onImageLinkClick: (event) => handleImageLinkClick(event),
340
372
  onFloatContextMenu: (event) => emit('float-context-menu', event),
341
373
  // body 菜单超链接项 → 拉起配置弹窗;右键「预览」→ 打开 lightbox
342
374
  openHyperlinkPopup,
@@ -6,7 +6,9 @@ import '@veltra/desktop/components/palette/style'
6
6
  import '@veltra/desktop/components/scroll/style'
7
7
  import '@veltra/desktop/components/tip/style'
8
8
  import '@veltra/desktop/components/loading/style' // v-loading(sheet.vue grid 容器)
9
- import '@veltra/desktop/components/message/style' // message / messageConfirm(弹层、tabs、公式栏)
9
+ import '@veltra/desktop/components/message/style' // message(弹层、tabs、公式栏)
10
+ import '@veltra/desktop/components/message-confirm/style' // messageConfirm(导入替换 / 图片链接打开确认)
11
+ import '@veltra/desktop/components/image-previewer/style' // UImagePreviewer(右键「预览」lightbox)
10
12
  import '@veltra/desktop/components/input/style' // UInput(find-popup / insert-image-popup)
11
13
  import '@veltra/desktop/components/file-picker/style' // UFilePicker(insert-image-popup)
12
14
  import '@veltra/desktop/components/button/style' // UButton(insert-image-popup URL 插入)
@@ -55,9 +55,9 @@ interface UseSheetGridOptions {
55
55
  onCellLinkHover?: (event: CellLinkHoverEvent) => void
56
56
  /** 单元格超链接离开(引擎透传 → 宿主 emit cell-link-leave) */
57
57
  onCellLinkLeave?: (event: CellLinkLeaveEvent) => void
58
- /** 单元格超链接单击(引擎透传 → 宿主 emit cell-link-click;打开行为仍归引擎默认) */
58
+ /** 单元格超链接单击(引擎透传 → 宿主 emit cell-link-click;打开由 sheet.vue 直开接管) */
59
59
  onCellLinkClick?: (event: CellLinkClickEvent) => void
60
- /** 图片链接单击(引擎透传 → 宿主 emit image-link-click;kind 区分嵌入/浮动) */
60
+ /** 图片链接单击(引擎透传 → 宿主 emit image-link-click;打开由 sheet.vue 确认后接管) */
61
61
  onImageLinkClick?: (event: ImageLinkClickEvent) => void
62
62
  /** 浮动图右键(引擎透传 → 宿主 emit float-context-menu;右键不穿透到 body 菜单) */
63
63
  onFloatContextMenu?: (event: FloatContextMenuPayload) => void
@@ -192,6 +192,8 @@ export function useSheetGrid(options: UseSheetGridOptions) {
192
192
  // 画布跟随 UI 主题:loadTheme 注入的 CSS 变量换算为引擎 override
193
193
  //(未 loadTheme 时为 undefined,引擎保持缺省白底)
194
194
  theme: resolveCanvasThemeOverride(host),
195
+ // 关闭引擎默认打开:链接格单击直开、图片链接单击确认后开(sheet.vue 接管)
196
+ openCellLinkOnClick: false,
195
197
  onContextMenu: handleContextMenu,
196
198
  onEditStart: (addr) => formulaBarRef.value?.mirrorGridEdit(addr),
197
199
  onEditEnd: (addr) => formulaBarRef.value?.exitMirror(addr),
@@ -118,13 +118,14 @@ export interface SheetEmits {
118
118
  (name: 'cell-link-leave', payload: CellLinkLeaveEvent): void
119
119
  /**
120
120
  * 单元格超链接单击(引擎 `CellLinkClickEvent` 形态:真实指针 down+up 命中
121
- * 同一链接格;框选拖拽/键盘选中不触发)。单击打开仍由引擎
122
- * `openCellLinkOnClick` 默认处理,ultra-ui 不接管
121
+ * 同一链接格;框选拖拽/键盘选中不触发)。组件已接管打开:emit 后直接新标签
122
+ * 打开(引擎 `openCellLinkOnClick` 已关闭)
123
123
  */
124
124
  (name: 'cell-link-click', payload: CellLinkClickEvent): void
125
125
  /**
126
126
  * 图片链接单击(引擎 `ImageLinkClickEvent`,`kind` 区分 `cell` 嵌入图 /
127
- * `float` 浮动图);默认打开行为同上由引擎处理
127
+ * `float` 浮动图)。组件已接管打开:emit 后弹确认框,确认才新标签打开
128
+ * (防误触跳转;引擎 `openCellLinkOnClick` 已关闭)
128
129
  */
129
130
  (name: 'image-link-click', payload: ImageLinkClickEvent): void
130
131
  /**