summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/MainList.svelte
blob: 3abe09b4794f39d942e632d8f0e1573e9c507217 (plain)
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
<!--
	The main list (the reference `MainList`/`GameList`): one row per entry of
	the server's `currentList`. The reference lays wide screens out as two
	long-press reorderable columns; the web uses a single centred column with
	drag-to-reorder (the reference `ReorderableWrap` → `reorderList`).
-->
<script lang="ts">
	import type { GameSession } from '$lib/state/session';
	import { isCharacter, isMonster, isNoteRow } from '$lib/protocol/types';
	import type { CharacterItem, MonsterItem, NoteRowItem } from '$lib/protocol/types';
	import type { CampaignData } from '$lib/data/gameData';
	import type { FigureRef } from './StatusMenu.svelte';
	import CharacterRow from './CharacterRow.svelte';
	import MonsterRow from './MonsterRow.svelte';
	import NoteRow from './NoteRow.svelte';

	let {
		session,
		onOpenMenu,
		onEditNote,
		onAddSummon,
		campaignData,
		onOpenAbility,
		onZoomAbility
	}: {
		session: GameSession;
		onOpenMenu: (figure: FigureRef) => void;
		onEditNote: (item: NoteRowItem) => void;
		onAddSummon: (item: CharacterItem) => void;
		campaignData: CampaignData | null;
		onOpenAbility: (item: MonsterItem) => void;
		onZoomAbility: (item: MonsterItem) => void;
	} = $props();

	const gameState = $derived(session.state);
	const player = $derived(session.player);

	// --- reordering (HTML5 drag & drop, the reference long-press drag) -------

	let dragIndex = $state<number | null>(null);
	let dropIndex = $state<number | null>(null);

	function onDragStart(e: DragEvent, i: number) {
		if (!$player) return;
		dragIndex = i;
		if (e.dataTransfer) {
			e.dataTransfer.effectAllowed = 'move';
			e.dataTransfer.setData('text/plain', String(i));
		}
	}
	function onDragOver(e: DragEvent, i: number) {
		if (dragIndex == null) return;
		e.preventDefault();
		if (e.dataTransfer) e.dataTransfer.dropEffect = 'move';
		dropIndex = i;
	}
	function onDrop() {
		if (dragIndex != null && dropIndex != null && dragIndex !== dropIndex) {
			session.command('reorderList', {
				newIndex: dropIndex,
				oldIndex: dragIndex
			});
		}
		dragIndex = null;
		dropIndex = null;
	}
	function onDragEnd() {
		dragIndex = null;
		dropIndex = null;
	}
</script>

<div class="list" role="list" aria-label="Game list">
	{#if $gameState}
		{#each $gameState.currentList as item, i (item.id)}
			<div
				role="listitem"
				class="list__slot"
				class:list__slot--drop={dropIndex === i && dragIndex != null && dragIndex !== i}
				draggable={!!$player}
				ondragstart={(e) => onDragStart(e, i)}
				ondragover={(e) => onDragOver(e, i)}
				ondrop={onDrop}
				ondragend={onDragEnd}
				ondragleave={() => {
					if (dropIndex === i) dropIndex = null;
				}}
			>
				{#if isCharacter(item)}
					<CharacterRow
						{session}
						{item}
						{onOpenMenu}
						onAddSummon={() => onAddSummon(item)}
					/>
				{:else if isMonster(item)}
					<MonsterRow
						{session}
						{item}
						{onOpenMenu}
						{campaignData}
						{onOpenAbility}
						{onZoomAbility}
					/>
				{:else if isNoteRow(item)}
					<NoteRow {session} {item} onEdit={onEditNote} />
				{/if}
			</div>
		{/each}
		{#if $gameState.currentList.length === 0}
			<p style="padding: 40px 12px; text-align: center; opacity: 0.7">
				No characters or monsters yet — use the menu to set a scenario
				and add characters.
			</p>
		{/if}
	{/if}
</div>

<style>
	.list__slot {
		width: 100%;
	}
	.list__slot--drop {
		outline: 2px solid #7ec8ff;
		outline-offset: -2px;
		border-radius: 6px;
	}
	.list__slot:focus-visible {
		outline: 2px solid #7ec8ff;
	}
</style>