feat: Add post and comment editing (#12)

* feat: Add editing

* feat: Add comment badges

* fix: make loading more consistent

* misc: remove prop drilling

* feat: Add post editing

* misc: update version

* feat: Add comment deletion
This commit is contained in:
Xylight authored and GitHub committed 2023-07-25 11:12:15 -07:00
1 parent 0327ae4aed
commit 7b1ad247f9
16 files changed
+417 -192

No files matched your search

@@ -1,5 +1,13 @@
<script lang="ts">
import { Icon, Minus, Plus } from 'svelte-hero-icons'
import {
ArrowUp,
Bookmark,
Icon,
Minus,
Pencil,
Plus,
Trash,
} from 'svelte-hero-icons'
import type { CommentNodeI } from './comments'
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
import CommentForm from './CommentForm.svelte'
@@ -7,6 +15,12 @@
import UserLink from '$lib/components/user/UserLink.svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte'
import CommentActions from '$lib/components/lemmy/comment/CommentActions.svelte'
import { createEventDispatcher } from 'svelte'
import type { CommentView } from 'lemmy-js-client'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import { authData, getClient } from '$lib/lemmy.js'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import TextArea from '$lib/components/input/TextArea.svelte'
export let node: CommentNodeI
export let postId: number
@@ -14,8 +28,48 @@
export let open = true
export let replying = false
let editing = false
let newComment = node.comment_view.comment.content
</script>
{#if editing}
<Modal
bind:open={editing}
action="Save"
on:action={async () => {
if (!$authData || newComment.length <= 0) return
try {
await getClient().editComment({
auth: $authData.token,
comment_id: node.comment_view.comment.id,
content: newComment,
})
node.comment_view.comment.content = newComment
editing = false
toast({
content:
'Successfully edited comment. You may need to refresh to see changes.',
type: ToastType.success,
})
} catch (err) {
toast({
// @ts-ignore i hate this
content: err,
type: ToastType.error,
})
}
}}
>
<span slot="title">Edit comment</span>
<TextArea bind:value={newComment} />
</Modal>
{/if}
<li
class="py-2 {node.depth == 0
? 'border-b border-slate-200 dark:border-zinc-800'
@@ -32,7 +86,15 @@
<span class="text-sky-500">OP</span>
{/if}
</span>
<span class="opacity-60 flex flex-row gap-1">
<span class="text-slate-600 dark:text-zinc-400 flex flex-row gap-1">
{#if !open}
<div
class="flex items-center gap-0.5 mr-1 text-slate-800 dark:text-zinc-200"
>
<Icon src={ArrowUp} mini size="14" />
{node.comment_view.counts.score}
</div>
{/if}
<RelativeDate date={new Date(node.comment_view.comment.published)} />
<span>•</span>
<span>
@@ -44,6 +106,17 @@
) || 0}%
</span>
</span>
<span class="text-slate-600 dark:text-zinc-400 flex flex-row gap-2 ml-1">
{#if node.comment_view.comment.updated}
<Icon src={Pencil} solid size="12" title="Edited" />
{/if}
{#if node.comment_view.comment.deleted || node.comment_view.comment.removed}
<Icon src={Trash} solid size="12" title="Deleted" />
{/if}
{#if node.comment_view.saved}
<Icon src={Bookmark} solid size="12" title="Saved" />
{/if}
</span>
<span
class="ml-auto translate-x-1 opacity-0
group-hover:translate-x-0 group-hover:opacity-100 text-xs transition-all
@@ -68,7 +141,11 @@
<Markdown source={node.comment_view.comment.content} />
</div>
<div class="flex flex-row gap-2 items-center h-[26px]">
<CommentActions comment={node.comment_view} bind:replying />
<CommentActions
comment={node.comment_view}
bind:replying
on:edit={() => (editing = true)}
/>
</div>
</div>
{#if replying}
@@ -9,12 +9,16 @@
ChatBubbleOvalLeft,
EllipsisHorizontal,
Icon,
PencilSquare,
Trash,
} from 'svelte-hero-icons'
import { authData, getClient } from '$lib/lemmy.js'
import { authData, getClient, user } from '$lib/lemmy.js'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import Menu from '$lib/components/ui/menu/Menu.svelte'
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
import Button from '$lib/components/input/Button.svelte'
import { createEventDispatcher } from 'svelte'
import { isCommentMutable } from '$lib/components/lemmy/post/helpers.js'
export let comment: CommentView
export let replying: boolean = false
@@ -41,6 +45,8 @@
})
}
}
const dispatcher = createEventDispatcher<{ edit: CommentView }>()
</script>
<div class="flex flex-row gap-2 items-center mt-1 h-[26px] relative">
@@ -53,34 +59,59 @@
<Icon src={ChatBubbleOvalLeft} width={16} height={16} mini />
<span class="text-xs">Reply</span>
</Button>
<div class="h-full relative">
<Menu
let:toggleOpen
class="h-[26px] top-0"
absolute
alignment="bottom-center"
<Menu
let:toggleOpen
class="h-[26px] top-0 leading-3"
alignment="bottom-center"
>
<Button
slot="button"
color="elevatedLow"
on:click={toggleOpen}
size="sm"
class="!p-1"
>
<Button
slot="button"
color="elevatedLow"
on:click={toggleOpen}
size="sm"
class="!p-1"
>
<Icon
src={EllipsisHorizontal}
width={16}
height={16}
mini
slot="icon"
/>
</Button>
{#if $authData?.token}
<MenuButton on:click={save}>
<Icon src={comment.saved ? BookmarkSlash : Bookmark} mini size="16" />
<span>{comment.saved ? 'Unsave' : 'Save'}</span>
<Icon src={EllipsisHorizontal} width={16} height={16} mini slot="icon" />
</Button>
<span class="text-xs opacity-80 py-1 my-1 px-4">Comment actions</span>
{#if $authData?.token}
{#if comment.creator.id == $user?.person.id}
<MenuButton on:click={() => dispatcher('edit', comment)}>
<Icon src={PencilSquare} mini size="16" />
<span>Edit</span>
</MenuButton>
{/if}
</Menu>
</div>
<MenuButton on:click={save}>
<Icon src={comment.saved ? BookmarkSlash : Bookmark} mini size="16" />
<span>{comment.saved ? 'Unsave' : 'Save'}</span>
</MenuButton>
{#if $user && $authData && isCommentMutable(comment, $user)}
<MenuButton
color={Color.dangerSecondary}
on:click={async () => {
if (!$authData) return
try {
await getClient().deleteComment({
auth: $authData.token,
comment_id: comment.comment.id,
deleted: true,
})
toast({
content: 'The comment was deleted.',
type: ToastType.success,
})
} catch (error) {
toast({
content: 'Failed to delete comment',
type: ToastType.error,
})
}
}}
>
<Icon src={Trash} mini size="16" />
<span>Delete</span>
</MenuButton>
{/if}
{/if}
</Menu>
</div>
@@ -6,9 +6,11 @@
import Button from '$lib/components/input/Button.svelte'
import { ChevronDown, Icon } from 'svelte-hero-icons'
import { authData, getClient } from '$lib/lemmy.js'
import type { Post } from 'lemmy-js-client'
import type { CommentView, Post } from 'lemmy-js-client'
import { fly } from 'svelte/transition'
import { toast, ToastType } from '$lib/components/ui/toasts/toasts.js'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import TextArea from '$lib/components/input/TextArea.svelte'
export let nodes: CommentNodeI[]
export let isParent: boolean
@@ -80,7 +82,7 @@
{/if}
{#if node.comment_view.counts.child_count > 0 && node.children.length == 0}
<div
class="my-2 min-w-[7rem] w-max h-8 border-l-2 border-slate-200 dark:border-zinc-900 ml-2.5 pl-1"
class="my-2 min-w-[7rem] w-max h-8 border-l-2 border-slate-200 dark:border-zinc-900 pl-2"
>
<Button
loading={node.loading}