Browse Source

feat(nc-gui): attachment rename file logic

pull/4931/head
Wing-Kam Wong 2 years ago
parent
commit
ed51a53d59
  1. 33
      packages/nc-gui/components/cell/attachment/utils.ts

33
packages/nc-gui/components/cell/attachment/utils.ts

@ -1,3 +1,4 @@
import RenameFile from './RenameFile.vue'
import {
ColumnInj,
EditModeInj,
@ -48,7 +49,7 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
/** keep user selected File object */
const storedFiles = ref<AttachmentProps[]>([])
const attachments = ref<File[]>([])
const attachments = ref<AttachmentProps[]>([])
const modalVisible = ref(false)
@ -87,12 +88,12 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
new Promise<AttachmentProps>((resolve) => {
const res: AttachmentProps = { ...file, file, title: file.name, mimetype: file.type }
if (isImage(file.name, (<any>file).mimetype ?? file.type)) {
renameFile(file).then((renamedFile) => {
if (isImage(renamedFile.name, (<any>renamedFile).mimetype ?? renamedFile.type)) {
const reader = new FileReader()
reader.onload = (e) => {
res.data = e.target?.result
resolve(res)
}
@ -104,6 +105,7 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
} else {
resolve(res)
}
})
}),
),
)
@ -115,7 +117,8 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
const newAttachments = []
for (const file of selectedFiles) {
for (let file of selectedFiles) {
file = await renameFile(file)
try {
const data = await api.storage.upload(
{
@ -136,6 +139,28 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
updateModelValue(JSON.stringify([...attachments.value, ...newAttachments]))
}
async function renameFile(file: File) {
return new Promise<File>((resolve) => {
const { close } = useDialog(RenameFile, {
fileName: file.name,
fileNames: [...attachments.value.map((file) => file.title), ...storedFiles.value.map((file) => file.title)],
onRename: (newName: string) => {
close()
resolve(
new File([file], newName, {
type: file.type,
lastModified: file.lastModified,
}),
)
},
onCancel: () => {
close()
resolve(file)
},
})
})
}
/** save files on drop */
async function onDrop(droppedFiles: File[] | null) {
if (droppedFiles) {

Loading…
Cancel
Save