summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/menus/FigureNoteMenu.svelte
blob: 45c7a4d4f382d5de710c103f31a7f9fbb1e1ad79 (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
<!--
	A figure's quick note (the reference `SetFigureNoteMenu`): the free-form
	note shown inline on a figure's row (character or monster). The note is
	committed (via `setFigureNote`) when the field loses focus or the dialog
	is dismissed, and only when the text actually changed, so an unedited
	open/close doesn't pollute the undo history.
-->
<script lang="ts">
	import type { GameSession } from '$lib/state/session';
	import Menu from '../Menu.svelte';

	let {
		session,
		figureId,
		initial,
		open = $bindable(true)
	}: {
		session: GameSession;
		figureId: string;
		initial: string;
		open?: boolean;
	} = $props();

	const player = $derived(session.player);

	// The edit buffer is seeded from the prop once on mount (the menu is
	// remounted when opened); a later prop change must not clobber the edit.
	// svelte-ignore state_referenced_locally
	let text = $state(initial);
	// svelte-ignore state_referenced_locally
	let lastCommitted = $state(initial);

	function commit() {
		const trimmed = text.trim();
		if (trimmed !== lastCommitted) {
			lastCommitted = trimmed;
			if ($player)
				session.command('setFigureNote', { note: trimmed, figureId });
		}
	}

	function close() {
		commit();
		open = false;
	}
</script>

<Menu {open}>
	<textarea
		class="search search--area"
		rows={3}
		maxlength={140}
		placeholder="…"
		bind:value={text}
		readonly={!$player}
		onblur={commit}
	></textarea>
	{#if $player}
		<div class="actions">
			<button class="pill" onclick={close}>Save</button>
		</div>
	{/if}
</Menu>

<style>
	textarea.search--area {
		resize: vertical;
		min-height: 64px;
	}
	.actions {
		display: flex;
		justify-content: flex-end;
		margin-top: 10px;
	}
	.pill {
		padding: 6px 14px;
		font-size: 13px;
		border: none;
		border-radius: 4px;
		background: rgba(255, 255, 255, 0.12);
		color: inherit;
		cursor: pointer;
	}
	.pill:hover {
		background: rgba(255, 255, 255, 0.25);
	}
</style>