From 0b5c4f5946e017ece250a4906b25891d61af32fa Mon Sep 17 00:00:00 2001 From: Andrew Fryer <3588046+flamewave000@users.noreply.github.com> Date: Sat, 22 Feb 2025 18:43:41 -0500 Subject: [PATCH] flag-edit: Improve document lookup from UUID --- df-flag-edit/CHANGELOG.md | 3 +++ df-flag-edit/module.json | 8 +++--- df-flag-edit/src/FlagEditor.mjs | 42 +++---------------------------- df-flag-edit/src/df-flag-edit.mjs | 5 ++-- 4 files changed, 13 insertions(+), 45 deletions(-) diff --git a/df-flag-edit/CHANGELOG.md b/df-flag-edit/CHANGELOG.md index da1eeeb..9ebafc6 100644 --- a/df-flag-edit/CHANGELOG.md +++ b/df-flag-edit/CHANGELOG.md @@ -1,5 +1,8 @@ # DragonFlagon Flag Editor +## Release 2.0.1 (2025-02-22) +- **UPDATE:** Now using built in `fromUuid` and `fromUuidSync` helper functions to more reliably lookup documents and better support embedded documents which previously had issues. + ## Release 2.0.0 (2025-02-13) - **UPDATE:** Migrated to v12. - **UPDATE:** Downgraded TS -> JS (such sad). diff --git a/df-flag-edit/module.json b/df-flag-edit/module.json index 32f2052..519a147 100644 --- a/df-flag-edit/module.json +++ b/df-flag-edit/module.json @@ -1,15 +1,13 @@ { "id": "df-flag-edit", "title": "DF Flags Editor", - "version": "2.0.0", + "version": "2.0.1", "description": "FoundryVTT Document Flags Editor", "authors": [ { "name": "flamewave000", "discord": "flamewave000", "url": "https://github.com/flamewave000" } ], "compatibility": { "minimum": 12, "verified": 12.331 }, "esmodules": "{{sources}}", "styles": "{{css}}", - "languages": [ - { "lang": "en", "name": "English", "path": "lang/en.json" } - ], + "languages": [{ "lang": "en", "name": "English", "path": "lang/en.json" }], "url": "https://dragonflagon.cafe/mods/url/df-flag-edit", "license": "https://dragonflagon.cafe/mods/lic/df-flag-edit", "manifest": "https://dragonflagon.cafe/mods/man/df-flag-edit", @@ -19,7 +17,7 @@ "bugs": "https://dragonflagon.cafe/mods/bug/df-flag-edit", "manifestPlusVersion": "1.0.0", "media": [ - { "type": "cover", "url": "https://raw.githubusercontent.com/flamewave000/dragonflagon-fvtt/master/.assets/cover.png" }, + { "type": "cover", "url": "https://raw.githubusercontent.com/flamewave000/dragonflagon-fvtt/master/.assets/df-flag-edit/cover.png" }, { "type": "icon", "url": "https://raw.githubusercontent.com/flamewave000/dragonflagon-fvtt/master/.assets/logo.png" } ] } diff --git a/df-flag-edit/src/FlagEditor.mjs b/df-flag-edit/src/FlagEditor.mjs index a79cef8..a7e850b 100644 --- a/df-flag-edit/src/FlagEditor.mjs +++ b/df-flag-edit/src/FlagEditor.mjs @@ -19,33 +19,6 @@ export default class FlagEditor extends Application { }); } - /** - * @param {string} id - * @returns {Promise} - */ - static async _findObject(id) { - if (typeof id !== 'string' && !(id instanceof String)) return Promise.reject("Invalid parameter: id must be type 'string'"); - return await new Promise((res, rej) => { - // Search the collections - const collections = game.collections; - for (const [key, map] of collections.entries()) { - if (FlagEditor.IGNORED_COLLECTIONS.includes(key) || !map.has(id)) continue; - res(map.get(id)); - break; - } - rej(); - }).catch(() => new Promise((res, rej) => { - // Search the layers - for (const layer of canvas.layers) { - if (!(layer instanceof PlaceablesLayer)) continue; - if (!layer.documentCollection.has(id)) continue; - res(layer.documentCollection.get(id)); - break; - } - rej(); - })).catch(() => Promise.resolve(null)); - } - static init() { SETTINGS.register(FlagEditor.PREF_LAST_OBJ, { scope: 'client', type: String, default: '', config: false }); Hooks.on('renderSettings', (/**@type {Settings}*/ _, /**@type {JQuery}*/ html) => { @@ -221,7 +194,7 @@ export default class FlagEditor extends Application { try { // If we are an ID if (FlagEditor.isID(data)) - document = await FlagEditor.extractID(data); + document = await fromUuid(data); // we are an object path else { let temp = FlagEditor.evaluateDocument(data); @@ -229,7 +202,7 @@ export default class FlagEditor extends Application { if (!FlagEditor.isDocument(temp)) { // If the Object Path result is an ID if ((temp instanceof String || typeof temp === 'string') && FlagEditor.isID(temp)) { - temp = await FlagEditor.extractID(temp); + temp = await fromUuid(temp); } else throw 'Invalid object from path'; } document = temp; @@ -253,7 +226,7 @@ export default class FlagEditor extends Application { * @returns {boolean} */ static isID(target) { - return /^['"`]?[a-z0-9]+['"`]?$/im.test(target); + return !!fromUuidSync(target); } /** * @param {any} target @@ -262,13 +235,6 @@ export default class FlagEditor extends Application { static isDocument(target) { return target?.data !== undefined || target?.document !== undefined; } - /** - * @param {string} target - * @returns {Promise} - */ - static extractID(target) { - return FlagEditor._findObject(target.match(/^['"`]?([a-z0-9]+)['"`]?$/im)[1]); - } /** * @param {string} target * @returns {FoundryDocument | string | null} @@ -281,7 +247,7 @@ window.showFlagEditorForDocument = async (document) => { if (document.data === undefined) { if (document.document === undefined) { if (document instanceof String || typeof document === 'string') { - document = FlagEditor._findObject(document); + document = await fromUuid(document); } else throw Error("Invalid object: document must be of type 'string', 'Document', or 'DocumentData'"); } diff --git a/df-flag-edit/src/df-flag-edit.mjs b/df-flag-edit/src/df-flag-edit.mjs index 04e9c2c..0d4232c 100644 --- a/df-flag-edit/src/df-flag-edit.mjs +++ b/df-flag-edit/src/df-flag-edit.mjs @@ -9,9 +9,10 @@ Hooks.once('init', function () { }); function launchFlagEditorForEvent(/**@type {JQuery.ClickEvent}*/event) { - const id = $(event.currentTarget).parents('.window-app').attr('id').split('-').pop(); + const id = $(event.currentTarget).parents('.window-app').attr('id').split('-'); + id.shift(); if (!id) return; - SETTINGS.set(FlagEditor.PREF_LAST_OBJ, id); + SETTINGS.set(FlagEditor.PREF_LAST_OBJ, id.join('.')); new FlagEditor().render(true); } Hooks.once('ready', function () {