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,
|
2022-01-08 01:49:35 +00:00
|
|
|
SelectionHandler,
|
2022-10-04 08:47:03 +00:00
|
|
|
eventUtils,
|
2021-09-07 09:07:27 +00:00
|
|
|
EdgeHandler,
|
|
|
|
EdgeStyle,
|
2022-01-08 01:49:35 +00:00
|
|
|
RubberBandHandler,
|
2021-09-07 09:07:27 +00:00
|
|
|
} from '@maxgraph/core';
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
import { globalTypes } from '../.storybook/preview';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
title: 'Misc/Guides',
|
|
|
|
argTypes: {
|
|
|
|
...globalTypes,
|
|
|
|
rubberBand: {
|
|
|
|
type: 'boolean',
|
2021-08-30 14:20:26 +00:00
|
|
|
defaultValue: true,
|
|
|
|
},
|
|
|
|
},
|
2021-04-25 03:39:40 +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';
|
|
|
|
|
|
|
|
// Alt disables guides
|
2022-01-08 01:49:35 +00:00
|
|
|
SelectionHandler.prototype.useGuidesForEvent = function (me) {
|
2022-10-04 08:47:03 +00:00
|
|
|
return !eventUtils.isAltDown(me.getEvent());
|
2021-04-25 03:39:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Defines the guides to be red (default)
|
2022-01-08 01:49:35 +00:00
|
|
|
// constants.GUIDE_COLOR = '#FF0000';
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Defines the guides to be 1 pixel (default)
|
2022-01-08 01:49:35 +00:00
|
|
|
// constants.GUIDE_STROKEWIDTH = 1;
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Enables snapping waypoints to terminals
|
2021-08-30 14:20:26 +00:00
|
|
|
EdgeHandler.prototype.snapToTerminals = true;
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Creates the graph inside the given container
|
2021-08-30 14:20:26 +00:00
|
|
|
const graph = new Graph(container);
|
2021-04-25 03:39:40 +00:00
|
|
|
graph.setConnectable(true);
|
|
|
|
graph.gridSize = 30;
|
|
|
|
|
2022-10-04 08:47:03 +00:00
|
|
|
// Enables guides
|
|
|
|
const selectionHandler = graph.getPlugin('SelectionHandler');
|
|
|
|
if (selectionHandler)
|
|
|
|
selectionHandler.guidesEnabled = true;
|
|
|
|
|
2021-04-25 03:39:40 +00:00
|
|
|
// Changes the default style for edges "in-place" and assigns
|
2021-08-30 14:20:26 +00:00
|
|
|
// an alternate edge style which is applied in Graph.flip
|
2021-04-25 03:39:40 +00:00
|
|
|
// when the user double clicks on the adjustment control point
|
|
|
|
// of the edge. The ElbowConnector edge style switches to TopToBottom
|
|
|
|
// if the horizontal style is true.
|
|
|
|
const style = graph.getStylesheet().getDefaultEdgeStyle();
|
2021-05-02 06:04:34 +00:00
|
|
|
style.rounded = true;
|
2021-08-30 14:20:26 +00:00
|
|
|
style.edge = EdgeStyle.ElbowConnector;
|
2021-04-25 03:39:40 +00:00
|
|
|
graph.alternateEdgeStyle = 'elbow=vertical';
|
|
|
|
|
|
|
|
// Enables rubberband selection
|
2022-01-08 01:49:35 +00:00
|
|
|
if (args.rubberBand) new RubberBandHandler(graph);
|
2021-04-25 03:39:40 +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(() => {
|
|
|
|
let v1;
|
2021-04-25 03:39:40 +00:00
|
|
|
v1 = graph.insertVertex(parent, null, 'Hello,', 20, 40, 80, 70);
|
|
|
|
const v2 = graph.insertVertex(parent, null, 'World!', 200, 140, 80, 40);
|
|
|
|
const e1 = graph.insertEdge(parent, null, '', v1, v2);
|
2022-01-08 01:49:35 +00:00
|
|
|
});
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Handles cursor keys
|
2021-08-30 14:20:26 +00:00
|
|
|
const nudge = function (keyCode) {
|
2021-04-25 03:39:40 +00:00
|
|
|
if (!graph.isSelectionEmpty()) {
|
|
|
|
let dx = 0;
|
|
|
|
let dy = 0;
|
|
|
|
|
|
|
|
if (keyCode === 37) {
|
|
|
|
dx = -1;
|
|
|
|
} else if (keyCode === 38) {
|
|
|
|
dy = -1;
|
|
|
|
} else if (keyCode === 39) {
|
|
|
|
dx = 1;
|
|
|
|
} else if (keyCode === 40) {
|
|
|
|
dy = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
graph.moveCells(graph.getSelectionCells(), dx, dy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transfer initial focus to graph container for keystroke handling
|
|
|
|
graph.container.focus();
|
|
|
|
|
|
|
|
// Handles keystroke events
|
2022-01-08 01:49:35 +00:00
|
|
|
const keyHandler = new KeyHandler(graph);
|
2021-04-25 03:39:40 +00:00
|
|
|
|
|
|
|
// Ignores enter keystroke. Remove this line if you want the
|
|
|
|
// enter keystroke to stop editing
|
2021-08-30 14:20:26 +00:00
|
|
|
keyHandler.enter = function () {};
|
2021-04-25 03:39:40 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
keyHandler.bindKey(37, function () {
|
2021-04-25 03:39:40 +00:00
|
|
|
nudge(37);
|
|
|
|
});
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
keyHandler.bindKey(38, function () {
|
2021-04-25 03:39:40 +00:00
|
|
|
nudge(38);
|
|
|
|
});
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
keyHandler.bindKey(39, function () {
|
2021-04-25 03:39:40 +00:00
|
|
|
nudge(39);
|
|
|
|
});
|
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
keyHandler.bindKey(40, function () {
|
2021-04-25 03:39:40 +00:00
|
|
|
nudge(40);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return container;
|
2021-08-30 14:20:26 +00:00
|
|
|
};
|
2021-04-25 03:39:40 +00:00
|
|
|
|
2021-08-30 14:20:26 +00:00
|
|
|
export const Default = Template.bind({});
|