From c9711ac34547cb63150f4cbf0396476b33f2e9e8 Mon Sep 17 00:00:00 2001 From: Jazza231 Date: Tue, 18 Jun 2024 10:47:03 +0800 Subject: [PATCH] Addon.json update and dont delete and remove all the time --- addons/clones/addon.json | 2 +- addons/clones/spriteCount.js | 30 +++++++++++++++++++----------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/addons/clones/addon.json b/addons/clones/addon.json index 3843f63316..b215d58ffb 100644 --- a/addons/clones/addon.json +++ b/addons/clones/addon.json @@ -1,6 +1,6 @@ { "name": "Clone counter", - "description": "Adds a counter above the stage in the editor which shows the total amount of clones.", + "description": "Adds a counter above the stage in the editor which shows the total amount of clones. There is also an option to add a counter to individual sprites.", "credits": [ { "name": "Jeffalo" diff --git a/addons/clones/spriteCount.js b/addons/clones/spriteCount.js index 9ea406561b..b70d28debf 100644 --- a/addons/clones/spriteCount.js +++ b/addons/clones/spriteCount.js @@ -2,7 +2,7 @@ export default async function ({ addon, console }) { const vm = addon.tab.traps.vm; const ogCloneCounter = vm.runtime.changeCloneCounter; - function updateCounts(remove) { + async function updateCounts(remove) { let counts = {}; vm.runtime.targets @@ -13,20 +13,28 @@ export default async function ({ addon, console }) { else counts[target] += 1; }); - const spriteNames = Array.from( - document - .querySelector("[class*=sprite-selector_items-wrapper]") - .querySelectorAll("[class*=sprite-selector-item_sprite-name]") - ); + const spriteWrapper = await addon.tab.waitForElement("[class*=sprite-selector_items-wrapper]"); + const spriteNames = Array.from(spriteWrapper.querySelectorAll("[class*=sprite-selector-item_sprite-name]")); spriteNames.forEach((spriteName) => { - spriteName.querySelector(".clone-count")?.remove(); - if ((counts[spriteName.innerText.split("\n")[0]] !== undefined) & !remove) { - const count = document.createElement("div"); + if (counts[spriteName.innerText.split("\n")[0]] !== undefined) { + const existingElement = spriteName.querySelector(".sa-clone-count"); + let count; + + if (remove) { + existingElement?.remove(); + return; + } + + if (existingElement) { + count = existingElement; + } else { + count = document.createElement("div"); + count.classList.add("sa-clone-count"); + spriteName.appendChild(count); + } - count.classList.add("clone-count"); count.innerText = `(${counts[spriteName.innerText.split("\n")[0]]} clones)`; - spriteName.appendChild(count); } }); }