Fix issue #57: Sync foundry folder colors
Some checks failed
Test / test (push) Failing after 2s
Some checks failed
Test / test (push) Failing after 2s
This commit is contained in:
@@ -33,6 +33,7 @@ let itemSyncErrorLogged = false;
|
||||
let folderSyncTimeout;
|
||||
let folderSyncRunning = false;
|
||||
const syncingItemIds = new Set();
|
||||
const syncingFolderIds = new Set();
|
||||
const queuedItemChanges = new Map();
|
||||
|
||||
function getCollectionContents(collection, collectionName) {
|
||||
@@ -675,10 +676,23 @@ function isWorldItemFolder(folder, seen = new Set()) {
|
||||
return isWorldItemFolder(parent, seen);
|
||||
}
|
||||
|
||||
// A Foundry folder's colour, or null for one the GM left uncoloured. Null travels as null:
|
||||
// it tells Dragonroll to take a colour off the folder it was given before, whereas a module
|
||||
// that says nothing about colours at all leaves the campaign's own choice alone.
|
||||
function itemFolderColor(folder) {
|
||||
const color = typeof folder?.color === "string" ? folder.color.trim() : "";
|
||||
return color || null;
|
||||
}
|
||||
|
||||
function worldItemFolderSnapshots() {
|
||||
return (game.folders?.contents || [])
|
||||
.filter((folder) => isWorldItemFolder(folder))
|
||||
.map((folder) => ({ id: folder.id, name: folder.name, parent: itemFolderParent(folder)?.id || null }));
|
||||
.map((folder) => ({
|
||||
id: folder.id,
|
||||
name: folder.name,
|
||||
parent: itemFolderParent(folder)?.id || null,
|
||||
color: itemFolderColor(folder),
|
||||
}));
|
||||
}
|
||||
|
||||
// A world item travels with the folder it stands in, so Dragonroll can file it the
|
||||
@@ -706,6 +720,35 @@ function getWorldItemSnapshots() {
|
||||
return (game.items?.contents || []).map(serializeFoundryWorldItem).filter(Boolean);
|
||||
}
|
||||
|
||||
// --- Folder colours ----------------------------------------------------------
|
||||
// Colouring a folder is the one thing about the tree the campaign gets to do on its
|
||||
// own: the world owns what the folders are called and how they nest, and Dragonroll
|
||||
// owns the colour its user picks for one. So the colour travels back with the folder
|
||||
// list the item reconcile reads, and lands on the world's folder — marked, like every
|
||||
// Dragonroll write, so the world does not echo it straight back to the campaign.
|
||||
|
||||
async function withFolderSyncSuppressed(folder, callback) {
|
||||
if (!folder?.id) return callback();
|
||||
syncingFolderIds.add(folder.id);
|
||||
try {
|
||||
return await callback();
|
||||
} finally {
|
||||
syncingFolderIds.delete(folder.id);
|
||||
}
|
||||
}
|
||||
|
||||
async function applyDragonrollFolderColors(entries) {
|
||||
for (const entry of entries || []) {
|
||||
const folder = entry?.foundryFolderId ? game.folders?.get(entry.foundryFolderId) : null;
|
||||
// Only the world's item directories: a Dragonroll folder the world has no document
|
||||
// for is the campaign's business alone, and making one there is not ours to do.
|
||||
if (!folder || folder.documentName !== "Folder" || !isWorldItemFolder(folder)) continue;
|
||||
const color = entry.color || null;
|
||||
if (itemFolderColor(folder) === color) continue;
|
||||
await withFolderSyncSuppressed(folder, () => folder.update({ color }));
|
||||
}
|
||||
}
|
||||
|
||||
function dragonrollItemIdOf(item) {
|
||||
return item.getFlag?.(MODULE_ID, "itemId") || item.flags?.[MODULE_ID]?.itemId || "";
|
||||
}
|
||||
@@ -916,10 +959,13 @@ async function reconcileDragonrollItems() {
|
||||
if (!token || !game.user?.isGM || !game.world || itemSyncRunning || !isAutoSyncEnabled()) return;
|
||||
itemSyncRunning = true;
|
||||
try {
|
||||
const result = await getDragonroll("foundry/sync/items", token);
|
||||
// The folder colours are applied first, and whatever system this world runs: a
|
||||
// colour is the same on every one of them, while writing an item is what needs D&D 5e.
|
||||
await applyDragonrollFolderColors(result.folders);
|
||||
if (game.system.id !== "dnd5e") {
|
||||
throw new Error("Item sync currently requires the D&D 5e Foundry system.");
|
||||
}
|
||||
const result = await getDragonroll("foundry/sync/items", token);
|
||||
const remoteItems = result.items || [];
|
||||
const remoteById = new Map(remoteItems.map((entry) => [String(entry._id), entry]));
|
||||
const localByRemoteId = new Map(game.items.contents
|
||||
@@ -1347,7 +1393,9 @@ Hooks.once("ready", () => {
|
||||
}
|
||||
// An item folder the GM made, renamed or nested changes the tree the campaign
|
||||
// mirrors; nothing about the items themselves changed, so only the tree goes.
|
||||
// A colour Dragonroll has just written into the folder is not the GM changing it.
|
||||
if (document.documentName === "Folder") {
|
||||
if (syncingFolderIds.has(document.id)) return;
|
||||
if (document.type === "Item" && isWorldItemFolder(document)) queueFoundryItemFolders();
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user