flotilla/src/app/components/Content.svelte

193 lines
5.5 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-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"
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, userSettingValues} 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
2025-02-11 23:37:50 +00:00
hideMediaAtDepth?: number
2025-02-04 00:37:14 +00:00
expandMode?: string
minimalQuote?: boolean
2025-02-04 00:37:14 +00:00
depth?: number
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),
2025-02-11 23:37:50 +00:00
hideMediaAtDepth = 1,
2025-02-04 00:37:14 +00:00
expandMode = "block",
minimalQuote = false,
2025-02-04 00:37:14 +00:00
depth = 0,
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-02-11 23:37:50 +00:00
if (!parsed || hideMediaAtDepth <= depth) return false
2024-11-19 22:13:00 +00:00
if (isLink(parsed) && $userSettingValues.show_media && isStartOrEnd(i)) {
2024-11-19 22:13:00 +00:00
return true
}
2025-02-11 23:37:50 +00:00
if ((isEvent(parsed) || isAddress(parsed)) && isStartOrEnd(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)
const isStartOrEnd = (i: number) => isStart(i) || isEnd(i)
2024-09-24 21:12:38 +00:00
2024-10-17 22:21:22 +00:00
const ignoreWarning = () => {
warning = null
}
2025-02-04 00:37:14 +00:00
let warning = $state(
$userSettingValues.hide_sensitive && event.tags.find(nthEq(0, "content-warning"))?.[1],
)
const shortContent = $derived(
2025-02-04 00:37:14 +00:00
showEntire
? fullContent
: truncate(fullContent, {
minLength,
maxLength,
2025-02-11 23:37:50 +00:00
mediaLength: hideMediaAtDepth <= depth ? 20 : 200,
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">
<Icon icon="danger" />
<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}
<ContentLinkInline value={parsed.value} />
{/if}
{:else if isProfile(parsed)}
2025-04-15 18:03:27 +00:00
<ContentMention value={parsed.value} {url} />
2024-10-17 22:21:22 +00:00
{:else if isEvent(parsed) || isAddress(parsed)}
2024-11-19 22:13:00 +00:00
{#if isBlock(i)}
<ContentQuote
{depth}
{url}
{hideMediaAtDepth}
value={parsed.value}
{event}
minimal={minimalQuote} />
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>