GeezerVison is showing github.com — feat(engine): add context menu · Devancn/lowcode-engine@1b00c61
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: .gitignore +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -108,3 +108,5 @@ typings/ | 108 | 108 | # codealike | 109 | 109 | codealike.json | 110 | 110 | .node | | 111 | + | | 112 | +.must.config.js |
Collapse file Copy file name to clipboardExpand all lines: docs/docs/api/commonUI.md +20Lines changed: 20 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -29,6 +29,26 @@ CommonUI API 是一个专为低代码引擎设计的组件 UI 库,使用它开 | 29 | 29 | | className | className | string (optional) | | | 30 | 30 | | onClick | 点击事件 | () => void (optional) | | | 31 | 31 |
| | 32 | +### ContextMenu | | 33 | + | | 34 | +| 参数 | 说明 | 类型 | 默认值 | | | 35 | +|--------|----------------------------------------------------|------------------------------------|--------| | | 36 | +| menus | 定义上下文菜单的动作数组 | IPublicTypeContextMenuAction[] | | | | 37 | +| children | 组件的子元素 | React.ReactElement[] | | | | 38 | + | | 39 | +**IPublicTypeContextMenuAction Interface** | | 40 | + | | 41 | +| 参数 | 说明 | 类型 | 默认值 | | | 42 | +|------------|--------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|----------------------------------------| | | 43 | +| name | 动作的唯一标识符<br>Unique identifier for the action | string | | | | 44 | +| title | 显示的标题,可以是字符串或国际化数据<br>Display title, can be a string or internationalized data | string \| IPublicTypeI18nData (optional) | | | | 45 | +| type | 菜单项类型<br>Menu item type | IPublicEnumContextMenuType (optional) | IPublicEnumPContextMenuType.MENU_ITEM | | | 46 | +| action | 点击时执行的动作,可选<br>Action to execute on click, optional | (nodes: IPublicModelNode[]) => void (optional) | | | | 47 | +| items | 子菜单项或生成子节点的函数,可选,仅支持两级<br>Sub-menu items or function to generate child node, optional | Omit<IPublicTypeContextMenuAction, 'items'>[] \| ((nodes: IPublicModelNode[]) => Omit<IPublicTypeContextMenuAction, 'items'>[]) (optional) | | | | 48 | +| condition | 显示条件函数<br>Function to determine display condition | (nodes: IPublicModelNode[]) => boolean (optional) | | | | 49 | +| disabled | 禁用条件函数,可选<br>Function to determine disabled condition, optional | (nodes: IPublicModelNode[]) => boolean (optional) | | | | 50 | + | | 51 | + | 32 | 52 | ### Balloon | 33 | 53 | 详细文档: [Balloon Documentation](https://fusion.design/pc/component/balloon) | 34 | 54 |
| |
Collapse file Copy file name to clipboardExpand all lines: docs/docs/api/configOptions.md +6Lines changed: 6 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -185,6 +185,12 @@ config.set('enableCondition', false) | 185 | 185 |
| 186 | 186 | `@type {boolean}` `@default {false}` | 187 | 187 |
| | 188 | +#### enableContextMenu - 开启右键菜单 | | 189 | + | | 190 | +`@type {boolean}` `@default {false}` | | 191 | + | | 192 | +是否开启右键菜单 | | 193 | + | 188 | 194 | #### disableDetecting | 189 | 195 |
| 190 | 196 | `@type {boolean}` `@default {false}` | |
Collapse file Copy file name to clipboardExpand all lines: docs/docs/guide/expand/editor/theme.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -128,6 +128,7 @@ sidebar_position: 9 | 128 | 128 | - `--pane-title-height`: 面板标题高度 | 129 | 129 | - `--pane-title-font-size`: 面板标题字体大小 | 130 | 130 | - `--pane-title-padding`: 面板标题边距 | | 131 | +- `--context-menu-item-height`: 右键菜单项高度 | 131 | 132 |
| 132 | 133 |
| 133 | 134 |
| |
Collapse file Copy file name to clipboard +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +.engine-context-menu { | | 2 | + &.next-menu.next-ver .next-menu-item { | | 3 | + padding-right: 30px; | | 4 | + | | 5 | + .next-menu-item-inner { | | 6 | + height: var(--context-menu-item-height, 30px); | | 7 | + line-height: var(--context-menu-item-height, 30px); | | 8 | + } | | 9 | + } | | 10 | +} |
Collapse file Copy file name to clipboard +145Lines changed: 145 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { IPublicTypeContextMenuAction, IPublicEnumContextMenuType, IPublicTypeContextMenuItem, IPublicApiMaterial } from '@alilc/lowcode-types'; | | 2 | +import { IDesigner, INode } from './designer'; | | 3 | +import { parseContextMenuAsReactNode, parseContextMenuProperties } from '@alilc/lowcode-utils'; | | 4 | +import { Menu } from '@alifd/next'; | | 5 | +import { engineConfig } from '@alilc/lowcode-editor-core'; | | 6 | +import './context-menu-actions.scss'; | | 7 | + | | 8 | +export interface IContextMenuActions { | | 9 | + actions: IPublicTypeContextMenuAction[]; | | 10 | + | | 11 | + adjustMenuLayoutFn: (actions: IPublicTypeContextMenuItem[]) => IPublicTypeContextMenuItem[]; | | 12 | + | | 13 | + addMenuAction: IPublicApiMaterial['addContextMenuOption']; | | 14 | + | | 15 | + removeMenuAction: IPublicApiMaterial['removeContextMenuOption']; | | 16 | + | | 17 | + adjustMenuLayout: IPublicApiMaterial['adjustContextMenuLayout']; | | 18 | +} | | 19 | + | | 20 | +export class ContextMenuActions implements IContextMenuActions { | | 21 | + actions: IPublicTypeContextMenuAction[] = []; | | 22 | + | | 23 | + designer: IDesigner; | | 24 | + | | 25 | + dispose: Function[]; | | 26 | + | | 27 | + enableContextMenu: boolean; | | 28 | + | | 29 | + constructor(designer: IDesigner) { | | 30 | + this.designer = designer; | | 31 | + this.dispose = []; | | 32 | + | | 33 | + engineConfig.onGot('enableContextMenu', (enable) => { | | 34 | + if (this.enableContextMenu === enable) { | | 35 | + return; | | 36 | + } | | 37 | + this.enableContextMenu = enable; | | 38 | + this.dispose.forEach(d => d()); | | 39 | + if (enable) { | | 40 | + this.initEvent(); | | 41 | + } | | 42 | + }); | | 43 | + } | | 44 | + | | 45 | + handleContextMenu = ( | | 46 | + nodes: INode[], | | 47 | + event: MouseEvent, | | 48 | + ) => { | | 49 | + const designer = this.designer; | | 50 | + event.stopPropagation(); | | 51 | + event.preventDefault(); | | 52 | + | | 53 | + const actions = designer.contextMenuActions.actions; | | 54 | + | | 55 | + const { bounds } = designer.project.simulator?.viewport || { bounds: { left: 0, top: 0 } }; | | 56 | + const { left: simulatorLeft, top: simulatorTop } = bounds; | | 57 | + | | 58 | + let destroyFn: Function | undefined; | | 59 | + | | 60 | + const destroy = () => { | | 61 | + destroyFn?.(); | | 62 | + }; | | 63 | + | | 64 | + const menus: IPublicTypeContextMenuItem[] = parseContextMenuProperties(actions, { | | 65 | + nodes: nodes.map(d => designer.shellModelFactory.createNode(d)!), | | 66 | + destroy, | | 67 | + }); | | 68 | + | | 69 | + if (!menus.length) { | | 70 | + return; | | 71 | + } | | 72 | + | | 73 | + const layoutMenu = designer.contextMenuActions.adjustMenuLayoutFn(menus); | | 74 | + | | 75 | + const menuNode = parseContextMenuAsReactNode(layoutMenu, { | | 76 | + destroy, | | 77 | + nodes: nodes.map(d => designer.shellModelFactory.createNode(d)!), | | 78 | + designer, | | 79 | + }); | | 80 | + | | 81 | + const target = event.target; | | 82 | + | | 83 | + const { top, left } = target?.getBoundingClientRect(); | | 84 | + | | 85 | + const menuInstance = Menu.create({ | | 86 | + target: event.target, | | 87 | + offset: [event.clientX - left + simulatorLeft, event.clientY - top + simulatorTop], | | 88 | + children: menuNode, | | 89 | + className: 'engine-context-menu', | | 90 | + }); | | 91 | + | | 92 | + destroyFn = (menuInstance as any).destroy; | | 93 | + }; | | 94 | + | | 95 | + initEvent() { | | 96 | + const designer = this.designer; | | 97 | + this.dispose.push( | | 98 | + designer.editor.eventBus.on('designer.builtinSimulator.contextmenu', ({ | | 99 | + node, | | 100 | + originalEvent, | | 101 | + }: { | | 102 | + node: INode; | | 103 | + originalEvent: MouseEvent; | | 104 | + }) => { | | 105 | + // 如果右键的节点不在 当前选中的节点中,选中该节点 | | 106 | + if (!designer.currentSelection.has(node.id)) { | | 107 | + designer.currentSelection.select(node.id); | | 108 | + } | | 109 | + const nodes = designer.currentSelection.getNodes(); | | 110 | + this.handleContextMenu(nodes, originalEvent); | | 111 | + }), | | 112 | + (() => { | | 113 | + const handleContextMenu = (e: MouseEvent) => { | | 114 | + this.handleContextMenu([], e); | | 115 | + }; | | 116 | + | | 117 | + document.addEventListener('contextmenu', handleContextMenu); | | 118 | + | | 119 | + return () => { | | 120 | + document.removeEventListener('contextmenu', handleContextMenu); | | 121 | + }; | | 122 | + })(), | | 123 | + ); | | 124 | + } | | 125 | + | | 126 | + adjustMenuLayoutFn: (actions: IPublicTypeContextMenuItem[]) => IPublicTypeContextMenuItem[] = (actions) => actions; | | 127 | + | | 128 | + addMenuAction(action: IPublicTypeContextMenuAction) { | | 129 | + this.actions.push({ | | 130 | + type: IPublicEnumContextMenuType.MENU_ITEM, | | 131 | + ...action, | | 132 | + }); | | 133 | + } | | 134 | + | | 135 | + removeMenuAction(name: string) { | | 136 | + const i = this.actions.findIndex((action) => action.name === name); | | 137 | + if (i > -1) { | | 138 | + this.actions.splice(i, 1); | | 139 | + } | | 140 | + } | | 141 | + | | 142 | + adjustMenuLayout(fn: (actions: IPublicTypeContextMenuItem[]) => IPublicTypeContextMenuItem[]) { | | 143 | + this.adjustMenuLayoutFn = fn; | | 144 | + } | | 145 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/designer/src/designer/designer.ts +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,7 +20,7 @@ import { | 20 | 20 | } from '@alilc/lowcode-types'; | 21 | 21 | import { mergeAssets, IPublicTypeAssetsJson, isNodeSchema, isDragNodeObject, isDragNodeDataObject, isLocationChildrenDetail, Logger } from '@alilc/lowcode-utils'; | 22 | 22 | import { IProject, Project } from '../project'; | 23 | | -import { Node, DocumentModel, insertChildren, INode } from '../document'; | | 23 | +import { Node, DocumentModel, insertChildren, INode, ISelection } from '../document'; | 24 | 24 | import { ComponentMeta, IComponentMeta } from '../component-meta'; | 25 | 25 | import { INodeSelector, Component } from '../simulator'; | 26 | 26 | import { Scroller } from './scroller'; | @@ -32,6 +32,7 @@ import { OffsetObserver, createOffsetObserver } from './offset-observer'; | 32 | 32 | import { ISettingTopEntry, SettingTopEntry } from './setting'; | 33 | 33 | import { BemToolsManager } from '../builtin-simulator/bem-tools/manager'; | 34 | 34 | import { ComponentActions } from '../component-actions'; | | 35 | +import { ContextMenuActions, IContextMenuActions } from '../context-menu-actions'; | 35 | 36 |
| 36 | 37 | const logger = new Logger({ level: 'warn', bizName: 'designer' }); | 37 | 38 |
| @@ -72,12 +73,16 @@ export interface IDesigner { | 72 | 73 |
| 73 | 74 | get componentActions(): ComponentActions; | 74 | 75 |
| | 76 | + get contextMenuActions(): ContextMenuActions; | | 77 | + | 75 | 78 | get editor(): IPublicModelEditor; | 76 | 79 |
| 77 | 80 | get detecting(): Detecting; | 78 | 81 |
| 79 | 82 | get simulatorComponent(): ComponentType<any> | undefined; | 80 | 83 |
| | 84 | + get currentSelection(): ISelection; | | 85 | + | 81 | 86 | createScroller(scrollable: IPublicTypeScrollable): IPublicModelScroller; | 82 | 87 |
| 83 | 88 | refreshComponentMetasMap(): void; | @@ -122,6 +127,8 @@ export class Designer implements IDesigner { | 122 | 127 |
| 123 | 128 | readonly componentActions = new ComponentActions(); | 124 | 129 |
| | 130 | + readonly contextMenuActions: IContextMenuActions; | | 131 | + | 125 | 132 | readonly activeTracker = new ActiveTracker(); | 126 | 133 |
| 127 | 134 | readonly detecting = new Detecting(); | @@ -198,6 +205,8 @@ export class Designer implements IDesigner { | 198 | 205 | this.postEvent('dragstart', e); | 199 | 206 | }); | 200 | 207 |
| | 208 | + this.contextMenuActions = new ContextMenuActions(this); | | 209 | + | 201 | 210 | this.dragon.onDrag((e) => { | 202 | 211 | if (this.props?.onDrag) { | 203 | 212 | this.props.onDrag(e); | |
Collapse file Copy file name to clipboardExpand all lines: packages/designer/src/index.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,3 +6,4 @@ export * from './project'; | 6 | 6 | export * from './builtin-simulator'; | 7 | 7 | export * from './plugin'; | 8 | 8 | export * from './types'; | | 9 | +export * from './context-menu-actions'; |
Collapse file Copy file name to clipboardExpand all lines: packages/editor-core/src/config.ts +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -159,6 +159,11 @@ const VALID_ENGINE_OPTIONS = { | 159 | 159 | type: 'function', | 160 | 160 | description: '应用级设计模式下,窗口为空时展示的占位组件', | 161 | 161 | }, | | 162 | + enableContextMenu: { | | 163 | + type: 'boolean', | | 164 | + description: '是否开启右键菜单', | | 165 | + default: false, | | 166 | + }, | 162 | 167 | hideComponentAction: { | 163 | 168 | type: 'boolean', | 164 | 169 | description: '是否隐藏设计器辅助层', | |
Collapse file Copy file name to clipboardExpand all lines: packages/engine/src/engine-core.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -62,6 +62,7 @@ import { setterRegistry } from './inner-plugins/setter-registry'; | 62 | 62 | import { defaultPanelRegistry } from './inner-plugins/default-panel-registry'; | 63 | 63 | import { shellModelFactory } from './modules/shell-model-factory'; | 64 | 64 | import { builtinHotkey } from './inner-plugins/builtin-hotkey'; | | 65 | +import { defaultContextMenu } from './inner-plugins/default-context-menu'; | 65 | 66 | import { OutlinePlugin } from '@alilc/lowcode-plugin-outline-pane'; | 66 | 67 |
| 67 | 68 | export * from './modules/skeleton-types'; | @@ -78,6 +79,7 @@ async function registryInnerPlugin(designer: IDesigner, editor: IEditor, plugins | 78 | 79 | await plugins.register(defaultPanelRegistryPlugin); | 79 | 80 | await plugins.register(builtinHotkey); | 80 | 81 | await plugins.register(registerDefaults, {}, { autoInit: true }); | | 82 | + await plugins.register(defaultContextMenu); | 81 | 83 |
| 82 | 84 | return () => { | 83 | 85 | plugins.delete(OutlinePlugin.pluginName); | @@ -86,6 +88,7 @@ async function registryInnerPlugin(designer: IDesigner, editor: IEditor, plugins | 86 | 88 | plugins.delete(defaultPanelRegistryPlugin.pluginName); | 87 | 89 | plugins.delete(builtinHotkey.pluginName); | 88 | 90 | plugins.delete(registerDefaults.pluginName); | | 91 | + plugins.delete(defaultContextMenu.pluginName); | 89 | 92 | }; | 90 | 93 | } | 91 | 94 |
| |
|