-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MHDialog tidyup, validator implementation, documentation
- Loading branch information
Showing
13 changed files
with
252 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
export const MODULE = 'pf2e-macro-helper-library'; | ||
export const NOTIFY = () => game.settings.get(MODULE,'notify-on-error'); | ||
export const PHYSICAL_ITEM_TYPES = ["armor", "backpack", "book", "consumable", "equipment", "shield", "treasure", "weapon"]; | ||
export const fu = foundry.utils; | ||
export const CONSOLE_TYPES = ["debug","info","warn","error"]; | ||
export const BANNER_TYPES = CONSOLE_TYPES.slice(1) | ||
export const COLOURS = { | ||
error: 'var(--color-level-error, red)' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,75 @@ | ||
import { NOTIFY } from "../constants.mjs"; | ||
import { BANNER_TYPES, CONSOLE_TYPES } from "../constants.mjs"; | ||
import { NOTIFY } from "../settings.mjs"; | ||
import { localize } from "./stringHelpers.mjs"; | ||
export const PREFIX = "MHL"; | ||
|
||
export function localizedError(str, data = {}, { notify = null, prefix = "", log = {} } = {}) { | ||
notify ??= NOTIFY; | ||
notify ??= NOTIFY(); | ||
let errorstr = "" + prefix; | ||
if (typeof log === "object" && Object.keys(log).length) console.error(log); | ||
errorstr += typeof str === "string" ? localize(str, data) : localize(`${PREFIX}.Error.Type.String`); | ||
if (typeof log === "object" && Object.keys(log).length) mhlog(log, "error"); | ||
if (typeof str !== "string") { | ||
throw MHLError(`MHL.Error.Type.String`, { var: "str" }, { func: "localizedError", log: { str } }); | ||
} | ||
errorstr += localize(str, data); | ||
if (notify) ui.notifications.error(errorstr, { console: false }); | ||
return Error(errorstr); | ||
} | ||
export function localizedBanner(str, data = {}, { notify = null, prefix = "", log = {}, type="info" } = {}) { | ||
const func = 'localizedBanner'; | ||
notify ??= NOTIFY; | ||
|
||
export function localizedBanner(str, data = {}, { notify = null, prefix = "", log = {}, type = "info", console=true} = {}) { | ||
const func = "localizedBanner"; | ||
notify ??= NOTIFY(); | ||
if (!notify) return false; | ||
if (!['info','error','warn'].includes(type)) throw MHLError(`${PREFIX}.Error.BannerType`, null, {func, log:{type}}) | ||
if (!BANNER_TYPES.includes(type)) throw MHLError(`MHL.Error.BannerType`, null, { func, log: { type } }); | ||
let bannerstr = "" + prefix; | ||
if (typeof log === "object" && Object.keys(log).length) console[type](log); | ||
if (typeof str !== "string") throw MHLError(`${PREFIX}.Error.Type.String`, {var:'str'}, {func,log:{str}}); | ||
if (typeof log === "object" && Object.keys(log).length) mhlog(log,type); | ||
if (typeof str !== "string") { | ||
throw MHLError(`MHL.Error.Type.String`, { var: "str" }, { func, log: { str } }); | ||
} | ||
bannerstr += localize(str, data); | ||
return ui.notifications[type](bannerstr); | ||
return ui.notifications[type](bannerstr, {console}); | ||
} | ||
|
||
export function MHLError( | ||
str, | ||
data = {}, | ||
{ notify = null, prefix = "MacroHelperLibrary: ", log = {}, func = null } = {} | ||
{ notify = null, prefix = "MHL | ", log = {}, func = null } = {} | ||
) { | ||
if (func && typeof func === "string") prefix += func; | ||
return localizedError(str, data, { notify, prefix }); | ||
return localizedError(str, data, { notify, prefix, log }); | ||
} | ||
|
||
// taken from https://stackoverflow.com/a/32728075, slightly modernized | ||
/** | ||
* Checks if value is empty. Deep-checks arrays and objects | ||
* Note: isEmpty([]) == true, isEmpty({}) == true, isEmpty([{0:false},"",0]) == true, isEmpty({0:1}) == false | ||
* @param value | ||
* @returns {boolean} | ||
*/ | ||
export function isEmpty(value) { | ||
const isEmptyObject = (a) => { | ||
if (!Array.isArray(a)) { | ||
// it's an Object, not an Array | ||
const hasNonempty = Object.keys(a).some((e) => !isEmpty(a[e])); | ||
return hasNonempty ? false : isEmptyObject(Object.keys(a)); | ||
} | ||
return !a.some((e) => !isEmpty(e)); | ||
}; | ||
return ( | ||
value == false || | ||
typeof value === "undefined" || | ||
value == null || | ||
(typeof value === "object" && isEmptyObject(value)) | ||
); | ||
} | ||
|
||
export function log(loggable, type = null, prefix = null) { | ||
type ??= "debug"; | ||
if (!CONSOLE_TYPES.includes(type)) { | ||
throw MHLError(`MHL.Error.LogTypes`, { types: BANNER_TYPES.join(", ") }, { func: "log: ", log: { type } }); | ||
} | ||
prefix ??= ""; | ||
return console[type](prefix, loggable); | ||
} | ||
|
||
export function mhlog(loggable, type = null) { | ||
return log(loggable, type, "MacroHelperLibrary |"); | ||
} |
Oops, something went wrong.