maxGraph/packages/html/stories/Anchors.stories.js

128 lines
3.7 KiB
JavaScript
Raw Normal View History

2021-09-07 09:07:27 +00:00
import {
Graph,
InternalEvent,
Finish converting core to ts, JSDoc conversion, consistency+convention changes, example bugfixes (#70) * reorganised directories; removed mx prefix * reduced directory hierarchies; removed mx prefix; type fixes * convert remaining javascript to ts * fix/add types * add type defs * type updates; moved codecs to where they're used * reorganise constants into enums+type additions * removed "Function:" and "Variable:" prefixes from comments, as they aren't needed in JSDoc * removed "Function:", "Variable:" and "Class:" prefixes from comments, as they aren't needed in JSDoc * removed "Function:" prefixes from comments, as they aren't needed in JSDoc * minor changes * convert code example blocks to markdown * module casing updates * converted parameter function documentation to JSDoc * documentation+type updates * removed react subdir (for now) * reorganised various `utils` functions into different files * type updates/bugfixes/workarounds * rename Rubberband and CellEditor to be *Handler to match the other plugins * move codec classes to where they're used to reduce cyclic dependencies * move codec classes to where they're used to reduce cyclic dependencies * type updates/reorganize layout file structure * renamed various files for consistency * import fixes * renamed GraphHandler SelectionHander and various fixes * convert EventObject parameters to objects * add basic better-docs config * update better-docs config * bugfix for shared variables in Graph persisting across instances * fixed accessing handlers in examples; renamed Model to GraphModel * fixed accessing handlers in examples; renamed Model to GraphModel * restored selection model * bugfix * renamed getModel to getDataModel * changed to use graph.batchUpdate() to reduce lines of code * changed to use graph.batchUpdate() to reduce lines of code * finished annotations+added TypeDoc * convert remaining Cell[] instances to CellArray * convert NaturalDocs links to JSDoc
2022-01-08 01:49:35 +00:00
RubberBandHandler,
2021-09-07 09:07:27 +00:00
ConnectionHandler,
ConnectionConstraint,
Geometry,
2021-09-12 14:39:02 +00:00
PolylineShape,
2021-09-07 09:07:27 +00:00
Point,
CellState,
} from '@maxgraph/core';
2021-04-21 04:54:09 +00:00
import { globalTypes } from '../.storybook/preview';
export default {
title: 'Connections/Anchors',
argTypes: {
2021-04-21 04:54:09 +00:00
...globalTypes,
rubberBand: {
type: 'boolean',
defaultValue: true,
},
},
};
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';
if (!args.contextMenu) InternalEvent.disableContextMenu(container);
class MyCustomConnectionHandler extends ConnectionHandler {
// Enables connect preview for the default edge style
createEdgeState(me) {
const edge = graph.createEdge(null, null, null, null, null);
return new CellState(this.graph.view, edge, this.graph.getCellStyle(edge));
}
}
class MyCustomGraph extends Graph {
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);
}
}
class MyCustomGeometryClass extends Geometry {
// Defines the default constraints for the vertices
constraints = [
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),
];
}
// Edges have no connection points
2021-09-12 14:39:02 +00:00
PolylineShape.prototype.constraints = null;
// Creates the graph inside the given container
const graph = new MyCustomGraph(container);
graph.setConnectable(true);
// Specifies the default edge style
graph.getStylesheet().getDefaultEdgeStyle().edgeStyle = 'orthogonalEdgeStyle';
// Enables rubberband selection
Finish converting core to ts, JSDoc conversion, consistency+convention changes, example bugfixes (#70) * reorganised directories; removed mx prefix * reduced directory hierarchies; removed mx prefix; type fixes * convert remaining javascript to ts * fix/add types * add type defs * type updates; moved codecs to where they're used * reorganise constants into enums+type additions * removed "Function:" and "Variable:" prefixes from comments, as they aren't needed in JSDoc * removed "Function:", "Variable:" and "Class:" prefixes from comments, as they aren't needed in JSDoc * removed "Function:" prefixes from comments, as they aren't needed in JSDoc * minor changes * convert code example blocks to markdown * module casing updates * converted parameter function documentation to JSDoc * documentation+type updates * removed react subdir (for now) * reorganised various `utils` functions into different files * type updates/bugfixes/workarounds * rename Rubberband and CellEditor to be *Handler to match the other plugins * move codec classes to where they're used to reduce cyclic dependencies * move codec classes to where they're used to reduce cyclic dependencies * type updates/reorganize layout file structure * renamed various files for consistency * import fixes * renamed GraphHandler SelectionHander and various fixes * convert EventObject parameters to objects * add basic better-docs config * update better-docs config * bugfix for shared variables in Graph persisting across instances * fixed accessing handlers in examples; renamed Model to GraphModel * fixed accessing handlers in examples; renamed Model to GraphModel * restored selection model * bugfix * renamed getModel to getDataModel * changed to use graph.batchUpdate() to reduce lines of code * changed to use graph.batchUpdate() to reduce lines of code * finished annotations+added TypeDoc * convert remaining Cell[] instances to CellArray * convert NaturalDocs links to JSDoc
2022-01-08 01:49:35 +00:00
if (args.rubberBand) new RubberBandHandler(graph);
// 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;
};
export const Default = Template.bind({});