@veltra/sheet 4.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.
Files changed (30) hide show
  1. package/dist/components/sheet/float-image-menu.js +59 -0
  2. package/dist/components/sheet/popup-helpers.js +18 -2
  3. package/dist/components/sheet/popups/hyperlink-popup.js +127 -0
  4. package/dist/components/sheet/sheet-context-menu.js +75 -3
  5. package/dist/components/sheet/sheet.js +110 -15
  6. package/dist/components/sheet/style2.css +62 -0
  7. package/dist/components/sheet/use-cell-link-tooltip.js +33 -0
  8. package/dist/components/sheet/use-sheet-grid.js +32 -1
  9. package/dist/components/sheet/use-sheet-state.js +1 -0
  10. package/dist/tools/context.d.ts +20 -2
  11. package/dist/tools/context.js +9 -1
  12. package/dist/types/index.d.ts +2 -2
  13. package/dist/types/sheet.d.ts +50 -1
  14. package/package.json +1 -1
  15. package/src/components/sheet/__test__/float-image-menu.test.ts +130 -0
  16. package/src/components/sheet/__test__/hyperlink-popup.test.ts +176 -0
  17. package/src/components/sheet/__test__/sheet-component.test.ts +474 -16
  18. package/src/components/sheet/__test__/sheet-context-menu.test.ts +212 -2
  19. package/src/components/sheet/float-image-menu.ts +78 -0
  20. package/src/components/sheet/popup-helpers.ts +68 -2
  21. package/src/components/sheet/popups/hyperlink-popup.vue +112 -0
  22. package/src/components/sheet/sheet-context-menu.ts +86 -3
  23. package/src/components/sheet/sheet.vue +117 -3
  24. package/src/components/sheet/style.scss +66 -0
  25. package/src/components/sheet/use-cell-link-tooltip.ts +42 -0
  26. package/src/components/sheet/use-sheet-grid.ts +60 -3
  27. package/src/components/sheet/use-sheet-state.ts +8 -2
  28. package/src/tools/__test__/context.test.ts +89 -0
  29. package/src/tools/context.ts +33 -2
  30. package/src/types/sheet.ts +55 -0
@@ -220,17 +220,18 @@ describe('build*Menus', () => {
220
220
  expect(readonlyCol.render).toBeUndefined()
221
221
  })
222
222
 
223
- it('body 菜单:合并/取消合并 + 设置数据格式子菜单 + 插入图片,不含行列插入删除', () => {
223
+ it('body 菜单:合并/取消合并 + 设置数据格式子菜单 + 插入超链接 + 插入图片,不含行列插入删除', () => {
224
224
  const sheet = new Sheet()
225
225
  const ctx = createSheetContext(sheet)
226
226
  sheet.selectCell({ row: 0, col: 0 })
227
227
  const menus = buildBodyMenus(ctx)
228
- expect(menus).toHaveLength(5)
228
+ expect(menus).toHaveLength(6)
229
229
  expect(menus.map((m) => m.label ?? (m.divider ? '---' : ''))).toEqual([
230
230
  '合并单元格',
231
231
  '取消合并单元格',
232
232
  '---',
233
233
  '设置数据格式',
234
+ '插入超链接',
234
235
  '插入图片'
235
236
  ])
236
237
  const insertImage = menus.find((m) => m.label === '插入图片')!
@@ -329,6 +330,8 @@ describe('build*Menus', () => {
329
330
  const menus = buildBodyMenus(ctx)
330
331
  expect(menus.find((m) => m.label === '插入图片')?.disabled).toBe(true)
331
332
  expect(menus.find((m) => m.label === '设置数据格式')?.disabled).toBe(true)
333
+ // 无活动格:超链接项同样禁用
334
+ expect(menus.find((m) => m.label === '插入超链接')?.disabled).toBe(true)
332
335
  })
333
336
 
334
337
  it('行号/列头菜单仍含插入删除行列', () => {
@@ -364,3 +367,210 @@ describe('build*Menus', () => {
364
367
  expect(menus[0]?.keepOpen).toBe(true)
365
368
  })
366
369
  })
370
+
371
+ describe('buildBodyMenus 超链接项(按目标格链接状态出项)', () => {
372
+ /** 记录句柄调用的简单 spy(mode + 编辑回填载荷) */
373
+ function popupSpy() {
374
+ const calls: Array<{ mode: string; initial?: { url: string; text?: string } }> = []
375
+ const open = (mode: 'cell', initial?: { url: string; text?: string }): void => {
376
+ calls.push({ mode, initial })
377
+ }
378
+ return { calls, open }
379
+ }
380
+
381
+ it('无链接格:仅「插入超链接」,callback 经句柄以 cell 模式拉起弹窗(无回填)', () => {
382
+ const sheet = new Sheet()
383
+ const ctx = createSheetContext(sheet)
384
+ sheet.selectCell({ row: 0, col: 0 })
385
+ const spy = popupSpy()
386
+ const menus = buildBodyMenus(ctx, { openHyperlinkPopup: spy.open })
387
+ const item = menus.find((m) => m.label === '插入超链接')!
388
+ expect(item.disabled).toBeFalsy()
389
+ expect(menus.some((m) => m.label === '编辑超链接')).toBe(false)
390
+ expect(menus.some((m) => m.label === '取消超链接')).toBe(false)
391
+ item.callback!()
392
+ expect(spy.calls).toEqual([{ mode: 'cell', initial: undefined }])
393
+ })
394
+
395
+ it('有链接格:「编辑超链接」回填原 url/text;「取消超链接」clearCellLink 后 getCellLink undefined 且 undo 还原', () => {
396
+ const sheet = new Sheet()
397
+ const ctx = createSheetContext(sheet)
398
+ const addr = { row: 1, col: 2 }
399
+ sheet.selectCell(addr)
400
+ sheet.setCellLink(addr, { url: 'https://example.com', text: '示例' })
401
+ const spy = popupSpy()
402
+ const menus = buildBodyMenus(ctx, { openHyperlinkPopup: spy.open })
403
+
404
+ expect(menus.some((m) => m.label === '插入超链接')).toBe(false)
405
+ const edit = menus.find((m) => m.label === '编辑超链接')!
406
+ expect(edit.disabled).toBeFalsy()
407
+ edit.callback!()
408
+ expect(spy.calls).toEqual([
409
+ { mode: 'cell', initial: { url: 'https://example.com', text: '示例' } }
410
+ ])
411
+
412
+ const clear = menus.find((m) => m.label === '取消超链接')!
413
+ expect(clear.disabled).toBeFalsy()
414
+ clear.callback!()
415
+ expect(ctx.getCellLink(addr)).toBeUndefined()
416
+ expect(ctx.undo()).toBe(true)
417
+ expect(ctx.getCellLink(addr)).toEqual({ url: 'https://example.com', text: '示例' })
418
+ })
419
+
420
+ it('readonly:插入/编辑/取消项禁用;句柄缺省:插入/编辑项禁用', () => {
421
+ const sheet = new Sheet()
422
+ const ctx = createSheetContext(sheet)
423
+ sheet.selectCell({ row: 0, col: 0 })
424
+
425
+ // 句柄缺省(无宿主接线,如直接调用 build):插入禁用
426
+ const noHandle = buildBodyMenus(ctx)
427
+ expect(noHandle.find((m) => m.label === '插入超链接')?.disabled).toBe(true)
428
+
429
+ // readonly:插入禁用
430
+ const readonlyInsert = buildBodyMenus(ctx, { readonly: true, openHyperlinkPopup: () => {} })
431
+ expect(readonlyInsert.find((m) => m.label === '插入超链接')?.disabled).toBe(true)
432
+
433
+ // readonly + 有链接:编辑/取消均禁用
434
+ sheet.setCellLink({ row: 0, col: 0 }, { url: 'https://example.com' })
435
+ const readonlyEdit = buildBodyMenus(ctx, { readonly: true, openHyperlinkPopup: () => {} })
436
+ expect(readonlyEdit.find((m) => m.label === '编辑超链接')?.disabled).toBe(true)
437
+ expect(readonlyEdit.find((m) => m.label === '取消超链接')?.disabled).toBe(true)
438
+ // readonly 不产生模型写入
439
+ expect(readonlyEdit.find((m) => m.label === '取消超链接')?.callback).toBeTruthy()
440
+ })
441
+ })
442
+
443
+ describe('buildBodyMenus 嵌入图项(按目标格嵌入图状态出项)', () => {
444
+ const ADDR = { row: 1, col: 2 }
445
+
446
+ /** 弹窗句柄 spy(含图片目标记录) */
447
+ function popupSpy() {
448
+ const calls: Array<{
449
+ mode: 'image'
450
+ initial?: { url: string }
451
+ target?: { kind: 'cell'; addr: { row: number; col: number } }
452
+ }> = []
453
+ return {
454
+ calls,
455
+ open: (
456
+ mode: 'image',
457
+ initial?: { url: string },
458
+ target?: { kind: 'cell'; addr: { row: number; col: number } }
459
+ ): void => {
460
+ calls.push({ mode, initial, target })
461
+ }
462
+ }
463
+ }
464
+
465
+ /** 预览句柄 spy */
466
+ function previewSpy() {
467
+ const previews: unknown[] = []
468
+ return { previews, open: (source: unknown): void => previews.push(source) }
469
+ }
470
+
471
+ it('无嵌入图:不出现图片项', () => {
472
+ const sheet = new Sheet()
473
+ const ctx = createSheetContext(sheet)
474
+ sheet.selectCell(ADDR)
475
+ const labels = buildBodyMenus(ctx).map((m) => m.label ?? (m.divider ? '---' : ''))
476
+ expect(labels).toHaveLength(6)
477
+ for (const label of ['预览', '图片超链接', '取消超链接', '移除图片']) {
478
+ expect(labels).not.toContain(label)
479
+ }
480
+ })
481
+
482
+ it('无链接嵌入图:divider + 预览 / 图片超链接 / 移除图片(无取消超链接)', () => {
483
+ const sheet = new Sheet()
484
+ const ctx = createSheetContext(sheet)
485
+ ctx.insertCellImage(ADDR, { src: 'https://example.com/a.png' })
486
+ sheet.selectCell(ADDR)
487
+ const popup = popupSpy()
488
+ const preview = previewSpy()
489
+
490
+ const menus = buildBodyMenus(ctx, {
491
+ openHyperlinkPopup: popup.open,
492
+ openImagePreview: preview.open
493
+ })
494
+ expect(menus.map((m) => m.label ?? (m.divider ? '---' : ''))).toEqual([
495
+ '合并单元格',
496
+ '取消合并单元格',
497
+ '---',
498
+ '设置数据格式',
499
+ '插入超链接',
500
+ '插入图片',
501
+ '---',
502
+ '预览',
503
+ '图片超链接',
504
+ '移除图片'
505
+ ])
506
+
507
+ menus.find((m) => m.label === '预览')!.callback!()
508
+ expect(preview.previews).toEqual([{ src: 'https://example.com/a.png' }])
509
+ menus.find((m) => m.label === '图片超链接')!.callback!()
510
+ expect(popup.calls).toEqual([
511
+ { mode: 'image', initial: undefined, target: { kind: 'cell', addr: ADDR } }
512
+ ])
513
+ })
514
+
515
+ it('有链接嵌入图:含取消超链接;图片超链接回填 url;取消清 link 保其余字段且 undo 还原', () => {
516
+ const sheet = new Sheet()
517
+ const ctx = createSheetContext(sheet)
518
+ ctx.insertCellImage(ADDR, { src: 'https://example.com/a.png', link: 'https://example.com' })
519
+ sheet.selectCell(ADDR)
520
+ const popup = popupSpy()
521
+
522
+ const menus = buildBodyMenus(ctx, { openHyperlinkPopup: popup.open })
523
+ expect(menus.map((m) => m.label)).toContain('取消超链接')
524
+
525
+ menus.find((m) => m.label === '图片超链接')!.callback!()
526
+ expect(popup.calls).toEqual([
527
+ {
528
+ mode: 'image',
529
+ initial: { url: 'https://example.com' },
530
+ target: { kind: 'cell', addr: ADDR }
531
+ }
532
+ ])
533
+
534
+ menus.find((m) => m.label === '取消超链接')!.callback!()
535
+ expect(ctx.getCellImage(ADDR)).toEqual({ src: 'https://example.com/a.png' })
536
+ expect(ctx.undo()).toBe(true)
537
+ expect(ctx.getCellImage(ADDR)).toEqual({
538
+ src: 'https://example.com/a.png',
539
+ link: 'https://example.com'
540
+ })
541
+ })
542
+
543
+ it('移除图片:hasCellImage false 且 undo 还原', () => {
544
+ const sheet = new Sheet()
545
+ const ctx = createSheetContext(sheet)
546
+ ctx.insertCellImage(ADDR, { src: 'https://example.com/a.png' })
547
+ sheet.selectCell(ADDR)
548
+
549
+ buildBodyMenus(ctx).find((m) => m.label === '移除图片')!.callback!()
550
+ expect(ctx.hasCellImage(ADDR)).toBe(false)
551
+ expect(ctx.undo()).toBe(true)
552
+ expect(ctx.hasCellImage(ADDR)).toBe(true)
553
+ })
554
+
555
+ it('readonly:图片超链接/取消超链接/移除图片禁用、预览可用;句柄缺省:预览/图片超链接禁用', () => {
556
+ const sheet = new Sheet()
557
+ const ctx = createSheetContext(sheet)
558
+ ctx.insertCellImage(ADDR, { src: 'https://example.com/a.png', link: 'https://e.com' })
559
+ sheet.selectCell(ADDR)
560
+
561
+ const readonly = buildBodyMenus(ctx, {
562
+ readonly: true,
563
+ openHyperlinkPopup: () => {},
564
+ openImagePreview: () => {}
565
+ })
566
+ expect(readonly.find((m) => m.label === '预览')?.disabled).toBeFalsy()
567
+ expect(readonly.find((m) => m.label === '图片超链接')?.disabled).toBe(true)
568
+ expect(readonly.find((m) => m.label === '取消超链接')?.disabled).toBe(true)
569
+ expect(readonly.find((m) => m.label === '移除图片')?.disabled).toBe(true)
570
+
571
+ const noHandles = buildBodyMenus(ctx)
572
+ expect(noHandles.find((m) => m.label === '预览')?.disabled).toBe(true)
573
+ expect(noHandles.find((m) => m.label === '图片超链接')?.disabled).toBe(true)
574
+ expect(noHandles.find((m) => m.label === '移除图片')?.disabled).toBeFalsy()
575
+ })
576
+ })
@@ -0,0 +1,78 @@
1
+ import type { ContextmenuItem } from '@veltra/desktop'
2
+ import { contextmenu } from '@veltra/desktop'
3
+
4
+ import type { SheetContext } from '../../tools/context'
5
+ import type { FloatContextMenuPayload } from '../../types'
6
+ import type { HyperlinkImageTarget, ImagePreviewSource, OpenHyperlinkPopup } from './popup-helpers'
7
+ import { imageLinkInitial, imagePreviewSource } from './popup-helpers'
8
+
9
+ /** 浮动图菜单注入句柄(lightbox 预览 / 图片链接弹窗由 sheet.vue 挂载,保持构建纯函数可测) */
10
+ export interface FloatImageMenuHandles {
11
+ /** 打开 lightbox 预览;缺省时「预览」项禁用 */
12
+ openImagePreview?: (source: ImagePreviewSource) => void
13
+ /** 拉起图片超链接配置弹窗(image 模式);缺省时「设置超链接」项禁用 */
14
+ openHyperlinkPopup?: OpenHyperlinkPopup
15
+ }
16
+
17
+ /**
18
+ * 浮动图右键独立菜单:预览 / 设置超链接 / 取消超链接(无链接不出现)/ 删除图片。
19
+ * 设置走 hyperlink 弹窗 image 模式(确认 `updateImage(id, { link })`);取消直写
20
+ * `updateImage(id, { link: null })`(null 清除,可 undo);删除走 `removeImage`
21
+ * (命令系统,可 undo)。命中 id 不存在(并发删除)返回空数组,调用方不弹菜单。
22
+ */
23
+ export function buildFloatImageMenus(
24
+ ctx: SheetContext,
25
+ id: string,
26
+ handles?: FloatImageMenuHandles,
27
+ options?: { readonly?: boolean }
28
+ ): ContextmenuItem[] {
29
+ const image = ctx.getImages().find((item) => item.id === id)
30
+ if (!image) return []
31
+ const readonly = options?.readonly === true
32
+ const target: HyperlinkImageTarget = { kind: 'float', id }
33
+ const items: ContextmenuItem[] = [
34
+ {
35
+ label: '预览',
36
+ disabled: !handles?.openImagePreview,
37
+ callback: () => handles?.openImagePreview?.(imagePreviewSource(image))
38
+ },
39
+ {
40
+ label: '设置超链接',
41
+ disabled: readonly || !handles?.openHyperlinkPopup,
42
+ callback: () => handles?.openHyperlinkPopup?.('image', imageLinkInitial(image.link), target)
43
+ },
44
+ ...(image.link
45
+ ? [
46
+ {
47
+ label: '取消超链接',
48
+ disabled: readonly,
49
+ callback: () => ctx.updateImage(id, { link: null })
50
+ }
51
+ ]
52
+ : []),
53
+ { label: '删除图片', disabled: readonly, callback: () => ctx.removeImage(id) }
54
+ ]
55
+ return items
56
+ }
57
+
58
+ /**
59
+ * 消费引擎浮动图右键事件:按命中浮动图 link 状态构建独立菜单并经 `contextmenu.pop`
60
+ * 弹出(viewport 坐标定位,与 body 右键菜单同口径)。右键不穿透到格菜单由引擎
61
+ * 保证(floatObjects 命中即不派发单元格 contextmenu),此处只弹浮动图菜单。
62
+ */
63
+ export function popFloatImageMenu(options: {
64
+ ctx: SheetContext
65
+ event: FloatContextMenuPayload
66
+ handles?: FloatImageMenuHandles
67
+ readonly?: boolean
68
+ }): void {
69
+ const menus = buildFloatImageMenus(options.ctx, options.event.id, options.handles, {
70
+ readonly: options.readonly
71
+ })
72
+ if (menus.length === 0) return
73
+ contextmenu.pop({
74
+ mousePosition: { x: options.event.clientX, y: options.event.clientY },
75
+ width: 'max-content',
76
+ menus
77
+ })
78
+ }
@@ -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>
@@ -8,6 +8,8 @@ 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
10
  import { pickAndInsertImage } from './insert-image'
11
+ import type { ImagePreviewSource, OpenHyperlinkPopup } from './popup-helpers'
12
+ import { imageLinkInitial, imagePreviewSource } from './popup-helpers'
11
13
 
12
14
  /** 插入数量钳制范围(对齐 univer) */
13
15
  export const INSERT_COUNT_MIN = 1
@@ -263,11 +265,90 @@ export function applyNumFmtToSelection(ctx: SheetContext, numFmt: NumFmt): void
263
265
  for (const range of ctx.getSelection().ranges) ctx.applyStyle(range, { numFmt })
264
266
  }
265
267
 
266
- /** body 格右键菜单:合并/取消合并 + 设置数据格式 + 插入图片(行列插入/删除仅行号/列头菜单) */
267
- export function buildBodyMenus(ctx: SheetContext): ContextmenuItem[] {
268
+ /** body 格右键菜单选项(超链接 / 图片预览项行为注入) */
269
+ export interface BodyMenuOptions {
270
+ /** 只读模式:超链接与嵌入图写入项禁用(对齐行高/列宽 readonly 禁用约定) */
271
+ readonly?: boolean
272
+ /** 拉起超链接配置弹窗的句柄(缺省时插入/编辑项禁用,保持 build 纯函数可测) */
273
+ openHyperlinkPopup?: OpenHyperlinkPopup
274
+ /** 打开 lightbox 预览的句柄(缺省时嵌入图「预览」项禁用) */
275
+ openImagePreview?: (source: ImagePreviewSource) => void
276
+ }
277
+
278
+ /**
279
+ * body 菜单超链接项:按目标格链接状态出项——
280
+ * 无链接「插入超链接」;有链接「编辑超链接」(回填原 url/text)+「取消超链接」。
281
+ * 取消直走 clearCellLink(可 undo);插入/编辑经注入句柄拉起弹窗。
282
+ */
283
+ function cellLinkMenuItems(
284
+ ctx: SheetContext,
285
+ active: CellAddress | null | undefined,
286
+ options?: BodyMenuOptions
287
+ ): ContextmenuItem[] {
288
+ if (!active) return [{ label: '插入超链接', disabled: true }]
289
+ const handle = options?.openHyperlinkPopup
290
+ const disabled = options?.readonly === true || !handle
291
+ const link = ctx.getCellLink(active)
292
+ if (!link) {
293
+ return [{ label: '插入超链接', disabled, callback: () => handle?.('cell') }]
294
+ }
295
+ return [
296
+ { label: '编辑超链接', disabled, callback: () => handle?.('cell', link) },
297
+ {
298
+ label: '取消超链接',
299
+ disabled: options?.readonly === true,
300
+ callback: () => ctx.clearCellLink(active)
301
+ }
302
+ ]
303
+ }
304
+
305
+ /**
306
+ * body 菜单嵌入图项:目标格含嵌入图时附加——预览 / 图片超链接 / 取消超链接
307
+ * (按载荷 link 状态出项)/ 移除图片。设置走 hyperlink 弹窗 image 模式(确认后
308
+ * 由宿主覆盖写 `insertCellImage(addr, { ...payload, link })`);取消直写覆盖
309
+ * `link: undefined`(引擎 CellImagePayload 清除约定,其余字段保留);移除走
310
+ * `removeCellImage`(命令系统,可 undo)。无嵌入图返回空数组(不出现图片项)。
311
+ */
312
+ function cellImageMenuItems(
313
+ ctx: SheetContext,
314
+ active: CellAddress | null | undefined,
315
+ options?: BodyMenuOptions
316
+ ): ContextmenuItem[] {
317
+ if (!active || !ctx.hasCellImage(active)) return []
318
+ const payload = ctx.getCellImage(active)
319
+ if (!payload) return []
320
+ const handle = options?.openHyperlinkPopup
321
+ const readonly = options?.readonly === true
322
+ const items: ContextmenuItem[] = [
323
+ {
324
+ label: '预览',
325
+ disabled: !options?.openImagePreview,
326
+ callback: () => options?.openImagePreview?.(imagePreviewSource(payload))
327
+ },
328
+ {
329
+ label: '图片超链接',
330
+ disabled: readonly || !handle,
331
+ callback: () =>
332
+ handle?.('image', imageLinkInitial(payload.link), { kind: 'cell', addr: active })
333
+ }
334
+ ]
335
+ if (payload.link) {
336
+ items.push({
337
+ label: '取消超链接',
338
+ disabled: readonly,
339
+ callback: () => ctx.insertCellImage(active, { ...payload, link: undefined })
340
+ })
341
+ }
342
+ items.push({ label: '移除图片', disabled: readonly, callback: () => ctx.removeCellImage(active) })
343
+ return items
344
+ }
345
+
346
+ /** body 格右键菜单:合并/取消合并 + 设置数据格式 + 超链接 + 插入图片(+ 嵌入图四项;行列插入/删除仅行号/列头菜单) */
347
+ export function buildBodyMenus(ctx: SheetContext, options?: BodyMenuOptions): ContextmenuItem[] {
268
348
  const mergeTool = defaultToolRegistry.get('merge')
269
349
  const unmergeTool = defaultToolRegistry.get('unmerge')
270
350
  const active = ctx.getSelection().activeCell
351
+ const imageItems = cellImageMenuItems(ctx, active, options)
271
352
 
272
353
  return [
273
354
  {
@@ -302,7 +383,9 @@ export function buildBodyMenus(ctx: SheetContext): ContextmenuItem[] {
302
383
  }
303
384
  ]
304
385
  },
305
- { label: '插入图片', disabled: !active, callback: () => pickAndInsertImage(ctx) }
386
+ ...cellLinkMenuItems(ctx, active, options),
387
+ { label: '插入图片', disabled: !active, callback: () => pickAndInsertImage(ctx) },
388
+ ...(imageItems.length > 0 ? [{ divider: true } as const, ...imageItems] : [])
306
389
  ]
307
390
  }
308
391