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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
<!--
The note-row editor (the reference `NoteRowMenu`): used both to create a
new note (note == null) and to edit an existing one. In create mode all
edits accumulate locally and are committed by the Add button; in edit
mode colour/link changes commit immediately and the text commits on close
(only when it actually changed, so an unedited open/close doesn't pollute
the undo history).
-->
<script lang="ts">
import type { GameSession } from '$lib/state/session';
import {
isCharacter,
isMonster,
type CharacterItem,
type MonsterItem,
type NoteRowItem
} from '$lib/protocol/types';
import { t } from '$lib/i18n';
import Menu from '../Menu.svelte';
/** The reference `_kPalette` (12 swatches, ARGB). */
const PALETTE = [
0xff4e5d6c,
0xff6d4c41,
0xff37474f,
0xff5d4037,
0xff455a64,
0xff6a1b9a,
0xffad1457,
0xffc62828,
0xffe65100,
0xff2e7d32,
0xff1565c0,
0xff00838f
];
let {
session,
note = null,
presetLinkId = '',
presetStandeeNr = 0,
open = $bindable(true)
}: {
session: GameSession;
/** The existing note to edit; null to create a new one. */
note?: NoteRowItem | null;
/** Create mode only: pre-select a link target (status-menu entry). */
presetLinkId?: string;
presetStandeeNr?: number;
open?: boolean;
} = $props();
const player = session.player;
const gameState = session.state;
const isEdit = $derived(note != null);
const canAct = $derived(!!$player && $gameState != null);
let text = $state(note?.text ?? '');
let color = $state(note?.color ?? 0xff4e5d6c);
let linkedId = $state(note?.linkedId ?? presetLinkId);
let standeeNr = $state(note?.standeeNr ?? presetStandeeNr);
let lastCommitted = $state(text);
function colorCss(c: number): string {
return `#${c.toString(16).padStart(8, '0').slice(2)}`;
}
function cmd(name: string, args: Record<string, unknown>) {
if ($player) session.command(name, args);
}
/** Every link target (characters and monsters) in list order. */
const figures = $derived.by(() => {
const out: { id: string; kind: 'player' | 'monster'; display: string }[] = [];
for (const item of $gameState?.currentList ?? []) {
if (isCharacter(item)) {
const c = item as CharacterItem;
out.push({ id: c.id, kind: 'player', display: c.characterState.display || c.id });
} else if (isMonster(item)) {
out.push({ id: item.id, kind: 'monster', display: item.id });
}
}
return out;
});
/** The linked monster (for the standee selector), if any. */
const linkedMonster = $derived.by((): MonsterItem | null => {
const gs = $gameState;
if (linkedId === '' || gs == null) return null;
return (
gs.currentList.find((i): i is MonsterItem => isMonster(i) && i.id === linkedId) ??
null
);
});
function pickColor(c: number) {
color = c;
if (note) cmd('setNoteRowColor', { color: c, noteId: note.id });
}
function setLink(id: string, nr: number) {
linkedId = id;
standeeNr = id === '' ? 0 : nr;
if (note)
cmd('setNoteRowLink', {
noteId: note.id,
linkedId,
standeeNr
});
}
function commitText() {
if (!note) return;
const trimmed = text.trim();
if (trimmed !== lastCommitted) {
lastCommitted = trimmed;
cmd('setNoteRowText', { text: trimmed, noteId: note.id });
}
}
function add() {
cmd('addNoteRow', {
noteRow: {
id: 'note_' + crypto.randomUUID(),
text: text.trim(),
color,
linkedId,
standeeNr
}
});
open = false;
}
function remove() {
if (note) cmd('removeNoteRow', { noteId: note.id });
open = false;
}
function close() {
commitText();
open = false;
}
</script>
<Menu {open} title={t('noteRowTitle')}>
<textarea
class="search search--area"
rows={3}
maxlength={300}
placeholder={t('noteRowHint')}
bind:value={text}
readonly={!canAct}
onblur={commitText}
></textarea>
{#if canAct}
<div class="fieldlabel">{t('noteRowColourLabel')}</div>
<div class="palette" role="radiogroup" aria-label={t('noteRowColourLabel')}>
{#each PALETTE as c (c)}
<button
class="swatch"
class:swatch--selected={c === color}
style:background={colorCss(c)}
aria-label={colorCss(c)}
onclick={() => pickColor(c)}
></button>
{/each}
</div>
<div class="fieldlabel">{t('noteRowConnectLabel')}</div>
<select
class="search"
style="margin: 0"
value={linkedId}
onchange={(e) => setLink(e.currentTarget.value, 0)}
>
<option value="">{t('noteRowConnectNone')}</option>
{#each figures as f (f.id)}
<option value={f.id}>
{f.kind === 'player' ? t('noteRowConnectPlayer') : t('noteRowConnectMonster')}:
{f.display}
</option>
{/each}
</select>
{#if linkedMonster}
<select
class="search"
style="margin: 0"
value={linkedMonster.monsterInstances.some((s) => s.standeeNr === standeeNr)
? standeeNr
: 0}
onchange={(e) => setLink(linkedId, Number(e.currentTarget.value))}
>
<option value={0}>{t('noteRowWholeGroup')}</option>
{#each linkedMonster.monsterInstances as s (s.standeeNr)}
<option value={s.standeeNr}>{t('noteRowStandeeNr', { nr: s.standeeNr })}</option>
{/each}
</select>
{/if}
<div class="actions">
{#if isEdit && note}
<button class="pill pill--danger" onclick={remove}>{t('noteRowDelete')}</button>
{:else}
<span></span>
{/if}
{#if isEdit}
<button class="pill" onclick={close}>{t('close')}</button>
{:else}
<button class="pill pill--primary" onclick={add}>{t('noteRowAddFromMenu')}</button>
{/if}
</div>
{/if}
</Menu>
<style>
textarea.search--area {
resize: vertical;
min-height: 64px;
margin-bottom: 10px;
}
.fieldlabel {
font-size: 13px;
margin: 10px 0 4px;
opacity: 0.85;
}
.palette {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.swatch {
width: 26px;
height: 26px;
border-radius: 50%;
border: 1px solid rgba(0, 0, 0, 0.4);
cursor: pointer;
padding: 0;
}
.swatch--selected {
border: 3px solid #fff;
}
.actions {
display: flex;
justify-content: space-between;
gap: 8px;
margin-top: 14px;
}
.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);
}
.pill--primary {
background: rgba(255, 255, 255, 0.3);
font-weight: 600;
}
.pill--danger {
color: #ff5252;
}
</style>
|