Make it so that Handlebars partials get reloaded automatically
This commit is contained in:
parent
93b55df045
commit
dda38b6695
3 changed files with 45 additions and 11 deletions
|
|
@ -1,11 +1,15 @@
|
||||||
|
// Class imports
|
||||||
import { CharacterActor } from "./documents/CharacterActor.js";
|
import { CharacterActor } from "./documents/CharacterActor.js";
|
||||||
import { CharacterSheet } from "./sheets/CharacterSheet.js";
|
import { CharacterSheet } from "./sheets/CharacterSheet.js";
|
||||||
|
|
||||||
|
// Utility imports
|
||||||
import * as hbs from "./handlebars.js";
|
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 = {
|
game.boilerplate = {
|
||||||
CharacterActor,
|
CharacterActor,
|
||||||
};
|
};
|
||||||
|
|
@ -20,6 +24,6 @@ Hooks.once(`init`, async function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
Hooks.once(`ready`, function() {
|
Hooks.once(`ready`, () => {
|
||||||
console.info(".dungeon | Ready");
|
console.info(".dungeon | Ready");
|
||||||
});
|
});
|
||||||
|
|
@ -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() {
|
export async function registerHandlebarsHelpers() {
|
||||||
Handlebars.registerHelper({
|
Handlebars.registerHelper({
|
||||||
"dotdungeon-array": createArray
|
"dotdungeon-array": createArray
|
||||||
|
|
@ -7,20 +14,13 @@ export async function registerHandlebarsHelpers() {
|
||||||
export async function preloadHandlebarsTemplates() {
|
export async function preloadHandlebarsTemplates() {
|
||||||
console.groupCollapsed(`.dungeon | Handlebars template loading`)
|
console.groupCollapsed(`.dungeon | Handlebars template loading`)
|
||||||
const pathPrefix = `systems/dotdungeon/templates/`;
|
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 = {};
|
const paths = {};
|
||||||
|
|
||||||
for ( const partial of partials ) {
|
for ( const partial of partials ) {
|
||||||
console.debug(`Loading partial: ${partial}`);
|
console.debug(`Loading partial: ${partial}`);
|
||||||
const path = `${pathPrefix}${partial}`;
|
const path = `${pathPrefix}${partial}`;
|
||||||
paths[path] = path;
|
paths[`dotdungeon.${partial.split("/").pop().replace(".hbs", "")}`] = path;
|
||||||
paths[`dotdungeon.${path.split("/").pop().replace(".hbs", "")}`] = path;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.debug(`Loaded ${partials.length} partials`);
|
console.debug(`Loaded ${partials.length} partials`);
|
||||||
|
|
|
||||||
30
module/hooks/hotReload.js
Normal file
30
module/hooks/hotReload.js
Normal 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;
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue