summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/NoteRow.svelte
blob: 34af63ebd5873ca59f55c6b3787630c24ac92636 (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
<!--
	A note row (the reference `NoteRowWidget`, Phase 3 placeholder): a
	free-form coloured row. Tapping it edits the text inline and commits
	through `setNoteRowText`.
-->
<script lang="ts">
	import type { GameSession } from '$lib/state/session';
	import type { NoteRowItem } from '$lib/protocol/types';

	let {
		session,
		item
	}: {
		session: GameSession;
		item: NoteRowItem;
	} = $props();

	const player = session.player;

	let editing = $state(false);
	let draft = $state(item.text);

	function colorCss(color: number): string {
		return `#${color.toString(16).padStart(8, '0').slice(2)}`;
	}

	function startEdit() {
		if (!$player) return;
		draft = item.text;
		editing = true;
	}

	function commit() {
		editing = false;
		if (draft !== item.text) {
			session.command('setNoteRowText', { text: draft, noteId: item.id });
		}
	}
</script>

<div class="row row--note" style:background-color={colorCss(item.color)}>
	<div class="note__text">
		{#if editing}
			<input
				class="search"
				style="margin: 0; width: 100%"
				bind:value={draft}
				onblur={commit}
				onkeydown={(e) => {
					if (e.key === 'Enter') commit();
				}}
			/>
		{:else}
			<button class="note__edit" onclick={startEdit}>{item.text || '…'}</button>
		{/if}
	</div>
</div>