diff --git a/module/handlebars.mjs b/module/handlebars.mjs index 2029787..14902c6 100644 --- a/module/handlebars.mjs +++ b/module/handlebars.mjs @@ -1,3 +1,5 @@ +import helpers from "./helpers/index.mjs"; + export const partials = [ `actors/char-sheet-mvp/partials/dice_choice.hbs`, `actors/char-sheet-mvp/partials/stat.hbs`, @@ -7,13 +9,7 @@ export const partials = [ ]; export async function registerHandlebarsHelpers() { - Handlebars.registerHelper({ - "dotdungeon-array": createArray, - "dotdungeon-toFriendlyDuration": toFriendlyDuration, - "dotdungeon-objectValue": objectValue, - "dotdungeon-stringify": v => JSON.stringify(v, null, ` `), - "dotdungeon-expanded": detailsExpanded, - }); + Handlebars.registerHelper(helpers); }; export async function preloadHandlebarsTemplates() { @@ -31,59 +27,4 @@ export async function preloadHandlebarsTemplates() { console.debug(`Loaded ${partials.length} partials`); console.groupEnd(); 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} 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; }; \ No newline at end of file diff --git a/module/helpers/createArray.mjs b/module/helpers/createArray.mjs new file mode 100644 index 0000000..6fb6500 --- /dev/null +++ b/module/helpers/createArray.mjs @@ -0,0 +1,3 @@ +export function createArray(...args) { + return args.slice(0, -1); +}; \ No newline at end of file diff --git a/module/helpers/detailsExpanded.mjs b/module/helpers/detailsExpanded.mjs new file mode 100644 index 0000000..bf9ddef --- /dev/null +++ b/module/helpers/detailsExpanded.mjs @@ -0,0 +1,11 @@ +/** + * Checks if the specified collapseId is currently open, so that during re-renders + * it remains open or closed. + * + * @param {Set} 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; +}; \ No newline at end of file diff --git a/module/helpers/index.mjs b/module/helpers/index.mjs new file mode 100644 index 0000000..a05a4c6 --- /dev/null +++ b/module/helpers/index.mjs @@ -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, +}; \ No newline at end of file diff --git a/module/helpers/objectValue.mjs b/module/helpers/objectValue.mjs new file mode 100644 index 0000000..be22aed --- /dev/null +++ b/module/helpers/objectValue.mjs @@ -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; +}; \ No newline at end of file diff --git a/module/helpers/schemaOptions.mjs b/module/helpers/schemaOptions.mjs new file mode 100644 index 0000000..7547589 --- /dev/null +++ b/module/helpers/schemaOptions.mjs @@ -0,0 +1 @@ +export function schemaOptions() {}; \ No newline at end of file diff --git a/module/helpers/toFriendlyDuration.mjs b/module/helpers/toFriendlyDuration.mjs new file mode 100644 index 0000000..9a2ad30 --- /dev/null +++ b/module/helpers/toFriendlyDuration.mjs @@ -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; +}; \ No newline at end of file