atagen

malice

public
Star0
Watch0
ba90983cec2eb8367c17aef8b5f937543c803672
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git
web/src/lib/MessageText.svelteSvelte83 lines3.6 KB
1
<script lang="ts">
2
  import EmojiText from './EmojiText.svelte';
3
  import RichNodes from './RichNodes.svelte';
4
  import { parseCode } from './code';
5
  import { segmentInline } from './inline';
6
  import type { Mention } from './mentions';
7
  import { parseRich } from './rich';
8
9
  export let text: string;
10
  export let html: string | null = null;
11
  export let jumbo = false;
12
  export let mentions: Mention[] = [];
13
  export let self: string | null = null;
14
  export let nameOf: (userId: string) => string | null = () => null;
15
16
  $: rich = html ? parseRich(html) : null;
17
  $: segments = parseCode(text);
18
  $: pieces = segments.map((s) => (s.kind === 'text' ? segmentInline(s.text, mentions) : []));
19
  $: single = segments.length === 1 && pieces[0].length === 1;
20
</script>
21
22
{#if rich}<div class="rich"><RichNodes nodes={rich} {mentions} {self} {nameOf} /></div>{:else}{#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 === p.user_id ? (nameOf(p.user_id) ?? p.text) : p.text} /></span>{:else if p.kind === 'link'}<a class="link" href={p.href} target="_blank" rel="noopener noreferrer"><EmojiText text={p.text} /></a>{:else}<EmojiText text={p.text} jumbo={jumbo && single} />{/if}{/each}{/if}{/each}{/if}
23
24
<style>
25
  code, .rich :global(code) {
26
    font-family: ui-monospace, 'JetBrains Mono', 'Fira Code', Menlo, monospace;
27
    font-size: 0.9em;
28
  }
29
  .inline, .rich :global(:not(pre) > code) {
30
    padding: 1px 5px;
31
    border-radius: var(--r-sm);
32
    background: var(--bg-soft);
33
    border: 1px solid var(--border);
34
  }
35
  .block, .rich :global(pre) {
36
    margin: 4px 0;
37
    padding: 8px 10px;
38
    border-radius: var(--r-sm);
39
    background: var(--bg-soft);
40
    border: 1px solid var(--border);
41
    overflow-x: auto;
42
    white-space: pre;
43
    line-height: 1.4;
44
  }
45
  .mention, .rich :global(.mention) {
46
    padding: 0 4px;
47
    border-radius: var(--r-sm);
48
    font-weight: 600;
49
    color: var(--accent);
50
    background: color-mix(in srgb, var(--accent) 18%, transparent);
51
  }
52
  .mention.me, .rich :global(.mention.me) {
53
    color: var(--warn-text);
54
    background: color-mix(in srgb, var(--warn) 28%, transparent);
55
  }
56
  .link, .rich :global(.link) { color: var(--accent); overflow-wrap: anywhere; }
57
  .link:hover, .rich :global(.link:hover) { text-decoration: underline; }
58
  .rich { white-space: normal; }
59
  .rich :global(:is(p, ul, ol, blockquote, h1, h2, h3, h4, h5, h6, table, details)) { margin: 4px 0; }
60
  .rich :global(:first-child) { margin-top: 0; }
61
  .rich :global(:last-child) { margin-bottom: 0; }
62
  .rich :global(:is(ul, ol)) { padding-left: 22px; }
63
  .rich :global(blockquote) {
64
    padding: 2px 10px;
65
    border-left: 3px solid var(--border);
66
    color: var(--muted);
67
  }
68
  .rich :global(:is(h1, h2, h3, h4, h5, h6)) { line-height: 1.3; }
69
  .rich :global(h1) { font-size: 1.4em; }
70
  .rich :global(h2) { font-size: 1.25em; }
71
  .rich :global(:is(h3, h4, h5, h6)) { font-size: 1.1em; }
72
  .rich :global(hr) { border: none; border-top: 1px solid var(--border); margin: 8px 0; }
73
  .rich :global(table) { border-collapse: collapse; }
74
  .rich :global(:is(th, td)) { border: 1px solid var(--border); padding: 3px 8px; text-align: left; }
75
  .rich :global(.spoiler) {
76
    border-radius: var(--r-sm);
77
    background: var(--text);
78
    color: transparent;
79
    cursor: pointer;
80
  }
81
  .rich :global(.spoiler:is(:hover, :focus)) { background: var(--bg-soft); color: inherit; }
82
  .rich :global(.emote) { height: 1.6em; width: auto; vertical-align: middle; }
83
</style>