GetDocFromElement is now async to work with compendiums. Added GetDocFromElementSync for use in contextMenu condition cases

This commit is contained in:
WBHarry 2025-07-26 17:18:52 +02:00
parent e6bfe08d83
commit f3c48c68b9
5 changed files with 89 additions and 54 deletions

View file

@ -235,9 +235,25 @@ export const updateActorTokens = async (actor, update) => {
* @param {HTMLElement} element - The DOM element to start the search from.
* @returns {foundry.abstract.Document|null} The resolved document, or null if not found or invalid.
*/
export function getDocFromElement(element) {
export async function getDocFromElement(element) {
const target = element.closest('[data-item-uuid]');
return foundry.utils.fromUuidSync(target.dataset.itemUuid) ?? null;
return (await foundry.utils.fromUuid(target.dataset.itemUuid)) ?? null;
}
/**
* Retrieves a Foundry document associated with the nearest ancestor element
* that has a `data-item-uuid` attribute.
* @param {HTMLElement} element - The DOM element to start the search from.
* @returns {foundry.abstract.Document|null} The resolved document, or null if not found, invalid
* or in embedded compendium collection.
*/
export function getDocFromElementSync(element) {
const target = element.closest('[data-item-uuid]');
try {
return foundry.utils.fromUuidSync(target.dataset.itemUuid) ?? null;
} catch (_) {
return null;
}
}
/**