atagen

malice

public
Star0
Watch0
c8d898e6eb59492dd33226d492b4a9e44d606800
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git

Blame: web/src/lib/ReactionPicker.svelte

544a170ead2catagen2026-10-20 11:47 UTC1
<script lang="ts">
445bf199a9cfatagen2026-10-25 02:56 UTC2
  import EmojiText from './EmojiText.svelte';
544a170ead2catagen2026-10-20 11:47 UTC3
  import { onMount } from 'svelte';
ecb8598b38afatagen2026-10-22 09:19 UTC4
  import { mediaUrl, messageMediaUrl } from './api';
544a170ead2catagen2026-10-20 11:47 UTC5
  import Emoji from './Emoji.svelte';
544a170ead2catagen2026-10-20 11:47 UTC6
  import { loadEmoji, searchEmoji } from './emoji';
544a170ead2catagen2026-10-20 11:47 UTC7
  import type { EmojiGroup } from './emoji';
544a170ead2catagen2026-10-20 11:47 UTC8
  import type { ImagePack, PackImage, QuickReaction } from './types';
132eeceeb362atagen2026-10-25 03:34 UTC9
  import { hasUsage } from './packs';
544a170ead2catagen2026-10-20 11:47 UTC10
544a170ead2catagen2026-10-20 11:47 UTC11
  export let packs: ImagePack[];
544a170ead2catagen2026-10-20 11:47 UTC12
  export let quick: QuickReaction[];
544a170ead2catagen2026-10-20 11:47 UTC13
  export let onPick: (key: string, shortcode: string | null) => void;
544a170ead2catagen2026-10-20 11:47 UTC14
  export let onQuickChange: (list: QuickReaction[]) => void;
544a170ead2catagen2026-10-20 11:47 UTC15
  export let onClose: () => void;
df3c0f2b3da4atagen2026-10-22 13:07 UTC16
  export let embedded = false;
544a170ead2catagen2026-10-20 11:47 UTC17
544a170ead2catagen2026-10-20 11:47 UTC18
  let root: HTMLDivElement;
544a170ead2catagen2026-10-20 11:47 UTC19
  let input: HTMLInputElement;
544a170ead2catagen2026-10-20 11:47 UTC20
  let query = '';
df3c0f2b3da4atagen2026-10-22 13:07 UTC21
  let customising = embedded;
544a170ead2catagen2026-10-20 11:47 UTC22
  let groups: EmojiGroup[] | null = null;
544a170ead2catagen2026-10-20 11:47 UTC23
  let loadFailed = false;
544a170ead2catagen2026-10-20 11:47 UTC24
544a170ead2catagen2026-10-20 11:47 UTC25
  onMount(() => {
df3c0f2b3da4atagen2026-10-22 13:07 UTC26
    if (!embedded) input.focus();
544a170ead2catagen2026-10-20 11:47 UTC27
    loadEmoji().then(
544a170ead2catagen2026-10-20 11:47 UTC28
      (g) => (groups = g),
544a170ead2catagen2026-10-20 11:47 UTC29
      () => (loadFailed = true)
544a170ead2catagen2026-10-20 11:47 UTC30
    );
544a170ead2catagen2026-10-20 11:47 UTC31
  });
544a170ead2catagen2026-10-20 11:47 UTC32
544a170ead2catagen2026-10-20 11:47 UTC33
  $: q = query.trim().toLowerCase().replace(/^:|:$/g, '');
544a170ead2catagen2026-10-20 11:47 UTC34
  $: filteredPacks = packs
544a170ead2catagen2026-10-20 11:47 UTC35
    .map((pack) => ({ pack, images: pack.images.filter((i) => imageMatches(i, q)) }))
544a170ead2catagen2026-10-20 11:47 UTC36
    .filter((p) => p.images.length > 0);
544a170ead2catagen2026-10-20 11:47 UTC37
  $: filteredGroups = searchEmoji(groups ?? [], q);
544a170ead2catagen2026-10-20 11:47 UTC38
  $: nothingFound = q !== '' && groups !== null && filteredPacks.length === 0 && filteredGroups.length === 0;
544a170ead2catagen2026-10-20 11:47 UTC39
544a170ead2catagen2026-10-20 11:47 UTC40
  function imageMatches(image: PackImage, needle: string): boolean {
132eeceeb362atagen2026-10-25 03:34 UTC41
    if (!hasUsage(image, 'Emoticon')) return false;
544a170ead2catagen2026-10-20 11:47 UTC42
    if (!needle) return true;
544a170ead2catagen2026-10-20 11:47 UTC43
    return image.shortcode.toLowerCase().includes(needle) || (image.body ?? '').toLowerCase().includes(needle);
544a170ead2catagen2026-10-20 11:47 UTC44
  }
544a170ead2catagen2026-10-20 11:47 UTC45
ecb8598b38afatagen2026-10-22 09:19 UTC46
  function packName(pack: ImagePack): string {
ecb8598b38afatagen2026-10-22 09:19 UTC47
    return pack.display_name ?? 'Emoji pack';
ecb8598b38afatagen2026-10-22 09:19 UTC48
  }
ecb8598b38afatagen2026-10-22 09:19 UTC49
544a170ead2catagen2026-10-20 11:47 UTC50
  function isQuick(key: string): boolean {
544a170ead2catagen2026-10-20 11:47 UTC51
    return quick.some((r) => r.key === key);
544a170ead2catagen2026-10-20 11:47 UTC52
  }
544a170ead2catagen2026-10-20 11:47 UTC53
544a170ead2catagen2026-10-20 11:47 UTC54
  function choose(key: string, shortcode: string | null): void {
544a170ead2catagen2026-10-20 11:47 UTC55
    if (!customising) {
544a170ead2catagen2026-10-20 11:47 UTC56
      onPick(key, shortcode);
544a170ead2catagen2026-10-20 11:47 UTC57
      return;
544a170ead2catagen2026-10-20 11:47 UTC58
    }
544a170ead2catagen2026-10-20 11:47 UTC59
    if (isQuick(key)) onQuickChange(quick.filter((r) => r.key !== key));
8c81366a752fatagen2026-10-26 03:56 UTC60
    else onQuickChange([...quick, { key, shortcode }]);
544a170ead2catagen2026-10-20 11:47 UTC61
  }
544a170ead2catagen2026-10-20 11:47 UTC62
544a170ead2catagen2026-10-20 11:47 UTC63
  function quickLabel(r: QuickReaction): string {
544a170ead2catagen2026-10-20 11:47 UTC64
    return r.shortcode ? `:${r.shortcode}:` : r.key;
544a170ead2catagen2026-10-20 11:47 UTC65
  }
544a170ead2catagen2026-10-20 11:47 UTC66
544a170ead2catagen2026-10-20 11:47 UTC67
  function onKeydown(e: KeyboardEvent): void {
544a170ead2catagen2026-10-20 11:47 UTC68
    if (e.key !== 'Escape') return;
544a170ead2catagen2026-10-20 11:47 UTC69
    e.stopPropagation();
544a170ead2catagen2026-10-20 11:47 UTC70
    onClose();
544a170ead2catagen2026-10-20 11:47 UTC71
  }
544a170ead2catagen2026-10-20 11:47 UTC72
544a170ead2catagen2026-10-20 11:47 UTC73
  function closeOutside(e: PointerEvent): void {
df3c0f2b3da4atagen2026-10-22 13:07 UTC74
    if (!embedded && !root.contains(e.target as Node)) onClose();
544a170ead2catagen2026-10-20 11:47 UTC75
  }
544a170ead2catagen2026-10-20 11:47 UTC76
</script>
544a170ead2catagen2026-10-20 11:47 UTC77
544a170ead2catagen2026-10-20 11:47 UTC78
<svelte:window on:pointerdown={closeOutside} />
544a170ead2catagen2026-10-20 11:47 UTC79
544a170ead2catagen2026-10-20 11:47 UTC80
<div
544a170ead2catagen2026-10-20 11:47 UTC81
  class="picker"
df3c0f2b3da4atagen2026-10-22 13:07 UTC82
  class:embedded
544a170ead2catagen2026-10-20 11:47 UTC83
  role="dialog"
544a170ead2catagen2026-10-20 11:47 UTC84
  tabindex="-1"
544a170ead2catagen2026-10-20 11:47 UTC85
  aria-label={customising ? 'Customise quick reactions' : 'Pick a reaction'}
544a170ead2catagen2026-10-20 11:47 UTC86
  bind:this={root}
544a170ead2catagen2026-10-20 11:47 UTC87
  on:keydown={onKeydown}
544a170ead2catagen2026-10-20 11:47 UTC88
>
544a170ead2catagen2026-10-20 11:47 UTC89
  <input
544a170ead2catagen2026-10-20 11:47 UTC90
    class="search"
544a170ead2catagen2026-10-20 11:47 UTC91
    type="search"
544a170ead2catagen2026-10-20 11:47 UTC92
    placeholder="Search emoji"
544a170ead2catagen2026-10-20 11:47 UTC93
    aria-label="Search emoji"
544a170ead2catagen2026-10-20 11:47 UTC94
    autocomplete="off"
544a170ead2catagen2026-10-20 11:47 UTC95
    bind:this={input}
544a170ead2catagen2026-10-20 11:47 UTC96
    bind:value={query}
544a170ead2catagen2026-10-20 11:47 UTC97
  />
544a170ead2catagen2026-10-20 11:47 UTC98
544a170ead2catagen2026-10-20 11:47 UTC99
  {#if customising}
544a170ead2catagen2026-10-20 11:47 UTC100
    <div class="quick" role="group" aria-label="Quick reactions">
544a170ead2catagen2026-10-20 11:47 UTC101
      <div class="quick-head">
544a170ead2catagen2026-10-20 11:47 UTC102
        <span>Quick reactions</span>
544a170ead2catagen2026-10-20 11:47 UTC103
      </div>
544a170ead2catagen2026-10-20 11:47 UTC104
      <div class="quick-row">
544a170ead2catagen2026-10-20 11:47 UTC105
        {#each quick as r (r.key)}
544a170ead2catagen2026-10-20 11:47 UTC106
          <span class="quick-item">
544a170ead2catagen2026-10-20 11:47 UTC107
            {#if r.key.startsWith('mxc://')}
7643577c76bbatagen2026-10-25 07:37 UTC108
              <img src={mediaUrl(r.key, 64) ?? ''} alt={quickLabel(r)} />
544a170ead2catagen2026-10-20 11:47 UTC109
            {:else}
544a170ead2catagen2026-10-20 11:47 UTC110
              <Emoji emoji={r.key} size={18} />
544a170ead2catagen2026-10-20 11:47 UTC111
            {/if}
544a170ead2catagen2026-10-20 11:47 UTC112
            <button
544a170ead2catagen2026-10-20 11:47 UTC113
              type="button"
544a170ead2catagen2026-10-20 11:47 UTC114
              class="remove"
544a170ead2catagen2026-10-20 11:47 UTC115
              aria-label={`Remove ${quickLabel(r)} from quick reactions`}
544a170ead2catagen2026-10-20 11:47 UTC116
              on:click={() => onQuickChange(quick.filter((x) => x.key !== r.key))}
544a170ead2catagen2026-10-20 11:47 UTC117
            >
544a170ead2catagen2026-10-20 11:47 UTC118
              ×
544a170ead2catagen2026-10-20 11:47 UTC119
            </button>
544a170ead2catagen2026-10-20 11:47 UTC120
          </span>
544a170ead2catagen2026-10-20 11:47 UTC121
        {:else}
544a170ead2catagen2026-10-20 11:47 UTC122
          <span class="muted">None. Pick emoji below to add them.</span>
544a170ead2catagen2026-10-20 11:47 UTC123
        {/each}
544a170ead2catagen2026-10-20 11:47 UTC124
      </div>
544a170ead2catagen2026-10-20 11:47 UTC125
    </div>
544a170ead2catagen2026-10-20 11:47 UTC126
  {/if}
544a170ead2catagen2026-10-20 11:47 UTC127
544a170ead2catagen2026-10-20 11:47 UTC128
  <div class="scroll" aria-busy={groups === null && !loadFailed}>
544a170ead2catagen2026-10-20 11:47 UTC129
    {#each filteredPacks as { pack, images } (pack.id)}
ecb8598b38afatagen2026-10-22 09:19 UTC130
      <section class="group" aria-label={packName(pack)}>
445bf199a9cfatagen2026-10-25 02:56 UTC131
        <h3><EmojiText text={packName(pack)} /></h3>
544a170ead2catagen2026-10-20 11:47 UTC132
        <div class="grid">
ecb8598b38afatagen2026-10-22 09:19 UTC133
          {#each images as image (image.reaction_key + image.shortcode)}
544a170ead2catagen2026-10-20 11:47 UTC134
            <button
544a170ead2catagen2026-10-20 11:47 UTC135
              type="button"
544a170ead2catagen2026-10-20 11:47 UTC136
              class="cell"
ecb8598b38afatagen2026-10-22 09:19 UTC137
              class:chosen={customising && isQuick(image.reaction_key)}
544a170ead2catagen2026-10-20 11:47 UTC138
              title={`:${image.shortcode}:`}
544a170ead2catagen2026-10-20 11:47 UTC139
              aria-label={`:${image.shortcode}:`}
ecb8598b38afatagen2026-10-22 09:19 UTC140
              aria-pressed={customising ? isQuick(image.reaction_key) : undefined}
ecb8598b38afatagen2026-10-22 09:19 UTC141
              on:click={() => choose(image.reaction_key, image.shortcode)}
544a170ead2catagen2026-10-20 11:47 UTC142
            >
ecb8598b38afatagen2026-10-22 09:19 UTC143
              <img src={messageMediaUrl(image.media_id, 'Thumbnail')} alt="" loading="lazy" />
544a170ead2catagen2026-10-20 11:47 UTC144
            </button>
544a170ead2catagen2026-10-20 11:47 UTC145
          {/each}
544a170ead2catagen2026-10-20 11:47 UTC146
        </div>
544a170ead2catagen2026-10-20 11:47 UTC147
      </section>
544a170ead2catagen2026-10-20 11:47 UTC148
    {/each}
544a170ead2catagen2026-10-20 11:47 UTC149
544a170ead2catagen2026-10-20 11:47 UTC150
    {#if loadFailed}
544a170ead2catagen2026-10-20 11:47 UTC151
      <p class="status" role="alert">Could not load the emoji list.</p>
544a170ead2catagen2026-10-20 11:47 UTC152
    {:else if groups === null}
544a170ead2catagen2026-10-20 11:47 UTC153
      <p class="status" role="status">Loading emoji…</p>
544a170ead2catagen2026-10-20 11:47 UTC154
    {:else}
544a170ead2catagen2026-10-20 11:47 UTC155
      {#each filteredGroups as g (g.name)}
544a170ead2catagen2026-10-20 11:47 UTC156
        <section class="group" aria-label={g.name}>
544a170ead2catagen2026-10-20 11:47 UTC157
          <h3>{g.name}</h3>
544a170ead2catagen2026-10-20 11:47 UTC158
          <div class="grid">
544a170ead2catagen2026-10-20 11:47 UTC159
            {#each g.emojis as e (e.emoji)}
544a170ead2catagen2026-10-20 11:47 UTC160
              <button
544a170ead2catagen2026-10-20 11:47 UTC161
                type="button"
544a170ead2catagen2026-10-20 11:47 UTC162
                class="cell"
544a170ead2catagen2026-10-20 11:47 UTC163
                class:chosen={customising && isQuick(e.emoji)}
544a170ead2catagen2026-10-20 11:47 UTC164
                title={e.label}
544a170ead2catagen2026-10-20 11:47 UTC165
                aria-label={e.label}
544a170ead2catagen2026-10-20 11:47 UTC166
                aria-pressed={customising ? isQuick(e.emoji) : undefined}
544a170ead2catagen2026-10-20 11:47 UTC167
                on:click={() => choose(e.emoji, e.shortcodes[0] ?? null)}
544a170ead2catagen2026-10-20 11:47 UTC168
              >
d07bca5da4aeatagen2026-10-26 03:48 UTC169
                <Emoji emoji={e.emoji} size={24} lazy />
544a170ead2catagen2026-10-20 11:47 UTC170
              </button>
544a170ead2catagen2026-10-20 11:47 UTC171
            {/each}
544a170ead2catagen2026-10-20 11:47 UTC172
          </div>
544a170ead2catagen2026-10-20 11:47 UTC173
        </section>
544a170ead2catagen2026-10-20 11:47 UTC174
      {/each}
544a170ead2catagen2026-10-20 11:47 UTC175
    {/if}
544a170ead2catagen2026-10-20 11:47 UTC176
    {#if nothingFound}
445bf199a9cfatagen2026-10-25 02:56 UTC177
      <p class="status" role="status">No emoji match "<EmojiText text={query.trim()} />".</p>
544a170ead2catagen2026-10-20 11:47 UTC178
    {/if}
544a170ead2catagen2026-10-20 11:47 UTC179
  </div>
544a170ead2catagen2026-10-20 11:47 UTC180
05a48e9e7942atagen2026-10-24 09:32 UTC181
  {#if !embedded}
05a48e9e7942atagen2026-10-24 09:32 UTC182
    <div class="foot">
df3c0f2b3da4atagen2026-10-22 13:07 UTC183
      <button type="button" class="mode" aria-pressed={customising} on:click={() => (customising = !customising)}>
df3c0f2b3da4atagen2026-10-22 13:07 UTC184
        {customising ? 'Done' : 'Customise quick reactions'}
df3c0f2b3da4atagen2026-10-22 13:07 UTC185
      </button>
05a48e9e7942atagen2026-10-24 09:32 UTC186
    </div>
05a48e9e7942atagen2026-10-24 09:32 UTC187
  {/if}
544a170ead2catagen2026-10-20 11:47 UTC188
</div>
544a170ead2catagen2026-10-20 11:47 UTC189
544a170ead2catagen2026-10-20 11:47 UTC190
<style>
df3c0f2b3da4atagen2026-10-22 13:07 UTC191
  .picker.embedded { width: 100%; height: 340px; box-shadow: none; }
544a170ead2catagen2026-10-20 11:47 UTC192
  .picker {
544a170ead2catagen2026-10-20 11:47 UTC193
    outline: none;
544a170ead2catagen2026-10-20 11:47 UTC194
    display: flex; flex-direction: column; gap: 6px;
544a170ead2catagen2026-10-20 11:47 UTC195
    width: 320px; height: 380px; max-width: 100%; max-height: 100%;
544a170ead2catagen2026-10-20 11:47 UTC196
    padding: 8px;
544a170ead2catagen2026-10-20 11:47 UTC197
    background: var(--panel);
544a170ead2catagen2026-10-20 11:47 UTC198
    border: 1px solid var(--border);
05a48e9e7942atagen2026-10-24 09:32 UTC199
    border-radius: var(--r-md);
544a170ead2catagen2026-10-20 11:47 UTC200
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
544a170ead2catagen2026-10-20 11:47 UTC201
  }
544a170ead2catagen2026-10-20 11:47 UTC202
  .search {
544a170ead2catagen2026-10-20 11:47 UTC203
    background: var(--bg-soft);
544a170ead2catagen2026-10-20 11:47 UTC204
    border: 1px solid var(--border);
05a48e9e7942atagen2026-10-24 09:32 UTC205
    border-radius: var(--r-md);
544a170ead2catagen2026-10-20 11:47 UTC206
    color: var(--text);
544a170ead2catagen2026-10-20 11:47 UTC207
    padding: 6px 10px;
544a170ead2catagen2026-10-20 11:47 UTC208
    font-size: 13px;
544a170ead2catagen2026-10-20 11:47 UTC209
  }
544a170ead2catagen2026-10-20 11:47 UTC210
  .search:focus-visible { outline: none; border-color: var(--accent); }
544a170ead2catagen2026-10-20 11:47 UTC211
  .muted { color: var(--muted); font-size: 12px; }
05a48e9e7942atagen2026-10-24 09:32 UTC212
  .quick { border: 1px solid var(--border); border-radius: var(--r-md); padding: 6px; background: var(--bg-soft); }
544a170ead2catagen2026-10-20 11:47 UTC213
  .quick-head { display: flex; justify-content: space-between; font-size: 12px; font-weight: 650; margin-bottom: 4px; }
544a170ead2catagen2026-10-20 11:47 UTC214
  .quick-row { display: flex; flex-wrap: wrap; gap: 4px; min-height: 28px; align-items: center; }
544a170ead2catagen2026-10-20 11:47 UTC215
  .quick-item {
544a170ead2catagen2026-10-20 11:47 UTC216
    display: inline-flex; align-items: center; gap: 2px;
544a170ead2catagen2026-10-20 11:47 UTC217
    padding: 1px 2px 1px 6px;
05a48e9e7942atagen2026-10-24 09:32 UTC218
    border: 1px solid var(--border); border-radius: var(--r-sm);
544a170ead2catagen2026-10-20 11:47 UTC219
    font-size: 16px;
544a170ead2catagen2026-10-20 11:47 UTC220
  }
544a170ead2catagen2026-10-20 11:47 UTC221
  .quick-item img { width: 18px; height: 18px; object-fit: contain; }
544a170ead2catagen2026-10-20 11:47 UTC222
  .remove {
544a170ead2catagen2026-10-20 11:47 UTC223
    background: transparent; border: none; color: var(--muted);
05a48e9e7942atagen2026-10-24 09:32 UTC224
    cursor: pointer; font-size: 14px; line-height: 1; padding: 2px 5px; border-radius: var(--r-sm);
544a170ead2catagen2026-10-20 11:47 UTC225
  }
544a170ead2catagen2026-10-20 11:47 UTC226
  .remove:hover { color: var(--danger); }
544a170ead2catagen2026-10-20 11:47 UTC227
  .scroll { flex: 1; min-height: 0; overflow-y: auto; }
544a170ead2catagen2026-10-20 11:47 UTC228
  .group h3 {
544a170ead2catagen2026-10-20 11:47 UTC229
    margin: 6px 2px 2px; font-size: 11px; font-weight: 650;
544a170ead2catagen2026-10-20 11:47 UTC230
    text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted);
544a170ead2catagen2026-10-20 11:47 UTC231
  }
544a170ead2catagen2026-10-20 11:47 UTC232
  .grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(32px, 1fr)); gap: 2px; }
544a170ead2catagen2026-10-20 11:47 UTC233
  .cell {
544a170ead2catagen2026-10-20 11:47 UTC234
    display: grid; place-items: center;
544a170ead2catagen2026-10-20 11:47 UTC235
    height: 32px; padding: 0;
05a48e9e7942atagen2026-10-24 09:32 UTC236
    background: transparent; border: 1px solid transparent; border-radius: var(--r-sm);
544a170ead2catagen2026-10-20 11:47 UTC237
    font-size: 20px; cursor: pointer; color: var(--text);
544a170ead2catagen2026-10-20 11:47 UTC238
  }
544a170ead2catagen2026-10-20 11:47 UTC239
  .cell img { width: 24px; height: 24px; object-fit: contain; }
df3c0f2b3da4atagen2026-10-22 13:07 UTC240
  .cell:hover:not(:disabled), .cell:focus-visible { background: var(--hover); border-color: var(--border); outline: none; }
df3c0f2b3da4atagen2026-10-22 13:07 UTC241
  .cell.chosen { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 16%, transparent); }
544a170ead2catagen2026-10-20 11:47 UTC242
  .cell:disabled { opacity: 0.3; cursor: default; }
544a170ead2catagen2026-10-20 11:47 UTC243
  .status { color: var(--muted); font-size: 13px; text-align: center; margin: 16px 0; }
544a170ead2catagen2026-10-20 11:47 UTC244
  .foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
544a170ead2catagen2026-10-20 11:47 UTC245
  .mode {
544a170ead2catagen2026-10-20 11:47 UTC246
    margin-left: auto;
05a48e9e7942atagen2026-10-24 09:32 UTC247
    background: transparent; border: 1px solid var(--border); border-radius: var(--r-sm);
544a170ead2catagen2026-10-20 11:47 UTC248
    color: var(--text); font-size: 12px; padding: 3px 8px; cursor: pointer;
544a170ead2catagen2026-10-20 11:47 UTC249
  }
544a170ead2catagen2026-10-20 11:47 UTC250
  .mode:hover, .mode[aria-pressed='true'] { border-color: var(--accent); }
544a170ead2catagen2026-10-20 11:47 UTC251
</style>