Add editing to issue/patch descriptions, comments and replies

Also does some refactoring for `action`, `editable` and other variables
we were using which weren't very clear

Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
Sebastian Martinez
2023-10-26 16:27:54 +02:00
parent 9530af9ada
commit 5e5f7fc12f
22 changed files with 707 additions and 319 deletions
+53 -18
View File
@@ -1,29 +1,35 @@
<script lang="ts" strictEvents>
import { createEventDispatcher } from "svelte";
import { createEventDispatcher, tick } from "svelte";
import { httpdStore } from "@app/lib/httpd";
import { authenticated } from "@app/lib/httpd";
import * as utils from "@app/lib/utils";
import ExtendedTextarea from "@app/components/ExtendedTextarea.svelte";
import IconButton from "@app/components/IconButton.svelte";
import IconSmall from "@app/components/IconSmall.svelte";
import Markdown from "@app/components/Markdown.svelte";
import NodeId from "./NodeId.svelte";
import NodeId from "@app/components/NodeId.svelte";
import ReactionSelector from "@app/components/ReactionSelector.svelte";
import Reactions from "@app/components/Reactions.svelte";
import Textarea from "@app/components/Textarea.svelte";
export let id: string | undefined = undefined;
export let authorId: string;
export let authorAlias: string | undefined = undefined;
export let body: string;
export let reactions: [string, string][];
export let action: "create" | "view" = "view";
export let caption = "commented";
export let rawPath: string;
export let timestamp: number;
export let isReply: boolean = false;
export let isLastReply: boolean = false;
// TODO: Remove flag once `radicle-httpd` fixes embed editing
export let disableEdit: boolean = false;
let editInProgress = false;
const dispatch = createEventDispatcher<{
react: { nids: string[]; id: string; reaction: string };
edit: string;
}>();
$: groupedReactions = reactions?.reduce(
@@ -64,13 +70,18 @@
}
.timestamp {
color: var(--color-fill-gray);
margin-left: auto;
font-size: var(--font-size-small);
}
.header-right {
display: flex;
margin-left: auto;
gap: 0.5rem;
}
.card-body {
word-wrap: break-word;
font-size: var(--font-size-small);
padding-left: 2rem;
padding-right: 2rem;
}
.actions {
display: flex;
@@ -80,6 +91,10 @@
padding-left: 2rem;
height: 22px;
}
.edit-buttons {
display: flex;
gap: 0.25rem;
}
.reply .card-body,
.reply .actions {
padding-left: 1rem;
@@ -108,30 +123,50 @@
</div>
<NodeId nodeId={authorId} alias={authorAlias} />
{caption}
<div class="timestamp" title={utils.absoluteTimestamp(timestamp)}>
{utils.formatTimestamp(timestamp)}
<div class="header-right">
{#if id && $authenticated && !editInProgress && !disableEdit}
<div class="edit-buttons">
<IconButton
title="edit comment"
on:click={() => (editInProgress = true)}>
<IconSmall name={"edit"} />
</IconButton>
</div>
{/if}
<div class="timestamp" title={utils.absoluteTimestamp(timestamp)}>
{utils.formatTimestamp(timestamp)}
</div>
</div>
</div>
</div>
<div class="card-body">
{#if action === "create"}
<Textarea
resizable
bind:value={body}
on:submit
placeholder="Leave a comment" />
{#if editInProgress}
<ExtendedTextarea
{body}
enableAttachments
submitCaption="Save"
placeholder="Leave your description"
on:submit={({ detail: { comment } }) => {
editInProgress = false;
dispatch("edit", comment);
}}
on:close={async () => {
body = body;
await tick();
editInProgress = false;
}} />
{:else if body.trim() === ""}
<span class="txt-missing">No description</span>
{:else}
<Markdown {rawPath} content={body} />
{/if}
</div>
{#if (id && $httpdStore.state === "authenticated") || (id && groupedReactions.size > 0)}
{#if (id && $authenticated) || (id && groupedReactions.size > 0)}
<div class="actions">
{#if id && $httpdStore.state === "authenticated"}
{#if id && $authenticated}
<ReactionSelector
nid={$httpdStore.session.publicKey}
nid={$authenticated.session.publicKey}
reactions={groupedReactions}
on:select={event => {
if (id) {
@@ -141,7 +176,7 @@
{/if}
{#if id && groupedReactions.size > 0}
<Reactions
clickable={$httpdStore.state === "authenticated"}
clickable={Boolean($authenticated)}
reactions={groupedReactions}
on:remove={event => {
if (id) {
+62
View File
@@ -0,0 +1,62 @@
<script lang="ts">
import type { Embed } from "@app/lib/file";
import { createEventDispatcher } from "svelte";
import ExtendedTextarea from "@app/components/ExtendedTextarea.svelte";
const dispatch = createEventDispatcher<{
submit: {
comment: string;
embeds: Embed[];
};
}>();
export let body: string | undefined = undefined;
export let placeholder: string | undefined = undefined;
export let submitCaption: string | undefined = undefined;
export let enableAttachments: boolean = false;
export let inline: boolean = false;
export let focus: boolean = false;
export let submitInProgress: boolean = false;
let active: boolean = false;
</script>
<style>
.inactive {
box-shadow: 0 0 0 1px var(--color-border-hint);
border-radius: var(--border-radius-small);
padding: 0.5rem 0.75rem;
background-color: var(--color-background-dip);
font-size: var(--font-size-small);
color: var(--color-fill-gray);
cursor: text;
}
.inactive:hover {
box-shadow: 0 0 0 1px var(--color-border-default);
}
</style>
{#if active}
<ExtendedTextarea
{inline}
{placeholder}
{submitCaption}
{submitInProgress}
{focus}
{body}
{enableAttachments}
on:close={() => (active = false)}
on:submit={event => {
active = false;
dispatch("submit", event.detail);
}} />
{:else}
<!-- svelte-ignore a11y-click-events-have-key-events -->
<div
class="inactive"
role="button"
tabindex="0"
on:click={() => (active = true)}>
{placeholder}
</div>
{/if}
@@ -1,5 +1,5 @@
<script lang="ts" strictEvents>
import type { Embed } from "@app/lib/file";
import type { Embed, EmbedWithOid } from "@app/lib/file";
import { createEventDispatcher } from "svelte";
@@ -11,31 +11,34 @@
import Button from "./Button.svelte";
import IconSmall from "./IconSmall.svelte";
import Loading from "./Loading.svelte";
import Markdown from "./Markdown.svelte";
import Radio from "./Radio.svelte";
import Textarea from "./Textarea.svelte";
export let enableAttachments: boolean = false;
export let placeholder: string = "Leave your comment";
export let submitCaption: string = "Comment";
export let focus: boolean = false;
export let inline: boolean = false;
export let body: string = "";
export let submitInProgress: boolean = false;
let commentBody: string = "";
let active: boolean = false;
let preview: boolean = false;
let newEmbeds: Embed[] = [];
let newEmbeds: EmbedWithOid[] = [];
let selectionStart = 0;
let selectionEnd = 0;
const dispatch = createEventDispatcher<{
submit: { comment: string; embeds: Embed[] };
close: null;
click: null;
}>();
function submit() {
dispatch("submit", { comment: commentBody, embeds: newEmbeds });
dispatch("submit", { comment: body, embeds: newEmbeds });
preview = false;
newEmbeds = [];
active = false;
}
const MAX_BLOB_SIZE = 4_194_304;
@@ -73,9 +76,9 @@
},
];
const embedText = `![${embed.name}](${embed.oid})\n`;
commentBody = commentBody
body = body
.slice(0, selectionStart)
.concat(embedText, commentBody.slice(selectionEnd));
.concat(embedText, body.slice(selectionEnd));
selectionStart += embedText.length;
selectionEnd = selectionStart;
}),
@@ -93,6 +96,7 @@
flex-direction: column;
align-items: flex-start;
gap: 1rem;
width: 100%;
}
.inline {
border: 0;
@@ -120,90 +124,71 @@
margin-left: 1px;
margin-top: 1px;
}
.inactive {
box-shadow: 0 0 0 1px var(--color-border-hint);
border-radius: var(--border-radius-small);
padding: 0.5rem 0.75rem;
background-color: var(--color-background-dip);
font-size: var(--font-size-small);
color: var(--color-fill-gray);
cursor: text;
}
.inactive:hover {
box-shadow: 0 0 0 1px var(--color-border-default);
}
</style>
{#if active}
<div class="comment-section" class:inline>
<Radio>
<div class="comment-section" class:inline>
<Radio>
<Button
styleBorderRadius="0"
variant={!preview ? "secondary" : "gray"}
on:click={() => {
preview = false;
}}>
<IconSmall name="edit" />
Edit
</Button>
<Button
styleBorderRadius="0"
disabled={body === ""}
variant={preview ? "secondary" : "gray"}
on:click={() => {
preview = true;
}}>
<IconSmall name="eye-open" />
Preview
</Button>
</Radio>
{#if preview}
<div class="preview">
<Markdown content={body} embeds={newEmbeds} />
</div>
{:else}
<Textarea
on:drop={handleFileDrop}
bind:selectionEnd
bind:selectionStart
{focus}
on:submit={submit}
bind:value={body}
{placeholder} />
{/if}
<div class="actions">
{#if !preview}
<div class="caption">
Markdown supported. {#if enableAttachments}Drop attachments into the
text area.{/if} Press {utils.isMac() ? "⌘" : "ctrl"}↵ to submit.
</div>
{/if}
<div class="buttons">
<Button
styleBorderRadius="0"
variant={!preview ? "secondary" : "gray"}
disabled={submitInProgress}
variant="outline"
on:click={() => {
preview = false;
dispatch("close");
}}>
<IconSmall name="edit" />
Edit
Cancel
</Button>
<Button
styleBorderRadius="0"
disabled={commentBody === ""}
variant={preview ? "secondary" : "gray"}
on:click={() => {
preview = true;
}}>
<IconSmall name="eye-open" />
Preview
variant="secondary"
disabled={!body || submitInProgress}
on:click={submit}>
{#if submitInProgress}
<Loading small noDelay />
{:else}
{submitCaption}
{/if}
</Button>
</Radio>
{#if preview}
<div class="preview">
<Markdown content={commentBody} embeds={newEmbeds} />
</div>
{:else}
<Textarea
on:drop={handleFileDrop}
bind:selectionEnd
bind:selectionStart
{focus}
on:submit={submit}
bind:value={commentBody}
{placeholder} />
{/if}
<div class="actions">
{#if !preview}
<div class="caption">
Markdown supported. {#if enableAttachments}Drop attachments into the
text area.{/if} Press {utils.isMac() ? "⌘" : "ctrl"}↵ to submit.
</div>
{/if}
<div class="buttons">
<Button
variant="outline"
on:click={() => {
preview = false;
active = false;
}}>
Cancel
</Button>
<Button variant="secondary" disabled={!commentBody} on:click={submit}>
Comment
</Button>
</div>
</div>
</div>
{:else}
<!-- svelte-ignore a11y-click-events-have-key-events -->
<div
class="inactive"
role="button"
tabindex="0"
on:click={() => {
commentBody = "";
active = true;
dispatch("click");
}}>
{placeholder}
</div>
{/if}
</div>
+17 -10
View File
@@ -1,6 +1,9 @@
<script lang="ts">
import Loading from "./Loading.svelte";
export let title: string | undefined = undefined;
export let ariaLabel: string | undefined = undefined;
export let loading: boolean = false;
</script>
<style>
@@ -23,13 +26,17 @@
}
</style>
<!-- svelte-ignore a11y-click-events-have-key-events -->
<div
role="button"
tabindex="0"
aria-label={ariaLabel}
{title}
class="button"
on:click>
<slot />
</div>
{#if loading}
<Loading small noDelay />
{:else}
<!-- svelte-ignore a11y-click-events-have-key-events -->
<div
role="button"
tabindex="0"
aria-label={ariaLabel}
{title}
class="button"
on:click>
<slot />
</div>
{/if}
+4 -2
View File
@@ -1,4 +1,6 @@
<script lang="ts">
import type { EmbedWithOid } from "@app/lib/file";
import dompurify from "dompurify";
import matter from "@radicle/gray-matter";
import { afterUpdate } from "svelte";
@@ -15,7 +17,7 @@
canonicalize,
isCommit,
} from "@app/lib/utils";
import { mimes, type Embed } from "@app/lib/file";
import { mimes } from "@app/lib/file";
export let content: string;
// If present, resolve all relative links with respect to this URL
@@ -24,7 +26,7 @@
export let rawPath: string | undefined = undefined;
// If present, means we are in a preview context,
// use this for image previews instead of /raw URLs.
export let embeds: Embed[] | undefined = undefined;
export let embeds: EmbedWithOid[] | undefined = undefined;
$: doc = matter(content);
$: frontMatter = Object.entries(doc.data).filter(
+10 -3
View File
@@ -6,12 +6,12 @@
import * as utils from "@app/lib/utils";
import CommentComponent from "@app/components/Comment.svelte";
import CommentTextarea from "./CommentTextarea.svelte";
import CommentToggleInput from "@app/components/CommentToggleInput.svelte";
import IconSmall from "./IconSmall.svelte";
export let thread: { root: Comment; replies: Comment[] };
export let rawPath: string;
export let enableAttachments: boolean;
export let enableAttachments: boolean = false;
async function toggleReply() {
// This tick allows the DOM to update before scrolling.
@@ -28,6 +28,7 @@
embeds: { name: string; content: string }[];
body: string;
};
editComment: { id: string; body: string };
react: { nids: string[]; commentId: string | undefined; reaction: string };
cancel: never;
}>();
@@ -70,7 +71,10 @@
authorAlias={root.author.alias}
reactions={root.reactions}
timestamp={root.timestamp}
disableEdit={root.embeds.length > 0}
body={root.body}
on:edit={event =>
dispatch("editComment", { id: root.id, body: event.detail })}
on:react>
<IconSmall name="chat" slot="icon" />
</CommentComponent>
@@ -88,14 +92,17 @@
isLastReply={replies[replies.length - 1] === reply}
reactions={reply.reactions}
timestamp={reply.timestamp}
disableEdit={reply.embeds.length > 0}
body={reply.body}
on:edit={event =>
dispatch("editComment", { id: reply.id, body: event.detail })}
on:react />
{/each}
</div>
{/if}
{#if $httpdStore.state === "authenticated"}
<div id={`reply-${root.id}`} class="reply">
<CommentTextarea
<CommentToggleInput
inline
placeholder="Reply to comment"
on:click={toggleReply}