REFACTOR:

now prepare description onRender and simply the other methods
This commit is contained in:
Joaquin Pereyra 2025-08-12 17:33:17 -03:00
parent 7318b7b6da
commit 69f0106139

View file

@ -149,6 +149,7 @@ export default function DHApplicationMixin(Base) {
async _onRender(context, options) { async _onRender(context, options) {
await super._onRender(context, options); await super._onRender(context, options);
this._createTagifyElements(this.options.tagifyConfigs); this._createTagifyElements(this.options.tagifyConfigs);
await this.#prepareInventoryDescription();
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
@ -162,13 +163,7 @@ export default function DHApplicationMixin(Base) {
const { actionId, itemUuid } = el.parentElement.dataset; const { actionId, itemUuid } = el.parentElement.dataset;
const selector = `${actionId ? `[data-action-id="${actionId}"]` : `[data-item-uuid="${itemUuid}"]`} .extensible`; const selector = `${actionId ? `[data-action-id="${actionId}"]` : `[data-item-uuid="${itemUuid}"]`} .extensible`;
const newExtensible = newElement.querySelector(selector); const newExtensible = newElement.querySelector(selector);
newExtensible?.classList.add('extended');
if (!newExtensible) continue;
newExtensible.classList.add('extended');
const descriptionElement = newExtensible.querySelector('.invetory-description');
if (descriptionElement) {
this.#prepareInventoryDescription(newExtensible, descriptionElement);
}
} }
} }
@ -407,24 +402,28 @@ export default function DHApplicationMixin(Base) {
/** /**
* Prepares and enriches an inventory item or action description for display. * Prepares and enriches an inventory item or action description for display.
* @param {HTMLElement} extensibleElement - The parent element containing the description.
* @param {HTMLElement} descriptionElement - The element where the enriched description will be rendered.
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async #prepareInventoryDescription(extensibleElement, descriptionElement) { async #prepareInventoryDescription() {
const parent = extensibleElement.closest('[data-item-uuid], [data-action-id]'); // Get all inventory item elements with a data-item-uuid attribute
const { actionId, itemUuid } = parent?.dataset || {}; const inventoryItems = this.element.querySelectorAll('.inventory-item[data-item-uuid]');
if (!actionId && !itemUuid) return; for (const el of inventoryItems) {
// Get the doc uuid from the element
const { itemUuid } = el?.dataset || {};
if (!itemUuid) continue;
const doc = itemUuid //get doc by uuid
? await getDocFromElement(extensibleElement) const doc = await fromUuid(itemUuid);
: this.document.system.attack?.id === actionId
? this.document.system.attack
: this.document.system.actions?.get(actionId);
if (!doc) return;
//get inventory-item description element
const descriptionElement = el.querySelector('.invetory-description');
if (!doc || !descriptionElement) continue;
// localize the description (idk if it's still necessary)
const description = game.i18n.localize(doc.system?.description ?? doc.description); const description = game.i18n.localize(doc.system?.description ?? doc.description);
const isAction = !!actionId;
// Enrich the description and attach it;
const isAction = doc.documentName === 'Action';
descriptionElement.innerHTML = await foundry.applications.ux.TextEditor.implementation.enrichHTML( descriptionElement.innerHTML = await foundry.applications.ux.TextEditor.implementation.enrichHTML(
description, description,
{ {
@ -434,14 +433,13 @@ export default function DHApplicationMixin(Base) {
} }
); );
} }
}
/* -------------------------------------------- */ /* -------------------------------------------- */
/* Application Clicks Actions */ /* Application Clicks Actions */
/* -------------------------------------------- */ /* -------------------------------------------- */
static async #addNewItem(event, target) { static async #addNewItem(event, target) {
const { type } = target.dataset;
const createChoice = await foundry.applications.api.DialogV2.wait({ const createChoice = await foundry.applications.api.DialogV2.wait({
classes: ['dh-style', 'two-big-buttons'], classes: ['dh-style', 'two-big-buttons'],
buttons: [ buttons: [
@ -609,10 +607,7 @@ export default function DHApplicationMixin(Base) {
static async #toggleExtended(_, target) { static async #toggleExtended(_, target) {
const container = target.closest('.inventory-item'); const container = target.closest('.inventory-item');
const extensible = container?.querySelector('.extensible'); const extensible = container?.querySelector('.extensible');
const t = extensible?.classList.toggle('extended'); extensible?.classList.toggle('extended');
const descriptionElement = extensible?.querySelector('.invetory-description');
if (t && !!descriptionElement) await this.#prepareInventoryDescription(extensible, descriptionElement);
} }
} }