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.
238 lines
6.5 KiB
238 lines
6.5 KiB
2 years ago
|
<script setup lang="ts">
|
||
2 years ago
|
import { onKeyDown } from '@vueuse/core'
|
||
2 years ago
|
import { useProvideAttachmentCell } from './utils'
|
||
2 years ago
|
import { useSortable } from './sort'
|
||
2 years ago
|
import Modal from './Modal.vue'
|
||
2 years ago
|
import Carousel from './Carousel.vue'
|
||
2 years ago
|
import {
|
||
2 years ago
|
IsFormInj,
|
||
2 years ago
|
IsGalleryInj,
|
||
2 years ago
|
inject,
|
||
2 years ago
|
isImage,
|
||
2 years ago
|
nextTick,
|
||
2 years ago
|
openLink,
|
||
|
ref,
|
||
|
useDropZone,
|
||
|
useSmartsheetRowStoreOrThrow,
|
||
|
useSmartsheetStoreOrThrow,
|
||
|
watch,
|
||
|
} from '#imports'
|
||
2 years ago
|
|
||
|
interface Props {
|
||
2 years ago
|
modelValue?: string | Record<string, any>[] | null
|
||
2 years ago
|
rowIndex?: number
|
||
2 years ago
|
}
|
||
|
|
||
|
interface Emits {
|
||
|
(event: 'update:modelValue', value: string | Record<string, any>): void
|
||
|
}
|
||
|
|
||
2 years ago
|
const { modelValue, rowIndex } = defineProps<Props>()
|
||
2 years ago
|
|
||
|
const emits = defineEmits<Emits>()
|
||
|
|
||
2 years ago
|
const isForm = inject(IsFormInj, ref(false))
|
||
|
|
||
2 years ago
|
const isGallery = inject(IsGalleryInj, ref(false))
|
||
|
|
||
2 years ago
|
const attachmentCellRef = ref<HTMLDivElement>()
|
||
|
|
||
2 years ago
|
const sortableRef = ref<HTMLDivElement>()
|
||
|
|
||
2 years ago
|
const { cellRefs } = useSmartsheetStoreOrThrow()!
|
||
|
|
||
2 years ago
|
const {
|
||
|
column,
|
||
|
modalVisible,
|
||
|
attachments,
|
||
|
visibleItems,
|
||
|
onDrop,
|
||
|
isLoading,
|
||
|
open,
|
||
|
FileIcon,
|
||
|
selectedImage,
|
||
|
isReadonly,
|
||
2 years ago
|
storedFiles,
|
||
2 years ago
|
} = useProvideAttachmentCell(updateModelValue)
|
||
2 years ago
|
|
||
2 years ago
|
const currentCellRef = ref()
|
||
|
|
||
|
watch(
|
||
|
[() => rowIndex, isForm],
|
||
|
() => {
|
||
2 years ago
|
if (!rowIndex && isForm.value && isGallery.value) {
|
||
2 years ago
|
currentCellRef.value = attachmentCellRef.value
|
||
|
} else {
|
||
|
nextTick(() => {
|
||
2 years ago
|
const nextCell = cellRefs.value.reduceRight((cell, curr) => {
|
||
|
if (!cell && curr.dataset.key === `${rowIndex}${column.value.id}`) cell = curr
|
||
2 years ago
|
|
||
|
return cell
|
||
2 years ago
|
}, undefined as HTMLTableDataCellElement | undefined)
|
||
|
|
||
|
if (!nextCell) {
|
||
|
currentCellRef.value = attachmentCellRef.value
|
||
|
} else {
|
||
|
currentCellRef.value = nextCell
|
||
|
}
|
||
2 years ago
|
})
|
||
|
}
|
||
|
},
|
||
|
{ immediate: true, flush: 'post' },
|
||
2 years ago
|
)
|
||
2 years ago
|
|
||
2 years ago
|
const { dragging } = useSortable(sortableRef, visibleItems, updateModelValue, isReadonly)
|
||
2 years ago
|
|
||
2 years ago
|
const { state: rowState } = useSmartsheetRowStoreOrThrow()
|
||
|
|
||
2 years ago
|
const { isOverDropZone } = useDropZone(currentCellRef, onDrop)
|
||
2 years ago
|
|
||
2 years ago
|
/** on new value, reparse our stored attachments */
|
||
2 years ago
|
watch(
|
||
|
() => modelValue,
|
||
|
(nextModel) => {
|
||
|
if (nextModel) {
|
||
2 years ago
|
try {
|
||
|
attachments.value = ((typeof nextModel === 'string' ? JSON.parse(nextModel) : nextModel) || []).filter(Boolean)
|
||
2 years ago
|
} catch (e) {
|
||
|
console.error(e)
|
||
2 years ago
|
attachments.value = []
|
||
|
}
|
||
2 years ago
|
}
|
||
|
},
|
||
|
{ immediate: true },
|
||
|
)
|
||
|
|
||
2 years ago
|
/** updates attachments array for autosave */
|
||
2 years ago
|
function updateModelValue(data: string | Record<string, any>) {
|
||
|
emits('update:modelValue', typeof data !== 'string' ? JSON.stringify(data) : data)
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
/** Close modal on escape press, disable dropzone as well */
|
||
2 years ago
|
onKeyDown('Escape', () => {
|
||
|
modalVisible.value = false
|
||
|
isOverDropZone.value = false
|
||
|
})
|
||
2 years ago
|
|
||
2 years ago
|
/** sync storedFiles state with row state */
|
||
2 years ago
|
watch(
|
||
2 years ago
|
() => storedFiles.value.length || 0,
|
||
2 years ago
|
() => {
|
||
2 years ago
|
rowState.value[column.value.title!] = storedFiles.value
|
||
2 years ago
|
},
|
||
|
)
|
||
2 years ago
|
|
||
|
const { isSharedForm } = useSmartsheetStoreOrThrow()
|
||
2 years ago
|
</script>
|
||
|
|
||
|
<template>
|
||
2 years ago
|
<div
|
||
|
ref="attachmentCellRef"
|
||
|
class="nc-attachment-cell relative flex-1 color-transition flex items-center justify-between gap-1"
|
||
|
>
|
||
2 years ago
|
<Carousel />
|
||
|
|
||
2 years ago
|
<template v-if="isSharedForm || (!isReadonly && !dragging && !!currentCellRef)">
|
||
2 years ago
|
<general-overlay
|
||
|
v-model="isOverDropZone"
|
||
|
inline
|
||
2 years ago
|
:target="currentCellRef"
|
||
2 years ago
|
class="text-white text-lg ring ring-accent bg-gray-700/75 flex items-center justify-center gap-2 backdrop-blur-xl"
|
||
2 years ago
|
>
|
||
2 years ago
|
<MaterialSymbolsFileCopyOutline class="text-accent" /> Drop here
|
||
2 years ago
|
</general-overlay>
|
||
2 years ago
|
</template>
|
||
|
|
||
2 years ago
|
<div
|
||
|
v-if="!isReadonly"
|
||
|
:class="{ 'mx-auto px-4': !visibleItems.length }"
|
||
2 years ago
|
class="group flex gap-1 items-center active:ring rounded border-1 p-1 hover:(bg-primary bg-opacity-10)"
|
||
2 years ago
|
@click.stop="open"
|
||
|
>
|
||
|
<MdiReload v-if="isLoading" :class="{ 'animate-infinite animate-spin': isLoading }" />
|
||
2 years ago
|
|
||
2 years ago
|
<a-tooltip v-else placement="bottom">
|
||
|
<template #title> Click or drop a file into cell </template>
|
||
2 years ago
|
|
||
2 years ago
|
<div class="flex items-center gap-2">
|
||
2 years ago
|
<MaterialSymbolsAttachFile class="transform group-hover:(text-accent scale-120) text-gray-500 text-[10px]" />
|
||
2 years ago
|
|
||
2 years ago
|
<div v-if="!visibleItems.length" class="group-hover:text-primary text-gray-500 text-xs">Add file(s)</div>
|
||
2 years ago
|
</div>
|
||
|
</a-tooltip>
|
||
|
</div>
|
||
2 years ago
|
<div v-else class="flex" />
|
||
2 years ago
|
|
||
2 years ago
|
<template v-if="visibleItems.length">
|
||
2 years ago
|
<div
|
||
|
ref="sortableRef"
|
||
|
:class="{ dragging }"
|
||
2 years ago
|
class="flex justify-center items-center flex-wrap gap-2 p-1 scrollbar-thin-dull max-h-[150px] overflow-auto"
|
||
2 years ago
|
>
|
||
2 years ago
|
<div
|
||
2 years ago
|
v-for="(item, i) of visibleItems"
|
||
|
:key="item.url || item.title"
|
||
2 years ago
|
:class="isImage(item.title, item.mimetype ?? item.type) ? '' : 'border-1 rounded'"
|
||
2 years ago
|
class="nc-attachment flex items-center justify-center min-h-[50px]"
|
||
2 years ago
|
>
|
||
2 years ago
|
<a-tooltip placement="bottom">
|
||
|
<template #title>
|
||
|
<div class="text-center w-full">{{ item.title }}</div>
|
||
|
</template>
|
||
2 years ago
|
|
||
2 years ago
|
<nuxt-img
|
||
2 years ago
|
v-if="isImage(item.title, item.mimetype ?? item.type) && (item.url || item.data)"
|
||
2 years ago
|
quality="75"
|
||
2 years ago
|
placeholder
|
||
2 years ago
|
:alt="item.title || `#${i}`"
|
||
|
:src="item.url || item.data"
|
||
2 years ago
|
class="ring-1 ring-gray-300 rounded max-h-[50px] max-w-[50px]"
|
||
2 years ago
|
@click="selectedImage = item"
|
||
2 years ago
|
/>
|
||
2 years ago
|
|
||
2 years ago
|
<component :is="FileIcon(item.icon)" v-else-if="item.icon" @click="openLink(item.url || item.data)" />
|
||
2 years ago
|
|
||
2 years ago
|
<IcOutlineInsertDriveFile v-else @click.stop="openLink(item.url || item.data)" />
|
||
2 years ago
|
</a-tooltip>
|
||
|
</div>
|
||
2 years ago
|
</div>
|
||
|
|
||
2 years ago
|
<div class="group flex gap-1 items-center border-1 active:ring rounded p-1 hover:(bg-primary bg-opacity-10)">
|
||
2 years ago
|
<MdiReload v-if="isLoading" :class="{ 'animate-infinite animate-spin': isLoading }" />
|
||
|
|
||
|
<a-tooltip v-else placement="bottom">
|
||
|
<template #title> View attachments </template>
|
||
|
|
||
2 years ago
|
<MdiArrowExpand
|
||
2 years ago
|
class="select-none transform group-hover:(text-accent scale-120) text-[10px] text-gray-500"
|
||
2 years ago
|
@click.stop="modalVisible = true"
|
||
|
/>
|
||
2 years ago
|
</a-tooltip>
|
||
|
</div>
|
||
2 years ago
|
</template>
|
||
|
|
||
|
<Modal />
|
||
|
</div>
|
||
|
</template>
|
||
2 years ago
|
|
||
|
<style lang="scss">
|
||
2 years ago
|
.nc-cell {
|
||
|
.nc-attachment-cell {
|
||
2 years ago
|
.nc-attachment {
|
||
|
@apply w-[50px] h-[50px] min-h-[50px] min-w-[50px];
|
||
|
}
|
||
|
|
||
2 years ago
|
.ghost,
|
||
|
.ghost > * {
|
||
|
@apply !pointer-events-none;
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
.dragging {
|
||
|
.ant-tooltip {
|
||
|
@apply !hidden;
|
||
|
}
|
||
2 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
</style>
|