This commit is contained in:
WBHarry 2025-07-25 01:45:53 +02:00
parent f0a809266d
commit e8343de4e4
4 changed files with 52 additions and 52 deletions

View file

@ -63,6 +63,26 @@ export default class ClassSheet extends DHBaseItemSheet {
return this.document.system.features.map(x => x.item); 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 */ /**@inheritdoc */
async _prepareContext(_options) { async _prepareContext(_options) {
const context = await super._prepareContext(_options); const context = await super._prepareContext(_options);

View file

@ -159,12 +159,12 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel {
const allowed = await super._preUpdate(changed, options, userId); const allowed = await super._preUpdate(changed, options, userId);
if (allowed === false) return false; if (allowed === false) return false;
addLinkedItemsDiff(changed.system?.features, this.features, options, 'changedFeatures'); addLinkedItemsDiff(changed.system?.features, this.features, options);
} }
_onUpdate(changed, options, userId) { _onUpdate(changed, options, userId) {
super._onUpdate(changed, options, userId); super._onUpdate(changed, options, userId);
updateLinkedItemApps(options, 'changedFeatures', this.parent.sheet); updateLinkedItemApps(options, this.parent.sheet);
} }
} }

View file

@ -102,44 +102,28 @@ export default class DHClass extends BaseDataItem {
const allowed = await super._preUpdate(changed, options, userId); const allowed = await super._preUpdate(changed, options, userId);
if (allowed === false) return false; 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; for (let path of paths) {
addLinkedItemsDiff( const currentItems = [].concat(foundry.utils.getProperty(this, path) ?? []);
guide?.suggestedPrimaryWeapon ? [guide.suggestedPrimaryWeapon] : null, const changedItems = [].concat(foundry.utils.getProperty(changed, `system.${path}`) ?? []);
this.characterGuide.suggestedPrimaryWeapon ? [this.characterGuide.suggestedPrimaryWeapon] : [], if (!changedItems.length) continue;
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'
);
addLinkedItemsDiff(changed.system?.inventory?.take, this.inventory.take, options, 'changedTake'); addLinkedItemsDiff(changedItems, currentItems, options);
addLinkedItemsDiff(changed.system?.inventory?.choiceA, this.inventory.choiceA, options, 'changedChoiceA'); }
addLinkedItemsDiff(changed.system?.inventory?.choiceB, this.inventory.choiceB, options, 'changedChoiceB');
} }
_onUpdate(changed, options, userId) { _onUpdate(changed, options, userId) {
super._onUpdate(changed, options, userId); super._onUpdate(changed, options, userId);
updateLinkedItemApps(options, 'changedSubclasses', this.parent.sheet); updateLinkedItemApps(options, 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);
} }
} }

View file

@ -247,16 +247,15 @@ export function getDocFromElement(element) {
* @param {Array} changedItems The candidate changed list * @param {Array} changedItems The candidate changed list
* @param {Array} currentItems The current list * @param {Array} currentItems The current list
* @param {object} options Additional options which modify the update request * @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) { if (changedItems) {
const prevItems = new Set(currentItems); const prevItems = new Set(currentItems);
const newItems = new Set(changedItems); const newItems = new Set(changedItems);
options[optionsName] = { options.toLink = Array.from(newItems.difference(prevItems).map(item => item?.item ?? item));
toLink: Array.from(newItems.difference(prevItems).map(item => item?.item ?? item)), options.toUnlink = Array.from(
toUnlink: Array.from(prevItems.difference(newItems).map(item => item?.item?.uuid ?? item?.uuid ?? item)) 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 * Adds or removes the current Application from linked document apps
* depending on an update diff in the linked item list. * depending on an update diff in the linked item list.
* @param {object} options Additional options which modify the update requests * @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 * @param {object} sheet The application to add or remove from document apps
*/ */
export function updateLinkedItemApps(options, optionName, sheet) { export function updateLinkedItemApps(options, sheet) {
if (options[optionName]) { options.toLink?.forEach(featureUuid => {
options[optionName].toLink.forEach(featureUuid => { const doc = foundry.utils.fromUuidSync(featureUuid);
const doc = foundry.utils.fromUuidSync(featureUuid); doc.apps[sheet.id] = sheet;
doc.apps[sheet.id] = sheet; });
}); options.toUnlink?.forEach(featureUuid => {
options[optionName].toUnlink.forEach(featureUuid => { const doc = foundry.utils.fromUuidSync(featureUuid);
const doc = foundry.utils.fromUuidSync(featureUuid); delete doc.apps[sheet.id];
delete doc.apps[sheet.id]; });
});
}
} }
export const itemAbleRollParse = (value, actor, item) => { export const itemAbleRollParse = (value, actor, item) => {