useNewNode.ts 1.66 KB
import { XYPosition } from '@vue-flow/core';
import { Config as InputConfig } from '../packages/Entry/Input/config';
import { Config as OutputConfig } from '../packages/Flow/Output/config';
import { Config as RuleChainConfig } from '../packages/Flow/RuleChain/config';
import { useAddNodes } from './useAddNodes';

export function useNewNode() {
  const getInputNodeConfig = (id?: string) => {
    const { getAddNodesParams } = useAddNodes();

    const newNode = getAddNodesParams(
      { x: 75, y: 50 },
      {
        ...new InputConfig(),
        data: {
          name: '输入',
          description: '规则链的逻辑输入,将传入消息转发到下一个相关规则节点。',
        },
      },
      { id, draggable: false, selectable: false }
    );

    return newNode;
  };

  const getOutputNodeConfig = (name: string, position: XYPosition, id?: string) => {
    const { getAddNodesParams } = useAddNodes();

    const newNode = getAddNodesParams(
      position,
      {
        ...new OutputConfig(),
        data: {
          name,
        },
      },
      { id, draggable: false, selectable: false }
    );

    return newNode;
  };

  const getRuleChainNodeConfig = ({
    name,
    position,
    ruleChainId,
    id,
  }: {
    name: string;
    position: XYPosition;
    ruleChainId: string;
    id?: string;
  }) => {
    const { getAddNodesParams } = useAddNodes();
    const newNode = getAddNodesParams(
      position,
      {
        ...new RuleChainConfig(),
        data: { name, configuration: { ruleChainId } },
      },
      {
        id,
      }
    );

    return newNode;
  };

  return { getInputNodeConfig, getOutputNodeConfig, getRuleChainNodeConfig };
}