@antv/x6、@antv/x6-react-components X6 图编辑引擎在 react typescript 项目中实践

X6 图编辑引擎

X6 是 AntV 旗下的图编辑引擎,提供了一系列开箱即用的交互组件和简单易用的节点定制能力,方便我们快速搭建 DAG 图、ER 图、流程图等应用。

说明文档:https://x6.antv.vision/zh/docs/tutorial/about

 

源码

import React, { useEffect, useState } from 'react'
import './index.scss'
import { GithubOutlined, UserOutlined } from '@ant-design/icons'
import ToolBar from './components/ToolBar'
import RightDrawer from './components/RightDrawer'
import FlowGraph from './Graph'
import { useHistory } from 'react-router-dom'

const Index = () => {
  const history = useHistory();
  const [ isReady, setIsReady ] = useState(false) // 是否初始化完成,渲染 toolbar
  const [ isRightDrawer, setIsRightDrawer ] = useState(false)
  const [ selectCell, setSelectCell ] = useState({})

  useEffect(() => {
    const graph = FlowGraph.init()
    setIsReady(true)

    // 节点选中事件
    graph.on('selection:changed', (args) => {
      args.added.forEach((cell: any) => {
        const shape = cell.store.data.shape

        if (shape !== 'edge') {
          setSelectCell(cell)
          setIsRightDrawer(true)
        }
      })
    })
    graph.on('blank:click', () => {
      setIsRightDrawer(false)
    })

    const resizeFn = () => {
      const { width, height } = getContainerSize()
      graph.resize(width, height)
    }
    resizeFn()

    window.addEventListener('resize', resizeFn)
    return () => {
      window.removeEventListener('resize', resizeFn)
    }
  }, [])

  const getContainerSize = () => {
    return {
      width: document.body.offsetWidth - 214,
      height: document.body.offsetHeight - 95,
    }
  }

  const openGithub = () => {
    window.open(
      'https://www.ifrontend.net/',
      '_blank',
    )
  }

  const openUser = () => {
    history.push('/about')
  }

  const closeRightDrawer = () => {
    setIsRightDrawer(false)
  }

  return (
    <div className="antv-x6">
      <div className="header">
        <span className="txt">React-AntvX6-App</span>
        <span className="icon">
          <UserOutlined onClick={openUser} title="关于我们"></UserOutlined>
          <GithubOutlined onClick={openGithub} title="Github"></GithubOutlined>
        </span>
      </div>
      <div className="toolbar">
        {isReady && <ToolBar />}
      </div>
      {isRightDrawer && <RightDrawer selectCell={selectCell} close={closeRightDrawer} />}
      <div className="content">
        <div id="stencil" className="shapes"></div>
        <div id="container"></div>
        <div id="minimap" className="minimap"></div>
      </div>
    </div>
  )
}

export default Index

详细Github地址 :https://gitee.com/tangweiwei/vue-antvx6-app

前端开发那点事

微信公众号搜索“前端开发那点事”

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伍哥的传说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值