Skip to content

Commit

Permalink
Extracted common active effect summarization code to share.
Browse files Browse the repository at this point in the history
  • Loading branch information
kgar committed Dec 18, 2024
1 parent 3167037 commit 05db40f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 76 deletions.
43 changes: 5 additions & 38 deletions src/components/info-card/Cards/EffectInfoCard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { CONSTANTS } from 'src/constants';
import { FoundryAdapter } from 'src/foundry/foundry-adapter';
import type { ActiveEffect5e } from 'src/types/types';
import { ActiveEffectsHelper } from 'src/utils/active-effect';
interface Props {
activeEffect: ActiveEffect5e;
Expand All @@ -18,43 +19,9 @@
}),
);
let pills = $derived.by(() => {
let result = [];
if (activeEffect.disabled) {
result.push('EFFECT.Disabled');
}
if (activeEffect.transfer) {
result.push('EFFECT.Transfer');
}
if (activeEffect.isSuppressed) {
result.push('DND5E.Suppressed');
}
Array.from<string>(activeEffect.statuses)
.map(
(x: string) => CONFIG.statusEffects.find((y) => y.id === x)?.name ?? x,
)
.forEach((e) => {
result.push(e);
});
return result;
});
function findMode(mode: number) {
const entry = Object.entries(CONST.ACTIVE_EFFECT_MODES).find(
([_, value]) => value === mode,
);
if (!entry) {
return '';
}
return localize(`EFFECT.MODE_${entry[0]}`);
}
let pills = $derived.by(() =>
ActiveEffectsHelper.getActiveEffectPills(activeEffect),
);
const localize = FoundryAdapter.localize;
</script>
Expand Down Expand Up @@ -88,7 +55,7 @@

<ul style="margin-block-start: 1rem;" class="unlist flex-column small-gap">
{#each activeEffect.changes as change}
{@const modeLabel = findMode(change.mode)}
{@const modeLabel = ActiveEffectsHelper.findMode(change.mode)}
<li>
<div>
<strong class="break-word">{change.key}</strong>
Expand Down
43 changes: 5 additions & 38 deletions src/components/item-list/EffectSummary.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { FoundryAdapter } from 'src/foundry/foundry-adapter';
import type { ActiveEffect5e } from 'src/types/types';
import HorizontalLineSeparator from '../layout/HorizontalLineSeparator.svelte';
import { ActiveEffectsHelper } from 'src/utils/active-effect';
interface Props {
activeEffect: ActiveEffect5e;
Expand All @@ -14,43 +15,9 @@
FoundryAdapter.enrichHtml(activeEffect.description ?? ''),
);
let pills = $derived.by(() => {
let result = [];
if (activeEffect.disabled) {
result.push('EFFECT.Disabled');
}
if (activeEffect.transfer) {
result.push('EFFECT.Transfer');
}
if (activeEffect.isSuppressed) {
result.push('DND5E.Suppressed');
}
Array.from<string>(activeEffect.statuses)
.map(
(x: string) => CONFIG.statusEffects.find((y) => y.id === x)?.name ?? x,
)
.forEach((e) => {
result.push(e);
});
return result;
});
function findMode(mode: number) {
const entry = Object.entries(CONST.ACTIVE_EFFECT_MODES).find(
([_, value]) => value === mode,
);
if (!entry) {
return '';
}
return localize(`EFFECT.MODE_${entry[0]}`);
}
let pills = $derived.by(() =>
ActiveEffectsHelper.getActiveEffectPills(activeEffect),
);
const localize = FoundryAdapter.localize;
</script>
Expand Down Expand Up @@ -84,7 +51,7 @@
</thead>
<tbody>
{#each activeEffect.changes as change}
{@const modeLabel = findMode(change.mode)}
{@const modeLabel = ActiveEffectsHelper.findMode(change.mode)}

<tr>
<td
Expand Down
40 changes: 40 additions & 0 deletions src/utils/active-effect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ActiveEffect5e } from 'src/types/types';
import { isNil } from './data';
import { debug, error } from './logging';
import { FoundryAdapter } from 'src/foundry/foundry-adapter';

export class ActiveEffectsHelper {
static isActiveEffectAppliedToField(document: any, field: string) {
Expand All @@ -19,4 +21,42 @@ export class ActiveEffectsHelper {
return false;
}
}

static getActiveEffectPills(activeEffect: ActiveEffect5e) {
let result = [];

if (activeEffect.disabled) {
result.push('EFFECT.Disabled');
}

if (activeEffect.transfer) {
result.push('EFFECT.Transfer');
}

if (activeEffect.isSuppressed) {
result.push('DND5E.Suppressed');
}

Array.from<string>(activeEffect.statuses)
.map(
(x: string) => CONFIG.statusEffects.find((y) => y.id === x)?.name ?? x
)
.forEach((e) => {
result.push(e);
});

return result;
}

static findMode(mode: number, fallback = '—') {
const entry = Object.entries(CONST.ACTIVE_EFFECT_MODES).find(
([_, value]) => value === mode
);

if (!entry) {
return fallback;
}

return FoundryAdapter.localize(`EFFECT.MODE_${entry[0]}`);
}
}

0 comments on commit 05db40f

Please sign in to comment.