@services/utilities.js
General-purpose helpers used across the system. Import with:
import { localize, openFilePicker, activateTextEditor, kvOptions,
moveCardById, toggleCardSpanById,
getRandomIntinRange, getRandomBellCurveWithMode,
lerpColor, lerpColorToHexAsString, lerp } from "@services/utilities.js";
Localization
localize(key: string) → string
- Thin wrapper over Foundry
game.i18n.localize(key)
. - Use for all player-facing strings. Keys should come from
config.sr3e.*
.
- Thin wrapper over Foundry
File picker
async openFilePicker(document: Document) → Promise<string>
- Opens Foundry’s FilePicker for images.
- Seeds with
document.img
, updates the document with the selected path, and resolves with that path. - Throws: none; user cancellation simply leaves the document unchanged.
Rich text editor
activateTextEditor({ target, content, owner, editable, callback })
- If
editable
, mounts Foundry’sTextEditor
ontarget
(height 300); callscallback(html)
on save. - If not
editable
, enrichescontent
intotarget.innerHTML
.
- If
Option helpers
kvOptions(map: Record<string,string>) → Array<{ value: string, label: string }>
- Converts an object map into UI-ready options using
localize(token)
for labels. - Example:
{ short: config.procedure.range.short, long: config.procedure.range.long }
.
- Converts an object map into UI-ready options using
Card layout helpers
-
moveCardById(id: string, direction: "up" | "down")
- Reorders a card in the
cardLayout
Svelte store. - No-op if card not found or bounds exceeded.
- Reorders a card in the
-
toggleCardSpanById(id: string)
- Cycles a card’s
span
through 1 → 2 → 3 → 1 in thecardLayout
store.
- Cycles a card’s
Note: These depend on the cardLayout
store from module/svelteStore.js
.
Randomization
-
getRandomIntinRange(x: number, y: number) → number
- Inclusive integer in
[x, y]
.
- Inclusive integer in
-
getRandomBellCurveWithMode(min: number, max: number, mode: number) → number
- Truncated normal-ish integer sampler using Box–Muller; clamps to
[min, max]
and skews bymode
. - Throws if
min ≥ max
ormode
is outside the range.
- Truncated normal-ish integer sampler using Box–Muller; clamps to
Interpolation
-
lerp(a: number, b: number, t: number) → number
- Linear interpolation for scalars.
-
lerpColor(hex1: "#RRGGBB", hex2: "#RRGGBB", t: number) → "#RRGGBB"
- Interpolates two hex colors and returns a hex string.
-
lerpColorToHexAsString(color1: "#RRGGBB", color2: "#RRGGBB", t: number) → "#RRGGBB"
- Equivalent to
lerpColor
; kept for compatibility.
- Equivalent to