| 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 | export let nameOf: (userId: string) => string | null = () => null; |
| 12 | |
| 13 | $: segments = parseCode(text); |
| 14 | $: pieces = segments.map((s) => (s.kind === 'text' ? segmentInline(s.text, mentions) : [])); |
| 15 | $: single = segments.length === 1 && pieces[0].length === 1; |
| 16 | </script> |
| 17 | |
| 18 | {#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">{p.text}</a>{:else}<EmojiText text={p.text} jumbo={jumbo && single} />{/if}{/each}{/if}{/each} |
| 19 | |
| 20 | <style> |
| 21 | code { |
| 22 | font-family: ui-monospace, 'JetBrains Mono', 'Fira Code', Menlo, monospace; |
| 23 | font-size: 0.9em; |
| 24 | } |
| 25 | .inline { |
| 26 | padding: 1px 5px; |
| 27 | border-radius: 4px; |
| 28 | background: var(--bg-soft); |
| 29 | border: 1px solid var(--border); |
| 30 | } |
| 31 | .block { |
| 32 | margin: 4px 0; |
| 33 | padding: 8px 10px; |
| 34 | border-radius: 6px; |
| 35 | background: var(--bg-soft); |
| 36 | border: 1px solid var(--border); |
| 37 | overflow-x: auto; |
| 38 | white-space: pre; |
| 39 | line-height: 1.4; |
| 40 | } |
| 41 | .mention { |
| 42 | padding: 0 4px; |
| 43 | border-radius: 4px; |
| 44 | font-weight: 600; |
| 45 | color: var(--accent); |
| 46 | background: color-mix(in srgb, var(--accent) 18%, transparent); |
| 47 | } |
| 48 | .mention.me { |
| 49 | color: var(--warn-text); |
| 50 | background: color-mix(in srgb, var(--warn) 28%, transparent); |
| 51 | } |
| 52 | .link { color: var(--accent); overflow-wrap: anywhere; } |
| 53 | .link:hover { text-decoration: underline; } |
| 54 | </style> |