GeezerVison is showing github.com — feat: optimize context menu details · cdlinsen/lowcode-engine@3627ae3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/docs/api/material.md +37Lines changed: 37 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -237,6 +237,43 @@ material.modifyBuiltinComponentAction('remove', (action) => { | 237 | 237 | }); | 238 | 238 | ``` | 239 | 239 |
| | 240 | +### 右键菜单项 | | 241 | +#### addContextMenuOption | | 242 | + | | 243 | +添加右键菜单项 | | 244 | + | | 245 | +```typescript | | 246 | +/** | | 247 | + * 添加右键菜单项 | | 248 | + * @param action | | 249 | + */ | | 250 | +addContextMenuOption(action: IPublicTypeContextMenuAction): void; | | 251 | +``` | | 252 | + | | 253 | +#### removeContextMenuOption | | 254 | + | | 255 | +删除特定右键菜单项 | | 256 | + | | 257 | +```typescript | | 258 | +/** | | 259 | + * 删除特定右键菜单项 | | 260 | + * @param name | | 261 | + */ | | 262 | +removeContextMenuOption(name: string): void; | | 263 | +``` | | 264 | + | | 265 | +#### adjustContextMenuLayout | | 266 | + | | 267 | +调整右键菜单项布局,每次调用都会覆盖之前注册的调整函数,只有最后注册的函数会被应用。 | | 268 | + | | 269 | +```typescript | | 270 | +/** | | 271 | + * 调整右键菜单项布局 | | 272 | + * @param actions | | 273 | + */ | | 274 | +adjustContextMenuLayout(fn: (actions: IPublicTypeContextMenuItem[]) => IPublicTypeContextMenuItem[]): void; | | 275 | +``` | | 276 | + | 240 | 277 | ### 物料元数据 | 241 | 278 | #### getComponentMeta | 242 | 279 | 获取指定名称的物料元数据 | |
Collapse file Copy file name to clipboardExpand all lines: packages/designer/src/context-menu-actions.ts +4-12Lines changed: 4 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { IPublicTypeContextMenuAction, IPublicEnumContextMenuType, IPublicTypeContextMenuItem, IPublicApiMaterial } from '@alilc/lowcode-types'; | 2 | 2 | import { IDesigner, INode } from './designer'; | 3 | | -import { parseContextMenuAsReactNode, parseContextMenuProperties, uniqueId } from '@alilc/lowcode-utils'; | | 3 | +import { createContextMenu, parseContextMenuAsReactNode, parseContextMenuProperties, uniqueId } from '@alilc/lowcode-utils'; | 4 | 4 | import { Menu } from '@alifd/next'; | 5 | 5 | import { engineConfig } from '@alilc/lowcode-editor-core'; | 6 | 6 | import './context-menu-actions.scss'; | @@ -178,18 +178,10 @@ export class ContextMenuActions implements IContextMenuActions { | 178 | 178 | designer, | 179 | 179 | }); | 180 | 180 |
| 181 | | - const target = event.target; | 182 | | - | 183 | | - const { top, left } = target?.getBoundingClientRect(); | 184 | | - | 185 | | - const menuInstance = Menu.create({ | 186 | | - target: event.target, | 187 | | - offset: [event.clientX - left + simulatorLeft, event.clientY - top + simulatorTop], | 188 | | - children: menuNode, | 189 | | - className: 'engine-context-menu', | | 181 | + destroyFn = createContextMenu(menuNode, { | | 182 | + event, | | 183 | + offset: [simulatorLeft, simulatorTop], | 190 | 184 | }); | 191 | | - | 192 | | - destroyFn = (menuInstance as any).destroy; | 193 | 185 | }; | 194 | 186 |
| 195 | 187 | initEvent() { | |
Collapse file Copy file name to clipboardExpand all lines: packages/engine/src/inner-plugins/default-context-menu.ts +75-15Lines changed: 75 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { | 2 | 2 | IPublicEnumContextMenuType, | | 3 | + IPublicEnumDragObjectType, | 3 | 4 | IPublicEnumTransformStage, | 4 | 5 | IPublicModelNode, | 5 | 6 | IPublicModelPluginContext, | | 7 | + IPublicTypeDragNodeDataObject, | | 8 | + IPublicTypeI18nData, | 6 | 9 | IPublicTypeNodeSchema, | 7 | 10 | } from '@alilc/lowcode-types'; | 8 | | -import { isProjectSchema } from '@alilc/lowcode-utils'; | | 11 | +import { isI18nData, isProjectSchema } from '@alilc/lowcode-utils'; | 9 | 12 | import { Notification } from '@alifd/next'; | 10 | | -import { intl } from '../locale'; | | 13 | +import { intl, getLocale } from '../locale'; | 11 | 14 |
| 12 | 15 | function getNodesSchema(nodes: IPublicModelNode[]) { | 13 | 16 | const componentsTree = nodes.map((node) => node?.exportSchema(IPublicEnumTransformStage.Clone)); | 14 | 17 | const data = { type: 'nodeSchema', componentsMap: {}, componentsTree }; | 15 | 18 | return data; | 16 | 19 | } | 17 | 20 |
| | 21 | +function getIntlStr(data: string | IPublicTypeI18nData) { | | 22 | + if (!isI18nData(data)) { | | 23 | + return data; | | 24 | + } | | 25 | + | | 26 | + const locale = getLocale(); | | 27 | + return data[locale] || data['zh-CN'] || data['zh_CN'] || data['en-US'] || data['en_US'] || ''; | | 28 | +} | | 29 | + | 18 | 30 | async function getClipboardText(): Promise<IPublicTypeNodeSchema[]> { | 19 | 31 | return new Promise((resolve, reject) => { | 20 | 32 | // 使用 Clipboard API 读取剪贴板内容 | @@ -71,12 +83,18 @@ export const defaultContextMenu = (ctx: IPublicModelPluginContext) => { | 71 | 83 | material.addContextMenuOption({ | 72 | 84 | name: 'copyAndPaste', | 73 | 85 | title: intl('CopyAndPaste'), | | 86 | + disabled: (nodes) => { | | 87 | + return nodes?.filter((node) => !node?.canPerformAction('copy')).length > 0; | | 88 | + }, | 74 | 89 | condition: (nodes) => { | 75 | 90 | return nodes.length === 1; | 76 | 91 | }, | 77 | 92 | action(nodes) { | 78 | 93 | const node = nodes[0]; | 79 | 94 | const { document: doc, parent, index } = node; | | 95 | + const data = getNodesSchema(nodes); | | 96 | + clipboard.setData(data); | | 97 | + | 80 | 98 | if (parent) { | 81 | 99 | const newNode = doc?.insertNode(parent, node, (index ?? 0) + 1, true); | 82 | 100 | newNode?.select(); | @@ -87,6 +105,9 @@ export const defaultContextMenu = (ctx: IPublicModelPluginContext) => { | 87 | 105 | material.addContextMenuOption({ | 88 | 106 | name: 'copy', | 89 | 107 | title: intl('Copy'), | | 108 | + disabled: (nodes) => { | | 109 | + return nodes?.filter((node) => !node?.canPerformAction('copy')).length > 0; | | 110 | + }, | 90 | 111 | condition(nodes) { | 91 | 112 | return nodes.length > 0; | 92 | 113 | }, | @@ -101,7 +122,7 @@ export const defaultContextMenu = (ctx: IPublicModelPluginContext) => { | 101 | 122 | }); | 102 | 123 |
| 103 | 124 | material.addContextMenuOption({ | 104 | | - name: 'zhantieToBottom', | | 125 | + name: 'pasteToBottom', | 105 | 126 | title: intl('PasteToTheBottom'), | 106 | 127 | condition: (nodes) => { | 107 | 128 | return nodes.length === 1; | @@ -116,10 +137,30 @@ export const defaultContextMenu = (ctx: IPublicModelPluginContext) => { | 116 | 137 |
| 117 | 138 | try { | 118 | 139 | const nodeSchema = await getClipboardText(); | | 140 | + if (nodeSchema.length === 0) { | | 141 | + return; | | 142 | + } | 119 | 143 | if (parent) { | 120 | | - nodeSchema.forEach((schema, schemaIndex) => { | 121 | | - doc?.insertNode(parent, schema, (index ?? 0) + 1 + schemaIndex, true); | | 144 | + let canAddNodes = nodeSchema.filter((nodeSchema: IPublicTypeNodeSchema) => { | | 145 | + const dragNodeObject: IPublicTypeDragNodeDataObject = { | | 146 | + type: IPublicEnumDragObjectType.NodeData, | | 147 | + data: nodeSchema, | | 148 | + }; | | 149 | + return doc?.checkNesting(parent, dragNodeObject); | | 150 | + }); | | 151 | + if (canAddNodes.length === 0) { | | 152 | + Notification.open({ | | 153 | + content: `${nodeSchema.map(d => getIntlStr(d.title || d.componentName)).join(',')}等组件无法放置到${getIntlStr(parent.title || parent.componentName as any)}内`, | | 154 | + type: 'error', | | 155 | + }); | | 156 | + return; | | 157 | + } | | 158 | + const nodes: IPublicModelNode[] = []; | | 159 | + canAddNodes.forEach((schema, schemaIndex) => { | | 160 | + const node = doc?.insertNode(parent, schema, (index ?? 0) + 1 + schemaIndex, true); | | 161 | + node && nodes.push(node); | 122 | 162 | }); | | 163 | + doc?.selection.selectAll(nodes.map((node) => node?.id)); | 123 | 164 | } | 124 | 165 | } catch (error) { | 125 | 166 | console.error(error); | @@ -128,7 +169,7 @@ export const defaultContextMenu = (ctx: IPublicModelPluginContext) => { | 128 | 169 | }); | 129 | 170 |
| 130 | 171 | material.addContextMenuOption({ | 131 | | - name: 'zhantieToInner', | | 172 | + name: 'pasteToInner', | 132 | 173 | title: intl('PasteToTheInside'), | 133 | 174 | condition: (nodes) => { | 134 | 175 | return nodes.length === 1; | @@ -140,19 +181,35 @@ export const defaultContextMenu = (ctx: IPublicModelPluginContext) => { | 140 | 181 | }, | 141 | 182 | async action(nodes) { | 142 | 183 | const node = nodes[0]; | 143 | | - const { document: doc, parent } = node; | | 184 | + const { document: doc } = node; | 144 | 185 |
| 145 | 186 | try { | 146 | 187 | const nodeSchema = await getClipboardText(); | 147 | | - if (parent) { | 148 | | - const index = node.children?.size || 0; | 149 | | - | 150 | | - if (parent) { | 151 | | - nodeSchema.forEach((schema, schemaIndex) => { | 152 | | - doc?.insertNode(node, schema, (index ?? 0) + 1 + schemaIndex, true); | 153 | | - }); | 154 | | - } | | 188 | + const index = node.children?.size || 0; | | 189 | + if (nodeSchema.length === 0) { | | 190 | + return; | 155 | 191 | } | | 192 | + let canAddNodes = nodeSchema.filter((nodeSchema: IPublicTypeNodeSchema) => { | | 193 | + const dragNodeObject: IPublicTypeDragNodeDataObject = { | | 194 | + type: IPublicEnumDragObjectType.NodeData, | | 195 | + data: nodeSchema, | | 196 | + }; | | 197 | + return doc?.checkNesting(node, dragNodeObject); | | 198 | + }); | | 199 | + if (canAddNodes.length === 0) { | | 200 | + Notification.open({ | | 201 | + content: `${nodeSchema.map(d => getIntlStr(d.title || d.componentName)).join(',')}等组件无法放置到${getIntlStr(node.title || node.componentName as any)}内`, | | 202 | + type: 'error', | | 203 | + }); | | 204 | + return; | | 205 | + } | | 206 | + | | 207 | + const nodes: IPublicModelNode[] = []; | | 208 | + nodeSchema.forEach((schema, schemaIndex) => { | | 209 | + const newNode = doc?.insertNode(node, schema, (index ?? 0) + 1 + schemaIndex, true); | | 210 | + newNode && nodes.push(newNode); | | 211 | + }); | | 212 | + doc?.selection.selectAll(nodes.map((node) => node?.id)); | 156 | 213 | } catch (error) { | 157 | 214 | console.error(error); | 158 | 215 | } | @@ -162,6 +219,9 @@ export const defaultContextMenu = (ctx: IPublicModelPluginContext) => { | 162 | 219 | material.addContextMenuOption({ | 163 | 220 | name: 'delete', | 164 | 221 | title: intl('Delete'), | | 222 | + disabled(nodes) { | | 223 | + return nodes?.filter((node) => !node?.canPerformAction('remove')).length > 0; | | 224 | + }, | 165 | 225 | condition(nodes) { | 166 | 226 | return nodes.length > 0; | 167 | 227 | }, | |
Collapse file Copy file name to clipboardExpand all lines: packages/engine/src/locale/index.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,7 +2,7 @@ import { createIntl } from '@alilc/lowcode-editor-core'; | 2 | 2 | import enUS from './en-US.json'; | 3 | 3 | import zhCN from './zh-CN.json'; | 4 | 4 |
| 5 | | -const { intl } = createIntl?.({ | | 5 | +const { intl, getLocale } = createIntl?.({ | 6 | 6 | 'en-US': enUS, | 7 | 7 | 'zh-CN': zhCN, | 8 | 8 | }) || { | @@ -11,4 +11,4 @@ const { intl } = createIntl?.({ | 11 | 11 | }, | 12 | 12 | }; | 13 | 13 |
| 14 | | -export { intl, enUS, zhCN }; | | 14 | +export { intl, enUS, zhCN, getLocale }; |
Collapse file Copy file name to clipboardExpand all lines: packages/shell/src/components/context-menu.tsx +3-10Lines changed: 3 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import { Menu } from '@alifd/next'; | 2 | | -import { parseContextMenuAsReactNode, parseContextMenuProperties } from '@alilc/lowcode-utils'; | | 1 | +import { createContextMenu, parseContextMenuAsReactNode, parseContextMenuProperties } from '@alilc/lowcode-utils'; | 3 | 2 | import { engineConfig } from '@alilc/lowcode-editor-core'; | 4 | 3 | import { IPublicTypeContextMenuAction } from '@alilc/lowcode-types'; | 5 | 4 | import React from 'react'; | @@ -18,8 +17,6 @@ export function ContextMenu({ children, menus }: { | 18 | 17 | event.preventDefault(); | 19 | 18 | event.stopPropagation(); | 20 | 19 |
| 21 | | - const target = event.target; | 22 | | - const { top, left } = target?.getBoundingClientRect(); | 23 | 20 | let destroyFn: Function | undefined; | 24 | 21 | const destroy = () => { | 25 | 22 | destroyFn?.(); | @@ -32,13 +29,9 @@ export function ContextMenu({ children, menus }: { | 32 | 29 | return; | 33 | 30 | } | 34 | 31 |
| 35 | | - const menuInstance = Menu.create({ | 36 | | - target: event.target, | 37 | | - offset: [event.clientX - left, event.clientY - top], | 38 | | - children, | | 32 | + destroyFn = createContextMenu(children, { | | 33 | + event, | 39 | 34 | }); | 40 | | - | 41 | | - destroyFn = (menuInstance as any).destroy; | 42 | 35 | }; | 43 | 36 |
| 44 | 37 | // 克隆 children 并添加 onContextMenu 事件处理器 | |
Collapse file Copy file name to clipboardExpand all lines: packages/utils/src/context-menu.tsx +53-2Lines changed: 53 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,6 +53,8 @@ const Tree = (props: { | 53 | 53 | ); | 54 | 54 | }; | 55 | 55 |
| | 56 | +let destroyFn: Function | undefined; | | 57 | + | 56 | 58 | export function parseContextMenuAsReactNode(menus: IPublicTypeContextMenuItem[], options: { | 57 | 59 | nodes?: IPublicModelNode[] | null; | 58 | 60 | destroy?: Function; | @@ -89,14 +91,12 @@ export function parseContextMenuAsReactNode(menus: IPublicTypeContextMenuItem[], | 89 | 91 | return children; | 90 | 92 | } | 91 | 93 |
| 92 | | -let destroyFn: Function | undefined; | 93 | 94 | export function parseContextMenuProperties(menus: (IPublicTypeContextMenuAction | Omit<IPublicTypeContextMenuAction, 'items'>)[], options: { | 94 | 95 | nodes?: IPublicModelNode[] | null; | 95 | 96 | destroy?: Function; | 96 | 97 | event?: MouseEvent; | 97 | 98 | }, level = 1): IPublicTypeContextMenuItem[] { | 98 | 99 | destroyFn?.(); | 99 | | - destroyFn = options.destroy; | 100 | 100 |
| 101 | 101 | const { nodes, destroy } = options; | 102 | 102 | if (level > MAX_LEVEL) { | @@ -146,4 +146,55 @@ export function parseContextMenuProperties(menus: (IPublicTypeContextMenuAction | 146 | 146 | return menus; | 147 | 147 | } | 148 | 148 | }, []); | | 149 | +} | | 150 | + | | 151 | +let cachedMenuItemHeight: string | undefined; | | 152 | + | | 153 | +function getMenuItemHeight() { | | 154 | + if (cachedMenuItemHeight) { | | 155 | + return cachedMenuItemHeight; | | 156 | + } | | 157 | + const root = document.documentElement; | | 158 | + const styles = getComputedStyle(root); | | 159 | + // Access the value of the CSS variable | | 160 | + const menuItemHeight = styles.getPropertyValue('--context-menu-item-height').trim(); | | 161 | + cachedMenuItemHeight = menuItemHeight; | | 162 | + | | 163 | + return menuItemHeight; | | 164 | +} | | 165 | + | | 166 | +export function createContextMenu(children: React.ReactNode[], { | | 167 | + event, | | 168 | + offset = [0, 0], | | 169 | +}: { | | 170 | + event: MouseEvent | React.MouseEvent; | | 171 | + offset?: [number, number]; | | 172 | +}) { | | 173 | + const viewportWidth = window.innerWidth; | | 174 | + const viewportHeight = window.innerHeight; | | 175 | + const dividerCount = React.Children.count(children.filter(child => React.isValidElement(child) && child.type === Divider)); | | 176 | + const popupItemCount = React.Children.count(children.filter(child => React.isValidElement(child) && (child.type === PopupItem || child.type === Item))); | | 177 | + const menuHeight = popupItemCount * parseInt(getMenuItemHeight(), 10) + dividerCount * 8 + 16; | | 178 | + const menuWidthLimit = 200; | | 179 | + const target = event.target; | | 180 | + const { top, left } = (target as any)?.getBoundingClientRect(); | | 181 | + let x = event.clientX - left + offset[0]; | | 182 | + let y = event.clientY - top + offset[1]; | | 183 | + if (x + menuWidthLimit + left > viewportWidth) { | | 184 | + x = x - menuWidthLimit; | | 185 | + } | | 186 | + if (y + menuHeight + top > viewportHeight) { | | 187 | + y = y - menuHeight; | | 188 | + } | | 189 | + | | 190 | + const menuInstance = Menu.create({ | | 191 | + target, | | 192 | + offset: [x, y, 0, 0], | | 193 | + children, | | 194 | + className: 'engine-context-menu', | | 195 | + }); | | 196 | + | | 197 | + destroyFn = (menuInstance as any).destroy; | | 198 | + | | 199 | + return destroyFn; | 149 | 200 | } |
|