-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Item.svelte
205 lines (171 loc) · 6.34 KB
/
Item.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<script>
import { createEventDispatcher, getContext } from "svelte";
import { slide } from "svelte/transition";
import { localize } from "#runtime/svelte/helper";
import pressedKeysStore from "../../stores/pressedKeysStore";
import getKeyPressAsOptions from "../handlers/getKeyPressAsOptions";
import getSummaryData from "../../utils/summaries/getSummaryData";
import ItemActionButtons from "./ItemActionButtons.svelte";
import ItemInnerWrapper from "./ItemInnerWrapper.svelte";
import ItemSummary from "./itemSummaries/ItemSummary.svelte";
export let item;
export let action = null;
export let actionId = null;
export let showDescription = false;
const actor = getContext("actor");
const { A5E } = CONFIG;
const dispatch = createEventDispatcher();
let rightClickConfigure =
game.settings.get("a5e", "itemRightClickConfigure") ?? false;
let isGM = game.user.isGM;
function determineActionListVisibility(action, item, sheetIsLocked) {
if (action) return false;
if (item.actions?.count < 2) {
return item.actions
?.values()
?.some((action) => action.uses?.value || action.uses?.max);
}
if (item.getFlag("a5e", "showActionList") === false) return false;
if (game.settings.get("a5e", "collapseActionList") && sheetIsLocked) return false;
return true;
}
function onItemActivate() {
const options = getKeyPressAsOptions($pressedKeysStore);
item.activate(actionId, options);
}
async function onConfigure() {
if (actionId) {
await item.actions?.configure(actionId);
return;
}
item.configureItem();
}
function onDragStart(event) {
const dragData = item.toDragData();
if (!dragData) return;
dragData.actorId = item?.parent.id;
dragData.actionId = actionId;
return event.dataTransfer.setData("text/plain", JSON.stringify(dragData));
}
async function getDescription() {
const data =
(await TextEditor.enrichHTML(
actionId ? action.description : item.system.description,
{
async: true,
secrets: item.isOwner,
relativeTo: item,
rollData: $actor?.getRollData(this) ?? {},
},
)) ?? localize("A5E.NoDescription");
return data;
}
$: containerItems = (item?.contents ?? []).reduce((acc, i) => {
if (!i) return acc;
if (i.parent?.id !== $actor.id) return acc;
acc.push([i?.id ?? foundry.utils.randomId(), i]);
return acc;
}, []);
$: description = getDescription(item)
.then((data) => (description = data))
.catch((err) => (description = err));
$: sheetIsLocked = !$actor.isOwner ? true : $actor.flags?.a5e?.sheetIsLocked ?? true;
$: showActionList = determineActionListVisibility(action, item, sheetIsLocked);
$: showContainerItems =
item.getFlag("a5e", "showContainer") ?? containerItems.length > 0;
$: summaryData = getSummaryData(item, action);
</script>
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
<li
class="a5e-item"
class:a5e-item--highlight={Number(item.system.prepared ?? 0) ===
A5E.PREPARED_STATES.PREPARED ||
(item.system.equippedState === A5E.EQUIPPED_STATES.EQUIPPED &&
!item.system?.containerId)}
class:a5e-item--purple-highlight={Number(item.system.prepared ?? 0) ===
A5E.PREPARED_STATES.ALWAYS_PREPARED}
class:a5e-item--red-highlight={item.system.requiresBloodied}
draggable="true"
data-document-uuid={item.uuid}
on:dragstart={onDragStart}
on:drop|preventDefault|stopPropagation={(e) => dispatch("dropObject", e)}
on:click={() => {
showDescription = !showDescription;
}}
on:auxclick={() => {
if (rightClickConfigure) onConfigure();
}}
>
<button
class="a5e-item__roll-button"
class:a5e-item__roll-button--shift={$pressedKeysStore.Shift}
class:a5e-item__roll-button--ctrl={$pressedKeysStore.Control}
class:disable-pointer-events={!$actor.isOwner}
on:click|stopPropagation={({ target }) => {
target.blur();
onItemActivate();
}}
>
<img
class="a5e-item__image"
src={action?.img ?? item.img ?? "icons/svg/item-bag.svg"}
alt={action?.name ?? item.name}
/>
<img class="a5e-item__image--die" src="/icons/svg/d20.svg" alt="Roll" />
</button>
<ItemInnerWrapper
{actionId}
{action}
{item}
on:toggleActionList={() => {
showActionList = !showActionList;
item.setFlag("a5e", "showActionList", showActionList);
}}
on:toggleContainer={() => {
showContainerItems = !showContainerItems;
item.setFlag("a5e", "showContainer", showContainerItems);
}}
/>
{#if $actor.isOwner}
<ItemActionButtons action={actionId} {item} />
{/if}
</li>
{#if showDescription}
<div class="description-wrapper" transition:slide|local>
{#if !isGM && item.type === "object" && item.system.unidentified}
{@html item.system.unidentifiedDescription ??
localize("A5E.NoUnidentifiedDescription")}
{:else}
{#if Object.values(summaryData).some(Boolean)}
<ItemSummary {summaryData} --margin-bottom="0.375rem" />
{#if description}
<hr class="a5e-rule" style="margin-block: 0.5rem;" />
{/if}
{/if}
{@html description}
{/if}
</div>
{/if}
{#if showActionList}
<ul class="a5e-item-list a5e-item-list--sub-items">
{#each item?.actions?.entries() ?? [] as [id, action] (id)}
<svelte:self {item} {action} actionId={id} />
{/each}
</ul>
{/if}
{#if showContainerItems}
<ul class="a5e-item-list a5e-item-list--sub-items">
{#each containerItems as [id, child] (id)}
<svelte:self item={child} />
{/each}
</ul>
{/if}
<style lang="scss">
.description-wrapper {
padding: 0.25rem;
}
.disable-pointer-events {
pointer-events: none;
}
</style>