-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathPort.js
71 lines (61 loc) · 2.15 KB
/
Port.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import useDrag from '../../shared/internal_hooks/useDrag';
import useCanvas from '../../shared/internal_hooks/useCanvas';
import getRelativePoint from '../../shared/functions/getRelativePoint';
/**
* Port
* @param props
* @returns {*}
* @constructor
*/
const Port = (props) => {
const { id, canLink, alignment, onDragNewSegment, onSegmentFail, onSegmentConnect, onMount, type, ...rest } = props;
const canvas = useCanvas();
const { ref, onDrag, onDragEnd } = useDrag();
onDrag((event, info) => {
if (onDragNewSegment) {
event.stopImmediatePropagation();
event.stopPropagation();
const from = getRelativePoint(info.start, [canvas.x, canvas.y]);
const to = getRelativePoint([event.clientX, event.clientY], [canvas.x, canvas.y]);
onDragNewSegment(id, from, to, alignment);
}
});
onDragEnd((event) => {
const targetPort = event.target.getAttribute('data-port-id');
if (targetPort && event.target !== ref.current && canLink(id, targetPort, type) && onSegmentConnect) {
const args = type === 'input' ? [id, targetPort, type] : [targetPort, id, type];
onSegmentConnect(...args);
return;
}
/* eslint-disable no-unused-expressions */
onSegmentFail && onSegmentFail(id, type);
/* eslint-enable no-unused-expressions */
});
useEffect(() => {
if (ref.current && onMount) {
onMount(id, ref.current);
}
}, [ref.current]);
return (<div className="bi bi-diagram-port" data-port-id={id} ref={ref} {...rest} />);
};
Port.propTypes = {
id: PropTypes.oneOfType([PropTypes.string, PropTypes.symbol]).isRequired,
type: PropTypes.oneOf(['input', 'output']).isRequired,
onDragNewSegment: PropTypes.func,
onSegmentFail: PropTypes.func,
onSegmentConnect: PropTypes.func,
canLink: PropTypes.func,
onMount: PropTypes.func,
alignment: PropTypes.oneOf(['right', 'left', 'top', 'bottom']),
};
Port.defaultProps = {
onDragNewSegment: undefined,
onSegmentFail: undefined,
onSegmentConnect: undefined,
canLink: () => true,
onMount: undefined,
alignment: undefined,
};
export default React.memo(Port);