Load icons during initial setup and allow hot-reloading icons

This commit is contained in:
Oliver-Akins 2024-01-31 18:34:24 -07:00
parent b9ed3289dc
commit a657b2e3e8
6 changed files with 49 additions and 44 deletions

View file

@ -29,7 +29,7 @@ import loadSettings from "./settings/index.mjs";
import DOTDUNGEON from "./config.mjs"; import DOTDUNGEON from "./config.mjs";
Hooks.once(`init`, () => { Hooks.once(`init`, async () => {
console.debug(`.dungeon | Initializing`); console.debug(`.dungeon | Initializing`);
loadSettings(); loadSettings();
@ -76,10 +76,14 @@ Hooks.once(`init`, () => {
makeDefault: true, makeDefault: true,
types: ["pet"], types: ["pet"],
lable: "dotdungeon.sheet-names.PetSheet" lable: "dotdungeon.sheet-names.PetSheet"
}) });
hbs.registerHandlebarsHelpers(); hbs.registerHandlebarsHelpers();
hbs.preloadHandlebarsTemplates(); hbs.preloadHandlebarsTemplates();
CONFIG.CACHE = {};
CONFIG.CACHE.icons = await hbs.preloadIcons();
}); });

View file

@ -78,7 +78,6 @@ export async function preloadHandlebarsTemplates() {
* displaying the icon * displaying the icon
*/ */
export async function preloadIcons() { export async function preloadIcons() {
console.groupCollapsed(`.dungeon | Loading icons for handlebars`);
const pathPrefix = `systems/dotdungeon/assets/` const pathPrefix = `systems/dotdungeon/assets/`
const parsedIcons = {}; const parsedIcons = {};
@ -90,21 +89,18 @@ export async function preloadIcons() {
if (response.status !== 200) { continue }; if (response.status !== 200) { continue };
const svgData = await response.text(); const svgData = await response.text();
parsedIcons[iconName] = svgData; parsedIcons[iconName] = svgData;
console.debug(`Loaded icon: ${icon}`);
} catch { } catch {
console.error(`Failed to fetch/parse icon: ${icon}`); console.error(`.dungeon | Failed to fetch/parse icon: ${icon}`);
continue; continue;
}; };
} }
else if (icon.endsWith(`.png`)) { else if (icon.endsWith(`.png`)) {
parsedIcons[iconName] = `<img alt="" src="${pathPrefix}${icon}">`; parsedIcons[iconName] = `<img alt="" src="${pathPrefix}${icon}">`;
console.debug(`Loaded icon: ${icon}`);
} }
else { else {
console.warn(`Icon "${icon}" failed to be handled by a loader`) console.warn(`.dungeon | Icon "${icon}" failed to be handled by a loader`)
}; };
}; };
console.groupEnd();
return parsedIcons; return parsedIcons;
}; };

View file

@ -1,15 +1,16 @@
import * as hbs from "../handlebars.mjs"; import * as hbs from "../handlebars.mjs";
Hooks.on(`hotReload`, async (data) => { const loaders = {
if (data.extension !== 'hbs') { svg(data) {
return true; const iconName = data.path.split(`/`).slice(-1)[0].slice(0, -4);
}; console.log(`.dungeon | hot-reloading icon: ${iconName}`);
CONFIG.CACHE.icons[iconName] = data.content;
},
hbs(data) {
if (!hbs.partials.some(p => data.path.endsWith(p))) { if (!hbs.partials.some(p => data.path.endsWith(p))) {
return true; return true;
}; };
// Compile the new template data. // Compile the new template data.
let template; let template;
try { try {
@ -31,4 +32,10 @@ Hooks.on(`hotReload`, async (data) => {
_templateCache[templateName] = template; _templateCache[templateName] = template;
return false; return false;
},
};
Hooks.on(`hotReload`, async (data) => {
if (!loaders[data.extension]) return;
return loaders[data.extension](data);
}); });

View file

@ -1,5 +1,4 @@
import DOTDUNGEON from "../config.mjs"; import DOTDUNGEON from "../config.mjs";
import { preloadIcons } from "../handlebars.mjs";
export class GenericActorSheet extends ActorSheet { export class GenericActorSheet extends ActorSheet {
_expanded = new Set(); _expanded = new Set();
@ -29,7 +28,7 @@ export class GenericActorSheet extends ActorSheet {
ctx.actor = this.actor; ctx.actor = this.actor;
ctx.config = DOTDUNGEON; ctx.config = DOTDUNGEON;
ctx.icons = await preloadIcons(); ctx.icons = CONFIG.CACHE.icons;
return ctx; return ctx;
}; };

View file

@ -1,5 +1,4 @@
import DOTDUNGEON from "../config.mjs"; import DOTDUNGEON from "../config.mjs";
import { preloadIcons } from "../handlebars.mjs";
export class GenericItemSheet extends ItemSheet { export class GenericItemSheet extends ItemSheet {
_expanded = new Set(); _expanded = new Set();
@ -35,7 +34,7 @@ export class GenericItemSheet extends ItemSheet {
}; };
ctx.config = DOTDUNGEON; ctx.config = DOTDUNGEON;
ctx.icons = await preloadIcons(); ctx.icons = CONFIG.CACHE.icons;
return ctx; return ctx;
}; };

View file

@ -32,8 +32,8 @@
], ],
"flags": { "flags": {
"hotReload": { "hotReload": {
"extensions": ["css", "hbs", "json", "js", "mjs"], "extensions": ["css", "hbs", "json", "js", "mjs", "svg"],
"paths": ["templates", "langs", ".styles", "module"] "paths": ["templates", "langs", ".styles", "module", "assets"]
} }
} }
} }