forked from element-plus/element-plus
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
374 additions
and
375 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
<script setup lang="ts"> | ||
import { computed, onBeforeUnmount, ref, watch } from 'vue'; | ||
import { useStore } from 'vuex'; | ||
import { ElMessage } from 'element-plus'; | ||
import { useI18n } from 'vue-i18n'; | ||
import { sendEvent } from '@/admin/umeng'; | ||
import { getOSPathSeparator } from '@/utils'; | ||
import { useRoute } from 'vue-router'; | ||
import { FILE_UPLOAD_MODE_DIR } from '@/constants'; | ||
import FileUpload from '@/components/file/FileUpload.vue'; | ||
const props = defineProps<{ | ||
ns: ListStoreNamespace; | ||
activeDialogKey?: DialogKey; | ||
activeId: string; | ||
form: BaseModel; | ||
services: FileServices<BaseModel>; | ||
}>(); | ||
// i18n | ||
const { t } = useI18n(); | ||
// route | ||
const route = useRoute(); | ||
// store | ||
const store = useStore(); | ||
const fileUploadRef = ref<typeof FileUpload>(); | ||
const mode = ref<FileUploadMode>(FILE_UPLOAD_MODE_DIR); | ||
const files = ref<FileWithPath[]>([]); | ||
const fileUploadVisible = computed( | ||
() => props.activeDialogKey === 'uploadFiles' | ||
); | ||
const name = computed(() => props.form?.name); | ||
const confirmLoading = ref<boolean>(false); | ||
const confirmDisabled = computed<boolean>(() => !files.value.length); | ||
const isDetail = computed<boolean>(() => !!props.activeId); | ||
const { listRootDir, saveFilesBinary } = props.services; | ||
const id = computed<string>(() => { | ||
const { activeId, form } = props; | ||
if (isDetail.value) { | ||
return activeId; | ||
} else { | ||
return form?._id as string; | ||
} | ||
}); | ||
const hasMultiDir = computed<boolean>(() => { | ||
if (!files.value) return false; | ||
const set = new Set<string>(); | ||
for (const f of files.value) { | ||
const lv1 = f?.path?.split(getOSPathSeparator())[0] as string; | ||
if (!set.has(lv1)) { | ||
set.add(lv1); | ||
} | ||
if (set.size > 1) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}); | ||
const uploadInfo = computed(() => { | ||
const info = { | ||
fileCount: files.value.length, | ||
filePaths: files.value.map(f => f.path || f.name), | ||
} as FileUploadInfo; | ||
if (mode.value === FILE_UPLOAD_MODE_DIR) { | ||
const f = files.value[0]; | ||
info.dirName = f?.path?.split(getOSPathSeparator())[0]; | ||
} | ||
return info; | ||
}); | ||
const getFilePath = (f: FileWithPath): string => { | ||
const path = f.path; | ||
if (!path) return f.name; | ||
if (hasMultiDir.value) { | ||
return path; | ||
} else { | ||
return path | ||
.split(getOSPathSeparator()) | ||
.filter((_: any, i: number) => i > 0) | ||
.join(getOSPathSeparator()); | ||
} | ||
}; | ||
const uploadFiles = async () => { | ||
if (!files.value) return; | ||
await saveFilesBinary( | ||
id.value, | ||
files.value.map((f: FileWithPath) => { | ||
return { path: getFilePath(f) as string, file: f as File }; | ||
}) | ||
); | ||
await listRootDir(id.value); | ||
}; | ||
const onUploadClose = () => { | ||
const { ns } = props; | ||
store.commit(`${ns}/hideDialog`); | ||
}; | ||
const onUploadConfirm = async () => { | ||
const { ns } = props; | ||
confirmLoading.value = true; | ||
try { | ||
sendEvent('click_spider_detail_actions_upload_confirm', { | ||
mode: mode.value, | ||
}); | ||
await uploadFiles(); | ||
await ElMessage.success(t('common.message.success.upload')); | ||
} catch (e: any) { | ||
await ElMessage.error(e); | ||
} finally { | ||
confirmLoading.value = false; | ||
store.commit(`${ns}/hideDialog`); | ||
} | ||
}; | ||
const onModeChange = (value: FileUploadMode) => { | ||
mode.value = value; | ||
}; | ||
const onFilesChange = (fileList: FileWithPath[]) => { | ||
if (!fileList.length) return; | ||
files.value = fileList; | ||
sendEvent('click_spider_detail_actions_files_change'); | ||
}; | ||
const title = computed(() => { | ||
return ( | ||
t('components.file.upload.title') + (name.value ? ` - ${name.value}` : '') | ||
); | ||
}); | ||
watch(fileUploadVisible, () => { | ||
if (!fileUploadVisible.value) { | ||
files.value = []; | ||
} | ||
}); | ||
watch(mode, () => { | ||
files.value = []; | ||
}); | ||
onBeforeUnmount(() => { | ||
const { ns } = props; | ||
store.commit(`${ns}/hideDialog`); | ||
}); | ||
</script> | ||
|
||
<template> | ||
<cl-dialog | ||
:visible="fileUploadVisible" | ||
:title="title" | ||
:confirm-loading="confirmLoading" | ||
:confirm-disabled="confirmDisabled" | ||
@close="onUploadClose" | ||
@confirm="onUploadConfirm" | ||
> | ||
<cl-file-upload | ||
ref="fileUploadRef" | ||
:mode="mode" | ||
:upload-info="uploadInfo" | ||
@mode-change="onModeChange" | ||
@files-change="onFilesChange" | ||
/> | ||
</cl-dialog> | ||
</template> | ||
|
||
<style lang="scss" scoped></style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<script setup lang="ts"> | ||
import { useStore } from 'vuex'; | ||
import useGitService from '@/services/git/gitService'; | ||
import useGitDetail from '@/views/git/detail/useGitDetail'; | ||
// store | ||
const ns = 'git'; | ||
const store = useStore(); | ||
const { git: state } = store.state as RootStoreState; | ||
const { activeId } = useGitDetail(); | ||
</script> | ||
|
||
<template> | ||
<cl-upload-files-dialog | ||
:ns="ns" | ||
:active-dialog-key="state.activeDialogKey" | ||
:active-id="activeId" | ||
:form="state.form" | ||
:services="useGitService(store)" | ||
/> | ||
</template> | ||
|
||
<style lang="scss" scoped></style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.