@veltra/sheet 4.0.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.
Files changed (36) hide show
  1. package/dist/components/sheet/float-image-menu.js +87 -0
  2. package/dist/components/sheet/insert-image.js +33 -4
  3. package/dist/components/sheet/popup-helpers.js +18 -2
  4. package/dist/components/sheet/popups/hyperlink-popup.js +127 -0
  5. package/dist/components/sheet/sheet-context-menu.js +122 -5
  6. package/dist/components/sheet/sheet.js +132 -15
  7. package/dist/components/sheet/style.d.ts +2 -0
  8. package/dist/components/sheet/style.js +2 -0
  9. package/dist/components/sheet/style2.css +62 -0
  10. package/dist/components/sheet/use-cell-link-tooltip.js +33 -0
  11. package/dist/components/sheet/use-sheet-grid.js +33 -1
  12. package/dist/components/sheet/use-sheet-state.js +1 -0
  13. package/dist/tools/context.d.ts +20 -2
  14. package/dist/tools/context.js +9 -1
  15. package/dist/types/index.d.ts +2 -2
  16. package/dist/types/sheet.d.ts +51 -1
  17. package/package.json +2 -2
  18. package/src/components/sheet/__test__/float-image-menu.test.ts +196 -0
  19. package/src/components/sheet/__test__/hyperlink-popup.test.ts +176 -0
  20. package/src/components/sheet/__test__/insert-image.test.ts +60 -0
  21. package/src/components/sheet/__test__/sheet-component.test.ts +539 -16
  22. package/src/components/sheet/__test__/sheet-context-menu.test.ts +293 -4
  23. package/src/components/sheet/float-image-menu.ts +100 -0
  24. package/src/components/sheet/insert-image.ts +32 -3
  25. package/src/components/sheet/popup-helpers.ts +68 -2
  26. package/src/components/sheet/popups/hyperlink-popup.vue +112 -0
  27. package/src/components/sheet/sheet-context-menu.ts +125 -6
  28. package/src/components/sheet/sheet.vue +149 -3
  29. package/src/components/sheet/style.scss +66 -0
  30. package/src/components/sheet/style.ts +3 -1
  31. package/src/components/sheet/use-cell-link-tooltip.ts +42 -0
  32. package/src/components/sheet/use-sheet-grid.ts +62 -3
  33. package/src/components/sheet/use-sheet-state.ts +8 -2
  34. package/src/tools/__test__/context.test.ts +89 -0
  35. package/src/tools/context.ts +33 -2
  36. package/src/types/sheet.ts +56 -0
@@ -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,22 +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(5)
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
+ '插入超链接',
236
+ '插入图片',
237
+ '嵌入图片'
235
238
  ])
236
239
  const insertImage = menus.find((m) => m.label === '插入图片')!
237
240
  expect(insertImage.disabled).toBeFalsy()
238
241
  expect(insertImage.callback).toBeTruthy()
242
+ const embedImage = menus.find((m) => m.label === '嵌入图片')!
243
+ expect(embedImage.disabled).toBeFalsy()
244
+ expect(embedImage.callback).toBeTruthy()
239
245
  const labels = menus.map((m) => m.label).join()
240
246
  expect(labels).not.toContain('插入行')
241
247
  expect(labels).not.toContain('插入列')
@@ -322,13 +328,16 @@ describe('build*Menus', () => {
322
328
  expect(ctx.canUndo).toBe(false)
323
329
  })
324
330
 
325
- it('body 插入图片 / 设置数据格式:无活动格时禁用', () => {
331
+ it('body 插入图片 / 嵌入图片 / 设置数据格式:无活动格时禁用', () => {
326
332
  const sheet = new Sheet()
327
333
  const ctx = createSheetContext(sheet)
328
334
  sheet.selection.clear()
329
335
  const menus = buildBodyMenus(ctx)
330
336
  expect(menus.find((m) => m.label === '插入图片')?.disabled).toBe(true)
337
+ expect(menus.find((m) => m.label === '嵌入图片')?.disabled).toBe(true)
331
338
  expect(menus.find((m) => m.label === '设置数据格式')?.disabled).toBe(true)
339
+ // 无活动格:超链接项同样禁用
340
+ expect(menus.find((m) => m.label === '插入超链接')?.disabled).toBe(true)
332
341
  })
333
342
 
334
343
  it('行号/列头菜单仍含插入删除行列', () => {
@@ -364,3 +373,283 @@ describe('build*Menus', () => {
364
373
  expect(menus[0]?.keepOpen).toBe(true)
365
374
  })
366
375
  })
376
+
377
+ describe('buildBodyMenus 超链接项(按目标格链接状态出项)', () => {
378
+ /** 记录句柄调用的简单 spy(mode + 编辑回填载荷) */
379
+ function popupSpy() {
380
+ const calls: Array<{ mode: string; initial?: { url: string; text?: string } }> = []
381
+ const open = (mode: 'cell', initial?: { url: string; text?: string }): void => {
382
+ calls.push({ mode, initial })
383
+ }
384
+ return { calls, open }
385
+ }
386
+
387
+ it('无链接格:仅「插入超链接」,callback 经句柄以 cell 模式拉起弹窗(无回填)', () => {
388
+ const sheet = new Sheet()
389
+ const ctx = createSheetContext(sheet)
390
+ sheet.selectCell({ row: 0, col: 0 })
391
+ const spy = popupSpy()
392
+ const menus = buildBodyMenus(ctx, { openHyperlinkPopup: spy.open })
393
+ const item = menus.find((m) => m.label === '插入超链接')!
394
+ expect(item.disabled).toBeFalsy()
395
+ expect(menus.some((m) => m.label === '编辑超链接')).toBe(false)
396
+ expect(menus.some((m) => m.label === '取消超链接')).toBe(false)
397
+ item.callback!()
398
+ expect(spy.calls).toEqual([{ mode: 'cell', initial: undefined }])
399
+ })
400
+
401
+ it('有链接格:「编辑超链接」回填原 url/text;「取消超链接」clearCellLink 后 getCellLink undefined 且 undo 还原', () => {
402
+ const sheet = new Sheet()
403
+ const ctx = createSheetContext(sheet)
404
+ const addr = { row: 1, col: 2 }
405
+ sheet.selectCell(addr)
406
+ sheet.setCellLink(addr, { url: 'https://example.com', text: '示例' })
407
+ const spy = popupSpy()
408
+ const menus = buildBodyMenus(ctx, { openHyperlinkPopup: spy.open })
409
+
410
+ expect(menus.some((m) => m.label === '插入超链接')).toBe(false)
411
+ const edit = menus.find((m) => m.label === '编辑超链接')!
412
+ expect(edit.disabled).toBeFalsy()
413
+ edit.callback!()
414
+ expect(spy.calls).toEqual([
415
+ { mode: 'cell', initial: { url: 'https://example.com', text: '示例' } }
416
+ ])
417
+
418
+ const clear = menus.find((m) => m.label === '取消超链接')!
419
+ expect(clear.disabled).toBeFalsy()
420
+ clear.callback!()
421
+ expect(ctx.getCellLink(addr)).toBeUndefined()
422
+ expect(ctx.undo()).toBe(true)
423
+ expect(ctx.getCellLink(addr)).toEqual({ url: 'https://example.com', text: '示例' })
424
+ })
425
+
426
+ it('readonly:插入/编辑/取消项禁用;句柄缺省:插入/编辑项禁用', () => {
427
+ const sheet = new Sheet()
428
+ const ctx = createSheetContext(sheet)
429
+ sheet.selectCell({ row: 0, col: 0 })
430
+
431
+ // 句柄缺省(无宿主接线,如直接调用 build):插入禁用
432
+ const noHandle = buildBodyMenus(ctx)
433
+ expect(noHandle.find((m) => m.label === '插入超链接')?.disabled).toBe(true)
434
+
435
+ // readonly:插入禁用
436
+ const readonlyInsert = buildBodyMenus(ctx, { readonly: true, openHyperlinkPopup: () => {} })
437
+ expect(readonlyInsert.find((m) => m.label === '插入超链接')?.disabled).toBe(true)
438
+
439
+ // readonly + 有链接:编辑/取消均禁用
440
+ sheet.setCellLink({ row: 0, col: 0 }, { url: 'https://example.com' })
441
+ const readonlyEdit = buildBodyMenus(ctx, { readonly: true, openHyperlinkPopup: () => {} })
442
+ expect(readonlyEdit.find((m) => m.label === '编辑超链接')?.disabled).toBe(true)
443
+ expect(readonlyEdit.find((m) => m.label === '取消超链接')?.disabled).toBe(true)
444
+ // readonly 不产生模型写入
445
+ expect(readonlyEdit.find((m) => m.label === '取消超链接')?.callback).toBeTruthy()
446
+ })
447
+ })
448
+
449
+ describe('buildBodyMenus 嵌入图项(按目标格嵌入图状态出项)', () => {
450
+ const ADDR = { row: 1, col: 2 }
451
+
452
+ /** 弹窗句柄 spy(含图片目标记录) */
453
+ function popupSpy() {
454
+ const calls: Array<{
455
+ mode: 'image'
456
+ initial?: { url: string }
457
+ target?: { kind: 'cell'; addr: { row: number; col: number } }
458
+ }> = []
459
+ return {
460
+ calls,
461
+ open: (
462
+ mode: 'image',
463
+ initial?: { url: string },
464
+ target?: { kind: 'cell'; addr: { row: number; col: number } }
465
+ ): void => {
466
+ calls.push({ mode, initial, target })
467
+ }
468
+ }
469
+ }
470
+
471
+ /** 预览句柄 spy */
472
+ function previewSpy() {
473
+ const previews: unknown[] = []
474
+ return { previews, open: (source: unknown): void => previews.push(source) }
475
+ }
476
+
477
+ it('无嵌入图:不出现图片项(普通格菜单 + 嵌入图片入口)', () => {
478
+ const sheet = new Sheet()
479
+ const ctx = createSheetContext(sheet)
480
+ sheet.selectCell(ADDR)
481
+ const labels = buildBodyMenus(ctx).map((m) => m.label ?? (m.divider ? '---' : ''))
482
+ expect(labels).toHaveLength(7)
483
+ expect(labels).toContain('嵌入图片')
484
+ for (const label of [
485
+ '预览',
486
+ '图片超链接',
487
+ '取消超链接',
488
+ '转为浮动图片',
489
+ '替换图片',
490
+ '移除图片'
491
+ ]) {
492
+ expect(labels).not.toContain(label)
493
+ }
494
+ })
495
+
496
+ it('嵌入图格出纯图片菜单:不含合并/数据格式/超链接/插入图片等普通格项(无链接)', () => {
497
+ const sheet = new Sheet()
498
+ const ctx = createSheetContext(sheet)
499
+ ctx.insertCellImage(ADDR, { src: 'https://example.com/a.png' })
500
+ sheet.selectCell(ADDR)
501
+ const popup = popupSpy()
502
+ const preview = previewSpy()
503
+
504
+ const menus = buildBodyMenus(ctx, {
505
+ openHyperlinkPopup: popup.open,
506
+ openImagePreview: preview.open
507
+ })
508
+ expect(menus.map((m) => m.label)).toEqual([
509
+ '预览',
510
+ '图片超链接',
511
+ '转为浮动图片',
512
+ '替换图片',
513
+ '移除图片'
514
+ ])
515
+ for (const label of [
516
+ '合并单元格',
517
+ '取消合并单元格',
518
+ '设置数据格式',
519
+ '插入超链接',
520
+ '插入图片',
521
+ '嵌入图片'
522
+ ]) {
523
+ expect(menus.find((m) => m.label === label)).toBeUndefined()
524
+ }
525
+
526
+ menus.find((m) => m.label === '预览')!.callback!()
527
+ expect(preview.previews).toEqual([{ src: 'https://example.com/a.png' }])
528
+ menus.find((m) => m.label === '图片超链接')!.callback!()
529
+ expect(popup.calls).toEqual([
530
+ { mode: 'image', initial: undefined, target: { kind: 'cell', addr: ADDR } }
531
+ ])
532
+ })
533
+
534
+ it('有链接嵌入图:含取消超链接;图片超链接回填 url;取消清 link 保其余字段且 undo 还原', () => {
535
+ const sheet = new Sheet()
536
+ const ctx = createSheetContext(sheet)
537
+ ctx.insertCellImage(ADDR, { src: 'https://example.com/a.png', link: 'https://example.com' })
538
+ sheet.selectCell(ADDR)
539
+ const popup = popupSpy()
540
+
541
+ const menus = buildBodyMenus(ctx, { openHyperlinkPopup: popup.open })
542
+ expect(menus.map((m) => m.label)).toEqual([
543
+ '预览',
544
+ '图片超链接',
545
+ '取消超链接',
546
+ '转为浮动图片',
547
+ '替换图片',
548
+ '移除图片'
549
+ ])
550
+
551
+ menus.find((m) => m.label === '图片超链接')!.callback!()
552
+ expect(popup.calls).toEqual([
553
+ {
554
+ mode: 'image',
555
+ initial: { url: 'https://example.com' },
556
+ target: { kind: 'cell', addr: ADDR }
557
+ }
558
+ ])
559
+
560
+ menus.find((m) => m.label === '取消超链接')!.callback!()
561
+ expect(ctx.getCellImage(ADDR)).toEqual({ src: 'https://example.com/a.png' })
562
+ expect(ctx.undo()).toBe(true)
563
+ expect(ctx.getCellImage(ADDR)).toEqual({
564
+ src: 'https://example.com/a.png',
565
+ link: 'https://example.com'
566
+ })
567
+ })
568
+
569
+ it('移除图片:hasCellImage false 且 undo 还原', () => {
570
+ const sheet = new Sheet()
571
+ const ctx = createSheetContext(sheet)
572
+ ctx.insertCellImage(ADDR, { src: 'https://example.com/a.png' })
573
+ sheet.selectCell(ADDR)
574
+
575
+ buildBodyMenus(ctx).find((m) => m.label === '移除图片')!.callback!()
576
+ expect(ctx.hasCellImage(ADDR)).toBe(false)
577
+ expect(ctx.undo()).toBe(true)
578
+ expect(ctx.hasCellImage(ADDR)).toBe(true)
579
+ })
580
+
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:图片超链接/取消超链接/转浮动/替换/移除禁用、预览可用;句柄缺省:预览/图片超链接禁用', () => {
632
+ const sheet = new Sheet()
633
+ const ctx = createSheetContext(sheet)
634
+ ctx.insertCellImage(ADDR, { src: 'https://example.com/a.png', link: 'https://e.com' })
635
+ sheet.selectCell(ADDR)
636
+
637
+ const readonly = buildBodyMenus(ctx, {
638
+ readonly: true,
639
+ openHyperlinkPopup: () => {},
640
+ openImagePreview: () => {}
641
+ })
642
+ expect(readonly.find((m) => m.label === '预览')?.disabled).toBeFalsy()
643
+ expect(readonly.find((m) => m.label === '图片超链接')?.disabled).toBe(true)
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)
647
+ expect(readonly.find((m) => m.label === '移除图片')?.disabled).toBe(true)
648
+
649
+ const noHandles = buildBodyMenus(ctx)
650
+ expect(noHandles.find((m) => m.label === '预览')?.disabled).toBe(true)
651
+ expect(noHandles.find((m) => m.label === '图片超链接')?.disabled).toBe(true)
652
+ expect(noHandles.find((m) => m.label === '转为浮动图片')?.disabled).toBeFalsy()
653
+ expect(noHandles.find((m) => m.label === '移除图片')?.disabled).toBeFalsy()
654
+ })
655
+ })
@@ -0,0 +1,100 @@
1
+ import type { ContextmenuItem } from '@veltra/desktop'
2
+ import { contextmenu } from '@veltra/desktop'
3
+ import type { CellImagePayload } from 'infinitable/sheet'
4
+
5
+ import type { SheetContext } from '../../tools/context'
6
+ import type { FloatContextMenuPayload } from '../../types'
7
+ import type { HyperlinkImageTarget, ImagePreviewSource, OpenHyperlinkPopup } from './popup-helpers'
8
+ import { imageLinkInitial, imagePreviewSource } from './popup-helpers'
9
+
10
+ /** 浮动图菜单注入句柄(lightbox 预览 / 图片链接弹窗由 sheet.vue 挂载,保持构建纯函数可测) */
11
+ export interface FloatImageMenuHandles {
12
+ /** 打开 lightbox 预览;缺省时「预览」项禁用 */
13
+ openImagePreview?: (source: ImagePreviewSource) => void
14
+ /** 拉起图片超链接配置弹窗(image 模式);缺省时「设置超链接」项禁用 */
15
+ openHyperlinkPopup?: OpenHyperlinkPopup
16
+ }
17
+
18
+ /**
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
+ * 调用方不弹菜单。
43
+ */
44
+ export function buildFloatImageMenus(
45
+ ctx: SheetContext,
46
+ id: string,
47
+ handles?: FloatImageMenuHandles,
48
+ options?: { readonly?: boolean }
49
+ ): ContextmenuItem[] {
50
+ const image = ctx.getImages().find((item) => item.id === id)
51
+ if (!image) return []
52
+ const readonly = options?.readonly === true
53
+ const target: HyperlinkImageTarget = { kind: 'float', id }
54
+ const items: ContextmenuItem[] = [
55
+ {
56
+ label: '预览',
57
+ disabled: !handles?.openImagePreview,
58
+ callback: () => handles?.openImagePreview?.(imagePreviewSource(image))
59
+ },
60
+ {
61
+ label: '设置超链接',
62
+ disabled: readonly || !handles?.openHyperlinkPopup,
63
+ callback: () => handles?.openHyperlinkPopup?.('image', imageLinkInitial(image.link), target)
64
+ },
65
+ ...(image.link
66
+ ? [
67
+ {
68
+ label: '取消超链接',
69
+ disabled: readonly,
70
+ callback: () => ctx.updateImage(id, { link: null })
71
+ }
72
+ ]
73
+ : []),
74
+ { label: '嵌入单元格', disabled: readonly, callback: () => embedFloatImageToCell(ctx, id) },
75
+ { label: '删除图片', disabled: readonly, callback: () => ctx.removeImage(id) }
76
+ ]
77
+ return items
78
+ }
79
+
80
+ /**
81
+ * 消费引擎浮动图右键事件:按命中浮动图 link 状态构建独立菜单并经 `contextmenu.pop`
82
+ * 弹出(viewport 坐标定位,与 body 右键菜单同口径)。右键不穿透到格菜单由引擎
83
+ * 保证(floatObjects 命中即不派发单元格 contextmenu),此处只弹浮动图菜单。
84
+ */
85
+ export function popFloatImageMenu(options: {
86
+ ctx: SheetContext
87
+ event: FloatContextMenuPayload
88
+ handles?: FloatImageMenuHandles
89
+ readonly?: boolean
90
+ }): void {
91
+ const menus = buildFloatImageMenus(options.ctx, options.event.id, options.handles, {
92
+ readonly: options.readonly
93
+ })
94
+ if (menus.length === 0) return
95
+ contextmenu.pop({
96
+ mousePosition: { x: options.event.clientX, y: options.event.clientY },
97
+ width: 'max-content',
98
+ menus
99
+ })
100
+ }
@@ -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,6 +1,11 @@
1
- import type { CellRange } from 'infinitable/sheet'
1
+ import type { CellAddress, CellLinkPayload, CellRange } from 'infinitable/sheet'
2
2
  import type { BorderPreset } from 'infinitable/sheet'
3
- import { BORDER_STYLE_WIDTH, type BorderLineStyle } from 'infinitable/sheet'
3
+ import {
4
+ BORDER_STYLE_WIDTH,
5
+ IMAGE_MIME,
6
+ type BorderLineStyle,
7
+ type SheetImageType
8
+ } from 'infinitable/sheet'
4
9
 
5
10
  import type { SheetContext } from '../../tools/context'
6
11
 
@@ -10,6 +15,67 @@ export function currentRange(context: SheetContext): CellRange | null {
10
15
  return ranges[0] ?? (activeCell ? { start: activeCell, end: activeCell } : null)
11
16
  }
12
17
 
18
+ // ─── 超链接配置弹窗共享类型(hyperlink-popup.vue 与菜单/宿主接线共用) ──
19
+
20
+ /** 弹窗模式:cell = 单元格链接(地址 + 显示文本);image = 图片链接(仅地址) */
21
+ export type HyperlinkPopupMode = 'cell' | 'image'
22
+
23
+ /**
24
+ * 弹窗确认载荷:显示文本留空时不含 text(不写入链接文本覆盖 →
25
+ * 单元格沿用自身内容作为显示,见 spec「显示文本留空提交沿用原显示文本」)
26
+ */
27
+ export interface HyperlinkConfirmPayload {
28
+ url: string
29
+ text?: string
30
+ }
31
+
32
+ /**
33
+ * 拉起超链接配置弹窗的句柄:右键菜单 build 函数经 options 注入
34
+ * (保持纯函数可测,不直接 import 组件状态);编辑时回填原链接载荷。
35
+ * image 模式必须携带写入目标(浮动图 id / 嵌入图格),confirm 据此路由写入。
36
+ */
37
+ export type OpenHyperlinkPopup = (
38
+ mode: HyperlinkPopupMode,
39
+ initial?: CellLinkPayload,
40
+ imageTarget?: HyperlinkImageTarget
41
+ ) => void
42
+
43
+ /** image 模式链接写入目标:浮动图(按 id,updateImage)或嵌入图(按格,覆盖写) */
44
+ export type HyperlinkImageTarget =
45
+ | { kind: 'float'; id: string }
46
+ | { kind: 'cell'; addr: CellAddress }
47
+
48
+ /** 图片链接 → 弹窗回填载荷(无链接 → undefined 插入态;浮动图与嵌入图共用) */
49
+ export function imageLinkInitial(link: string | undefined): CellLinkPayload | undefined {
50
+ return link ? { url: link } : undefined
51
+ }
52
+
53
+ // ─── 图片 lightbox 预览(右键「预览」拉起,sheet.vue 挂 UImagePreviewer) ──
54
+
55
+ /** lightbox 预览源:URL 或图片字节 + MIME 提示(字节转 objectURL 由预览组件回收) */
56
+ export interface ImagePreviewSource {
57
+ /** URL 来源(浮动图 src / 嵌入图 src,优先于字节) */
58
+ src?: string
59
+ /** 图片字节(SheetImage.data / CellImagePayload.data) */
60
+ data?: Uint8Array
61
+ /** 字节源的 MIME 提示(引擎 type → IMAGE_MIME) */
62
+ mime?: string
63
+ }
64
+
65
+ /**
66
+ * 图片载荷 → 预览源归一:src 优先(URL 来源直接引用);否则取字节 + 模型 type
67
+ * 映射 MIME(type 缺省按 png,同引擎嵌入图渲染约定)。浮动图(type 必填)与
68
+ * 嵌入图(type 可选)共用。
69
+ */
70
+ export function imagePreviewSource(payload: {
71
+ src?: string
72
+ data?: Uint8Array
73
+ type?: SheetImageType
74
+ }): ImagePreviewSource {
75
+ if (payload.src) return { src: payload.src }
76
+ return { data: payload.data, mime: IMAGE_MIME[payload.type ?? 'png'] }
77
+ }
78
+
13
79
  // ─── 边框面板常量 ────────────────────────────────────────────
14
80
 
15
81
  export const BORDER_LINE_STYLES: BorderLineStyle[] = ['thin', 'medium', 'thick', 'dashed', 'dotted']
@@ -0,0 +1,112 @@
1
+ <template>
2
+ <Teleport to="body">
3
+ <div
4
+ :class="cls.e('link-dialog-mask')"
5
+ :style="{ zIndex: maskZIndex }"
6
+ @mousedown.self="handleCancel"
7
+ >
8
+ <div :class="cls.e('link-dialog')" role="dialog" aria-modal="true">
9
+ <div :class="cls.e('link-dialog-title')">{{ title }}</div>
10
+ <div :class="cls.e('link-dialog-body')">
11
+ <div :class="cls.e('popup-row')">
12
+ <label :class="cls.e('popup-label')" :for="urlId">地址</label>
13
+ <u-input
14
+ :id="urlId"
15
+ v-model="url"
16
+ :class="cls.e('link-dialog-input')"
17
+ placeholder="https://example.com"
18
+ size="small"
19
+ @keydown.enter="handleConfirm"
20
+ />
21
+ </div>
22
+ <div v-if="mode === 'cell'" :class="cls.e('popup-row')">
23
+ <label :class="cls.e('popup-label')" :for="textId">显示文本</label>
24
+ <u-input
25
+ :id="textId"
26
+ v-model="text"
27
+ :class="cls.e('link-dialog-input')"
28
+ placeholder="留空沿用单元格内容"
29
+ size="small"
30
+ @keydown.enter="handleConfirm"
31
+ />
32
+ </div>
33
+ </div>
34
+ <div :class="cls.e('link-dialog-footer')">
35
+ <u-button size="small" @click="handleCancel">取消</u-button>
36
+ <u-button size="small" type="primary" @click="handleConfirm">确定</u-button>
37
+ </div>
38
+ </div>
39
+ </div>
40
+ </Teleport>
41
+ </template>
42
+
43
+ <script lang="ts" setup>
44
+ import { UButton, UInput, message } from '@veltra/desktop'
45
+ import { bem, zIndex } from '@veltra/utils'
46
+ import { computed, onBeforeUnmount, onMounted, ref, useId } from 'vue'
47
+
48
+ import type { HyperlinkConfirmPayload, HyperlinkPopupMode } from '../popup-helpers'
49
+
50
+ defineOptions({ name: 'USheetHyperlinkPopup' })
51
+
52
+ /**
53
+ * 超链接配置弹窗(右键菜单拉起,无工具栏锚点——不走 UDropdown 工具面板链路,
54
+ * 自绘遮罩 + 居中面板,对齐 loading-mask 自绘先例):
55
+ * - 地址必填:空提交拦截并 message.error,弹窗不关
56
+ * - 显示文本可留空:留空不写入 text(单元格沿用自身内容作为显示)
57
+ * - mode="cell" 两字段;mode="image" 仅地址(图片超链接复用,写入目标由宿主收口)
58
+ * - 确认仅 emit confirm;关闭(取消/Esc/遮罩点击)emit close,均由宿主收口
59
+ */
60
+ const props = defineProps<{
61
+ mode: HyperlinkPopupMode
62
+ /** 编辑回填的原链接载荷(插入时缺省) */
63
+ initial?: { url: string; text?: string }
64
+ }>()
65
+
66
+ const emit = defineEmits<{ confirm: [payload: HyperlinkConfirmPayload]; close: [] }>()
67
+
68
+ const cls = bem('sheet')
69
+
70
+ /** 字段输入 id(useId:多实例同开不撞固定 id) */
71
+ const urlId = useId()
72
+ const textId = useId()
73
+
74
+ const url = ref(props.initial?.url ?? '')
75
+ const text = ref(props.initial?.text ?? '')
76
+
77
+ /** 遮罩层级:挂载时取全局递增 z-index(与 desktop 弹层互不压盖) */
78
+ const maskZIndex = zIndex()
79
+
80
+ const title = computed(() => {
81
+ if (props.mode === 'image') return '设置图片超链接'
82
+ return props.initial ? '编辑超链接' : '插入超链接'
83
+ })
84
+
85
+ function handleConfirm(): void {
86
+ const nextUrl = url.value.trim()
87
+ if (!nextUrl) {
88
+ message.error('请输入链接地址')
89
+ return
90
+ }
91
+ const nextText = text.value.trim()
92
+ // 显示文本留空不写入(不含 text 字段 → 单元格沿用自身内容显示)
93
+ emit('confirm', nextText ? { url: nextUrl, text: nextText } : { url: nextUrl })
94
+ }
95
+
96
+ function handleCancel(): void {
97
+ emit('close')
98
+ }
99
+
100
+ /** Esc 关闭(window 级监听:弹窗内输入框未聚焦时同样生效) */
101
+ function onKeydown(event: KeyboardEvent): void {
102
+ if (event.key === 'Escape') handleCancel()
103
+ }
104
+
105
+ onMounted(() => {
106
+ window.addEventListener('keydown', onKeydown)
107
+ })
108
+
109
+ onBeforeUnmount(() => {
110
+ window.removeEventListener('keydown', onKeydown)
111
+ })
112
+ </script>