2022-04-17 06:58:35 +00:00
|
|
|
import {
|
|
|
|
Graph,
|
|
|
|
RubberBandHandler,
|
|
|
|
KeyHandler,
|
|
|
|
constants,
|
|
|
|
Rectangle,
|
|
|
|
} from '@maxgraph/core';
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
import { globalTypes } from '../.storybook/preview';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
title: 'Labels/Labels',
|
|
|
|
argTypes: {
|
|
|
|
...globalTypes,
|
|
|
|
rubberBand: {
|
|
|
|
type: 'boolean',
|
2021-08-30 14:20:26 +00:00
|
|
|
defaultValue: true,
|
|
|
|
},
|
|
|
|
},
|
2021-04-24 12:30:27 +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';
|
|
|
|
|
|
|
|
// Creates the graph inside the given container
|
2021-08-30 14:20:26 +00:00
|
|
|
const graph = new Graph(container);
|
2021-04-24 12:30:27 +00:00
|
|
|
graph.setTooltips(true);
|
|
|
|
graph.htmlLabels = true;
|
|
|
|
graph.vertexLabelsMovable = true;
|
|
|
|
|
2022-01-08 01:49:35 +00:00
|
|
|
if (args.rubberBand) new RubberBandHandler(graph);
|
2021-04-24 12:30:27 +00:00
|
|
|
|
2022-01-08 01:49:35 +00:00
|
|
|
new KeyHandler(graph);
|
2021-04-24 12:30:27 +00:00
|
|
|
|
2022-01-08 01:49:35 +00:00
|
|
|
const graphHandler = graph.getPlugin('SelectionHandler');
|
2021-09-10 05:17:59 +00:00
|
|
|
|
2021-04-24 12:30:27 +00:00
|
|
|
// Do not allow removing labels from parents
|
2021-09-10 05:17:59 +00:00
|
|
|
graphHandler.removeCellsFromParent = false;
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
// Autosize labels on insert where autosize=1
|
|
|
|
graph.autoSizeCellsOnAdd = true;
|
|
|
|
|
|
|
|
// Allows moving of relative cells
|
2021-08-30 14:20:26 +00:00
|
|
|
graph.isCellLocked = function (cell) {
|
2021-04-24 12:30:27 +00:00
|
|
|
return this.isCellsLocked();
|
|
|
|
};
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
graph.isCellResizable = function (cell) {
|
2021-04-25 03:39:40 +00:00
|
|
|
const geo = cell.getGeometry();
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
return geo == null || !geo.relative;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Truncates the label to the size of the vertex
|
2021-08-30 14:20:26 +00:00
|
|
|
graph.getLabel = function (cell) {
|
2021-04-24 12:30:27 +00:00
|
|
|
const label = this.labelsVisible ? this.convertValueToString(cell) : '';
|
2021-04-25 03:39:40 +00:00
|
|
|
const geometry = cell.getGeometry();
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
if (
|
2021-04-25 03:39:40 +00:00
|
|
|
!cell.isCollapsed() &&
|
2021-04-24 12:30:27 +00:00
|
|
|
geometry != null &&
|
2021-08-30 14:20:26 +00:00
|
|
|
(geometry.offset == null || (geometry.offset.x == 0 && geometry.offset.y == 0)) &&
|
2021-04-25 03:39:40 +00:00
|
|
|
cell.isVertex() &&
|
2021-04-24 12:30:27 +00:00
|
|
|
geometry.width >= 2
|
|
|
|
) {
|
|
|
|
const style = this.getCellStyle(cell);
|
2022-01-08 01:49:35 +00:00
|
|
|
const fontSize = style.fontSize || constants.DEFAULT_FONTSIZE;
|
2021-04-24 12:30:27 +00:00
|
|
|
const max = geometry.width / (fontSize * 0.625);
|
|
|
|
|
|
|
|
if (max < label.length) {
|
|
|
|
return `${label.substring(0, max)}...`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return label;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Enables wrapping for vertex labels
|
2021-08-30 14:20:26 +00:00
|
|
|
graph.isWrapping = function (cell) {
|
2021-04-25 03:39:40 +00:00
|
|
|
return cell.isCollapsed();
|
2021-04-24 12:30:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Enables clipping of vertex labels if no offset is defined
|
2021-08-30 14:20:26 +00:00
|
|
|
graph.isLabelClipped = function (cell) {
|
2021-04-25 03:39:40 +00:00
|
|
|
const geometry = cell.getGeometry();
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
geometry != null &&
|
|
|
|
!geometry.relative &&
|
2021-08-30 14:20:26 +00:00
|
|
|
(geometry.offset == null || (geometry.offset.x == 0 && geometry.offset.y == 0))
|
2021-04-24 12:30:27 +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
|
2022-01-08 01:49:35 +00:00
|
|
|
graph.batchUpdate(() => {
|
2021-08-30 14:20:26 +00:00
|
|
|
const v1 = graph.insertVertex(parent, null, 'vertexLabelsMovable', 20, 20, 80, 30);
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
// Places sublabels inside the vertex
|
2022-04-17 06:58:35 +00:00
|
|
|
const label11 = graph.insertVertex(
|
|
|
|
v1,
|
|
|
|
null,
|
|
|
|
'Label1',
|
|
|
|
0.5,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
2022-05-08 09:05:22 +00:00
|
|
|
{ baseStyleNames: [] },
|
2022-04-17 06:58:35 +00:00
|
|
|
true
|
|
|
|
);
|
|
|
|
const label12 = graph.insertVertex(
|
|
|
|
v1,
|
|
|
|
null,
|
|
|
|
'Label2',
|
|
|
|
0.5,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
2022-05-08 09:05:22 +00:00
|
|
|
{ baseStyleNames: [] },
|
2022-04-17 06:58:35 +00:00
|
|
|
true
|
|
|
|
);
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
const v2 = graph.insertVertex(
|
|
|
|
parent,
|
|
|
|
null,
|
|
|
|
'Wrapping and clipping is enabled only if the cell is collapsed, otherwise the label is truncated if there is no manual offset.',
|
|
|
|
200,
|
|
|
|
150,
|
|
|
|
80,
|
|
|
|
30
|
|
|
|
);
|
2021-08-30 14:20:26 +00:00
|
|
|
v2.geometry.alternateBounds = new Rectangle(0, 0, 80, 30);
|
2021-04-24 12:30:27 +00:00
|
|
|
const e1 = graph.insertEdge(parent, null, 'edgeLabelsMovable', v1, v2);
|
|
|
|
|
|
|
|
// Places sublabels inside the vertex
|
2022-04-17 06:58:35 +00:00
|
|
|
const label21 = graph.insertVertex(
|
|
|
|
v2,
|
|
|
|
null,
|
|
|
|
'Label1',
|
|
|
|
0.5,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
2022-05-08 09:05:22 +00:00
|
|
|
{ baseStyleNames: [] },
|
2022-04-17 06:58:35 +00:00
|
|
|
true
|
|
|
|
);
|
|
|
|
const label22 = graph.insertVertex(
|
|
|
|
v2,
|
|
|
|
null,
|
|
|
|
'Label2',
|
|
|
|
0.5,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
2022-05-08 09:05:22 +00:00
|
|
|
{ baseStyleNames: [] },
|
2022-04-17 06:58:35 +00:00
|
|
|
true
|
|
|
|
);
|
2022-01-08 01:49:35 +00:00
|
|
|
});
|
2021-04-24 12:30:27 +00:00
|
|
|
|
|
|
|
return container;
|
2021-08-30 14:20:26 +00:00
|
|
|
};
|
2021-04-24 12:30:27 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
export const Default = Template.bind({});
|