From 6d12e652bbf133b5e6f85b3b4889218258604610 Mon Sep 17 00:00:00 2001 From: Roman Bruckner Date: Wed, 18 Sep 2024 10:58:15 +0200 Subject: [PATCH] fix(dia.LinkView): invalidate the root node cache when labels change (#2758) --- packages/joint-core/src/dia/CellView.mjs | 6 ++++++ packages/joint-core/src/dia/LinkView.mjs | 7 +++++++ packages/joint-core/test/jointjs/linkView.js | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/packages/joint-core/src/dia/CellView.mjs b/packages/joint-core/src/dia/CellView.mjs index e1c5f2dba..b698b5c0d 100644 --- a/packages/joint-core/src/dia/CellView.mjs +++ b/packages/joint-core/src/dia/CellView.mjs @@ -768,6 +768,12 @@ export const CellView = View.extend({ this.metrics = {}; }, + cleanNodeCache: function(node) { + const id = node.id; + if (!id) return; + delete this.metrics[id]; + }, + nodeCache: function(magnet) { var metrics = this.metrics; diff --git a/packages/joint-core/src/dia/LinkView.mjs b/packages/joint-core/src/dia/LinkView.mjs index 774a3972b..7e3a923ef 100644 --- a/packages/joint-core/src/dia/LinkView.mjs +++ b/packages/joint-core/src/dia/LinkView.mjs @@ -461,6 +461,13 @@ export const LinkView = CellView.extend({ if (!this._V.labels) return this; + if (!this.paper.options.labelLayer) { + // If there is no label layer, the cache needs to be cleared + // of the root node because the labels are attached + // to it and could affect the bounding box. + this.cleanNodeCache(this.el); + } + var model = this.model; var labels = model.get('labels') || []; var canLabelMove = this.can('labelMove'); diff --git a/packages/joint-core/test/jointjs/linkView.js b/packages/joint-core/test/jointjs/linkView.js index df42a56b2..39139e905 100644 --- a/packages/joint-core/test/jointjs/linkView.js +++ b/packages/joint-core/test/jointjs/linkView.js @@ -91,6 +91,26 @@ QUnit.module('linkView', function(hooks) { assert.equal(group[0], circleNode); assert.equal(group[1], rectNode); }); + + QUnit.test('change label position', function(assert) { + + link.labels([{ + position: { distance: 0.5, offset: 0 }, + attrs: { rect: { x: -10, y: -10, width: 20, height: 20 }} + }]); + + const Y = 3; + + const linkBBoxBefore = linkView.getNodeBBox(linkView.el); + assert.deepEqual(linkBBoxBefore.toJSON(), { x: 100, y: 90, width: 100, height: 20 }); + + link.label(0, { position: { distance: 0.5, offset: Y }}); + + const linkBBoxAfter = linkView.getNodeBBox(linkView.el); + assert.deepEqual(linkBBoxAfter.toJSON(), { x: 100, y: 90 + Y, width: 100, height: 20 }); + + assert.notOk(linkBBoxBefore.equals(linkBBoxAfter)); + }); }); QUnit.module('addLabel', function(hooks) {