From 4210f86fbe626f84412727aff50299bc9d150a7a Mon Sep 17 00:00:00 2001 From: "stephan.strehler" Date: Thu, 28 Mar 2024 16:48:01 +0100 Subject: [PATCH 1/4] #920: add reader support for file input --- .../src/components/DwfFilePreview.vue | 58 +++++-------------- .../src/components/DwfMultiFileInput.vue | 34 +++++------ .../src/middleware/fileSize.ts | 13 +++++ .../src/middleware/url.ts | 17 ++++++ 4 files changed, 59 insertions(+), 63 deletions(-) create mode 100644 digiwf-apps/packages/components/digiwf-multi-file-input/src/middleware/fileSize.ts create mode 100644 digiwf-apps/packages/components/digiwf-multi-file-input/src/middleware/url.ts diff --git a/digiwf-apps/packages/components/digiwf-multi-file-input/src/components/DwfFilePreview.vue b/digiwf-apps/packages/components/digiwf-multi-file-input/src/components/DwfFilePreview.vue index 666fb204b5..69b5838f38 100644 --- a/digiwf-apps/packages/components/digiwf-multi-file-input/src/components/DwfFilePreview.vue +++ b/digiwf-apps/packages/components/digiwf-multi-file-input/src/components/DwfFilePreview.vue @@ -1,10 +1,9 @@ @@ -52,6 +53,8 @@ import {computed, defineComponent} from "vue"; import {fileIcons} from "../util"; +import {formatBytes} from "@/middleware/fileSize"; +import {createBlobUrl} from "@/middleware/url"; export default defineComponent({ props: ['document', 'readonly'], @@ -63,47 +66,11 @@ export default defineComponent({ const icon = computed(() => fileIcons[props.document.type] ?? "mdi-file"); const isImage = computed(() => props.document.type.toLowerCase() === "image/jpeg" || props.document.type.toLowerCase() === "image/png"); - - const blobUrl = (): string => { - const byteCharacters = calcByteCharacters.value; - - const byteArrays: Uint8Array[] = []; - - for (let offset = 0; offset < byteCharacters.length; offset += 1024) { - const slice = byteCharacters.slice(offset, offset + 1024); - - const byteNumbers = new Array(slice.length); - for (let i = 0; i < slice.length; i++) { - byteNumbers[i] = slice.charCodeAt(i); - } - const byteArray = new Uint8Array(byteNumbers); - byteArrays.push(byteArray); - } - const blob = new Blob(byteArrays, {type: props.document.type}); - return URL.createObjectURL(blob); - } + const isPdf = computed(() => props.document.type === 'application/pdf') const openInTab = () => { - const calcVlobUrl = blobUrl(); - const link = document.createElement("a"); - link.href = calcVlobUrl; - link.setAttribute("download", props.document.name!); - document.body.appendChild(link); - link.click(); - } - - const formatBytes = (decimals = 2) => { - const k = 1024; - const dm = decimals < 0 ? 0 : decimals; - const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; - - const i = Math.floor(Math.log(props.document.size) / Math.log(k)); - - return ( - parseFloat((props.document.size / Math.pow(k, i)).toFixed(dm)) + - " " + - sizes[i] - ); + const url = createBlobUrl(calcByteCharacters.value, props.document.type); + window.open(url, "_blank" ) } const removeDocument = () => { @@ -114,6 +81,7 @@ export default defineComponent({ calcByteCharacters, icon, isImage, + isPdf, openInTab, formatBytes, removeDocument diff --git a/digiwf-apps/packages/components/digiwf-multi-file-input/src/components/DwfMultiFileInput.vue b/digiwf-apps/packages/components/digiwf-multi-file-input/src/components/DwfMultiFileInput.vue index f1519578b3..e92011d54f 100644 --- a/digiwf-apps/packages/components/digiwf-multi-file-input/src/components/DwfMultiFileInput.vue +++ b/digiwf-apps/packages/components/digiwf-multi-file-input/src/components/DwfMultiFileInput.vue @@ -17,15 +17,16 @@ type="file" v-bind="schema['x-props']" @change="changeInput" + aria-label="Datei hochladen" >