mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
416 lines
12 KiB
416 lines
12 KiB
2 years ago
|
<script setup lang="ts">
|
||
2 years ago
|
import { Form, message } from 'ant-design-vue'
|
||
2 years ago
|
import type { TableType } from 'nocodb-sdk'
|
||
2 years ago
|
import type { UploadChangeParam, UploadFile } from 'ant-design-vue'
|
||
2 years ago
|
import {
|
||
|
ExcelTemplateAdapter,
|
||
|
ExcelUrlTemplateAdapter,
|
||
|
JSONTemplateAdapter,
|
||
|
JSONUrlTemplateAdapter,
|
||
|
computed,
|
||
|
extractSdkResponseErrorMsg,
|
||
|
fieldRequiredValidator,
|
||
|
importCsvUrlValidator,
|
||
|
importExcelUrlValidator,
|
||
|
importUrlValidator,
|
||
|
reactive,
|
||
|
ref,
|
||
|
useI18n,
|
||
|
useProject,
|
||
|
useVModel,
|
||
|
} from '#imports'
|
||
2 years ago
|
|
||
|
interface Props {
|
||
|
modelValue: boolean
|
||
|
importType: 'csv' | 'json' | 'excel'
|
||
2 years ago
|
importOnly?: boolean
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
const { importType, importOnly = false, ...rest } = defineProps<Props>()
|
||
2 years ago
|
|
||
|
const emit = defineEmits(['update:modelValue'])
|
||
|
|
||
|
const { t } = useI18n()
|
||
|
|
||
2 years ago
|
const { tables } = useProject()
|
||
2 years ago
|
|
||
2 years ago
|
const activeKey = ref('uploadTab')
|
||
2 years ago
|
|
||
2 years ago
|
const jsonEditorRef = ref()
|
||
2 years ago
|
|
||
2 years ago
|
const templateEditorRef = ref()
|
||
2 years ago
|
|
||
2 years ago
|
const loading = ref(false)
|
||
2 years ago
|
|
||
2 years ago
|
const templateData = ref()
|
||
2 years ago
|
|
||
2 years ago
|
const importData = ref()
|
||
2 years ago
|
|
||
2 years ago
|
const importColumns = ref([])
|
||
|
|
||
2 years ago
|
const templateEditorModal = ref(false)
|
||
2 years ago
|
|
||
2 years ago
|
const useForm = Form.useForm
|
||
2 years ago
|
|
||
2 years ago
|
const importState = reactive({
|
||
2 years ago
|
fileList: [] as (UploadFile & { data: string | ArrayBuffer })[],
|
||
2 years ago
|
url: '',
|
||
|
jsonEditor: {},
|
||
|
parserConfig: {
|
||
|
maxRowsToParse: 500,
|
||
|
normalizeNested: true,
|
||
|
importData: true,
|
||
2 years ago
|
},
|
||
2 years ago
|
})
|
||
2 years ago
|
|
||
2 years ago
|
const isImportTypeJson = computed(() => importType === 'json')
|
||
2 years ago
|
|
||
2 years ago
|
const isImportTypeCsv = computed(() => importType === 'csv')
|
||
2 years ago
|
|
||
2 years ago
|
const IsImportTypeExcel = computed(() => importType === 'excel')
|
||
2 years ago
|
|
||
2 years ago
|
const validators = computed(() => ({
|
||
|
url: [fieldRequiredValidator, importUrlValidator, isImportTypeCsv.value ? importCsvUrlValidator : importExcelUrlValidator],
|
||
|
maxRowsToParse: [fieldRequiredValidator],
|
||
|
}))
|
||
2 years ago
|
|
||
2 years ago
|
const { validate, validateInfos } = useForm(importState, validators)
|
||
2 years ago
|
|
||
2 years ago
|
const importMeta = computed(() => {
|
||
2 years ago
|
if (IsImportTypeExcel.value) {
|
||
2 years ago
|
return {
|
||
2 years ago
|
header: `${t('title.quickImport')} - EXCEL`,
|
||
2 years ago
|
uploadHint: t('msg.info.excelSupport'),
|
||
|
urlInputLabel: t('msg.info.excelURL'),
|
||
|
loadUrlDirective: ['c:quick-import:excel:load-url'],
|
||
2 years ago
|
acceptTypes: '.xls, .xlsx, .xlsm, .ods, .ots',
|
||
2 years ago
|
}
|
||
2 years ago
|
} else if (isImportTypeCsv.value) {
|
||
2 years ago
|
return {
|
||
2 years ago
|
header: `${t('title.quickImport')} - CSV`,
|
||
2 years ago
|
uploadHint: '',
|
||
|
urlInputLabel: t('msg.info.csvURL'),
|
||
|
loadUrlDirective: ['c:quick-import:csv:load-url'],
|
||
2 years ago
|
acceptTypes: '.csv',
|
||
2 years ago
|
}
|
||
2 years ago
|
} else if (isImportTypeJson.value) {
|
||
2 years ago
|
return {
|
||
2 years ago
|
header: `${t('title.quickImport')} - JSON`,
|
||
2 years ago
|
uploadHint: '',
|
||
2 years ago
|
acceptTypes: '.json',
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
return {}
|
||
2 years ago
|
})
|
||
|
|
||
2 years ago
|
const dialogShow = useVModel(rest, 'modelValue', emit)
|
||
2 years ago
|
|
||
2 years ago
|
const disablePreImportButton = computed(() => {
|
||
|
if (activeKey.value === 'uploadTab') {
|
||
|
return !(importState.fileList.length > 0)
|
||
|
} else if (activeKey.value === 'urlTab') {
|
||
|
if (!validateInfos.url.validateStatus) return true
|
||
2 years ago
|
|
||
2 years ago
|
return validateInfos.url.validateStatus === 'error'
|
||
|
} else if (activeKey.value === 'jsonEditorTab') {
|
||
2 years ago
|
return !jsonEditorRef.value?.isValid
|
||
2 years ago
|
}
|
||
|
})
|
||
|
|
||
2 years ago
|
const disableImportButton = computed(() => !templateEditorRef.value?.isValid)
|
||
2 years ago
|
|
||
2 years ago
|
const disableFormatJsonButton = computed(() => !jsonEditorRef.value?.isValid)
|
||
2 years ago
|
|
||
2 years ago
|
const modalWidth = computed(() => {
|
||
2 years ago
|
if (importType === 'excel' && templateEditorModal.value) {
|
||
2 years ago
|
return 'max(90vw, 600px)'
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return 'max(60vw, 600px)'
|
||
|
})
|
||
|
|
||
2 years ago
|
async function handlePreImport() {
|
||
|
loading.value = true
|
||
2 years ago
|
|
||
2 years ago
|
if (activeKey.value === 'uploadTab') {
|
||
2 years ago
|
await parseAndExtractData(importState.fileList[0].data, importState.fileList[0].name)
|
||
2 years ago
|
} else if (activeKey.value === 'urlTab') {
|
||
|
try {
|
||
|
await validate()
|
||
2 years ago
|
|
||
2 years ago
|
await parseAndExtractData(importState.url, '')
|
||
|
} catch (e: any) {
|
||
2 years ago
|
message.error(await extractSdkResponseErrorMsg(e))
|
||
2 years ago
|
}
|
||
|
} else if (activeKey.value === 'jsonEditorTab') {
|
||
|
await parseAndExtractData(JSON.stringify(importState.jsonEditor), '')
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
loading.value = false
|
||
|
}
|
||
|
|
||
|
async function handleImport() {
|
||
2 years ago
|
try {
|
||
|
loading.value = true
|
||
2 years ago
|
|
||
2 years ago
|
await templateEditorRef.value.importTemplate()
|
||
|
} catch (e: any) {
|
||
2 years ago
|
return message.error(await extractSdkResponseErrorMsg(e))
|
||
2 years ago
|
} finally {
|
||
|
loading.value = false
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
dialogShow.value = false
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
async function parseAndExtractData(val: string | ArrayBuffer, name: string) {
|
||
2 years ago
|
try {
|
||
|
templateData.value = null
|
||
|
importData.value = null
|
||
2 years ago
|
importColumns.value = []
|
||
2 years ago
|
|
||
|
const templateGenerator = getAdapter(name, val)
|
||
|
|
||
2 years ago
|
if (!templateGenerator) {
|
||
2 years ago
|
message.error(t('msg.error.templateGeneratorNotFound'))
|
||
2 years ago
|
return
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
await templateGenerator.init()
|
||
2 years ago
|
|
||
2 years ago
|
templateGenerator.parse()
|
||
|
templateData.value = templateGenerator.getTemplate()
|
||
|
templateData.value.tables[0].table_name = populateUniqueTableName()
|
||
|
importData.value = templateGenerator.getData()
|
||
2 years ago
|
|
||
2 years ago
|
if (importOnly) importColumns.value = templateGenerator.getColumns()
|
||
2 years ago
|
|
||
2 years ago
|
templateEditorModal.value = true
|
||
|
} catch (e: any) {
|
||
2 years ago
|
message.error(await extractSdkResponseErrorMsg(e))
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
function rejectDrop(fileList: UploadFile[]) {
|
||
2 years ago
|
fileList.map((file) => {
|
||
2 years ago
|
return message.error(`${t('msg.error.fileUploadFailed')} ${file.name}`)
|
||
2 years ago
|
})
|
||
|
}
|
||
|
|
||
|
function handleChange(info: UploadChangeParam) {
|
||
2 years ago
|
const status = info.file.status
|
||
2 years ago
|
|
||
2 years ago
|
if (status !== 'uploading' && status !== 'removed') {
|
||
2 years ago
|
const reader = new FileReader()
|
||
|
|
||
|
reader.onload = (e: ProgressEvent<FileReader>) => {
|
||
|
const target = importState.fileList.find((f) => f.uid === info.file.uid)
|
||
|
|
||
2 years ago
|
if (e.target && e.target.result) {
|
||
|
/** if the file was pushed into the list by `<a-upload-dragger>` we just add the data to the file */
|
||
2 years ago
|
if (target) {
|
||
|
target.data = e.target.result
|
||
|
} else if (!target) {
|
||
2 years ago
|
/** if the file was added programmatically and not with d&d, we create file infos and push it into the list */
|
||
2 years ago
|
importState.fileList.push({
|
||
2 years ago
|
...info.file,
|
||
2 years ago
|
status: 'done',
|
||
|
data: e.target.result,
|
||
|
})
|
||
|
}
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
|
||
|
reader.readAsArrayBuffer(info.file.originFileObj!)
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
if (status === 'done') {
|
||
2 years ago
|
message.success(`Uploaded file ${info.file.name} successfully`)
|
||
2 years ago
|
} else if (status === 'error') {
|
||
2 years ago
|
message.error(`${t('msg.error.fileUploadFailed')} ${info.file.name}`)
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
function formatJson() {
|
||
2 years ago
|
jsonEditorRef.value?.format()
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
function populateUniqueTableName() {
|
||
2 years ago
|
let c = 1
|
||
2 years ago
|
|
||
2 years ago
|
while (tables.value.some((t: TableType) => t.title === `Sheet${c}`)) {
|
||
|
c++
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return `Sheet${c}`
|
||
|
}
|
||
|
|
||
2 years ago
|
function getAdapter(name: string, val: any) {
|
||
2 years ago
|
if (IsImportTypeExcel.value || isImportTypeCsv.value) {
|
||
2 years ago
|
switch (activeKey.value) {
|
||
|
case 'uploadTab':
|
||
|
return new ExcelTemplateAdapter(name, val, importState.parserConfig)
|
||
|
case 'urlTab':
|
||
|
return new ExcelUrlTemplateAdapter(val, importState.parserConfig)
|
||
2 years ago
|
}
|
||
2 years ago
|
} else if (isImportTypeJson.value) {
|
||
2 years ago
|
switch (activeKey.value) {
|
||
|
case 'uploadTab':
|
||
|
return new JSONTemplateAdapter(name, val, importState.parserConfig)
|
||
|
case 'urlTab':
|
||
|
return new JSONUrlTemplateAdapter(val, importState.parserConfig)
|
||
|
case 'jsonEditorTab':
|
||
|
return new JSONTemplateAdapter(name, val, importState.parserConfig)
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return null
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
defineExpose({
|
||
|
handleChange,
|
||
|
})
|
||
2 years ago
|
</script>
|
||
|
|
||
|
<template>
|
||
2 years ago
|
<a-modal v-model:visible="dialogShow" :width="modalWidth" @keydown.esc="dialogShow = false">
|
||
|
<div class="px-5">
|
||
|
<div class="prose-xl font-weight-bold my-5">{{ importMeta.header }}</div>
|
||
|
|
||
|
<div class="mt-5">
|
||
|
<TemplateEditor
|
||
|
v-if="templateEditorModal"
|
||
|
ref="templateEditorRef"
|
||
|
:project-template="templateData"
|
||
|
:import-data="importData"
|
||
|
:import-columns="importColumns"
|
||
|
:import-only="importOnly"
|
||
|
:quick-import-type="importType"
|
||
|
:max-rows-to-parse="importState.parserConfig.maxRowsToParse"
|
||
|
@import="handleImport"
|
||
|
/>
|
||
|
|
||
2 years ago
|
<a-tabs v-else v-model:activeKey="activeKey" hide-add type="editable-card" tab-position="top">
|
||
2 years ago
|
<a-tab-pane key="uploadTab" :closable="false">
|
||
|
<template #tab>
|
||
2 years ago
|
<!-- Upload -->
|
||
2 years ago
|
<div class="flex items-center gap-2">
|
||
|
<MdiFileUploadOutline />
|
||
2 years ago
|
{{ $t('general.upload') }}
|
||
2 years ago
|
</div>
|
||
|
</template>
|
||
|
|
||
|
<div class="py-6">
|
||
|
<a-upload-dragger
|
||
|
v-model:fileList="importState.fileList"
|
||
|
name="file"
|
||
|
class="nc-input-import !scrollbar-thin-dull"
|
||
|
:accept="importMeta.acceptTypes"
|
||
2 years ago
|
:max-count="1"
|
||
2 years ago
|
list-type="picture"
|
||
|
@change="handleChange"
|
||
|
@reject="rejectDrop"
|
||
|
>
|
||
|
<MdiFilePlusOutline size="large" />
|
||
|
|
||
2 years ago
|
<!-- Click or drag file to this area to upload -->
|
||
|
<p class="ant-upload-text">{{ $t('msg.info.import.clickOrDrag') }}</p>
|
||
2 years ago
|
|
||
|
<p class="ant-upload-hint">
|
||
|
{{ importMeta.uploadHint }}
|
||
|
</p>
|
||
|
</a-upload-dragger>
|
||
|
</div>
|
||
|
</a-tab-pane>
|
||
|
|
||
|
<a-tab-pane v-if="isImportTypeJson" key="jsonEditorTab" :closable="false">
|
||
|
<template #tab>
|
||
|
<span class="flex items-center gap-2">
|
||
|
<MdiCodeJson />
|
||
|
JSON Editor
|
||
|
</span>
|
||
|
</template>
|
||
|
|
||
|
<div class="pb-3 pt-3">
|
||
|
<MonacoEditor ref="jsonEditorRef" v-model="importState.jsonEditor" class="min-h-60 max-h-80" />
|
||
|
</div>
|
||
|
</a-tab-pane>
|
||
|
|
||
|
<a-tab-pane v-else key="urlTab" :closable="false">
|
||
|
<template #tab>
|
||
|
<span class="flex items-center gap-2">
|
||
|
<MdiLinkVariant />
|
||
|
URL
|
||
|
</span>
|
||
|
</template>
|
||
|
|
||
|
<div class="pr-10 pt-5">
|
||
|
<a-form :model="importState" name="quick-import-url-form" layout="horizontal" class="mb-0">
|
||
|
<a-form-item :label="importMeta.urlInputLabel" v-bind="validateInfos.url">
|
||
|
<a-input v-model:value="importState.url" size="large" />
|
||
|
</a-form-item>
|
||
|
</a-form>
|
||
|
</div>
|
||
|
</a-tab-pane>
|
||
|
</a-tabs>
|
||
|
</div>
|
||
|
|
||
|
<div v-if="!templateEditorModal">
|
||
|
<a-divider />
|
||
|
|
||
|
<div class="mb-4">
|
||
2 years ago
|
<!-- Advanced Settings -->
|
||
|
<span class="prose-lg">{{ $t('title.advancedSettings') }}</span>
|
||
2 years ago
|
|
||
|
<a-form-item class="mt-4 mb-2" :label="t('msg.info.footMsg')" v-bind="validateInfos.maxRowsToParse">
|
||
|
<a-input-number v-model:value="importState.parserConfig.maxRowsToParse" :min="1" :max="50000" />
|
||
|
</a-form-item>
|
||
|
|
||
2 years ago
|
<!-- Flatten nested -->
|
||
2 years ago
|
<div v-if="isImportTypeJson" class="mt-3">
|
||
|
<a-checkbox v-model:checked="importState.parserConfig.normalizeNested">
|
||
2 years ago
|
<span class="caption">{{ $t('labels.flattenNested') }}</span>
|
||
2 years ago
|
</a-checkbox>
|
||
|
</div>
|
||
|
|
||
2 years ago
|
<!-- Import Data -->
|
||
2 years ago
|
<div v-if="isImportTypeJson" class="mt-4">
|
||
2 years ago
|
<a-checkbox v-model:checked="importState.parserConfig.importData">{{ $t('labels.importData') }}</a-checkbox>
|
||
2 years ago
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
2 years ago
|
<template #footer>
|
||
2 years ago
|
<a-button v-if="templateEditorModal" key="back" @click="templateEditorModal = false">Back</a-button>
|
||
2 years ago
|
|
||
2 years ago
|
<a-button v-else key="cancel" @click="dialogShow = false">{{ $t('general.cancel') }}</a-button>
|
||
2 years ago
|
|
||
2 years ago
|
<a-button
|
||
|
v-if="activeKey === 'jsonEditorTab' && !templateEditorModal"
|
||
|
key="format"
|
||
|
:disabled="disableFormatJsonButton"
|
||
|
@click="formatJson"
|
||
2 years ago
|
>
|
||
2 years ago
|
Format JSON
|
||
|
</a-button>
|
||
|
|
||
2 years ago
|
<a-button
|
||
|
v-if="!templateEditorModal"
|
||
|
key="pre-import"
|
||
|
type="primary"
|
||
2 years ago
|
class="nc-btn-import"
|
||
2 years ago
|
:loading="loading"
|
||
|
:disabled="disablePreImportButton"
|
||
|
@click="handlePreImport"
|
||
2 years ago
|
>
|
||
|
{{ $t('activity.import') }}
|
||
|
</a-button>
|
||
|
|
||
|
<a-button v-else key="import" type="primary" :loading="loading" :disabled="disableImportButton" @click="handleImport">
|
||
|
{{ $t('activity.import') }}
|
||
2 years ago
|
</a-button>
|
||
2 years ago
|
</template>
|
||
2 years ago
|
</a-modal>
|
||
2 years ago
|
</template>
|