BasicConnectingComponent.jsx 2.02 KB
Newer Older
Tamas Kecskes's avatar
Tamas Kecskes committed
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
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import Z_LEVELS from '../../utils/zLevels';

export default class BasicConnectingComponent extends Component {
    static propTypes = {
        connectionManager: PropTypes.object.isRequired,
    };

    constructor(props) {
        super(props);

        const {connectionManager} = this.props;

        connectionManager.setListener(this.onChange);
    }

    state = {
        startPos: null,
        currentPos: null,
        isConnecting: false,
    };

    onChange = (event) => {
        this.setState(event);
    };

    render() {
        const {isConnecting, startPos, currentPos} = this.state;
        let top;
        let left;
        let width;
        let height;

        if (isConnecting) {
            top = Math.min(startPos.y, currentPos.y);
            left = Math.min(startPos.x, currentPos.x);
            height = Math.abs(currentPos.y - startPos.y) + 5;
            width = Math.abs(currentPos.x - startPos.x) + 5;
            if (height > 0 && width > 0) {
                return (
                    <svg
                        width={width}
                        height={height}
                        viewBox={`${0} ${0} ${width} ${height}`}
                        style={{
                            position: 'absolute',
                            top: `${top - 5}px`,
                            left: `${left - 5}px`,
                            zIndex: Z_LEVELS.connection,
                        }}
                    >
                        <line
                            strokeWidth="3"
                            stroke="orange"
                            strokeDasharray="5 5"
                            x1={startPos.x - (left - 5)}
                            y1={startPos.y - (top - 5)}
                            x2={currentPos.x - (left - 5)}
                            y2={currentPos.y - (top - 5)}
                        />
                    </svg>
                );
            }
        }
        return null;
    }
}