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

Write new ClientDocument type definition, use with JournalEntry #6462

Merged
merged 1 commit into from
Feb 21, 2023
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
Write new ClientDocument type definition, use with JournalEntry
  • Loading branch information
stwlam committed Feb 20, 2023
commit bf691d2da397248ef33567a9ab53b500d1e44677
20 changes: 5 additions & 15 deletions .betterer.results
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,22 @@ exports[`too-much-lint`] = {
[9, 14, 3, "Unexpected any. Specify a different type.", "193409811"],
[10, 15, 3, "Unexpected any. Specify a different type.", "193409811"]
],
"types/foundry/client/collections/compendium-collection.d.ts:766536613": [
[212, 23, 3, "Unexpected any. Specify a different type.", "193409811"]
"types/foundry/client/collections/compendium-collection.d.ts:775132723": [
[246, 23, 3, "Unexpected any. Specify a different type.", "193409811"]
],
"types/foundry/client/documents/mixins/canvas-document-mixin.d.ts:3303075782": [
[16, 22, 3, "Unexpected any. Specify a different type.", "193409811"],
[21, 57, 3, "Unexpected any. Specify a different type.", "193409811"],
[54, 57, 3, "Unexpected any. Specify a different type.", "193409811"]
],
"types/foundry/client/documents/mixins/client-document-mixin.d.ts:1741650789": [
"types/foundry/client/documents/mixins/client-document-mixin.d.ts:122377703": [
[8, 18, 3, "Unexpected any. Specify a different type.", "193409811"],
[65, 43, 3, "Unexpected any. Specify a different type.", "193409811"],
[165, 17, 3, "Unexpected any. Specify a different type.", "193409811"],
[166, 19, 3, "Unexpected any. Specify a different type.", "193409811"],
[169, 21, 3, "Unexpected any. Specify a different type.", "193409811"],
[307, 44, 3, "Unexpected any. Specify a different type.", "193409811"]
],
"types/foundry/client/game.d.ts:2501689875": [
[182, 32, 3, "Unexpected any. Specify a different type.", "193409811"]
],
"types/foundry/client/pixi/placeables-layer/base.d.ts:7710705": [
[134, 20, 3, "Unexpected any. Specify a different type.", "193409811"],
[158, 20, 3, "Unexpected any. Specify a different type.", "193409811"],
Expand All @@ -51,15 +48,8 @@ exports[`too-much-lint`] = {
[138, 36, 3, "Unexpected any. Specify a different type.", "193409811"],
[143, 44, 3, "Unexpected any. Specify a different type.", "193409811"]
],
"types/foundry/common/abstract/data.d.ts:1438318714": [
[22, 34, 3, "Unexpected any. Specify a different type.", "193409811"]
],
"types/foundry/common/abstract/document.d.ts:459111865": [
[572, 29, 3, "Unexpected any. Specify a different type.", "193409811"]
],
"types/foundry/common/utils/collection.d.ts:1050385599": [
[111, 31, 3, "Unexpected any. Specify a different type.", "193409811"],
[113, 43, 3, "Unexpected any. Specify a different type.", "193409811"]
"types/foundry/common/abstract/data.d.ts:2798586197": [
[25, 34, 3, "Unexpected any. Specify a different type.", "193409811"]
]
}`
};
2 changes: 1 addition & 1 deletion packs/scripts/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const npcSystemKeys = new Set(Object.keys({ ...templateJSON.Actor.templates.comm
const idsToNames: Map<string, Map<string, string>> = new Map();

type CompendiumDocumentPF2e = ActorPF2e | ItemPF2e | JournalEntry | MacroPF2e | RollTable;
type PackEntry = CompendiumDocumentPF2e["data"]["_source"];
type PackEntry = CompendiumDocumentPF2e["_source"];

function assertDocIdSame(newSource: PackEntry, jsonPath: string): void {
if (fs.existsSync(jsonPath)) {
Expand Down
2 changes: 1 addition & 1 deletion packs/scripts/packman/compendium-pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface REMaybeWithUUIDs extends RuleElementSource {
uuid?: unknown;
}

type CompendiumSource = CompendiumDocument["data"]["_source"];
type CompendiumSource = CompendiumDocument["_source"];
export function isActorSource(docSource: CompendiumSource): docSource is ActorSourcePF2e {
return (
"system" in docSource && isObject(docSource.system) && "items" in docSource && Array.isArray(docSource.items)
Expand Down
6 changes: 3 additions & 3 deletions packs/scripts/run-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const isItemData = (docSource: CompendiumSource): docSource is ItemSourcePF2e =>
return "type" in docSource && itemTypes.includes(docSource.type);
};

const isJournalEntryData = (docSource: CompendiumSource): docSource is foundry.data.JournalEntrySource => {
const isJournalEntryData = (docSource: CompendiumSource): docSource is foundry.documents.JournalEntrySource => {
return "pages" in docSource && Array.isArray(docSource.pages);
};

Expand Down Expand Up @@ -186,7 +186,7 @@ async function migrate() {
let source:
| ActorSourcePF2e
| ItemSourcePF2e
| foundry.data.JournalEntrySource
| foundry.documents.JournalEntrySource
| foundry.data.MacroSource
| foundry.data.RollTableSource;
try {
Expand All @@ -202,7 +202,7 @@ async function migrate() {
const updated = await (async (): Promise<
| ActorSourcePF2e
| ItemSourcePF2e
| foundry.data.JournalEntrySource
| foundry.documents.JournalEntrySource
| foundry.data.MacroSource
| foundry.data.RollTableSource
> => {
Expand Down
2 changes: 1 addition & 1 deletion src/module/active-effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export class ActiveEffectPF2e extends ActiveEffect {
constructor(
data: DeepPartial<foundry.data.ActiveEffectSource>,
context?: DocumentConstructionContext<ActiveEffectPF2e>
context?: DocumentConstructionContext<ActiveEffectPF2e["parent"]>
) {
data.disabled = true;
data.transfer = false;
Expand Down
7 changes: 5 additions & 2 deletions src/module/actor/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ActorPF2e extends Actor<TokenDocumentPF2e, ItemTypeMap> {
/** A cached copy of `Actor#itemTypes`, lazily regenerated every data preparation cycle */
private _itemTypes?: { [K in keyof ItemTypeMap]: Embedded<ItemTypeMap[K]>[] } | null;

constructor(data: PreCreate<ActorSourcePF2e>, context: DocumentConstructionContext<ActorPF2e> = {}) {
constructor(data: PreCreate<ActorSourcePF2e>, context: DocumentConstructionContext<TokenDocumentPF2e | null> = {}) {
super(data, context);

// Add debounced checkAreaEffects method
Expand Down Expand Up @@ -1604,7 +1604,10 @@ interface ActorUpdateContext<T extends ActorPF2e> extends DocumentUpdateContext<

/** A `Proxy` to to get Foundry to construct `ActorPF2e` subclasses */
const ActorProxyPF2e = new Proxy(ActorPF2e, {
construct(_target, args: [source: PreCreate<ActorSourcePF2e>, context: DocumentConstructionContext<ActorPF2e>]) {
construct(
_target,
args: [source: PreCreate<ActorSourcePF2e>, context: DocumentConstructionContext<ActorPF2e["parent"]>]
) {
return new CONFIG.PF2E.Actor.documentClasses[args[0].type](...args);
},
});
Expand Down
5 changes: 1 addition & 4 deletions src/module/chat-message/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ import * as Listeners from "./listeners";

class ChatMessagePF2e extends ChatMessage<ActorPF2e> {
/** The chat log doesn't wait for data preparation before rendering, so set some data in the constructor */
constructor(
data: DeepPartial<ChatMessageSourcePF2e> = {},
context: DocumentConstructionContext<ChatMessagePF2e> = {}
) {
constructor(data: DeepPartial<ChatMessageSourcePF2e> = {}, context: DocumentConstructionContext<null> = {}) {
data.flags = mergeObject(expandObject(data.flags ?? {}), { core: {}, pf2e: {} });
super(data, context);

Expand Down
5 changes: 4 additions & 1 deletion src/module/item/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,10 @@ interface ItemPF2e extends Item<ActorPF2e> {

/** A `Proxy` to to get Foundry to construct `ItemPF2e` subclasses */
const ItemProxyPF2e = new Proxy(ItemPF2e, {
construct(_target, args: [source: PreCreate<ItemSourcePF2e>, context: DocumentConstructionContext<ItemPF2e>]) {
construct(
_target,
args: [source: PreCreate<ItemSourcePF2e>, context: DocumentConstructionContext<ItemPF2e["parent"]>]
) {
return new CONFIG.PF2E.Item.documentClasses[args[0].type](...args);
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/module/item/spell/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { InstancePool } from "@system/damage/terms";
import { DamageModifierDialog } from "@system/damage/modifier-dialog";
import { MeasuredTemplatePF2e } from "@module/canvas";

interface SpellConstructionContext extends DocumentConstructionContext<SpellPF2e> {
interface SpellConstructionContext extends DocumentConstructionContext<ActorPF2e | null> {
fromConsumable?: boolean;
}

Expand Down
2 changes: 1 addition & 1 deletion src/module/migration/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ interface MigrationBase {
* Update the macro to the latest schema version.
* @param source Macro data to update. This should be a `MacroData` from the previous version.
*/
updateJournalEntry?(source: foundry.data.JournalEntrySource): Promise<void>;
updateJournalEntry?(source: foundry.documents.JournalEntrySource): Promise<void>;

/**
* Update the macro to the latest schema version.
Expand Down
2 changes: 1 addition & 1 deletion src/module/migration/migrations/602-update-diehard-feat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class Migration602UpdateDiehardFeat extends MigrationBase {
static override version = 0.602;
override requiresFlush = true;

private diehardPromise: Promise<ClientDocument | null>;
private diehardPromise: Promise<ClientDocument | ClientDocument2 | null>;

constructor() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ interface ReplaceItemArgs {
items: ItemSourcePF2e[];
type: string;
slug: string;
replacement: ClientDocument | null;
replacement: ClientDocument | ClientDocument2 | null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ export class Migration692CraftingEntryFeatReplacement extends MigrationBase {
static override version = 0.692;
override requiresFlush = true;

private slugToPromise = new Map<string, Promise<ClientDocument<foundry.abstract.Document> | null>>([
private slugToPromise = new Map<
string,
Promise<ClientDocument<foundry.abstract.Document> | ClientDocument2 | null>
>([
["advanced-alchemy", fromUuid("Compendium.pf2e.classfeatures.Pe0zmIqyTBc2Td0I")],
["field-discovery-bomber", fromUuid("Compendium.pf2e.classfeatures.8QAFgy9U8PxEa7Dw")],
["field-discovery-chirurgeon", fromUuid("Compendium.pf2e.classfeatures.qC0Iz6SlG2i9gv6g")],
Expand Down Expand Up @@ -68,5 +71,5 @@ export class Migration692CraftingEntryFeatReplacement extends MigrationBase {
interface ReplaceItemArgs {
items: ItemSourcePF2e[];
current: ItemSourcePF2e;
replacement: ClientDocument | null;
replacement: ClientDocument | ClientDocument2 | null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class Migration805InlineDamageRolls extends MigrationBase {
source.system = recursiveReplaceString(source.system, (s) => this.#updateDamageFormula(s));
}

override async updateJournalEntry(source: foundry.data.JournalEntrySource): Promise<void> {
override async updateJournalEntry(source: foundry.documents.JournalEntrySource): Promise<void> {
source.pages = recursiveReplaceString(source.pages, (s) => this.#updateDamageFormula(s));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class Migration811InlineDamageRollsPersistent extends MigrationBase {
source.system = recursiveReplaceString(source.system, (s) => this.#updateDamageFormula(s));
}

override async updateJournalEntry(source: foundry.data.JournalEntrySource): Promise<void> {
override async updateJournalEntry(source: foundry.documents.JournalEntrySource): Promise<void> {
source.pages = recursiveReplaceString(source.pages, (s) => this.#updateDamageFormula(s));
}
}
2 changes: 1 addition & 1 deletion src/module/migration/migrations/821-inline-damage-rolls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class Migration821InlineDamageRolls extends MigrationBase {
source.system = recursiveReplaceString(source.system, (s) => this.#updateDamageFormula(s));
}

override async updateJournalEntry(source: foundry.data.JournalEntrySource): Promise<void> {
override async updateJournalEntry(source: foundry.documents.JournalEntrySource): Promise<void> {
source.pages = recursiveReplaceString(source.pages, (s) => this.#updateDamageFormula(s));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/module/migration/runner/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ export class MigrationRunnerBase {
}

async getUpdatedJournalEntry(
source: foundry.data.JournalEntrySource,
source: foundry.documents.JournalEntrySource,
migrations: MigrationBase[]
): Promise<foundry.data.JournalEntrySource> {
): Promise<foundry.documents.JournalEntrySource> {
const clone = deepClone(source);

for (const migration of migrations) {
Expand Down
40 changes: 20 additions & 20 deletions src/scripts/register-sheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,16 @@ import {
} from "@item";
import { AfflictionSheetPF2e } from "@item/affliction";
import { JournalSheetPF2e, JournalTextTinyMCESheetPF2e } from "@module/journal-entry/sheet";
import { UserPF2e } from "@module/user";
import { UserConfigPF2e } from "@module/user/sheet";
import { TokenConfigPF2e, TokenDocumentPF2e } from "@scene";
import { SceneConfigPF2e } from "@scene/sheet";
import { LocalizePF2e } from "@system/localize";

export function registerSheets() {
export function registerSheets(): void {
const translations = LocalizePF2e.translations.PF2E;
const sheetLabel = translations.SheetLabel;

DocumentSheetConfig.unregisterSheet(JournalEntry, "core", JournalSheet);
DocumentSheetConfig.registerSheet(JournalEntry, "pf2e", JournalSheetPF2e, {
label: () =>
game.i18n.format("SHEETS.DefaultDocumentSheet", { document: game.i18n.localize("DOCUMENT.JournalEntry") }),
makeDefault: true,
});

// Replace the TinyMCE sheet with the version that'll let us inject themes
DocumentSheetConfig.unregisterSheet(JournalEntryPage, "core", JournalTextTinyMCESheet);
DocumentSheetConfig.registerSheet(JournalEntryPage, "pf2e", JournalTextTinyMCESheetPF2e, {
types: ["text"],
label: game.i18n.localize("EDITOR.TinyMCE"),
});

DocumentSheetConfig.registerSheet(Scene, "pf2e", SceneConfigPF2e, { makeDefault: true });
Scenes.registerSheet("pf2e", SceneConfigPF2e, { makeDefault: true });
DocumentSheetConfig.registerSheet(TokenDocumentPF2e, "pf2e", TokenConfigPF2e, { makeDefault: true });

// ACTORS
Expand Down Expand Up @@ -156,10 +141,25 @@ export function registerSheets() {
});
}

// User
// JOURNAL

DocumentSheetConfig.unregisterSheet(User, "core", UserConfig);
DocumentSheetConfig.registerSheet(UserPF2e, "pf2e", UserConfigPF2e, {
Journal.unregisterSheet("core", JournalSheet);
Journal.registerSheet("pf2e", JournalSheetPF2e, {
label: () =>
game.i18n.format("SHEETS.DefaultDocumentSheet", { document: game.i18n.localize("DOCUMENT.JournalEntry") }),
makeDefault: true,
});

// Replace the TinyMCE sheet with the version that'll let us inject themes
DocumentSheetConfig.unregisterSheet(JournalEntryPage, "core", JournalTextTinyMCESheet);
DocumentSheetConfig.registerSheet(JournalEntryPage, "pf2e", JournalTextTinyMCESheetPF2e, {
types: ["text"],
label: game.i18n.localize("EDITOR.TinyMCE"),
});

// User
Users.unregisterSheet("core", UserConfig);
Users.registerSheet("pf2e", UserConfigPF2e, {
makeDefault: true,
label: () => game.i18n.format("SHEETS.DefaultDocumentSheet", { document: game.i18n.localize("DOCUMENT.User") }),
});
Expand Down
13 changes: 9 additions & 4 deletions src/scripts/ui/inline-roll-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { MeasuredTemplatePF2e } from "@module/canvas";
const inlineSelector = ["action", "check", "effect-area", "repost"].map((keyword) => `[data-pf2-${keyword}]`).join(",");

export const InlineRollLinks = {
injectRepostElement: (links: HTMLElement[], foundryDoc?: ClientDocument): void => {
injectRepostElement: (links: HTMLElement[], foundryDoc?: ClientDocument | ClientDocument2): void => {
for (const link of links) {
if (!foundryDoc || foundryDoc.isOwner) link.classList.add("with-repost");

Expand All @@ -37,7 +37,7 @@ export const InlineRollLinks = {
}
},

listen: ($html: HTMLElement | JQuery, foundryDoc?: ClientDocument): void => {
listen: ($html: HTMLElement | JQuery, foundryDoc?: ClientDocument | ClientDocument2): void => {
const html = $html instanceof HTMLElement ? $html : $html[0]!;
if ($html instanceof HTMLElement) $html = $($html);

Expand All @@ -47,7 +47,9 @@ export const InlineRollLinks = {

InlineRollLinks.flavorDamageRolls(html, foundryDoc instanceof ActorPF2e ? foundryDoc : null);

const documentFromDOM = (html: HTMLElement): ActorPF2e | JournalEntry | JournalEntryPage | null => {
const documentFromDOM = (
html: HTMLElement
): ActorPF2e | JournalEntry | JournalEntryPage<JournalEntry> | null => {
if (foundryDoc instanceof ChatMessagePF2e) return foundryDoc.actor ?? foundryDoc.journalEntry ?? null;
if (
foundryDoc instanceof ActorPF2e ||
Expand Down Expand Up @@ -249,7 +251,10 @@ export const InlineRollLinks = {
});
},

repostAction: (target: HTMLElement, document: ActorPF2e | JournalEntry | JournalEntryPage | null = null): void => {
repostAction: (
target: HTMLElement,
document: ActorPF2e | JournalEntry | JournalEntryPage<JournalEntry> | null = null
): void => {
if (!["pf2Action", "pf2Check", "pf2EffectArea"].some((d) => d in target.dataset)) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/ui/user-visibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class UserVisibilityPF2e {
type UserVisibility = "all" | "owner" | "gm" | "none";

interface ProcessOptions {
document?: ClientDocument | null;
document?: ClientDocument | ClientDocument2 | null;
message?: ChatMessagePF2e;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/fakes/fake-actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class FakeActor {

id: string;

constructor(data: ActorSourcePF2e, public options: DocumentConstructionContext<ActorPF2e> = {}) {
constructor(data: ActorSourcePF2e, public options: DocumentConstructionContext<null> = {}) {
this.id = data._id;
this._data = duplicate(data);
this._data.items ??= [];
Expand Down
2 changes: 1 addition & 1 deletion tests/fakes/fake-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class FakeItem {

parent: ActorPF2e | null = null;

constructor(data: ItemSourcePF2e, public options: DocumentConstructionContext<ItemPF2e> = {}) {
constructor(data: ItemSourcePF2e, public options: DocumentConstructionContext<ActorPF2e | null> = {}) {
this._data = duplicate(data);
this.parent = options.parent ?? null;
}
Expand Down
5 changes: 4 additions & 1 deletion tests/fakes/fake-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export class FakeToken {
parent: ScenePF2e | null;
data: foundry.data.TokenData<TokenDocumentPF2e>;

constructor(data: foundry.data.TokenSource, context: TokenDocumentConstructionContext<TokenDocumentPF2e> = {}) {
constructor(
data: foundry.data.TokenSource,
context: TokenDocumentConstructionContext<ScenePF2e | null, ActorPF2e | null> = {}
) {
this.data = duplicate(data) as foundry.data.TokenData<TokenDocumentPF2e>;
this.parent = context.parent ?? null;
this._actor = context.actor ?? null;
Expand Down
Loading