@veltra/sheet 3.0.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/components/sheet/float-image-menu.js +59 -0
  2. package/dist/components/sheet/import-file.js +3 -1
  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/popups/import.worker.d.ts +4 -0
  6. package/dist/components/sheet/popups/import.worker.js +3 -1
  7. package/dist/components/sheet/sheet-context-menu.js +81 -7
  8. package/dist/components/sheet/sheet.js +112 -17
  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 +82 -2
  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 +54 -7
  17. package/package.json +2 -2
  18. package/src/components/sheet/__test__/float-image-menu.test.ts +130 -0
  19. package/src/components/sheet/__test__/grid-test-utils.ts +2 -3
  20. package/src/components/sheet/__test__/hyperlink-popup.test.ts +176 -0
  21. package/src/components/sheet/__test__/import-file.test.ts +33 -2
  22. package/src/components/sheet/__test__/sheet-component.test.ts +476 -18
  23. package/src/components/sheet/__test__/sheet-context-menu.test.ts +255 -2
  24. package/src/components/sheet/__test__/sheet-scrollbar.test.ts +23 -73
  25. package/src/components/sheet/__test__/sheet-theme.test.ts +212 -0
  26. package/src/components/sheet/float-image-menu.ts +78 -0
  27. package/src/components/sheet/import-file.ts +19 -2
  28. package/src/components/sheet/popup-helpers.ts +68 -2
  29. package/src/components/sheet/popups/hyperlink-popup.vue +112 -0
  30. package/src/components/sheet/popups/import.worker.ts +18 -4
  31. package/src/components/sheet/sheet-context-menu.ts +100 -7
  32. package/src/components/sheet/sheet.vue +120 -7
  33. package/src/components/sheet/style.scss +66 -0
  34. package/src/components/sheet/use-cell-link-tooltip.ts +42 -0
  35. package/src/components/sheet/use-sheet-grid.ts +111 -4
  36. package/src/components/sheet/use-sheet-state.ts +8 -2
  37. package/src/tools/__test__/context.test.ts +89 -0
  38. package/src/tools/context.ts +33 -2
  39. package/src/types/sheet.ts +59 -7
@@ -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
+ }
@@ -24,7 +24,18 @@ export interface ImportFileOptions {
24
24
  onWorkbookReplaced?: () => void
25
25
  }
26
26
 
27
- type ParsedXlsx = { sheets: { name: string; snapshot: SheetSnapshot }[]; activeIndex: number }
27
+ /** worker done 载荷 / 主线程降级解析共用的条目形状(结构化可赋给 SheetReplaceItem) */
28
+ type ParsedXlsx = {
29
+ sheets: {
30
+ name: string
31
+ snapshot: SheetSnapshot
32
+ /** 默认行高 px(模型未声明则不携带) */
33
+ defaultRowHeight?: number
34
+ /** 默认列宽 px(模型未声明则不携带) */
35
+ defaultColWidth?: number
36
+ }[]
37
+ activeIndex: number
38
+ }
28
39
 
29
40
  /**
30
41
  * xlsx 解析(worker 优先):importXlsx 是同步重活,主线程直接跑会冻结 UI。
@@ -79,7 +90,13 @@ async function parseXlsxAsync(
79
90
 
80
91
  function toSnapshotArray(wb: Workbook): ParsedXlsx {
81
92
  return {
82
- sheets: wb.getSheets().map((s) => ({ name: s.name, snapshot: s.snapshot() })),
93
+ sheets: wb.getSheets().map((s) => ({
94
+ name: s.name,
95
+ snapshot: s.snapshot(),
96
+ // 模型未声明默认尺寸不序列化(口径同 snapshot() 的条件字段写法)
97
+ ...(s.defaultRowHeight !== undefined ? { defaultRowHeight: s.defaultRowHeight } : {}),
98
+ ...(s.defaultColWidth !== undefined ? { defaultColWidth: s.defaultColWidth } : {})
99
+ })),
83
100
  activeIndex: wb.activeSheetIndex
84
101
  }
85
102
  }
@@ -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>
@@ -5,8 +5,9 @@
5
5
  *
6
6
  * 协议:主线程 postMessage({ buffer: ArrayBuffer }) → worker 返回
7
7
  * - { type: 'progress', done, total }(模型分片构建进度,xlsx 解析段无进度)
8
- * - { type: 'done', ok: true, sheets: [{ name, snapshot }], activeIndex }(快照为
9
- * 纯数据,可结构化克隆);失败 { type: 'done', ok: false, error }。
8
+ * - { type: 'done', ok: true, sheets: [{ name, snapshot, defaultRowHeight?,
9
+ * defaultColWidth? }], activeIndex }(快照为纯数据,可结构化克隆;默认行列
10
+ * 尺寸未声明不携带);失败 { type: 'done', ok: false, error }。
10
11
  *
11
12
  * 解析与构建经 infinitable/sheet importXlsx 完成(hucre 已内联进引擎统一包);
12
13
  * 名字唯一化与活动表对齐在 worker 内完成,主线程直接把快照替换进目标
@@ -22,7 +23,14 @@ export type ImportWorkerResponse =
22
23
  | {
23
24
  type: 'done'
24
25
  ok: true
25
- sheets: { name: string; snapshot: SheetSnapshot }[]
26
+ sheets: {
27
+ name: string
28
+ snapshot: SheetSnapshot
29
+ /** 默认行高 px(模型未声明则不携带;形状同上游 SheetReplaceItem 对应字段) */
30
+ defaultRowHeight?: number
31
+ /** 默认列宽 px(模型未声明则不携带;形状同上游 SheetReplaceItem 对应字段) */
32
+ defaultColWidth?: number
33
+ }[]
26
34
  activeIndex: number
27
35
  }
28
36
  | { type: 'done'; ok: false; error: string }
@@ -43,7 +51,13 @@ self.onmessage = (e: MessageEvent<{ buffer: ArrayBuffer }>): void => {
43
51
  ;(self as unknown as Worker).postMessage(progress)
44
52
  }
45
53
  })
46
- const sheets = wb.getSheets().map((s) => ({ name: s.name, snapshot: s.snapshot() }))
54
+ const sheets = wb.getSheets().map((s) => ({
55
+ name: s.name,
56
+ snapshot: s.snapshot(),
57
+ // 模型未声明默认尺寸不序列化(口径同 snapshot() 的条件字段写法)
58
+ ...(s.defaultRowHeight !== undefined ? { defaultRowHeight: s.defaultRowHeight } : {}),
59
+ ...(s.defaultColWidth !== undefined ? { defaultColWidth: s.defaultColWidth } : {})
60
+ }))
47
61
  const response: ImportWorkerResponse = {
48
62
  type: 'done',
49
63
  ok: true,
@@ -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
@@ -106,6 +108,10 @@ function insertCountRender(options: {
106
108
  export interface HeaderMenuOptions {
107
109
  /** 只读模式:行高/列宽项禁用(尺寸写入属编辑操作) */
108
110
  readonly?: boolean
111
+ /** 右键命中的行号(行头菜单):冻结以点击行为准,缺省回落选区末端 */
112
+ row?: number
113
+ /** 右键命中的列号(列头菜单):冻结以点击列为准,缺省回落选区末端 */
114
+ col?: number
109
115
  }
110
116
 
111
117
  /**
@@ -148,7 +154,11 @@ export function buildRowHeaderMenus(
148
154
  const rowCount = defaultInsertCount(range, 'row')
149
155
  const deleteCount = range ? endRow - startRow + 1 : 1
150
156
  const frozen = ctx.frozen
151
- const freezeActive = frozen.rows === endRow + 1
157
+ // 冻结目标 = 右键命中的行;缺省回落选区末端。不能直接用选区末端:先冻结行的
158
+ // 整行选区横跨全部列,随后列头右键判定「在选区内」保留选区,选区末端列会把
159
+ // 冻结列放大到全部列(反之亦然),表现为冻结不生效
160
+ const freezeRow = (options?.row ?? endRow) + 1
161
+ const freezeActive = frozen.rows === freezeRow
152
162
 
153
163
  return [
154
164
  {
@@ -181,7 +191,7 @@ export function buildRowHeaderMenus(
181
191
  { divider: true },
182
192
  {
183
193
  label: freezeActive ? '✓ 冻结到当前行' : '冻结到当前行',
184
- callback: () => ctx.setFrozen(endRow + 1, frozen.cols)
194
+ callback: () => ctx.setFrozen(freezeRow, frozen.cols)
185
195
  },
186
196
  {
187
197
  label: '取消冻结',
@@ -202,7 +212,9 @@ export function buildColHeaderMenus(
202
212
  const colCount = defaultInsertCount(range, 'col')
203
213
  const deleteCount = range ? endCol - startCol + 1 : 1
204
214
  const frozen = ctx.frozen
205
- const freezeActive = frozen.cols === endCol + 1
215
+ // 冻结目标 = 右键命中的列(同 buildRowHeaderMenus:整行/整列选区残留时选区末端会放大冻结范围)
216
+ const freezeCol = (options?.col ?? endCol) + 1
217
+ const freezeActive = frozen.cols === freezeCol
206
218
 
207
219
  return [
208
220
  {
@@ -235,7 +247,7 @@ export function buildColHeaderMenus(
235
247
  { divider: true },
236
248
  {
237
249
  label: freezeActive ? '✓ 冻结到当前列' : '冻结到当前列',
238
- callback: () => ctx.setFrozen(frozen.rows, endCol + 1)
250
+ callback: () => ctx.setFrozen(frozen.rows, freezeCol)
239
251
  },
240
252
  {
241
253
  label: '取消冻结',
@@ -253,11 +265,90 @@ export function applyNumFmtToSelection(ctx: SheetContext, numFmt: NumFmt): void
253
265
  for (const range of ctx.getSelection().ranges) ctx.applyStyle(range, { numFmt })
254
266
  }
255
267
 
256
- /** body 格右键菜单:合并/取消合并 + 设置数据格式 + 插入图片(行列插入/删除仅行号/列头菜单) */
257
- 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[] {
258
348
  const mergeTool = defaultToolRegistry.get('merge')
259
349
  const unmergeTool = defaultToolRegistry.get('unmerge')
260
350
  const active = ctx.getSelection().activeCell
351
+ const imageItems = cellImageMenuItems(ctx, active, options)
261
352
 
262
353
  return [
263
354
  {
@@ -292,7 +383,9 @@ export function buildBodyMenus(ctx: SheetContext): ContextmenuItem[] {
292
383
  }
293
384
  ]
294
385
  },
295
- { 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] : [])
296
389
  ]
297
390
  }
298
391