mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-29 18:20:30 +08:00
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:
@@ -0,0 +1,194 @@
|
||||
<script lang="ts" strictEvents>
|
||||
import type { Embed, EmbedWithOid } from "@app/lib/file";
|
||||
|
||||
import { createEventDispatcher } from "svelte";
|
||||
|
||||
import * as modal from "@app/lib/modal";
|
||||
import * as utils from "@app/lib/utils";
|
||||
import { embed } from "@app/lib/file";
|
||||
|
||||
import ErrorModal from "@app/modals/ErrorModal.svelte";
|
||||
|
||||
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 preview: boolean = false;
|
||||
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: body, embeds: newEmbeds });
|
||||
preview = false;
|
||||
newEmbeds = [];
|
||||
}
|
||||
|
||||
const MAX_BLOB_SIZE = 4_194_304;
|
||||
|
||||
function handleFileDrop(event: DragEvent) {
|
||||
if (!enableAttachments) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
if (event.dataTransfer) {
|
||||
const embeds = Array.from(event.dataTransfer.files).map(embed);
|
||||
void Promise.all(embeds).then(embeds =>
|
||||
embeds.forEach(embed => {
|
||||
if (embed.content.length > MAX_BLOB_SIZE) {
|
||||
modal.show({
|
||||
component: ErrorModal,
|
||||
props: {
|
||||
title: "File too large",
|
||||
subtitle: [
|
||||
"The file you tried to upload is too large.",
|
||||
"The maximum file size is 4MB.",
|
||||
],
|
||||
error: { message: `File ${embed.name} is too large` },
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
newEmbeds = [
|
||||
...newEmbeds,
|
||||
{
|
||||
oid: embed.oid,
|
||||
name: embed.name,
|
||||
content: embed.content,
|
||||
},
|
||||
];
|
||||
const embedText = `\n`;
|
||||
body = body
|
||||
.slice(0, selectionStart)
|
||||
.concat(embedText, body.slice(selectionEnd));
|
||||
selectionStart += embedText.length;
|
||||
selectionEnd = selectionStart;
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.comment-section {
|
||||
border: 1px solid var(--color-border-hint);
|
||||
padding: 1rem;
|
||||
border-radius: var(--border-radius-small);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: 1rem;
|
||||
width: 100%;
|
||||
}
|
||||
.inline {
|
||||
border: 0;
|
||||
padding: 0;
|
||||
}
|
||||
.actions {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
width: 100%;
|
||||
}
|
||||
.buttons {
|
||||
display: flex;
|
||||
margin-left: auto;
|
||||
gap: 1rem;
|
||||
}
|
||||
.caption {
|
||||
font-size: var(--font-size-small);
|
||||
color: var(--color-fill-gray);
|
||||
}
|
||||
.preview {
|
||||
font-size: var(--font-size-small);
|
||||
min-height: 6.375rem;
|
||||
padding: 0.75rem;
|
||||
margin-left: 1px;
|
||||
margin-top: 1px;
|
||||
}
|
||||
</style>
|
||||
|
||||
<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
|
||||
disabled={submitInProgress}
|
||||
variant="outline"
|
||||
on:click={() => {
|
||||
preview = false;
|
||||
dispatch("close");
|
||||
}}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
variant="secondary"
|
||||
disabled={!body || submitInProgress}
|
||||
on:click={submit}>
|
||||
{#if submitInProgress}
|
||||
<Loading small noDelay />
|
||||
{:else}
|
||||
{submitCaption}
|
||||
{/if}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user