mirror of
https://github.com/Foundryborne/daggerheart.git
synced 2026-01-13 12:11:07 +01:00
FEAT: new ForeignDocumentUUIDField
FIX: Remove unnecessary fields FEAT: use ForeignDocumentUUIDField in the Item Class DataModel
This commit is contained in:
parent
996e72d4f2
commit
00fc07d67c
5 changed files with 43 additions and 98 deletions
30
module/data/fields/foreignDocumentUUIDField.mjs
Normal file
30
module/data/fields/foreignDocumentUUIDField.mjs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
* A class extending DocumentUUIDField to allow selecting a foreign document reference
|
||||
* that resolves to either the document, the index(for items in compenidums) or the UUID string.
|
||||
*/
|
||||
export default class ForeignDocumentUUIDField extends foundry.data.fields.DocumentUUIDField {
|
||||
static get _defaults() {
|
||||
return foundry.utils.mergeObject(super._defaults, {
|
||||
nullable: true,
|
||||
readonly: false,
|
||||
idOnly: false,
|
||||
});
|
||||
}
|
||||
|
||||
initialize(value, _model, _options = {}) {
|
||||
if (this.idOnly) return value;
|
||||
return () => {
|
||||
try {
|
||||
const doc = fromUuidSync(value);
|
||||
return doc;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return value ?? null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
toObject(value) {
|
||||
return value?.uuid ?? value;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue