Allow to attach multiple images to post

This commit is contained in:
silverpill 2022-09-25 23:30:47 +00:00
parent a2744288a5
commit f0dad3a71c
2 changed files with 17 additions and 11 deletions

View file

@ -160,18 +160,18 @@ export interface PostData {
in_reply_to_id: string | null; in_reply_to_id: string | null;
visibility: string; visibility: string;
mentions: string[]; mentions: string[];
attachments: Attachment[];
} }
export async function createPost( export async function createPost(
authToken: string, authToken: string,
postData: PostData, postData: PostData,
attachment: Attachment | null,
): Promise<Post> { ): Promise<Post> {
const url = `${BACKEND_URL}/api/v1/statuses` const url = `${BACKEND_URL}/api/v1/statuses`
// Convert to Mastodon API Status entity // Convert to Mastodon API Status entity
const statusData = { const statusData = {
status: postData.content, 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, in_reply_to_id: postData.in_reply_to_id,
visibility: postData.visibility, visibility: postData.visibility,
mentions: postData.mentions, mentions: postData.mentions,

View file

@ -16,12 +16,16 @@
required required
:placeholder="inReplyTo ? 'Your reply' : 'What\'s on your mind?'" :placeholder="inReplyTo ? 'Your reply' : 'What\'s on your mind?'"
></textarea> ></textarea>
<div v-if="attachment" class="attachments"> <div v-if="attachments.length > 0" class="attachments">
<div class="attachment"> <div
v-for="(attachment, index) in attachments"
class="attachment"
:key="attachment.id"
>
<button <button
class="remove-attachment" class="remove-attachment"
title="Remove attachment" title="Remove attachment"
@click.prevent="removeAttachment()" @click.prevent="removeAttachment(index)"
> >
<img :src="require('@/assets/feather/x.svg')"> <img :src="require('@/assets/feather/x.svg')">
</button> </button>
@ -136,7 +140,7 @@ const postFormContentRef = $ref<HTMLTextAreaElement | null>(null)
const attachmentUploadInputRef = $ref<HTMLInputElement | null>(null) const attachmentUploadInputRef = $ref<HTMLInputElement | null>(null)
let content = $ref("") let content = $ref("")
let attachment = $ref<Attachment | null>(null) let attachments = $ref<Attachment[]>([])
let visibility = $ref(Visibility.Public) let visibility = $ref(Visibility.Public)
let visibilityMenuVisible = $ref(false) let visibilityMenuVisible = $ref(false)
@ -182,14 +186,15 @@ async function onAttachmentUpload(event: Event) {
} }
const imageDataUrl = await fileToDataUrl(files[0]) const imageDataUrl = await fileToDataUrl(files[0])
const imageBase64 = dataUrlToBase64(imageDataUrl) const imageBase64 = dataUrlToBase64(imageDataUrl)
attachment = await uploadAttachment( const attachment = await uploadAttachment(
ensureAuthToken(), ensureAuthToken(),
imageBase64, imageBase64,
) )
attachments.push(attachment)
} }
function removeAttachment() { function removeAttachment(index: number) {
attachment = null attachments.splice(index, 1)
} }
function toggleVisibilityMenu() { function toggleVisibilityMenu() {
@ -218,6 +223,7 @@ async function publish() {
in_reply_to_id: props.inReplyTo ? props.inReplyTo.id : null, in_reply_to_id: props.inReplyTo ? props.inReplyTo.id : null,
visibility: visibility, visibility: visibility,
mentions: [], mentions: [],
attachments: attachments,
} }
isLoading = true isLoading = true
let post let post
@ -225,7 +231,6 @@ async function publish() {
post = await createPost( post = await createPost(
ensureAuthToken(), ensureAuthToken(),
postData, postData,
attachment,
) )
} catch (error: any) { } catch (error: any) {
errorMessage = error.message errorMessage = error.message
@ -235,7 +240,7 @@ async function publish() {
// Refresh editor // Refresh editor
errorMessage = null errorMessage = null
isLoading = false isLoading = false
attachment = null attachments = []
content = "" content = ""
if (postFormContentRef) { if (postFormContentRef) {
await nextTick() await nextTick()
@ -287,6 +292,7 @@ textarea {
} }
.attachment { .attachment {
display: flex;
position: relative; position: relative;
.remove-attachment { .remove-attachment {