From 902bfac490e8afbb1551d10cd0b84912010867de Mon Sep 17 00:00:00 2001 From: WBHarry Date: Wed, 6 Aug 2025 12:01:22 +0200 Subject: [PATCH] Improved domainremoval cleanup --- lang/en.json | 6 +- .../settings/homebrewSettings.mjs | 90 +++++++++++++------ module/data/item/domainCard.mjs | 2 +- module/helpers/utils.mjs | 4 + .../settings/homebrew-settings/domains.less | 13 ++- .../settings/homebrew-settings/domains.hbs | 6 +- 6 files changed, 86 insertions(+), 35 deletions(-) diff --git a/lang/en.json b/lang/en.json index 0a9b091b..b080fcc8 100755 --- a/lang/en.json +++ b/lang/en.json @@ -2146,9 +2146,13 @@ "domainsTitle": "Base Domains", "homebrewDomains": "Homebrew Domains", "newDomain": "New Domain", + "newDomainInputTitle": "Create Homebrew Domain", + "newDomainInputLabel": "New Domain Name", + "newDomainInputHint": "This cannot be altered later", "editDomain": "Active Domain", "deleteDomain": "Delete Domain", - "deleteDomainText": "Are you sure you want to delete the {name} domain? It will be removed from all Actors in this world where it's currently used." + "deleteDomainText": "Are you sure you want to delete the {name} domain? It will be immediately removed from all Actors in this world where it's currently used. Compendiums are not cleared.", + "duplicateDomain": "There is already a domain with this identification." } }, "Menu": { diff --git a/module/applications/settings/homebrewSettings.mjs b/module/applications/settings/homebrewSettings.mjs index f0c645bd..3fa42afd 100644 --- a/module/applications/settings/homebrewSettings.mjs +++ b/module/applications/settings/homebrewSettings.mjs @@ -1,4 +1,5 @@ import { DhHomebrew } from '../../data/settings/_module.mjs'; +import { slugify } from '../../helpers/utils.mjs'; const { HandlebarsApplicationMixin, ApplicationV2 } = foundry.applications.api; export default class DhHomebrewSettings extends HandlebarsApplicationMixin(ApplicationV2) { @@ -199,18 +200,50 @@ export default class DhHomebrewSettings extends HandlebarsApplicationMixin(Appli this.render(); } - static async addDomain() { - const id = foundry.utils.randomID(); - this.settings.updateSource({ - [`domains.${id}`]: { - id: id, - label: game.i18n.localize('DAGGERHEART.SETTINGS.Homebrew.domains.newDomain'), - src: 'icons/svg/portal.svg' - } - }); + static async addDomain(event) { + event.preventDefault(); + const content = new foundry.data.fields.StringField({ + label: game.i18n.localize('DAGGERHEART.SETTINGS.Homebrew.domains.newDomainInputLabel'), + hint: game.i18n.localize('DAGGERHEART.SETTINGS.Homebrew.domains.newDomainInputHint'), + required: true + }).toFormGroup({}, { name: 'domainName', localize: true }).outerHTML; - this.selected.domain = id; - this.render(); + async function callback(_, button) { + const domainName = button.form.elements.domainName.value; + if (!domainName) return; + + const newSlug = slugify(domainName); + const existingDomains = [ + ...Object.values(this.settings.domains), + ...Object.values(CONFIG.DH.DOMAIN.domains) + ]; + if (existingDomains.find(x => slugify(game.i18n.localize(x.label)) === newSlug)) { + ui.notifications.warn(game.i18n.localize('DAGGERHEART.SETTINGS.Homebrew.domains.duplicateDomain')); + return; + } + + this.settings.updateSource({ + [`domains.${newSlug}`]: { + id: newSlug, + label: domainName, + src: 'icons/svg/portal.svg' + } + }); + + this.selected.domain = newSlug; + this.render(); + } + + foundry.applications.api.DialogV2.prompt({ + content: content, + rejectClose: false, + modal: true, + ok: { callback: callback.bind(this) }, + window: { + title: game.i18n.localize('DAGGERHEART.SETTINGS.Homebrew.domains.newDomainInputTitle') + }, + position: { width: 400 } + }); } static toggleSelectedDomain(_, target) { @@ -230,20 +263,30 @@ export default class DhHomebrewSettings extends HandlebarsApplicationMixin(Appli if (!confirmed) return; - const updateClasses = game.items.filter( - x => x.type === 'class' && x.system.domains.includes(this.selected.domain) - ); + await this.settings.updateSource({ + [`domains.-=${this.selected.domain}`]: null + }); + + const currentSettings = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.Homebrew); + if (currentSettings.domains[this.selected.domain]) { + await currentSettings.updateSource({ [`domains.-=${this.selected.domain}`]: null }); + await game.settings.set(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.Homebrew, currentSettings); + } + + const updateClasses = game.items.filter(x => x.type === 'class'); for (let actor of game.actors) { - updateClasses.push( - ...actor.items.filter(x => x.type === 'class' && x.system.domains.includes(this.selected.domain)) - ); + updateClasses.push(...actor.items.filter(x => x.type === 'class')); } for (let c of updateClasses) { - const newDomains = c.system.domains.map(x => - x === this.selected.domain ? CONFIG.DH.DOMAIN.domains.arcana.id : x - ); - await c.update({ 'system.domains': newDomains }); + if (c.system.domains.includes(this.selected.domain)) { + const newDomains = + c.system.domains.length === 1 + ? [CONFIG.DH.DOMAIN.domains.arcana.id] + : c.system.domains.filter(x => x !== this.selected.domain); + await c.update({ 'system.domains': newDomains }); + } + c.sheet.render(); } const updateDomainCards = game.items.filter( @@ -251,12 +294,9 @@ export default class DhHomebrewSettings extends HandlebarsApplicationMixin(Appli ); for (let d of updateDomainCards) { await d.update({ 'system.domain': CONFIG.DH.DOMAIN.domains.arcana.id }); + d.sheet.render(); } - await this.settings.updateSource({ - [`domains.-=${this.selected.domain}`]: null - }); - this.selected.domain = null; this.render(); } diff --git a/module/data/item/domainCard.mjs b/module/data/item/domainCard.mjs index 463b73a7..34754a41 100644 --- a/module/data/item/domainCard.mjs +++ b/module/data/item/domainCard.mjs @@ -18,7 +18,7 @@ export default class DHDomainCard extends BaseDataItem { return { ...super.defineSchema(), domain: new fields.StringField({ - choices: CONFIG.DH.DOMAIN.allDomains(), + choices: CONFIG.DH.DOMAIN.allDomains, required: true, initial: CONFIG.DH.DOMAIN.domains.arcana.id }), diff --git a/module/helpers/utils.mjs b/module/helpers/utils.mjs index f6cbb387..f1df4723 100644 --- a/module/helpers/utils.mjs +++ b/module/helpers/utils.mjs @@ -370,3 +370,7 @@ export async function createEmbeddedItemWithEffects(actor, baseData, update) { return doc; } + +export const slugify = name => { + return name.toLowerCase().replaceAll(' ', '-').replaceAll('.', ''); +}; diff --git a/styles/less/ui/settings/homebrew-settings/domains.less b/styles/less/ui/settings/homebrew-settings/domains.less index 15eabd7a..e0e0dcd7 100644 --- a/styles/less/ui/settings/homebrew-settings/domains.less +++ b/styles/less/ui/settings/homebrew-settings/domains.less @@ -127,13 +127,20 @@ } .domain-filepicker-row { - display: flex; - justify-content: space-between; - gap: 8px; + width: 100%; .form-group { flex: 1; gap: 8px; + + .form-fields { + flex: 1; + display: flex; + + file-picker { + flex: 1; + } + } } } diff --git a/templates/settings/homebrew-settings/domains.hbs b/templates/settings/homebrew-settings/domains.hbs index b88e44fe..0946f211 100644 --- a/templates/settings/homebrew-settings/domains.hbs +++ b/templates/settings/homebrew-settings/domains.hbs @@ -38,11 +38,7 @@
{{localize "DAGGERHEART.SETTINGS.Homebrew.domains.editDomain"}} -
- {{formGroup settingFields.schema.fields.domains.element.fields.label value=selectedDomain.label name=(concat "domains." selectedDomain.id ".label") localize=true}} - {{formGroup settingFields.schema.fields.domains.element.fields.src value=selectedDomain.src name=(concat "domains." selectedDomain.id ".src") localize=true}} -
- +
{{formGroup settingFields.schema.fields.domains.element.fields.src value=selectedDomain.src name=(concat "domains." selectedDomain.id ".src") localize=true}}