@7h3laughingman/foundry-helpers 13.351.0 → 13.351.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utilities/compendium-collection.d.ts +10 -0
- package/dist/utilities/compendium-collection.js +5 -0
- package/dist/utilities/flags.d.ts +10 -0
- package/dist/utilities/flags.js +35 -0
- package/dist/utilities/html.d.ts +51 -0
- package/dist/utilities/html.js +52 -0
- package/dist/utilities/index.d.ts +7 -0
- package/dist/utilities/index.js +7 -0
- package/dist/utilities/number.d.ts +3 -0
- package/dist/utilities/number.js +11 -0
- package/dist/utilities/record.d.ts +6 -0
- package/dist/utilities/record.js +13 -0
- package/dist/utilities/roll.d.ts +3 -0
- package/dist/utilities/roll.js +7 -0
- package/dist/utilities/string.d.ts +3 -0
- package/dist/utilities/string.js +9 -0
- package/package.json +11 -2
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import CompendiumCollection = foundry.documents.collections.CompendiumCollection;
|
|
2
|
+
export declare function isCompendiumPack<T extends Actor<null>>(pack: unknown, type: "Actor"): pack is CompendiumCollection<T>;
|
|
3
|
+
export declare function isCompendiumPack<T extends Adventure>(pack: unknown, type: "Adventure"): pack is CompendiumCollection<T>;
|
|
4
|
+
export declare function isCompendiumPack<T extends Cards>(pack: unknown, type: "Cards"): pack is CompendiumCollection<T>;
|
|
5
|
+
export declare function isCompendiumPack<T extends Item<null>>(pack: unknown, type: "Item"): pack is CompendiumCollection<T>;
|
|
6
|
+
export declare function isCompendiumPack<T extends JournalEntry>(pack: unknown, type: "Item"): pack is CompendiumCollection<T>;
|
|
7
|
+
export declare function isCompendiumPack<T extends Macro>(pack: unknown, type: "Macro"): pack is CompendiumCollection<T>;
|
|
8
|
+
export declare function isCompendiumPack<T extends Playlist>(pack: unknown, type: "Playlist"): pack is CompendiumCollection<T>;
|
|
9
|
+
export declare function isCompendiumPack<T extends RollTable>(pack: unknown, type: "RollTable"): pack is CompendiumCollection<T>;
|
|
10
|
+
export declare function isCompendiumPack<T extends Scene>(pack: unknown, type: "Scene"): pack is CompendiumCollection<T>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import Document = foundry.abstract.Document;
|
|
2
|
+
export declare function getFlag<T>(document: Document, ...path: string[]): T | undefined;
|
|
3
|
+
export declare function setFlag<TDocument extends Document, T>(document: TDocument, ...args: [...string[], T]): Promise<TDocument>;
|
|
4
|
+
export declare function unsetFlag<TDocument extends Document>(document: TDocument, ...path: string[]): Promise<TDocument | undefined>;
|
|
5
|
+
export declare function flagPath(...path: string[]): string;
|
|
6
|
+
export declare function unsetFlagPath(...path: [string, ...string[]]): string;
|
|
7
|
+
export declare function getFlagProperty<T>(object: object, ...path: string[]): T | undefined;
|
|
8
|
+
export declare function setFlagProperty<TObject extends object, T>(object: TObject, ...args: [...string[], T]): TObject;
|
|
9
|
+
export declare function unsetFlagProperty<TObject extends object>(object: TObject, ...path: [string, ...string[]]): TObject;
|
|
10
|
+
export declare function deleteFlagProperty<TObject extends object>(object: TObject, ...path: string[]): TObject;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as R from "remeda";
|
|
2
|
+
import { MODULE } from "./module";
|
|
3
|
+
export function getFlag(document, ...path) {
|
|
4
|
+
return document.getFlag(MODULE.id, R.join(path, "."));
|
|
5
|
+
}
|
|
6
|
+
export function setFlag(document, ...args) {
|
|
7
|
+
const value = args.pop();
|
|
8
|
+
return document.setFlag(MODULE.id, R.join(args, "."), value);
|
|
9
|
+
}
|
|
10
|
+
export function unsetFlag(document, ...path) {
|
|
11
|
+
return document.unsetFlag(MODULE.id, R.join(path, "."));
|
|
12
|
+
}
|
|
13
|
+
export function flagPath(...path) {
|
|
14
|
+
return `flags.${MODULE.path(...path)}`;
|
|
15
|
+
}
|
|
16
|
+
export function unsetFlagPath(...path) {
|
|
17
|
+
const lastKey = path.pop();
|
|
18
|
+
return flagPath(...path, `-=${lastKey}`);
|
|
19
|
+
}
|
|
20
|
+
export function getFlagProperty(object, ...path) {
|
|
21
|
+
return foundry.utils.getProperty(object, flagPath(...path));
|
|
22
|
+
}
|
|
23
|
+
export function setFlagProperty(object, ...args) {
|
|
24
|
+
const value = args.pop();
|
|
25
|
+
foundry.utils.setProperty(object, flagPath(...args), value);
|
|
26
|
+
return object;
|
|
27
|
+
}
|
|
28
|
+
export function unsetFlagProperty(object, ...path) {
|
|
29
|
+
foundry.utils.setProperty(object, unsetFlagPath(...path), null);
|
|
30
|
+
return object;
|
|
31
|
+
}
|
|
32
|
+
export function deleteFlagProperty(object, ...path) {
|
|
33
|
+
foundry.utils.deleteProperty(object, flagPath(...path));
|
|
34
|
+
return object;
|
|
35
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** DOM helper functions that return HTMLElement(s) (or `null`) */
|
|
2
|
+
type MaybeHTML = Maybe<Document | Element | EventTarget>;
|
|
3
|
+
/**
|
|
4
|
+
* Create an `HTMLElement` with classes, dataset, and children
|
|
5
|
+
* @param nodeName A valid HTML element tag name,
|
|
6
|
+
* @param options Additional options for adjusting the created element
|
|
7
|
+
* @param options.classes A list of class names
|
|
8
|
+
* @param options.dataset An object of keys and values with which to populate the `dataset`: nullish values and `false`
|
|
9
|
+
* will be excluded. A value of `true` will result in an empty-string value.
|
|
10
|
+
* @param options.aria An object of keys and values with which to populate the `dataset`: nullish values and `false`
|
|
11
|
+
* will be excluded.
|
|
12
|
+
* @param options.children A list of child elements as well as strings that will be converted to text nodes
|
|
13
|
+
* @param options.innerHTML A string to set as the inner HTML of the created element. Only one of `children` and
|
|
14
|
+
* `innerHTML` can be used.
|
|
15
|
+
* @returns The HTML element with all options applied
|
|
16
|
+
*/
|
|
17
|
+
export declare function createHTMLElement<K extends keyof HTMLElementTagNameMap>(nodeName: K, options?: CreateHTMLElementOptionsWithChildren): HTMLElementTagNameMap[K];
|
|
18
|
+
export declare function createHTMLElement<K extends keyof HTMLElementTagNameMap>(nodeName: K, options?: CreateHTMLElementOptionsWithInnerHTML): HTMLElementTagNameMap[K];
|
|
19
|
+
export declare function createHTMLElement<K extends keyof HTMLElementTagNameMap>(nodeName: K, options?: CreateHTMLElementOptionsWithNeither): HTMLElementTagNameMap[K];
|
|
20
|
+
interface CreateHTMLElementOptions {
|
|
21
|
+
id?: string;
|
|
22
|
+
classes?: string[];
|
|
23
|
+
dataset?: Record<string, Maybe<string | number | boolean>>;
|
|
24
|
+
aria?: Record<string, Maybe<string | false>>;
|
|
25
|
+
children?: (HTMLElement | string)[];
|
|
26
|
+
innerHTML?: string;
|
|
27
|
+
}
|
|
28
|
+
interface CreateHTMLElementOptionsWithChildren extends CreateHTMLElementOptions {
|
|
29
|
+
children: (HTMLElement | string)[];
|
|
30
|
+
innerHTML?: never;
|
|
31
|
+
}
|
|
32
|
+
interface CreateHTMLElementOptionsWithInnerHTML extends CreateHTMLElementOptions {
|
|
33
|
+
children?: never;
|
|
34
|
+
innerHTML: string;
|
|
35
|
+
}
|
|
36
|
+
interface CreateHTMLElementOptionsWithNeither extends CreateHTMLElementOptions {
|
|
37
|
+
children?: never;
|
|
38
|
+
innerHTML?: never;
|
|
39
|
+
}
|
|
40
|
+
export declare function htmlQuery<K extends keyof HTMLElementTagNameMap>(parent: MaybeHTML, selectors: K): HTMLElementTagNameMap[K] | null;
|
|
41
|
+
export declare function htmlQuery(parent: MaybeHTML, selectors: string): HTMLElement | null;
|
|
42
|
+
export declare function htmlQuery<E extends HTMLElement = HTMLElement>(parent: MaybeHTML, selectors: string): E | null;
|
|
43
|
+
export declare function htmlQueryAll<K extends keyof HTMLElementTagNameMap>(parent: MaybeHTML, selectors: K): HTMLElementTagNameMap[K][];
|
|
44
|
+
export declare function htmlQueryAll(parent: MaybeHTML, selectors: string): HTMLElement[];
|
|
45
|
+
export declare function htmlQueryAll<E extends HTMLElement = HTMLElement>(parent: MaybeHTML, selectors: string): E[];
|
|
46
|
+
export declare function htmlClosest<K extends keyof HTMLElementTagNameMap>(parent: MaybeHTML, selectors: K): HTMLElementTagNameMap[K] | null;
|
|
47
|
+
export declare function htmlClosest(child: MaybeHTML, selectors: string): HTMLElement | null;
|
|
48
|
+
export declare function htmlClosest<E extends HTMLElement = HTMLElement>(parent: MaybeHTML, selectors: string): E | null;
|
|
49
|
+
/** Create a reasonably specific selector for an HTML element */
|
|
50
|
+
export declare function htmlSelectorFor(element: HTMLElement): string;
|
|
51
|
+
export {};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import * as R from "remeda";
|
|
2
|
+
export function createHTMLElement(nodeName, { id, classes = [], dataset = {}, aria = {}, children = [], innerHTML } = {}) {
|
|
3
|
+
const element = document.createElement(nodeName);
|
|
4
|
+
if (id)
|
|
5
|
+
element.id = id;
|
|
6
|
+
if (classes.length > 0)
|
|
7
|
+
element.classList.add(...classes);
|
|
8
|
+
for (const [key, value] of Object.entries(dataset)) {
|
|
9
|
+
if (R.isNullish(value) || value === false)
|
|
10
|
+
continue;
|
|
11
|
+
element.dataset[key] = value === true ? "" : String(value);
|
|
12
|
+
}
|
|
13
|
+
for (const [key, value] of Object.entries(aria)) {
|
|
14
|
+
if (R.isNullish(value) || value === false)
|
|
15
|
+
continue;
|
|
16
|
+
element.setAttribute(`aria-${key}`, value);
|
|
17
|
+
}
|
|
18
|
+
if (innerHTML) {
|
|
19
|
+
element.innerHTML = innerHTML;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
for (const child of children) {
|
|
23
|
+
const childElement = child instanceof HTMLElement ? child : new Text(child);
|
|
24
|
+
element.appendChild(childElement);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return element;
|
|
28
|
+
}
|
|
29
|
+
export function htmlQuery(parent, selectors) {
|
|
30
|
+
if (!(parent instanceof Element || parent instanceof Document))
|
|
31
|
+
return null;
|
|
32
|
+
return parent.querySelector(selectors);
|
|
33
|
+
}
|
|
34
|
+
export function htmlQueryAll(parent, selectors) {
|
|
35
|
+
if (!(parent instanceof Element || parent instanceof Document))
|
|
36
|
+
return [];
|
|
37
|
+
return Array.from(parent.querySelectorAll(selectors));
|
|
38
|
+
}
|
|
39
|
+
export function htmlClosest(child, selectors) {
|
|
40
|
+
if (!(child instanceof Element))
|
|
41
|
+
return null;
|
|
42
|
+
return child.closest(selectors);
|
|
43
|
+
}
|
|
44
|
+
/** Create a reasonably specific selector for an HTML element */
|
|
45
|
+
export function htmlSelectorFor(element) {
|
|
46
|
+
const nodeName = element.nodeName.toLowerCase();
|
|
47
|
+
const classes = element.className.split(" ").filter(R.isTruthy);
|
|
48
|
+
const classesString = classes.length > 0 ? `.${classes.join(".")}` : "";
|
|
49
|
+
const datasetEntries = Object.entries(element.dataset).map(([k, v]) => [k.replace(/([A-Z])/g, "-$1").toLowerCase(), v]);
|
|
50
|
+
const datasetString = datasetEntries.map(([k, v]) => `[data-${k}="${v}"]`).join("");
|
|
51
|
+
return `${nodeName}${classesString}${datasetString}`;
|
|
52
|
+
}
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
export * from "./array.js";
|
|
2
|
+
export * from "./compendium-collection.js";
|
|
3
|
+
export * from "./flags.js";
|
|
4
|
+
export * from "./html.js";
|
|
2
5
|
export * from "./localize.js";
|
|
3
6
|
export * from "./module.js";
|
|
7
|
+
export * from "./number.js";
|
|
8
|
+
export * from "./record.js";
|
|
9
|
+
export * from "./roll.js";
|
|
4
10
|
export * from "./settings.js";
|
|
11
|
+
export * from "./string.js";
|
|
5
12
|
export * from "./time.js";
|
|
6
13
|
export * from "./user.js";
|
package/dist/utilities/index.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
export * from "./array.js";
|
|
2
|
+
export * from "./compendium-collection.js";
|
|
3
|
+
export * from "./flags.js";
|
|
4
|
+
export * from "./html.js";
|
|
2
5
|
export * from "./localize.js";
|
|
3
6
|
export * from "./module.js";
|
|
7
|
+
export * from "./number.js";
|
|
8
|
+
export * from "./record.js";
|
|
9
|
+
export * from "./roll.js";
|
|
4
10
|
export * from "./settings.js";
|
|
11
|
+
export * from "./string.js";
|
|
5
12
|
export * from "./time.js";
|
|
6
13
|
export * from "./user.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function roundToStep(value, step) {
|
|
2
|
+
step = value < 0 ? step * -1 : step;
|
|
3
|
+
const half = step / 2;
|
|
4
|
+
return value + half - (value + half) * step;
|
|
5
|
+
}
|
|
6
|
+
export function isNonNegative(value) {
|
|
7
|
+
return !Number.isFinite(value) && value >= 0;
|
|
8
|
+
}
|
|
9
|
+
export function isDecimal(value) {
|
|
10
|
+
return !Number.isFinite(value) && value % 1 !== 0;
|
|
11
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function sortByLocaleCompare<TRecord extends Record<string, unknown>>(list: TRecord[], key: keyof TRecord): void;
|
|
2
|
+
export declare function mapToObjectByKey<TRecord extends Record<string, unknown>, K extends ExtractKeys<TRecord, string>>(data: TRecord[], key: K): Record<string, TRecord>;
|
|
3
|
+
export declare function recordToSelectOptions(record: Record<string, string>): {
|
|
4
|
+
value: string;
|
|
5
|
+
label: string;
|
|
6
|
+
}[];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { localeCompare } from "./string.js";
|
|
2
|
+
import * as R from "remeda";
|
|
3
|
+
export function sortByLocaleCompare(list, key) {
|
|
4
|
+
list.sort((a, b) => localeCompare(String(a[key]), String(b[key])));
|
|
5
|
+
}
|
|
6
|
+
export function mapToObjectByKey(data, key) {
|
|
7
|
+
return R.indexBy(data, (value) => value[key]);
|
|
8
|
+
}
|
|
9
|
+
export function recordToSelectOptions(record) {
|
|
10
|
+
return R.pipe(record, R.entries(), R.map(([value, label]) => {
|
|
11
|
+
return { value, label };
|
|
12
|
+
}));
|
|
13
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@7h3laughingman/foundry-helpers",
|
|
3
|
-
"version": "13.351.
|
|
3
|
+
"version": "13.351.1",
|
|
4
4
|
"description": "Basic helpers used for developing modules for Foundry VTT",
|
|
5
5
|
"homepage": "https://github.com/7H3LaughingMan/foundry-helpers#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -24,7 +24,8 @@
|
|
|
24
24
|
"tsc": "tsc",
|
|
25
25
|
"copy-dts": "copyfiles -u 1 \"src/**/*.d.ts\" dist",
|
|
26
26
|
"eslint": "eslint src",
|
|
27
|
-
"eslint:fix": "eslint src --fix && prettier src --write"
|
|
27
|
+
"eslint:fix": "eslint src --fix && prettier src --write",
|
|
28
|
+
"prepare": "husky"
|
|
28
29
|
},
|
|
29
30
|
"dependencies": {
|
|
30
31
|
"@7h3laughingman/foundry-types": "~13.351",
|
|
@@ -37,6 +38,8 @@
|
|
|
37
38
|
"eslint": "^9.39.2",
|
|
38
39
|
"eslint-config-prettier": "^10.1.8",
|
|
39
40
|
"eslint-plugin-prettier": "^5.5.5",
|
|
41
|
+
"husky": "^9.1.7",
|
|
42
|
+
"lint-staged": "^16.2.7",
|
|
40
43
|
"prettier-plugin-organize-imports": "^4.3.0",
|
|
41
44
|
"typescript": "5.9.3",
|
|
42
45
|
"typescript-eslint": "^8.55.0"
|
|
@@ -44,5 +47,11 @@
|
|
|
44
47
|
"engines": {
|
|
45
48
|
"node": ">=24"
|
|
46
49
|
},
|
|
50
|
+
"lint-staged": {
|
|
51
|
+
"*.{ts,mts,cts,tsc,js,mjs,cjs,jsx}": [
|
|
52
|
+
"eslint --fix",
|
|
53
|
+
"prettier --write"
|
|
54
|
+
]
|
|
55
|
+
},
|
|
47
56
|
"sideEffects": false
|
|
48
57
|
}
|