Skip to content

Commit

Permalink
Add token schema definition (foundryvtt#9042)
Browse files Browse the repository at this point in the history
  • Loading branch information
stwlam committed Jul 31, 2023
1 parent 77aaf76 commit d8be25e
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 178 deletions.
2 changes: 1 addition & 1 deletion src/module/canvas/status-effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class StatusEffects {
}

static async onRenderTokenHUD(html: HTMLElement, tokenData: TokenHUDData): Promise<void> {
const token = canvas.tokens.get(tokenData._id);
const token = canvas.tokens.get(tokenData._id ?? "");
if (!token) return;

const iconGrid = html.querySelector<HTMLElement>(".status-effects");
Expand Down
7 changes: 4 additions & 3 deletions src/module/scene/token-document/actor-delta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ActorSystemSource } from "@actor/data/base.ts";
import { ItemPF2e } from "@item";
import { ItemSourcePF2e } from "@item/data/index.ts";
import type { TombstoneSource } from "types/foundry/common/data/data.d.ts";
import type { ActorDeltaSource } from "types/foundry/common/documents/actor-delta.d.ts";
import { TokenDocumentPF2e } from "./document.ts";

class ActorDeltaPF2e<TParent extends TokenDocumentPF2e | null> extends ActorDelta<TParent> {
Expand Down Expand Up @@ -71,12 +72,12 @@ class ActorDeltaPF2e<TParent extends TokenDocumentPF2e | null> extends ActorDelt
}

interface ActorDeltaPF2e<TParent extends TokenDocumentPF2e | null> extends ActorDelta<TParent> {
readonly _source: ActorDeltaSourcePF2e<TParent>;
readonly _source: ActorDeltaSourcePF2e;
}

type ActorDeltaSourcePF2e<TParent extends TokenDocumentPF2e | null> = ActorDelta<TParent>["_source"] & {
interface ActorDeltaSourcePF2e extends ActorDeltaSource {
system: ActorSystemSource | null;
items: (ItemSourcePF2e | TombstoneSource)[];
};
}

export { ActorDeltaPF2e };
5 changes: 2 additions & 3 deletions src/module/scene/token-document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class TokenDocumentPF2e<TParent extends ScenePF2e | null = ScenePF2e | null> ext
protected override _initialize(options?: Record<string, unknown>): void {
this.constructed ??= false;
this.auras = new Map();

this._source.flags.pf2e ??= {};
this._source.flags.pf2e.linkToActorSize ??= true;
this._source.flags.pf2e.autoscale = this._source.flags.pf2e.linkToActorSize
Expand Down Expand Up @@ -300,7 +301,7 @@ class TokenDocumentPF2e<TParent extends ScenePF2e | null = ScenePF2e | null> ext

// Always override token images if in Nath mode
if (game.settings.get("pf2e", "nathMode") && defaultIcons.includes(token.texture.src)) {
token.texture.src = ((): VideoFilePath => {
token.texture.src = ((): ImageFilePath | VideoFilePath => {
switch (actor.alliance) {
case "party":
return "systems/pf2e/icons/default-icons/alternatives/nath/ally.webp";
Expand Down Expand Up @@ -481,8 +482,6 @@ interface TokenDocumentPF2e<TParent extends ScenePF2e | null = ScenePF2e | null>
get object(): TokenPF2e<this> | null;
get sheet(): TokenConfigPF2e<this>;
delta: ActorDeltaPF2e<this> | null;

overlayEffect: ImageFilePath;
}

export { TokenDocumentPF2e };
2 changes: 1 addition & 1 deletion src/scripts/hooks/render-token-hud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const RenderTokenHUD = {
const html = $html[0];
game.pf2e.StatusEffects.onRenderTokenHUD(html, data);

const token = canvas.scene?.tokens.get(data._id)?.object;
const token = canvas.scene?.tokens.get(data._id ?? "")?.object;
RenderTokenHUD.addClownCarButton(html, token);
});
},
Expand Down
2 changes: 1 addition & 1 deletion tests/mocks/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class MockToken {
}

get id(): string {
return this._source._id;
return this._source._id!;
}

get name(): string {
Expand Down
8 changes: 4 additions & 4 deletions types/foundry/common/data/data.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DataModel, Document } from "../abstract/module.d.ts";
import type { BaseActor, BaseActorDelta, BaseScene, BaseToken } from "../documents/module.d.ts";
import type { TokenBarData, TokenSource } from "../documents/token.d.ts";
import type { TokenSource } from "../documents/token.d.ts";
import type * as fields from "./fields.d.ts";

/**
Expand Down Expand Up @@ -132,7 +132,7 @@ export class TextureData extends fields.SchemaField<TextureDataSchema> {

type TextureDataSchema = {
/** The URL of the texture source. */
src: fields.FilePathField;
src: fields.FilePathField<ImageFilePath | VideoFilePath, ImageFilePath | VideoFilePath, true, false, true>;
/** The scale of the texture in the X dimension. */
scaleX: fields.NumberField<number, number, false, false>;
/** The scale of the texture in the Y dimension. */
Expand Down Expand Up @@ -165,9 +165,9 @@ export class PrototypeToken<TParent extends BaseActor | null> extends Document<T

lightAnimation: AnimationData;

bar1: TokenBarData;
bar1: BaseToken["bar1"];

bar2: TokenBarData;
bar2: BaseToken["bar1"];
}

export interface PrototypeToken<TParent extends BaseActor | null>
Expand Down
13 changes: 3 additions & 10 deletions types/foundry/common/data/fields.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,14 +494,6 @@ export class ObjectField<
): DataModelValidationFailure | boolean | void;
}

export type FlagField<
TSourceProp extends { [K in string]?: Record<string, unknown> } = { [K in string]?: Record<string, unknown> },
TModelProp = TSourceProp,
TRequired extends boolean = false,
TNullable extends boolean = false,
THasInitial extends boolean = true
> = ObjectField<TSourceProp, TModelProp, TRequired, TNullable, THasInitial>;

type ArrayFieldOptions<
TSourceProp extends unknown[],
TRequired extends boolean,
Expand Down Expand Up @@ -644,7 +636,8 @@ export class EmbeddedDataField<

override initialize(
value: MaybeSchemaProp<TModelProp["schema"]["fields"], TRequired, TNullable, THasInitial>,
model: ConstructorOf<abstract.DataModel>
model: ConstructorOf<abstract.DataModel>,
options?: object
): MaybeSchemaProp<TModelProp, TRequired, TNullable, THasInitial>;

override toObject(
Expand Down Expand Up @@ -1040,6 +1033,6 @@ declare global {
type HexColorString = `#${string}`;
type AudioFilePath = `${string}.${AudioFileExtension}`;
type ImageFilePath = `${string}.${ImageFileExtension}`;
type VideoFilePath = `${string}.${VideoFileExtension}` | ImageFilePath;
type VideoFilePath = `${string}.${VideoFileExtension}`;
type FilePath = AudioFilePath | ImageFilePath | VideoFilePath;
}
4 changes: 2 additions & 2 deletions types/foundry/common/documents/actor-delta.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { BaseActiveEffect, BaseActor, BaseItem, BaseToken, ItemSource } fro
* @param data Initial data used to construct the ActorDelta.
* @param context Construction context options.
*/
export default class BaseActorDelta<TParent extends BaseToken | null> extends abstract.Document<
export default class BaseActorDelta<TParent extends abstract.Document | null> extends abstract.Document<
TParent,
ActorDeltaSchema
> {
Expand All @@ -24,7 +24,7 @@ export default class BaseActorDelta<TParent extends BaseToken | null> extends ab
static override defineSchema(): ActorDeltaSchema;
}

export default interface BaseActorDelta<TParent extends BaseToken | null>
export default interface BaseActorDelta<TParent extends abstract.Document | null>
extends abstract.Document<TParent, ActorDeltaSchema>,
ModelPropsFromSchema<ActorDeltaSchema> {
readonly _source: ActorDeltaSource;
Expand Down
Loading

0 comments on commit d8be25e

Please sign in to comment.