@kne/react-flow-chart 0.1.0

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.
@@ -0,0 +1,702 @@
1
+ import React, { createContext, useContext as useContext$1, useState, useRef, useMemo, useEffect } from 'react';
2
+ import useEvent from '@kne/use-event';
3
+ import classnames from 'classnames';
4
+ import { ReactComponent } from './add~nSLLXReQ.svg';
5
+ import arrow from './arrow~bbpMpvlG.svg';
6
+ import { ReactComponent as ReactComponent$1 } from './delete~RuDNzAkQ.svg';
7
+ import { v4 } from 'uuid';
8
+ import useControlValue from '@kne/use-control-value';
9
+
10
+ function _objectWithoutPropertiesLoose(r, e) {
11
+ if (null == r) return {};
12
+ var t = {};
13
+ for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
14
+ if (e.includes(n)) continue;
15
+ t[n] = r[n];
16
+ }
17
+ return t;
18
+ }
19
+
20
+ var style = {"container":"_AIhaC","show-scrollbar":"_qp1e9","content":"_XZmmG","is-scale":"_q6z6A","vertical":"_B4OPa","arrow-line":"_W5dM8","show-arrow":"_0Cmjn","add-node":"_sVaw9","add-node-btn":"_Y4-Yv","dot":"_06DKa","normal":"_59XmF","node-type-popover":"_6Grsl","show":"_DGtHa","action-bar":"_vxlwT","action-scale":"_xd3mP","action-scale-btn":"_IVeCE","action-scale-num":"_c9-Rl","condition-node-container":"_QTtQr","condition-add-btn":"_9Gq3x","condition-node-item-list":"_MlJIV","condition-node-item":"_4TP17","condition-node-item-line":"_Q-b5J","condition-node-item-last-line":"_n1DDB","condition-node-item-link-line":"_l-p-G","condition-node-item-node-wrap":"_MnV8A","condition-node-item-link-cross-line":"_ERDNK","delete-node":"_I73Kt","end-node-container":"_WzQqY","node-container":"_HGnYM","normal-node-content":"_Uf6Nh","normal-node-title":"_-7Pvj","normal-node-data":"_leHgE","normal-node-data-text":"_Kqf3r","normal-node-data-icon":"_-RR3K","node-type-content":"_0sM8G","node-type-group":"_wsgr5","node-type-group-name":"_ka6d9","node-type-list":"_lmTJ8","node-type-item":"_4q38j","normal-node-container":"_vS7Kd","normal-node-wrap":"_FhjH6","normal-node-content-wrap":"_EYYsJ","start-node-container":"_pydi2","start-node-content":"_u41XY"};
21
+
22
+ const defaultNodeList = [{
23
+ id: 'startEvent',
24
+ type: 'start',
25
+ title: '开始'
26
+ }, {
27
+ id: 'endEvent',
28
+ type: 'end',
29
+ title: '结束'
30
+ }];
31
+ const defaultNodeTypeList = [{
32
+ name: '普通节点',
33
+ list: [{
34
+ type: 'normal',
35
+ name: '普通节点'
36
+ }]
37
+ }, {
38
+ name: '分支节点',
39
+ list: [{
40
+ type: 'condition',
41
+ name: '条件分支'
42
+ }]
43
+ }];
44
+
45
+ const context = createContext({});
46
+ const {
47
+ Provider,
48
+ Consumer
49
+ } = context;
50
+ const useContext = () => {
51
+ return useContext$1(context);
52
+ };
53
+
54
+ const ActionBar = props => {
55
+ const {
56
+ scale,
57
+ onChange
58
+ } = Object.assign({}, {}, props);
59
+ return /*#__PURE__*/React.createElement("div", {
60
+ className: style['action-bar']
61
+ }, /*#__PURE__*/React.createElement("div", {
62
+ className: style['action-scale']
63
+ }, /*#__PURE__*/React.createElement("div", {
64
+ className: style['action-scale-btn'],
65
+ onClick: () => {
66
+ onChange(scale + 10);
67
+ }
68
+ }, /*#__PURE__*/React.createElement("svg", {
69
+ viewBox: "0 0 1024 1024",
70
+ version: "1.1",
71
+ xmlns: "http://www.w3.org/2000/svg"
72
+ }, /*#__PURE__*/React.createElement("path", {
73
+ d: "M580.722174 437.990403 580.722174 78.171384 436.794158 78.171384 436.794158 437.990403 76.975139 437.990403 76.975139 581.918419 436.794158 581.918419 436.794158 941.737438 580.722174 941.737438 580.722174 581.918419 940.542216 581.918419 940.542216 437.990403Z"
74
+ }))), /*#__PURE__*/React.createElement("div", {
75
+ className: style['action-scale-num']
76
+ }, scale.toFixed(0), "%"), /*#__PURE__*/React.createElement("div", {
77
+ className: style['action-scale-btn'],
78
+ onClick: () => {
79
+ onChange(scale - 10);
80
+ }
81
+ }, /*#__PURE__*/React.createElement("svg", {
82
+ viewBox: "0 0 1024 1024",
83
+ version: "1.1",
84
+ xmlns: "http://www.w3.org/2000/svg"
85
+ }, /*#__PURE__*/React.createElement("path", {
86
+ d: "M587.229378 437.990403 580.722174 437.990403 76.975139 437.990403 76.975139 581.918419 580.722174 581.918419 587.229378 581.918419 940.542216 581.918419 940.542216 437.990403Z"
87
+ })))));
88
+ };
89
+
90
+ const ArrowLine = props => {
91
+ const {
92
+ showArrow
93
+ } = Object.assign({}, {
94
+ showArrow: true
95
+ }, props);
96
+ const {
97
+ vertical
98
+ } = useContext();
99
+ return /*#__PURE__*/React.createElement("div", {
100
+ className: classnames(style['arrow-line'], {
101
+ [style['show-arrow']]: showArrow,
102
+ [style['vertical']]: vertical
103
+ })
104
+ });
105
+ };
106
+
107
+ const NodeTypeContent = props => {
108
+ const {
109
+ onClick
110
+ } = Object.assign({}, {}, props);
111
+ const {
112
+ nodeTypeList
113
+ } = useContext();
114
+ return /*#__PURE__*/React.createElement("div", {
115
+ className: style['node-type-content']
116
+ }, (nodeTypeList || defaultNodeTypeList).map((group, groupIndex) => {
117
+ return /*#__PURE__*/React.createElement("div", {
118
+ className: style['node-type-group'],
119
+ key: groupIndex
120
+ }, /*#__PURE__*/React.createElement("div", {
121
+ className: style['node-type-group-name']
122
+ }, group.name), /*#__PURE__*/React.createElement("div", {
123
+ className: style['node-type-list']
124
+ }, group.list.map((nodeType, typeIndex) => {
125
+ return /*#__PURE__*/React.createElement("div", {
126
+ className: style['node-type-item'],
127
+ key: typeIndex,
128
+ onClick: e => {
129
+ onClick && onClick(nodeType);
130
+ }
131
+ }, nodeType.name);
132
+ })));
133
+ }));
134
+ };
135
+
136
+ const AddNode = props => {
137
+ const {
138
+ nodeData,
139
+ btnType
140
+ } = Object.assign({}, {
141
+ btnType: 'normal'
142
+ }, props);
143
+ const [addNodeBtnType, setAddNodeBtnType] = useState('');
144
+ const [showNodeTypePopover, setShowNodeTypePopover] = useState(false);
145
+ const [nodeTypePopoverStyle, setNodeTypePopoverStyle] = useState({});
146
+ const nodeTypePopoverRef = useRef(null);
147
+ const {
148
+ readonly,
149
+ vertical,
150
+ emitter
151
+ } = useContext();
152
+ const type = useMemo(() => {
153
+ return addNodeBtnType === 'normal' ? addNodeBtnType : btnType || addNodeBtnType;
154
+ }, [addNodeBtnType, btnType]);
155
+ const nodeTypePopoverStyleChange = () => {
156
+ const el = nodeTypePopoverRef.current;
157
+ if (!el) {
158
+ return;
159
+ }
160
+ const elWidth = el.offsetWidth,
161
+ elHeight = el.offsetHeight;
162
+ const {
163
+ left,
164
+ top
165
+ } = el.getBoundingClientRect();
166
+ const windowWidth = window.innerWidth,
167
+ windowHeight = window.innerHeight;
168
+ const style = {};
169
+ style[top + elHeight > windowHeight ? 'bottom' : 'top'] = 0;
170
+ style[left + elWidth > windowWidth ? 'right' : 'left'] = 42 + 'px';
171
+ setNodeTypePopoverStyle(style);
172
+ };
173
+ const nodeTypePopoverStyleReset = () => {
174
+ setNodeTypePopoverStyle({
175
+ left: '42px',
176
+ top: 0
177
+ });
178
+ };
179
+ useEffect(() => {
180
+ if (showNodeTypePopover) {
181
+ nodeTypePopoverStyleChange();
182
+ } else {
183
+ setTimeout(() => {
184
+ nodeTypePopoverStyleReset();
185
+ }, 300);
186
+ }
187
+ }, [showNodeTypePopover]);
188
+ if (readonly) {
189
+ return null;
190
+ }
191
+ return /*#__PURE__*/React.createElement("div", {
192
+ className: classnames(style['add-node'], {
193
+ [style['vertical']]: vertical
194
+ }),
195
+ onMouseEnter: () => {
196
+ setAddNodeBtnType('normal');
197
+ },
198
+ onMouseLeave: () => {
199
+ setAddNodeBtnType('');
200
+ }
201
+ }, /*#__PURE__*/React.createElement("div", {
202
+ className: classnames(style['add-node-btn'], type && style[type]),
203
+ onMouseEnter: () => {
204
+ setShowNodeTypePopover(true);
205
+ },
206
+ onMouseLeave: () => {
207
+ setShowNodeTypePopover(false);
208
+ }
209
+ }, /*#__PURE__*/React.createElement(ReactComponent, null), /*#__PURE__*/React.createElement("div", {
210
+ ref: nodeTypePopoverRef,
211
+ className: classnames(style['node-type-popover'], {
212
+ [style['show']]: showNodeTypePopover
213
+ }),
214
+ style: nodeTypePopoverStyle
215
+ }, /*#__PURE__*/React.createElement(NodeTypeContent, {
216
+ onClick: ({
217
+ type
218
+ }) => {
219
+ emitter.emit('add-node-type-click', {
220
+ type,
221
+ nodeData
222
+ });
223
+ setShowNodeTypePopover(false);
224
+ }
225
+ }))));
226
+ };
227
+
228
+ const StartNode = props => {
229
+ const {
230
+ vertical
231
+ } = useContext();
232
+ const {
233
+ nodeData,
234
+ isMouseEnter
235
+ } = Object.assign({}, props);
236
+ return /*#__PURE__*/React.createElement("div", {
237
+ className: classnames(style['start-node-container'], {
238
+ [style['vertical']]: vertical
239
+ })
240
+ }, /*#__PURE__*/React.createElement("div", {
241
+ className: style['start-node-content']
242
+ }, nodeData.title), /*#__PURE__*/React.createElement(ArrowLine, null), /*#__PURE__*/React.createElement(AddNode, {
243
+ nodeData: nodeData,
244
+ btnType: isMouseEnter ? 'dot' : ''
245
+ }));
246
+ };
247
+
248
+ const EndNode = props => {
249
+ const {
250
+ nodeData
251
+ } = Object.assign({}, {}, props);
252
+ return /*#__PURE__*/React.createElement("div", {
253
+ className: style['end-node-container']
254
+ }, nodeData.title);
255
+ };
256
+
257
+ const ConditionNode = props => {
258
+ const {
259
+ nodeData,
260
+ isMouseEnter
261
+ } = Object.assign({}, {
262
+ nodeList: null,
263
+ data: null,
264
+ isMouseEnter: false
265
+ }, props);
266
+ const {
267
+ vertical,
268
+ readonly,
269
+ emitter
270
+ } = useContext();
271
+ return /*#__PURE__*/React.createElement("div", {
272
+ className: classnames(style['condition-node-container'], {
273
+ [style['vertical']]: vertical
274
+ })
275
+ }, !readonly && /*#__PURE__*/React.createElement("div", {
276
+ className: style['condition-add-btn'],
277
+ onClick: () => {
278
+ emitter.emit('add-condition-branch-click', {
279
+ nodeData
280
+ });
281
+ }
282
+ }, "\u6DFB\u52A0\u6761\u4EF6"), /*#__PURE__*/React.createElement("div", {
283
+ className: style['condition-node-item-list']
284
+ }, nodeData.children.map((node, index) => {
285
+ return /*#__PURE__*/React.createElement("div", {
286
+ key: index,
287
+ className: style['condition-node-item']
288
+ }, /*#__PURE__*/React.createElement("div", {
289
+ className: classnames(style['condition-node-item-line'], style['condition-node-item-first-line'])
290
+ }), /*#__PURE__*/React.createElement("div", {
291
+ className: classnames(style['condition-node-item-line'], style['condition-node-item-last-line'])
292
+ }), /*#__PURE__*/React.createElement("div", {
293
+ className: style['condition-node-item-link-line']
294
+ }), /*#__PURE__*/React.createElement("div", {
295
+ className: style['condition-node-item-node-wrap']
296
+ }, (Array.isArray(node) ? node : [node]).map(node => {
297
+ return /*#__PURE__*/React.createElement(Node, {
298
+ key: node.id,
299
+ nodeData: node,
300
+ isMouseEnter: isMouseEnter
301
+ });
302
+ }), /*#__PURE__*/React.createElement("div", {
303
+ className: style['condition-node-item-link-cross-line']
304
+ })));
305
+ })), /*#__PURE__*/React.createElement(ArrowLine, null), /*#__PURE__*/React.createElement(AddNode, {
306
+ nodeData: nodeData,
307
+ btnType: isMouseEnter ? 'dot' : ''
308
+ }));
309
+ };
310
+
311
+ const NodeContent = props => {
312
+ const {
313
+ nodeData
314
+ } = Object.assign({}, {}, props);
315
+ return /*#__PURE__*/React.createElement("div", {
316
+ className: style['normal-node-content']
317
+ }, /*#__PURE__*/React.createElement("div", {
318
+ className: style['normal-node-title']
319
+ }, nodeData.title || ''), /*#__PURE__*/React.createElement("div", {
320
+ className: style['normal-node-data']
321
+ }, /*#__PURE__*/React.createElement("div", {
322
+ className: style['normal-node-data-text']
323
+ }, nodeData.content || ''), /*#__PURE__*/React.createElement("img", {
324
+ src: arrow,
325
+ alt: "\u7BAD\u5934",
326
+ className: style['normal-node-data-icon']
327
+ })));
328
+ };
329
+
330
+ const DeleteNode = props => {
331
+ const {
332
+ onClick
333
+ } = Object.assign({}, {}, props);
334
+ return /*#__PURE__*/React.createElement("div", {
335
+ className: style['delete-node'],
336
+ onClick: e => {
337
+ e.stopPropagation();
338
+ onClick && onClick(e);
339
+ }
340
+ }, /*#__PURE__*/React.createElement(ReactComponent$1, null));
341
+ };
342
+
343
+ const NormalNode = props => {
344
+ const {
345
+ nodeData,
346
+ isMouseEnter
347
+ } = Object.assign({}, {}, props);
348
+ const [showDeleteBtn, setShowDeleteBtn] = useState(false);
349
+ const {
350
+ readonly,
351
+ vertical,
352
+ emitter
353
+ } = useContext();
354
+ const timer = useRef(null);
355
+ return /*#__PURE__*/React.createElement("div", {
356
+ className: classnames(style['normal-node-container'], {
357
+ [style['vertical']]: vertical
358
+ })
359
+ }, /*#__PURE__*/React.createElement("div", {
360
+ className: style['normal-node-wrap']
361
+ }, /*#__PURE__*/React.createElement("div", {
362
+ className: style['normal-node-content-wrap'],
363
+ onMouseEnter: () => {
364
+ clearTimeout(timer.current);
365
+ setShowDeleteBtn(true);
366
+ },
367
+ onMouseLeave: () => {
368
+ timer.current = setTimeout(() => {
369
+ setShowDeleteBtn(false);
370
+ }, 200);
371
+ }
372
+ }, /*#__PURE__*/React.createElement(NodeContent, {
373
+ nodeData: nodeData
374
+ }), showDeleteBtn && !readonly && /*#__PURE__*/React.createElement(DeleteNode, {
375
+ onClick: () => {
376
+ emitter.emit('delete-node-click', {
377
+ nodeData
378
+ });
379
+ }
380
+ })), /*#__PURE__*/React.createElement(ArrowLine, null), /*#__PURE__*/React.createElement(AddNode, {
381
+ nodeData: nodeData,
382
+ btnType: isMouseEnter ? 'dot' : ''
383
+ }), Array.isArray(nodeData.children) && nodeData.children.length > 0 && nodeData.children.map(node => {
384
+ return /*#__PURE__*/React.createElement(Node, {
385
+ key: node.id,
386
+ nodeData: node,
387
+ isMouseEnter: isMouseEnter
388
+ });
389
+ })));
390
+ };
391
+
392
+ const Node = props => {
393
+ const {
394
+ nodeData,
395
+ belongConditionNodeData,
396
+ isMouseEnter
397
+ } = Object.assign({}, {}, props);
398
+ const [isCurrentMouseEnter, setIsCurrentMouseEnter] = useState(false);
399
+ const renderNode = ({
400
+ nodeData
401
+ }) => {
402
+ if (nodeData.type === 'start') {
403
+ return /*#__PURE__*/React.createElement(StartNode, {
404
+ nodeData: nodeData,
405
+ isMouseEnter: isMouseEnter || isCurrentMouseEnter
406
+ });
407
+ }
408
+ if (nodeData.type === 'end') {
409
+ return /*#__PURE__*/React.createElement(EndNode, {
410
+ nodeData: nodeData
411
+ });
412
+ }
413
+ if (nodeData.type === 'condition') {
414
+ return /*#__PURE__*/React.createElement(ConditionNode, {
415
+ nodeData: nodeData,
416
+ isMouseEnter: isMouseEnter || isCurrentMouseEnter
417
+ });
418
+ }
419
+ return /*#__PURE__*/React.createElement(NormalNode, {
420
+ nodeData: nodeData,
421
+ isMouseEnter: isMouseEnter || isCurrentMouseEnter,
422
+ belongConditionNodeData: belongConditionNodeData
423
+ });
424
+ };
425
+ return /*#__PURE__*/React.createElement("div", {
426
+ className: style['node-container'],
427
+ onMouseEnter: () => {
428
+ setIsCurrentMouseEnter(true);
429
+ },
430
+ onMouseLeave: () => {
431
+ setIsCurrentMouseEnter(false);
432
+ }
433
+ }, renderNode({
434
+ nodeData
435
+ }));
436
+ };
437
+
438
+ const appendNode = (tree, {
439
+ currentNodeId,
440
+ node,
441
+ type
442
+ }) => {
443
+ const core = tree => {
444
+ if (Array.isArray(tree)) {
445
+ const currentIndex = tree.findIndex(node => node.id === currentNodeId);
446
+ if (currentIndex > -1) {
447
+ const newTree = tree.slice(0);
448
+ if (newTree[currentIndex].type === 'condition' && type === 'condition') {
449
+ const newNode = Object.assign({}, newTree[currentIndex]);
450
+ const newChildren = (newNode.children || []).slice(0);
451
+ newChildren.push([node]);
452
+ newNode.children = newChildren;
453
+ newTree.splice(currentIndex, 1, newNode);
454
+ } else {
455
+ newTree.splice(currentIndex + 1, 0, node);
456
+ }
457
+ return newTree;
458
+ } else {
459
+ return tree.map(node => core(node));
460
+ }
461
+ }
462
+ return Object.assign({}, tree, tree.children ? {
463
+ children: core(tree.children)
464
+ } : null);
465
+ };
466
+ return core(tree);
467
+ };
468
+ const removeNode = (tree, {
469
+ currentNodeId
470
+ }) => {
471
+ const core = tree => {
472
+ if (Array.isArray(tree)) {
473
+ const currentIndex = tree.findIndex(node => node.id === currentNodeId);
474
+ if (currentIndex > -1) {
475
+ const newTree = tree.slice(0);
476
+ if (newTree[currentIndex].type === 'branch') {
477
+ return [];
478
+ }
479
+ newTree.splice(currentIndex, 1);
480
+ return newTree;
481
+ } else {
482
+ return tree.map(node => core(node)).filter(node => {
483
+ return !(node.type === 'condition' && node.children.length < 2);
484
+ });
485
+ }
486
+ }
487
+ return Object.assign({}, tree, tree.children ? {
488
+ children: core(tree.children).filter(item => item && item.length > 0)
489
+ } : null);
490
+ };
491
+ return core(tree);
492
+ };
493
+ const replaceNode = (tree, {
494
+ node
495
+ }) => {
496
+ const core = tree => {
497
+ if (Array.isArray(tree)) {
498
+ for (let node of tree) {
499
+ const res = core(node);
500
+ if (res) {
501
+ return res;
502
+ }
503
+ }
504
+ }
505
+ if (tree.id === node.id) {
506
+ return Object.assign({}, node, {
507
+ type: tree.type
508
+ });
509
+ }
510
+ return tree.children ? core(tree.children) : null;
511
+ };
512
+ return core(tree);
513
+ };
514
+
515
+ const _excluded = ["className"],
516
+ _excluded2 = ["readonly", "showScrollBar", "enableDrag", "initFit", "vertical", "onEditNode"];
517
+ const FlowChart = _ref => {
518
+ let {
519
+ className
520
+ } = _ref,
521
+ props = _objectWithoutPropertiesLoose(_ref, _excluded);
522
+ const _Object$assign = Object.assign({}, {
523
+ showScrollBar: true,
524
+ enableDrag: true,
525
+ initFit: true,
526
+ vertical: false
527
+ }, props),
528
+ {
529
+ readonly,
530
+ showScrollBar,
531
+ enableDrag,
532
+ initFit,
533
+ vertical,
534
+ onEditNode
535
+ } = _Object$assign,
536
+ otherProps = _objectWithoutPropertiesLoose(_Object$assign, _excluded2);
537
+ const [nodeListState, setNodeList] = useControlValue(otherProps);
538
+ const nodeList = Array.isArray(nodeListState) && nodeListState.length > 0 ? nodeListState : defaultNodeList;
539
+ const [scale, setScale] = useState(100);
540
+ const mousePosition = useRef({});
541
+ const containerRef = useRef(null);
542
+ const contentRef = useRef(null);
543
+ const nodeListRef = useRef(null);
544
+ nodeListRef.current = nodeList;
545
+ const onEditNodeRef = useRef(null);
546
+ onEditNodeRef.current = onEditNode;
547
+ const emitter = useEvent({
548
+ name: 'flow-chart'
549
+ });
550
+ useEffect(() => {
551
+ if (initFit) {
552
+ const containerSize = containerRef.current.getBoundingClientRect();
553
+ const containerRatio = containerSize.width / containerSize.height;
554
+ const contentSize = contentRef.current.getBoundingClientRect();
555
+ const contentRatio = contentSize.width / contentSize.height;
556
+ let w, h;
557
+ if (containerRatio > contentRatio) {
558
+ // 以容器的高为准
559
+ h = containerSize.height;
560
+ w = contentRatio * h;
561
+ } else {
562
+ // 以容器的宽为准
563
+ w = containerSize.width;
564
+ h = w / contentRatio;
565
+ }
566
+ setScale(parseInt(w / contentSize.width * 100));
567
+ const x = (contentSize.width - containerSize.width) / 2;
568
+ const y = (contentSize.height - containerSize.height) / 2;
569
+ containerRef.current.scrollTo(x, y);
570
+ }
571
+ }, [initFit]);
572
+ useEffect(() => {
573
+ emitter.addListener('add-node-type-click', ({
574
+ type,
575
+ nodeData
576
+ }) => {
577
+ if (type === 'condition') {
578
+ return setNodeList(appendNode(nodeListRef.current, {
579
+ currentNodeId: nodeData.id,
580
+ node: {
581
+ id: v4(),
582
+ type: 'condition',
583
+ title: '条件分支',
584
+ children: [[{
585
+ id: v4(),
586
+ type: 'branch',
587
+ title: '条件',
588
+ content: '条件内容'
589
+ }], [{
590
+ id: v4(),
591
+ type: 'branch',
592
+ title: '条件',
593
+ content: '条件内容'
594
+ }]]
595
+ }
596
+ }));
597
+ }
598
+ if (type === 'normal') {
599
+ return setNodeList(appendNode(nodeListRef.current, {
600
+ currentNodeId: nodeData.id,
601
+ node: {
602
+ id: v4(),
603
+ type: 'normal',
604
+ title: '普通节点',
605
+ content: '节点内容'
606
+ }
607
+ }));
608
+ }
609
+ });
610
+ emitter.addListener('delete-node-click', ({
611
+ nodeData
612
+ }) => {
613
+ setNodeList(removeNode(nodeListRef.current, {
614
+ currentNodeId: nodeData.id
615
+ }));
616
+ });
617
+ emitter.addListener('add-condition-branch-click', ({
618
+ nodeData
619
+ }) => {
620
+ setNodeList(appendNode(nodeListRef.current, {
621
+ currentNodeId: nodeData.id,
622
+ type: 'condition',
623
+ node: {
624
+ id: v4(),
625
+ type: 'branch',
626
+ title: '条件',
627
+ content: '条件内容'
628
+ }
629
+ }));
630
+ });
631
+ emitter.addListener('node-content-click', ({
632
+ nodeData
633
+ }) => {
634
+ onEditNodeRef.current && onEditNodeRef.current({
635
+ nodeData,
636
+ replaceNode: node => {
637
+ setNodeList(replaceNode(nodeListRef.current, {
638
+ node
639
+ }));
640
+ }
641
+ });
642
+ });
643
+ const mouseMoveHandler = e => {
644
+ if (!(mousePosition.current.isMousedown && containerRef.current && enableDrag)) return;
645
+ e.preventDefault();
646
+ let nx = mousePosition.current.scrollLeft - (e.clientX - mousePosition.current.x);
647
+ let ny = mousePosition.current.scrollTop - (e.clientY - mousePosition.current.y);
648
+ containerRef.current.scrollTo(nx, ny);
649
+ };
650
+ const mouseUpHandler = () => {
651
+ mousePosition.current.isMousedown = false;
652
+ };
653
+ window.addEventListener('mousemove', mouseMoveHandler, false);
654
+ window.addEventListener('mouseup', mouseUpHandler, false);
655
+ return () => {
656
+ emitter.removeAllListeners();
657
+ window.removeEventListener('mousemove', mouseMoveHandler, false);
658
+ window.removeEventListener('mouseup', mouseUpHandler, false);
659
+ };
660
+ }, [emitter, enableDrag]);
661
+ return /*#__PURE__*/React.createElement(Provider, {
662
+ value: {
663
+ readonly,
664
+ vertical,
665
+ nodeList,
666
+ emitter
667
+ }
668
+ }, /*#__PURE__*/React.createElement("div", {
669
+ ref: containerRef,
670
+ className: classnames(className, 'flow-chart-container', style['container'], {
671
+ [style['show-scrollbar']]: showScrollBar
672
+ }),
673
+ onMouseDown: e => {
674
+ mousePosition.current.isMousedown = true;
675
+ mousePosition.current.x = e.clientX;
676
+ mousePosition.current.y = e.clientY;
677
+ mousePosition.current.scrollLeft = containerRef.current.scrollLeft;
678
+ mousePosition.current.scrollTop = containerRef.current.scrollTop;
679
+ }
680
+ }, /*#__PURE__*/React.createElement(ActionBar, {
681
+ scale: scale,
682
+ onChange: setScale
683
+ }), /*#__PURE__*/React.createElement("div", {
684
+ ref: contentRef,
685
+ className: classnames('flow-chart-content', style['content'], {
686
+ [style['vertical']]: vertical,
687
+ [style['is-scale']]: scale <= 100
688
+ }),
689
+ style: {
690
+ transform: `scale(${scale / 100})`
691
+ }
692
+ }, nodeList.map(node => {
693
+ return /*#__PURE__*/React.createElement(Node, {
694
+ key: node.id,
695
+ nodeList: nodeList,
696
+ nodeData: node
697
+ });
698
+ }))));
699
+ };
700
+
701
+ export { FlowChart as default };
702
+ //# sourceMappingURL=index.modern.js.map