add item support and dynamic translation handling to path browser choices

This commit is contained in:
CPTN Cosmo 2026-04-13 22:19:50 +02:00
parent a9d0167ebe
commit 8c4be1594e

View file

@ -97,37 +97,17 @@ export class DhPathBrowserApp extends HandlebarsApplicationMixin(ApplicationV2)
static getChangeChoices() { static getChangeChoices() {
const ignoredActorKeys = ['config', 'DhEnvironment', 'DhParty']; const ignoredActorKeys = ['config', 'DhEnvironment', 'DhParty'];
const getAllLeaves = (root, group, parentPath = '') => { const getTranslations = (model, path) => {
const leaves = [];
const rootKey = `${parentPath ? `${parentPath}.` : ''}${root.name}`;
for (const field of Object.values(root.fields)) {
if (field instanceof foundry.data.fields.SchemaField)
leaves.push(...getAllLeaves(field, group, rootKey));
else
leaves.push({
value: `${rootKey}.${field.name}`,
label: game.i18n.localize(field.label),
hint: game.i18n.localize(field.hint),
group
});
}
return leaves;
};
return Object.keys(game.system.api.models.actors).reduce((acc, key) => {
if (ignoredActorKeys.includes(key)) return acc;
const model = game.system.api.models.actors[key];
const group = game.i18n.localize(model.metadata.label);
const attributes = CONFIG.Token.documentClass.getTrackedAttributes(model.metadata.type);
const getTranslations = path => {
if (path === 'resources.hope.max') if (path === 'resources.hope.max')
return { return {
label: game.i18n.localize('DAGGERHEART.SETTINGS.Homebrew.FIELDS.maxHope.label'), label: game.i18n.localize('DAGGERHEART.SETTINGS.Homebrew.FIELDS.maxHope.label'),
hint: '' hint: ''
}; };
// Armor overrides
if (path.endsWith('armor.max')) return { label: game.i18n.localize('DAGGERHEART.ArmorScoreMax'), hint: '' };
if (path.endsWith('armor.current')) return { label: game.i18n.localize('DAGGERHEART.ArmorScoreCurrent'), hint: '' };
const field = model.schema.getField(path); const field = model.schema.getField(path);
return { return {
label: field ? game.i18n.localize(field.label) : path, label: field ? game.i18n.localize(field.label) : path,
@ -135,23 +115,68 @@ export class DhPathBrowserApp extends HandlebarsApplicationMixin(ApplicationV2)
}; };
}; };
const getAllLeaves = (model, root, group, parentPath = '') => {
if (!root) return [];
const leaves = [];
const rootKey = `${parentPath ? `${parentPath}.` : ''}${root.name}`;
const fields = root.fields || root;
for (const [name, field] of Object.entries(fields)) {
const currentPath = `${rootKey}.${name}`;
if (field instanceof foundry.data.fields.SchemaField) {
leaves.push(...getAllLeaves(model, field, group, rootKey));
} else {
const trans = getTranslations(model, currentPath);
leaves.push({
value: currentPath,
label: trans.label || name,
hint: trans.hint || '',
group
});
}
}
return leaves;
};
const choices = [];
// Process Actors
for (const [key, model] of Object.entries(game.system.api.models.actors)) {
if (ignoredActorKeys.includes(key)) continue;
const group = game.i18n.localize(model.metadata.label);
const attributes = CONFIG.Token.documentClass.getTrackedAttributes(model.metadata.type);
const bars = attributes.bar.flatMap(x => { const bars = attributes.bar.flatMap(x => {
const baseJoined = x.join('.'); const baseJoined = x.join('.');
return [ return [
{ value: `${baseJoined}.max`, ...getTranslations(`${baseJoined}.max`), group }, { value: `${baseJoined}.max`, ...getTranslations(model, `${baseJoined}.max`), group },
{ value: `${baseJoined}.value`, ...getTranslations(`${baseJoined}.value`), group } { value: `${baseJoined}.value`, ...getTranslations(model, `${baseJoined}.value`), group }
]; ];
}); });
const values = attributes.value.flatMap(x => { const values = attributes.value.flatMap(x => {
const joined = x.join('.'); const joined = x.join('.');
return { value: joined, ...getTranslations(joined), group }; return { value: joined, ...getTranslations(model, joined), group };
}); });
const bonuses = getAllLeaves(model.schema.fields.bonuses, group); const bonuses = getAllLeaves(model, model.schema.fields.bonuses, group);
const rules = getAllLeaves(model.schema.fields.rules, group); const rules = getAllLeaves(model, model.schema.fields.rules, group);
const armor = getAllLeaves(model, model.schema.fields.armor, group);
acc.push(...bars, ...values, ...rules, ...bonuses); choices.push(...bars, ...values, ...rules, ...bonuses, ...armor);
return acc; }
}, []);
// Process Items
for (const [key, model] of Object.entries(game.system.api.models.items)) {
const group = `${game.i18n.localize('DOCUMENT.Item')} (${game.i18n.localize(model.metadata.label)})`;
const bonuses = getAllLeaves(model, model.schema.fields.bonuses, group);
const armor = getAllLeaves(model, model.schema.fields.armor, group);
choices.push(...bonuses, ...armor);
}
return choices;
} }
} }