atagen

malice

public
Star0
Watch0
705b1e1062c28d089cbd8a8c402d6fbf7b312a29
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git
web/src/lib/MessageText.svelteSvelte53 lines1.9 KB
1
<script lang="ts">
2
  import EmojiText from './EmojiText.svelte';
3
  import { parseCode } from './code';
4
  import { segmentInline } from './inline';
5
  import type { Mention } from './mentions';
6
7
  export let text: string;
8
  export let jumbo = false;
9
  export let mentions: Mention[] = [];
10
  export let self: string | null = null;
11
12
  $: segments = parseCode(text);
13
  $: pieces = segments.map((s) => (s.kind === 'text' ? segmentInline(s.text, mentions) : []));
14
  $: single = segments.length === 1 && pieces[0].length === 1;
15
</script>
16
17
{#each segments as s, i}{#if s.kind === 'inline'}<code class="inline">{s.text}</code>{:else if s.kind === 'block'}<pre class="block"><code>{s.text}</code></pre>{:else}{#each pieces[i] as p}{#if p.kind === 'mention'}<span class="mention" class:me={p.user_id === self} title={p.user_id}><EmojiText text={p.text} /></span>{:else if p.kind === 'link'}<a class="link" href={p.href} target="_blank" rel="noopener noreferrer">{p.text}</a>{:else}<EmojiText text={p.text} jumbo={jumbo && single} />{/if}{/each}{/if}{/each}
18
19
<style>
20
  code {
21
    font-family: ui-monospace, 'JetBrains Mono', 'Fira Code', Menlo, monospace;
22
    font-size: 0.9em;
23
  }
24
  .inline {
25
    padding: 1px 5px;
26
    border-radius: 4px;
27
    background: var(--bg-soft);
28
    border: 1px solid var(--border);
29
  }
30
  .block {
31
    margin: 4px 0;
32
    padding: 8px 10px;
33
    border-radius: 6px;
34
    background: var(--bg-soft);
35
    border: 1px solid var(--border);
36
    overflow-x: auto;
37
    white-space: pre;
38
    line-height: 1.4;
39
  }
40
  .mention {
41
    padding: 0 4px;
42
    border-radius: 4px;
43
    font-weight: 600;
44
    color: var(--accent);
45
    background: color-mix(in srgb, var(--accent) 18%, transparent);
46
  }
47
  .mention.me {
48
    color: var(--warn-text);
49
    background: color-mix(in srgb, var(--warn) 28%, transparent);
50
  }
51
  .link { color: var(--accent); overflow-wrap: anywhere; }
52
  .link:hover { text-decoration: underline; }
53
</style>