Skip to content

Commit

Permalink
flag-edit: Improve document lookup from UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
flamewave000 committed Feb 23, 2025
1 parent 40c0c39 commit 0b5c4f5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 45 deletions.
3 changes: 3 additions & 0 deletions df-flag-edit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
8 changes: 3 additions & 5 deletions df-flag-edit/module.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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" }
]
}
42 changes: 4 additions & 38 deletions df-flag-edit/src/FlagEditor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,6 @@ export default class FlagEditor extends Application {
});
}

/**
* @param {string} id
* @returns {Promise<FoundryDocument | null>}
*/
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<HTMLElement>}*/ html) => {
Expand Down Expand Up @@ -221,15 +194,15 @@ 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);
// If the result is NOT a Document/Data
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;
Expand All @@ -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
Expand All @@ -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<FoundryDocument | null>}
*/
static extractID(target) {
return FlagEditor._findObject(target.match(/^['"`]?([a-z0-9]+)['"`]?$/im)[1]);
}
/**
* @param {string} target
* @returns {FoundryDocument | string | null}
Expand All @@ -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'");
}
Expand Down
5 changes: 3 additions & 2 deletions df-flag-edit/src/df-flag-edit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 0b5c4f5

Please sign in to comment.