Refactor the custom helpers into their own files
This commit is contained in:
parent
af2df4f7c5
commit
0392d339db
7 changed files with 69 additions and 62 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
import helpers from "./helpers/index.mjs";
|
||||||
|
|
||||||
export const partials = [
|
export const partials = [
|
||||||
`actors/char-sheet-mvp/partials/dice_choice.hbs`,
|
`actors/char-sheet-mvp/partials/dice_choice.hbs`,
|
||||||
`actors/char-sheet-mvp/partials/stat.hbs`,
|
`actors/char-sheet-mvp/partials/stat.hbs`,
|
||||||
|
|
@ -7,13 +9,7 @@ export const partials = [
|
||||||
];
|
];
|
||||||
|
|
||||||
export async function registerHandlebarsHelpers() {
|
export async function registerHandlebarsHelpers() {
|
||||||
Handlebars.registerHelper({
|
Handlebars.registerHelper(helpers);
|
||||||
"dotdungeon-array": createArray,
|
|
||||||
"dotdungeon-toFriendlyDuration": toFriendlyDuration,
|
|
||||||
"dotdungeon-objectValue": objectValue,
|
|
||||||
"dotdungeon-stringify": v => JSON.stringify(v, null, ` `),
|
|
||||||
"dotdungeon-expanded": detailsExpanded,
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function preloadHandlebarsTemplates() {
|
export async function preloadHandlebarsTemplates() {
|
||||||
|
|
@ -31,59 +27,4 @@ export async function preloadHandlebarsTemplates() {
|
||||||
console.debug(`Loaded ${partials.length} partials`);
|
console.debug(`Loaded ${partials.length} partials`);
|
||||||
console.groupEnd();
|
console.groupEnd();
|
||||||
return loadTemplates(paths);
|
return loadTemplates(paths);
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
function createArray(...args) {
|
|
||||||
return args.slice(0, -1);
|
|
||||||
};
|
|
||||||
|
|
||||||
function objectValue(obj, keypath) {
|
|
||||||
function helper(o, k) {
|
|
||||||
let v = o[k[0]];
|
|
||||||
if (typeof v === "object") {
|
|
||||||
return helper(v, k.slice(1));
|
|
||||||
};
|
|
||||||
return v;
|
|
||||||
};
|
|
||||||
let resp = helper(obj, keypath.string.split(`.`));
|
|
||||||
return resp;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
const secondsInAMinute = 60;
|
|
||||||
const secondsInAnHour = 60 * secondsInAMinute;
|
|
||||||
/**
|
|
||||||
* Converts a duration into a more human-friendly format
|
|
||||||
* @param {number} duration The length of time in seconds
|
|
||||||
* @returns The human-friendly time string
|
|
||||||
*/
|
|
||||||
function toFriendlyDuration(duration) {
|
|
||||||
let friendly = ``;
|
|
||||||
if (duration >= secondsInAnHour) {
|
|
||||||
let hours = Math.floor(duration / secondsInAnHour);
|
|
||||||
friendly += `${hours}h`;
|
|
||||||
duration -= hours * secondsInAnHour;
|
|
||||||
};
|
|
||||||
if (duration >= secondsInAMinute) {
|
|
||||||
let minutes = Math.floor(duration / secondsInAMinute);
|
|
||||||
friendly += `${minutes}m`;
|
|
||||||
duration -= minutes * secondsInAMinute;
|
|
||||||
};
|
|
||||||
if (duration > 0) {
|
|
||||||
friendly += `${duration}s`;
|
|
||||||
};
|
|
||||||
return friendly;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if the specified collapseId is currently open, so that during re-renders
|
|
||||||
* it remains open or closed.
|
|
||||||
*
|
|
||||||
* @param {Set<string>} expanded A set indicating what collapseIds are expanded
|
|
||||||
* @param {string} collapseId The collapseId to check for
|
|
||||||
* @returns {"open"|null} The HTML insertion indicating the details is expanded
|
|
||||||
*/
|
|
||||||
function detailsExpanded(expanded, collapseId) {
|
|
||||||
return expanded.has(collapseId) ? "open" : null;
|
|
||||||
};
|
};
|
||||||
3
module/helpers/createArray.mjs
Normal file
3
module/helpers/createArray.mjs
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
export function createArray(...args) {
|
||||||
|
return args.slice(0, -1);
|
||||||
|
};
|
||||||
11
module/helpers/detailsExpanded.mjs
Normal file
11
module/helpers/detailsExpanded.mjs
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
/**
|
||||||
|
* Checks if the specified collapseId is currently open, so that during re-renders
|
||||||
|
* it remains open or closed.
|
||||||
|
*
|
||||||
|
* @param {Set<string>} expanded A set indicating what collapseIds are expanded
|
||||||
|
* @param {string} collapseId The collapseId to check for
|
||||||
|
* @returns {"open"|null} The HTML insertion indicating the details is expanded
|
||||||
|
*/
|
||||||
|
export function detailsExpanded(expanded, collapseId) {
|
||||||
|
return expanded.has(collapseId) ? "open" : null;
|
||||||
|
};
|
||||||
14
module/helpers/index.mjs
Normal file
14
module/helpers/index.mjs
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
import { schemaOptions } from "./schemaOptions.mjs";
|
||||||
|
import { createArray } from "./createArray.mjs";
|
||||||
|
import { detailsExpanded } from "./detailsExpanded.mjs";
|
||||||
|
import { objectValue } from "./objectValue.mjs";
|
||||||
|
import { toFriendlyDuration } from "./toFriendlyDuration.mjs";
|
||||||
|
|
||||||
|
export default {
|
||||||
|
"dotdungeon-schemaOptions": schemaOptions,
|
||||||
|
"dotdungeon-array": createArray,
|
||||||
|
"dotdungeon-toFriendlyDuration": toFriendlyDuration,
|
||||||
|
"dotdungeon-objectValue": objectValue,
|
||||||
|
"dotdungeon-stringify": v => JSON.stringify(v, null, ` `),
|
||||||
|
"dotdungeon-expanded": detailsExpanded,
|
||||||
|
};
|
||||||
11
module/helpers/objectValue.mjs
Normal file
11
module/helpers/objectValue.mjs
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
export function objectValue(obj, keypath) {
|
||||||
|
function helper(o, k) {
|
||||||
|
let v = o[k[0]];
|
||||||
|
if (typeof v === "object") {
|
||||||
|
return helper(v, k.slice(1));
|
||||||
|
};
|
||||||
|
return v;
|
||||||
|
};
|
||||||
|
let resp = helper(obj, keypath.string.split(`.`));
|
||||||
|
return resp;
|
||||||
|
};
|
||||||
1
module/helpers/schemaOptions.mjs
Normal file
1
module/helpers/schemaOptions.mjs
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
export function schemaOptions() {};
|
||||||
26
module/helpers/toFriendlyDuration.mjs
Normal file
26
module/helpers/toFriendlyDuration.mjs
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
const secondsInAMinute = 60;
|
||||||
|
const secondsInAnHour = 60 * secondsInAMinute;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a duration into a more human-friendly format
|
||||||
|
* @param {number} duration The length of time in seconds
|
||||||
|
* @returns The human-friendly time string
|
||||||
|
*/
|
||||||
|
export function toFriendlyDuration(duration) {
|
||||||
|
let friendly = ``;
|
||||||
|
if (duration >= secondsInAnHour) {
|
||||||
|
let hours = Math.floor(duration / secondsInAnHour);
|
||||||
|
friendly += `${hours}h`;
|
||||||
|
duration -= hours * secondsInAnHour;
|
||||||
|
};
|
||||||
|
if (duration >= secondsInAMinute) {
|
||||||
|
let minutes = Math.floor(duration / secondsInAMinute);
|
||||||
|
friendly += `${minutes}m`;
|
||||||
|
duration -= minutes * secondsInAMinute;
|
||||||
|
};
|
||||||
|
if (duration > 0) {
|
||||||
|
friendly += `${duration}s`;
|
||||||
|
};
|
||||||
|
return friendly;
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue