Load icons during initial setup and allow hot-reloading icons
This commit is contained in:
parent
b9ed3289dc
commit
a657b2e3e8
6 changed files with 49 additions and 44 deletions
|
|
@ -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();
|
||||
});
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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] = `<img alt="" src="${pathPrefix}${icon}">`;
|
||||
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;
|
||||
};
|
||||
|
|
@ -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);
|
||||
});
|
||||
|
|
@ -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;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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"]
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue