From dda38b66959bec75794caf056ad21202164d96c6 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Sun, 26 Nov 2023 01:30:15 -0700 Subject: [PATCH] Make it so that Handlebars partials get reloaded automatically --- module/dotdungeon.js | 10 +++++++--- module/handlebars.js | 16 ++++++++-------- module/hooks/hotReload.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 module/hooks/hotReload.js diff --git a/module/dotdungeon.js b/module/dotdungeon.js index 7175981..09a1990 100644 --- a/module/dotdungeon.js +++ b/module/dotdungeon.js @@ -1,11 +1,15 @@ +// Class imports import { CharacterActor } from "./documents/CharacterActor.js"; import { CharacterSheet } from "./sheets/CharacterSheet.js"; +// Utility imports import * as hbs from "./handlebars.js"; -// import diceChoice from "../templates/actors/char-sheet-mvp/partials/dice_choice.hbs?raw"; +// Non-Setup hooks +import "./hooks/hotReload.js"; -Hooks.once(`init`, async function () { + +Hooks.once(`init`, async () => { game.boilerplate = { CharacterActor, }; @@ -20,6 +24,6 @@ Hooks.once(`init`, async function () { }); -Hooks.once(`ready`, function() { +Hooks.once(`ready`, () => { console.info(".dungeon | Ready"); }); \ No newline at end of file diff --git a/module/handlebars.js b/module/handlebars.js index 81af235..c6a9862 100644 --- a/module/handlebars.js +++ b/module/handlebars.js @@ -1,3 +1,10 @@ +export const partials = [ + "actors/char-sheet-mvp/partials/dice_choice.hbs", + "actors/char-sheet-mvp/partials/stat.hbs", + "actors/char-sheet-mvp/partials/skill.hbs", + "actors/char-sheet-mvp/partials/panel.hbs", +] + export async function registerHandlebarsHelpers() { Handlebars.registerHelper({ "dotdungeon-array": createArray @@ -7,20 +14,13 @@ export async function registerHandlebarsHelpers() { export async function preloadHandlebarsTemplates() { console.groupCollapsed(`.dungeon | Handlebars template loading`) const pathPrefix = `systems/dotdungeon/templates/`; - const partials = [ - "actors/char-sheet-mvp/partials/dice_choice.hbs", - "actors/char-sheet-mvp/partials/stat.hbs", - "actors/char-sheet-mvp/partials/skill.hbs", - "actors/char-sheet-mvp/partials/panel.hbs", - ]; const paths = {}; for ( const partial of partials ) { console.debug(`Loading partial: ${partial}`); const path = `${pathPrefix}${partial}`; - paths[path] = path; - paths[`dotdungeon.${path.split("/").pop().replace(".hbs", "")}`] = path; + paths[`dotdungeon.${partial.split("/").pop().replace(".hbs", "")}`] = path; } console.debug(`Loaded ${partials.length} partials`); diff --git a/module/hooks/hotReload.js b/module/hooks/hotReload.js new file mode 100644 index 0000000..7d582d3 --- /dev/null +++ b/module/hooks/hotReload.js @@ -0,0 +1,30 @@ +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 templateName = `dotdungeon.${data.path.split("/").pop().replace(".hbs", "")}`; + Handlebars.registerPartial(templateName, template); + _templateCache[templateName] = template; + + // Re-render open windows + for (const window of ui.windows) { + window.render(true); + }; + + return false; +}); \ No newline at end of file