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

149 lines
4.3 KiB
JavaScript
Raw Normal View History

/*
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 {
chore: improve the configuration to build the npm package (#87) Root and all package.json files - Fix license, version and authors - Remove the non-relevant .npmignore file at the project. It was designed for mxgraph, the whole directory structure changed, and we don't plan to publish the root package `core` package - Generate type definition and javascript files for ESM output - dedicated folder for `esm` and `types` in the `dist` folder to clearly separate concerns - exported in the package.json - Add npm script for `npm pack` configuration - Drop the `postinstall` npm script that built the `core` bundle. It would have been run when the package is installed in dependent projects at dependencies install. And it would have failed as it tries to build the project from TypeScript sources that are not available in the npm package. fix: ConstraintHandler now loads image as everywhere else in the code using `Client.imageBasePath` Using import doesn't work as the image is not available in the npm package. It generated error like this in ViteJS project: Failed to resolve import "../../../images/point.gif" from "../core/dist/esm/view/handler/ConstraintHandler.js". Does the file exist? This gif import had been introduced to make `Anchors.stories.js` retrieve the gif file. Configuring `imageBasePath` as already done in other stories fixes the issue. Don't export EditorCodec and StylesheetCodec as they generate errors. This is a temporary solution to avoid error in dependent project (at least, with ViteJS projects).
2022-07-05 06:08:25 +00:00
Client,
2021-09-07 09:07:27 +00:00
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 }) => {
chore: improve the configuration to build the npm package (#87) Root and all package.json files - Fix license, version and authors - Remove the non-relevant .npmignore file at the project. It was designed for mxgraph, the whole directory structure changed, and we don't plan to publish the root package `core` package - Generate type definition and javascript files for ESM output - dedicated folder for `esm` and `types` in the `dist` folder to clearly separate concerns - exported in the package.json - Add npm script for `npm pack` configuration - Drop the `postinstall` npm script that built the `core` bundle. It would have been run when the package is installed in dependent projects at dependencies install. And it would have failed as it tries to build the project from TypeScript sources that are not available in the npm package. fix: ConstraintHandler now loads image as everywhere else in the code using `Client.imageBasePath` Using import doesn't work as the image is not available in the npm package. It generated error like this in ViteJS project: Failed to resolve import "../../../images/point.gif" from "../core/dist/esm/view/handler/ConstraintHandler.js". Does the file exist? This gif import had been introduced to make `Anchors.stories.js` retrieve the gif file. Configuring `imageBasePath` as already done in other stories fixes the issue. Don't export EditorCodec and StylesheetCodec as they generate errors. This is a temporary solution to avoid error in dependent project (at least, with ViteJS projects).
2022-07-05 06:08:25 +00:00
Client.setImageBasePath('/images');
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) {
2022-01-22 03:54:58 +00:00
// Overridden to define per-geometry connection points
if (terminal && terminal.cell) {
if (terminal.shape.stencil) {
if (terminal.shape.stencil.constraints) {
return terminal.shape.stencil.constraints;
}
2022-01-22 03:54:58 +00:00
} else if (terminal.cell.geometry.constraints) {
return terminal.cell.geometry.constraints;
}
}
2022-01-22 03:54:58 +00:00
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: '',
2022-01-22 03:54:58 +00:00
source: v1,
target: v2,
});
});
return container;
};
export const Default = Template.bind({});