From d0ec29416471f2a7a5dac0b37d182d265af18361 Mon Sep 17 00:00:00 2001 From: Matt Smith Date: Sun, 9 Aug 2020 21:03:17 -0500 Subject: [PATCH] 3: Fix issue with adding and removing attributes - Applied patch by @imposeren to handle bug with removing and adding attributes. This patch resolves the issue by refactoring the attribute creation logic to use loop to choose a safer default attribute key before creation. --- module/actor-sheet.js | 10 ++++++++-- module/item-sheet.js | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/module/actor-sheet.js b/module/actor-sheet.js index dd5add0..5e2e95f 100644 --- a/module/actor-sheet.js +++ b/module/actor-sheet.js @@ -82,9 +82,15 @@ export class SimpleActorSheet extends ActorSheet { // Add new attribute if ( action === "create" ) { - const nk = Object.keys(attrs).length + 1; + const objKeys = Object.keys(attrs); + let nk = Object.keys(attrs).length + 1; + let newValue = `attr${nk}`; let newKey = document.createElement("div"); - newKey.innerHTML = ``; + while ( objKeys.includes(newValue) ) { + ++nk; + newValue = `attr${nk}`; + }; + newKey.innerHTML = ``; newKey = newKey.children[0]; form.appendChild(newKey); await this._onSubmit(event); diff --git a/module/item-sheet.js b/module/item-sheet.js index 8d378e1..c19ca80 100644 --- a/module/item-sheet.js +++ b/module/item-sheet.js @@ -67,9 +67,15 @@ export class SimpleItemSheet extends ItemSheet { // Add new attribute if ( action === "create" ) { - const nk = Object.keys(attrs).length + 1; + const objKeys = Object.keys(attrs); + let nk = Object.keys(attrs).length + 1; + let newValue = `attr${nk}`; let newKey = document.createElement("div"); - newKey.innerHTML = ``; + while ( objKeys.includes(newValue) ) { + ++nk; + newValue = `attr${nk}`; + } + newKey.innerHTML = ``; newKey = newKey.children[0]; form.appendChild(newKey); await this._onSubmit(event);