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.
|
|
|
|
*/
|
|
|
|
|
2021-09-07 09:07:27 +00:00
|
|
|
import {
|
|
|
|
Graph,
|
|
|
|
EdgeHandler,
|
2022-01-08 01:49:35 +00:00
|
|
|
SelectionHandler,
|
2021-09-07 09:07:27 +00:00
|
|
|
CellRenderer,
|
2021-09-12 14:39:02 +00:00
|
|
|
MarkerShape,
|
2021-09-07 09:07:27 +00:00
|
|
|
CylinderShape,
|
2021-09-12 14:39:02 +00:00
|
|
|
ArrowShape,
|
2021-09-07 09:07:27 +00:00
|
|
|
Point,
|
|
|
|
} from '@maxgraph/core';
|
2021-04-21 11:36:38 +00:00
|
|
|
|
|
|
|
import { globalTypes } from '../.storybook/preview';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
title: 'Icon_Images/Markers',
|
|
|
|
argTypes: {
|
2021-08-30 14:20:26 +00:00
|
|
|
...globalTypes,
|
|
|
|
},
|
2021-04-21 11:36:38 +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';
|
|
|
|
|
|
|
|
// Enables guides
|
2022-01-08 01:49:35 +00:00
|
|
|
SelectionHandler.prototype.guidesEnabled = true;
|
2021-08-30 14:20:26 +00:00
|
|
|
EdgeHandler.prototype.snapToTerminals = true;
|
2021-04-21 11:36:38 +00:00
|
|
|
|
|
|
|
// Registers and defines the custom marker
|
2021-09-12 14:39:02 +00:00
|
|
|
MarkerShape.addMarker(
|
2021-08-30 14:20:26 +00:00
|
|
|
'dash',
|
|
|
|
function (canvas, shape, type, pe, unitX, unitY, size, source, sw, filled) {
|
|
|
|
const nx = unitX * (size + sw + 1);
|
|
|
|
const ny = unitY * (size + sw + 1);
|
|
|
|
|
|
|
|
return function () {
|
|
|
|
canvas.begin();
|
|
|
|
canvas.moveTo(pe.x - nx / 2 - ny / 2, pe.y - ny / 2 + nx / 2);
|
|
|
|
canvas.lineTo(pe.x + ny / 2 - (3 * nx) / 2, pe.y - (3 * ny) / 2 - nx / 2);
|
|
|
|
canvas.stroke();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
2021-04-21 11:36:38 +00:00
|
|
|
|
|
|
|
// Defines custom message shape
|
2021-08-30 14:20:26 +00:00
|
|
|
class MessageShape extends CylinderShape {
|
2021-04-21 11:36:38 +00:00
|
|
|
redrawPath(path, x, y, w, h, isForeground) {
|
|
|
|
if (isForeground) {
|
|
|
|
path.moveTo(0, 0);
|
|
|
|
path.lineTo(w / 2, h / 2);
|
|
|
|
path.lineTo(w, 0);
|
|
|
|
} else {
|
|
|
|
path.moveTo(0, 0);
|
|
|
|
path.lineTo(w, 0);
|
|
|
|
path.lineTo(w, h);
|
|
|
|
path.lineTo(0, h);
|
|
|
|
path.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-30 14:20:26 +00:00
|
|
|
CellRenderer.registerShape('message', MessageShape);
|
2021-04-21 11:36:38 +00:00
|
|
|
|
|
|
|
// Defines custom edge shape
|
2021-09-12 14:39:02 +00:00
|
|
|
class LinkShape extends ArrowShape {
|
2021-04-21 11:36:38 +00:00
|
|
|
paintEdgeShape(c, pts) {
|
|
|
|
const width = 10;
|
|
|
|
|
|
|
|
// Base vector (between end points)
|
|
|
|
const p0 = pts[0];
|
|
|
|
const pe = pts[pts.length - 1];
|
|
|
|
|
|
|
|
const dx = pe.x - p0.x;
|
|
|
|
const dy = pe.y - p0.y;
|
|
|
|
const dist = Math.sqrt(dx * dx + dy * dy);
|
|
|
|
const length = dist;
|
|
|
|
|
|
|
|
// Computes the norm and the inverse norm
|
|
|
|
const nx = dx / dist;
|
|
|
|
const ny = dy / dist;
|
|
|
|
const basex = length * nx;
|
|
|
|
const basey = length * ny;
|
|
|
|
const floorx = (width * ny) / 3;
|
|
|
|
const floory = (-width * nx) / 3;
|
|
|
|
|
|
|
|
// Computes points
|
|
|
|
const p0x = p0.x - floorx / 2;
|
|
|
|
const p0y = p0.y - floory / 2;
|
|
|
|
const p1x = p0x + floorx;
|
|
|
|
const p1y = p0y + floory;
|
|
|
|
const p2x = p1x + basex;
|
|
|
|
const p2y = p1y + basey;
|
|
|
|
const p3x = p2x + floorx;
|
|
|
|
const p3y = p2y + floory;
|
|
|
|
// p4 not necessary
|
|
|
|
const p5x = p3x - 3 * floorx;
|
|
|
|
const p5y = p3y - 3 * floory;
|
|
|
|
|
|
|
|
c.begin();
|
|
|
|
c.moveTo(p1x, p1y);
|
|
|
|
c.lineTo(p2x, p2y);
|
|
|
|
c.moveTo(p5x + floorx, p5y + floory);
|
|
|
|
c.lineTo(p0x, p0y);
|
|
|
|
c.stroke();
|
|
|
|
}
|
|
|
|
}
|
2021-08-30 14:20:26 +00:00
|
|
|
CellRenderer.registerShape('link', LinkShape);
|
2021-04-21 11:36:38 +00:00
|
|
|
|
|
|
|
// Creates the graph
|
2021-08-30 14:20:26 +00:00
|
|
|
const graph = new Graph(container);
|
2021-04-21 11:36:38 +00:00
|
|
|
|
|
|
|
// Sets default styles
|
|
|
|
let style = graph.getStylesheet().getDefaultVertexStyle();
|
|
|
|
style.fillColor = '#FFFFFF';
|
|
|
|
style.strokeColor = '#000000';
|
|
|
|
style.fontColor = '#000000';
|
2023-01-06 15:53:54 +00:00
|
|
|
style.fontStyle = 1;
|
2021-04-21 11:36:38 +00:00
|
|
|
|
|
|
|
style = graph.getStylesheet().getDefaultEdgeStyle();
|
|
|
|
style.strokeColor = '#000000';
|
|
|
|
style.fontColor = '#000000';
|
2023-01-06 15:53:54 +00:00
|
|
|
style.fontStyle = 0;
|
|
|
|
style.startSize = 8;
|
|
|
|
style.endSize = 8;
|
2021-04-21 11:36:38 +00:00
|
|
|
|
|
|
|
// Populates the graph
|
|
|
|
const parent = graph.getDefaultParent();
|
|
|
|
|
2022-01-08 01:49:35 +00:00
|
|
|
graph.batchUpdate(() => {
|
2021-04-21 11:36:38 +00:00
|
|
|
const v1 = graph.insertVertex(parent, null, 'v1', 20, 20, 80, 30);
|
|
|
|
const v2 = graph.insertVertex(parent, null, 'v2', 440, 20, 80, 30);
|
2022-04-17 06:58:35 +00:00
|
|
|
const e1 = graph.insertEdge(parent, null, '', v1, v2, {
|
|
|
|
dashed: 1,
|
|
|
|
startArrow: 'oval',
|
|
|
|
endArrow: 'block',
|
|
|
|
sourcePerimeterSpacing: 4,
|
2023-01-06 15:53:54 +00:00
|
|
|
startFill: false,
|
|
|
|
endFill: false,
|
2022-04-17 06:58:35 +00:00
|
|
|
});
|
|
|
|
const e11 = graph.insertVertex(e1, null, 'Label', 0, 0, 20, 14, {
|
|
|
|
shape: 'message',
|
|
|
|
labelBackgroundColor: '#ffffff',
|
|
|
|
labelPosition: 'left',
|
|
|
|
spacingRight: 2,
|
|
|
|
align: 'right',
|
|
|
|
fontStyle: 0,
|
|
|
|
});
|
2021-08-30 14:20:26 +00:00
|
|
|
e11.geometry.offset = new Point(-10, -7);
|
2021-04-21 11:36:38 +00:00
|
|
|
e11.geometry.relative = true;
|
|
|
|
e11.connectable = false;
|
|
|
|
|
|
|
|
const v3 = graph.insertVertex(parent, null, 'v3', 20, 120, 80, 30);
|
|
|
|
const v4 = graph.insertVertex(parent, null, 'v4', 440, 120, 80, 30);
|
2022-04-17 06:58:35 +00:00
|
|
|
const e2 = graph.insertEdge(parent, null, 'Label', v3, v4, {
|
|
|
|
startArrow: 'dash',
|
|
|
|
startSize: 12,
|
|
|
|
endArrow: 'block',
|
|
|
|
labelBackgroundColor: '#FFFFFF',
|
|
|
|
});
|
|
|
|
|
|
|
|
const v5 = graph.insertVertex(parent, null, 'v5', 40, 220, 40, 40, {
|
|
|
|
shape: 'ellipse',
|
|
|
|
perimeter: 'ellipsePerimeter',
|
|
|
|
});
|
|
|
|
const v6 = graph.insertVertex(parent, null, 'v6', 460, 220, 40, 40, {
|
|
|
|
shape: 'doubleEllipse',
|
|
|
|
perimeter: 'ellipsePerimeter',
|
|
|
|
});
|
|
|
|
const e3 = graph.insertEdge(parent, null, 'Link', v5, v6, {
|
|
|
|
shape: 'link',
|
|
|
|
labelBackgroundColor: '#FFFFFF',
|
|
|
|
});
|
2022-01-08 01:49:35 +00:00
|
|
|
});
|
2021-04-21 11:36:38 +00:00
|
|
|
|
|
|
|
return container;
|
2021-08-30 14:20:26 +00:00
|
|
|
};
|
2021-04-21 11:36:38 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
export const Default = Template.bind({});
|