FEAT: add template to items sheet

This commit is contained in:
Joaquin Pereyra 2025-07-12 19:49:09 -03:00
parent 542742447d
commit 2e02d95afa
24 changed files with 283 additions and 457 deletions

View file

@ -223,19 +223,59 @@ export default function DHApplicationMixin(Base) {
* @type {ApplicationClickAction}
*/
static async #createDoc(event, target) {
const { documentClass, type } = target.dataset;
const parent = this.document;
const { documentClass, type, inVault, disabled } = target.dataset;
const parentIsItem = this.document.documentName === 'Item';
const parent = parentIsItem && documentClass === 'Item' ? null : this.document;
const cls = getDocumentClass(documentClass);
return await cls.createDocuments(
[
{
name: cls.defaultName({ type, parent }),
type
if (type === 'action') {
const { type: actionType } = await foundry.applications.api.DialogV2.input({
window: { title: 'Select Action Type' },
content: await foundry.applications.handlebars.renderTemplate(
'systems/daggerheart/templates/actionTypes/actionType.hbs',
{ types: CONFIG.DH.ACTIONS.actionTypes }
),
ok: {
label: game.i18n.format('DOCUMENT.Create', {
type: game.i18n.localize('DAGGERHEART.GENERAL.Action.single')
}),
}
],
{ parent, renderSheet: !event.shiftKey }
);
});
if (!actionType) return;
const cls = game.system.api.models.actions.actionsTypes[actionType]
const action = new cls({
_id: foundry.utils.randomID(),
type: actionType,
name: game.i18n.localize(CONFIG.DH.ACTIONS.actionTypes[actionType].name),
...cls.getSourceConfig(this.document)
},
{
parent: this.document
}
);
await this.document.update({ 'system.actions': [...this.document.system.actions, action] });
await new DHActionConfig(this.document.system.actions[this.document.system.actions.length - 1]).render({
force: true
});
return action;
} else {
const cls = getDocumentClass(documentClass);
const data = {
name: cls.defaultName({ type, parent }),
type,
}
if (inVault) data["system.inVault"] = true;
if (disabled) data.disabled = true;
const doc = await cls.create(data, { parent, renderSheet: !event.shiftKey });
if (parentIsItem && type === 'feature') {
await this.document.update({
'system.features': [...this.document.system.features, doc]
});
}
return doc;
}
}
/**
@ -244,9 +284,9 @@ export default function DHApplicationMixin(Base) {
*/
static #editDoc(_event, target) {
const doc = getDocFromElement(target);
if (doc) return doc.sheet.render({ force: true });
// TODO: REDO this
if (doc) return doc.sheet.render({ force: true });
const { actionId } = target.closest('[data-action-id]').dataset;
const { actions, attack } = this.document.system;
const action = attack.id === actionId ? attack : actions?.find(a => a.id === actionId);

View file

@ -15,15 +15,13 @@ export default class DHBaseItemSheet extends DHApplicationMixin(ItemSheetV2) {
static DEFAULT_OPTIONS = {
classes: ['item'],
position: { width: 600 },
window: { resizable: true },
form: {
submitOnChange: true
},
actions: {
addAction: DHBaseItemSheet.#addAction,
editAction: DHBaseItemSheet.#editAction,
removeAction: DHBaseItemSheet.#removeAction,
addFeature: DHBaseItemSheet.#addFeature,
editFeature: DHBaseItemSheet.#editFeature,
removeFeature: DHBaseItemSheet.#removeFeature
},
dragDrop: [
@ -48,8 +46,8 @@ export default class DHBaseItemSheet extends DHApplicationMixin(ItemSheetV2) {
/* -------------------------------------------- */
/**@inheritdoc */
async _preparePartContext(partId, context) {
await super._preparePartContext(partId, context);
async _preparePartContext(partId, context, options) {
await super._preparePartContext(partId, context, options);
const { TextEditor } = foundry.applications.ux;
switch (partId) {
@ -61,76 +59,37 @@ export default class DHBaseItemSheet extends DHApplicationMixin(ItemSheetV2) {
secrets: this.item.isOwner
});
break;
case "effects":
await this._prepareEffectsContext(context, options)
break;
}
return context;
}
/**
* Prepare render context for the Effect part.
* @param {ApplicationRenderContext} context
* @param {ApplicationRenderOptions} options
* @returns {Promise<void>}
* @protected
*/
async _prepareEffectsContext(context, _options) {
context.effects = {
actives: [],
inactives: [],
};
for (const effect of this.item.effects) {
const list = effect.active ? context.effects.actives : context.effects.inactives;
list.push(effect);
}
}
/* -------------------------------------------- */
/* Application Clicks Actions */
/* -------------------------------------------- */
/**
* Render a dialog prompting the user to select an action type.
*
* @returns {Promise<object>} An object containing the selected action type.
*/
static async selectActionType() {
const content = await foundry.applications.handlebars.renderTemplate(
'systems/daggerheart/templates/actionTypes/actionType.hbs',
{ types: CONFIG.DH.ACTIONS.actionTypes }
),
title = 'Select Action Type';
return foundry.applications.api.DialogV2.prompt({
window: { title },
content,
ok: {
label: title,
callback: (event, button, dialog) => button.form.elements.type.value
}
});
}
/**
* Add a new action to the item, prompting the user for its type.
* @type {ApplicationClickAction}
*/
static async #addAction(_event, _button) {
const actionType = await DHBaseItemSheet.selectActionType();
if (!actionType) return;
try {
const cls =
game.system.api.models.actions.actionsTypes[actionType] ??
game.system.api.models.actions.actionsTypes.attack,
action = new cls(
{
_id: foundry.utils.randomID(),
type: actionType,
name: game.i18n.localize(CONFIG.DH.ACTIONS.actionTypes[actionType].name),
...cls.getSourceConfig(this.document)
},
{
parent: this.document
}
);
await this.document.update({ 'system.actions': [...this.document.system.actions, action] });
await new DHActionConfig(this.document.system.actions[this.document.system.actions.length - 1]).render({
force: true
});
} catch (error) {
console.log(error);
}
}
/**
* Edit an existing action on the item
* @type {ApplicationClickAction}
*/
static async #editAction(_event, button) {
const action = this.document.system.actions[button.dataset.index];
await new DHActionConfig(action).render({ force: true });
}
/**
* Remove an action from the item.
@ -162,30 +121,16 @@ export default class DHBaseItemSheet extends DHApplicationMixin(ItemSheetV2) {
* @type {ApplicationClickAction}
*/
static async #addFeature(_event, _button) {
const feature = await game.items.documentClass.create({
const cls = foundry.documents.Item.implementation;
const feature = await cls.create({
type: 'feature',
name: game.i18n.format('DOCUMENT.New', { type: game.i18n.localize('TYPES.Item.feature') })
name: cls.defaultName({ type: 'feature' }),
});
await this.document.update({
'system.features': [...this.document.system.features.filter(x => x).map(x => x.uuid), feature.uuid]
'system.features': [...this.document.system.features, feature]
});
}
/**
* Edit an existing feature on the item
* @type {ApplicationClickAction}
*/
static async #editFeature(_event, button) {
const target = button.closest('.feature-item');
const feature = this.document.system.features.find(x => x?.id === target.id);
if (!feature) {
ui.notifications.warn(game.i18n.localize('DAGGERHEART.UI.notifications.featureIsMissing'));
return;
}
feature.sheet.render(true);
}
/**
* Remove a feature from the item.
* @type {ApplicationClickAction}

View file

@ -28,20 +28,4 @@ export default class BeastformSheet extends DHBaseItemSheet {
labelPrefix: 'DAGGERHEART.GENERAL.Tabs'
}
};
/**@inheritdoc */
async _prepareContext(_options) {
const context = await super._prepareContext(_options);
context.document = context.document.toObject();
context.document.effects = this.document.effects.map(effect => {
const data = effect.toObject();
data.id = effect.id;
if (effect.type === 'beastform') data.mandatory = true;
return data;
});
return context;
}
}

View file

@ -10,10 +10,8 @@ export default class ClassSheet extends DHBaseItemSheet {
actions: {
removeItemFromCollection: ClassSheet.#removeItemFromCollection,
removeSuggestedItem: ClassSheet.#removeSuggestedItem,
viewDoc: ClassSheet.#viewDoc,
addFeature: this.addFeature,
editFeature: this.editFeature,
deleteFeature: this.deleteFeature
addFeature: ClassSheet.#addFeature,
deleteFeature: ClassSheet.#deleteFeature
},
tagifyConfigs: [
{
@ -157,55 +155,27 @@ export default class ClassSheet extends DHBaseItemSheet {
await this.document.update({ [`system.characterGuide.${target}`]: null });
}
/**
* Open the sheet of a item by UUID.
* @param {PointerEvent} _event -
* @param {HTMLElement} button
*/
static async #viewDoc(_event, button) {
const doc = await fromUuid(button.dataset.uuid);
doc.sheet.render({ force: true });
}
static async #addFeature(_, target) {
const { actionPath } = target.dataset;
const cls = foundry.documents.Item.implementation;
getActionPath(type) {
return type === 'hope' ? 'hopeFeatures' : 'classFeatures';
}
static async addFeature(_, target) {
const actionPath = this.getActionPath(target.dataset.type);
const feature = await game.items.documentClass.create({
const feature = await cls.create({
type: 'feature',
name: game.i18n.format('DOCUMENT.New', { type: game.i18n.localize('TYPES.Item.feature') })
name: cls.defaultName({ type: 'feature'}),
});
await this.document.update({
[`system.${actionPath}`]: [
...this.document.system[actionPath].filter(x => x).map(x => x.uuid),
...this.document.system[actionPath],
feature.uuid
]
});
}
static async editFeature(_, button) {
const target = button.closest('.feature-item');
const actionPath = this.getActionPath(button.dataset.type);
const feature = this.document.system[actionPath].find(x => x?.id === target.dataset.featureId);
if (!feature) {
ui.notifications.warn(game.i18n.localize('DAGGERHEART.UI.notifications.featureIsMissing'));
return;
}
feature.sheet.render(true);
}
static async deleteFeature(event, button) {
event.stopPropagation();
const target = button.closest('.feature-item');
const actionPath = this.getActionPath(button.dataset.type);
static async #deleteFeature(_, button) {
const { actionPath, itemUuid } = button.dataset;
await this.document.update({
[`system.${actionPath}`]: this.document.system[actionPath]
.filter(feature => feature && feature.id !== target.dataset.featureId)
.map(x => x.uuid)
[`system.${actionPath}`]: this.document.system[actionPath].filter(f => f.uuid !== itemUuid)
});
}
}

View file

@ -1,5 +1,3 @@
import { actionsTypes } from '../../../data/action/_module.mjs';
import DHActionConfig from '../../sheets-configs/action-config.mjs';
import DHBaseItemSheet from '../api/base-item.mjs';
export default class FeatureSheet extends DHBaseItemSheet {
@ -7,13 +5,7 @@ export default class FeatureSheet extends DHBaseItemSheet {
static DEFAULT_OPTIONS = {
id: 'daggerheart-feature',
classes: ['feature'],
position: { height: 600 },
window: { resizable: true },
actions: {
addAction: FeatureSheet.#addAction,
editAction: FeatureSheet.#editAction,
removeAction: FeatureSheet.#removeAction
}
actions: {}
};
/**@override */
@ -39,92 +31,4 @@ export default class FeatureSheet extends DHBaseItemSheet {
labelPrefix: 'DAGGERHEART.GENERAL.Tabs'
}
};
/* -------------------------------------------- */
/**@inheritdoc */
async _prepareContext(_options) {
const context = await super._prepareContext(_options);
return context;
}
/* -------------------------------------------- */
/* Application Clicks Actions */
/* -------------------------------------------- */
/**
* Render a dialog prompting the user to select an action type.
*
* @returns {Promise<object>} An object containing the selected action type.
*/
static async selectActionType() {
const content = await foundry.applications.handlebars.renderTemplate(
'systems/daggerheart/templates/actionTypes/actionType.hbs',
{ types: CONFIG.DH.ACTIONS.actionTypes }
),
title = 'Select Action Type';
return foundry.applications.api.DialogV2.prompt({
window: { title },
content,
ok: {
label: title,
callback: (event, button, dialog) => button.form.elements.type.value
}
});
}
/**
* Add a new action to the item, prompting the user for its type.
* @param {PointerEvent} _event - The originating click event
* @param {HTMLElement} _button - The capturing HTML element which defines the [data-action="addAction"]
*/
static async #addAction(_event, _button) {
const actionType = await FeatureSheet.selectActionType();
if (!actionType) return;
try {
const cls = actionsTypes[actionType] ?? actionsTypes.attack,
action = new cls(
{
_id: foundry.utils.randomID(),
type: actionType,
name: game.i18n.localize(CONFIG.DH.ACTIONS.actionTypes[actionType].name),
...cls.getSourceConfig(this.document)
},
{
parent: this.document
}
);
await this.document.update({ 'system.actions': [...this.document.system.actions, action] });
await new DHActionConfig(this.document.system.actions[this.document.system.actions.length - 1]).render({
force: true
});
} catch (error) {
console.log(error);
}
}
/**
* Edit an existing action on the item
* @param {PointerEvent} _event - The originating click event
* @param {HTMLElement} button - The capturing HTML element which defines the [data-action="editAction"]
*/
static async #editAction(_event, button) {
const action = this.document.system.actions[button.dataset.index];
await new DHActionConfig(action).render({ force: true });
}
/**
* Remove an action from the item.
* @param {PointerEvent} event - The originating click event
* @param {HTMLElement} button - The capturing HTML element which defines the [data-action="removeAction"]
*/
static async #removeAction(event, button) {
event.stopPropagation();
const actionIndex = button.closest('[data-index]').dataset.index;
await this.document.update({
'system.actions': this.document.system.actions.filter((_, index) => index !== Number.parseInt(actionIndex))
});
}
}

View file

@ -8,7 +8,6 @@ export default class SubclassSheet extends DHBaseItemSheet {
window: { resizable: false },
actions: {
addFeature: this.addFeature,
editFeature: this.editFeature,
deleteFeature: this.deleteFeature
}
};
@ -38,30 +37,20 @@ export default class SubclassSheet extends DHBaseItemSheet {
};
static async addFeature(_, target) {
const feature = await game.items.documentClass.create({
const cls = foundry.documents.Item.implementation;
const feature = await cls.create({
type: 'feature',
name: game.i18n.format('DOCUMENT.New', { type: game.i18n.localize('TYPES.Item.feature') })
name: cls.defaultName({ type: 'feature' }),
});
await this.document.update({
[`system.${target.dataset.type}`]: feature.uuid
[`system.${target.dataset.type}`]: feature
});
}
static async editFeature(_, button) {
const feature = this.document.system[button.dataset.type];
if (!feature) {
ui.notifications.warn(game.i18n.localize('DAGGERHEART.UI.notifications.featureIsMissing'));
return;
}
feature.sheet.render(true);
}
static async deleteFeature(event, button) {
event.stopPropagation();
static async deleteFeature(_, button) {
await this.document.update({
[`system.${button.dataset.type}`]: null
[`system.${button.dataset.actionPath}`]: null
});
}