diff --git a/module/applications/sheets/items/class.mjs b/module/applications/sheets/items/class.mjs index e4c04b62..37db5b8d 100644 --- a/module/applications/sheets/items/class.mjs +++ b/module/applications/sheets/items/class.mjs @@ -63,6 +63,26 @@ export default class ClassSheet extends DHBaseItemSheet { return this.document.system.features.map(x => x.item); } + /**@inheritdoc */ + async _onFirstRender(context, options) { + await super._onFirstRender(context, options); + + const paths = [ + 'subclasses', + 'characterGuide.suggestedPrimaryWeapon', + 'characterGuide.suggestedSecondaryWeapon', + 'characterGuide.suggestedArmor', + 'inventory.take', + 'inventory.choiceA', + 'inventory.choiceB' + ]; + + paths.forEach(path => { + const docs = [].concat(foundry.utils.getProperty(this.document, `system.${path}`) ?? []); + docs.forEach(doc => (doc.apps[this.id] = this)); + }); + } + /**@inheritdoc */ async _prepareContext(_options) { const context = await super._prepareContext(_options); diff --git a/module/data/item/base.mjs b/module/data/item/base.mjs index 291403c5..5b95a810 100644 --- a/module/data/item/base.mjs +++ b/module/data/item/base.mjs @@ -159,12 +159,12 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel { const allowed = await super._preUpdate(changed, options, userId); if (allowed === false) return false; - addLinkedItemsDiff(changed.system?.features, this.features, options, 'changedFeatures'); + addLinkedItemsDiff(changed.system?.features, this.features, options); } _onUpdate(changed, options, userId) { super._onUpdate(changed, options, userId); - updateLinkedItemApps(options, 'changedFeatures', this.parent.sheet); + updateLinkedItemApps(options, this.parent.sheet); } } diff --git a/module/data/item/class.mjs b/module/data/item/class.mjs index 3242aafb..d64c77cc 100644 --- a/module/data/item/class.mjs +++ b/module/data/item/class.mjs @@ -102,44 +102,28 @@ export default class DHClass extends BaseDataItem { const allowed = await super._preUpdate(changed, options, userId); if (allowed === false) return false; - addLinkedItemsDiff(changed.system?.subclasses, this.subclasses, options, 'changedSubclasses'); + const paths = [ + 'subclasses', + 'characterGuide.suggestedPrimaryWeapon', + 'characterGuide.suggestedSecondaryWeapon', + 'characterGuide.suggestedArmor', + 'inventory.take', + 'inventory.choiceA', + 'inventory.choiceB' + ]; - const guide = changed.system?.characterGuide; - addLinkedItemsDiff( - guide?.suggestedPrimaryWeapon ? [guide.suggestedPrimaryWeapon] : null, - this.characterGuide.suggestedPrimaryWeapon ? [this.characterGuide.suggestedPrimaryWeapon] : [], - options, - 'primaryWeapon' - ); - addLinkedItemsDiff( - guide?.suggestedSecondaryWeapon ? [guide.suggestedSecondaryWeapon] : null, - this.characterGuide.suggestedSecondaryWeapon ? [this.characterGuide.suggestedSecondaryWeapon] : [], - options, - 'secondaryWeapon' - ); - addLinkedItemsDiff( - guide?.suggestedArmor ? [guide.suggestedArmor] : null, - this.characterGuide.suggestedArmor ? [this.characterGuide.suggestedArmor] : [], - options, - 'armor' - ); + for (let path of paths) { + const currentItems = [].concat(foundry.utils.getProperty(this, path) ?? []); + const changedItems = [].concat(foundry.utils.getProperty(changed, `system.${path}`) ?? []); + if (!changedItems.length) continue; - addLinkedItemsDiff(changed.system?.inventory?.take, this.inventory.take, options, 'changedTake'); - addLinkedItemsDiff(changed.system?.inventory?.choiceA, this.inventory.choiceA, options, 'changedChoiceA'); - addLinkedItemsDiff(changed.system?.inventory?.choiceB, this.inventory.choiceB, options, 'changedChoiceB'); + addLinkedItemsDiff(changedItems, currentItems, options); + } } _onUpdate(changed, options, userId) { super._onUpdate(changed, options, userId); - updateLinkedItemApps(options, 'changedSubclasses', this.parent.sheet); - - updateLinkedItemApps(options, 'primaryWeapon', this.parent.sheet); - updateLinkedItemApps(options, 'secondaryWeapon', this.parent.sheet); - updateLinkedItemApps(options, 'armor', this.parent.sheet); - - updateLinkedItemApps(options, 'changedTake', this.parent.sheet); - updateLinkedItemApps(options, 'changedChoiceA', this.parent.sheet); - updateLinkedItemApps(options, 'changedChoiceB', this.parent.sheet); + updateLinkedItemApps(options, this.parent.sheet); } } diff --git a/module/helpers/utils.mjs b/module/helpers/utils.mjs index 25f3748a..4fcce619 100644 --- a/module/helpers/utils.mjs +++ b/module/helpers/utils.mjs @@ -247,16 +247,15 @@ export function getDocFromElement(element) { * @param {Array} changedItems The candidate changed list * @param {Array} currentItems The current list * @param {object} options Additional options which modify the update request - * @param {string} optionsName The name of the options property holding the diff */ -export function addLinkedItemsDiff(changedItems, currentItems, options, optionsName) { +export function addLinkedItemsDiff(changedItems, currentItems, options) { if (changedItems) { const prevItems = new Set(currentItems); const newItems = new Set(changedItems); - options[optionsName] = { - toLink: Array.from(newItems.difference(prevItems).map(item => item?.item ?? item)), - toUnlink: Array.from(prevItems.difference(newItems).map(item => item?.item?.uuid ?? item?.uuid ?? item)) - }; + options.toLink = Array.from(newItems.difference(prevItems).map(item => item?.item ?? item)); + options.toUnlink = Array.from( + prevItems.difference(newItems).map(item => item?.item?.uuid ?? item?.uuid ?? item) + ); } } @@ -264,20 +263,17 @@ export function addLinkedItemsDiff(changedItems, currentItems, options, optionsN * Adds or removes the current Application from linked document apps * depending on an update diff in the linked item list. * @param {object} options Additional options which modify the update requests - * @param {string} optionName The prop name on options of the update diff * @param {object} sheet The application to add or remove from document apps */ -export function updateLinkedItemApps(options, optionName, sheet) { - if (options[optionName]) { - options[optionName].toLink.forEach(featureUuid => { - const doc = foundry.utils.fromUuidSync(featureUuid); - doc.apps[sheet.id] = sheet; - }); - options[optionName].toUnlink.forEach(featureUuid => { - const doc = foundry.utils.fromUuidSync(featureUuid); - delete doc.apps[sheet.id]; - }); - } +export function updateLinkedItemApps(options, sheet) { + options.toLink?.forEach(featureUuid => { + const doc = foundry.utils.fromUuidSync(featureUuid); + doc.apps[sheet.id] = sheet; + }); + options.toUnlink?.forEach(featureUuid => { + const doc = foundry.utils.fromUuidSync(featureUuid); + delete doc.apps[sheet.id]; + }); } export const itemAbleRollParse = (value, actor, item) => {