Set command codeblock to expandable

This commit is contained in:
WBHarry 2026-01-03 02:50:47 +01:00
parent 5c3a2cf060
commit 958dd8b1d0
7 changed files with 58 additions and 17 deletions

View file

@ -1226,7 +1226,10 @@
},
"fearRoll": {
"label": "Fear Roll",
"hint": "this: Action\nroll: DhRoll\nactor: DhActor"
"hint": "this: Action, roll: DhRoll, actor: DhActor"
},
"triggerTexts": {
"strangePatternsContent": "Increase Hope or clear stress?"
},
"triggerError": "{trigger} trigger failed for {actor}. It's probably configured wrong."
},

View file

@ -7,6 +7,7 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
this.action = action;
this.openSection = null;
this.openTrigger = this.action.triggers.length > 0 ? 0 : null;
}
get title() {
@ -31,7 +32,8 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
addDamage: this.addDamage,
removeDamage: this.removeDamage,
addTrigger: this.addTrigger,
removeTrigger: this.removeTrigger
removeTrigger: this.removeTrigger,
expandTrigger: this.expandTrigger
},
form: {
handler: this.updateForm,
@ -126,9 +128,10 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
context.baseAttackBonus = this.action.actor?.system.attack?.roll.bonus;
context.hasRoll = this.action.hasRoll;
context.triggerOptions = CONFIG.DH.TRIGGER.triggers;
context.triggers = context.source.triggers.map(trigger => ({
context.triggers = context.source.triggers.map((trigger, index) => ({
...trigger,
hint: CONFIG.DH.TRIGGER.triggers[trigger.trigger].hint
hint: CONFIG.DH.TRIGGER.triggers[trigger.trigger].hint,
revealed: this.openTrigger === index
}));
const settingsTiers = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.LevelTiers).tiers;
@ -245,7 +248,7 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
static addTrigger() {
const data = this.action.toObject();
data.triggers.push({ hook: CONFIG.DH.TRIGGER.triggers.dualityRoll.id });
data.triggers.push({ trigger: CONFIG.DH.TRIGGER.triggers.dualityRoll.id });
this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(data) });
}
@ -267,6 +270,33 @@ export default class DHActionBaseConfig extends DaggerheartSheet(ApplicationV2)
this.constructor.updateForm.bind(this)(null, null, { object: foundry.utils.flattenObject(data) });
}
static async expandTrigger(_event, button) {
const index = Number.parseInt(button.dataset.index);
const toggle = (element, codeMirror) => {
codeMirror.classList.toggle('revealed');
const button = element.querySelector('a > i');
button.classList.toggle('fa-angle-up');
button.classList.toggle('fa-angle-down');
};
const fieldset = button.closest('fieldset');
const codeMirror = fieldset.querySelector('.code-mirror-wrapper');
toggle(fieldset, codeMirror);
if (this.openTrigger !== null && this.openTrigger !== index) {
const previouslyExpanded = fieldset
.closest(`section`)
.querySelector(`fieldset[data-index="${this.openTrigger}"]`);
const codeMirror = previouslyExpanded.querySelector('.code-mirror-wrapper');
toggle(previouslyExpanded, codeMirror);
this.openTrigger = index;
} else if (this.openTrigger === index) {
this.openTrigger = null;
} else {
this.openTrigger = index;
}
}
/** Specific implementation in extending classes **/
static async addEffect(_event) {}
static removeEffect(_event, _button) {}

View file

@ -4,6 +4,7 @@ export default class TriggerField extends foundry.data.fields.SchemaField {
{
trigger: new foundry.data.fields.StringField({
nullable: false,
blank: false,
initial: CONFIG.DH.TRIGGER.triggers.dualityRoll.id,
choices: CONFIG.DH.TRIGGER.triggers
}),

View file

@ -84,7 +84,7 @@
"triggers": [
{
"trigger": "dualityRoll",
"command": "/* Check if there's a Strange Pattern match */\nconst dice = [roll.dFear.total, roll.dHope.total];\nconst resource = this.parent.resource?.diceStates ? Object.values(this.parent.resource.diceStates).map(x => x.value)[0] : null;\nconst diceMatch = dice.includes(resource);\n\nif (!diceMatch) return;\n\n/* Create a dialog to choose Hope or Stress - or to cancel*/\nconst choice = await foundry.applications.api.DialogV2.wait({\n classes: ['dh-style', 'two-big-buttons'],\n window: { title: 'Strange Patterns' },\n content: 'Increase Hope or clear stress?',\n buttons: [\n {\n action: 'hope',\n label: 'Hope',\n icon: 'fa-solid fa-plus'\n },\n {\n action: 'stress',\n label: 'Stress',\n icon: 'fa-solid fa-book'\n }\n ]\n});\n \nif (!choice) return;\n\n/* Return resource update according to choice */\nconst isHope = choice === 'hope';\nreturn [{ key: choice, value: isHope ? 1 : -1, total: isHope ? -1 : 1, enabled: true }];"
"command": "/* Check if there's a Strange Pattern match */\nconst dice = [roll.dFear.total, roll.dHope.total];\nconst resource = this.parent.resource?.diceStates ? Object.values(this.parent.resource.diceStates).map(x => x.value)[0] : null;\nconst diceMatch = dice.includes(resource);\n\nif (!diceMatch) return;\n\n/* Create a dialog to choose Hope or Stress - or to cancel*/\nconst choice = await foundry.applications.api.DialogV2.wait({\n classes: ['dh-style', 'two-big-buttons'],\n window: { title: this.item.name },\n content: game.i18n.localize('DAGGERHEART.CONFIG.Triggers.triggerTexts.strangePatternsContent'),\n buttons: [\n {\n action: 'hope',\n label: game.i18n.localize('DAGGERHEART.GENERAL.hope'),\n icon: 'fa-solid fa-hands-holding'\n },\n {\n action: 'stress',\n label: game.i18n.localize('DAGGERHEART.GENERAL.stress'),\n icon: 'fa-solid fa-bolt-lightning'\n }\n ]\n});\n \nif (!choice) return;\n\n/* Return resource update according to choice */\nconst isHope = choice === 'hope';\nreturn [{ key: choice, value: isHope ? 1 : -1, total: isHope ? -1 : 1, enabled: true }];"
}
]
}

View file

@ -545,11 +545,6 @@
font-size: var(--font-size-12);
padding-left: 3px;
}
code-mirror {
width: 100%;
height: 500px;
}
}
.application.setting.dh-style {

View file

@ -21,4 +21,15 @@
}
}
}
.code-mirror-wrapper {
width: 100%;
height: 0;
min-height: 0;
transition: height 0.1s ease-in-out;
&.revealed {
height: 300px;
}
}
}

View file

@ -3,23 +3,24 @@
data-group="primary"
data-tab="trigger"
>
<button data-action="addTrigger">{{localize "Add Trigger"}} <i class="fa-solid fa-plus icon-button"></i></button>
<button type="button" data-action="addTrigger">{{localize "Add Trigger"}} <i class="fa-solid fa-plus icon-button"></i></button>
{{#each @root.triggers as |trigger index|}}
<fieldset class="one-column">
<fieldset class="one-column" data-index="{{index}}">
<legend><a data-action="removeTrigger" data-index="{{index}}"><i class="fa-solid fa-trash"></i></a></legend>
<div class="trigger-data">
<select name="{{concat "triggers." index ".trigger"}}">
{{selectOptions @root.triggerOptions seleced=trigger.trigger localize=true}}
</select>
{{formInput @root.fields.triggers.element.fields.trigger value=trigger.trigger name=(concat "triggers." index ".trigger") blank=false localize=true}}
<div class="hint-section">
<strong>{{localize "Context: "}}</strong>
<span class="hint">{{localize trigger.hint}}</span>
</div>
<a data-action="expandTrigger" data-index="{{index}}"><i class="fa-solid fa-angle-down"></i></a>
</div>
<div class="code-mirror-wrapper {{#if trigger.revealed}}revealed{{/if}}">
{{formInput @root.fields.triggers.element.fields.command value=trigger.command elementType="code-mirror" name=(concat "triggers." index ".command") aria=(object label=(localize "Test")) }}
</div>
</fieldset>
{{/each}}
</section>