Skip to content

Commit 993603b

Browse files
authored
refactor: export mxGraph objects directly instead of via mxgraph namespace (#3491)
Destructure mxGraph objects from the mxgraph factory export so internal code imports them directly (e.g. `mxGraph`, `mxConstants`) rather than accessing them through `mxgraph.mxGraph`, `mxgraph.mxConstants`. This enables tree-shaking since bundlers can track individual named imports, improves readability by removing the repetitive `mxgraph.` prefix, and makes imports explicit about which mxGraph objects each module actually depends on. mxGraph is exported separately with an explicit `typeof` annotation to work around api-extractor losing constructor type information during .d.ts rollup, which caused TS2507 in downstream type checks.
1 parent 4242f32 commit 993603b

11 files changed

Lines changed: 55 additions & 49 deletions

File tree

dev/ts/component/SvgExporter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616

1717
import type { mxGraph, mxSvgCanvas2D as mxSvgCanvas2DType } from 'mxgraph';
1818

19-
import { mxgraph, mxClient, mxConstants, mxSvgCanvas2D, mxUtils } from '../../../src/component/mxgraph/initializer';
19+
import { mxClient, mxConstants, mxImageExport, mxSvgCanvas2D, mxUtils } from '../../../src/component/mxgraph/initializer';
2020

2121
interface SvgExportOptions {
2222
scale: number;
@@ -92,7 +92,7 @@ ${svgAsString}
9292

9393
svgCanvas.scale(s);
9494

95-
const imgExport = new mxgraph.mxImageExport();
95+
const imgExport = new mxImageExport();
9696
// FIXME only the first overlay is placed at the right position
9797
// overlays put on element of subprocess/call-activity are not placed correctly in svg export
9898
imgExport.includeOverlays = true;

src/component/mxgraph/BpmnCellRenderer.ts

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

1717
import type { IconPainter } from './shape/render';
18-
import type { mxCellState, mxImageShape, mxShape } from 'mxgraph';
18+
import type { mxCellState, mxImageShape as mxImageShapeType, mxShape } from 'mxgraph';
1919

20-
import { mxgraph, mxRectangle } from './initializer';
20+
import { mxCellRenderer, mxDictionary, mxImageShape, mxRectangle } from './initializer';
2121
import { CustomCellOverlay } from './overlay/custom-overlay';
2222
import { OverlayBadgeShape } from './overlay/shapes';
2323
import { overrideCreateSvgCanvas } from './shape/utils';
2424

25-
export class BpmnCellRenderer extends mxgraph.mxCellRenderer {
25+
export class BpmnCellRenderer extends mxCellRenderer {
2626
constructor(private readonly iconPainter: IconPainter) {
2727
super();
2828
}
@@ -33,7 +33,7 @@ export class BpmnCellRenderer extends mxgraph.mxCellRenderer {
3333
let dict = null;
3434

3535
if (overlays != null) {
36-
dict = new mxgraph.mxDictionary<mxShape>();
36+
dict = new mxDictionary<mxShape>();
3737

3838
for (const currentOverlay of overlays) {
3939
const shape = state.overlays == null ? null : state.overlays.remove(currentOverlay);
@@ -48,16 +48,16 @@ export class BpmnCellRenderer extends mxgraph.mxCellRenderer {
4848
if (currentOverlay instanceof CustomCellOverlay) {
4949
overlayShape = new OverlayBadgeShape(currentOverlay.label, new mxRectangle(0, 0, 0, 0), currentOverlay.style);
5050
} else {
51-
overlayShape = new mxgraph.mxImageShape(new mxRectangle(0, 0, 0, 0), currentOverlay.image.src);
52-
(overlayShape as mxImageShape).preserveImageAspect = false;
51+
overlayShape = new mxImageShape(new mxRectangle(0, 0, 0, 0), currentOverlay.image.src);
52+
(overlayShape as mxImageShapeType).preserveImageAspect = false;
5353
}
5454
// END bpmn-visualization CUSTOMIZATION
5555

5656
overlayShape.dialect = state.view.graph.dialect;
5757
overlayShape.overlay = currentOverlay;
5858

5959
// The 'initializeOverlay' signature forces us to hardly cast the overlayShape
60-
this.initializeOverlay(state, overlayShape as mxImageShape);
60+
this.initializeOverlay(state, overlayShape as mxImageShapeType);
6161
this.installCellOverlayListeners(state, currentOverlay, overlayShape);
6262

6363
if (currentOverlay.cursor != null) {

src/component/mxgraph/BpmnGraph.ts

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

1717
import type { IconPainter } from './shape/render';
18-
import type { mxCellRenderer, mxCellState, mxGraphView, mxPoint } from 'mxgraph';
18+
import type { mxCellRenderer, mxCellState, mxGraphView as mxGraphViewType, mxPoint } from 'mxgraph';
1919

2020
import { BpmnCellRenderer } from './BpmnCellRenderer';
21-
import { mxgraph } from './initializer';
21+
import { mxGraph, mxGraphView } from './initializer';
2222

2323
/**
2424
* Temporary storage for iconPainter during BpmnGraph construction.
@@ -49,7 +49,7 @@ import { mxgraph } from './initializer';
4949
*/
5050
let pendingIconPainter: IconPainter | undefined;
5151

52-
export class BpmnGraph extends mxgraph.mxGraph {
52+
export class BpmnGraph extends mxGraph {
5353
/**
5454
* @internal
5555
*/
@@ -72,7 +72,7 @@ export class BpmnGraph extends mxgraph.mxGraph {
7272
/**
7373
* @internal
7474
*/
75-
override createGraphView(): mxGraphView {
75+
override createGraphView(): mxGraphViewType {
7676
return new BpmnGraphView(this);
7777
}
7878

@@ -108,7 +108,7 @@ export class BpmnGraph extends mxgraph.mxGraph {
108108
}
109109
}
110110

111-
class BpmnGraphView extends mxgraph.mxGraphView {
111+
class BpmnGraphView extends mxGraphView {
112112
override getFloatingTerminalPoint(edge: mxCellState, start: mxCellState, end: mxCellState, source: boolean): mxPoint {
113113
// some values may be null: the first and the last values are null prior computing floating terminal points
114114
const edgePoints = edge.absolutePoints.filter(Boolean);

src/component/mxgraph/config/register-style-definitions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
import type { mxAbstractCanvas2D, mxCell, mxPoint, mxShape } from 'mxgraph';
1818

1919
import { ShapeBpmnElementKind } from '../../../model/bpmn/internal';
20-
import { mxCellRenderer, mxgraph } from '../initializer';
20+
import { mxCellRenderer, mxMarker } from '../initializer';
2121
import {
2222
BusinessRuleTaskShape,
2323
CallActivityShape,
@@ -102,5 +102,5 @@ const dashMarkerFactory = (
102102
};
103103

104104
export const registerEdgeMarkers = (): void => {
105-
mxgraph.mxMarker.addMarker(MarkerIdentifier.ARROW_DASH, dashMarkerFactory);
105+
mxMarker.addMarker(MarkerIdentifier.ARROW_DASH, dashMarkerFactory);
106106
};

src/component/mxgraph/initializer.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,34 @@ import factory, { type mxGraphExportObject } from 'mxgraph';
3333
*/
3434
export const mxgraph = initialize();
3535

36-
/** @internal */
37-
export const mxCellRenderer = mxgraph.mxCellRenderer;
38-
/** @internal */
39-
export const mxClient = mxgraph.mxClient;
40-
/** @internal */
41-
export const mxConstants = mxgraph.mxConstants;
42-
/** @internal */
43-
export const mxEvent = mxgraph.mxEvent;
44-
/** @internal */
45-
export const mxPerimeter = mxgraph.mxPerimeter;
46-
/** @internal */
47-
export const mxPoint = mxgraph.mxPoint;
48-
/** @internal */
49-
export const mxRectangle = mxgraph.mxRectangle;
50-
/** @internal */
51-
export const mxRectangleShape = mxgraph.mxRectangleShape;
52-
/** @internal */
53-
export const mxSvgCanvas2D = mxgraph.mxSvgCanvas2D;
54-
/** @internal */
55-
export const mxUtils = mxgraph.mxUtils;
36+
// Destructured exports for convenient access to mxGraph objects.
37+
// mxGraph is exported separately to preserve its constructor types in the api-extractor generated .d.ts (destructuring loses `typeof` during rollup).
38+
export const {
39+
mxCellOverlay,
40+
mxCellRenderer,
41+
mxClient,
42+
mxConnector,
43+
mxConstants,
44+
mxDictionary,
45+
mxEllipse,
46+
mxEvent,
47+
mxGeometry, // at least used in tests
48+
mxGraphView,
49+
mxImageExport,
50+
mxImageShape,
51+
mxMarker,
52+
mxPerimeter,
53+
mxPoint,
54+
mxRectangle,
55+
mxRectangleShape,
56+
mxRhombus,
57+
mxSvgCanvas2D,
58+
mxText,
59+
mxUtils,
60+
} = mxgraph;
61+
62+
// Declare the type, because api-extractor loses the `typeof` during rollup when destructuring (see comment above).
63+
export const mxGraph: typeof mxgraph.mxGraph = mxgraph.mxGraph;
5664

5765
/** @internal */
5866
declare global {

src/component/mxgraph/overlay/custom-overlay.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
import type { OverlayStyle } from '../../registry';
1818
import type { mxCellState, mxPoint as mxPointType, mxRectangle as mxRectangleType } from 'mxgraph';
1919

20-
import { mxgraph, mxConstants, mxPoint, mxRectangle } from '../initializer';
20+
import { mxCellOverlay, mxConstants, mxPoint, mxRectangle } from '../initializer';
2121

2222
export type VerticalAlignType = 'bottom' | 'middle' | 'top';
2323
export type HorizontalAlignType = 'left' | 'center' | 'right';
@@ -34,7 +34,7 @@ export interface CustomCellOverlayPosition {
3434

3535
export type CustomCellOverlayStyle = Required<OverlayStyle>;
3636

37-
export class CustomCellOverlay extends mxgraph.mxCellOverlay {
37+
export class CustomCellOverlay extends mxCellOverlay {
3838
readonly style: CustomCellOverlayStyle;
3939

4040
constructor(

src/component/mxgraph/overlay/shapes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ limitations under the License.
1717
import type { CustomCellOverlayStyle } from './custom-overlay';
1818
import type { mxRectangle } from 'mxgraph';
1919

20-
import { mxgraph } from '../initializer';
20+
import { mxText } from '../initializer';
2121

22-
export class OverlayBadgeShape extends mxgraph.mxText {
22+
export class OverlayBadgeShape extends mxText {
2323
constructor(value: string, bounds: mxRectangle, style: CustomCellOverlayStyle) {
2424
super(
2525
value,

src/component/mxgraph/shape/edges.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ limitations under the License.
1616

1717
import type { mxAbstractCanvas2D, mxPoint } from 'mxgraph';
1818

19-
import { mxgraph, mxSvgCanvas2D, mxUtils } from '../initializer';
19+
import { mxConnector, mxSvgCanvas2D, mxUtils } from '../initializer';
2020
import { BpmnStyleIdentifier } from '../style';
2121

22-
export class BpmnConnector extends mxgraph.mxConnector {
22+
export class BpmnConnector extends mxConnector {
2323
override paintEdgeShape(c: mxAbstractCanvas2D, pts: mxPoint[]): void {
2424
// The indirection via functions for markers is needed in
2525
// order to apply the offsets before painting the line and

src/component/mxgraph/shape/event-shapes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ import type { BpmnCanvas, PaintParameter, IconPainter } from './render';
1818
import type { mxAbstractCanvas2D } from 'mxgraph';
1919

2020
import { ShapeBpmnEventDefinitionKind } from '../../../model/bpmn/internal';
21-
import { mxgraph, mxUtils } from '../initializer';
21+
import { mxEllipse, mxUtils } from '../initializer';
2222
import { BpmnStyleIdentifier, StyleDefault } from '../style';
2323

2424
import { buildPaintParameter } from './render/icon-painter';
2525

2626
/**
2727
* @internal
2828
*/
29-
export class EventShape extends mxgraph.mxEllipse {
29+
export class EventShape extends mxEllipse {
3030
// The actual value is injected at runtime by BpmnCellRenderer
3131
protected iconPainter: IconPainter = undefined;
3232

src/component/mxgraph/shape/gateway-shapes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ import type { IconPainter, PaintParameter } from './render';
1818
import type { mxAbstractCanvas2D } from 'mxgraph';
1919

2020
import { ShapeBpmnEventBasedGatewayKind } from '../../../model/bpmn/internal';
21-
import { mxgraph, mxUtils } from '../initializer';
21+
import { mxRhombus, mxUtils } from '../initializer';
2222
import { BpmnStyleIdentifier, StyleDefault } from '../style';
2323
import { getBpmnIsInstantiating } from '../style/utils';
2424

2525
import { buildPaintParameter } from './render/icon-painter';
2626

27-
abstract class GatewayShape extends mxgraph.mxRhombus {
27+
abstract class GatewayShape extends mxRhombus {
2828
// The actual value is injected at runtime by BpmnCellRenderer
2929
protected iconPainter: IconPainter = undefined;
3030

0 commit comments

Comments
 (0)