Browse Source

feat(nc-gui): revise rename attachment logic

pull/4931/head
Wing-Kam Wong 2 years ago
parent
commit
24708d5da8
  1. 9
      packages/nc-gui/components/cell/attachment/Modal.vue
  2. 27
      packages/nc-gui/components/cell/attachment/RenameFile.vue
  3. 23
      packages/nc-gui/components/cell/attachment/utils.ts
  4. 2
      packages/nc-gui/components/smartsheet/column/AttachmentOptions.vue

9
packages/nc-gui/components/cell/attachment/Modal.vue

@ -22,6 +22,7 @@ const {
selectedImage, selectedImage,
selectedVisibleItems, selectedVisibleItems,
bulkDownloadFiles, bulkDownloadFiles,
renameFile,
} = useAttachmentCell()! } = useAttachmentCell()!
// todo: replace placeholder var // todo: replace placeholder var
@ -144,6 +145,14 @@ function onRemoveFileClick(title: any, i: number) {
</div> </div>
</a-tooltip> </a-tooltip>
<a-tooltip placement="bottom">
<template #title> Rename File </template>
<div class="nc-attachment-download group-hover:(opacity-100) mr-[35px]">
<MdiEditOutline @click.stop="renameFile(item, i)" />
</div>
</a-tooltip>
<div <div
:class="[dragging ? 'cursor-move' : 'cursor-pointer']" :class="[dragging ? 'cursor-move' : 'cursor-pointer']"
class="nc-attachment h-full w-full flex items-center justify-center" class="nc-attachment h-full w-full flex items-center justify-center"

27
packages/nc-gui/components/cell/attachment/RenameFile.vue

@ -2,8 +2,7 @@
import { generateUniqueName, onKeyStroke, onMounted, reactive, ref } from '#imports' import { generateUniqueName, onKeyStroke, onMounted, reactive, ref } from '#imports'
const props = defineProps<{ const props = defineProps<{
fileName: string title: string
fileNames: string[]
}>() }>()
const emit = defineEmits<{ const emit = defineEmits<{
@ -15,9 +14,8 @@ const inputEl = ref()
const visible = ref(true) const visible = ref(true)
const fileEnding = props.fileName.split('.').pop()
const form = reactive({ const form = reactive({
name: props.fileName.replace(`.${fileEnding}`, ''), title: props.title,
}) })
function renameFile(fileName: string) { function renameFile(fileName: string) {
@ -26,22 +24,11 @@ function renameFile(fileName: string) {
} }
async function useRandomName() { async function useRandomName() {
form.name = await generateUniqueName() form.title = await generateUniqueName()
} }
const rules = { const rules = {
name: [ title: [{ required: true, message: 'title is required.' }],
{ required: true, message: 'Filename is required.' },
{
validator: (_: unknown, v: string) =>
new Promise((resolve, reject) => {
props.fileNames.every((fileName) => fileName.replace(`.${fileEnding}`, '') !== v)
? resolve(true)
: reject(new Error(`File name should be unique.`))
}),
message: 'File name should be unique.',
},
],
} }
function onCancel() { function onCancel() {
@ -71,9 +58,9 @@ onMounted(() => {
@cancel="onCancel" @cancel="onCancel"
> >
<div class="flex flex-col items-center justify-center h-full"> <div class="flex flex-col items-center justify-center h-full">
<a-form class="w-full h-full" no-style :model="form" @finish="renameFile(`${form.name}.${fileEnding}`)"> <a-form class="w-full h-full" no-style :model="form" @finish="renameFile(form.title)">
<a-form-item class="w-full" name="name" :rules="rules.name"> <a-form-item class="w-full" name="title" :rules="rules.title">
<a-input ref="inputEl" v-model:value="form.name" class="w-full" :placeholder="$t('general.rename')" /> <a-input ref="inputEl" v-model:value="form.title" class="w-full" :placeholder="$t('general.rename')" />
</a-form-item> </a-form-item>
<div class="flex items-center justify-center gap-6 w-full mt-4"> <div class="flex items-center justify-center gap-6 w-full mt-4">
<button class="scaling-btn bg-opacity-100" type="submit"> <button class="scaling-btn bg-opacity-100" type="submit">

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

@ -1,3 +1,4 @@
import type { AttachmentType } from 'nocodb-sdk'
import RenameFile from './RenameFile.vue' import RenameFile from './RenameFile.vue'
import { import {
ColumnInj, ColumnInj,
@ -126,7 +127,7 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
const files: File[] = [] const files: File[] = []
for (let file of selectedFiles) { for (const file of selectedFiles) {
// verify number of files // verify number of files
if (visibleItems.value.length + selectedFiles.length > attachmentMeta.maxNumberOfAttachments) { if (visibleItems.value.length + selectedFiles.length > attachmentMeta.maxNumberOfAttachments) {
message.error( message.error(
@ -170,23 +171,19 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
updateModelValue(JSON.stringify([...attachments.value, ...newAttachments])) updateModelValue(JSON.stringify([...attachments.value, ...newAttachments]))
} }
async function renameFile(file: File) { async function renameFile(attachment: AttachmentType, idx: number) {
return new Promise<File>((resolve) => { return new Promise<boolean>((resolve) => {
const { close } = useDialog(RenameFile, { const { close } = useDialog(RenameFile, {
fileName: file.name, title: attachment.title,
fileNames: [...attachments.value.map((file) => file.title), ...storedFiles.value.map((file) => file.title)], onRename: (newTitle: string) => {
onRename: (newName: string) => { attachments.value[idx].title = newTitle
updateModelValue(JSON.stringify(attachments.value))
close() close()
resolve( resolve(true)
new File([file], newName, {
type: file.type,
lastModified: file.lastModified,
}),
)
}, },
onCancel: () => { onCancel: () => {
close() close()
resolve(file) resolve(true)
}, },
}) })
}) })

2
packages/nc-gui/components/smartsheet/column/AttachmentOptions.vue

@ -1,6 +1,6 @@
<script setup lang="ts"> <script setup lang="ts">
import { useGlobal, useVModel } from '#imports'
import { fileMimeTypes } from './utils' import { fileMimeTypes } from './utils'
import { useGlobal, useVModel } from '#imports'
interface Option { interface Option {
key: string key: string

Loading…
Cancel
Save