From a657b2e3e8ea236d6020454cfb96194b7a35c1d6 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Wed, 31 Jan 2024 18:34:24 -0700 Subject: [PATCH] Load icons during initial setup and allow hot-reloading icons --- module/dotdungeon.mjs | 8 +++- module/handlebars.mjs | 8 +--- module/hooks/hotReload.mjs | 67 ++++++++++++++++------------- module/sheets/GenericActorSheet.mjs | 3 +- module/sheets/GenericItemSheet.mjs | 3 +- system.json | 4 +- 6 files changed, 49 insertions(+), 44 deletions(-) diff --git a/module/dotdungeon.mjs b/module/dotdungeon.mjs index 49c6374..4784205 100644 --- a/module/dotdungeon.mjs +++ b/module/dotdungeon.mjs @@ -29,7 +29,7 @@ import loadSettings from "./settings/index.mjs"; import DOTDUNGEON from "./config.mjs"; -Hooks.once(`init`, () => { +Hooks.once(`init`, async () => { console.debug(`.dungeon | Initializing`); loadSettings(); @@ -76,10 +76,14 @@ Hooks.once(`init`, () => { makeDefault: true, types: ["pet"], lable: "dotdungeon.sheet-names.PetSheet" - }) + }); + hbs.registerHandlebarsHelpers(); hbs.preloadHandlebarsTemplates(); + + CONFIG.CACHE = {}; + CONFIG.CACHE.icons = await hbs.preloadIcons(); }); diff --git a/module/handlebars.mjs b/module/handlebars.mjs index 8cec531..32eec11 100644 --- a/module/handlebars.mjs +++ b/module/handlebars.mjs @@ -78,7 +78,6 @@ export async function preloadHandlebarsTemplates() { * displaying the icon */ export async function preloadIcons() { - console.groupCollapsed(`.dungeon | Loading icons for handlebars`); const pathPrefix = `systems/dotdungeon/assets/` const parsedIcons = {}; @@ -90,21 +89,18 @@ export async function preloadIcons() { if (response.status !== 200) { continue }; const svgData = await response.text(); parsedIcons[iconName] = svgData; - console.debug(`Loaded icon: ${icon}`); } catch { - console.error(`Failed to fetch/parse icon: ${icon}`); + console.error(`.dungeon | Failed to fetch/parse icon: ${icon}`); continue; }; } else if (icon.endsWith(`.png`)) { parsedIcons[iconName] = ``; - console.debug(`Loaded icon: ${icon}`); } 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; }; \ No newline at end of file diff --git a/module/hooks/hotReload.mjs b/module/hooks/hotReload.mjs index ee4d283..c2370ee 100644 --- a/module/hooks/hotReload.mjs +++ b/module/hooks/hotReload.mjs @@ -1,34 +1,41 @@ import * as hbs from "../handlebars.mjs"; +const loaders = { + svg(data) { + 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))) { + return true; + }; + + // Compile the new template data. + let template; + try { + template = Handlebars.compile(data.content); + } catch (err) { + return console.error(err); + }; + + // Re-register our new partial template & cache it. + const alias = data.path + .split(`/`) + .pop() + .split(`.`) + .slice(0, -1) + .reverse() + .join(`.`); + const templateName = `dotdungeon.${alias}`; + Handlebars.registerPartial(templateName, template); + _templateCache[templateName] = template; + + return false; + }, +}; + Hooks.on(`hotReload`, async (data) => { - if (data.extension !== 'hbs') { - return true; - }; - - if (!hbs.partials.some(p => data.path.endsWith(p))) { - return true; - }; - - - // Compile the new template data. - let template; - try { - template = Handlebars.compile(data.content); - } catch (err) { - return console.error(err); - }; - - // Re-register our new partial template & cache it. - const alias = data.path - .split(`/`) - .pop() - .split(`.`) - .slice(0, -1) - .reverse() - .join(`.`); - const templateName = `dotdungeon.${alias}`; - Handlebars.registerPartial(templateName, template); - _templateCache[templateName] = template; - - return false; + if (!loaders[data.extension]) return; + return loaders[data.extension](data); }); \ No newline at end of file diff --git a/module/sheets/GenericActorSheet.mjs b/module/sheets/GenericActorSheet.mjs index 1dcc4c8..80818d7 100644 --- a/module/sheets/GenericActorSheet.mjs +++ b/module/sheets/GenericActorSheet.mjs @@ -1,5 +1,4 @@ import DOTDUNGEON from "../config.mjs"; -import { preloadIcons } from "../handlebars.mjs"; export class GenericActorSheet extends ActorSheet { _expanded = new Set(); @@ -29,7 +28,7 @@ export class GenericActorSheet extends ActorSheet { ctx.actor = this.actor; ctx.config = DOTDUNGEON; - ctx.icons = await preloadIcons(); + ctx.icons = CONFIG.CACHE.icons; return ctx; }; diff --git a/module/sheets/GenericItemSheet.mjs b/module/sheets/GenericItemSheet.mjs index 7299004..098cc5b 100644 --- a/module/sheets/GenericItemSheet.mjs +++ b/module/sheets/GenericItemSheet.mjs @@ -1,5 +1,4 @@ import DOTDUNGEON from "../config.mjs"; -import { preloadIcons } from "../handlebars.mjs"; export class GenericItemSheet extends ItemSheet { _expanded = new Set(); @@ -35,7 +34,7 @@ export class GenericItemSheet extends ItemSheet { }; ctx.config = DOTDUNGEON; - ctx.icons = await preloadIcons(); + ctx.icons = CONFIG.CACHE.icons; return ctx; }; diff --git a/system.json b/system.json index f32c11b..df3a65a 100644 --- a/system.json +++ b/system.json @@ -32,8 +32,8 @@ ], "flags": { "hotReload": { - "extensions": ["css", "hbs", "json", "js", "mjs"], - "paths": ["templates", "langs", ".styles", "module"] + "extensions": ["css", "hbs", "json", "js", "mjs", "svg"], + "paths": ["templates", "langs", ".styles", "module", "assets"] } } } \ No newline at end of file