impr: restructure src/lib
This commit is contained in:
376 files changed
+1000
-1033
No files matched your search
@@ -0,0 +1,323 @@
|
||||
<script lang="ts">
|
||||
import { page } from '$app/state'
|
||||
import { profile } from '$lib/app/auth.svelte.js'
|
||||
import UserLink from '$comp/lemmy/user/UserLink.svelte'
|
||||
import Markdown from '$lib/app/markdown/Markdown.svelte'
|
||||
import { publishedToDate } from '$comp/util/date.js'
|
||||
import RelativeDate, {
|
||||
formatRelativeDate,
|
||||
} from '$comp/util/RelativeDate.svelte'
|
||||
import { t } from '$lib/app/i18n'
|
||||
import { getClient } from '$lib/api/client.svelte'
|
||||
import { errorMessage } from '$lib/lemmy/error'
|
||||
import { settings } from '$lib/settings.svelte'
|
||||
import { Button, Modal, toast } from 'mono-svelte'
|
||||
import {
|
||||
Bookmark,
|
||||
Icon,
|
||||
Microphone,
|
||||
Minus,
|
||||
Pencil,
|
||||
Plus,
|
||||
ShieldCheck,
|
||||
Trash,
|
||||
} from 'svelte-hero-icons'
|
||||
import { expoOut } from 'svelte/easing'
|
||||
import type { ClassValue } from 'svelte/elements'
|
||||
import { slide } from 'svelte/transition'
|
||||
import type { CommentNodeI } from './comments.svelte'
|
||||
import CommentForm from './CommentForm.svelte'
|
||||
import CommentActions from './CommentActions.svelte'
|
||||
|
||||
interface Props {
|
||||
node: CommentNodeI
|
||||
postId: number
|
||||
op?: boolean
|
||||
actions?: boolean
|
||||
meta?: boolean
|
||||
open?: boolean
|
||||
replying?: boolean
|
||||
contentClass?: ClassValue
|
||||
class?: ClassValue
|
||||
metaSuffix?: import('svelte').Snippet
|
||||
children?: import('svelte').Snippet
|
||||
}
|
||||
|
||||
let {
|
||||
node = $bindable(),
|
||||
postId,
|
||||
op = false,
|
||||
actions = true,
|
||||
meta = true,
|
||||
replying = $bindable(false),
|
||||
open = $bindable(true),
|
||||
contentClass = '',
|
||||
class: clazz = '',
|
||||
metaSuffix,
|
||||
children,
|
||||
}: Props = $props()
|
||||
|
||||
let editing = $state(false)
|
||||
let newComment = $state(node.comment_view.comment.content)
|
||||
let editingLoad = $state(false)
|
||||
|
||||
async function save(type: 'reply' | 'edit') {
|
||||
if (!profile.current?.jwt || newComment.length <= 0) return
|
||||
|
||||
editingLoad = true
|
||||
|
||||
try {
|
||||
if (type == 'edit') {
|
||||
await getClient().editComment({
|
||||
comment_id: node.comment_view.comment.id,
|
||||
content: newComment,
|
||||
})
|
||||
|
||||
node.comment_view.comment.content = newComment
|
||||
|
||||
editing = false
|
||||
}
|
||||
} catch (err) {
|
||||
toast({
|
||||
content: errorMessage(err as string),
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
|
||||
editingLoad = false
|
||||
}
|
||||
</script>
|
||||
|
||||
{#if editing}
|
||||
<Modal bind:open={editing}>
|
||||
{#snippet customTitle()}
|
||||
<div>{$t('form.edit')}</div>
|
||||
{/snippet}
|
||||
<form
|
||||
onsubmit={(e) => {
|
||||
e.preventDefault()
|
||||
save(replying ? 'reply' : 'edit')
|
||||
}}
|
||||
class="contents"
|
||||
>
|
||||
<CommentForm
|
||||
bind:value={newComment}
|
||||
postId={node.comment_view.comment.id}
|
||||
actions={false}
|
||||
preview={true}
|
||||
/>
|
||||
<Button
|
||||
submit
|
||||
color="primary"
|
||||
size="lg"
|
||||
loading={editingLoad}
|
||||
disabled={editingLoad}
|
||||
class="w-full"
|
||||
>
|
||||
{$t('form.submit')}
|
||||
</Button>
|
||||
</form>
|
||||
</Modal>
|
||||
{/if}
|
||||
|
||||
<li
|
||||
class={['py-3 relative', clazz]}
|
||||
id={node.comment_view.comment.id.toString()}
|
||||
>
|
||||
{#if meta}
|
||||
<label
|
||||
for="comment-expand-{node.comment_view.comment.id}"
|
||||
class="flex flex-row cursor-pointer gap-2 items-center group text-sm flex-wrap w-full z-0 group relative"
|
||||
>
|
||||
<div
|
||||
class={[
|
||||
'absolute -inset-0.5 right-1 group-hover:right-0 group-hover:-inset-1.5 opacity-0 group-hover:opacity-100 transition-all',
|
||||
'bg-slate-100 dark:bg-zinc-900 -z-10 rounded-full inline-flex items-center justify-end',
|
||||
]}
|
||||
>
|
||||
{#if node.comment_view.counts.child_count > 0}
|
||||
{@const children = node.comment_view.counts.child_count}
|
||||
<div
|
||||
aria-label={$t('aria.comments.children', { childCount: children })}
|
||||
class="font-medium"
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
{/if}
|
||||
<div
|
||||
class={[
|
||||
!open && 'rotate-90',
|
||||
'transition-all duration-500 ease-out my-auto h-full w-8 grid place-items-center',
|
||||
]}
|
||||
>
|
||||
<Icon src={open ? Minus : Plus} size="16" micro />
|
||||
</div>
|
||||
</div>
|
||||
{@render metaSuffix?.()}
|
||||
<span class:font-bold={op} class="flex flex-row gap-1 items-center">
|
||||
<UserLink
|
||||
inComment
|
||||
avatarSize={20}
|
||||
avatar
|
||||
user={node.comment_view.creator}
|
||||
>
|
||||
{#snippet extraBadges()}
|
||||
{#if node.comment_view.creator_is_moderator}
|
||||
<Icon
|
||||
src={ShieldCheck}
|
||||
size="14"
|
||||
micro
|
||||
class="text-green-500"
|
||||
aria-label={$t('class.moderator')}
|
||||
/>
|
||||
{/if}
|
||||
{#if node.comment_view.creator_is_admin}
|
||||
<Icon
|
||||
src={ShieldCheck}
|
||||
size="16"
|
||||
micro
|
||||
class="text-red-500"
|
||||
aria-label={$t('class.admin')}
|
||||
/>
|
||||
{/if}
|
||||
{/snippet}
|
||||
</UserLink>
|
||||
{#if op}
|
||||
<Icon mini size="16" src={Microphone} class="text-sky-600" />
|
||||
{/if}
|
||||
</span>
|
||||
<RelativeDate
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
date={publishedToDate(node.comment_view.comment.published)}
|
||||
/>
|
||||
<span class="text-slate-600 dark:text-zinc-400 flex flex-row gap-2 ml-1">
|
||||
{#if node.comment_view.comment.updated}{@const edited = $t(
|
||||
'post.meta.lastEdited',
|
||||
{
|
||||
default: formatRelativeDate(
|
||||
publishedToDate(node.comment_view.comment.updated),
|
||||
{
|
||||
style: 'long',
|
||||
},
|
||||
),
|
||||
},
|
||||
)}
|
||||
<div title={edited}>
|
||||
<Icon src={Pencil} micro size="14" />
|
||||
</div>
|
||||
{/if}
|
||||
{#if node.comment_view.comment.deleted}
|
||||
<Icon
|
||||
src={Trash}
|
||||
solid
|
||||
size="12"
|
||||
aria-label={$t('post.badges.deleted')}
|
||||
class="text-red-600 dark:text-red-500"
|
||||
/>
|
||||
{/if}
|
||||
{#if node.comment_view.comment.removed}
|
||||
<Icon
|
||||
src={Trash}
|
||||
solid
|
||||
size="12"
|
||||
aria-label={$t('post.badges.removed')}
|
||||
class="text-green-600 dark:text-green-500"
|
||||
/>
|
||||
{/if}
|
||||
{#if node.comment_view.saved}
|
||||
<Icon
|
||||
src={Bookmark}
|
||||
solid
|
||||
size="12"
|
||||
aria-label={$t('post.badges.saved')}
|
||||
class="text-yellow-600 dark:text-yellow-500"
|
||||
/>
|
||||
{/if}
|
||||
</span>
|
||||
{#if settings.debugInfo}
|
||||
<span class="text-slate-600 dark:text-zinc-400 font-mono ml-auto">
|
||||
#{node.comment_view.comment.id}
|
||||
</span>
|
||||
{/if}
|
||||
</label>
|
||||
{/if}
|
||||
<input
|
||||
class="hidden comment-expand"
|
||||
type="checkbox"
|
||||
id="comment-expand-{node.comment_view.comment.id}"
|
||||
bind:checked={open}
|
||||
/>
|
||||
<div class={['expand max-w-full', contentClass]}>
|
||||
<div id="comment-content">
|
||||
<div
|
||||
class={[
|
||||
'flex flex-col whitespace-pre-wrap max-w-full gap-1 mt-1 relative w-full',
|
||||
]}
|
||||
>
|
||||
<Markdown
|
||||
source={node.comment_view.comment.content}
|
||||
noStyle
|
||||
class={[
|
||||
'text-[15px] sm:text-base text-slate-700 dark:text-zinc-300 *:leading-[1.6] break-words space-y-3',
|
||||
node.comment_view.comment.distinguished
|
||||
? 'bg-green-400/10 p-2 rounded-xl max-w-max font-medium'
|
||||
: page.url.hash.slice(1) ==
|
||||
node.comment_view.comment.id.toString() &&
|
||||
'bg-slate-100 dark:bg-zinc-900 p-2 rounded-xl max-w-max',
|
||||
]}
|
||||
/>
|
||||
{#if actions}
|
||||
<CommentActions
|
||||
comment={node.comment_view}
|
||||
bind:replying
|
||||
onedit={() => (editing = true)}
|
||||
disabled={node.comment_view.banned_from_community ||
|
||||
node.comment_view.post.locked}
|
||||
/>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#if replying}
|
||||
<div transition:slide={{ duration: 600, easing: expoOut }}>
|
||||
<CommentForm
|
||||
label={$t('comment.reply')}
|
||||
{postId}
|
||||
parentId={node.comment_view.comment.id}
|
||||
oncomment={(e) => {
|
||||
node.children = [
|
||||
{
|
||||
children: [],
|
||||
comment_view: e.comment_view,
|
||||
depth: node.depth + 1,
|
||||
expanded: true,
|
||||
},
|
||||
...node.children,
|
||||
]
|
||||
replying = false
|
||||
}}
|
||||
oncancel={() => (replying = false)}
|
||||
/>
|
||||
</div>
|
||||
{/if}
|
||||
{@render children?.()}
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
|
||||
<style>
|
||||
.expand {
|
||||
display: grid;
|
||||
grid-template-rows: 0fr;
|
||||
grid-template-columns: 100%;
|
||||
overflow: hidden;
|
||||
transition: grid-template-rows 0.5s cubic-bezier(0.19, 1, 0.22, 1);
|
||||
}
|
||||
|
||||
.comment-expand:checked + .expand {
|
||||
grid-template-rows: 1fr;
|
||||
}
|
||||
|
||||
.expand > * {
|
||||
min-height: 0;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,131 @@
|
||||
<script lang="ts">
|
||||
import { profile } from '$lib/app/auth.svelte.js'
|
||||
import type { CommentView } from '$lib/api/types'
|
||||
import CommentModerationMenu from '$comp/lemmy/moderation/CommentModerationMenu.svelte'
|
||||
import { amMod, isAdmin, report } from '$comp/lemmy/moderation/moderation.js'
|
||||
import { t } from '$lib/app/i18n'
|
||||
import { deleteItem, save } from '$lib/lemmy/contentview.js'
|
||||
import { settings } from '$lib/settings.svelte'
|
||||
import { Button, Menu, MenuButton } from 'mono-svelte'
|
||||
import {
|
||||
Bookmark,
|
||||
BookmarkSlash,
|
||||
ChatBubbleOvalLeft,
|
||||
EllipsisHorizontal,
|
||||
Flag,
|
||||
PencilSquare,
|
||||
Share,
|
||||
Trash,
|
||||
} from 'svelte-hero-icons'
|
||||
import CommentVote from './CommentVote.svelte'
|
||||
|
||||
interface Props {
|
||||
comment: CommentView
|
||||
replying?: boolean
|
||||
disabled?: boolean
|
||||
onedit?: (comment: CommentView) => void
|
||||
}
|
||||
|
||||
let {
|
||||
comment = $bindable(),
|
||||
replying = $bindable(false),
|
||||
disabled = false,
|
||||
onedit,
|
||||
}: Props = $props()
|
||||
</script>
|
||||
|
||||
<div
|
||||
class={[
|
||||
'flex flex-row items-center gap-0.5 h-7 w-full',
|
||||
settings.posts.reverseActions && 'flex-row-reverse',
|
||||
]}
|
||||
>
|
||||
<CommentVote
|
||||
upvotes={comment.counts.upvotes}
|
||||
downvotes={comment.counts.downvotes}
|
||||
vote={comment.my_vote}
|
||||
comment={comment.comment}
|
||||
/>
|
||||
<Button
|
||||
color="tertiary"
|
||||
rounding="pill"
|
||||
size="sm"
|
||||
class="text-slate-500 dark:text-zinc-400 gap-1!"
|
||||
onclick={() => (replying = !replying)}
|
||||
disabled={comment.post.locked || disabled || !profile.current.jwt}
|
||||
icon={ChatBubbleOvalLeft}
|
||||
>
|
||||
{$t('comment.reply')}
|
||||
</Button>
|
||||
{#if profile.current?.user && (amMod(profile.current?.user, comment.community) || isAdmin(profile.current.user))}
|
||||
<CommentModerationMenu bind:item={comment} />
|
||||
{/if}
|
||||
<Menu placement="bottom">
|
||||
{#snippet target(attachment)}
|
||||
<Button
|
||||
{@attach attachment}
|
||||
title={$t('comment.actions.label')}
|
||||
color="tertiary"
|
||||
rounding="pill"
|
||||
size="square-md"
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
icon={EllipsisHorizontal}
|
||||
></Button>
|
||||
{/snippet}
|
||||
<MenuButton
|
||||
onclick={() => {
|
||||
if (navigator.share)
|
||||
navigator.share?.({
|
||||
url: comment.comment.ap_id,
|
||||
})
|
||||
else navigator.clipboard.writeText(comment.comment.ap_id)
|
||||
}}
|
||||
icon={Share}
|
||||
>
|
||||
{$t('post.actions.more.share')}
|
||||
</MenuButton>
|
||||
{#if profile.current?.jwt}
|
||||
{#if comment.creator.id == profile.current.user?.local_user_view.person.id}
|
||||
<MenuButton onclick={() => onedit?.(comment)} icon={PencilSquare}>
|
||||
{$t('post.actions.more.edit')}
|
||||
</MenuButton>
|
||||
{/if}
|
||||
<MenuButton
|
||||
onclick={async () => {
|
||||
if (profile.current?.jwt)
|
||||
comment.saved = await save(comment, !comment.saved)
|
||||
}}
|
||||
icon={comment.saved ? BookmarkSlash : Bookmark}
|
||||
>
|
||||
{comment.saved ? $t('post.actions.unsave') : $t('post.actions.save')}
|
||||
</MenuButton>
|
||||
{#if profile.current?.user && profile.current.jwt && profile.current.user.local_user_view.person.id == comment.creator.id}
|
||||
<MenuButton
|
||||
color="danger-subtle"
|
||||
onclick={async () => {
|
||||
if (profile.current?.jwt)
|
||||
comment.comment.deleted = await deleteItem(
|
||||
comment,
|
||||
!comment.comment.deleted,
|
||||
)
|
||||
}}
|
||||
icon={Trash}
|
||||
>
|
||||
{comment.comment.deleted
|
||||
? $t('post.actions.more.restore')
|
||||
: $t('post.actions.more.delete')}
|
||||
</MenuButton>
|
||||
{/if}
|
||||
{#if profile.current.jwt && profile.current.user?.local_user_view.person.id != comment.creator.id}
|
||||
<MenuButton
|
||||
onclick={() => report(comment)}
|
||||
color="danger-subtle"
|
||||
icon={Flag}
|
||||
>
|
||||
{$t('moderation.report')}
|
||||
</MenuButton>
|
||||
{/if}
|
||||
{/if}
|
||||
</Menu>
|
||||
<div class="flex-1 w-full"></div>
|
||||
</div>
|
||||
@@ -0,0 +1,177 @@
|
||||
<script lang="ts">
|
||||
import { profile } from '$lib/app/auth.svelte.js'
|
||||
import Markdown from '$lib/app/markdown/Markdown.svelte'
|
||||
import MarkdownEditor from '$lib/app/markdown/MarkdownEditor.svelte'
|
||||
import { t } from '$lib/app/i18n'
|
||||
import { client, site } from '$lib/api/client.svelte'
|
||||
import { errorMessage } from '$lib/lemmy/error'
|
||||
import { placeholders } from '$lib/util.svelte.js'
|
||||
import type { CommentResponse } from '$lib/api/types'
|
||||
import { Button, Menu, MenuButton, toast } from 'mono-svelte'
|
||||
import { Icon, Language, XMark } from 'svelte-hero-icons'
|
||||
import type { ClassValue, HTMLTextareaAttributes } from 'svelte/elements'
|
||||
|
||||
interface Props extends Omit<HTMLTextareaAttributes, 'oncancel'> {
|
||||
postId: number
|
||||
parentId?: number | undefined
|
||||
locked?: boolean
|
||||
banned?: boolean
|
||||
rows?: number
|
||||
placeholder?: string | undefined
|
||||
value?: string
|
||||
actions?: boolean
|
||||
tools?: boolean
|
||||
preview?: boolean
|
||||
class?: ClassValue
|
||||
required?: boolean
|
||||
id?: string
|
||||
label?: string
|
||||
editing?: boolean
|
||||
oncomment?: (comment: CommentResponse) => void
|
||||
onconfirm?: (value: string) => void
|
||||
oncancel?: (cancel: boolean) => void
|
||||
}
|
||||
|
||||
let {
|
||||
postId,
|
||||
parentId = undefined,
|
||||
locked = false,
|
||||
banned = false,
|
||||
rows = 7,
|
||||
placeholder = undefined,
|
||||
value = $bindable(''),
|
||||
actions = true,
|
||||
preview: previewAction = true,
|
||||
editing = false,
|
||||
oncancel,
|
||||
oncomment,
|
||||
...rest
|
||||
}: Props = $props()
|
||||
|
||||
let loading = $state(false)
|
||||
let preview = false
|
||||
let language: number | undefined = $state()
|
||||
|
||||
async function submit() {
|
||||
if (
|
||||
!profile.current?.user ||
|
||||
!profile.current?.jwt ||
|
||||
value == '' ||
|
||||
editing
|
||||
)
|
||||
return
|
||||
|
||||
loading = true
|
||||
|
||||
try {
|
||||
const response = await client().createComment({
|
||||
content: value,
|
||||
post_id: postId,
|
||||
parent_id: parentId,
|
||||
})
|
||||
oncomment?.(response)
|
||||
|
||||
value = ''
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
toast({
|
||||
content: errorMessage(err as string),
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
|
||||
loading = false
|
||||
}
|
||||
</script>
|
||||
|
||||
<form
|
||||
onsubmit={(e) => {
|
||||
e.preventDefault()
|
||||
submit()
|
||||
}}
|
||||
class="flex flex-col gap-2 relative"
|
||||
>
|
||||
{#if preview}
|
||||
<div
|
||||
class="bg-slate-100 dark:bg-zinc-900 px-3 py-2.5 h-64 border
|
||||
border-slate-300 dark:border-zinc-700 rounded-md overflow-auto text-sm resize-y"
|
||||
>
|
||||
<Markdown source={value} />
|
||||
</div>
|
||||
{:else}
|
||||
<MarkdownEditor
|
||||
{...rest}
|
||||
{rows}
|
||||
placeholder={locked
|
||||
? $t('comment.locked')
|
||||
: banned
|
||||
? $t('comment.banned')
|
||||
: (placeholder ?? placeholders.get('comment'))}
|
||||
bind:value
|
||||
disabled={locked || loading || banned}
|
||||
previewButton={previewAction}
|
||||
>
|
||||
<Menu>
|
||||
{#snippet target(attachment)}
|
||||
<Button
|
||||
{@attach attachment}
|
||||
size="custom"
|
||||
rounding="xl"
|
||||
class="w-7.5 h-7.5"
|
||||
color={language != undefined ? 'primary' : 'ghost'}
|
||||
title={$t('form.profile.languages.title')}
|
||||
>
|
||||
<Icon src={Language} size="14" micro />
|
||||
</Button>
|
||||
{/snippet}
|
||||
|
||||
{#if site.data}
|
||||
<MenuButton
|
||||
class="min-h-[16px] py-0"
|
||||
onclick={() => (language = undefined)}
|
||||
>
|
||||
<Icon src={XMark} size="16" micro />
|
||||
{$t('form.post.unset')}
|
||||
</MenuButton>
|
||||
{#each site.data?.all_languages as languageOption}
|
||||
<MenuButton
|
||||
class="min-h-[16px] py-0"
|
||||
onclick={() => {
|
||||
language = languageOption.id
|
||||
}}
|
||||
>
|
||||
{languageOption.name}
|
||||
</MenuButton>
|
||||
{/each}
|
||||
{/if}
|
||||
</Menu>
|
||||
<div class="flex-1"></div>
|
||||
{#if actions}
|
||||
<Button
|
||||
size="custom"
|
||||
title={$t('common.cancel')}
|
||||
onclick={() => oncancel?.(true)}
|
||||
color="tertiary"
|
||||
class="w-8 h-8"
|
||||
rounding="xl"
|
||||
>
|
||||
<Icon
|
||||
src={XMark}
|
||||
size="16"
|
||||
micro
|
||||
class="text-slate-600 dark:text-zinc-400"
|
||||
/>
|
||||
</Button>
|
||||
<Button
|
||||
submit
|
||||
color="primary"
|
||||
rounding="xl"
|
||||
{loading}
|
||||
disabled={locked || loading || banned}
|
||||
>
|
||||
{$t('form.submit')}
|
||||
</Button>
|
||||
{/if}
|
||||
</MarkdownEditor>
|
||||
{/if}
|
||||
</form>
|
||||
@@ -0,0 +1,79 @@
|
||||
<script lang="ts">
|
||||
import { publishedToDate } from '$comp/util/date.js'
|
||||
import { t } from '$lib/app/i18n'
|
||||
import type { CommentView } from '$lib/api/types'
|
||||
import { Button, Material } from 'mono-svelte'
|
||||
import { ArrowUturnUp, Icon } from 'svelte-hero-icons'
|
||||
import type { ClassValue } from 'svelte/elements'
|
||||
import { PostMeta } from '../post'
|
||||
import Comment from './Comment.svelte'
|
||||
|
||||
interface Props {
|
||||
comment: CommentView
|
||||
community?: boolean
|
||||
meta?: boolean
|
||||
class?: string
|
||||
commentClass?: ClassValue
|
||||
actions?: boolean
|
||||
}
|
||||
|
||||
let {
|
||||
comment,
|
||||
community = false,
|
||||
meta = true,
|
||||
class: clazz = '',
|
||||
commentClass = '',
|
||||
actions = true,
|
||||
...rest
|
||||
}: Props = $props()
|
||||
</script>
|
||||
|
||||
<Material
|
||||
class={['flex flex-col flex-1 rounded-none list-none', clazz]}
|
||||
color="none"
|
||||
padding="none"
|
||||
>
|
||||
{#if meta}
|
||||
<div class="flex flex-row justify-between items-center gap-2">
|
||||
<div class="flex flex-col gap-2">
|
||||
<PostMeta
|
||||
badges={{
|
||||
nsfw: comment.post.nsfw,
|
||||
removed: comment.post.removed,
|
||||
admin: false,
|
||||
moderator: false,
|
||||
saved: false,
|
||||
deleted: comment.post.deleted,
|
||||
featured:
|
||||
comment.post.featured_community || comment.post.featured_local,
|
||||
locked: comment.post.locked,
|
||||
}}
|
||||
published={publishedToDate(comment.comment.published)}
|
||||
community={community ? comment.community : undefined}
|
||||
title={comment.post.name}
|
||||
id={comment.post.id}
|
||||
titleClass="text-sm text-slate-500 dark:text-zinc-400"
|
||||
/>
|
||||
</div>
|
||||
<Button
|
||||
color="primary"
|
||||
rounding="pill"
|
||||
size="sm"
|
||||
href="/comment/{comment.comment.id}"
|
||||
class="self-start"
|
||||
>
|
||||
{$t('common.jump')}
|
||||
<Icon src={ArrowUturnUp} size="14" micro />
|
||||
</Button>
|
||||
</div>
|
||||
{/if}
|
||||
<Comment
|
||||
postId={comment.post.id}
|
||||
node={{ children: [], comment_view: comment, depth: 1, expanded: true }}
|
||||
replying={false}
|
||||
{meta}
|
||||
{actions}
|
||||
{...rest}
|
||||
class={commentClass}
|
||||
/>
|
||||
</Material>
|
||||
@@ -0,0 +1,60 @@
|
||||
<script lang="ts">
|
||||
import VirtualList from '$lib/app/render/VirtualList.svelte'
|
||||
import type { Post } from '$lib/api/types'
|
||||
import type { CommentNodeI } from './comments.svelte'
|
||||
import Comments from './CommentTree.svelte'
|
||||
import { onMount } from 'svelte'
|
||||
|
||||
interface Props {
|
||||
nodes: CommentNodeI[]
|
||||
post: Post
|
||||
scrollTo?: string
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
if (scrollTo) {
|
||||
const element = document?.getElementById(scrollTo)
|
||||
setTimeout(() => {
|
||||
element?.scrollIntoView({ behavior: 'smooth', block: 'start' })
|
||||
}, 100)
|
||||
}
|
||||
|
||||
const observer = new ResizeObserver((entries) => {
|
||||
entries.forEach((entry) => {
|
||||
if (offsetEl) {
|
||||
initialOffset =
|
||||
entry.target.getBoundingClientRect().top + window.scrollY
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
if (offsetEl) observer.observe(offsetEl)
|
||||
return () => observer.disconnect()
|
||||
})
|
||||
|
||||
let { nodes, post, scrollTo }: Props = $props()
|
||||
|
||||
let offsetEl = $state<HTMLElement>()
|
||||
let initialOffset = $derived(offsetEl?.offsetTop)
|
||||
</script>
|
||||
|
||||
<div bind:this={offsetEl}>
|
||||
{#if offsetEl}
|
||||
<VirtualList
|
||||
class="divide-y-2 divide-slate-100 dark:divide-zinc-900 -mx-3 sm:-mx-6"
|
||||
overscan={5}
|
||||
estimatedHeight={500}
|
||||
items={nodes}
|
||||
{initialOffset}
|
||||
>
|
||||
{#snippet item(index)}
|
||||
<div class={['px-3 sm:px-6', index % 2 == 1 && '']}>
|
||||
<Comments
|
||||
bind:nodes={() => [nodes[index]], (v) => (nodes[index] = v[0])}
|
||||
{post}
|
||||
/>
|
||||
</div>
|
||||
{/snippet}
|
||||
</VirtualList>
|
||||
{/if}
|
||||
</div>
|
||||
@@ -0,0 +1,141 @@
|
||||
<script lang="ts">
|
||||
import { browser } from '$app/environment'
|
||||
import { goto } from '$app/navigation'
|
||||
import { client } from '$lib/api/client.svelte'
|
||||
import type { Post } from '$lib/api/types'
|
||||
import { t } from '$lib/app/i18n'
|
||||
import { errorMessage } from '$lib/lemmy/error'
|
||||
import { Button, toast } from 'mono-svelte'
|
||||
import { ArrowDownCircle } from 'svelte-hero-icons'
|
||||
import Comment from './Comment.svelte'
|
||||
import Comments from './CommentTree.svelte'
|
||||
import { buildCommentsTree, type CommentNodeI } from './comments.svelte'
|
||||
|
||||
interface Props {
|
||||
nodes: CommentNodeI[]
|
||||
post: Post
|
||||
}
|
||||
|
||||
let { nodes = $bindable(), post }: Props = $props()
|
||||
|
||||
let childrenPage = 0
|
||||
|
||||
async function fetchChildren(parent: CommentNodeI) {
|
||||
if (
|
||||
!(
|
||||
parent.comment_view.counts.child_count > 0 &&
|
||||
parent.children.length == 0
|
||||
)
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
parent.loading = true
|
||||
|
||||
const newComments = await client().getComments({
|
||||
max_depth: 5,
|
||||
parent_id: parent.comment_view.comment.id,
|
||||
type_: 'All',
|
||||
page: childrenPage,
|
||||
})
|
||||
|
||||
if (newComments.comments.length == 0) {
|
||||
toast({
|
||||
content: $t('toast.noComments'),
|
||||
type: 'error',
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const tree = buildCommentsTree(newComments.comments, parent.depth)
|
||||
|
||||
// 0.18.2 -> 0.18.3 broke this
|
||||
// so i'm adding this check
|
||||
const treeParent = tree.find(
|
||||
(c) => c.comment_view.comment.id == parent.comment_view.comment.id,
|
||||
)
|
||||
|
||||
if (treeParent) {
|
||||
// < 0.18.3
|
||||
parent.children = treeParent.children
|
||||
if (treeParent.children.length == 0) {
|
||||
toast({
|
||||
content: $t('toast.noComments'),
|
||||
type: 'warning',
|
||||
})
|
||||
}
|
||||
} else {
|
||||
// 0.18.3+
|
||||
parent.children = tree
|
||||
if (tree.length == 0) {
|
||||
toast({
|
||||
content: $t('toast.noComments'),
|
||||
type: 'warning',
|
||||
})
|
||||
}
|
||||
}
|
||||
childrenPage++
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
toast({
|
||||
content: errorMessage(err as string),
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<ul class="animate-pop-in">
|
||||
{#each nodes as node, index (node.comment_view.comment.id)}
|
||||
<Comment
|
||||
bind:node={nodes[index]}
|
||||
postId={post.id}
|
||||
op={post.creator_id == node.comment_view.creator.id}
|
||||
contentClass={[
|
||||
(node.children.length > 0 ||
|
||||
node.comment_view.counts.child_count > 0) &&
|
||||
'border-l',
|
||||
'ml-2.5 pl-3 sm:pl-4 lg:pl-5',
|
||||
'border-slate-200 dark:border-zinc-800',
|
||||
]}
|
||||
bind:open={nodes[index].expanded}
|
||||
>
|
||||
{#if node.children?.length > 0}
|
||||
<Comments {post} bind:nodes={nodes[index].children} />
|
||||
{/if}
|
||||
</Comment>
|
||||
{#if node.comment_view.counts.child_count > 0 && node.children.length == 0}
|
||||
<svelte:element
|
||||
this={browser ? 'div' : 'a'}
|
||||
class="w-full h-10 -mt-2 -ml-2.5"
|
||||
href="/comment/{node.comment_view.comment.id}"
|
||||
>
|
||||
<Button
|
||||
loading={nodes[index].loading}
|
||||
disabled={nodes[index].loading}
|
||||
rounding="pill"
|
||||
color="tertiary"
|
||||
class="font-normal text-slate-600 dark:text-zinc-400"
|
||||
shadow="none"
|
||||
loaderWidth={16}
|
||||
onclick={() => {
|
||||
if (nodes[index].depth > 4) {
|
||||
goto(`/comment/${nodes[index].comment_view.comment.id}#comments`)
|
||||
} else {
|
||||
nodes[index].loading = true
|
||||
fetchChildren(nodes[index]).then(
|
||||
() => (nodes[index].loading = false),
|
||||
)
|
||||
}
|
||||
}}
|
||||
icon={ArrowDownCircle}
|
||||
>
|
||||
{$t('comment.more', {
|
||||
comments: node.comment_view.counts.child_count,
|
||||
})}
|
||||
</Button>
|
||||
</svelte:element>
|
||||
{/if}
|
||||
{/each}
|
||||
</ul>
|
||||
@@ -0,0 +1,148 @@
|
||||
<script lang="ts">
|
||||
import { profile } from '$lib/app/auth.svelte.js'
|
||||
import { site } from '$lib/api/client.svelte'
|
||||
import FormattedNumber from '$comp/util/FormattedNumber.svelte'
|
||||
import { t } from '$lib/app/i18n'
|
||||
import { errorMessage } from '$lib/lemmy/error'
|
||||
import { settings } from '$lib/settings.svelte'
|
||||
import { buttonColor, toast } from 'mono-svelte'
|
||||
import { ChevronDown, ChevronUp, Icon } from 'svelte-hero-icons'
|
||||
import { backOut } from 'svelte/easing'
|
||||
import { fly } from 'svelte/transition'
|
||||
import { shouldShowVoteColor } from '../post/PostVote.svelte'
|
||||
import { vote as voteItem } from '$lib/lemmy/contentview'
|
||||
import type { Comment } from '$lib/api/types'
|
||||
|
||||
interface Props {
|
||||
vote?: number
|
||||
upvotes: number
|
||||
downvotes: number
|
||||
comment: Comment
|
||||
}
|
||||
|
||||
let {
|
||||
vote = $bindable(0),
|
||||
upvotes = $bindable(),
|
||||
downvotes = $bindable(),
|
||||
comment,
|
||||
}: Props = $props()
|
||||
|
||||
const castVote = async (newVote: number) => {
|
||||
if (!profile.current?.jwt) {
|
||||
toast({ content: $t('toast.loginVoteGate'), type: 'warning' })
|
||||
return
|
||||
}
|
||||
|
||||
switch (vote ?? 0) {
|
||||
case 0:
|
||||
// nothing was removed
|
||||
if (newVote == 1) upvotes++
|
||||
else downvotes++
|
||||
break
|
||||
case 1:
|
||||
// removed an upvote
|
||||
upvotes--
|
||||
if (newVote == -1) downvotes++
|
||||
break
|
||||
case -1:
|
||||
// removed a downvote
|
||||
downvotes--
|
||||
if (newVote == 1) upvotes++
|
||||
break
|
||||
}
|
||||
|
||||
vote = newVote
|
||||
|
||||
voteItem(comment, newVote)
|
||||
.then((res) => ({ upvotes, downvotes } = res))
|
||||
.catch((e) => {
|
||||
toast({ content: errorMessage(e), type: 'error' })
|
||||
})
|
||||
}
|
||||
|
||||
let voteRatio = $derived(
|
||||
Math.floor(((upvotes ?? 0) / ((upvotes ?? 0) + (downvotes ?? 0))) * 100),
|
||||
)
|
||||
</script>
|
||||
|
||||
{#snippet voteButton(
|
||||
votes: number,
|
||||
target: 'upvote' | 'downvote',
|
||||
vote?: number,
|
||||
)}
|
||||
{@const targetNum = target == 'upvote' ? 1 : -1}
|
||||
<button
|
||||
onclick={() => castVote(vote == targetNum ? 0 : targetNum)}
|
||||
class={[
|
||||
'flex items-center gap-0.5 transition-colors relative cursor-pointer h-full p-1.5',
|
||||
'first:rounded-l-3xl last:rounded-r-3xl',
|
||||
'last:flex-row-reverse',
|
||||
vote == targetNum
|
||||
? shouldShowVoteColor(
|
||||
vote,
|
||||
target == 'upvote' ? 'upvotes' : 'downvotes',
|
||||
)
|
||||
: 'hover:bg-slate-100 dark:hover:bg-zinc-800',
|
||||
]}
|
||||
aria-pressed={vote == targetNum}
|
||||
aria-label={$t(
|
||||
target == 'upvote'
|
||||
? 'post.actions.vote.upvote'
|
||||
: 'post.actions.vote.downvote',
|
||||
)}
|
||||
>
|
||||
<Icon src={target == 'upvote' ? ChevronUp : ChevronDown} size="18" micro />
|
||||
<div class="grid text-sm z-20">
|
||||
{#key votes}
|
||||
<span
|
||||
style="grid-column: 1; grid-row: 1;"
|
||||
in:fly={{ duration: 400, y: -10, easing: backOut }}
|
||||
out:fly={{ duration: 400, y: 10, easing: backOut }}
|
||||
aria-label={$t(
|
||||
target == 'upvote' ? 'aria.vote.upvotes' : 'aria.vote.downvotes',
|
||||
{ default: votes },
|
||||
)}
|
||||
>
|
||||
<FormattedNumber number={votes ?? 0} />
|
||||
</span>
|
||||
{/key}
|
||||
</div>
|
||||
</button>
|
||||
{/snippet}
|
||||
|
||||
<div
|
||||
class={[
|
||||
'h-full relative flex items-center overflow-hidden rounded-full hover:bg-transparent font-medium hover:dark:bg-transparent',
|
||||
buttonColor.ghost,
|
||||
voteRatio < 85 && settings.voteRatioBar && 'vote-ratio',
|
||||
]}
|
||||
style="--vote-ratio: {voteRatio}%;"
|
||||
>
|
||||
{@render voteButton(upvotes, 'upvote', vote)}
|
||||
<div class="h-full p-0! border-l border-slate-200 dark:border-zinc-800"></div>
|
||||
{#if site.data?.site_view.local_site.enable_downvotes ?? true}
|
||||
{@render voteButton(downvotes, 'downvote', vote)}
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.vote-ratio {
|
||||
z-index: 0;
|
||||
}
|
||||
|
||||
.vote-ratio::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
opacity: 10%;
|
||||
z-index: -10;
|
||||
left: 0;
|
||||
bottom: 0px;
|
||||
background: linear-gradient(
|
||||
to right,
|
||||
var(--color-indigo-500) calc(var(--vote-ratio) - 5%),
|
||||
var(--color-red-500) calc(var(--vote-ratio) + 5%)
|
||||
);
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,123 @@
|
||||
import { t } from '$lib/app/i18n/translations'
|
||||
import type { Comment, CommentView } from '$lib/client/types'
|
||||
import { SvelteMap } from 'svelte/reactivity'
|
||||
|
||||
export interface CommentNodeI {
|
||||
comment_view: CommentView
|
||||
children: Array<CommentNodeI>
|
||||
depth: number
|
||||
loading?: boolean
|
||||
expanded?: boolean
|
||||
}
|
||||
|
||||
function getCommentParentId(comment?: Comment): number | undefined {
|
||||
const split = comment?.path.split('.')
|
||||
// remove the 0
|
||||
split?.shift()
|
||||
|
||||
return split && split.length > 1
|
||||
? Number(split.at(split.length - 2))
|
||||
: undefined
|
||||
}
|
||||
|
||||
function getDepthFromComment(comment?: Comment): number | undefined {
|
||||
const len = comment?.path.split('.').length
|
||||
return len ? len - 2 : undefined
|
||||
}
|
||||
|
||||
export function buildCommentsTree(
|
||||
comments: CommentView[],
|
||||
baseDepth: number = 0,
|
||||
filter: (c: CommentView) => boolean = () => true,
|
||||
): CommentNodeI[] {
|
||||
const map = new SvelteMap<number, CommentNodeI>()
|
||||
|
||||
let min_depth = Number.MAX_VALUE
|
||||
for (const comment_view of comments) {
|
||||
const depthI = getDepthFromComment(comment_view.comment) ?? 0
|
||||
const depth = depthI + baseDepth
|
||||
if (comment_view.comment.content == '' && comment_view.comment.removed) {
|
||||
comment_view.comment.content = `*Removed by Moderator — [${t.get('routes.modlog.title')}](/modlog?comment=${comment_view.comment.id})*`
|
||||
}
|
||||
const node: CommentNodeI = {
|
||||
comment_view,
|
||||
children: [],
|
||||
depth: depth,
|
||||
expanded: true,
|
||||
}
|
||||
min_depth = Math.min(min_depth, depth)
|
||||
if (filter(comment_view)) {
|
||||
map.set(comment_view.comment.id, { ...node })
|
||||
}
|
||||
}
|
||||
|
||||
const tree: CommentNodeI[] = []
|
||||
|
||||
// push all nodes at the minimum depth to the top of the tree
|
||||
for (const comment_view of comments) {
|
||||
const cNode = map.get(comment_view.comment.id)
|
||||
if (cNode && cNode.depth == min_depth) {
|
||||
tree.push(cNode)
|
||||
}
|
||||
}
|
||||
|
||||
for (const comment_view of comments) {
|
||||
const child = map.get(comment_view.comment.id)
|
||||
if (child) {
|
||||
const parent_id = getCommentParentId(comment_view.comment)
|
||||
if (parent_id) {
|
||||
const parent = map.get(parent_id)
|
||||
// Necessary because blocked comment might not exist
|
||||
if (parent) {
|
||||
parent.children.push(child)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tree
|
||||
}
|
||||
|
||||
export function searchCommentTree(
|
||||
tree: CommentNodeI[],
|
||||
id: number,
|
||||
): CommentNodeI | undefined {
|
||||
for (const node of tree) {
|
||||
if (node.comment_view.comment.id === id) {
|
||||
return node
|
||||
}
|
||||
|
||||
for (const child of node.children) {
|
||||
const res = searchCommentTree([child], id)
|
||||
|
||||
if (res) {
|
||||
return res
|
||||
}
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
export function insertCommentIntoTree(
|
||||
tree: CommentNodeI[],
|
||||
cv: CommentView,
|
||||
parentComment: boolean,
|
||||
) {
|
||||
// Building a fake node to be used for later
|
||||
const node: CommentNodeI = {
|
||||
comment_view: cv,
|
||||
children: [],
|
||||
depth: 0,
|
||||
}
|
||||
|
||||
const parentId = getCommentParentId(cv.comment)
|
||||
if (parentId) {
|
||||
const parent_comment = searchCommentTree(tree, parentId)
|
||||
if (parent_comment) {
|
||||
node.depth = parent_comment.depth + 1
|
||||
parent_comment.children.unshift(node)
|
||||
}
|
||||
} else if (!parentComment) {
|
||||
tree.unshift(node)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user