atagen

malice

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