mirror of
https://github.com/Foundryborne/daggerheart.git
synced 2026-01-12 03:31:07 +01:00
Bug/124 foreigndocumentuuidfields fail initial initialization (#150)
* FIX: Remove the psudo-documents because they will be used. * FIX: ForeignDocumentUUIDField initialize like a getter FEAT: ForeignDocumentUUIDArrayField created and used * REFACTOR: prettier format --------- Co-authored-by: Joaquin Pereyra <joaquinpereyra98@users.noreply.github.com>
This commit is contained in:
parent
96ed90b5fc
commit
a0a5196825
20 changed files with 33 additions and 632 deletions
|
|
@ -1,3 +1,3 @@
|
|||
export { default as FormulaField } from './formulaField.mjs';
|
||||
export { default as ForeignDocumentUUIDField } from './foreignDocumentUUIDField.mjs';
|
||||
export { default as PseudoDocumentsField } from './pseudoDocumentsField.mjs';
|
||||
export { default as ForeignDocumentUUIDArrayField } from './foreignDocumentUUIDArrayField.mjs';
|
||||
|
|
|
|||
20
module/data/fields/foreignDocumentUUIDArrayField.mjs
Normal file
20
module/data/fields/foreignDocumentUUIDArrayField.mjs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import ForeignDocumentUUIDField from './foreignDocumentUUIDField.mjs';
|
||||
/**
|
||||
* A subclass of {@link foundry.data.fields.ArrayField} that defines an array of foreign document UUID references.
|
||||
*/
|
||||
export default class ForeignDocumentUUIDArrayField extends foundry.data.fields.ArrayField {
|
||||
/**
|
||||
* @param {foundry.data.types.DocumentUUIDFieldOptions} [fieldOption] - Options to configure each individual ForeignDocumentUUIDField.
|
||||
* @param {foundry.data.types.ArrayFieldOptions} [options] - Options to configure the array behavior
|
||||
* @param {foundry.data.types.DataFieldContext} [context] - Optional context for schema processing
|
||||
*/
|
||||
constructor(fieldOption = {}, options = {}, context = {}) {
|
||||
super(new ForeignDocumentUUIDField(fieldOption), options, context);
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
initialize(value, model, options = {}) {
|
||||
const v = super.initialize(value, model, options);
|
||||
return () => v.map(entry => (typeof entry === 'function' ? entry() : entry));
|
||||
}
|
||||
}
|
||||
|
|
@ -23,7 +23,7 @@ export default class ForeignDocumentUUIDField extends foundry.data.fields.Docume
|
|||
/**@override */
|
||||
initialize(value, _model, _options = {}) {
|
||||
if (this.idOnly) return value;
|
||||
return (() => {
|
||||
return () => {
|
||||
try {
|
||||
const doc = fromUuidSync(value);
|
||||
return doc;
|
||||
|
|
@ -31,7 +31,7 @@ export default class ForeignDocumentUUIDField extends foundry.data.fields.Docume
|
|||
console.error(error);
|
||||
return value ?? null;
|
||||
}
|
||||
})();
|
||||
};
|
||||
}
|
||||
|
||||
/**@override */
|
||||
|
|
|
|||
|
|
@ -1,56 +0,0 @@
|
|||
import PseudoDocument from '../pseudo-documents/base/pseudoDocument.mjs';
|
||||
|
||||
const { TypedObjectField, TypedSchemaField } = foundry.data.fields;
|
||||
|
||||
/**
|
||||
* @typedef _PseudoDocumentsFieldOptions
|
||||
* @property {Number} [max] - The maximum amount of elements (default: `Infinity`)
|
||||
* @property {String[]} [validTypes] - Allowed pseudo-documents types (default: `[]`)
|
||||
* @property {Function} [validateKey] - callback for validate keys of the object;
|
||||
|
||||
* @typedef {foundry.data.types.DataFieldOptions & _PseudoDocumentsFieldOptions} PseudoDocumentsFieldOptions
|
||||
*/
|
||||
export default class PseudoDocumentsField extends TypedObjectField {
|
||||
/**
|
||||
* @param {PseudoDocument} model - The PseudoDocument of each entry in this collection.
|
||||
* @param {PseudoDocumentsFieldOptions} [options] - Options which configure the behavior of the field
|
||||
* @param {foundry.data.types.DataFieldContext} [context] - Additional context which describes the field
|
||||
*/
|
||||
constructor(model, options = {}, context = {}) {
|
||||
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 = 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);
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
static get _defaults() {
|
||||
return Object.assign(super._defaults, {
|
||||
max: Infinity,
|
||||
validTypes: []
|
||||
});
|
||||
}
|
||||
|
||||
/** @override */
|
||||
_validateType(value, options = {}) {
|
||||
if (Object.keys(value).length > this.max) throw new Error(`cannot have more than ${this.max} elements`);
|
||||
return super._validateType(value, options);
|
||||
}
|
||||
|
||||
/** @override */
|
||||
initialize(value, model, options = {}) {
|
||||
if (!value) return;
|
||||
value = super.initialize(value, model, options);
|
||||
const collection = new foundry.utils.Collection(Object.values(value).map(d => [d._id, d]));
|
||||
return collection;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue