FEAT: add TypedPseudoDocument

REFACTOR: PreudoDocument
FIX: Typos Bug
This commit is contained in:
Joaquin Pereyra 2025-06-11 01:14:14 -03:00
parent a0b0411a48
commit 88bf26dad0
21 changed files with 559 additions and 447 deletions

View file

@ -0,0 +1,48 @@
import BasePseudoDocument from './base.mjs';
import SheetManagementMixin from './sheetManagementMixin.mjs';
/** @extends BasePseudoDocument */
export default class PseudoDocument extends SheetManagementMixin(BasePseudoDocument) {
static get TYPES() {
return (this._TYPES ??= Object.freeze(CONFIG.daggerheart.pseudoDocuments[this.metadata.name]));
}
static _TYPES;
/* -------------------------------------------- */
/**
* The type of this shape.
* @type {string}
*/
static TYPE = '';
/* -------------------------------------------- */
/** @override */
static defineSchema() {
const { fields } = foundry.data;
return Object.assign(super.defineSchema(), {
type: new fields.StringField({
required: true,
blank: false,
initial: this.TYPE,
validate: value => value === this.TYPE,
validationError: `must be equal to "${this.TYPE}"`
})
});
}
/** @inheritdoc */
static async create(data = {}, { parent, ...operation } = {}) {
data = foundry.utils.deepClone(data);
if (!data.type) data.type = Object.keys(this.TYPES)[0];
if (!(data.type in this.TYPES)) {
throw new Error(
`The '${data.type}' type is not a valid type for a '${this.metadata.documentName}' pseudo-document!`
);
}
return super.create(data, { parent, ...operation });
}
}