From bed7495a67b69c0e3912c4c27e71f594405321b4 Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Wed, 19 Oct 2022 22:45:03 +0800 Subject: [PATCH] feat(nc-gui): cover old logic for non streaming import --- .../nc-gui/components/dlg/QuickImport.vue | 66 ++++++++++--------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/packages/nc-gui/components/dlg/QuickImport.vue b/packages/nc-gui/components/dlg/QuickImport.vue index 2f24557d4b..d81e0fbef0 100644 --- a/packages/nc-gui/components/dlg/QuickImport.vue +++ b/packages/nc-gui/components/dlg/QuickImport.vue @@ -22,6 +22,7 @@ import { useProject, useVModel, } from '#imports' +import type { importFileList, streamImportFileList } from '~/lib' interface Props { modelValue: boolean @@ -60,9 +61,7 @@ const isParsingData = ref(false) const useForm = Form.useForm const importState = reactive({ - // TODO(import): remove - // fileList: [] as (UploadFile & { data: string | ArrayBuffer })[], - fileList: [] as UploadFile[], + fileList: [] as importFileList | streamImportFileList, url: '', jsonEditor: {}, parserConfig: { @@ -147,8 +146,11 @@ async function handlePreImport() { isParsingData.value = true if (activeKey.value === 'uploadTab') { - // TODO(import): update - await parseAndExtractData2(importState.fileList) + if (isImportTypeCsv) { + await parseAndExtractStreamData(importState.fileList as streamImportFileList) + } else { + await parseAndExtractData((importState.fileList as importFileList)[0].data) + } } else if (activeKey.value === 'urlTab') { try { await validate() @@ -183,8 +185,7 @@ async function handleImport() { dialogShow.value = false } -// papaparse experiment -async function parseAndExtractData2(val: UploadFile[]) { +async function parseAndExtractStreamData(val: UploadFile[]) { try { templateData.value = null importData.value = null @@ -248,31 +249,32 @@ function rejectDrop(fileList: UploadFile[]) { function handleChange(info: UploadChangeParam) { const status = info.file.status if (status && status !== 'uploading' && status !== 'removed') { - // const reader = new FileReader() - // reader.onload = (e: ProgressEvent) => { - // const target = importState.fileList.find((f) => f.uid === info.file.uid) - // if (e.target && e.target.result) { - // /** if the file was pushed into the list by `` we just add the data to the file */ - // if (target) { - // target.data = e.target.result - // } else if (!target) { - // /** if the file was added programmatically and not with d&d, we create file infos and push it into the list */ - // importState.fileList.push({ - // ...info.file, - // status: 'done', - // data: e.target.result, - // }) - // } - // } - // } - // reader.readAsArrayBuffer(info.file.originFileObj!) - - if (!importState.fileList.find((f) => f.uid === info.file.uid)) { - /** if the file was added programmatically and not with d&d, we create file infos and push it into the list */ - importState.fileList.push({ - ...info.file, - status: 'done', - }) + if (isImportTypeCsv) { + if (!importState.fileList.find((f) => f.uid === info.file.uid)) { + ;(importState.fileList as streamImportFileList).push({ + ...info.file, + status: 'done', + }) + } + } else { + const reader = new FileReader() + reader.onload = (e: ProgressEvent) => { + const target = (importState.fileList as importFileList).find((f) => f.uid === info.file.uid) + if (e.target && e.target.result) { + /** if the file was pushed into the list by `` we just add the data to the file */ + if (target) { + target.data = e.target.result + } else if (!target) { + /** if the file was added programmatically and not with d&d, we create file infos and push it into the list */ + importState.fileList.push({ + ...info.file, + status: 'done', + data: e.target.result, + }) + } + } + } + reader.readAsArrayBuffer(info.file.originFileObj!) } }