Skip to content

Commit

Permalink
fix(goals list component): fix error causing no new goal to get added…
Browse files Browse the repository at this point in the history
… when "add new goal" is clicked (#160)
  • Loading branch information
stanavdb authored Nov 20, 2024
1 parent fdc175d commit c4cd6a0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
60 changes: 54 additions & 6 deletions src/system/applications/actor/components/character/goals-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,6 @@ export class CharacterGoalsListComponent extends HandlebarsApplicationComponent<
// Ensure controls dropdown is closed
this.controlsDropdownExpanded = false;

// Get the goals
const goals = this.application.actor.system.goals;
if (!goals) return;

// Create goal
const goal = (await Item.create(
{
Expand All @@ -190,8 +186,10 @@ export class CharacterGoalsListComponent extends HandlebarsApplicationComponent<
{ parent: this.application.actor },
)) as GoalItem;

// Show item sheet
void goal.sheet?.render(true);
setTimeout(() => {
// Edit the goal
this.editGoal(goal.id);
});
}

/* --- Context --- */
Expand Down Expand Up @@ -226,6 +224,56 @@ export class CharacterGoalsListComponent extends HandlebarsApplicationComponent<
},
});
}

/* --- Helpers --- */

private editGoal(id: string) {
// Get goal element
const element = $(this.element!).find(`.goal[data-id="${id}"]`);

// Get span element
const span = element.find('span.title');

// Hide span title
span.addClass('inactive');

// Get input element
const input = element.find('input.title');

// Show
input.removeClass('inactive');

setTimeout(() => {
// Focus input
input.trigger('select');

// Add event handler
input.on('focusout', async () => {
// Remove handler
input.off('focusout');

// Get the goal
const goal = this.application.actor.items.get(id) as GoalItem;

// Update the connection
await goal.update({
name: input.val(),
});

// Render
void this.render();
});

input.on('keypress', (event) => {
if (event.which !== 13) return; // Enter key

event.preventDefault();
event.stopPropagation();

input.trigger('focusout');
});
});
}
}

// Register
Expand Down
1 change: 1 addition & 0 deletions src/templates/actors/character/components/goals-list.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<i class="fa-solid fa-diamond"></i>

<span class="title">{{goal.name}}</span>
<input type="text" class="title inactive" value="{{goal.name}}">

<a data-action="adjust-goal-progress"
{{#if @root.editable}}
Expand Down

0 comments on commit c4cd6a0

Please sign in to comment.