Skip to content

Commit

Permalink
refactor: simplify some code using the mxGraph API (#3153)
Browse files Browse the repository at this point in the history
`CoordinatesTranslator`: remove extra null check

`buildParameter`: shape properties are assumed to be set by the
CellRender, according to the cell style. So only use properties for the
shape style.

NOTE: we have a large number of tests, very varied, so they would have
detected regressions in code changes if there were any.
  • Loading branch information
tbouffard authored Aug 26, 2024
1 parent 5a6897b commit a1c7c26
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
14 changes: 6 additions & 8 deletions src/component/mxgraph/BpmnRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,12 @@ export class BpmnRenderer {
edge.geometry.height = labelBounds.height;

const edgeCenterCoordinate = this.coordinatesTranslator.computeEdgeCenter(edge);
if (edgeCenterCoordinate) {
edge.geometry.relative = false;

const labelBoundsRelativeCoordinateFromParent = this.coordinatesTranslator.computeRelativeCoordinates(edge.parent, new mxPoint(labelBounds.x, labelBounds.y));
const relativeLabelX = labelBoundsRelativeCoordinateFromParent.x + labelBounds.width / 2 - edgeCenterCoordinate.x;
const relativeLabelY = labelBoundsRelativeCoordinateFromParent.y - edgeCenterCoordinate.y;
edge.geometry.offset = new mxPoint(relativeLabelX, relativeLabelY);
}
edge.geometry.relative = false;

const labelBoundsRelativeCoordinateFromParent = this.coordinatesTranslator.computeRelativeCoordinates(edge.parent, new mxPoint(labelBounds.x, labelBounds.y));
const relativeLabelX = labelBoundsRelativeCoordinateFromParent.x + labelBounds.width / 2 - edgeCenterCoordinate.x;
const relativeLabelY = labelBoundsRelativeCoordinateFromParent.y - edgeCenterCoordinate.y;
edge.geometry.offset = new mxPoint(relativeLabelX, relativeLabelY);
}

this.insertMessageFlowIconIfNeeded(internalEdge, edge);
Expand Down
11 changes: 4 additions & 7 deletions src/component/mxgraph/renderer/CoordinatesTranslator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,9 @@ export default class CoordinatesTranslator {
const p0 = points[0];
const pe = points.at(-1);

if (p0 != null && pe != null) {
const dx = pe.x - p0.x;
const dy = pe.y - p0.y;
return new mxPoint(p0.x + dx / 2, p0.y + dy / 2);
}

return undefined;
// p0 and pe are always set (all tests passed so far)
const dx = pe.x - p0.x;
const dy = pe.y - p0.y;
return new mxPoint(p0.x + dx / 2, p0.y + dy / 2);
}
}
6 changes: 3 additions & 3 deletions src/component/mxgraph/shape/render/icon-painter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ export function buildPaintParameter({
isFilled?: boolean;
iconStrokeWidth?: number;
}): PaintParameter {
const shapeStrokeWidth = shape.strokewidth || mxUtils.getValue(shape.style, mxConstants.STYLE_STROKEWIDTH, StyleDefault.STROKE_WIDTH_THIN);
const fillColor = shape.fill || mxUtils.getValue(shape.style, mxConstants.STYLE_FILLCOLOR, StyleDefault.DEFAULT_FILL_COLOR);
const strokeColor = shape.stroke || mxUtils.getValue(shape.style, mxConstants.STYLE_STROKECOLOR, StyleDefault.DEFAULT_STROKE_COLOR);
const shapeStrokeWidth = mxUtils.getValue(shape.style, mxConstants.STYLE_STROKEWIDTH, StyleDefault.STROKE_WIDTH_THIN);
const fillColor = mxUtils.getValue(shape.style, mxConstants.STYLE_FILLCOLOR, StyleDefault.DEFAULT_FILL_COLOR);
const strokeColor = mxUtils.getValue(shape.style, mxConstants.STYLE_STROKECOLOR, StyleDefault.DEFAULT_STROKE_COLOR);
const margin = mxUtils.getValue(shape.style, mxConstants.STYLE_MARGIN, StyleDefault.DEFAULT_MARGIN);
ratioFromParent ??= 0.25;
isFilled ??= false;
Expand Down

0 comments on commit a1c7c26

Please sign in to comment.