Add header control for adding an embedded item into an actor.

This commit is contained in:
Oliver 2026-04-08 01:08:52 -06:00
parent 380ed3fe15
commit fc24caf08a
3 changed files with 51 additions and 14 deletions

View file

@ -84,6 +84,7 @@
}, },
"PlayerSheet": { "PlayerSheet": {
"manage-attributes": "Manage Attributes", "manage-attributes": "Manage Attributes",
"create-item": "Create Embedded Item",
"current-value": "Current value", "current-value": "Current value",
"max-value": "Maximum value", "max-value": "Maximum value",
"carry-capacity-used": "({percent}% Used)", "carry-capacity-used": "({percent}% Used)",

View file

@ -35,6 +35,7 @@ export class PlayerSheet extends
closeOnSubmit: false, closeOnSubmit: false,
}, },
actions: { actions: {
createEmbeddedItem: this.#createEmbeddedItem,
manageAttributes: this.#manageAttributes, manageAttributes: this.#manageAttributes,
configureSheet: this.#configureSheet, configureSheet: this.#configureSheet,
toggleExpand: this.#toggleExpand, toggleExpand: this.#toggleExpand,
@ -132,7 +133,8 @@ export class PlayerSheet extends
_getHeaderControls() { _getHeaderControls() {
const controls = super._getHeaderControls(); const controls = super._getHeaderControls();
controls.push({ controls.push(
{
icon: `fa-solid fa-at`, icon: `fa-solid fa-at`,
label: `taf.Apps.PlayerSheet.manage-attributes`, label: `taf.Apps.PlayerSheet.manage-attributes`,
action: `manageAttributes`, action: `manageAttributes`,
@ -142,7 +144,16 @@ export class PlayerSheet extends
const editable = this.isEditable; const editable = this.isEditable;
return isGM || (allowPlayerEdits && editable); return isGM || (allowPlayerEdits && editable);
}, },
}); },
{
icon: `fa-solid fa-suitcase`,
label: `taf.Apps.PlayerSheet.create-item`,
action: `createEmbeddedItem`,
visible: () => {
return this.isEditable;
},
},
);
return controls; return controls;
}; };
@ -158,7 +169,7 @@ export class PlayerSheet extends
label: _loc(`taf.misc.edit`), label: _loc(`taf.misc.edit`),
condition: (el) => { condition: (el) => {
const itemUuid = el.dataset.itemUuid; const itemUuid = el.dataset.itemUuid;
const itemExists = itemUuid != null && itemUuid !== ""; const itemExists = itemUuid != null && itemUuid !== ``;
return this.isEditable && itemExists; return this.isEditable && itemExists;
}, },
onClick: editItemFromElement, onClick: editItemFromElement,
@ -167,13 +178,13 @@ export class PlayerSheet extends
label: _loc(`taf.misc.delete`), label: _loc(`taf.misc.delete`),
condition: (el) => { condition: (el) => {
const itemUuid = el.dataset.itemUuid; const itemUuid = el.dataset.itemUuid;
const itemExists = itemUuid != null && itemUuid !== ""; const itemExists = itemUuid != null && itemUuid !== ``;
return this.isEditable && itemExists; return this.isEditable && itemExists;
}, },
onClick: deleteItemFromElement, onClick: deleteItemFromElement,
}, },
], ],
{ jQuery: false, fixed: true, }, { jQuery: false, fixed: true },
); );
}; };
@ -364,5 +375,30 @@ export class PlayerSheet extends
el.dataset.expanded = newExpanded; el.dataset.expanded = newExpanded;
}); });
}; };
/**
* Used by the sheet in order to create embedded items without needing to have
* equivalent World Items or Compendiums initially.
*
* @this {PlayerSheet}
*/
static async #createEmbeddedItem(event, target) {
let { itemGroup } = target.dataset ?? {};
if (itemGroup === `Items`) { itemGroup = undefined };
const data = {
name: Item.defaultName({
type: `generic`,
parent: this.actor,
}),
type: `generic`,
system: {
group: itemGroup,
},
};
const item = await Item.create(data, { parent: this.actor });
item?.sheet?.render({ force: true });
};
// #endregion Actions // #endregion Actions
}; };

View file

@ -35,7 +35,7 @@ export class GenericItemData extends foundry.abstract.TypeDataModel {
* rounds the number to the nearest 2 decimal places. * rounds the number to the nearest 2 decimal places.
*/ */
get quantifiedWeight() { get quantifiedWeight() {
const value = this.weight * this.quantity const value = this.weight * this.quantity;
return toPrecision(Math.max(value, 0), 2); return toPrecision(Math.max(value, 0), 2);
}; };
}; };