Changed ItemLinksField makeup

This commit is contained in:
WBHarry 2025-07-23 14:50:24 +02:00
parent 600c08cb23
commit 30f31e77dd
13 changed files with 120 additions and 96 deletions

View file

@ -176,12 +176,13 @@ export default class DHBaseItemSheet extends DHApplicationMixin(ItemSheetV2) {
* @type {ApplicationClickAction}
*/
static async #addFeature(_, target) {
const { type } = target.dataset;
const cls = foundry.documents.Item.implementation;
const featurePath = `system.itemLinks.${CONFIG.DH.ITEM.itemLinkFeatureTypes[target.dataset.type]}`;
const feature = await cls.create({
type: 'feature',
name: cls.defaultName({ type: 'feature' }),
[`system.itemLinks.${this.document.uuid}`]: CONFIG.DH.ITEM.itemLinkFeatureTypes[type]
[featurePath]: [this.document.uuid]
});
await this.document.update({
'system.features': [...this.document.system.features, feature].map(f => f.uuid)
@ -192,10 +193,15 @@ export default class DHBaseItemSheet extends DHApplicationMixin(ItemSheetV2) {
* Remove a feature from the item.
* @type {ApplicationClickAction}
*/
static async #deleteFeature(event, target) {
static async #deleteFeature(event, element) {
const target = element.closest('[data-item-uuid]');
const feature = getDocFromElement(target);
if (!feature) return ui.notifications.warn(game.i18n.localize('DAGGERHEART.UI.Notifications.featureIsMissing'));
await feature.update({ [`system.itemLinks.-=${this.document.uuid}`]: null });
const featurePath = `system.itemLinks.${CONFIG.DH.ITEM.itemLinkFeatureTypes[target.dataset.type]}`;
await feature.update({
[featurePath]: foundry.utils.getProperty(feature, featurePath).filter(x => x !== this.document.uuid)
});
await this.document.update({
'system.features': this.document.system.features.map(x => x.uuid).filter(uuid => uuid !== feature.uuid)
});
@ -267,23 +273,20 @@ export default class DHBaseItemSheet extends DHApplicationMixin(ItemSheetV2) {
* @param {DragEvent} event - The drag event
*/
async _onDrop(event) {
event.stopPropagation();
const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event);
if (data.fromInternal) return;
const target = event.target.closest('fieldset.drop-section');
const item = await fromUuid(data.uuid);
if (item?.type === 'feature') {
const { type } = target.dataset;
const previouslyLinked = item.system.itemLinks[this.document.uuid] !== undefined;
await item.update({
[`system.itemLinks.${this.document.uuid}`]: CONFIG.DH.ITEM.itemLinkFeatureTypes[type]
});
const existing = await item.addItemLink(this.document.uuid, target.dataset.type, true);
if (!previouslyLinked) {
if (existing) {
this.render();
} else {
const current = this.document.system.features.map(x => x.uuid);
await this.document.update({ 'system.features': [...current, item.uuid] });
} else {
this.render();
}
}
}

View file

@ -79,23 +79,29 @@ export default class ClassSheet extends DHBaseItemSheet {
/* -------------------------------------------- */
async linkedItemUpdate(item, property, replace) {
const removedLinkedItems = [];
const existingLink = item.system.itemLinks[this.document.uuid];
const existing = Object.values(item.system.itemLinks).some(x => x.some(uuid => uuid === this.document.uuid));
if (replace) {
const toRemove = this.document.system.linkedItems.find(
x => x.uuid !== item.uuid && x.system.itemLinks[this.document.uuid] === property
x => x.uuid !== item.uuid && x.system.itemLinks[property]?.has(this.document.uuid)
);
if (toRemove) {
removedLinkedItems.push(toRemove.uuid);
await toRemove.update({ [`system.itemLinks.-=${this.document.uuid}`]: null });
await toRemove.update({
[`system.itemLinks.${property}`]: toRemove.system.itemLinks[property].filter(
x => x !== this.document.uuid
)
});
}
}
await item.update({ [`system.itemLinks.${this.document.uuid}`]: CONFIG.DH.ITEM.itemLinkTypes[property] });
await item.addItemLink(this.document.uuid, property, true);
if (!existingLink) {
if (!existing) {
await this.document.update({
'system.linkedItems': [
...this.document.system.linkedItems.map(x => x.uuid).filter(x => !removedLinkedItems.includes(x)),
...this.document.system.linkedItems
.filter(x => !removedLinkedItems.includes(x.uuid))
.map(x => x.uuid),
item.uuid
]
});
@ -110,13 +116,15 @@ export default class ClassSheet extends DHBaseItemSheet {
const item = await fromUuid(data.uuid);
const target = event.target.closest('fieldset.drop-section');
if (item.type === 'subclass') {
const previouslyLinked = item.system.itemLinks[this.document.uuid] !== undefined;
if (previouslyLinked) return;
const existing = await item.addItemLink(this.document.uuid, CONFIG.DH.ITEM.itemLinkTypes.subclass, true);
await item.update({ [`system.itemLinks.${this.document.uuid}`]: null });
await this.document.update({
'system.subclasses': [...this.document.system.subclasses.map(x => x.uuid), item.uuid]
});
if (existing) {
this.render();
} else {
await this.document.update({
'system.subclasses': [...this.document.system.subclasses.map(x => x.uuid), item.uuid]
});
}
} else if (item.type === 'feature') {
super._onDrop(event);
} else if (item.type === 'weapon') {
@ -159,13 +167,16 @@ export default class ClassSheet extends DHBaseItemSheet {
* @param {HTMLElement} element - The capturing HTML element which defines the [data-action="removeLinkedItem"]
*/
static async #removeLinkedItem(_event, element) {
const { uuid } = element.dataset;
const item = this.document.system.linkedItems.find(x => x.uuid === uuid);
const { uuid, target } = element.dataset;
const prop = target === 'subclass' ? 'subclasses' : 'linkedItems';
const item = this.document.system[prop].find(x => x.uuid === uuid);
if (!item) return;
await item.update({ [`system.itemLinks-=${uuid}`]: null });
await this.document.update({
'system.linkedItems': this.document.system.linkedItems.filter(x => x.uuid !== uuid).map(x => x.uuid)
[`system.${prop}`]: this.document.system[prop].filter(x => x.uuid !== uuid).map(x => x.uuid)
});
await item.update({
[`system.itemLinks.${target}`]: item.system.itemLinks[target].filter(x => x !== this.document.uuid)
});
}
}