Implement code comments for single line selections

This commit is contained in:
Rūdolfs Ošiņš
2025-03-17 21:17:25 -06:00
parent a28aa0d6ab
commit 87a455d2a4
16 changed files with 849 additions and 431 deletions
+63 -54
View File
@@ -1,7 +1,8 @@
<script lang="ts">
import type { Author } from "@bindings/cob/Author";
import type { Comment } from "@bindings/cob/thread/Comment";
import type { CodeLocation } from "@bindings/cob/thread/CodeLocation";
import type { Embed } from "@bindings/cob/thread/Embed";
import type { Thread } from "@bindings/cob/thread/Thread";
import { tick } from "svelte";
import partial from "lodash/partial";
@@ -14,10 +15,7 @@
import Icon from "@app/components/Icon.svelte";
interface Props {
thread: {
root: Comment;
replies: Comment[];
};
thread: Thread<CodeLocation>;
rid: string;
canEditComment: (author: string) => true | undefined;
editComment?: (
@@ -35,6 +33,7 @@
authors: Author[],
reaction: string,
) => Promise<void>;
inline?: boolean;
}
const {
@@ -44,6 +43,7 @@
editComment,
createReply,
reactOnComment,
inline = false,
}: Props = $props();
async function toggleReply() {
@@ -76,6 +76,7 @@
display: flex;
flex-direction: column;
width: 100%;
font-family: var(--font-family-sans-serif);
}
.top-level-comment {
@@ -96,8 +97,53 @@
}
</style>
{#snippet repliesSnippet()}
<div style:width="100%">
{#if replies.length > 0}
{#each replies as reply}
<CommentComponent
disallowEmptyBody
{rid}
lastEdit={reply.edits.length > 1 ? reply.edits.at(-1) : undefined}
id={reply.id}
author={reply.author}
caption="replied"
reactions={reply.reactions}
timestamp={reply.edits[0].timestamp}
body={reply.edits.slice(-1)[0].body}
editComment={editComment &&
canEditComment(reply.author.did) &&
partial(editComment, reply.id)}
reactOnComment={reactOnComment &&
partial(reactOnComment, reply.id)} />
{/each}
{/if}
{#if createReply && showReplyForm}
<div id={`reply-${root.id}`} style:padding="1rem">
<ExtendedTextarea
disallowEmptyBody
{submitInProgress}
{rid}
placeholder="Reply to comment"
submitCaption="Reply"
focus
close={() => (showReplyForm = false)}
submit={async ({ comment, embeds }) => {
try {
submitInProgress = true;
await createReply(comment, Array.from(embeds.values()), root.id);
} finally {
showReplyForm = false;
submitInProgress = false;
}
}} />
</div>
{/if}
</div>
{/snippet}
<div class="comments" {style}>
<div class="top-level-comment">
<div class:top-level-comment={!inline}>
<CommentComponent
disallowEmptyBody
{rid}
@@ -112,58 +158,21 @@
partial(editComment, root.id)}
reactOnComment={reactOnComment && partial(reactOnComment, root.id)}>
{#snippet actions()}
<Icon name="reply" onclick={toggleReply} />
{#if createReply}
<Icon name="reply" onclick={toggleReply} />
{/if}
{/snippet}
</CommentComponent>
</div>
{#if replies.length > 0 || (createReply && showReplyForm)}
<Border variant="float" styleOverflow="hidden" flatTop>
<div style:width="100%">
{#if replies.length > 0}
{#each replies as reply}
<CommentComponent
disallowEmptyBody
{rid}
lastEdit={reply.edits.length > 1 ? reply.edits.at(-1) : undefined}
id={reply.id}
author={reply.author}
caption="replied"
reactions={reply.reactions}
timestamp={reply.edits[0].timestamp}
body={reply.edits.slice(-1)[0].body}
editComment={editComment &&
canEditComment(reply.author.did) &&
partial(editComment, reply.id)}
reactOnComment={reactOnComment &&
partial(reactOnComment, reply.id)} />
{/each}
{/if}
{#if createReply && showReplyForm}
<div id={`reply-${root.id}`} style:padding="1rem">
<ExtendedTextarea
disallowEmptyBody
{submitInProgress}
{rid}
placeholder="Reply to comment"
submitCaption="Reply"
focus
close={() => (showReplyForm = false)}
submit={async ({ comment, embeds }) => {
try {
submitInProgress = true;
await createReply(
comment,
Array.from(embeds.values()),
root.id,
);
} finally {
showReplyForm = false;
submitInProgress = false;
}
}} />
</div>
{/if}
{#if inline}
<div style:background-color="var(--color-background-float)">
{@render repliesSnippet()}
</div>
</Border>
{:else}
<Border variant="float" styleOverflow="hidden" flatTop={!inline}>
{@render repliesSnippet()}
</Border>
{/if}
{/if}
</div>