Skip to content

Commit

Permalink
fix(dia.HighlighterView): prevent highlighter mounting to unmounted c…
Browse files Browse the repository at this point in the history
…ell views (#2165)
  • Loading branch information
kumilingus authored May 4, 2023
1 parent 2e9cac2 commit a7a49df
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
15 changes: 14 additions & 1 deletion src/dia/HighlighterView.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const HighlighterView = mvc.View.extend({
nodeSelector: null,
node: null,
updateRequested: false,
postponedUpdate: false,
transformGroup: null,
detachedTransformGroup: null,

Expand All @@ -41,6 +42,10 @@ export const HighlighterView = mvc.View.extend({
// The cellView is now rendered/updated since it has a higher update priority.
this.updateRequested = false;
const { cellView, nodeSelector } = this;
if (!cellView.isMounted()) {
this.postponedUpdate = true;
return 0;
}
this.update(cellView, nodeSelector);
this.mount();
this.transform();
Expand Down Expand Up @@ -92,8 +97,15 @@ export const HighlighterView = mvc.View.extend({
},

mount() {
const { MOUNTABLE, cellView, el, options, transformGroup, detachedTransformGroup } = this;
const { MOUNTABLE, cellView, el, options, transformGroup, detachedTransformGroup, postponedUpdate, nodeSelector } = this;
if (!MOUNTABLE || transformGroup) return;
if (postponedUpdate) {
// The cellView was not mounted when the update was requested.
// The update was postponed until the cellView is mounted.
this.update(cellView, nodeSelector);
this.transform();
return;
}
const { vel: cellViewRoot, paper } = cellView;
const { layer: layerName } = options;
if (layerName) {
Expand Down Expand Up @@ -139,6 +151,7 @@ export const HighlighterView = mvc.View.extend({
update() {
const { node: prevNode, cellView, nodeSelector, updateRequested, id } = this;
if (updateRequested) return;
this.postponedUpdate = false;
const node = this.node = this.findNode(cellView, nodeSelector);
if (prevNode) {
this.unhighlight(cellView, prevNode);
Expand Down
4 changes: 0 additions & 4 deletions src/dia/ToolsView.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@ export const ToolsView = mvc.View.extend({
}
}
return this;
},

isMounted: function() {
return this.el.parentNode !== null;
}

});
24 changes: 24 additions & 0 deletions test/jointjs/dia/HighlighterView.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,30 @@ QUnit.module('HighlighterView', function(hooks) {
});
});

QUnit.test('are not mounted to an unmounted element view', function(assert) {
['back', null].forEach(layer => {
const id = 'highlighter-id';
paper.dumpViews({ viewport: () => false });
const highlighter = joint.dia.HighlighterView.add(elementView, 'root', id, { layer });
assert.notOk(highlighter.el.isConnected);
if (layer) {
assert.notOk(highlighter.transformGroup);
assert.notOk(highlighter.detachedTransformGroup);
}
paper.dumpViews({ viewport: () => true });
assert.ok(highlighter.el.isConnected);
if (layer) {
assert.ok(highlighter.transformGroup);
const { tx, ty } = highlighter.transformGroup.translate();
const { x, y } = element.position();
assert.equal(tx, x);
assert.equal(ty, y);
assert.notOk(highlighter.detachedTransformGroup);
}
joint.dia.HighlighterView.remove(elementView, id);
});
});

QUnit.test('are mounted and unmounted with the link view', function(assert) {
['back', null].forEach(layer => {
const id = 'highlighter-id';
Expand Down
10 changes: 4 additions & 6 deletions types/joint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1796,10 +1796,6 @@ export namespace dia {

mount(): this;

unmount(): this;

isMounted(): boolean;

protected simulateRelatedView(el: SVGElement): void;
}

Expand Down Expand Up @@ -1868,9 +1864,9 @@ export namespace dia {
nodeSelector: HighlighterView.NodeSelector | null;
node: SVGElement | null;
updateRequested: boolean;
postponedUpdate: boolean;
transformGroup: Vectorizer | null;

public unmount(): void;
detachedTransformGroup: Vectorizer | null;

protected findNode(cellView: dia.CellView, nodeSelector: HighlighterView.NodeSelector): SVGElement | null;

Expand Down Expand Up @@ -3397,6 +3393,8 @@ export namespace mvc {

unmount(): void;

isMounted(): boolean;

protected init(): void;

protected onRender(): void;
Expand Down

0 comments on commit a7a49df

Please sign in to comment.