关于在vue2中使用LogicFlow自定义节点

主要参考LogicFlow官方文档

在基础流程图搭建起来后,我们想要构建自己的需求风格,例如:

那么该如何对节点进行自定义设定呢?

文档当中有着详细的解释,本文以实际需求为例大体介绍:

import { RectNode, RectNodeModel, h } from "@logicflow/core";

class CustomNodeView extends RectNode {
  getShape() {
    // 获取XxxNodeModel中定义的形状属性
    const { model } = this.props;
    const { x, y, width, height, radius } = model;
    // 获取XxxNodeModel中定义的样式属性
    const style = model.getNodeStyle();

    return h('g', {}, [
        h('rect', {
            ...style,
            x: x - width / 2,
            y: y - height / 2,
            width,
            height,
            rx: radius,
            ry: radius,
        }),
        h('svg', {
            x: x - width / 2 + 5,
            y: y - height / 2 + 5,
            width: 35,
            height: 35,
            viewBox: "0 0 1028 1024",
        }, [
            h('path', {
                fill: '#fff',
                d: ''//节点的svg数据-可以在iconfont上自行查找喜欢的图标
                })
            ])
        
        ]);
    }
}


class CustomNodeModel extends RectNodeModel {
    getNodeStyle() {
        const style = super.getNodeStyle();
        style.stroke = '#8e8e8e';
        style.fill = '#cd4050';
        return style;
      }
      initNodeData(data) {
        super.initNodeData(data);
        this.width = 200;
        this.height = 50;
        this.radius = 10;
      }            
}

export default {
    type: "PdfNode",
    view: CustomNodeView,
    model: CustomNodeModel,
}

这样,我们一个自定义的节点就配置好了,接下来就是要将他注册到我们的左侧工具栏中:

<template>
    <div class="box" ref="box"></div>
</template>

<script>

import {LogicFlow, RectNode, RectNodeModel } from '@logicflow/core';
import { DndPanel, SelectionSelect, Control, NodeResize, Menu, InsertNodeInPolyline  } from '@logicflow/extension';
import '@logicflow/extension/lib/style/index.css';
import '@logicflow/core/dist/style/index.css';
import CustomNode from "./node-red/node/CustomNode";
import FlowNode from "./node-red/node/FlowNode";
import PdfNode from "./node-red/node/PdfNode";

NodeResize.step = 4;
LogicFlow.use(Control);
LogicFlow.use(NodeResize);
LogicFlow.use(DndPanel);
LogicFlow.use(SelectionSelect);
LogicFlow.use(Menu);
LogicFlow.use(InsertNodeInPolyline);


export default {
        props:["flowData",'ruleList'],
        data(){
            return{
                //编辑器内容数据
                editData: null,
                // LogicFlow对象
                lf: '',
                // 左侧工具栏数据
                patternItems: [
                    {
                      type: 'CustomNode',
                      text: '开始',
                      label: '开始节点',
                      icon: require('@/assets/images/flowStart.png'),
                    },
                    {
                      type: 'CustomNode',
                      text: '结束',
                      label: '结束节点',
                      icon: require('@/assets/images/flowStart.png'),
                    },
                    {
                      type: 'PdfNode',//刚刚自定义的节点内容
                      label: '生成pdf',
                      text: '生成pdf',
                      icon: require('@/assets/images/flowPdf.png'),
                    },
                ]
          }
        },

methods:{
            initEdit(){
                this.editData = this.flowData
                this.ruleList.forEach(item=>{
                  this.patternItems.push({
                      type: 'FlowNode',
                      label: item.name,
                      text: item.name,
                      icon: require('@/assets/images/flowFunction.png'),
                  })
                })
                this.lf = new LogicFlow({
                   container: this.$refs.box,
                    grid: true,
                    // 工具配置
                    textEdit: true,
                    isSilentMode: false,
                    edgeType: 'polyline',
                    snapline: true,
                    keyboard: {
                        enabled: true
                    },
                    style: {
                        outline: {
                            stroke: '#000000',
                            strokeWidth: 1,
                            strokeDasharray: '3,3',
                        },
                        controlPoint: {
                            width: 15,
                            height: 7,
                            fill: '#FFFFFF',
                            stroke: '#000000',
                        },
                    },
                });
                this.lf.setTheme({
                nodeText: { // 节点文本样式
                  fontSize: 18,
                  color: '#000'
                },
              });
            },
             renderEdit(){
                this.lf.extension.dndPanel.setPatternItems(this.patternItems);
                this.lf.register(CustomNode);
                this.lf.register(FlowNode);
                this.lf.register(PdfNode);
                this.lf.render(this.editData);
            }
        },
        mounted(){
            this.initEdit()
            this.renderEdit()
        },

记得给你的box,以及菜单栏一个样式

<style lang="scss" scoped>
.box{
    width: 100%;
    height: 80vh;
    overflow: auto;
}

::v-deep .lf-dndpanel{
  width: 11vw;
  height: 33vw;
  margin-top: 5vh;
  overflow-y: auto;
}
::v-deep .lf-dnd-item{
    margin-top: 10px;
}
::v-deep .lf-dnd-shape{
  width: 10vw;
  height: 2vw;
  background-size: 50%;
}
</style>

目录结构:

我也是参考了官方文档,以及两位老师的博客来解决的,有需要可以查看

【教程】LogicFlow流程图界的神器-CSDN博客

LogicFlow自定义业务节点_logicflow自定义节点-CSDN博客

Vue2使用LogicFlow,可以按照以下步骤进行: 1. 安装LogicFlow 可以通过npm或者yarn来安装LogicFlow,命令如下: ```bash npm install @logicflow/core --save ``` 或者 ```bash yarn add @logicflow/core ``` 2. 引入LogicFlowVue2的组件引入LogicFlow,可以在`<script>`标签添加如下代码: ```javascript import LogicFlow from '@logicflow/core'; import '@logicflow/core/dist/style/index.css'; ``` 3. 在Vue2使用LogicFlowVue2组件使用LogicFlow,可以在`mounted()`方法创建LogicFlow实例,代码如下: ```javascript mounted() { const lf = new LogicFlow({ container: document.querySelector('#container'), // 其他配置项 }); } ``` 其,`container`是在模板定义的元素,用于显示LogicFlow流程图。可以在模板添加如下代码: ```html <div id="container"></div> ``` 4. 添加节点和连线 可以通过LogicFlow提供的API来添加节点和连线。例如,可以添加一个开始节点和一个结束节点,代码如下: ```javascript mounted() { const lf = new LogicFlow({ container: document.querySelector('#container'), // 其他配置项 }); const startNode = lf.addNode({ type: 'start', x: 100, y: 100, text: '开始', }); const endNode = lf.addNode({ type: 'end', x: 400, y: 100, text: '结束', }); lf.addLine({ type: 'line', sourceNodeId: startNode.id, targetNodeId: endNode.id, text: '连接线', }); } ``` 通过以上步骤,就可以在Vue2使用LogicFlow来创建流程图了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值