Skip to content

Commit

Permalink
fix(dia.ToolsView): make sure tools are rendered before the first upd…
Browse files Browse the repository at this point in the history
…ate (#2815)
  • Loading branch information
kumilingus authored Dec 2, 2024
1 parent d1918e3 commit 29be243
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
33 changes: 22 additions & 11 deletions packages/joint-core/src/dia/ToolsView.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,7 @@ export const ToolsView = mvc.View.extend({
const tool = tools[i];
tool.updateVisibility();
if (!tool.isVisible()) continue;
if (!this.isRendered) {
// There is at least one visible tool
this.isRendered = Array(n).fill(false);
}
if (!this.isRendered[i]) {
// First update executes render()
tool.render();
this.isRendered[i] = true;
} else if (opt.tool !== tool.cid) {
if (this.ensureToolRendered(tools, i) && opt.tool !== tool.cid) {
tool.update();
}
}
Expand All @@ -79,6 +71,20 @@ export const ToolsView = mvc.View.extend({
return this;
},

ensureToolRendered(tools, i) {
if (!this.isRendered) {
// There is at least one visible tool
this.isRendered = Array(tools.length).fill(false);
}
if (!this.isRendered[i]) {
// First update executes render()
tools[i].render();
this.isRendered[i] = true;
return false;
}
return true;
},

focusTool: function(focusedTool) {

var tools = this.tools;
Expand All @@ -103,7 +109,7 @@ export const ToolsView = mvc.View.extend({
tool.show();
// Check if the tool is conditionally visible too
if (tool.isVisible()) {
tool.update();
this.ensureToolRendered(tools, i) && tool.update();
}
}
}
Expand All @@ -115,7 +121,12 @@ export const ToolsView = mvc.View.extend({
},

show: function() {
return this.blurTool(null);
this.blurTool(null);
// If this the first time the tools are shown, make sure they are mounted
if (!this.isMounted()) {
this.mount();
}
return this;
},

onRemove: function() {
Expand Down
15 changes: 15 additions & 0 deletions packages/joint-core/test/jointjs/dia/linkTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ QUnit.module('linkTools', function(hooks) {
button2UpdateSpy.restore();
});
});


QUnit.test('show()', function(assert) {
paper.freeze();
const remove = new joint.linkTools.Vertices();
const toolsView = new joint.dia.ToolsView({ tools: [remove] });
linkView.addTools(toolsView);
linkView.hideTools();
paper.unfreeze();
assert.notOk(toolsView.isRendered);
assert.notOk(toolsView.el.isConnected);
linkView.showTools();
assert.ok(toolsView.isRendered);
assert.ok(toolsView.el.isConnected);
});
});

QUnit.module('RotateLabel', function() {
Expand Down

0 comments on commit 29be243

Please sign in to comment.