From e1cdd7636a2528bbef377469f0eafa5569324a05 Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Thu, 28 Sep 2023 17:50:37 +0700 Subject: [PATCH] UBER-924: Fix file upload progress (#3757) Signed-off-by: Andrey Sobolev --- packages/ui/src/components/Loading.svelte | 12 ++++ .../src/components/AttachmentPopup.svelte | 19 +++++-- .../src/components/AttachmentPresenter.svelte | 10 +++- .../src/components/AttachmentRefInput.svelte | 36 +++++++++--- .../AttachmentStyleBoxEditor.svelte | 3 +- .../src/components/AttachmentStyledBox.svelte | 55 ++++++++++++++++--- .../src/components/NewMessage.svelte | 9 ++- 7 files changed, 118 insertions(+), 26 deletions(-) diff --git a/packages/ui/src/components/Loading.svelte b/packages/ui/src/components/Loading.svelte index 47cab768af..50620b562d 100644 --- a/packages/ui/src/components/Loading.svelte +++ b/packages/ui/src/components/Loading.svelte @@ -14,10 +14,22 @@ // limitations under the License. -->
diff --git a/plugins/attachment-resources/src/components/AttachmentPopup.svelte b/plugins/attachment-resources/src/components/AttachmentPopup.svelte index ead79c9ca0..e3ba9921ae 100644 --- a/plugins/attachment-resources/src/components/AttachmentPopup.svelte +++ b/plugins/attachment-resources/src/components/AttachmentPopup.svelte @@ -17,7 +17,7 @@ import type { Doc } from '@hcengineering/core' import { Attachment } from '@hcengineering/attachment' import { createQuery, getClient } from '@hcengineering/presentation' - import { ActionIcon, IconAdd, Label } from '@hcengineering/ui' + import { ActionIcon, IconAdd, Label, Loading } from '@hcengineering/ui' import { AttachmentPresenter } from '..' import attachment from '../plugin' import { uploadFile } from '../utils' @@ -31,6 +31,8 @@ let docs: Attachment[] = [] + let progress = false + const query = createQuery() $: query.query( attachment.class.Attachment, @@ -59,14 +61,19 @@ }) } - function fileSelected () { + async function fileSelected (): Promise { + progress = true + const list = inputFile.files if (list === null || list.length === 0) return for (let index = 0; index < list.length; index++) { const file = list.item(index) - if (file !== null) createAttachment(file) + if (file !== null) { + await createAttachment(file) + } } inputFile.value = '' + progress = false } let inputFile: HTMLInputElement @@ -94,7 +101,11 @@
{#if canAdd}
- + {#if progress} + + {:else} + + {/if}
{/if} diff --git a/plugins/attachment-resources/src/components/AttachmentPresenter.svelte b/plugins/attachment-resources/src/components/AttachmentPresenter.svelte index 4d02a2bb82..16de02d8c1 100644 --- a/plugins/attachment-resources/src/components/AttachmentPresenter.svelte +++ b/plugins/attachment-resources/src/components/AttachmentPresenter.svelte @@ -16,7 +16,7 @@ @@ -263,8 +276,13 @@ on:dragleave={() => {}} on:drop|preventDefault|stopPropagation={fileDrop} > - {#if attachments.size} + {#if attachments.size || progress}
+ {#if progress} +
+ +
+ {/if} {#each Array.from(attachments.values()) as attachment}
descriptionBox.saveNewAttachment(e.detail)} - on:detached={(e) => descriptionBox.removeAttachmentById(e.detail)} + useDirectAttachDelete showButtons on:blur={() => save(object, description)} on:changeContent={triggerSave} diff --git a/plugins/attachment-resources/src/components/AttachmentStyledBox.svelte b/plugins/attachment-resources/src/components/AttachmentStyledBox.svelte index 28c5d8e6bb..a148392707 100644 --- a/plugins/attachment-resources/src/components/AttachmentStyledBox.svelte +++ b/plugins/attachment-resources/src/components/AttachmentStyledBox.svelte @@ -25,6 +25,7 @@ import AttachmentPresenter from './AttachmentPresenter.svelte' import AttachmentPreview from './AttachmentPreview.svelte' import { ListSelectionProvider, SelectDirection } from '@hcengineering/view-resources' + import Loading from '@hcengineering/ui/src/components/Loading.svelte' export let objectId: Ref | undefined = undefined export let space: Ref | undefined = undefined @@ -46,6 +47,10 @@ export let enableBackReferences: boolean = false export let isScrollable = true + export let useDirectAttachDelete = false + + let progress = false + let draftKey = objectId ? `${objectId}_attachments` : undefined $: draftKey = objectId ? `${objectId}_attachments` : undefined @@ -164,6 +169,10 @@ saveDraft() dispatch('attach', { action: 'saved', value: attachments.size }) dispatch('attached', _id) + + if (useDirectAttachDelete) { + saveNewAttachment(_id) + } return { file: uuid, type: file.type } } catch (err: any) { setPlatformStatus(unknownError(err)) @@ -176,33 +185,48 @@ await client.addCollection(attachment.class.Attachment, space, objectId, _class, 'attachments', doc, doc._id) } - function fileSelected () { + async function fileSelected (): Promise { + progress = true const list = inputFile.files if (list === null || list.length === 0) return for (let index = 0; index < list.length; index++) { const file = list.item(index) - if (file !== null) createAttachment(file) + if (file !== null) { + await createAttachment(file) + } } inputFile.value = '' + progress = false } - export function fileDrop (e: DragEvent) { + export async function fileDrop (e: DragEvent): Promise { + progress = true const list = e.dataTransfer?.files if (list !== undefined && list.length !== 0) { for (let index = 0; index < list.length; index++) { const file = list.item(index) - if (file !== null) createAttachment(file) + if (file !== null) { + await createAttachment(file) + } } } + progress = false } async function removeAttachment (attachment: Attachment): Promise { + if (useDirectAttachDelete) { + progressItems.push(attachment._id) + progressItems = progressItems + await deleteAttachment(attachment) + } removedAttachments.add(attachment) attachments.delete(attachment._id) attachments = attachments refInput.removeAttachment(attachment.file) saveDraft() dispatch('detached', attachment._id) + + progressItems = progressItems.filter((it) => it !== attachment._id) } async function deleteAttachment (attachment: Attachment): Promise { @@ -299,7 +323,7 @@ return false } - export function pasteAction (evt: ClipboardEvent): void { + export async function pasteAction (evt: ClipboardEvent): Promise { if (!isAllowedPaste(evt)) { return } @@ -310,7 +334,7 @@ if (item.kind === 'file') { const blob = item.getAsFile() if (blob !== null) { - createAttachment(blob) + await createAttachment(blob) } } } @@ -323,6 +347,9 @@ size: attachments.size, values: attachments.size === 0 ? true : attachments }) + + let element: HTMLElement + let progressItems: Ref[] = []
- {#if attachments.size && enableAttachments} + {#if (attachments.size && enableAttachments) || progress}
{#each Array.from(attachments.values()) as attachment, index}
@@ -384,13 +411,25 @@ value={attachment} removable showPreview + progress={progressItems.includes(attachment._id)} on:remove={(result) => { - if (result !== undefined) removeAttachment(attachment) + if (result !== undefined) { + removeAttachment(attachment) + } }} /> {/if}
{/each} + {#if progress} +
+ { + element.scrollIntoView({ behavior: 'auto', block: 'center', inline: 'center' }) + }} + /> +
+ {/if}
{/if}
diff --git a/plugins/gmail-resources/src/components/NewMessage.svelte b/plugins/gmail-resources/src/components/NewMessage.svelte index 3c72349a52..2fce00edbf 100644 --- a/plugins/gmail-resources/src/components/NewMessage.svelte +++ b/plugins/gmail-resources/src/components/NewMessage.svelte @@ -36,6 +36,8 @@ const notificationClient = NotificationClientImpl.getClient() let objectId = generateId() + let progress = false + let copy: string = '' const obj: Data = { @@ -82,6 +84,7 @@ let inputFile: HTMLInputElement function fileSelected () { + progress = true const list = inputFile.files if (list === null || list.length === 0) return for (let index = 0; index < list.length; index++) { @@ -89,17 +92,20 @@ if (file !== null) createAttachment(file) } inputFile.value = '' + progress = false } function fileDrop (e: DragEvent) { e.preventDefault() e.stopPropagation() + progress = true const list = e.dataTransfer?.files if (list === undefined || list.length === 0) return for (let index = 0; index < list.length; index++) { const file = list.item(index) if (file !== null) createAttachment(file) } + progress = false } async function createAttachment (file: File) { @@ -187,11 +193,12 @@