From b269e585854ee6096f323fcef77d47a2bedb484a Mon Sep 17 00:00:00 2001 From: psitacus Date: Wed, 9 Jul 2025 01:05:44 -0600 Subject: [PATCH] deduplicate and simplify --- .../applications/sheets/actors/character.mjs | 31 +++++-------------- module/applications/sheets/items/armor.mjs | 13 ++------ module/applications/sheets/items/weapon.mjs | 13 ++------ module/helpers/attachmentHelper.mjs | 24 ++++++++++++++ 4 files changed, 37 insertions(+), 44 deletions(-) diff --git a/module/applications/sheets/actors/character.mjs b/module/applications/sheets/actors/character.mjs index db499954..2b0d4b09 100644 --- a/module/applications/sheets/actors/character.mjs +++ b/module/applications/sheets/actors/character.mjs @@ -656,31 +656,14 @@ export default class CharacterSheet extends DHBaseActorSheet { } async _onDragStart(event) { - const target = event.target || event.currentTarget; - const itemElement = target.closest('[data-item-id]'); + const item = this.getItem(event); - if (itemElement) { - const itemId = itemElement.dataset.itemId; - const itemType = itemElement.dataset.type; - - let item; - if (itemType === 'effect') { - item = this.document.effects.get(itemId); - } else { - item = this.document.items.get(itemId); - } - - if (item) { - const dragData = { - type: item.documentName, - uuid: item.uuid - }; - - event.dataTransfer.setData('text/plain', JSON.stringify(dragData)); - - - } - } + const dragData = { + type: item.documentName, + uuid: item.uuid + }; + + event.dataTransfer.setData('text/plain', JSON.stringify(dragData)); super._onDragStart(event); } diff --git a/module/applications/sheets/items/armor.mjs b/module/applications/sheets/items/armor.mjs index 15d6b9e5..55902b5f 100644 --- a/module/applications/sheets/items/armor.mjs +++ b/module/applications/sheets/items/armor.mjs @@ -1,5 +1,5 @@ import DHBaseItemSheet from '../api/base-item.mjs'; -import { copyAttachmentEffectsToActor, removeAttachmentEffectsFromActor } from '../../../helpers/attachmentHelper.mjs'; +import { copyAttachmentEffectsToActor, removeAttachmentFromItem } from '../../../helpers/attachmentHelper.mjs'; export default class ArmorSheet extends DHBaseItemSheet { /**@inheritdoc */ @@ -129,16 +129,9 @@ export default class ArmorSheet extends DHBaseItemSheet { * @param {HTMLElement} target - The clicked element */ static async #removeAttachment(event, target) { - const uuid = target.dataset.uuid; - const currentAttached = this.document.system.attached; - - await this.document.update({ - 'system.attached': currentAttached.filter(attachedUuid => attachedUuid !== uuid) - }); - - await removeAttachmentEffectsFromActor({ + await removeAttachmentFromItem({ parentItem: this.document, - attachedUuid: uuid, + attachedUuid: target.dataset.uuid, parentType: 'armor' }); } diff --git a/module/applications/sheets/items/weapon.mjs b/module/applications/sheets/items/weapon.mjs index a5ba1b7e..a07fad88 100644 --- a/module/applications/sheets/items/weapon.mjs +++ b/module/applications/sheets/items/weapon.mjs @@ -1,5 +1,5 @@ import DHBaseItemSheet from '../api/base-item.mjs'; -import { copyAttachmentEffectsToActor, removeAttachmentEffectsFromActor } from '../../../helpers/attachmentHelper.mjs'; +import { copyAttachmentEffectsToActor, removeAttachmentFromItem } from '../../../helpers/attachmentHelper.mjs'; export default class WeaponSheet extends DHBaseItemSheet { /**@inheritdoc */ @@ -130,16 +130,9 @@ export default class WeaponSheet extends DHBaseItemSheet { * @param {HTMLElement} target - The clicked element */ static async #removeAttachment(event, target) { - const uuid = target.dataset.uuid; - const currentAttached = this.document.system.attached; - - await this.document.update({ - 'system.attached': currentAttached.filter(attachedUuid => attachedUuid !== uuid) - }); - - await removeAttachmentEffectsFromActor({ + await removeAttachmentFromItem({ parentItem: this.document, - attachedUuid: uuid, + attachedUuid: target.dataset.uuid, parentType: 'weapon' }); } diff --git a/module/helpers/attachmentHelper.mjs b/module/helpers/attachmentHelper.mjs index 9513a385..b378a815 100644 --- a/module/helpers/attachmentHelper.mjs +++ b/module/helpers/attachmentHelper.mjs @@ -72,6 +72,30 @@ export async function removeAttachmentEffectsFromActor({ parentItem, attachedUui } } +/** + * Remove an attachment from an item and clean up its effects + * @param {Object} options - Configuration options + * @param {Item} options.parentItem - The item (armor/weapon) that the item is attached to + * @param {string} options.attachedUuid - UUID of the attached item being removed + * @param {string} options.parentType - Type of parent item ("armor" or "weapon") + * @returns {Promise} + */ +export async function removeAttachmentFromItem({ parentItem, attachedUuid, parentType }) { + const currentAttached = parentItem.system.attached; + + // Remove the attachment from the parent item's attached array + await parentItem.update({ + 'system.attached': currentAttached.filter(uuid => uuid !== attachedUuid) + }); + + // Remove any effects that came from this attachment + await removeAttachmentEffectsFromActor({ + parentItem, + attachedUuid, + parentType + }); +} + /** * Handle adding/removing attachment effects when a parent item is equipped/unequipped * @param {Object} options - Configuration options