2021-09-07 09:07:27 +00:00
|
|
|
import {
|
|
|
|
Graph,
|
|
|
|
InternalEvent,
|
2021-09-07 12:21:22 +00:00
|
|
|
RubberBand,
|
2021-09-07 09:07:27 +00:00
|
|
|
ConnectionHandler,
|
|
|
|
ConnectionConstraint,
|
|
|
|
Geometry,
|
|
|
|
Polyline,
|
|
|
|
Point,
|
|
|
|
CellState,
|
|
|
|
} from '@maxgraph/core';
|
2021-04-20 12:22:55 +00:00
|
|
|
|
2021-04-21 04:54:09 +00:00
|
|
|
import { globalTypes } from '../.storybook/preview';
|
2021-04-19 12:56:58 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
title: 'Connections/Anchors',
|
|
|
|
argTypes: {
|
2021-04-21 04:54:09 +00:00
|
|
|
...globalTypes,
|
2021-04-20 12:22:55 +00:00
|
|
|
rubberBand: {
|
|
|
|
type: 'boolean',
|
2021-08-30 14:20:26 +00:00
|
|
|
defaultValue: true,
|
|
|
|
},
|
|
|
|
},
|
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
|
|
|
if (!args.contextMenu) InternalEvent.disableContextMenu(container);
|
2021-04-19 12:56:58 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
class MyCustomConnectionHandler extends ConnectionHandler {
|
2021-04-19 12:56:58 +00:00
|
|
|
// Enables connect preview for the default edge style
|
|
|
|
createEdgeState(me) {
|
|
|
|
const edge = graph.createEdge(null, null, null, null, null);
|
2021-08-30 14:20:26 +00:00
|
|
|
return new CellState(this.graph.view, edge, this.graph.getCellStyle(edge));
|
2021-04-19 12:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
class MyCustomGraph extends Graph {
|
2021-04-19 12:56:58 +00:00
|
|
|
getAllConnectionConstraints(terminal, source) {
|
|
|
|
// Overridden to define per-shape connection points
|
|
|
|
if (terminal != null && terminal.shape != null) {
|
|
|
|
if (terminal.shape.stencil != null) {
|
|
|
|
if (terminal.shape.stencil.constraints != null) {
|
|
|
|
return terminal.shape.stencil.constraints;
|
|
|
|
}
|
|
|
|
} else if (terminal.shape.constraints != null) {
|
|
|
|
return terminal.shape.constraints;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
createConnectionHandler() {
|
|
|
|
return new MyCustomConnectionHandler(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
class MyCustomGeometryClass extends Geometry {
|
2021-04-19 12:56:58 +00:00
|
|
|
// Defines the default constraints for the vertices
|
|
|
|
constraints = [
|
2021-08-30 14:20:26 +00:00
|
|
|
new ConnectionConstraint(new Point(0.25, 0), true),
|
|
|
|
new ConnectionConstraint(new Point(0.5, 0), true),
|
|
|
|
new ConnectionConstraint(new Point(0.75, 0), true),
|
|
|
|
new ConnectionConstraint(new Point(0, 0.25), true),
|
|
|
|
new ConnectionConstraint(new Point(0, 0.5), true),
|
|
|
|
new ConnectionConstraint(new Point(0, 0.75), true),
|
|
|
|
new ConnectionConstraint(new Point(1, 0.25), true),
|
|
|
|
new ConnectionConstraint(new Point(1, 0.5), true),
|
|
|
|
new ConnectionConstraint(new Point(1, 0.75), true),
|
|
|
|
new ConnectionConstraint(new Point(0.25, 1), true),
|
|
|
|
new ConnectionConstraint(new Point(0.5, 1), true),
|
|
|
|
new ConnectionConstraint(new Point(0.75, 1), true),
|
2021-04-19 12:56:58 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edges have no connection points
|
2021-08-30 14:20:26 +00:00
|
|
|
Polyline.prototype.constraints = null;
|
2021-04-19 12:56:58 +00:00
|
|
|
|
|
|
|
// Creates the graph inside the given container
|
|
|
|
const graph = new MyCustomGraph(container);
|
|
|
|
graph.setConnectable(true);
|
|
|
|
|
|
|
|
// Specifies the default edge style
|
2021-08-30 14:20:26 +00:00
|
|
|
graph.getStylesheet().getDefaultEdgeStyle().edgeStyle = 'orthogonalEdgeStyle';
|
2021-04-19 12:56:58 +00:00
|
|
|
|
|
|
|
// Enables rubberband selection
|
2021-09-07 12:21:22 +00:00
|
|
|
if (args.rubberBand) new RubberBand(graph);
|
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: [20, 20],
|
|
|
|
size: [80, 30],
|
|
|
|
geometryClass: MyCustomGeometryClass,
|
|
|
|
});
|
|
|
|
const v2 = graph.insertVertex({
|
|
|
|
parent,
|
|
|
|
value: 'World!',
|
|
|
|
position: [200, 150],
|
|
|
|
size: [80, 30],
|
|
|
|
geometryClass: MyCustomGeometryClass,
|
|
|
|
});
|
|
|
|
const e1 = graph.insertEdge({
|
|
|
|
parent,
|
|
|
|
value: '',
|
|
|
|
position: v1,
|
|
|
|
size: v2,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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({});
|