flotilla/src/app/components/Content.svelte

207 lines
5.7 KiB
Svelte
Raw Normal View History

2024-09-24 21:12:38 +00:00
<script lang="ts">
import {fromNostrURI} from "@welshman/util"
2024-10-17 22:21:22 +00:00
import {nthEq} from "@welshman/lib"
2024-09-24 21:12:38 +00:00
import {
parse,
truncate,
2024-12-05 23:32:27 +00:00
renderAsHtml,
2024-09-24 21:12:38 +00:00
isText,
2025-05-12 22:10:24 +00:00
isEmoji,
2024-09-24 21:12:38 +00:00
isTopic,
isCode,
isCashu,
isInvoice,
isLink,
isProfile,
isEvent,
isEllipsis,
isAddress,
isNewline,
} from "@welshman/content"
2025-10-06 18:23:19 +00:00
import type {Parsed} from "@welshman/content"
2025-02-04 03:28:29 +00:00
import {preventDefault, stopPropagation} from "@lib/html"
2024-10-08 18:39:16 +00:00
import Link from "@lib/components/Link.svelte"
2025-09-10 22:26:39 +00:00
import Danger from "@assets/icons/danger-triangle.svg?dataurl"
2024-10-17 22:21:22 +00:00
import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte"
2024-10-08 18:39:16 +00:00
import ContentToken from "@app/components/ContentToken.svelte"
2025-05-12 22:10:24 +00:00
import ContentEmoji from "@app/components/ContentEmoji.svelte"
2024-10-08 18:39:16 +00:00
import ContentCode from "@app/components/ContentCode.svelte"
import ContentLinkInline from "@app/components/ContentLinkInline.svelte"
import ContentLinkBlock from "@app/components/ContentLinkBlock.svelte"
import ContentNewline from "@app/components/ContentNewline.svelte"
import ContentQuote from "@app/components/ContentQuote.svelte"
import ContentTopic from "@app/components/ContentTopic.svelte"
import ContentMention from "@app/components/ContentMention.svelte"
import {entityLink, userSettingsValues} from "@app/core/state"
2024-09-24 21:12:38 +00:00
2025-02-04 00:37:14 +00:00
interface Props {
event: any
minLength?: number
maxLength?: number
showEntire?: boolean
expandMode?: string
2025-10-06 18:23:19 +00:00
trimParent?: boolean
2025-04-15 18:03:27 +00:00
url?: string
2025-02-04 00:37:14 +00:00
}
let {
event,
minLength = 500,
maxLength = 700,
showEntire = $bindable(false),
expandMode = "block",
2025-10-06 18:23:19 +00:00
trimParent = false,
2025-04-15 18:03:27 +00:00
url,
2025-02-04 00:37:14 +00:00
}: Props = $props()
2024-09-24 21:12:38 +00:00
const fullContent = parse(event)
const expand = () => {
showEntire = true
}
2024-11-19 22:13:00 +00:00
const isBlock = (i: number) => {
const parsed = fullContent[i]
2025-10-06 18:23:19 +00:00
if (!parsed) return false
2024-11-19 22:13:00 +00:00
2025-09-25 17:12:58 +00:00
if (isLink(parsed) && $userSettingsValues.show_media && isStartAndEnd(i)) {
2024-11-19 22:13:00 +00:00
return true
}
2025-10-06 18:23:19 +00:00
if (isQuote(parsed) && isStartAndEnd(i)) {
2024-11-19 22:13:00 +00:00
return true
}
return false
}
2024-09-24 21:12:38 +00:00
const isBoundary = (i: number) => {
const parsed = fullContent[i]
if (!parsed || isNewline(parsed)) return true
if (isText(parsed)) return Boolean(parsed.value.match(/^\s+$/))
2024-09-24 21:12:38 +00:00
return false
}
const isStart = (i: number) => isBoundary(i - 1)
2024-10-10 17:00:01 +00:00
const isEnd = (i: number) => isBoundary(i + 1)
const isStartAndEnd = (i: number) => isStart(i) && isEnd(i)
2025-10-06 18:23:19 +00:00
const isQuote = (p: Parsed) => isEvent(p) || isAddress(p)
2024-10-17 22:21:22 +00:00
const ignoreWarning = () => {
warning = null
}
2025-02-04 00:37:14 +00:00
let warning = $state(
$userSettingsValues.hide_sensitive && event.tags.find(nthEq(0, "content-warning"))?.[1],
2025-02-04 00:37:14 +00:00
)
2025-10-06 18:23:19 +00:00
const dropWhile = <T,>(f: (x: T) => boolean, xs: Iterable<T>) => {
const result: T[] = []
for (const x of xs) {
if (result.length === 0 && f(x)) {
continue
}
result.push(x)
}
return result
}
const shortContent = $derived.by(() => {
let result = fullContent
if (trimParent && result.length > 0 && isQuote(result[0])) {
result = dropWhile(p => isQuote(p) || isNewline(p), result)
}
if (!showEntire) {
result = truncate(result, {
minLength,
maxLength,
mediaLength: 200,
})
}
return result
})
2025-02-04 00:37:14 +00:00
const hasEllipsis = $derived(shortContent.some(isEllipsis))
const expandInline = $derived(hasEllipsis && expandMode === "inline")
const expandBlock = $derived(hasEllipsis && expandMode === "block")
2024-09-24 21:12:38 +00:00
</script>
<div class="relative">
2024-10-17 22:21:22 +00:00
{#if warning}
<div class="card2 card2-sm bg-alt row-2">
2025-09-10 22:26:39 +00:00
<Icon icon={Danger} />
2024-10-17 22:21:22 +00:00
<p>
This note has been flagged by the author as "{warning}".<br />
<Button class="link" onclick={ignoreWarning}>Show anyway</Button>
2024-10-17 22:21:22 +00:00
</p>
</div>
{:else}
<div
2025-06-05 17:16:05 +00:00
class="overflow-hidden text-ellipsis break-words"
2024-10-29 21:02:51 +00:00
style={expandBlock ? "mask-image: linear-gradient(0deg, transparent 0px, black 100px)" : ""}>
2024-10-17 22:21:22 +00:00
{#each shortContent as parsed, i}
2025-02-06 19:33:01 +00:00
{#if isNewline(parsed) && !isBlock(i - 1)}
<ContentNewline value={parsed.value} />
2024-10-17 22:21:22 +00:00
{:else if isTopic(parsed)}
<ContentTopic value={parsed.value} />
2025-05-12 22:10:24 +00:00
{:else if isEmoji(parsed)}
<ContentEmoji value={parsed.value} />
2024-10-17 22:21:22 +00:00
{:else if isCode(parsed)}
2024-11-19 16:37:09 +00:00
<ContentCode
value={parsed.value}
isBlock={isStartAndEnd(i) || parsed.value.includes("\n")} />
2024-10-17 22:21:22 +00:00
{:else if isCashu(parsed) || isInvoice(parsed)}
<ContentToken value={parsed.value} />
{:else if isLink(parsed)}
2024-11-19 22:13:00 +00:00
{#if isBlock(i)}
2025-04-28 22:46:48 +00:00
<ContentLinkBlock value={parsed.value} {event} />
2024-10-17 22:21:22 +00:00
{:else}
2026-01-12 18:12:47 +00:00
<ContentLinkInline value={parsed.value} {event} />
2024-10-17 22:21:22 +00:00
{/if}
{:else if isProfile(parsed)}
2025-04-15 18:03:27 +00:00
<ContentMention value={parsed.value} {url} />
2025-10-06 18:23:19 +00:00
{:else if isQuote(parsed)}
2024-11-19 22:13:00 +00:00
{#if isBlock(i)}
2025-10-06 18:23:19 +00:00
<ContentQuote {url} value={parsed.value} {event} />
2024-10-17 22:21:22 +00:00
{:else}
<Link
external
class="overflow-hidden text-ellipsis whitespace-nowrap underline"
href={entityLink(parsed.raw)}>
{fromNostrURI(parsed.raw).slice(0, 16) + "…"}
</Link>
{/if}
2024-10-29 21:02:51 +00:00
{:else if isEllipsis(parsed) && expandInline}
2024-12-05 23:32:27 +00:00
{@html renderAsHtml(parsed)}
2024-10-31 19:24:40 +00:00
<button type="button" class="text-sm underline"> Read more </button>
{:else}
2024-12-05 23:32:27 +00:00
{@html renderAsHtml(parsed)}
{/if}
2024-10-17 22:21:22 +00:00
{/each}
</div>
2024-10-29 21:02:51 +00:00
{#if expandBlock}
2024-11-20 23:56:24 +00:00
<div class="relative z-feature -mt-6 flex justify-center py-2">
<button
type="button"
class="btn btn-neutral"
2025-02-04 00:37:14 +00:00
onclick={stopPropagation(preventDefault(expand))}>
2024-10-17 22:21:22 +00:00
See more
</button>
</div>
{/if}
{/if}
</div>