FIX: CONFIG types

This commit is contained in:
Joaquin Pereyra 2025-06-11 13:43:11 -03:00
parent 88bf26dad0
commit 27777dddba
7 changed files with 77 additions and 30 deletions

View file

@ -1,7 +1,15 @@
import { pseudoDocuments } from "../data/_module.mjs";
//CONFIG.daggerheart.pseudoDocuments
export default {
feature: {
weapon: pseudoDocuments.feature.WeaponFeature,
label: "DAGGERHEART.Feature.Label",
documentClass: pseudoDocuments.feature.BaseFeatureData,
types: {
weapon:{
label: "DAGGERHEART.Feature.Weapon.Label",
documentClass: pseudoDocuments.feature.WeaponFeature,
}
}
}
};

View file

@ -20,16 +20,14 @@ export default class PseudoDocumentsField extends TypedObjectField {
options.validateKey ||= key => foundry.data.validators.isValidId(key);
if (!foundry.utils.isSubclass(model, PseudoDocument)) throw new Error('The model must be a PseudoDocument');
const allTypes = foundry.utils.duplicate(model.TYPES);
options.validTypes ??= Object.keys(allTypes);
const filteredTypes = {};
for (const typeName of options.validTypes) {
if (typeName in allTypes) {
filteredTypes[typeName] = allTypes[typeName];
} else {
console.warn(`Document type "${typeName}" is not found in model.TYPES`);
}
}
const allTypes = model.TYPES;
const filteredTypes = options.validTypes
? Object.fromEntries(
Object.entries(allTypes).filter(([key]) => options.validTypes.includes(key))
)
: allTypes;
const field = new TypedSchemaField(filteredTypes);
super(field, options, context);
}

View file

@ -68,7 +68,7 @@ export default class BasePseudoDocument extends foundry.abstract.DataModel {
/**
* The parent document of this pseudo-document.
* @type {Document}
* @type {foundry.abstract.Document}
*/
get document() {
let parent = this;
@ -122,14 +122,13 @@ export default class BasePseudoDocument extends foundry.abstract.DataModel {
* @param {string} embeddedName The document name of the embedded pseudo-document.
* @param {string} id The id of the embedded pseudo-document.
* @param {object} [options] Retrieval options.
* @param {boolean} [options.invalid] Retrieve an invalid pseudo-document?
* @param {boolean} [options.strinct] Throw an error if the embedded pseudo-document does not exist?
* @returns {PseudoDocument|null}
*/
getEmbeddedDocument(embeddedName, id, { invalid = false, strict = false } = {}) {
getEmbeddedDocument(embeddedName, id, { strict = false } = {}) {
const embeds = this.constructor.metadata.embedded ?? {};
if (embeddedName in embeds) {
return foundry.utils.getProperty(this, embeds[embeddedName]).get(id) ?? null;
return foundry.utils.getProperty(this, embeds[embeddedName]).get(id, { strict }) ?? null;
}
return null;
}
@ -172,7 +171,8 @@ export default class BasePseudoDocument extends foundry.abstract.DataModel {
}
const update = { [`system.${fieldPath}.${id}`]: { ...data, _id: id } };
return parent.update(update, operation);
const updatedParent = await parent.update(update, operation);
return foundry.utils.getProperty(updatedParent, `system.${fieldPath}.${id}`);
}
/**

View file

@ -4,13 +4,13 @@ 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]));
const { types } = CONFIG.daggerheart.pseudoDocuments[this.metadata.name];
const typeEntries = Object.entries(types).map(([key, { documentClass }]) => [key, documentClass]);
return (this._TYPES ??= Object.freeze(Object.fromEntries(typeEntries)));
}
static _TYPES;
/* -------------------------------------------- */
/**
* The type of this shape.
* @type {string}
@ -19,6 +19,17 @@ export default class PseudoDocument extends SheetManagementMixin(BasePseudoDocum
/* -------------------------------------------- */
static getTypesChoices(validTypes) {
const { types } = CONFIG.daggerheart.pseudoDocuments[model.metadata.name];
const typeEntries = Object.entries(types)
.map(([key, { label }]) => [key, label])
.filter(([key]) => !validTypes || validTypes.includes(key));
return Object.entries(typeEntries);
}
/* -------------------------------------------- */
/** @override */
static defineSchema() {
const { fields } = foundry.data;

View file

@ -104,16 +104,52 @@ export default function SheetManagementMixin(Base) {
* @param {object} context
* @param {foundry.documents.Item} context.parent A parent for the document.
* @param {string[]|null} [context.types] A list of types to restrict the choices to, or null for no restriction.
* @returns {Promise<PseudoDocument|null>}
* @returns {Promise<BasePseudoDocument|null>}
*/
static async createDialog(data = {}, { parent, types = null, ...options } = {}) {}
static async createDialog(data = {}, { parent, types = null, ...options } = {}) {
// TODO
}
/**
* Present a Dialog form to confirm deletion of this PseudoDocument.
* @param {object} [options] Positioning and sizing options for the resulting dialog.
* @returns {Promise<PseudoDocument>} A Promise which resolves to the deleted PseudoDocument.
* @param {object} [options] - Additional options passed to `DialogV2.confirm`;
* @returns {Promise<foundry.abstract.Document>} A Promise which resolves to the deleted PseudoDocument.
*/
async deleteDialog(options = {}) {}
async deleteDialog(options = {}) {
const type = game.i18n.localize(this.constructor.metadata.label);
const content = options.content ?? `<p>
<strong>${game.i18n.localize("AreYouSure")}</strong>
${game.i18n.format("SIDEBAR.DeleteWarning", { type })}
</p>`;
return foundry.applications.api.DialogV2.confirm({
content,
yes: { callback: () => this.delete(operation) },
window: {
icon: "fa-solid fa-trash",
title: `${game.i18n.format("DOCUMENT.Delete", { type })}: ${this.name}`
},
...options
});
}
/**
* Gets the default new name for a Document
* @param {object} collection - Collection of Documents
* @returns {string}
*/
static defaultName(collection) {
const documentName = this.metadata.name;
const takenNames = new Set();
for (const document of collection) takenNames.add(document.name);
const config = CONFIG.daggerheart.pseudoDocuments[documentName];
const baseName = game.i18n.localize(config.label);
let name = baseName;
let index = 1;
while (takenNames.has(name)) name = `${baseName} (${++index})`;
return name;
}
}
return PseudoDocumentWithSheets;

View file

@ -7,7 +7,6 @@ export default class BaseFeatureData extends PseudoDocument {
super.metadata,
{
name: 'feature',
label: 'DAGGERHEART.Feature.Label',
embedded: {},
sheetClass: null //TODO: define feature-sheet
},

View file

@ -3,9 +3,4 @@ import BaseFeatureData from './baseFeatureData.mjs';
export default class WeaponFeature extends BaseFeatureData {
/**@override */
static TYPE = 'weapon';
/**@inheritdoc */
static get metadata() {
return foundry.utils.mergeObject(super.metadata, {}, { inplace: false });
}
}