atagen

malice

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