Skip to content

Commit cebb604

Browse files
committed
refactor: simplify registration of style definitions
Use functions instead of classes. bpmn-visualization doesn't provide extension points for the registration, so using internal functions is simpler than having classes with a single method.
1 parent 8624472 commit cebb604

4 files changed

Lines changed: 37 additions & 77 deletions

File tree

docs/contributors/mxgraph-integration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ The [style](https://jgraph.github.io/mxgraph/docs/js-api/files/view/mxStylesheet
4444
is defined and registered in `StyleConfigurator`. In particular, it refers to the name of a [mxShape](https://jgraph.github.io/mxgraph/docs/js-api/files/shape/mxShape-js.html)
4545
used for the rendering.
4646

47-
The `mxShape` can be a standard `mxGraph` class or a custom BPMN `mxShape` defined by the `bpmn-visualization`. The custom `mxShapes` are registered by [ShapeConfigurator](../../src/component/mxgraph/config/ShapeConfigurator.ts).
47+
The `mxShape` can be a standard `mxGraph` class or a custom BPMN `mxShape` defined by the `bpmn-visualization`. The custom `mxShapes` are registered by [registerShapes()](../../src/component/mxgraph/config/register-style-definitions.ts).
4848
which associates the `mxShape` name (used in style definition) with the `mxShape` class to be used.
4949

5050
For more details, see [BPMN Support - How To](./bpmn-support-how-to.md).

src/component/mxgraph/GraphConfigurator.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ limitations under the License.
1515
*/
1616

1717
import { BpmnGraph } from './BpmnGraph';
18-
import MarkerConfigurator from './config/MarkerConfigurator';
19-
import ShapeConfigurator from './config/ShapeConfigurator';
18+
import { registerEdgeMarkers, registerShapes } from './config/register-style-definitions';
2019
import { StyleConfigurator } from './config/StyleConfigurator';
2120

2221
/**
@@ -37,8 +36,8 @@ export default class GraphConfigurator {
3736
configure(): BpmnGraph {
3837
this.configureGraph();
3938
new StyleConfigurator(this.graph).configureStyles();
40-
new ShapeConfigurator().configureShapes();
41-
new MarkerConfigurator().configureMarkers();
39+
registerShapes();
40+
registerEdgeMarkers();
4241
return this.graph;
4342
}
4443

src/component/mxgraph/config/MarkerConfigurator.ts

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/component/mxgraph/config/ShapeConfigurator.ts renamed to src/component/mxgraph/config/register-style-definitions.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2020 Bonitasoft S.A.
2+
Copyright 2025 Bonitasoft S.A.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -14,10 +14,10 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import type { mxShape } from 'mxgraph';
17+
import type { mxAbstractCanvas2D, mxCell, mxPoint, mxShape } from 'mxgraph';
1818

1919
import { ShapeBpmnElementKind } from '../../../model/bpmn/internal';
20-
import { mxCellRenderer } from '../initializer';
20+
import { mxCellRenderer, mxgraph } from '../initializer';
2121
import {
2222
BusinessRuleTaskShape,
2323
CallActivityShape,
@@ -35,9 +35,9 @@ import { EndEventShape, EventShape, IntermediateEventShape, ThrowIntermediateEve
3535
import { MessageFlowIconShape } from '../shape/flow-shapes';
3636
import { ComplexGatewayShape, EventBasedGatewayShape, ExclusiveGatewayShape, InclusiveGatewayShape, ParallelGatewayShape } from '../shape/gateway-shapes';
3737
import { TextAnnotationShape } from '../shape/text-annotation-shapes';
38-
import { BpmnStyleIdentifier } from '../style';
38+
import { BpmnStyleIdentifier, MarkerIdentifier } from '../style';
3939

40-
const registerShapes = (): void => {
40+
export const registerShapes = (): void => {
4141
// Inspired by the default shapes registration done in maxGraph
4242
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- required by the signature of registerShape
4343
const shapesToRegister: [string, new (...arguments_: any) => mxShape][] = [
@@ -75,12 +75,32 @@ const registerShapes = (): void => {
7575
mxCellRenderer.registerShape(shapeName, shapeClass);
7676
}
7777
};
78+
// This implementation is adapted from the draw.io BPMN 'dash' marker
79+
// https://github.com/jgraph/drawio/blob/f539f1ff362e76127dcc7e68b5a9d83dd7d4965c/src/main/webapp/js/mxgraph/Shapes.js#L2796
80+
// prefix parameter name - common practice to acknowledge the fact that some parameter is unused (e.g. in TypeScript compiler)
81+
const dashMarkerFactory = (
82+
c: mxAbstractCanvas2D,
83+
_shape: mxShape,
84+
_type: string,
85+
pe: mxPoint,
86+
unitX: number,
87+
unitY: number,
88+
size: number,
89+
_source: mxCell,
90+
strokewidth: number,
91+
// eslint-disable-next-line unicorn/consistent-function-scoping -- Code from mxGraph example
92+
): (() => void) => {
93+
const nx = unitX * (size + strokewidth + 4);
94+
const ny = unitY * (size + strokewidth + 4);
7895

79-
/**
80-
* @internal
81-
*/
82-
export default class ShapeConfigurator {
83-
configureShapes(): void {
84-
registerShapes();
85-
}
86-
}
96+
return function () {
97+
c.begin();
98+
c.moveTo(pe.x - nx / 2 - ny / 2, pe.y - ny / 2 + nx / 2);
99+
c.lineTo(pe.x + ny / 2 - (3 * nx) / 2, pe.y - (3 * ny) / 2 - nx / 2);
100+
c.stroke();
101+
};
102+
};
103+
104+
export const registerEdgeMarkers = (): void => {
105+
mxgraph.mxMarker.addMarker(MarkerIdentifier.ARROW_DASH, dashMarkerFactory);
106+
};

0 commit comments

Comments
 (0)