From 149c95f9522a9832bcf91ec259fd40f146ff80c3 Mon Sep 17 00:00:00 2001 From: Hufe921 Date: Sat, 18 May 2024 20:56:58 +0800 Subject: [PATCH] feat: add table context to contextmenu and getRangeContext api #576 --- docs/en/guide/contextmenu-custom.md | 12 ++++++++++ docs/guide/contextmenu-custom.md | 12 ++++++++++ src/editor/core/command/CommandAdapt.ts | 18 ++++++++++++--- src/editor/core/contextmenu/ContextMenu.ts | 23 +++++++++++++++---- src/editor/interface/Range.ts | 3 +++ .../interface/contextmenu/ContextMenu.ts | 3 +++ 6 files changed, 63 insertions(+), 8 deletions(-) diff --git a/docs/en/guide/contextmenu-custom.md b/docs/en/guide/contextmenu-custom.md index 6eab72c..1643254 100644 --- a/docs/en/guide/contextmenu-custom.md +++ b/docs/en/guide/contextmenu-custom.md @@ -30,3 +30,15 @@ Usage: ```javascript const contextMenuList = await instance.register.getContextMenuList() ``` + +Remark: + +```javascript +// Example of modifying internal contextmenu +contextmenuList.forEach(menu => { + // Find the menu item through the menu key and modify its properties + if (menu.key === INTERNAL_CONTEXT_MENU_KEY.GLOBAL.PASTE) { + menu.when = () => false + } +}) +``` diff --git a/docs/guide/contextmenu-custom.md b/docs/guide/contextmenu-custom.md index ee1004f..518b7bf 100644 --- a/docs/guide/contextmenu-custom.md +++ b/docs/guide/contextmenu-custom.md @@ -30,3 +30,15 @@ instance.register.contextMenuList([ ```javascript const contextMenuList = await instance.register.getContextMenuList() ``` + +备注: + +```javascript +// 修改内部右键菜单示例 +contextmenuList.forEach(menu => { + // 通过菜单key找到菜单项后进行属性修改 + if (menu.key === INTERNAL_CONTEXT_MENU_KEY.GLOBAL.PASTE) { + menu.when = () => false + } +}) +``` diff --git a/src/editor/core/command/CommandAdapt.ts b/src/editor/core/command/CommandAdapt.ts index 611c7aa..f869289 100644 --- a/src/editor/core/command/CommandAdapt.ts +++ b/src/editor/core/command/CommandAdapt.ts @@ -2136,8 +2136,17 @@ export class CommandAdapt { // 区域信息 const zone = this.draw.getZone().getZone() // 表格信息 - const isTable = this.position.getPositionContext().isTable - return deepClone({ + const { isTable, trIndex, tdIndex, index } = + this.position.getPositionContext() + let tableElement: IElement | null = null + if (isTable) { + const originalElementList = this.draw.getOriginalElementList() + const originTableElement = originalElementList[index!] || null + if (originTableElement) { + tableElement = zipElementList([originTableElement])[0] + } + } + return deepClone({ isCollapsed, startElement, endElement, @@ -2145,7 +2154,10 @@ export class CommandAdapt { endPageNo, rangeRects, zone, - isTable + isTable, + trIndex: trIndex ?? null, + tdIndex: tdIndex ?? null, + tableElement }) } diff --git a/src/editor/core/contextmenu/ContextMenu.ts b/src/editor/core/contextmenu/ContextMenu.ts index df2a612..4cf4c6a 100644 --- a/src/editor/core/contextmenu/ContextMenu.ts +++ b/src/editor/core/contextmenu/ContextMenu.ts @@ -3,11 +3,13 @@ import { EDITOR_COMPONENT, EDITOR_PREFIX } from '../../dataset/constant/Editor' import { EditorComponent } from '../../dataset/enum/Editor' import { DeepRequired } from '../../interface/Common' import { IEditorOption } from '../../interface/Editor' +import { IElement } from '../../interface/Element' import { IContextMenuContext, IRegisterContextMenu } from '../../interface/contextmenu/ContextMenu' import { findParent } from '../../utils' +import { zipElementList } from '../../utils/element' import { Command } from '../command/Command' import { Draw } from '../draw/Draw' import { I18n } from '../i18n/I18n' @@ -150,10 +152,18 @@ export class ContextMenu { // 是否存在选区 const editorHasSelection = editorTextFocus && startIndex !== endIndex // 是否在表格内 - const positionContext = this.position.getPositionContext() - const isInTable = positionContext.isTable + const { isTable, trIndex, tdIndex, index } = + this.position.getPositionContext() + let tableElement: IElement | null = null + if (isTable) { + const originalElementList = this.draw.getOriginalElementList() + const originTableElement = originalElementList[index!] || null + if (originTableElement) { + tableElement = zipElementList([originTableElement])[0] + } + } // 是否存在跨行/列 - const isCrossRowCol = isInTable && !!crossRowCol + const isCrossRowCol = isTable && !!crossRowCol // 当前元素 const elementList = this.draw.getElementList() const startElement = elementList[startIndex] || null @@ -166,9 +176,12 @@ export class ContextMenu { isReadonly, editorHasSelection, editorTextFocus, - isInTable, isCrossRowCol, - zone + zone, + isInTable: isTable, + trIndex: trIndex ?? null, + tdIndex: tdIndex ?? null, + tableElement } } diff --git a/src/editor/interface/Range.ts b/src/editor/interface/Range.ts index 15a6f35..74f4be0 100644 --- a/src/editor/interface/Range.ts +++ b/src/editor/interface/Range.ts @@ -28,6 +28,9 @@ export type RangeContext = { rangeRects: RangeRect[] zone: EditorZone isTable: boolean + trIndex: number | null + tdIndex: number | null + tableElement: IElement | null } export interface IRangeParagraphInfo { diff --git a/src/editor/interface/contextmenu/ContextMenu.ts b/src/editor/interface/contextmenu/ContextMenu.ts index cdcaa5e..6ca3440 100644 --- a/src/editor/interface/contextmenu/ContextMenu.ts +++ b/src/editor/interface/contextmenu/ContextMenu.ts @@ -11,6 +11,9 @@ export interface IContextMenuContext { isInTable: boolean isCrossRowCol: boolean zone: EditorZone + trIndex: number | null + tdIndex: number | null + tableElement: IElement | null } export interface IRegisterContextMenu {