flotilla/src/app/components/Content.svelte

164 lines
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,
render as renderParsed,
isText,
isTopic,
isCode,
isCashu,
isInvoice,
isLink,
isProfile,
isEvent,
isEllipsis,
isAddress,
isNewline,
} from "@welshman/content"
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"
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"
2024-10-24 18:02:16 +00:00
import {entityLink, userSettingValues} from "@app/state"
2024-09-24 21:12:38 +00:00
export let event
export let minLength = 500
export let maxLength = 700
export let showEntire = false
2024-10-01 21:55:33 +00:00
export let hideMedia = false
2024-10-29 21:02:51 +00:00
export let expandMode = "block"
2024-09-24 21:12:38 +00:00
export let depth = 0
const fullContent = parse(event)
const expand = () => {
showEntire = true
}
2024-11-19 22:13:00 +00:00
const isBlock = (i: number) => {
const parsed = fullContent[i]
if (!parsed || hideMedia) 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
}
if ((isEvent(parsed) || isAddress(parsed)) && isStartOrEnd(i) && depth < 2) {
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
}
2024-10-17 22:46:26 +00:00
let warning =
2024-10-24 18:02:16 +00:00
$userSettingValues.hide_sensitive && event.tags.find(nthEq(0, "content-warning"))?.[1]
2024-10-17 22:21:22 +00:00
2024-09-24 21:12:38 +00:00
$: shortContent = showEntire
? fullContent
2024-10-01 21:55:33 +00:00
: truncate(fullContent, {
minLength,
maxLength,
mediaLength: hideMedia ? 20 : 200,
})
2024-09-24 21:12:38 +00:00
2024-10-29 21:02:51 +00:00
$: hasEllipsis = shortContent.find(isEllipsis)
$: expandInline = hasEllipsis && expandMode === "inline"
$: expandBlock = 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" on:click={ignoreWarning}>Show anyway</Button>
</p>
</div>
{:else}
<div
2024-10-31 17:21: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}
{#if isNewline(parsed)}
2024-11-19 22:13:00 +00:00
<ContentNewline value={parsed.value.slice(isBlock(i - 1) ? 1 : 0)} />
2024-10-17 22:21:22 +00:00
{:else if isTopic(parsed)}
<ContentTopic value={parsed.value} />
{: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)}
2024-10-17 22:21:22 +00:00
<ContentLinkBlock value={parsed.value} />
{:else}
<ContentLinkInline value={parsed.value} />
{/if}
{:else if isProfile(parsed)}
<ContentMention value={parsed.value} />
{:else if isEvent(parsed) || isAddress(parsed)}
2024-11-19 22:13:00 +00:00
{#if isBlock(i)}
2024-11-18 22:39:43 +00:00
<ContentQuote value={parsed.value} {depth} {event}>
2024-10-17 22:21:22 +00:00
<div slot="note-content" let:event>
<svelte:self {hideMedia} {event} depth={depth + 1} />
</div>
</ContentQuote>
{: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}
{@html renderParsed(parsed)}
2024-10-31 19:24:40 +00:00
<button type="button" class="text-sm underline"> Read more </button>
{:else}
2024-10-17 22:21:22 +00:00
{@html renderParsed(parsed)}
{/if}
2024-10-17 22:21:22 +00:00
{/each}
</div>
2024-10-29 21:02:51 +00:00
{#if expandBlock}
2024-10-24 18:55:15 +00:00
<div class="relative z-feature -mt-6 flex justify-center bg-gradient-to-t from-base-100 py-2">
2024-10-17 22:21:22 +00:00
<button type="button" class="btn" on:click|stopPropagation|preventDefault={expand}>
See more
</button>
</div>
{/if}
{/if}
</div>