2022-08-30 15:36:33 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021-present The maxGraph project Contributors
|
|
|
|
Copyright (c) 2006-2020, JGraph Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-01-06 13:16:46 +00:00
|
|
|
import { Graph, styleUtils } from '@maxgraph/core';
|
2021-04-19 12:56:58 +00:00
|
|
|
|
2021-04-21 04:54:09 +00:00
|
|
|
import { globalTypes } from '../.storybook/preview';
|
2021-04-20 12:22:55 +00:00
|
|
|
|
2021-04-19 12:56:58 +00:00
|
|
|
export default {
|
|
|
|
title: 'Connections/EdgeTolerance',
|
|
|
|
argTypes: {
|
2021-08-30 14:20:26 +00:00
|
|
|
...globalTypes,
|
|
|
|
},
|
2021-04-19 12:56:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
class MyCustomGraph extends Graph {
|
2021-04-19 12:56:58 +00:00
|
|
|
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) {
|
2023-01-06 13:16:46 +00:00
|
|
|
const pt = styleUtils.convertPoint(container, me.getX(), me.getY());
|
2021-04-19 12:56:58 +00:00
|
|
|
|
|
|
|
me.graphX = pt.x;
|
|
|
|
me.graphY = pt.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
const cell = this.getCellAt(me.graphX, me.graphY);
|
2021-08-30 14:20:26 +00:00
|
|
|
|
2021-04-25 03:39:40 +00:00
|
|
|
if (cell?.isEdge()) {
|
2021-04-19 12:56:58 +00:00
|
|
|
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) {
|
2023-01-06 13:16:46 +00:00
|
|
|
const pt = styleUtils.convertPoint(
|
2021-04-19 12:56:58 +00:00
|
|
|
el,
|
2022-01-08 01:49:35 +00:00
|
|
|
eventUtils.getClientX(evt),
|
|
|
|
eventUtils.getClientY(evt)
|
2021-04-19 12:56:58 +00:00
|
|
|
);
|
|
|
|
cell = this.getCellAt(pt.x, pt.y);
|
|
|
|
}
|
|
|
|
super.dblClick(evt, cell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates the graph inside the given container
|
|
|
|
const graph = new MyCustomGraph(container);
|
2021-09-10 05:17:59 +00:00
|
|
|
graph.setEventTolerance(20);
|
2021-04-19 12:56:58 +00:00
|
|
|
|
|
|
|
// 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,
|
2022-04-17 06:58:35 +00:00
|
|
|
style: {
|
|
|
|
edgeStyle: 'orthogonalEdgeStyle',
|
|
|
|
},
|
2021-04-19 12:56:58 +00:00
|
|
|
});
|
|
|
|
const e2 = graph.insertEdge({
|
|
|
|
parent,
|
|
|
|
source: v2,
|
|
|
|
target: v1,
|
2022-04-17 06:58:35 +00:00
|
|
|
style: {
|
|
|
|
edgeStyle: 'orthogonalEdgeStyle',
|
|
|
|
},
|
2021-04-19 12:56:58 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return container;
|
2021-08-30 14:20:26 +00:00
|
|
|
};
|
2021-04-19 12:56:58 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
export const Default = Template.bind({});
|