vue-wiring-diagram 1.0.15 → 1.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-wiring-diagram",
3
- "version": "1.0.15",
3
+ "version": "1.0.16",
4
4
  "description": "A Vue 3.x component for wiring diagram",
5
5
  "type": "module",
6
6
  "main": "dist/vue-wiring-diagram.umd.js",
@@ -64,7 +64,7 @@
64
64
  </template>
65
65
 
66
66
  <script setup>
67
- import {onMounted, reactive, ref, shallowRef} from "vue";
67
+ import {defineComponent, onMounted, reactive, ref, shallowRef} from "vue";
68
68
  import moment from 'moment'
69
69
  import {ElMessage, ElTooltip} from "element-plus";
70
70
  import {getData, showPorts} from "../common.js";
@@ -75,22 +75,26 @@ import edgeControl from "../edge-control/index.vue";
75
75
  import Shortcuts from "../Shortcuts.vue";
76
76
  import {Graph} from "@antv/x6";
77
77
  import {Stencil} from '@antv/x6-plugin-stencil'
78
- import {Transform} from "@antv/x6-plugin-transform";
79
- import {Snapline} from "@antv/x6-plugin-snapline";
80
- import {Clipboard} from '@antv/x6-plugin-clipboard'
81
- import {Keyboard} from "@antv/x6-plugin-keyboard";
82
- import {Selection} from '@antv/x6-plugin-selection'
83
- import {History} from '@antv/x6-plugin-history'
84
78
  import {Export} from "@antv/x6-plugin-export";
85
79
  import imageControl from "../image-control/index.vue";
86
- import {imageOptions} from "../image-control/imageOptions.js";
87
80
  import {baseShape} from "../baseShape.js";
88
81
  import {portsOptions} from "../portsOptions.js";
89
82
  import {Document, View, ArrowDown} from "@element-plus/icons-vue";
90
83
  import WiringDiagramPreview from "../preview/index.vue";
91
84
  import ImageManagement from "../image-control/image-management.vue";
92
85
  import {get} from "packages/http.js";
93
- import towerPoles from "packages/assets/image/tower-poles.svg";
86
+ import {
87
+ clipboard,
88
+ keyboard,
89
+ snapLine,
90
+ transform,
91
+ history,
92
+ selection,
93
+ setMathRandom,
94
+ deleteCell,
95
+ copyCell,
96
+ pasteCell, undoCell, redoCell, addLineTools
97
+ } from "packages/components/tools.js";
94
98
 
95
99
  defineOptions({
96
100
  name: 'editor'
@@ -106,7 +110,7 @@ const props = defineProps({
106
110
  const graph = ref() // 画布实例
107
111
  // 控制器
108
112
  const controls = reactive({
109
- label: null, // 控制器
113
+ label: '', // 控制器
110
114
  component: shallowRef(null), // 控制器组件
111
115
  })
112
116
  const controlsKey = ref(null) // 控制器key
@@ -117,14 +121,15 @@ const payload = ref(null)
117
121
  */
118
122
  const initGraph = () => {
119
123
  const container = document.getElementById('drawing-board')
120
- window.__x6_instances__ = []
121
124
  graph.value = new Graph({
122
125
  container: container,
123
126
  autoResize: true,
124
127
  panning: true,
125
128
  mousewheel: true,
126
129
  connecting: {
130
+ snap: true,
127
131
  allowEdge: true,
132
+ highlight: true,
128
133
  router: 'orth',
129
134
  connectionPoint: {
130
135
  name: 'anchor',
@@ -139,91 +144,74 @@ const initGraph = () => {
139
144
  zIndex: 0,
140
145
  })
141
146
  }
142
- }
143
- })
144
- window.__x6_instances__.push(graph.value)
145
-
146
- graph.value.use(
147
- new Transform({
148
- resizing: {
149
- enabled: true,
150
- preserveAspectRatio: false
151
- },
152
- rotating: true,
153
- })
154
- )
155
-
156
- graph.value.use(new Keyboard())
157
- graph.value.use(new Snapline())
158
- graph.value.use(new Clipboard({
159
- format(key) {
160
- return key.replace(/\s/g, '').replace('cmd', 'command')
161
147
  },
162
- }))
163
- graph.value.use(new History())
164
- graph.value.use(new Selection({
165
- enabled: true,
166
- multiple: true, //多选
167
- rubberband: true, //框选节点
168
- modifiers: ['alt'],
169
- showNodeSelectionBox: true,
170
- }))
171
-
148
+ })
149
+ graph.value.use(transform())
150
+ graph.value.use(keyboard())
151
+ graph.value.use(snapLine())
152
+ graph.value.use(clipboard())
153
+ graph.value.use(history())
154
+ graph.value.use(selection())
172
155
  graph.value.bindKey(['delete', 'backspace'], () => {
173
- const cells = graph.value.getSelectedCells()
174
- if (cells.length) {
175
- graph.value.removeCells(cells)
176
- }
177
- return false
156
+ deleteCell(graph.value)
178
157
  })
179
-
180
158
  graph.value.bindKey(['ctrl+c', 'cmd + c'], () => {
181
- const cells = graph.value.getSelectedCells()
182
- if (cells.length) {
183
- graph.value.copy(cells)
184
- ElMessage.success('复制成功')
185
- }
186
- return false
159
+ copyCell(graph.value)
187
160
  })
188
-
189
161
  graph.value.bindKey(['ctrl+v', 'cmd + v'], () => {
190
- if (!graph.value.isClipboardEmpty()) {
191
- const cells = graph.value.paste({offset: 32})
192
- graph.value.cleanSelection()
193
- graph.value.select(cells)
194
- ElMessage.success('粘贴成功')
195
- }
196
- return false
162
+ pasteCell(graph.value)
197
163
  })
198
-
199
164
  graph.value.bindKey(['ctrl+z', 'cmd + z'], () => {
200
- if (graph.value.canUndo()) {
201
- graph.value.undo()
202
- }
203
- return false
165
+ undoCell(graph.value)
204
166
  })
205
-
206
167
  graph.value.bindKey(['ctrl+y', 'cmd + y'], () => {
207
- if (graph.value.canRedo()) {
208
- graph.value.redo()
209
- }
210
- return false
168
+ redoCell(graph.value)
169
+ })
170
+
171
+ let setZIndexTimer = null
172
+
173
+ //给线添加拐点
174
+ graph.value.on('edge:mouseenter', ({cell}) => {
175
+ clearTimeout(setZIndexTimer)
176
+ cell.toFront()
177
+ addLineTools(cell)
178
+ })
179
+
180
+ // 隐藏线段的拐点
181
+ graph.value.on('edge:mouseleave', ({cell}) => {
182
+ setZIndexTimer = setTimeout(() => {
183
+ cell.toBack()
184
+ cell.removeTools()
185
+ }, 300)
186
+
187
+ })
188
+
189
+ //连接桩显示
190
+ graph.value.on('node:mouseenter', () => {
191
+ showPorts('#drawing-board', true)
192
+ })
193
+
194
+ //连接桩隐藏
195
+ graph.value.on('node:mouseleave', () => {
196
+ showPorts("#drawing-board", false)
211
197
  })
212
198
 
199
+ // 导出插件
200
+ graph.value.use(new Export())
201
+
213
202
  //选中画布
214
203
  graph.value.on('blank:click', ({e, x, y}) => {
215
204
  payload.value = graph
216
205
  controls.component = GraphControl
217
206
  controls.label = '画布属性'
218
- controlsKey.value = moment().valueOf()
207
+ controlsKey.value = setMathRandom()
219
208
  })
220
209
 
221
210
 
222
211
  //选中节点
223
212
  graph.value.on('cell:click', ({cell}) => {
224
- console.log('cell:click', cell, cell?.data?.type ?? cell?.shape)
225
213
  payload.value = cell
226
- controlsKey.value = moment().valueOf()
214
+ controlsKey.value = setMathRandom()
227
215
  if (cell.data?.type === 'text') {
228
216
  controls.component = textControl
229
217
  controls.label = '文本属性'
@@ -247,43 +235,6 @@ const initGraph = () => {
247
235
  controls.component = null
248
236
  controls.label = null
249
237
  })
250
-
251
- //给线添加拐点
252
- graph.value.on('edge:mouseenter', ({cell}) => {
253
- cell.addTools([
254
- {
255
- name: 'vertices',
256
- args: {
257
- snapRadius: 3,
258
- attrs: {
259
- r: 3,
260
- fill: '#239edd',
261
- cursor: 'move',
262
- 'stroke-width': 0
263
- },
264
- modifiers: ['alt']
265
- },
266
- },
267
- ]);
268
- })
269
-
270
- // 隐藏线段的拐点
271
- graph.value.on('edge:mouseleave', ({cell}) => {
272
- cell.removeTools()
273
- })
274
-
275
- //连接桩显示
276
- graph.value.on('node:mouseenter', () => {
277
- showPorts('#drawing-board', true)
278
- })
279
-
280
- //连接桩隐藏
281
- graph.value.on('node:mouseleave', () => {
282
- showPorts("#drawing-board", false)
283
- })
284
-
285
- // 导出插件
286
- graph.value.use(new Export())
287
238
  }
288
239
 
289
240
  const stencil = ref()
@@ -344,7 +295,6 @@ const initStencil = () => {
344
295
  document.getElementById('stencil').appendChild(stencil.value.container)
345
296
  })
346
297
  }).finally(() => {
347
- // 添加 hover 事件监听器
348
298
  setTimeout(() => {
349
299
  const nodes = stencil.value.container.querySelectorAll('.x6-node');
350
300
  nodes.forEach((node, index) => {
@@ -359,7 +309,6 @@ const initStencil = () => {
359
309
  text.style.fill = '#ffffff';
360
310
  }
361
311
  }
362
- // 添加 hover 事件监听器
363
312
  node.addEventListener('mouseenter', (e) => {
364
313
  if (index > baseShape.length) {
365
314
  const text = node.querySelector('text');
@@ -377,7 +326,7 @@ const initStencil = () => {
377
326
  }
378
327
  })
379
328
  });
380
- }, 20)
329
+ }, 200)
381
330
  })
382
331
  }
383
332
 
@@ -470,7 +419,6 @@ const drawLine = () => {
470
419
  */
471
420
  const adaptive = () => {
472
421
  graph.value.zoomToFit({maxScale: 1, padding: 20})
473
- // ElMessage.success('自适应成功')
474
422
  }
475
423
 
476
424
  const dialog = reactive({show: false}) // 弹窗
@@ -76,7 +76,6 @@ import {ref} from "vue";
76
76
  const props = defineProps({
77
77
  payload: Object
78
78
  })
79
- console.log(props.payload.value.options.grid)
80
79
  const color = ref(props.payload.value.options.background?.color || null) // 画布背景颜色
81
80
  const visible = ref(props.payload.value.options?.grid?.visible || null) // 网格是否显示
82
81
  const size = ref(props.payload.value.options?.grid?.size || 10) // 网格大小
@@ -33,6 +33,7 @@ export const stencilOptions = {
33
33
  },
34
34
  }
35
35
 
36
+ // 连接桩配置
36
37
  export const portOption = {
37
38
  position: 'absolute',
38
39
  args: {
@@ -93,7 +94,7 @@ export const textOptions = {
93
94
  }
94
95
 
95
96
 
96
- // 基础线配置
97
+ // 线配置
97
98
  export const lineOptions = {
98
99
  line: {
99
100
  stroke: '#ffffff',
@@ -105,7 +106,7 @@ export const lineOptions = {
105
106
  offset: 0,
106
107
  },
107
108
  name: null
108
- }, //取消箭头
109
+ },
109
110
  targetMarker: {
110
111
  args: {
111
112
  width: 10,
@@ -113,13 +114,6 @@ export const lineOptions = {
113
114
  offset: 0,
114
115
  },
115
116
  name: null
116
- }, //取消箭头
117
- },
118
- ports: {
119
- groups: {
120
- ports: {
121
- portOption
122
- },
123
- }
117
+ },
124
118
  },
125
119
  }
@@ -0,0 +1,196 @@
1
+ import {Transform} from "@antv/x6-plugin-transform";
2
+ import {Keyboard} from "@antv/x6-plugin-keyboard";
3
+ import {Snapline} from "@antv/x6-plugin-snapline";
4
+ import {Clipboard} from "@antv/x6-plugin-clipboard";
5
+ import {History} from "@antv/x6-plugin-history";
6
+ import {Selection} from "@antv/x6-plugin-selection";
7
+
8
+ /**
9
+ * 图形变换
10
+ * @returns {Transform}
11
+ */
12
+ export const transform = () => {
13
+ return new Transform({
14
+ resizing: {
15
+ enabled: true,
16
+ preserveAspectRatio: false
17
+ },
18
+ rotating: true,
19
+ })
20
+ }
21
+
22
+ /**
23
+ * 快捷键
24
+ * @returns {Keyboard}
25
+ */
26
+ export const keyboard = () => {
27
+ return new Keyboard()
28
+ }
29
+
30
+ /**
31
+ * 吸附线
32
+ * @returns {Snapline}
33
+ */
34
+ export const snapLine = () => {
35
+ return new Snapline()
36
+ }
37
+
38
+ /**
39
+ * 复制粘贴
40
+ * @returns {Clipboard|*}
41
+ */
42
+ export const clipboard = () => {
43
+ return new Clipboard({
44
+ format(key) {
45
+ return key.replace(/\s/g, '').replace('cmd', 'command')
46
+ },
47
+ })
48
+ }
49
+
50
+ // 排除撤销重做的命令
51
+ const excludeHistoryCommand = [
52
+ 'tools',
53
+ 'ports'
54
+ ]
55
+
56
+ /**
57
+ * 撤销重做
58
+ * @returns {History}
59
+ */
60
+ export const history = () => {
61
+ return new History({
62
+ beforeAddCommand: (name, args) => {
63
+ if (excludeHistoryCommand.includes(args.key)) {
64
+ return false
65
+ }
66
+ }
67
+ })
68
+ }
69
+
70
+ /**
71
+ * 框选
72
+ * @returns {Selection}
73
+ */
74
+ export const selection = () => {
75
+ return new Selection({
76
+ enabled: true,
77
+ multiple: true, //多选
78
+ rubberband: true, //框选节点
79
+ modifiers: ['alt'],
80
+ showNodeSelectionBox: true,
81
+ })
82
+ }
83
+
84
+ /**
85
+ * 删除
86
+ * @param graph
87
+ * @returns {boolean}
88
+ */
89
+ export const deleteCell = (graph) => {
90
+ const cells = graph.getSelectedCells()
91
+ if (cells.length) {
92
+ graph.removeCells(cells)
93
+ }
94
+ }
95
+
96
+ /**
97
+ * 复制
98
+ * @param graph
99
+ * @returns {boolean}
100
+ */
101
+ export const copyCell = (graph) => {
102
+ const cells = graph.getSelectedCells()
103
+ if (cells.length) {
104
+ graph.copy(cells)
105
+ }
106
+ }
107
+
108
+ /**
109
+ * 粘贴
110
+ * @param graph
111
+ * @returns {boolean}
112
+ */
113
+ export const pasteCell = (graph) => {
114
+ if (!graph.isClipboardEmpty()) {
115
+ const cells = graph.paste({offset: 32})
116
+ graph.cleanSelection()
117
+ graph.select(cells)
118
+ }
119
+ }
120
+
121
+ /**
122
+ * 撤销
123
+ * @param graph
124
+ * @returns {boolean}
125
+ */
126
+ export const undoCell = (graph) => {
127
+ if (graph.canUndo()) {
128
+ graph.undo()
129
+ }
130
+ }
131
+
132
+ /**
133
+ * 重做
134
+ * @param graph
135
+ * @returns {boolean}
136
+ */
137
+ export const redoCell = (graph) => {
138
+ if (graph.canRedo()) {
139
+ graph.redo()
140
+ }
141
+ }
142
+
143
+ /**
144
+ * 添加线工具
145
+ * @param cell
146
+ */
147
+ export const addLineTools = (cell) => {
148
+ cell.addTools([
149
+ {
150
+ name: 'vertices',
151
+ args: {
152
+ snapRadius: 3,
153
+ attrs: {
154
+ r: 3,
155
+ fill: '#239edd',
156
+ cursor: 'move',
157
+ 'stroke-width': 2
158
+ },
159
+ },
160
+ },
161
+ {
162
+ name: 'segments',
163
+ args: {
164
+ attrs: {
165
+ stroke: '#239edd',
166
+ strokeWidth: 2,
167
+ strokeDasharray: '5 5',
168
+ },
169
+ },
170
+ },
171
+ {
172
+ name: 'source-arrowhead',
173
+ args: {
174
+ attrs: {
175
+ stroke: '#239edd',
176
+ },
177
+ },
178
+ },
179
+ {
180
+ name: 'target-arrowhead',
181
+ args: {
182
+ attrs: {
183
+ stroke: '#239edd',
184
+ },
185
+ },
186
+ },
187
+ ]);
188
+ }
189
+
190
+ /**
191
+ * 设置随机数
192
+ * @returns {number}
193
+ */
194
+ export const setMathRandom = () => {
195
+ return Math.random()
196
+ }