atagen

malice

public
Star0
Watch0
7371424b2f055abe9920fadad7d1e66fc85462c2
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git
web/src/lib/ReactionPicker.svelteSvelte256 lines9.0 KB
1
<script lang="ts">
2
  import { onMount } from 'svelte';
3
  import { mediaUrl, messageMediaUrl } from './api';
4
  import Emoji from './Emoji.svelte';
5
  import { loadEmoji, searchEmoji } from './emoji';
6
  import type { EmojiGroup } from './emoji';
7
  import { MAX_QUICK_REACTIONS } from './types';
8
  import type { ImagePack, PackImage, QuickReaction } from './types';
9
10
  export let packs: ImagePack[];
11
  export let quick: QuickReaction[];
12
  export let onPick: (key: string, shortcode: string | null) => void;
13
  export let onQuickChange: (list: QuickReaction[]) => void;
14
  export let onClose: () => void;
15
  export let embedded = false;
16
17
  let root: HTMLDivElement;
18
  let input: HTMLInputElement;
19
  let query = '';
20
  let customising = embedded;
21
  let groups: EmojiGroup[] | null = null;
22
  let loadFailed = false;
23
24
  onMount(() => {
25
    if (!embedded) input.focus();
26
    loadEmoji().then(
27
      (g) => (groups = g),
28
      () => (loadFailed = true)
29
    );
30
  });
31
32
  $: q = query.trim().toLowerCase().replace(/^:|:$/g, '');
33
  $: filteredPacks = packs
34
    .map((pack) => ({ pack, images: pack.images.filter((i) => imageMatches(i, q)) }))
35
    .filter((p) => p.images.length > 0);
36
  $: filteredGroups = searchEmoji(groups ?? [], q);
37
  $: full = quick.length >= MAX_QUICK_REACTIONS;
38
  $: nothingFound = q !== '' && groups !== null && filteredPacks.length === 0 && filteredGroups.length === 0;
39
40
  function imageMatches(image: PackImage, needle: string): boolean {
41
    if (!needle) return true;
42
    return image.shortcode.toLowerCase().includes(needle) || (image.body ?? '').toLowerCase().includes(needle);
43
  }
44
45
  function packName(pack: ImagePack): string {
46
    return pack.display_name ?? 'Emoji pack';
47
  }
48
49
  function isQuick(key: string): boolean {
50
    return quick.some((r) => r.key === key);
51
  }
52
53
  function choose(key: string, shortcode: string | null): void {
54
    if (!customising) {
55
      onPick(key, shortcode);
56
      return;
57
    }
58
    if (isQuick(key)) onQuickChange(quick.filter((r) => r.key !== key));
59
    else if (!full) onQuickChange([...quick, { key, shortcode }]);
60
  }
61
62
  function quickLabel(r: QuickReaction): string {
63
    return r.shortcode ? `:${r.shortcode}:` : r.key;
64
  }
65
66
  function onKeydown(e: KeyboardEvent): void {
67
    if (e.key !== 'Escape') return;
68
    e.stopPropagation();
69
    onClose();
70
  }
71
72
  function closeOutside(e: PointerEvent): void {
73
    if (!embedded && !root.contains(e.target as Node)) onClose();
74
  }
75
</script>
76
77
<svelte:window on:pointerdown={closeOutside} />
78
79
<div
80
  class="picker"
81
  class:embedded
82
  role="dialog"
83
  tabindex="-1"
84
  aria-label={customising ? 'Customise quick reactions' : 'Pick a reaction'}
85
  bind:this={root}
86
  on:keydown={onKeydown}
87
>
88
  <input
89
    class="search"
90
    type="search"
91
    placeholder="Search emoji"
92
    aria-label="Search emoji"
93
    autocomplete="off"
94
    bind:this={input}
95
    bind:value={query}
96
  />
97
98
  {#if customising}
99
    <div class="quick" role="group" aria-label="Quick reactions">
100
      <div class="quick-head">
101
        <span>Quick reactions</span>
102
        <span class="muted">{quick.length}/{MAX_QUICK_REACTIONS}</span>
103
      </div>
104
      <div class="quick-row">
105
        {#each quick as r (r.key)}
106
          <span class="quick-item">
107
            {#if r.key.startsWith('mxc://')}
108
              <img src={mediaUrl(r.key) ?? ''} alt={quickLabel(r)} />
109
            {:else}
110
              <Emoji emoji={r.key} size={18} />
111
            {/if}
112
            <button
113
              type="button"
114
              class="remove"
115
              aria-label={`Remove ${quickLabel(r)} from quick reactions`}
116
              on:click={() => onQuickChange(quick.filter((x) => x.key !== r.key))}
117
            >
118
              ×
119
            </button>
120
          </span>
121
        {:else}
122
          <span class="muted">None. Pick emoji below to add them.</span>
123
        {/each}
124
      </div>
125
    </div>
126
  {/if}
127
128
  <div class="scroll" aria-busy={groups === null && !loadFailed}>
129
    {#each filteredPacks as { pack, images } (pack.id)}
130
      <section class="group" aria-label={packName(pack)}>
131
        <h3>{packName(pack)}</h3>
132
        <div class="grid">
133
          {#each images as image (image.reaction_key + image.shortcode)}
134
            <button
135
              type="button"
136
              class="cell"
137
              class:chosen={customising && isQuick(image.reaction_key)}
138
              title={`:${image.shortcode}:`}
139
              aria-label={`:${image.shortcode}:`}
140
              aria-pressed={customising ? isQuick(image.reaction_key) : undefined}
141
              disabled={customising && full && !isQuick(image.reaction_key)}
142
              on:click={() => choose(image.reaction_key, image.shortcode)}
143
            >
144
              <img src={messageMediaUrl(image.media_id, 'Thumbnail')} alt="" loading="lazy" />
145
            </button>
146
          {/each}
147
        </div>
148
      </section>
149
    {/each}
150
151
    {#if loadFailed}
152
      <p class="status" role="alert">Could not load the emoji list.</p>
153
    {:else if groups === null}
154
      <p class="status" role="status">Loading emoji…</p>
155
    {:else}
156
      {#each filteredGroups as g (g.name)}
157
        <section class="group" aria-label={g.name}>
158
          <h3>{g.name}</h3>
159
          <div class="grid">
160
            {#each g.emojis as e (e.emoji)}
161
              <button
162
                type="button"
163
                class="cell"
164
                class:chosen={customising && isQuick(e.emoji)}
165
                title={e.label}
166
                aria-label={e.label}
167
                aria-pressed={customising ? isQuick(e.emoji) : undefined}
168
                disabled={customising && full && !isQuick(e.emoji)}
169
                on:click={() => choose(e.emoji, e.shortcodes[0] ?? null)}
170
              >
171
                <Emoji emoji={e.emoji} size={24} />
172
              </button>
173
            {/each}
174
          </div>
175
        </section>
176
      {/each}
177
    {/if}
178
    {#if nothingFound}
179
      <p class="status" role="status">No emoji match "{query.trim()}".</p>
180
    {/if}
181
  </div>
182
183
  <div class="foot">
184
    {#if customising}
185
      <span class="muted">{full ? 'Quick reactions are full. Remove one to add another.' : 'Select emoji to add or remove them.'}</span>
186
    {/if}
187
    {#if !embedded}
188
      <button type="button" class="mode" aria-pressed={customising} on:click={() => (customising = !customising)}>
189
        {customising ? 'Done' : 'Customise quick reactions'}
190
      </button>
191
    {/if}
192
  </div>
193
</div>
194
195
<style>
196
  .picker.embedded { width: 100%; height: 340px; box-shadow: none; }
197
  .picker {
198
    outline: none;
199
    display: flex; flex-direction: column; gap: 6px;
200
    width: 320px; height: 380px; max-width: 100%; max-height: 100%;
201
    padding: 8px;
202
    background: var(--panel);
203
    border: 1px solid var(--border);
204
    border-radius: 10px;
205
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
206
  }
207
  .search {
208
    background: var(--bg-soft);
209
    border: 1px solid var(--border);
210
    border-radius: 8px;
211
    color: var(--text);
212
    padding: 6px 10px;
213
    font-size: 13px;
214
  }
215
  .search:focus-visible { outline: none; border-color: var(--accent); }
216
  .muted { color: var(--muted); font-size: 12px; }
217
  .quick { border: 1px solid var(--border); border-radius: 8px; padding: 6px; background: var(--bg-soft); }
218
  .quick-head { display: flex; justify-content: space-between; font-size: 12px; font-weight: 650; margin-bottom: 4px; }
219
  .quick-row { display: flex; flex-wrap: wrap; gap: 4px; min-height: 28px; align-items: center; }
220
  .quick-item {
221
    display: inline-flex; align-items: center; gap: 2px;
222
    padding: 1px 2px 1px 6px;
223
    border: 1px solid var(--border); border-radius: 999px;
224
    font-size: 16px;
225
  }
226
  .quick-item img { width: 18px; height: 18px; object-fit: contain; }
227
  .remove {
228
    background: transparent; border: none; color: var(--muted);
229
    cursor: pointer; font-size: 14px; line-height: 1; padding: 2px 5px; border-radius: 999px;
230
  }
231
  .remove:hover { color: var(--danger); }
232
  .scroll { flex: 1; min-height: 0; overflow-y: auto; }
233
  .group h3 {
234
    margin: 6px 2px 2px; font-size: 11px; font-weight: 650;
235
    text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted);
236
  }
237
  .grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(32px, 1fr)); gap: 2px; }
238
  .cell {
239
    display: grid; place-items: center;
240
    height: 32px; padding: 0;
241
    background: transparent; border: 1px solid transparent; border-radius: 6px;
242
    font-size: 20px; cursor: pointer; color: var(--text);
243
  }
244
  .cell img { width: 24px; height: 24px; object-fit: contain; }
245
  .cell:hover:not(:disabled), .cell:focus-visible { background: var(--hover); border-color: var(--border); outline: none; }
246
  .cell.chosen { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 16%, transparent); }
247
  .cell:disabled { opacity: 0.3; cursor: default; }
248
  .status { color: var(--muted); font-size: 13px; text-align: center; margin: 16px 0; }
249
  .foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
250
  .mode {
251
    margin-left: auto;
252
    background: transparent; border: 1px solid var(--border); border-radius: 6px;
253
    color: var(--text); font-size: 12px; padding: 3px 8px; cursor: pointer;
254
  }
255
  .mode:hover, .mode[aria-pressed='true'] { border-color: var(--accent); }
256
</style>