From 49f79133946664d4c6e37518e4db64d9abee4b29 Mon Sep 17 00:00:00 2001 From: Roman Bruckner Date: Sun, 1 Dec 2024 21:46:26 +0100 Subject: [PATCH] fix(dia.ToolsView): make sure tools are rendered before the first update --- packages/joint-core/src/dia/ToolsView.mjs | 26 ++++++++++++------- .../joint-core/test/jointjs/dia/linkTools.js | 13 ++++++++++ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/packages/joint-core/src/dia/ToolsView.mjs b/packages/joint-core/src/dia/ToolsView.mjs index 180f0889e..e22d600f6 100644 --- a/packages/joint-core/src/dia/ToolsView.mjs +++ b/packages/joint-core/src/dia/ToolsView.mjs @@ -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(); } } @@ -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; @@ -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(); } } } diff --git a/packages/joint-core/test/jointjs/dia/linkTools.js b/packages/joint-core/test/jointjs/dia/linkTools.js index 495469cc1..c2bec8181 100644 --- a/packages/joint-core/test/jointjs/dia/linkTools.js +++ b/packages/joint-core/test/jointjs/dia/linkTools.js @@ -196,6 +196,19 @@ 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); + linkView.showTools(); + assert.ok(toolsView.isRendered); + }); }); QUnit.module('RotateLabel', function() {