refactor: convert to ECMAScript where necessary

Also drop @league-of-foundry-developers/foundry-vtt-types.
This commit is contained in:
Johannes Loher 2022-11-17 00:12:29 +01:00
parent df4538f6ed
commit 6277e27056
69 changed files with 1077 additions and 1679 deletions

View file

@ -4,14 +4,12 @@
import { getCanvas, getGame } from "../utils/utils";
import type { DS4Actor } from "../documents/actor/actor";
/**
* Gets the currently active actor and token based on how {@link ChatMessage}
* determines the current speaker.
* @returns The currently active {@link DS4Actor} and {@link TokenDocument}.
* @returns {{actor?: import("../documents/actor/actor").DS4Actor, token?: TokenDocument}} The currently active actor and token.
*/
export function getActiveActorAndToken(): { actor?: DS4Actor; token?: TokenDocument } {
export function getActiveActorAndToken() {
const speaker = ChatMessage.getSpeaker();
const speakerToken = speaker.token ? getCanvas().tokens?.get(speaker.token)?.document : undefined;

View file

@ -7,19 +7,23 @@ import { notifications } from "../ui/notifications";
import { getGame } from "../utils/utils";
import { getActiveActorAndToken } from "./helpers";
import type { Check } from "../documents/actor/actor-data-properties-base";
/**
* Creates a macro from a check drop.
* Get an existing roll check macro if one exists, otherwise create a new one.
* @param check - The name of the check to perform.
* @param slot - The hotbar slot to use.
* @param {import("../documents/actor/actor-data-properties-base").Check} check The name of the check to perform
* @param {string} slot The hotbar slot to use
* @returns {Promise<void>} A promise that resoolves when the macro has been created.
*/
export async function createRollCheckMacro(check: Check, slot: string): Promise<void> {
export async function createRollCheckMacro(check, slot) {
const macro = await getOrCreateRollCheckMacro(check);
getGame().user?.assignHotbarMacro(macro ?? null, slot);
await getGame().user?.assignHotbarMacro(macro ?? null, slot);
}
async function getOrCreateRollCheckMacro(check: Check): Promise<Macro | undefined> {
/**
* @param {import("../documents/actor/actor-data-properties-base").Check} check The name of the check to perform
* @returns {Promise<Macro|undefined>} A promise that resolves to the created macro
*/
async function getOrCreateRollCheckMacro(check) {
const command = `game.ds4.macros.rollCheck("${check}");`;
const existingMacro = getGame().macros?.find(
@ -43,8 +47,10 @@ async function getOrCreateRollCheckMacro(check: Check): Promise<Macro | undefine
/**
* Executes the roll check macro for the given check.
* @param {import("../documents/actor/actor-data-properties-base").Check} check The name of the check to perform
* @returns {Promise<void>} A promise that resolves once the check has been performed.
*/
export async function rollCheck(check: Check): Promise<void> {
export async function rollCheck(check) {
const { actor, token } = getActiveActorAndToken();
if (!actor) {
return notifications.warn(getGame().i18n.localize("DS4.WarningMustControlActorToUseRollCheckMacro"));

View file

@ -9,15 +9,20 @@ import { getActiveActorAndToken } from "./helpers";
/**
* Creates a macro from an item drop.
* Get an existing roll item macro if one exists, otherwise create a new one.
* @param itemData - The item data
* @param slot - The hotbar slot to use
* @param {object} itemData The item data
* @param {string} slot The hotbar slot to use
* @returns {Promise<void>} A promise that resolves once the macro has been created.
*/
export async function createRollItemMacro(itemData: foundry.data.ItemData["_source"], slot: string): Promise<void> {
export async function createRollItemMacro(itemData, slot) {
const macro = await getOrCreateRollItemMacro(itemData);
getGame().user?.assignHotbarMacro(macro ?? null, slot);
await getGame().user?.assignHotbarMacro(macro ?? null, slot);
}
async function getOrCreateRollItemMacro(itemData: foundry.data.ItemData["_source"]): Promise<Macro | undefined> {
/**
* @param {object} itemData The item data
* @returns {Promise<Macro | undefined>} A promise that resolves to the created macro
*/
async function getOrCreateRollItemMacro(itemData) {
const command = `game.ds4.macros.rollItem("${itemData._id}");`;
const existingMacro = getGame().macros?.find((m) => m.name === itemData.name && m.data.command === command);
@ -39,8 +44,10 @@ async function getOrCreateRollItemMacro(itemData: foundry.data.ItemData["_source
/**
* Executes the roll item macro for the item associated to the given `itemId`.
* @param {string} itemId The id of the item to roll
* @returns {Promise<void>} A promise that resolves once the item has been rolled.
*/
export async function rollItem(itemId: string): Promise<void> {
export async function rollItem(itemId) {
const { actor, token } = getActiveActorAndToken();
if (!actor) {
return notifications.warn(getGame().i18n.localize("DS4.WarningMustControlActorToUseRollItemMacro"));