Skip to content

Commit 5004cec

Browse files
authored
feat: use new features of maxGraph 0.18.0 (#215)
The overall size of the all examples decreases: - use `BaseGraph` to control the default styles that are registered - use `EdgeMarker` instead of the duplicated implementation in the example In addition: - use the `CellEditorHandler` plugin that was missing in the list of declared plugins. - remove ts-ignore that are no longer generated when calling the maxGraph API
1 parent 065a0a2 commit 5004cec

File tree

2 files changed

+16
-82
lines changed

2 files changed

+16
-82
lines changed

projects/_shared/src/custom-shapes.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import {CellRenderer, EllipseShape, RectangleShape} from '@maxgraph/core';
33

44
export const registerCustomShapes = (): void => {
55
console.info('Registering custom shapes...');
6-
// TODO remove ts-ignore when maxGraph 0.18.0 is released
7-
// @ts-ignore
86
CellRenderer.registerShape('customRectangle', CustomRectangleShape);
9-
// @ts-ignore
107
CellRenderer.registerShape('customEllipse', CustomEllipseShape);
118
console.info('Custom shapes registered');
129
};

projects/_shared/src/generate-graph.ts

Lines changed: 16 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,40 @@
11
import {
2-
type AbstractCanvas2D,
2+
BaseGraph,
3+
CellEditorHandler,
34
CellRenderer,
4-
constants,
5+
EdgeMarker,
56
EdgeStyle,
67
EllipseShape,
7-
Graph,
88
InternalEvent,
99
MarkerShape,
1010
PanningHandler,
1111
Perimeter,
12-
type Point,
1312
RubberBandHandler,
1413
SelectionCellsHandler,
1514
SelectionHandler,
16-
type Shape,
17-
type StyleArrowValue,
1815
StyleRegistry,
1916
} from '@maxgraph/core';
2017
import {registerCustomShapes} from "./custom-shapes";
2118

2219

23-
// TODO remove this function when maxGraph 0.18.0 is released and import it from maxGraph instead using EdgeMarker.createArrow
24-
// It is currently duplicated from maxGraph as it is not exported in version 0.17.0
25-
const createArrow =
26-
(widthFactor: number) =>
27-
(
28-
canvas: AbstractCanvas2D,
29-
_shape: Shape,
30-
type: StyleArrowValue,
31-
pe: Point,
32-
unitX: number,
33-
unitY: number,
34-
size: number,
35-
_source: boolean,
36-
sw: number,
37-
filled: boolean
38-
) => {
39-
// The angle of the forward facing arrow sides against the x axis is
40-
// 26.565 degrees, 1/sin(26.565) = 2.236 / 2 = 1.118 ( / 2 allows for
41-
// only half the strokewidth is processed ).
42-
const endOffsetX = unitX * sw * 1.118;
43-
const endOffsetY = unitY * sw * 1.118;
44-
45-
unitX *= size + sw;
46-
unitY *= size + sw;
47-
48-
const pt = pe.clone();
49-
pt.x -= endOffsetX;
50-
pt.y -= endOffsetY;
51-
52-
const f = type !== constants.ARROW.CLASSIC && type !== constants.ARROW.CLASSIC_THIN ? 1 : 3 / 4;
53-
pe.x += -unitX * f - endOffsetX;
54-
pe.y += -unitY * f - endOffsetY;
55-
56-
return () => {
57-
canvas.begin();
58-
canvas.moveTo(pt.x, pt.y);
59-
canvas.lineTo(
60-
pt.x - unitX - unitY / widthFactor,
61-
pt.y - unitY + unitX / widthFactor
62-
);
63-
64-
if (type === constants.ARROW.CLASSIC || type === constants.ARROW.CLASSIC_THIN) {
65-
canvas.lineTo(pt.x - (unitX * 3) / 4, pt.y - (unitY * 3) / 4);
66-
}
67-
68-
canvas.lineTo(
69-
pt.x + unitY / widthFactor - unitX,
70-
pt.y - unitY - unitX / widthFactor
71-
);
72-
canvas.close();
73-
74-
if (filled) {
75-
canvas.fillAndStroke();
76-
} else {
77-
canvas.stroke();
78-
}
79-
};
80-
};
81-
8220
/**
83-
* Create a custom implementation to not load all default built-in styles. This is because Graph registers them.
84-
*
85-
* In the future, we expect to have an implementation of Graph that does not do it.
86-
* See https://github.com/maxGraph/maxGraph/issues/760
21+
* Custom implementation of {@link BaseGraph} that only register the built-in styles required by the example.
8722
*/
88-
class CustomGraph extends Graph {
23+
class CustomGraph extends BaseGraph {
8924
/**
9025
* Only registers the elements required for this example. Do not let Graph load all default built-in styles.
9126
*/
9227
protected override registerDefaults() {
9328
// Register builtin shapes
9429
// RectangleShape is not registered here because it is always available. It is the fallback shape for vertices when no shape is returned by the registry
95-
// TODO remove ts-ignore when maxGraph 0.18.0 is released
96-
// @ts-ignore
9730
CellRenderer.registerShape('ellipse', EllipseShape);
9831

9932
// Register builtin styles
10033
StyleRegistry.putValue('ellipsePerimeter', Perimeter.EllipsePerimeter);
10134
StyleRegistry.putValue('rectanglePerimeter', Perimeter.RectanglePerimeter); // declared in the default vertex style, so must be registered to be used
10235
StyleRegistry.putValue('orthogonalEdgeStyle', EdgeStyle.OrthConnector);
10336

104-
const arrowFunction = createArrow(2);
37+
const arrowFunction = EdgeMarker.createArrow(2);
10538
MarkerShape.addMarker('classic', arrowFunction);
10639
MarkerShape.addMarker('block', arrowFunction);
10740

@@ -120,12 +53,16 @@ export const initializeGraph = (container?: HTMLElement) => {
12053
// Disables the built-in context menu
12154
InternalEvent.disableContextMenu(container);
12255

123-
const graph = new CustomGraph(container, undefined, [
124-
PanningHandler, // Enables panning with the mouse
125-
RubberBandHandler, // Enables rubber band selection
126-
SelectionCellsHandler, // Enables management of selected cells
127-
SelectionHandler, // Enables selection with the mouse
128-
]);
56+
const graph = new CustomGraph({
57+
container,
58+
plugins: [
59+
CellEditorHandler, // Enables in-place editing of cell labels
60+
PanningHandler, // Enables panning with the mouse
61+
RubberBandHandler, // Enables rubber band selection
62+
SelectionCellsHandler, // Enables management of selected cells
63+
SelectionHandler, // Enables selection with the mouse
64+
],
65+
});
12966
graph.setPanning(true); // Use mouse right button for panning
13067

13168
// Customize the rubber band selection

0 commit comments

Comments
 (0)