Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/midden. Archive posts and media across platforms, with tenant accounts and Telegram access
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421<script setup vapor lang="ts">import { useResizeObserver } from '@vueuse/core'import { computed, shallowRef, useTemplateRef, watch } from 'vue'import { useRouter } from 'vue-router'import { menuOpener, type MenuOpener } from '../action-menu'import { previewURL, type Collection } from '../api'import { articleOf, mediaBesideArticle, withoutArticleLink } from '../article'import { useEntryState } from '../entry-state'import { navigatesHere, openInNewTab } from '../navigation'import { pollOf } from '../poll'import { collectionTime, present, sourceStateNotice, storageSize,} from '../presentation'import { partsFrom, richContext, richText } from '../rich-text'import { collectionRoute } from '../share'import { galleryAssets, postOrigin, telegramPost } from '../telegram'import ArticleCard from './ArticleCard.vue'import ExternalCard from './ExternalCard.vue'import MediaThumbs from './MediaThumbs.vue'import PhotoTags from './PhotoTags.vue'import PostAttachments from './PostAttachments.vue'import PostPoll from './PostPoll.vue'import PostRelations from './PostRelations.vue'import AppLink from './ui/AppLink.vue'import LoadingImage from './ui/LoadingImage.vue'import LockIcon from './ui/LockIcon.vue'import PlatformIcon from './ui/PlatformIcon.vue'import RichText from './ui/RichText.vue'import TimeText from './ui/TimeText.vue'import VerifiedBadge from './ui/VerifiedBadge.vue'import UnavailableMedia from './UnavailableMedia.vue'const props = defineProps<{ enlargedMedia?: boolean collection: Collection showSensitive?: boolean sort?: string /** Display name of the account this list belongs to, for its reposts. */ repostedBy?: string /** Set while the list is selecting; the row then toggles instead of opening. */ selected?: boolean /** Search terms to mark in the preview. */ highlight?: string[] /** * The list holds one platform's only, so the author's platform says * nothing and is left out (query.ts, `listPlatform`). */ hidePlatform?: boolean /** * A post of its author's thread, drawn around the post a page opens * (PostThread): its whole text and its counts, no menu, and the line * that joins its avatar to the post above and below it. */ thread?: { above: boolean; below: boolean }}>()const emit = defineEmits<{ toggle: [id: string] /** Its ⋯, for the list's one menu (CollectionRowMenu). */ menu: [collection: Collection, opener: MenuOpener]}>()const selectable = computed(() => props.selected !== undefined)// Captured before the row's own buttons and links, so none of them opens// anything while the list is selecting.function select(event: Event) { if (!selectable.value) return event.preventDefault() event.stopPropagation() emit('toggle', props.collection.id)}const router = useRouter()/** Where the last press began, to tell a click from a drag. */let pressedAt = { x: 0, y: 0 }function press(event: PointerEvent) { pressedAt = { x: event.clientX, y: event.clientY }}/** Beyond this a press that moved selected text or dragged, not clicked. */const DRAG = 4// The media viewer is a dialog inside the row: a tap on its image zooms.const CONTROLS = 'a, button, input, textarea, select, label, video, dialog'/** * The row opens its collection from anywhere but its own links and buttons, * without being a link: its text stays selectable, and a press that moved * or left text selected opens nothing. The head is the link itself, for * the keyboard and the context menu, and a plain click is passed to it. */function open(event: MouseEvent) { if (selectable.value || event.defaultPrevented) return if (event.target instanceof Element && event.target.closest(CONTROLS)) return if ( Math.hypot(event.clientX - pressedAt.x, event.clientY - pressedAt.y) > DRAG ) return if (getSelection()?.toString().trim()) return if (navigatesHere(event)) (event.currentTarget as HTMLElement) .querySelector<HTMLAnchorElement>('a.head') ?.click() else if (event.button === 1 || event.metaKey || event.ctrlKey) openInNewTab(router, collectionRoute(props.collection.id))}const view = computed(() => present(props.collection))// Only a saved `reposted` relation to the list's own account makes this row a// repost. Whoever wrote the post is never evidence of one.const reposter = computed(() => props.collection.relation_types?.includes('reposted') ? props.repostedBy : undefined,)// A Telegram forward is the channel's own message of another's post, so// its line names where it came from where an X repost's names who reposted.const repostLine = computed(() => { if (reposter.value) return `${reposter.value} 已转发` const post = telegramPost(props.collection) if (!post?.data.forwarded_from) return '' const from = postOrigin(post).forwarded?.name return from ? `转发自 ${from}` : '转发'})// A post previews its own text and a profile its own bio, at most six lines// of it (.preview) until expanded in place.// A post that publishes an X Article shows a card of it instead of its// link, and keeps whatever else it says.const hasArticle = computed(() => !!articleOf(props.collection))const text = computed(() => { const own = (view.value.bio ?? view.value.body).trim() return hasArticle.value ? withoutArticleLink(own) : own})const media = computed(() => mediaBesideArticle(props.collection))const parts = computed(() => richText(text.value, richContext(props.collection), props.highlight),)// Kept by the page of history, so coming back shows what was unfolded.const unfolded = useEntryState(`expanded:${props.collection.id}`, false)// A thread's posts are read in full, as the post they lead to is.const expanded = computed({ get: () => !!props.thread || unfolded.value, set: (value) => (unfolded.value = value),})// A search's first match past what six lines show: the preview starts a// little before it.const LEAD_CHARS = 100const LEAD_LINES = 5const LINE_BREAK = /\n/gconst shown = computed(() => { const first = parts.value.find((part) => part.mark) if (expanded.value || !first) return parts.value const before = text.value.slice(0, first.start) return before.length > LEAD_CHARS || (before.match(LINE_BREAK)?.length ?? 0) >= LEAD_LINES ? partsFrom(parts.value, first.start - 20) : parts.value})const preview = useTemplateRef<HTMLElement>('preview')const clamped = shallowRef(false)function measure() { const el = preview.value // Out of the document (its page waits in the page cache) it has no height // to tell by: left as it was, or the rows above the reader's place would // lose their button and get it back once shown again, moving the page. if (el && !el.isConnected) return // What the clamp hides is a line at least: less is a glyph that reaches // below its line (an emoji set larger, RichText), not hidden text. clamped.value = !!el && el.scrollHeight > el.clientHeight + Number.parseFloat(getComputedStyle(el).lineHeight) / 2}useResizeObserver(preview, measure)watch(shown, measure, { flush: 'post' })const when = computed(() => props.sort ? collectionTime(props.collection, props.sort) : props.collection.saved_at || props.collection.observed_at,)const meta = computed(() => [ sourceStateNotice(props.collection), // The banner above the post already names the account that reposted it. !reposter.value && props.collection.relation_types?.includes('reposted') && '转发', props.collection.relation_types?.includes('mentions') && '提及', ] .filter(Boolean) .join(' · '),)const stats = computed(() => props.thread ? (view.value.stats?.items ?? []).map( (stat) => `${stat.value} ${stat.label}`, ) : [],)// A thread's post is read for what it says, as the post it leads to.const storage = computed(() => props.thread || props.collection.storage_bytes === undefined ? '' : storageSize(props.collection.storage_bytes),)</script>
<template> <div class="row py-2.75 bg-transparent flex flex-col w-full cursor-pointer focus-visible:outline-offset--2 hover:bg-[color-mix(in_srgb,var(--text)_3%,var(--card))]" :class="[ { selected }, thread ? [ 'thread-post relative px-inset', thread.above && 'before:(content-empty absolute left-[calc(var(--inset)+1.3125rem)] top-0 h-[0.4375rem] w-0.5 bg-separator)', thread.below && 'after:(content-empty absolute left-[calc(var(--inset)+1.3125rem)] top-[3.6875rem] bottom-0 w-0.5 bg-separator)', ] : 'px-gutter hairline-between', ]" :role="selectable ? 'checkbox' : undefined" :aria-checked="selectable ? selected : undefined" :tabindex="selectable ? 0 : undefined" @click.capture="select" @keydown.space="select" @keydown.enter="select" @pointerdown="press" @click="open" @auxclick="open" > <!-- The repost line is a row of its own across the whole item, so the avatar and the name below it stay the post's author's: the original's on X, the forwarding channel's on Telegram. --> <span v-if="repostLine" class="repost text-subtle leading-[1.3] mb-1.25 flex gap-1.5 items-center text-13" > <span class="i-lucide-repeat size-3.5" aria-hidden="true" /> <span class="min-w-0 truncate">{{ repostLine }}</span> </span> <span class="post gap-x-3 grid grid-rows-[max-content_auto] items-center" :class=" selectable ? 'grid-cols-[1.375rem_2.5rem_minmax(0,1fr)_auto]' : thread ? 'grid-cols-[2.75rem_minmax(0,1fr)_auto]' : 'grid-cols-[2.5rem_minmax(0,1fr)_auto]' " > <span v-if="selectable" class="check text-white rounded-full flex-shrink-0 grid row-[1/span_2] size-5.5 self-center place-items-center" :class=" selected ? 'bg-link' : 'shadow-[inset_0_0_0_1.5px_var(--subtle)]' " aria-hidden="true" > <span v-if="selected" class="i-lucide-bold-check size-3.5" /> </span> <AppLink :href="view.profileHref" class="avatar-link flex-shrink-0 row-[1/span_2] block self-start" :class="thread ? 'size-11' : 'size-10'" :aria-label="`查看${view.name}的主页`" ><LoadingImage v-if="view.avatar && (!view.avatar.sensitive || showSensitive)" class="avatar rounded-full bg-fill flex-shrink-0 object-cover !min-h-0" :class="thread ? 'size-11' : 'size-10'" :src="previewURL(view.avatar)" alt="" loading="lazy" /><span v-else class="avatar initials text-link font-semibold rounded-full bg-fill flex-shrink-0 grid place-items-center text-17" :class="thread ? 'size-11' : 'size-10'" aria-hidden="true" :lang="view.nameLang" >{{ view.name.slice(0, 1) }}</span ></AppLink > <!-- Not draggable, so a press that moves selects the row's text. --> <AppLink :to="collectionRoute(collection.id)" class="head text-inherit no-underline flex flex-wrap gap-x-1.5 min-w-0 items-baseline focus-visible:rounded-sm" draggable="false" > <span class="identity leading-[1.3] flex min-w-0 items-baseline" ><strong class="name font-semibold min-w-0 truncate text-16" :lang="view.nameLang" >{{ view.name }}</strong ><VerifiedBadge v-if="view.verification" :kind="view.verification" /><LockIcon v-if="view.locked" class="lock ml-0.5 self-center text-15" /><PlatformIcon v-if="!view.handle && !hidePlatform" :type="view.platform" class="text-subtle ml-1 flex-none self-center text-13" /></span> <!-- The handle gives up room first: the name identifies the account, and the time and the size are short. --> <span class="byline text-subtle leading-[1.35] flex min-w-0 whitespace-nowrap overflow-hidden text-13 [&>*+*]:before:(mx-1.25 content-['·'])" ><span v-if="view.handle" class="handle flex min-w-0 items-center" ><span class="min-w-0 truncate">@{{ view.handle }}</span ><PlatformIcon v-if="!hidePlatform" :type="view.platform" class="ml-[0.25em] flex-none" /></span ><span class="when flex-shrink-0"><TimeText :at="when" /></span ><span v-if="storage" class="storage flex-shrink-0">{{ storage }}</span></span > </AppLink> <!-- Beside both lines, centred on them; its hit area overhangs them without making a single line taller. --> <button v-if="!selectable && !thread" type="button" class="more icon-button size-8 -my-1.5 -mr-1.5 focus-visible:outline-offset-0" aria-label="更多操作" title="更多操作" aria-haspopup="menu" @click="emit('menu', collection, menuOpener($event))" > <span class="i-lucide-ellipsis size-4.5" aria-hidden="true" /> </button> <span class="content col-[-3/-1] row-start-2 min-w-0 self-start not-empty:mt-1" > <PostRelations :collection="collection" :show-sensitive="showSensitive" compact replied /> <!-- Six lines hold a full-length tweet on a phone (about 280 Latin or 120 Chinese characters) and keep a long post from filling the screen. --> <span v-if="text" ref="preview" class="preview text-text leading-[1.45] whitespace-pre-wrap wrap-anywhere text-pretty text-15" :class="expanded ? 'block' : 'line-clamp-6'" :lang="view.lang" ><RichText :parts="shown" /></span> <button v-if="clamped && !expanded" type="button" class="text-link leading-[1.45] py-0.5 block text-15" @click="expanded = true" > 展开 </button> <span v-if="meta" class="meta text-subtle mt-0.75 block truncate text-13" >{{ meta }}</span > <ArticleCard v-if="hasArticle" :collection="collection" :show-sensitive="showSensitive" :highlight="highlight" :lang="view.lang" /> <!-- The post's own media comes before the post it quotes, as on X. --> <MediaThumbs :size="enlargedMedia ? 'medium' : 'small'" :desktop-size="enlargedMedia ? 'large' : 'medium'" :collection-id="collection.id" :collection="collection" :assets="galleryAssets(media)" :show-sensitive="showSensitive" /> <UnavailableMedia :assets="media" /> <ExternalCard :collection="collection" compact :show-sensitive="showSensitive" /> <PostAttachments :collection="collection" compact /> <PhotoTags :collection="collection" /> <PostPoll :poll="pollOf(collection)" compact /> <PostRelations :collection="collection" :show-sensitive="showSensitive" compact /> <span v-if="stats.length" class="stats text-subtle mt-1.5 flex flex-wrap tabular-nums text-13 [&>*+*]:before:(mx-1.25 content-['·'])" ><span v-for="stat in stats" :key="stat" class="stat whitespace-nowrap" >{{ stat }}</span ></span > </span> </span> </div></template>
<style scoped>/* Pressed where a click opens the post: its head, text or empty space. Its links, buttons, media, quoted post and viewer answer for themselves. A selected row shows its check, not a background. */.row:active:not( :has(:is(a:not(.head), button, video, dialog, .quote, .thumbs-group):active) ) { background: var(--fill);}.preview mark { border-radius: 0.1875rem; background: color-mix(in srgb, var(--link) 22%, transparent); color: inherit;}</style>