Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PickAThingPrompt generic on its main item #13202

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions src/module/apps/pick-a-thing-prompt.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import type { ActorPF2e } from "@actor";
import type { ItemPF2e } from "@item";
import type { UserPF2e } from "@module/user/document.ts";
import { PredicatePF2e } from "@system/predication.ts";
import { htmlClosest, htmlQuery, htmlQueryAll } from "@util";
import Tagify from "@yaireo/tagify";

/** Prompt the user to pick from a number of options */
abstract class PickAThingPrompt<T extends string | number | object> extends Application {
protected item: ItemPF2e<ActorPF2e>;
abstract class PickAThingPrompt<TItem extends ItemPF2e, TThing extends string | number | object> extends Application {
protected item: TItem;

#resolve?: (value: PickableThing<T> | null) => void;
#resolve?: (value: PickableThing<TThing> | null) => void;

protected selection: PickableThing<T> | null = null;
protected selection: PickableThing<TThing> | null = null;

protected choices: PickableThing<T>[];
protected choices: PickableThing<TThing>[];

/** If the number of choices is beyond a certain length, a select menu is presented instead of a list of buttons */
protected selectMenu?: Tagify<{ value: string; label: string }>;
Expand All @@ -22,7 +21,7 @@ abstract class PickAThingPrompt<T extends string | number | object> extends Appl

protected allowNoSelection: boolean;

constructor(data: PickAThingConstructorArgs<T>) {
constructor(data: PickAThingConstructorArgs<TItem, TThing>) {
super();
this.item = data.item;
this.choices = data.choices;
Expand All @@ -31,7 +30,7 @@ abstract class PickAThingPrompt<T extends string | number | object> extends Appl
this.allowNoSelection = data.allowNoSelection ?? false;
}

get actor(): ActorPF2e {
get actor(): TItem["parent"] {
return this.item.actor;
}

Expand All @@ -45,7 +44,7 @@ abstract class PickAThingPrompt<T extends string | number | object> extends Appl
};
}

protected getSelection(event: MouseEvent): PickableThing<T> | null {
protected getSelection(event: MouseEvent): PickableThing<TThing> | null {
const valueElement =
htmlClosest(event.target, ".content")?.querySelector<HTMLElement>("tag") ??
htmlClosest(event.target, "button[data-action=pick]") ??
Expand All @@ -58,7 +57,7 @@ abstract class PickAThingPrompt<T extends string | number | object> extends Appl
}

/** Return a promise containing the user's item selection, or `null` if no selection was made */
async resolveSelection(): Promise<PickableThing<T> | null> {
async resolveSelection(): Promise<PickableThing<TThing> | null> {
this.render(true);
return new Promise((resolve) => {
this.#resolve = resolve;
Expand Down Expand Up @@ -116,11 +115,11 @@ abstract class PickAThingPrompt<T extends string | number | object> extends Appl
}
}

interface PickAThingConstructorArgs<T extends string | number | object> {
interface PickAThingConstructorArgs<TItem extends ItemPF2e, TThing extends string | number | object> {
title?: string;
prompt?: string;
choices: PickableThing<T>[];
item: ItemPF2e<ActorPF2e>;
choices: PickableThing<TThing>[];
item: TItem;
predicate?: PredicatePF2e;
allowNoSelection?: boolean;
}
Expand Down
11 changes: 6 additions & 5 deletions src/module/rules/rule-element/choice-set/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ActorPF2e } from "@actor";
import { ItemPF2e } from "@item";
import {
PickableThing,
Expand All @@ -6,12 +7,12 @@ import {
PromptTemplateData,
} from "@module/apps/pick-a-thing-prompt.ts";
import { DropCanvasDataPF2e } from "@module/canvas/drop-canvas-data.ts";
import { PredicatePF2e } from "@system/predication.ts";
import type { PredicatePF2e } from "@system/predication.ts";
import { createHTMLElement, ErrorPF2e, htmlQuery, htmlQueryAll, sluggify } from "@util";
import { UUIDUtils } from "@util/uuid.ts";

/** Prompt the user for a selection among a set of options */
class ChoiceSetPrompt extends PickAThingPrompt<string | number | object> {
class ChoiceSetPrompt extends PickAThingPrompt<ItemPF2e<ActorPF2e>, string | number | object> {
/** The prompt statement to present the user in this application's window */
prompt: string;

Expand Down Expand Up @@ -221,11 +222,11 @@ class ChoiceSetPrompt extends PickAThingPrompt<string | number | object> {
}
}

interface ChoiceSetPrompt extends PickAThingPrompt<string | number | object> {
getSelection: (event: MouseEvent) => ChoiceSetChoice | null;
interface ChoiceSetPrompt extends PickAThingPrompt<ItemPF2e<ActorPF2e>, string | number | object> {
getSelection(event: MouseEvent): ChoiceSetChoice | null;
}

interface ChoiceSetPromptData extends PickAThingConstructorArgs<string | number | object> {
interface ChoiceSetPromptData extends PickAThingConstructorArgs<ItemPF2e<ActorPF2e>, string | number | object> {
prompt: string;
containsItems: boolean;
allowedDrops: { label: string | null; predicate: PredicatePF2e } | null;
Expand Down