Make it so that Handlebars partials get reloaded automatically

This commit is contained in:
Oliver-Akins 2023-11-26 01:30:15 -07:00
parent 93b55df045
commit dda38b6695
3 changed files with 45 additions and 11 deletions

View file

@ -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");
});

View file

@ -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`);

30
module/hooks/hotReload.js Normal file
View file

@ -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;
});