From 0bda6b5dbeaf719c84c0733fef16583407784118 Mon Sep 17 00:00:00 2001 From: WBHarry Date: Thu, 10 Jul 2025 22:21:01 +0200 Subject: [PATCH] Changed so ancestry uses a single Features field --- module/applications/sheets/items/ancestry.mjs | 54 +++++++++++-------- module/data/item/ancestry.mjs | 11 ++-- templates/sheets/items/ancestry/features.hbs | 12 ++--- 3 files changed, 46 insertions(+), 31 deletions(-) diff --git a/module/applications/sheets/items/ancestry.mjs b/module/applications/sheets/items/ancestry.mjs index 7a55166d..d104811e 100644 --- a/module/applications/sheets/items/ancestry.mjs +++ b/module/applications/sheets/items/ancestry.mjs @@ -19,6 +19,19 @@ export default class AncestrySheet extends DHHeritageSheet { features: { template: 'systems/daggerheart/templates/sheets/items/ancestry/features.hbs' } }; + async _preparePartContext(partId, context) { + await super._preparePartContext(partId, context); + + switch (partId) { + case 'features': + context.primaryFeature = this.document.system.features.find(x => x.primary)?.value; + context.secondaryFeature = this.document.system.features.find(x => !x.primary)?.value; + break; + } + + return context; + } + /* -------------------------------------------- */ /* Application Clicks Actions */ /* -------------------------------------------- */ @@ -32,10 +45,13 @@ export default class AncestrySheet extends DHHeritageSheet { type: 'feature', name: game.i18n.format('DOCUMENT.New', { type: game.i18n.localize('TYPES.Item.feature') }) }); + await this.document.update({ system: { - features: [...this.document.system.features.map(x => x.uuid), feature.uuid], - [`${button.dataset.type}Feature`]: feature.uuid + features: [ + ...this.document.system.features.map(x => ({ ...x, value: x.value.uuid })), + { primary: button.dataset.type === 'primary', value: feature.uuid } + ] } }); } @@ -46,7 +62,9 @@ export default class AncestrySheet extends DHHeritageSheet { */ static async #editFeature(_event, button) { const target = button.closest('.feature-item'); - const feature = this.document.system[`${target.dataset.type}Feature`]; + const feature = this.document.system.features.find(x => + target.dataset.type === 'primary' ? x.primary : !x.primary + )?.value; if (!feature) { ui.notifications.warn(game.i18n.localize('DAGGERHEART.UI.Notifications.featureIsMissing')); return; @@ -62,7 +80,9 @@ export default class AncestrySheet extends DHHeritageSheet { static async #removeFeature(event, button) { event.stopPropagation(); const target = button.closest('.feature-item'); - const feature = this.document.system[`${target.dataset.type}Feature`]; + const feature = this.document.system.features.find(x => + target.dataset.type === 'primary' ? x.primary : !x.primary + )?.value; if (feature) { const confirmed = await foundry.applications.api.DialogV2.confirm({ @@ -78,10 +98,9 @@ export default class AncestrySheet extends DHHeritageSheet { } await this.document.update({ - system: { - features: this.document.system.features.filter(x => x.uuid !== feature.uuid).map(x => x.uuid), - [`${target.dataset.type}Feature`]: null - } + 'system.features': this.document.system.features + .filter(x => x.value.uuid !== feature.uuid) + .map(x => ({ ...x, value: x.value.uuid })) }); } @@ -101,19 +120,12 @@ export default class AncestrySheet extends DHHeritageSheet { const item = await fromUuid(data.uuid); if (item?.type === 'feature') { - const update = { - system: { - features: [...this.document.system.features.map(x => x.uuid), item.uuid] - } - }; - - if (event.target.closest('.primary-feature')) { - update.system.primaryFeature = item.uuid; - } else if (event.target.closest('.secondary-feature')) { - update.system.secondaryFeature = item.uuid; - } - - await this.document.update(update); + await this.document.update({ + 'system.features': [ + ...this.document.system.features.map(x => ({ ...x, value: x.value.uuid })), + { primary: Boolean(event.target.closest('.primary-feature')), value: item.uuid } + ] + }); } } } diff --git a/module/data/item/ancestry.mjs b/module/data/item/ancestry.mjs index 7de44124..bfe21200 100644 --- a/module/data/item/ancestry.mjs +++ b/module/data/item/ancestry.mjs @@ -1,4 +1,3 @@ -import ForeignDocumentUUIDArrayField from '../fields/foreignDocumentUUIDArrayField.mjs'; import ForeignDocumentUUIDField from '../fields/foreignDocumentUUIDField.mjs'; import BaseDataItem from './base.mjs'; @@ -14,11 +13,15 @@ export default class DHAncestry extends BaseDataItem { /** @inheritDoc */ static defineSchema() { + const fields = foundry.data.fields; return { ...super.defineSchema(), - features: new ForeignDocumentUUIDArrayField({ type: 'Item' }), - primaryFeature: new ForeignDocumentUUIDField({ type: 'Item' }), - secondaryFeature: new ForeignDocumentUUIDField({ type: 'Item' }) + features: new fields.ArrayField( + new fields.SchemaField({ + primary: new fields.BooleanField(), + value: new ForeignDocumentUUIDField({ type: 'Item' }) + }) + ) }; } } diff --git a/templates/sheets/items/ancestry/features.hbs b/templates/sheets/items/ancestry/features.hbs index 9f1694b6..952174b1 100644 --- a/templates/sheets/items/ancestry/features.hbs +++ b/templates/sheets/items/ancestry/features.hbs @@ -6,13 +6,13 @@
{{localize "DAGGERHEART.ITEMS.Ancestry.primaryFeature"}}
- {{#if document.system.primaryFeature}} + {{#if primaryFeature}}
- - {{document.system.primaryFeature.name}} + + {{primaryFeature.name}}
@@ -24,13 +24,13 @@
{{localize "DAGGERHEART.ITEMS.Ancestry.secondaryFeature"}}
- {{#if document.system.secondaryFeature}} + {{#if secondaryFeature}}
- - {{document.system.secondaryFeature.name}} + + {{secondaryFeature.name}}