GeezerVison is showing github.com — feat(context-menu): update context-menu docs, details, styles · jeffywin/lowcode-engine@bb5d7dd
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/commonUI.md +62Lines changed: 62 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -48,8 +48,70 @@ CommonUI API 是一个专为低代码引擎设计的组件 UI 库,使用它开 | 48 | 48 | | condition | 显示条件函数<br/>Function to determine display condition | (nodes: IPublicModelNode[]) => boolean (optional) | | | 49 | 49 | | disabled | 禁用条件函数,可选<br/>Function to determine disabled condition, optional | (nodes: IPublicModelNode[]) => boolean (optional) | | | 50 | 50 |
| | 51 | +**ContextMenu 示例** | | 52 | + | | 53 | +```typescript | | 54 | +const App = () => { | | 55 | + const menuItems: IPublicTypeContextMenuAction[] = [ | | 56 | + { | | 57 | + name: 'a', | | 58 | + title: '选项 1', | | 59 | + action: () => console.log('选项 1 被点击'), | | 60 | + }, | | 61 | + { | | 62 | + name: 'b', | | 63 | + title: '选项 2', | | 64 | + action: () => console.log('选项 2 被点击'), | | 65 | + }, | | 66 | + ]; | | 67 | + | | 68 | + const ContextMenu = ctx.commonUI.ContextMenu; | | 69 | + | | 70 | + return ( | | 71 | + <div> | | 72 | + <ContextMenu menus={menuItems}> | | 73 | + <div>右键点击这里</div> | | 74 | + </ContextMenu> | | 75 | + </div> | | 76 | + ); | | 77 | +}; | | 78 | + | | 79 | +export default App; | | 80 | +``` | | 81 | + | | 82 | +**ContextMenu.create 示例** | | 83 | + | | 84 | +```typescript | | 85 | +const App = () => { | | 86 | + const menuItems: IPublicTypeContextMenuAction[] = [ | | 87 | + { | | 88 | + name: 'a', | | 89 | + title: '选项 1', | | 90 | + action: () => console.log('选项 1 被点击'), | | 91 | + }, | | 92 | + { | | 93 | + name: 'b', | | 94 | + title: '选项 2', | | 95 | + action: () => console.log('选项 2 被点击'), | | 96 | + }, | | 97 | + ]; | | 98 | + | | 99 | + const ContextMenu = ctx.commonUI.ContextMenu; | | 100 | + | | 101 | + return ( | | 102 | + <div> | | 103 | + <div onClick={(e) => { | | 104 | + ContextMenu.create(menuItems, e); | | 105 | + }}>点击这里</div> | | 106 | + </div> | | 107 | + ); | | 108 | +}; | | 109 | + | | 110 | +export default App; | | 111 | +``` | 51 | 112 |
| 52 | 113 | ### Balloon | | 114 | + | 53 | 115 | 详细文档: [Balloon Documentation](https://fusion.design/pc/component/balloon) | 54 | 116 |
| 55 | 117 | ### Breadcrumb | |
Collapse file Copy file name to clipboardExpand all lines: docs/docs/api/material.md +46Lines changed: 46 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -250,6 +250,33 @@ material.modifyBuiltinComponentAction('remove', (action) => { | 250 | 250 | addContextMenuOption(action: IPublicTypeContextMenuAction): void; | 251 | 251 | ``` | 252 | 252 |
| | 253 | +示例 | | 254 | + | | 255 | +```typescript | | 256 | +import { IPublicEnumContextMenuType } from '@alilc/lowcode-types'; | | 257 | + | | 258 | +material.addContextMenuOption({ | | 259 | + name: 'parentItem', | | 260 | + title: 'Parent Item', | | 261 | + condition: (nodes) => true, | | 262 | + items: [ | | 263 | + { | | 264 | + name: 'childItem1', | | 265 | + title: 'Child Item 1', | | 266 | + action: (nodes) => console.log('Child Item 1 clicked', nodes), | | 267 | + condition: (nodes) => true | | 268 | + }, | | 269 | + // 分割线 | | 270 | + { | | 271 | + type: IPublicEnumContextMenuType.SEPARATOR | | 272 | + name: 'separator.1' | | 273 | + } | | 274 | + // 更多子菜单项... | | 275 | + ] | | 276 | +}); | | 277 | + | | 278 | +``` | | 279 | + | 253 | 280 | #### removeContextMenuOption | 254 | 281 |
| 255 | 282 | 删除特定右键菜单项 | @@ -274,7 +301,26 @@ removeContextMenuOption(name: string): void; | 274 | 301 | adjustContextMenuLayout(fn: (actions: IPublicTypeContextMenuItem[]) => IPublicTypeContextMenuItem[]): void; | 275 | 302 | ``` | 276 | 303 |
| | 304 | +**示例** | | 305 | + | | 306 | +通过 adjustContextMenuLayout 补充分割线 | | 307 | + | | 308 | +```typescript | | 309 | +material.adjustContextMenuLayout((actions: IPublicTypeContextMenuAction) => { | | 310 | + const names = ['a', 'b']; | | 311 | + const newActions = []; | | 312 | + actions.forEach(d => { | | 313 | + newActions.push(d); | | 314 | + if (names.include(d.name)) { | | 315 | + newActions.push({ type: 'separator' }) | | 316 | + } | | 317 | + }); | | 318 | + return newActions | | 319 | +}) | | 320 | +``` | | 321 | + | 277 | 322 | ### 物料元数据 | | 323 | + | 278 | 324 | #### getComponentMeta | 279 | 325 | 获取指定名称的物料元数据 | 280 | 326 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/designer/src/context-menu-actions.ts +9-2Lines changed: 9 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import { IPublicTypeContextMenuAction, IPublicEnumContextMenuType, IPublicTypeContextMenuItem, IPublicApiMaterial } from '@alilc/lowcode-types'; | | 1 | +import { IPublicTypeContextMenuAction, IPublicEnumContextMenuType, IPublicTypeContextMenuItem, IPublicApiMaterial, IPublicModelPluginContext } from '@alilc/lowcode-types'; | 2 | 2 | import { IDesigner, INode } from './designer'; | 3 | 3 | import { createContextMenu, parseContextMenuAsReactNode, parseContextMenuProperties, uniqueId } from '@alilc/lowcode-utils'; | 4 | 4 | import { Menu } from '@alifd/next'; | @@ -48,6 +48,7 @@ export class GlobalContextMenuActions { | 48 | 48 | event.preventDefault(); | 49 | 49 |
| 50 | 50 | const actions: IPublicTypeContextMenuAction[] = []; | | 51 | + let contextMenu: ContextMenuActions = this.contextMenuActionsMap.values().next().value; | 51 | 52 | this.contextMenuActionsMap.forEach((contextMenu) => { | 52 | 53 | actions.push(...contextMenu.actions); | 53 | 54 | }); | @@ -57,11 +58,13 @@ export class GlobalContextMenuActions { | 57 | 58 | const destroy = () => { | 58 | 59 | destroyFn?.(); | 59 | 60 | }; | | 61 | + const pluginContext: IPublicModelPluginContext = contextMenu.designer.editor.get('pluginContext') as IPublicModelPluginContext; | 60 | 62 |
| 61 | 63 | const menus: IPublicTypeContextMenuItem[] = parseContextMenuProperties(actions, { | 62 | 64 | nodes: [], | 63 | 65 | destroy, | 64 | 66 | event, | | 67 | + pluginContext, | 65 | 68 | }); | 66 | 69 |
| 67 | 70 | if (!menus.length) { | @@ -73,6 +76,7 @@ export class GlobalContextMenuActions { | 73 | 76 | const menuNode = parseContextMenuAsReactNode(layoutMenu, { | 74 | 77 | destroy, | 75 | 78 | nodes: [], | | 79 | + pluginContext, | 76 | 80 | }); | 77 | 81 |
| 78 | 82 | const target = event.target; | @@ -160,10 +164,13 @@ export class ContextMenuActions implements IContextMenuActions { | 160 | 164 | destroyFn?.(); | 161 | 165 | }; | 162 | 166 |
| | 167 | + const pluginContext: IPublicModelPluginContext = this.designer.editor.get('pluginContext') as IPublicModelPluginContext; | | 168 | + | 163 | 169 | const menus: IPublicTypeContextMenuItem[] = parseContextMenuProperties(actions, { | 164 | 170 | nodes: nodes.map(d => designer.shellModelFactory.createNode(d)!), | 165 | 171 | destroy, | 166 | 172 | event, | | 173 | + pluginContext, | 167 | 174 | }); | 168 | 175 |
| 169 | 176 | if (!menus.length) { | @@ -175,7 +182,7 @@ export class ContextMenuActions implements IContextMenuActions { | 175 | 182 | const menuNode = parseContextMenuAsReactNode(layoutMenu, { | 176 | 183 | destroy, | 177 | 184 | nodes: nodes.map(d => designer.shellModelFactory.createNode(d)!), | 178 | | - designer, | | 185 | + pluginContext, | 179 | 186 | }); | 180 | 187 |
| 181 | 188 | destroyFn = createContextMenu(menuNode, { | |
Collapse file Copy file name to clipboardExpand all lines: packages/engine/src/engine-core.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -115,12 +115,11 @@ const innerSetters = new InnerSetters(); | 115 | 115 | const setters = new Setters(innerSetters); | 116 | 116 |
| 117 | 117 | const material = new Material(editor); | 118 | | -const commonUI = new CommonUI(); | | 118 | +const commonUI = new CommonUI(editor); | 119 | 119 | editor.set('project', project); | 120 | 120 | editor.set('setters' as any, setters); | 121 | 121 | editor.set('material', material); | 122 | 122 | editor.set('innerHotkey', innerHotkey); | 123 | | -editor.set('commonUI' as any, commonUI); | 124 | 123 | const config = new Config(engineConfig); | 125 | 124 | const event = new Event(commonEvent, { prefix: 'common' }); | 126 | 125 | const logger = new Logger({ level: 'warn', bizName: 'common' }); | @@ -147,6 +146,7 @@ const pluginContextApiAssembler: ILowCodePluginContextApiAssembler = { | 147 | 146 | context.commonUI = commonUI; | 148 | 147 | context.registerLevel = IPublicEnumPluginRegisterLevel.Default; | 149 | 148 | context.isPluginRegisteredInWorkspace = false; | | 149 | + editor.set('pluginContext', context); | 150 | 150 | }, | 151 | 151 | }; | 152 | 152 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/engine/src/inner-plugins/default-context-menu.ts +6-15Lines changed: 6 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,28 +5,18 @@ import { | 5 | 5 | IPublicModelNode, | 6 | 6 | IPublicModelPluginContext, | 7 | 7 | IPublicTypeDragNodeDataObject, | 8 | | - IPublicTypeI18nData, | 9 | 8 | IPublicTypeNodeSchema, | 10 | 9 | } from '@alilc/lowcode-types'; | 11 | | -import { isI18nData, isProjectSchema } from '@alilc/lowcode-utils'; | | 10 | +import { isProjectSchema } from '@alilc/lowcode-utils'; | 12 | 11 | import { Notification } from '@alifd/next'; | 13 | | -import { intl, getLocale } from '../locale'; | | 12 | +import { intl } from '../locale'; | 14 | 13 |
| 15 | 14 | function getNodesSchema(nodes: IPublicModelNode[]) { | 16 | 15 | const componentsTree = nodes.map((node) => node?.exportSchema(IPublicEnumTransformStage.Clone)); | 17 | 16 | const data = { type: 'nodeSchema', componentsMap: {}, componentsTree }; | 18 | 17 | return data; | 19 | 18 | } | 20 | 19 |
| 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 | | - | 30 | 20 | async function getClipboardText(): Promise<IPublicTypeNodeSchema[]> { | 31 | 21 | return new Promise((resolve, reject) => { | 32 | 22 | // 使用 Clipboard API 读取剪贴板内容 | @@ -61,8 +51,9 @@ async function getClipboardText(): Promise<IPublicTypeNodeSchema[]> { | 61 | 51 | } | 62 | 52 |
| 63 | 53 | export const defaultContextMenu = (ctx: IPublicModelPluginContext) => { | 64 | | - const { material, canvas } = ctx; | | 54 | + const { material, canvas, common } = ctx; | 65 | 55 | const { clipboard } = canvas; | | 56 | + const { intl: utilsIntl } = common.utils; | 66 | 57 |
| 67 | 58 | return { | 68 | 59 | init() { | @@ -150,7 +141,7 @@ export const defaultContextMenu = (ctx: IPublicModelPluginContext) => { | 150 | 141 | }); | 151 | 142 | if (canAddNodes.length === 0) { | 152 | 143 | Notification.open({ | 153 | | - content: `${nodeSchema.map(d => getIntlStr(d.title || d.componentName)).join(',')}等组件无法放置到${getIntlStr(parent.title || parent.componentName as any)}内`, | | 144 | + content: `${nodeSchema.map(d => utilsIntl(d.title || d.componentName)).join(',')}等组件无法放置到${utilsIntl(parent.title || parent.componentName as any)}内`, | 154 | 145 | type: 'error', | 155 | 146 | }); | 156 | 147 | return; | @@ -198,7 +189,7 @@ export const defaultContextMenu = (ctx: IPublicModelPluginContext) => { | 198 | 189 | }); | 199 | 190 | if (canAddNodes.length === 0) { | 200 | 191 | Notification.open({ | 201 | | - content: `${nodeSchema.map(d => getIntlStr(d.title || d.componentName)).join(',')}等组件无法放置到${getIntlStr(node.title || node.componentName as any)}内`, | | 192 | + content: `${nodeSchema.map(d => utilsIntl(d.title || d.componentName)).join(',')}等组件无法放置到${utilsIntl(node.title || node.componentName as any)}内`, | 202 | 193 | type: 'error', | 203 | 194 | }); | 204 | 195 | return; | |
Collapse file Copy file name to clipboardExpand all lines: packages/shell/src/api/commonUI.tsx +22-2Lines changed: 22 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import { IPublicApiCommonUI } from '@alilc/lowcode-types'; | | 1 | +import { IPublicApiCommonUI, IPublicModelPluginContext, IPublicTypeContextMenuAction } from '@alilc/lowcode-types'; | 2 | 2 | import { | | 3 | + IEditor, | 3 | 4 | Tip as InnerTip, | 4 | 5 | Title as InnerTitle, | 5 | 6 | } from '@alilc/lowcode-editor-core'; | 6 | 7 | import { Balloon, Breadcrumb, Button, Card, Checkbox, DatePicker, Dialog, Dropdown, Form, Icon, Input, Loading, Message, Overlay, Pagination, Radio, Search, Select, SplitButton, Step, Switch, Tab, Table, Tree, TreeSelect, Upload, Divider } from '@alifd/next'; | 7 | 8 | import { ContextMenu } from '../components/context-menu'; | | 9 | +import { editorSymbol } from '../symbols'; | 8 | 10 |
| 9 | 11 | export class CommonUI implements IPublicApiCommonUI { | | 12 | + [editorSymbol]: IEditor; | | 13 | + | 10 | 14 | Balloon = Balloon; | 11 | 15 | Breadcrumb = Breadcrumb; | 12 | 16 | Button = Button; | @@ -35,13 +39,29 @@ export class CommonUI implements IPublicApiCommonUI { | 35 | 39 | Upload = Upload; | 36 | 40 | Divider = Divider; | 37 | 41 |
| | 42 | + constructor(editor: IEditor) { | | 43 | + this[editorSymbol] = editor; | | 44 | + } | | 45 | + | 38 | 46 | get Tip() { | 39 | 47 | return InnerTip; | 40 | 48 | } | 41 | 49 | get Title() { | 42 | 50 | return InnerTitle; | 43 | 51 | } | | 52 | + | 44 | 53 | get ContextMenu() { | 45 | | - return ContextMenu; | | 54 | + const editor = this[editorSymbol]; | | 55 | + const innerContextMenu = (props: any) => { | | 56 | + const pluginContext: IPublicModelPluginContext = editor.get('pluginContext') as IPublicModelPluginContext; | | 57 | + return <ContextMenu {...props} pluginContext={pluginContext} />; | | 58 | + }; | | 59 | + | | 60 | + innerContextMenu.create = (menus: IPublicTypeContextMenuAction[], event: MouseEvent) => { | | 61 | + const pluginContext: IPublicModelPluginContext = editor.get('pluginContext') as IPublicModelPluginContext; | | 62 | + return ContextMenu.create(pluginContext, menus, event); | | 63 | + }; | | 64 | + | | 65 | + return innerContextMenu; | 46 | 66 | } | 47 | 67 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/shell/src/components/context-menu.tsx +24-4Lines changed: 24 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { createContextMenu, parseContextMenuAsReactNode, parseContextMenuProperties } from '@alilc/lowcode-utils'; | 2 | 2 | import { engineConfig } from '@alilc/lowcode-editor-core'; | 3 | | -import { IPublicTypeContextMenuAction } from '@alilc/lowcode-types'; | | 3 | +import { IPublicModelPluginContext, IPublicTypeContextMenuAction } from '@alilc/lowcode-types'; | 4 | 4 | import React from 'react'; | 5 | 5 |
| 6 | | -export function ContextMenu({ children, menus }: { | | 6 | +export function ContextMenu({ children, menus, pluginContext }: { | 7 | 7 | menus: IPublicTypeContextMenuAction[]; | 8 | 8 | children: React.ReactElement[] | React.ReactElement; | | 9 | + pluginContext: IPublicModelPluginContext; | 9 | 10 | }): React.ReactElement<any, string | React.JSXElementConstructor<any>> { | 10 | 11 | if (!engineConfig.get('enableContextMenu')) { | 11 | 12 | return ( | @@ -23,7 +24,10 @@ export function ContextMenu({ children, menus }: { | 23 | 24 | }; | 24 | 25 | const children: React.ReactNode[] = parseContextMenuAsReactNode(parseContextMenuProperties(menus, { | 25 | 26 | destroy, | 26 | | - })); | | 27 | + pluginContext, | | 28 | + }), { | | 29 | + pluginContext, | | 30 | + }); | 27 | 31 |
| 28 | 32 | if (!children?.length) { | 29 | 33 | return; | @@ -44,4 +48,20 @@ export function ContextMenu({ children, menus }: { | 44 | 48 | return ( | 45 | 49 | <>{childrenWithContextMenu}</> | 46 | 50 | ); | 47 | | -} | | 51 | +} | | 52 | + | | 53 | +ContextMenu.create = (pluginContext: IPublicModelPluginContext, menus: IPublicTypeContextMenuAction[], event: MouseEvent) => { | | 54 | + const children: React.ReactNode[] = parseContextMenuAsReactNode(parseContextMenuProperties(menus, { | | 55 | + pluginContext, | | 56 | + }), { | | 57 | + pluginContext, | | 58 | + }); | | 59 | + | | 60 | + if (!children?.length) { | | 61 | + return; | | 62 | + } | | 63 | + | | 64 | + return createContextMenu(children, { | | 65 | + event, | | 66 | + }); | | 67 | +}; |
|