gragh.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { Graph,Shape } from '@antv/x6';
  2. import { bindKey,myEvents } from './events';
  3. import { configOpt } from './toolConfig';
  4. import store from '@/vuex/index'
  5. const styleConfig=store.state.sand.styleConfig
  6. const { line } = configOpt;
  7. // wrapper DOM的Id mindmapDataUseFun 返回思维导图数组函数,传递给事件 type 模式,编辑和查看
  8. export function myGraph (wrapper,mindmapDataUseFun,type='edit') {
  9. const graph = new Graph({
  10. container: document.getElementById(wrapper),
  11. history:{
  12. enabled:true,
  13. beforeAddCommand(event, args){
  14. if(args.key=='tools'){
  15. // 工具的改变不加入撤销和重做的队列
  16. return false
  17. }
  18. }
  19. },
  20. autoResize: false,
  21. background: {
  22. color: '#fff',
  23. },
  24. scroller: {
  25. enabled: true,
  26. pannable: true,
  27. minVisibleWidth: 50,
  28. minVisibleHeight: 50,
  29. },
  30. selecting: {
  31. enabled: true,
  32. showNodeSelectionBox: false,
  33. multiple: true,
  34. multipleSelectionModifiers:['shift'],
  35. rubberband:true,
  36. // rubberNode:true,
  37. rubberEdge:true,//加上这个才能框选边 官方配置也不写……
  38. modifiers:['ctrl']
  39. },
  40. snapline: true, //对齐线
  41. clipboard: true,
  42. keyboard: {
  43. enabled: true,
  44. global: true,
  45. },//cv
  46. mousewheel:{
  47. enabled: true,
  48. modifiers:['ctrl','meta']
  49. }, //滚轮缩放
  50. grid: {
  51. size: 10, // 网格大小
  52. visible: false,
  53. },
  54. highlighting: {
  55. // 当链接桩可以被链接时,在链接桩外围渲染一个 2px 宽的天蓝色矩形框
  56. magnetAvailable: {
  57. name: "stroke",
  58. args: {
  59. padding: 0.8,
  60. attrs: {
  61. "stroke-width": 2,
  62. stroke: "skyblue",
  63. },
  64. },
  65. },
  66. // 连线过程中,自动吸附到链接桩时被使用
  67. magnetAdsorbed: {
  68. name: "stroke",
  69. args: {
  70. padding: 0.8,
  71. attrs: {
  72. "stroke-width": 4,
  73. stroke: "skyblue",
  74. },
  75. },
  76. },
  77. },
  78. interacting:type=='view'?{
  79. nodeMovable:false,
  80. magnetConnectable:false,
  81. edgeMovable:false,
  82. edgeLabelMovable:false,
  83. arrowheadMovable:false,
  84. vertexMovable:false,
  85. vertexMovable:false,
  86. vertexDeletable:false
  87. }:function(cellView){
  88. if(cellView.cell.shape == 'mindmap-child-datanode-title' && cellView.cell.data && cellView.cell.data.calculationMethod && cellView.cell.data.calculationMethod.length > 0){
  89. return {nodeMovable:false};
  90. }
  91. return true
  92. },
  93. connecting: {
  94. snap: true,
  95. // 允许连接到空白位置
  96. allowBlank: true,
  97. // 不允许创建循环连线
  98. allowLoop: false,
  99. // 不允许在相同节点创建多条边
  100. // allowMulti: false,
  101. //不允许直接连接到节点
  102. allowNode: false,
  103. // 高亮
  104. highlight: true,
  105. connector: {
  106. name: 'normal',
  107. args: {
  108. padding:1
  109. }
  110. },
  111. connectionPoint: 'anchor',
  112. router: "manhattan",
  113. // 定义边样式
  114. createEdge() {
  115. return new Shape.Edge({
  116. attrs: {
  117. line: {
  118. stroke: styleConfig.lineColor,
  119. strokeWidth: line.width,
  120. strokeDasharray: "",//虚线间隔
  121. sourceMarker: false,//起始箭头
  122. targetMarker: 'classic',//终止箭头
  123. },
  124. },
  125. zIndex: 0,
  126. })
  127. },
  128. },//连线
  129. resizing: {
  130. enabled: type=='view'?false:true,
  131. orthogonal: false,
  132. },
  133. scaling: {
  134. min: 0.5,
  135. max: 2
  136. },
  137. //小地图
  138. minimap: {
  139. enabled: true,
  140. container: document.getElementById("minimap"),
  141. }
  142. })
  143. /* 节点操作事件 */
  144. if(type!='view') myEvents(graph,mindmapDataUseFun);
  145. /* 键盘事件 */
  146. if(type!='view') bindKey(graph,mindmapDataUseFun);
  147. return graph;
  148. }