mirror of
https://gitee.com/ByteDance/flowgram.ai.git
synced 2025-07-07 17:43:29 +08:00
feat(demo-free-layout): context-menu-plugin (#329)
This commit is contained in:
parent
0b4b85da55
commit
b87c3e918c
@ -83,7 +83,7 @@ export const NodeMenu: FC<NodeMenuProps> = ({ node, deleteNode, updateTitleEdit
|
|||||||
}, [updateTitleEdit]);
|
}, [updateTitleEdit]);
|
||||||
|
|
||||||
if (!visible) {
|
if (!visible) {
|
||||||
return;
|
return <></>;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import { createMinimapPlugin } from '@flowgram.ai/minimap-plugin';
|
|||||||
import { createFreeSnapPlugin } from '@flowgram.ai/free-snap-plugin';
|
import { createFreeSnapPlugin } from '@flowgram.ai/free-snap-plugin';
|
||||||
import { createFreeNodePanelPlugin } from '@flowgram.ai/free-node-panel-plugin';
|
import { createFreeNodePanelPlugin } from '@flowgram.ai/free-node-panel-plugin';
|
||||||
import { createFreeLinesPlugin } from '@flowgram.ai/free-lines-plugin';
|
import { createFreeLinesPlugin } from '@flowgram.ai/free-lines-plugin';
|
||||||
import { FreeLayoutProps } from '@flowgram.ai/free-layout-editor';
|
import { FreeLayoutProps, WorkflowNodeLinesData } from '@flowgram.ai/free-layout-editor';
|
||||||
import { createFreeGroupPlugin } from '@flowgram.ai/free-group-plugin';
|
import { createFreeGroupPlugin } from '@flowgram.ai/free-group-plugin';
|
||||||
import { createContainerNodePlugin } from '@flowgram.ai/free-container-plugin';
|
import { createContainerNodePlugin } from '@flowgram.ai/free-container-plugin';
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ import { onDragLineEnd } from '../utils';
|
|||||||
import { FlowNodeRegistry, FlowDocumentJSON } from '../typings';
|
import { FlowNodeRegistry, FlowDocumentJSON } from '../typings';
|
||||||
import { shortcuts } from '../shortcuts';
|
import { shortcuts } from '../shortcuts';
|
||||||
import { CustomService, RunningService } from '../services';
|
import { CustomService, RunningService } from '../services';
|
||||||
import { createSyncVariablePlugin } from '../plugins';
|
import { createSyncVariablePlugin, createContextMenuPlugin } from '../plugins';
|
||||||
import { defaultFormMeta } from '../nodes/default-form-meta';
|
import { defaultFormMeta } from '../nodes/default-form-meta';
|
||||||
import { WorkflowNodeType } from '../nodes';
|
import { WorkflowNodeType } from '../nodes';
|
||||||
import { SelectorBoxPopover } from '../components/selector-box-popover';
|
import { SelectorBoxPopover } from '../components/selector-box-popover';
|
||||||
@ -92,7 +92,11 @@ export function useEditorProps(
|
|||||||
if (fromPort.node === toPort.node) {
|
if (fromPort.node === toPort.node) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
/**
|
||||||
|
* 线条环检测,不允许连接到前面的节点
|
||||||
|
* Line loop detection, which is not allowed to connect to the node in front of it
|
||||||
|
*/
|
||||||
|
return !fromPort.node.getData(WorkflowNodeLinesData).allInputNodes.includes(toPort.node);
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* Check whether the line can be deleted, this triggers on the default shortcut `Bakspace` or `Delete`
|
* Check whether the line can be deleted, this triggers on the default shortcut `Bakspace` or `Delete`
|
||||||
@ -157,6 +161,7 @@ export function useEditorProps(
|
|||||||
* Running line
|
* Running line
|
||||||
*/
|
*/
|
||||||
isFlowingLine: (ctx, line) => ctx.get(RunningService).isFlowingLine(line),
|
isFlowingLine: (ctx, line) => ctx.get(RunningService).isFlowingLine(line),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shortcuts
|
* Shortcuts
|
||||||
*/
|
*/
|
||||||
@ -249,9 +254,16 @@ export function useEditorProps(
|
|||||||
* 这个用于 loop 节点子画布的渲染
|
* 这个用于 loop 节点子画布的渲染
|
||||||
*/
|
*/
|
||||||
createContainerNodePlugin({}),
|
createContainerNodePlugin({}),
|
||||||
|
/**
|
||||||
|
* Group plugin
|
||||||
|
*/
|
||||||
createFreeGroupPlugin({
|
createFreeGroupPlugin({
|
||||||
groupNodeRender: GroupNodeRender,
|
groupNodeRender: GroupNodeRender,
|
||||||
}),
|
}),
|
||||||
|
/**
|
||||||
|
* ContextMenu plugin
|
||||||
|
*/
|
||||||
|
createContextMenuPlugin({}),
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
[]
|
[]
|
||||||
|
|||||||
@ -0,0 +1,52 @@
|
|||||||
|
import { NodePanelResult, WorkflowNodePanelService } from '@flowgram.ai/free-node-panel-plugin';
|
||||||
|
import {
|
||||||
|
Layer,
|
||||||
|
injectable,
|
||||||
|
inject,
|
||||||
|
FreeLayoutPluginContext,
|
||||||
|
WorkflowHoverService,
|
||||||
|
WorkflowNodeEntity,
|
||||||
|
WorkflowNodeJSON,
|
||||||
|
} from '@flowgram.ai/free-layout-editor';
|
||||||
|
|
||||||
|
@injectable()
|
||||||
|
export class ContextMenuLayer extends Layer {
|
||||||
|
@inject(FreeLayoutPluginContext) ctx: FreeLayoutPluginContext;
|
||||||
|
|
||||||
|
@inject(WorkflowNodePanelService) nodePanelService: WorkflowNodePanelService;
|
||||||
|
|
||||||
|
@inject(WorkflowHoverService) hoverService: WorkflowHoverService;
|
||||||
|
|
||||||
|
onReady() {
|
||||||
|
this.listenPlaygroundEvent('contextmenu', (e) => {
|
||||||
|
this.openNodePanel(e);
|
||||||
|
e.preventDefault();
|
||||||
|
e.stopPropagation();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
openNodePanel(e: MouseEvent) {
|
||||||
|
const pos = this.getPosFromMouseEvent(e);
|
||||||
|
this.nodePanelService.callNodePanel({
|
||||||
|
position: pos,
|
||||||
|
panelProps: {},
|
||||||
|
// handle node selection from panel - 处理从面板中选择节点
|
||||||
|
onSelect: async (panelParams?: NodePanelResult) => {
|
||||||
|
if (!panelParams) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const { nodeType, nodeJSON } = panelParams;
|
||||||
|
// create new workflow node based on selected type - 根据选择的类型创建新的工作流节点
|
||||||
|
const node: WorkflowNodeEntity = this.ctx.document.createWorkflowNodeByType(
|
||||||
|
nodeType,
|
||||||
|
pos,
|
||||||
|
nodeJSON ?? ({} as WorkflowNodeJSON)
|
||||||
|
);
|
||||||
|
// select the newly created node - 选择新创建的节点
|
||||||
|
this.ctx.selection.selection = [node];
|
||||||
|
},
|
||||||
|
// handle panel close - 处理面板关闭
|
||||||
|
onClose: () => {},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
import {
|
||||||
|
definePluginCreator,
|
||||||
|
PluginCreator,
|
||||||
|
FreeLayoutPluginContext,
|
||||||
|
} from '@flowgram.ai/free-layout-editor';
|
||||||
|
|
||||||
|
import { ContextMenuLayer } from './context-menu-layer';
|
||||||
|
|
||||||
|
export interface ContextMenuPluginOptions {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a plugin of contextmenu
|
||||||
|
* @param ctx - The plugin context, containing the document and other relevant information.
|
||||||
|
* @param options - Plugin options, currently an empty object.
|
||||||
|
*/
|
||||||
|
export const createContextMenuPlugin: PluginCreator<ContextMenuPluginOptions> = definePluginCreator<
|
||||||
|
ContextMenuPluginOptions,
|
||||||
|
FreeLayoutPluginContext
|
||||||
|
>({
|
||||||
|
onInit(ctx, options) {
|
||||||
|
ctx.playground.registerLayer(ContextMenuLayer);
|
||||||
|
},
|
||||||
|
});
|
||||||
@ -0,0 +1 @@
|
|||||||
|
export { createContextMenuPlugin } from './context-menu-plugin';
|
||||||
@ -1 +1,2 @@
|
|||||||
export { createSyncVariablePlugin } from './sync-variable-plugin/sync-variable-plugin';
|
export { createSyncVariablePlugin } from './sync-variable-plugin/sync-variable-plugin';
|
||||||
|
export { createContextMenuPlugin } from './context-menu-plugin';
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user