GeezerVison is showing github.com — feat(context-menu): add context-menu css theme, help config, ts define · levphon/lowcode-engine@844ca78
File treeExpand file treeCollapse file tree Open diff view settings docs/docs/guide/expand/editor
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/docs/guide/expand/editor/theme.md +5Lines changed: 5 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -53,6 +53,11 @@ sidebar_position: 9 | 53 | 53 | - `--color-text-reverse`: 反色情况下,文字颜色 | 54 | 54 | - `--color-text-disabled`: 禁用态文字颜色 | 55 | 55 |
| | 56 | +#### 菜单颜色 | | 57 | +- `--color-context-menu-text`: 菜单项颜色 | | 58 | +- `--color-context-menu-text-hover`: 菜单项 hover 颜色 | | 59 | +- `--color-context-menu-text-disabled`: 菜单项 disabled 颜色 | | 60 | + | 56 | 61 | #### 字段和边框颜色 | 57 | 62 |
| 58 | 63 | - `--color-field-label`: field 标签颜色 | |
Collapse file Copy file name to clipboardExpand all lines: packages/designer/src/builtin-simulator/host.ts +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -832,16 +832,22 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp | 832 | 832 | doc.addEventListener('contextmenu', (e: MouseEvent) => { | 833 | 833 | const targetElement = e.target as HTMLElement; | 834 | 834 | const nodeInst = this.getNodeInstanceFromElement(targetElement); | | 835 | + const editor = this.designer?.editor; | 835 | 836 | if (!nodeInst) { | | 837 | + editor?.eventBus.emit('designer.builtinSimulator.contextmenu', { | | 838 | + originalEvent: e, | | 839 | + }); | 836 | 840 | return; | 837 | 841 | } | 838 | 842 | const node = nodeInst.node || this.project.currentDocument?.focusNode; | 839 | 843 | if (!node) { | | 844 | + editor?.eventBus.emit('designer.builtinSimulator.contextmenu', { | | 845 | + originalEvent: e, | | 846 | + }); | 840 | 847 | return; | 841 | 848 | } | 842 | 849 |
| 843 | 850 | // dirty code should refector | 844 | | - const editor = this.designer?.editor; | 845 | 851 | const npm = node?.componentMeta?.npm; | 846 | 852 | const selected = | 847 | 853 | [npm?.package, npm?.componentName].filter((item) => !!item).join('-') || | |
Collapse file Copy file name to clipboardExpand all lines: packages/designer/src/context-menu-actions.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -201,6 +201,8 @@ export class ContextMenuActions implements IContextMenuActions { | 201 | 201 | node: INode; | 202 | 202 | originalEvent: MouseEvent; | 203 | 203 | }) => { | | 204 | + originalEvent.stopPropagation(); | | 205 | + originalEvent.preventDefault(); | 204 | 206 | // 如果右键的节点不在 当前选中的节点中,选中该节点 | 205 | 207 | if (!designer.currentSelection.has(node.id)) { | 206 | 208 | designer.currentSelection.select(node.id); | |
Collapse file Copy file name to clipboardExpand all lines: packages/engine/src/inner-plugins/default-context-menu.ts +21-15Lines changed: 21 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -60,7 +60,7 @@ export const defaultContextMenu = (ctx: IPublicModelPluginContext) => { | 60 | 60 | material.addContextMenuOption({ | 61 | 61 | name: 'selectComponent', | 62 | 62 | title: intl('SelectComponents'), | 63 | | - condition: (nodes) => { | | 63 | + condition: (nodes = []) => { | 64 | 64 | return nodes.length === 1; | 65 | 65 | }, | 66 | 66 | items: [ | @@ -74,14 +74,17 @@ export const defaultContextMenu = (ctx: IPublicModelPluginContext) => { | 74 | 74 | material.addContextMenuOption({ | 75 | 75 | name: 'copyAndPaste', | 76 | 76 | title: intl('CopyAndPaste'), | 77 | | - disabled: (nodes) => { | | 77 | + disabled: (nodes = []) => { | 78 | 78 | return nodes?.filter((node) => !node?.canPerformAction('copy')).length > 0; | 79 | 79 | }, | 80 | 80 | condition: (nodes) => { | 81 | | - return nodes.length === 1; | | 81 | + return nodes?.length === 1; | 82 | 82 | }, | 83 | 83 | action(nodes) { | 84 | | - const node = nodes[0]; | | 84 | + const node = nodes?.[0]; | | 85 | + if (!node) { | | 86 | + return; | | 87 | + } | 85 | 88 | const { document: doc, parent, index } = node; | 86 | 89 | const data = getNodesSchema(nodes); | 87 | 90 | clipboard.setData(data); | @@ -96,11 +99,11 @@ export const defaultContextMenu = (ctx: IPublicModelPluginContext) => { | 96 | 99 | material.addContextMenuOption({ | 97 | 100 | name: 'copy', | 98 | 101 | title: intl('Copy'), | 99 | | - disabled: (nodes) => { | | 102 | + disabled: (nodes = []) => { | 100 | 103 | return nodes?.filter((node) => !node?.canPerformAction('copy')).length > 0; | 101 | 104 | }, | 102 | | - condition(nodes) { | 103 | | - return nodes.length > 0; | | 105 | + condition(nodes = []) { | | 106 | + return nodes?.length > 0; | 104 | 107 | }, | 105 | 108 | action(nodes) { | 106 | 109 | if (!nodes || nodes.length < 1) { | @@ -116,7 +119,7 @@ export const defaultContextMenu = (ctx: IPublicModelPluginContext) => { | 116 | 119 | name: 'pasteToBottom', | 117 | 120 | title: intl('PasteToTheBottom'), | 118 | 121 | condition: (nodes) => { | 119 | | - return nodes.length === 1; | | 122 | + return nodes?.length === 1; | 120 | 123 | }, | 121 | 124 | async action(nodes) { | 122 | 125 | if (!nodes || nodes.length < 1) { | @@ -163,15 +166,18 @@ export const defaultContextMenu = (ctx: IPublicModelPluginContext) => { | 163 | 166 | name: 'pasteToInner', | 164 | 167 | title: intl('PasteToTheInside'), | 165 | 168 | condition: (nodes) => { | 166 | | - return nodes.length === 1; | | 169 | + return nodes?.length === 1; | 167 | 170 | }, | 168 | | - disabled: (nodes) => { | | 171 | + disabled: (nodes = []) => { | 169 | 172 | // 获取粘贴数据 | 170 | | - const node = nodes[0]; | | 173 | + const node = nodes?.[0]; | 171 | 174 | return !node.isContainerNode; | 172 | 175 | }, | 173 | 176 | async action(nodes) { | 174 | | - const node = nodes[0]; | | 177 | + const node = nodes?.[0]; | | 178 | + if (!node) { | | 179 | + return; | | 180 | + } | 175 | 181 | const { document: doc } = node; | 176 | 182 |
| 177 | 183 | try { | @@ -210,14 +216,14 @@ export const defaultContextMenu = (ctx: IPublicModelPluginContext) => { | 210 | 216 | material.addContextMenuOption({ | 211 | 217 | name: 'delete', | 212 | 218 | title: intl('Delete'), | 213 | | - disabled(nodes) { | | 219 | + disabled(nodes = []) { | 214 | 220 | return nodes?.filter((node) => !node?.canPerformAction('remove')).length > 0; | 215 | 221 | }, | 216 | | - condition(nodes) { | | 222 | + condition(nodes = []) { | 217 | 223 | return nodes.length > 0; | 218 | 224 | }, | 219 | 225 | action(nodes) { | 220 | | - nodes.forEach((node) => { | | 226 | + nodes?.forEach((node) => { | 221 | 227 | node.remove(); | 222 | 228 | }); | 223 | 229 | }, | |
Collapse file Copy file name to clipboardExpand all lines: packages/shell/src/components/context-menu.tsx +2-6Lines changed: 2 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,17 +25,13 @@ export function ContextMenu({ children, menus, pluginContext }: { | 25 | 25 | const children: React.ReactNode[] = parseContextMenuAsReactNode(parseContextMenuProperties(menus, { | 26 | 26 | destroy, | 27 | 27 | pluginContext, | 28 | | - }), { | 29 | | - pluginContext, | 30 | | - }); | | 28 | + }), { pluginContext }); | 31 | 29 |
| 32 | 30 | if (!children?.length) { | 33 | 31 | return; | 34 | 32 | } | 35 | 33 |
| 36 | | - destroyFn = createContextMenu(children, { | 37 | | - event, | 38 | | - }); | | 34 | + destroyFn = createContextMenu(children, { event }); | 39 | 35 | }; | 40 | 36 |
| 41 | 37 | // 克隆 children 并添加 onContextMenu 事件处理器 | |
Collapse file Copy file name to clipboardExpand all lines: packages/types/src/shell/type/context-menu.ts +10-4Lines changed: 10 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { IPublicEnumContextMenuType } from '../enum'; | 2 | 2 | import { IPublicModelNode } from '../model'; | 3 | 3 | import { IPublicTypeI18nData } from './i8n-data'; | | 4 | +import { IPublicTypeHelpTipConfig } from './widget-base-config'; | 4 | 5 |
| 5 | 6 | export interface IPublicTypeContextMenuItem extends Omit<IPublicTypeContextMenuAction, 'condition' | 'disabled' | 'items'> { | 6 | 7 | disabled?: boolean; | @@ -34,24 +35,29 @@ export interface IPublicTypeContextMenuAction { | 34 | 35 | * 点击时执行的动作,可选 | 35 | 36 | * Action to execute on click, optional | 36 | 37 | */ | 37 | | - action?: (nodes: IPublicModelNode[], event?: MouseEvent) => void; | | 38 | + action?: (nodes?: IPublicModelNode[], event?: MouseEvent) => void; | 38 | 39 |
| 39 | 40 | /** | 40 | 41 | * 子菜单项或生成子节点的函数,可选,仅支持两级 | 41 | 42 | * Sub-menu items or function to generate child node, optional | 42 | 43 | */ | 43 | | - items?: Omit<IPublicTypeContextMenuAction, 'items'>[] | ((nodes: IPublicModelNode[]) => Omit<IPublicTypeContextMenuAction, 'items'>[]); | | 44 | + items?: Omit<IPublicTypeContextMenuAction, 'items'>[] | ((nodes?: IPublicModelNode[]) => Omit<IPublicTypeContextMenuAction, 'items'>[]); | 44 | 45 |
| 45 | 46 | /** | 46 | 47 | * 显示条件函数 | 47 | 48 | * Function to determine display condition | 48 | 49 | */ | 49 | | - condition?: (nodes: IPublicModelNode[]) => boolean; | | 50 | + condition?: (nodes?: IPublicModelNode[]) => boolean; | 50 | 51 |
| 51 | 52 | /** | 52 | 53 | * 禁用条件函数,可选 | 53 | 54 | * Function to determine disabled condition, optional | 54 | 55 | */ | 55 | | - disabled?: (nodes: IPublicModelNode[]) => boolean; | | 56 | + disabled?: (nodes?: IPublicModelNode[]) => boolean; | | 57 | + | | 58 | + /** | | 59 | + * 帮助提示,可选 | | 60 | + */ | | 61 | + help?: IPublicTypeHelpTipConfig; | 56 | 62 | } | 57 | 63 |
|
Collapse file Copy file name to clipboardExpand all lines: packages/utils/src/context-menu.scss +14-7Lines changed: 14 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 10 | 10 |
| 11 | 11 | .engine-context-menu-item { | 12 | 12 | .engine-context-menu-text { | 13 | | - color: var(--color-text); | | 13 | + color: var(--color-context-menu-text, var(--color-text)); | | 14 | + display: flex; | | 15 | + align-items: center; | | 16 | + | | 17 | + .lc-help-tip { | | 18 | + margin-left: 4px; | | 19 | + opacity: 0.8; | | 20 | + } | 14 | 21 | } | 15 | 22 |
| 16 | | - &:hover { | 17 | | - .engine-context-menu-text { | 18 | | - color: var(--color-title); | | 23 | + &.disabled { | | 24 | + &:hover .engine-context-menu-text, .engine-context-menu-text { | | 25 | + color: var(--color-context-menu-text-disabled, var(--color-text-disabled)); | 19 | 26 | } | 20 | 27 | } | 21 | 28 |
| 22 | | - &.disbale { | | 29 | + &:hover { | 23 | 30 | .engine-context-menu-text { | 24 | | - color: var(--color-text-disabled); | | 31 | + color: var(--color-context-menu-text-hover, var(--color-title)); | 25 | 32 | } | 26 | 33 | } | 27 | 34 | } | 28 | 35 |
| 29 | 36 | .engine-context-menu-title { | 30 | | - color: var(--color-text); | | 37 | + color: var(--color-context-menu-text, var(--color-text)); | 31 | 38 | cursor: pointer; | 32 | 39 |
| 33 | 40 | &:hover { | |
Collapse file Copy file name to clipboardExpand all lines: packages/utils/src/context-menu.tsx +20-13Lines changed: 20 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -64,8 +64,9 @@ const Tree = (props: { | 64 | 64 | let destroyFn: Function | undefined; | 65 | 65 |
| 66 | 66 | export function parseContextMenuAsReactNode(menus: IPublicTypeContextMenuItem[], options: IOptions): React.ReactNode[] { | 67 | | - const { common } = options.pluginContext || {}; | | 67 | + const { common, commonUI } = options.pluginContext || {}; | 68 | 68 | const { intl = (title: any) => title } = common?.utils || {}; | | 69 | + const { HelpTip } = commonUI || {}; | 69 | 70 |
| 70 | 71 | const children: React.ReactNode[] = []; | 71 | 72 | menus.forEach((menu, index) => { | @@ -79,7 +80,7 @@ export function parseContextMenuAsReactNode(menus: IPublicTypeContextMenuItem[], | 79 | 80 | children.push(( | 80 | 81 | <PopupItem | 81 | 82 | className={classNames('engine-context-menu-item', { | 82 | | - disbale: menu.disabled, | | 83 | + disabled: menu.disabled, | 83 | 84 | })} | 84 | 85 | key={menu.name} | 85 | 86 | label={<div className="engine-context-menu-text">{intl(menu.title)}</div>} | @@ -93,14 +94,17 @@ export function parseContextMenuAsReactNode(menus: IPublicTypeContextMenuItem[], | 93 | 94 | children.push(( | 94 | 95 | <Item | 95 | 96 | className={classNames('engine-context-menu-item', { | 96 | | - disbale: menu.disabled, | | 97 | + disabled: menu.disabled, | 97 | 98 | })} | 98 | 99 | disabled={menu.disabled} | 99 | | - onClick={menu.action} | | 100 | + onClick={() => { | | 101 | + menu.action?.(); | | 102 | + }} | 100 | 103 | key={menu.name} | 101 | 104 | > | 102 | 105 | <div className="engine-context-menu-text"> | 103 | | - {intl(menu.title)} | | 106 | + { menu.title ? intl(menu.title) : null } | | 107 | + { menu.help ? <HelpTip size="xs" help={menu.help} direction="right" /> : null } | 104 | 108 | </div> | 105 | 109 | </Item> | 106 | 110 | )); | @@ -135,12 +139,14 @@ export function parseContextMenuProperties(menus: (IPublicTypeContextMenuAction | 135 | 139 | name, | 136 | 140 | title, | 137 | 141 | type = IPublicEnumContextMenuType.MENU_ITEM, | | 142 | + help, | 138 | 143 | } = menu; | 139 | 144 |
| 140 | 145 | const result: IPublicTypeContextMenuItem = { | 141 | 146 | name, | 142 | 147 | title, | 143 | 148 | type, | | 149 | + help, | 144 | 150 | action: () => { | 145 | 151 | destroy?.(); | 146 | 152 | menu.action?.(nodes || [], options.event); | @@ -193,26 +199,27 @@ export function createContextMenu(children: React.ReactNode[], { | 193 | 199 | event: MouseEvent | React.MouseEvent; | 194 | 200 | offset?: [number, number]; | 195 | 201 | }) { | | 202 | + event.preventDefault(); | | 203 | + event.stopPropagation(); | | 204 | + | 196 | 205 | const viewportWidth = window.innerWidth; | 197 | 206 | const viewportHeight = window.innerHeight; | 198 | 207 | const dividerCount = React.Children.count(children.filter(child => React.isValidElement(child) && child.type === Divider)); | 199 | 208 | const popupItemCount = React.Children.count(children.filter(child => React.isValidElement(child) && (child.type === PopupItem || child.type === Item))); | 200 | 209 | const menuHeight = popupItemCount * parseInt(getMenuItemHeight(), 10) + dividerCount * 8 + 16; | 201 | 210 | const menuWidthLimit = 200; | 202 | | - const target = event.target; | 203 | | - const { top, left } = (target as any)?.getBoundingClientRect(); | 204 | | - let x = event.clientX - left + offset[0]; | 205 | | - let y = event.clientY - top + offset[1]; | 206 | | - if (x + menuWidthLimit + left > viewportWidth) { | | 211 | + let x = event.clientX + offset[0]; | | 212 | + let y = event.clientY + offset[1]; | | 213 | + if (x + menuWidthLimit > viewportWidth) { | 207 | 214 | x = x - menuWidthLimit; | 208 | 215 | } | 209 | | - if (y + menuHeight + top > viewportHeight) { | | 216 | + if (y + menuHeight > viewportHeight) { | 210 | 217 | y = y - menuHeight; | 211 | 218 | } | 212 | 219 |
| 213 | 220 | const menuInstance = Menu.create({ | 214 | | - target, | 215 | | - offset: [x, y, 0, 0], | | 221 | + target: document.body, | | 222 | + offset: [x, y], | 216 | 223 | children, | 217 | 224 | className: 'engine-context-menu', | 218 | 225 | }); | |
|