Remade apps handling

This commit is contained in:
WBHarry 2025-07-24 21:42:05 +02:00
parent 967b3ba2ab
commit 7ff7f718ed
7 changed files with 72 additions and 13 deletions

View file

@ -108,6 +108,13 @@ export default function DHApplicationMixin(Base) {
tagifyConfigs: [] tagifyConfigs: []
}; };
/**
* Related documents that should cause a rerender of this application when updated.
*/
get relatedDocs() {
return [];
}
/* -------------------------------------------- */ /* -------------------------------------------- */
/**@inheritdoc */ /**@inheritdoc */
@ -118,9 +125,17 @@ export default function DHApplicationMixin(Base) {
/**@inheritdoc */ /**@inheritdoc */
async _onFirstRender(context, options) { async _onFirstRender(context, options) {
await super._onFirstRender(context, options); await super._onFirstRender(context, options);
this.relatedDocs.map(doc => (doc.apps[this.id] = this));
if (!!this.options.contextMenus.length) this._createContextMenus(); if (!!this.options.contextMenus.length) this._createContextMenus();
} }
/** @inheritDoc */
_onClose(options) {
super._onClose(options);
this.relatedDocs.map(doc => delete doc.apps[this.id]);
}
/**@inheritdoc */ /**@inheritdoc */
async _onRender(context, options) { async _onRender(context, options) {
await super._onRender(context, options); await super._onRender(context, options);
@ -511,19 +526,7 @@ export default function DHApplicationMixin(Base) {
*/ */
static async #editDoc(_event, target) { static async #editDoc(_event, target) {
const doc = getDocFromElement(target); const doc = getDocFromElement(target);
if (doc) { if (doc) return doc.sheet.render({ force: true });
const appId = this.element.id;
doc.apps[appId] = this;
const app = await doc.sheet.render({ force: true });
app.addEventListener(
'close',
() => {
delete doc.apps[appId];
},
{ once: true }
);
return;
}
// TODO: REDO this // TODO: REDO this
const { actionId } = target.closest('[data-action-id]').dataset; const { actionId } = target.closest('[data-action-id]').dataset;

View file

@ -13,6 +13,11 @@ export default class AncestrySheet extends DHHeritageSheet {
features: { template: 'systems/daggerheart/templates/sheets/items/ancestry/features.hbs' } features: { template: 'systems/daggerheart/templates/sheets/items/ancestry/features.hbs' }
}; };
/**@inheritdoc */
get relatedDocs() {
return this.document.system.features.map(x => x.item);
}
/* -------------------------------------------- */ /* -------------------------------------------- */
/* Application Drag/Drop */ /* Application Drag/Drop */
/* -------------------------------------------- */ /* -------------------------------------------- */

View file

@ -31,6 +31,11 @@ export default class BeastformSheet extends DHBaseItemSheet {
} }
}; };
/**@inheritdoc */
get relatedDocs() {
return this.document.system.features;
}
_attachPartListeners(partId, htmlElement, options) { _attachPartListeners(partId, htmlElement, options) {
super._attachPartListeners(partId, htmlElement, options); super._attachPartListeners(partId, htmlElement, options);

View file

@ -58,6 +58,11 @@ export default class ClassSheet extends DHBaseItemSheet {
} }
}; };
/**@inheritdoc */
get relatedDocs() {
return this.document.system.features.map(x => x.item);
}
/**@inheritdoc */ /**@inheritdoc */
async _prepareContext(_options) { async _prepareContext(_options) {
const context = await super._prepareContext(_options); const context = await super._prepareContext(_options);

View file

@ -15,4 +15,9 @@ export default class CommunitySheet extends DHHeritageSheet {
scrollable: ['.feature'] scrollable: ['.feature']
} }
}; };
/**@inheritdoc */
get relatedDocs() {
return this.document.system.features;
}
} }

View file

@ -35,4 +35,9 @@ export default class SubclassSheet extends DHBaseItemSheet {
labelPrefix: 'DAGGERHEART.GENERAL.Tabs' labelPrefix: 'DAGGERHEART.GENERAL.Tabs'
} }
}; };
/**@inheritdoc */
get relatedDocs() {
return this.document.system.features.map(x => x.item);
}
} }

View file

@ -148,4 +148,35 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel {
items.map(x => x.id) items.map(x => x.id)
); );
} }
async _preUpdate(changed, options, userId) {
const allowed = await super._preUpdate(changed, options, userId);
if (allowed === false) return false;
if (changed.system?.features) {
const prevFeatures = new Set(this.features);
const newFeatures = new Set(changed.system.features);
options.changedFeatures = {
toLink: Array.from(newFeatures.difference(prevFeatures).map(feature => feature.item ?? feature)),
toUnlink: Array.from(
prevFeatures.difference(newFeatures).map(feature => feature.item?.uuid ?? feature.uuid)
)
};
}
}
_onUpdate(changed, options, userId) {
super._onUpdate(changed, options, userId);
if (options.changedFeatures) {
options.changedFeatures.toLink.forEach(featureUuid => {
const doc = foundry.utils.fromUuidSync(featureUuid);
doc.apps[this.parent.sheet.id] = this.parent.sheet;
});
options.changedFeatures.toUnlink.forEach(featureUuid => {
const doc = foundry.utils.fromUuidSync(featureUuid);
delete doc.apps[this.parent.sheet.id];
});
}
}
} }