From f0dad3a71c26222ed791e059d64e3b1dcd9f9a90 Mon Sep 17 00:00:00 2001 From: silverpill Date: Sun, 25 Sep 2022 23:30:47 +0000 Subject: [PATCH] Allow to attach multiple images to post --- src/api/posts.ts | 4 ++-- src/components/PostEditor.vue | 24 +++++++++++++++--------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/api/posts.ts b/src/api/posts.ts index 1857a1e..5e18cd7 100644 --- a/src/api/posts.ts +++ b/src/api/posts.ts @@ -160,18 +160,18 @@ export interface PostData { in_reply_to_id: string | null; visibility: string; mentions: string[]; + attachments: Attachment[]; } export async function createPost( authToken: string, postData: PostData, - attachment: Attachment | null, ): Promise { const url = `${BACKEND_URL}/api/v1/statuses` // Convert to Mastodon API Status entity const statusData = { status: postData.content, - "media_ids[]": attachment ? [attachment.id] : null, + "media_ids[]": postData.attachments.map((attachment) => attachment.id), in_reply_to_id: postData.in_reply_to_id, visibility: postData.visibility, mentions: postData.mentions, diff --git a/src/components/PostEditor.vue b/src/components/PostEditor.vue index 868e97e..e5171f3 100644 --- a/src/components/PostEditor.vue +++ b/src/components/PostEditor.vue @@ -16,12 +16,16 @@ required :placeholder="inReplyTo ? 'Your reply' : 'What\'s on your mind?'" > -
-
+
+
@@ -136,7 +140,7 @@ const postFormContentRef = $ref(null) const attachmentUploadInputRef = $ref(null) let content = $ref("") -let attachment = $ref(null) +let attachments = $ref([]) let visibility = $ref(Visibility.Public) let visibilityMenuVisible = $ref(false) @@ -182,14 +186,15 @@ async function onAttachmentUpload(event: Event) { } const imageDataUrl = await fileToDataUrl(files[0]) const imageBase64 = dataUrlToBase64(imageDataUrl) - attachment = await uploadAttachment( + const attachment = await uploadAttachment( ensureAuthToken(), imageBase64, ) + attachments.push(attachment) } -function removeAttachment() { - attachment = null +function removeAttachment(index: number) { + attachments.splice(index, 1) } function toggleVisibilityMenu() { @@ -218,6 +223,7 @@ async function publish() { in_reply_to_id: props.inReplyTo ? props.inReplyTo.id : null, visibility: visibility, mentions: [], + attachments: attachments, } isLoading = true let post @@ -225,7 +231,6 @@ async function publish() { post = await createPost( ensureAuthToken(), postData, - attachment, ) } catch (error: any) { errorMessage = error.message @@ -235,7 +240,7 @@ async function publish() { // Refresh editor errorMessage = null isLoading = false - attachment = null + attachments = [] content = "" if (postFormContentRef) { await nextTick() @@ -287,6 +292,7 @@ textarea { } .attachment { + display: flex; position: relative; .remove-attachment {