Skip to content

FlowEditor 流程编排编辑器

基于 Vue Flow 构建,支持节点拖拽、连线、缩放、平移等操作的流程编辑器组件。

基础用法

Vue Flow mini map
vue
<template>
  <div style="height: 500px;">
    <x-flow-editor
      :nodes="nodes"
      :edges="edges"
      :show-minimap="true"
      :show-controls="true"
      :show-background="true"
      :editable="true"
      @nodes-change="handleNodesChange"
      @edges-change="handleEdgesChange"
      @node-click="handleNodeClick"
      @connect="handleConnect"
    />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { FlowEditor } from '@xto/flow'

const nodes = ref([
  { id: '1', position: { x: 100, y: 100 }, data: { label: '开始节点' } },
  { id: '2', position: { x: 300, y: 100 }, data: { label: '处理节点' } },
  { id: '3', position: { x: 500, y: 100 }, data: { label: '结束节点' } }
])

const edges = ref([
  { id: 'e1-2', source: '1', target: '2' },
  { id: 'e2-3', source: '2', target: '3' }
])

const handleNodesChange = (nodes) => {
  console.log('节点变化:', nodes)
}

const handleEdgesChange = (edges) => {
  console.log('连线变化:', edges)
}

const handleNodeClick = (node, event) => {
  console.log('点击节点:', node)
}

const handleConnect = (connection) => {
  console.log('新建连线:', connection)
}
</script>

背景类型

支持点状、线条、交叉三种背景类型。

vue
<template>
  <!-- 点状背景 -->
  <x-flow-editor :background-type="'dots'" :background-color="'#f5f5f5'" />

  <!-- 线条背景 -->
  <x-flow-editor :background-type="'lines'" />

  <!-- 交叉背景 -->
  <x-flow-editor :background-type="'cross'" />
</template>

连线类型

支持多种连线样式。

vue
<template>
  <!-- 贝塞尔曲线 -->
  <x-flow-editor :edge-type="'bezier'" />

  <!-- 直线 -->
  <x-flow-editor :edge-type="'straight'" />

  <!-- 步进线 -->
  <x-flow-editor :edge-type="'step'" />

  <!-- 平滑步进线 -->
  <x-flow-editor :edge-type="'smoothstep'" />

  <!-- 动画连线 -->
  <x-flow-editor :edge-type="'smoothstep'" :edge-animated="true" />
</template>

缩放控制

vue
<template>
  <x-flow-editor
    :zoomable="true"
    :pannable="true"
    :min-zoom="0.1"
    :max-zoom="4"
  />
</template>

禁用编辑

Vue Flow mini map
vue
<template>
  <!-- 只读模式 -->
  <x-flow-editor :editable="false" :selectable="false" :connectable="false" :draggable="false" />
</template>

隐藏辅助组件

vue
<template>
  <!-- 隐藏迷你地图 -->
  <x-flow-editor :show-minimap="false" />

  <!-- 隐藏控制面板 -->
  <x-flow-editor :show-controls="false" />

  <!-- 隐藏背景 -->
  <x-flow-editor :show-background="false" />
</template>

自定义节点类型

vue
<template>
  <x-flow-editor :nodes="nodes" :node-types="customNodeTypes" />
</template>

<script setup>
import { markRaw } from 'vue'
import CustomNode from './CustomNode.vue'

const customNodeTypes = {
  custom: markRaw(CustomNode)
}

const nodes = [
  { id: '1', type: 'custom', position: { x: 100, y: 100 }, data: { label: '自定义节点' } }
]
</script>

方法调用

vue
<template>
  <button @click="handleFitView">自适应视图</button>
  <button @click="handleZoomIn">放大</button>
  <button @click="handleZoomOut">缩小</button>
  
  <x-flow-editor ref="flowEditorRef" :nodes="nodes" />
</template>

<script setup>
import { ref } from 'vue'

const flowEditorRef = ref()

// 自适应视图
const handleFitView = () => {
  flowEditorRef.value?.fitView()
}

// 放大到指定比例
const handleZoomIn = () => {
  flowEditorRef.value?.zoomTo(1.5)
}

// 缩小
const handleZoomOut = () => {
  flowEditorRef.value?.zoomTo(0.5)
}

// 添加节点
const addNode = () => {
  flowEditorRef.value?.addNodes([
    { id: 'new-node', position: { x: 200, y: 200 }, data: { label: '新节点' } }
  ])
}

// 删除节点
const removeNode = (nodeId) => {
  flowEditorRef.value?.removeNodes([{ id: nodeId }])
}
</script>

API

Props

属性说明类型默认值
nodes初始节点数据Node[][]
edges初始连线数据Edge[][]
showMinimap是否显示迷你地图booleantrue
showControls是否显示控制面板booleantrue
showBackground是否显示背景网格booleantrue
backgroundType背景类型'dots' | 'lines' | 'cross''dots'
backgroundColor背景颜色string'#f5f5f5'
editable是否可编辑booleantrue
selectable是否可选择节点booleantrue
connectable是否可连接节点booleantrue
draggable是否可拖拽节点booleantrue
zoomable是否可缩放booleantrue
pannable是否可平移booleantrue
minZoom最小缩放比例number0.1
maxZoom最大缩放比例number4
nodeTypes自定义节点类型映射Record<string, Component>{}
edgeType连线类型'default' | 'straight' | 'step' | 'smoothstep' | 'bezier''smoothstep'
edgeAnimated是否显示连线动画booleanfalse

Events

事件名说明
nodes-change节点变化时触发
edges-change连线变化时触发
node-click点击节点时触发
node-dblclick双击节点时触发
node-drag拖拽节点时触发
node-drag-stop拖拽节点结束时触发
edge-click点击连线时触发
connect新建连线时触发
pane-click点击画布时触发
pane-ready画布就绪时触发
edge-delete删除连线时触发
connection-validate连线验证时触发

Methods

方法名说明
addNodes添加节点
addEdges添加连线
removeNodes删除节点
removeEdges删除连线
getNodes获取所有节点
getEdges获取所有连线
fitView自适应视图
zoomTo缩放到指定比例
setViewport设置视图位置
project像素坐标转流程坐标

基于 MIT 许可发布