自定义节点样式
安装相应的库,第二个引入控制画布缩放,历史前后退等功能
pnpm add @logicflow/core
# 插件包(不使用插件时不需要引入)
pnpm add @logicflow/extension
创建一个基础元素
<div class="container" ref="container"></div>
index.vue
import LogicFlow from "@logicflow/core";
import "@logicflow/core/lib/style/index.css";
import { Control } from '@logicflow/extension';
import '@logicflow/extension/lib/style/index.css'
import {onMounted,ref} from 'vue'
import ThemeRect from "./ThemeRect";
```javascript
const data = {
nodes: [
{
id: '1',
type: 'theme-rect',
x: 100,
y: 60,
properties:{
color: 'red',
bgc:'skyblue'
}
},
]
}
const lf = ref()
const container = ref()
onMounted(()=>{
LogicFlow.use(Control);
lf.value = new LogicFlow({
container: container.value,
grid: true,
isSilentMode: false,
stopScrollGraph: false,
stopMoveGraph: false,
stopZoomGraph: false,
adjustNodePosition: true,
height:800
});
lf.value.register(ThemeRect);
lf.value.render(data);
lf.value.translateCenter();
})
Node.vue
<template>
<div class="node">
<h1 className="title" :style="{backgroundColor:props.properties.color}">节点1</h1>
<div className="content" :style="{backgroundColor:props.properties.bgc}">
审批流节点
</div>
</div>
</template>
<script setup>
const props = defineProps(["properties"])
</script>
<style lang="scss" scoped>
.node{
width: 200px;
border: 1px solid #ccc;
.title{
text-align: center;
height: 50px;
line-height: 50px;
background-color: yellow;
}
.content{
display: flex;
justify-content: center;
align-items: center;
}
}
</style>
import { HtmlNodeModel, HtmlNode } from '@logicflow/core';
import Node from './Node.vue';
import { createApp, h } from 'vue';
class DemoModel extends HtmlNodeModel {
setAttributes(){
this.text.editable = false;
}
getOutlineStyle() {
const style = super.getOutlineStyle();
return style;
}
}
class DemoNode extends HtmlNode{
constructor(props) {
super(props)
this.isMounted = false
this.r = h(Node, {
properties: props.model.getProperties(),
})
this.app = createApp({
render: () => this.r
})
}
setHtml(rootEl) {
if (!this.isMounted) {
this.isMounted = true
const node = document.createElement('div')
rootEl.appendChild(node)
this.app.mount(node)
} else {
this.r.component.props.properties = this.props.model.getProperties()
}
setHtml(rootEl) {
if (!this.isMounted) {
this.isMounted = true
const node = document.createElement('div')
rootEl.appendChild(node)
this.app.mount(node)
} else {
this.r.component.props.properties = this.props.model.getProperties()
}
this.props.model.width = this.r.component.vnode.el.clientWidth + 2.5
this.props.model.height = this.r.component.vnode.el.clientHeight + 2.5
}
}
}
export default {
type: 'theme-rect',
view: DemoNode,
model: DemoModel
}
props.model.getProperties()方法用于读取对应节点的properties属性值
this.props.model.width = this.r.component.vnode.el.clientWidth + 2.5
this.props.model.height = this.r.component.vnode.el.clientHeight + 2.5
顶部创建一个添加节点操作区域,
<div class="opt">
<ul>
<li @mousedown="createStartNode">开始节点</li>
<li @mousedown="createEndNode">结束节点</li>
<li @mousedown="createFillNode" >填单节点</li>
<li @mousedown="createApproveNode">审批节点</li>
</ul>
</div>
(开始节点和结束节点的自定义组件未写)
function createFillNode(){
if(!lf.value) return
const node = {
id: setId(),
type: 'theme-rect',
properties:{
color: '#576a95',
bgc:'white',
title:'填单节点'
}
}
lf.value.dnd.startDrag(node);
}
function createApproveNode(){
if(!lf.value) return
const node = {
id: setId(),
type: 'theme-rect',
properties:{
color: '#ff943e',
bgc:'white',
title:'审批节点'
}
}
lf.value.dnd.startDrag(node);
}
常用方法