This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
forked from maxGraph/maxGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdgeTolerance.stories.js
114 lines (97 loc) · 3.06 KB
/
EdgeTolerance.stories.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { Graph, mathUtils } from '@maxgraph/core';
import { globalTypes } from '../.storybook/preview';
export default {
title: 'Connections/EdgeTolerance',
argTypes: {
...globalTypes,
},
};
const Template = ({ label, ...args }) => {
const container = document.createElement('div');
container.style.position = 'relative';
container.style.overflow = 'hidden';
container.style.width = `${args.width}px`;
container.style.height = `${args.height}px`;
container.style.background = 'url(/images/grid.gif)';
container.style.cursor = 'default';
class MyCustomGraph extends Graph {
fireMouseEvent(evtName, me, sender) {
// Overrides the mouse event dispatching mechanism to update the
// cell which is associated with the event in case the native hit
// detection did not return anything.
// Checks if native hit detection did not return anything
if (me.getState() == null) {
// Updates the graph coordinates in the event since we need
// them here. Storing them in the event means the overridden
// method doesn't have to do this again.
if (me.graphX == null || me.graphY == null) {
const pt = mathUtils.convertPoint(container, me.getX(), me.getY());
me.graphX = pt.x;
me.graphY = pt.y;
}
const cell = this.getCellAt(me.graphX, me.graphY);
if (cell?.isEdge()) {
me.state = this.view.getState(cell);
if (me.state != null && me.state.shape != null) {
this.container.style.cursor = me.state.shape.node.style.cursor;
}
}
}
if (me.state == null) {
this.container.style.cursor = 'default';
}
super.fireMouseEvent(evtName, me, sender);
}
dblClick(evt, cell) {
// Overrides double click handling to use the tolerance
if (cell == null) {
const pt = mathUtils.convertPoint(
el,
eventUtils.getClientX(evt),
eventUtils.getClientY(evt)
);
cell = this.getCellAt(pt.x, pt.y);
}
super.dblClick(evt, cell);
}
}
// Creates the graph inside the given container
const graph = new MyCustomGraph(container);
graph.setEventTolerance(20);
// Gets the default parent for inserting new cells. This
// is normally the first child of the root (ie. layer 0).
const parent = graph.getDefaultParent();
// Adds cells to the model in a single step
graph.batchUpdate(() => {
const v1 = graph.insertVertex({
parent,
value: 'Hello,',
position: [120, 120],
size: [80, 30],
});
const v2 = graph.insertVertex({
parent,
value: 'World!',
position: [400, 250],
size: [80, 30],
});
const e1 = graph.insertEdge({
parent,
source: v1,
target: v2,
style: {
edgeStyle: 'orthogonalEdgeStyle',
},
});
const e2 = graph.insertEdge({
parent,
source: v2,
target: v1,
style: {
edgeStyle: 'orthogonalEdgeStyle',
},
});
});
return container;
};
export const Default = Template.bind({});