Browse Source

fix(nc-gui): use fallback image

pull/5046/head
Wing-Kam Wong 2 years ago
parent
commit
5f250b5453
  1. 33
      packages/nc-gui/components/cell/attachment/index.vue

33
packages/nc-gui/components/cell/attachment/index.vue

@ -42,6 +42,8 @@ const attachmentCellRef = ref<HTMLDivElement>()
const sortableRef = ref<HTMLDivElement>() const sortableRef = ref<HTMLDivElement>()
const { appInfo } = useGlobal()
const currentCellRef = ref<Element | undefined>(dropZoneInjection.value) const currentCellRef = ref<Element | undefined>(dropZoneInjection.value)
const { cellRefs, isSharedForm } = useSmartsheetStoreOrThrow()! const { cellRefs, isSharedForm } = useSmartsheetStoreOrThrow()!
@ -60,7 +62,6 @@ const {
selectedImage, selectedImage,
isReadonly, isReadonly,
storedFiles, storedFiles,
getAttachmentUrl,
} = useProvideAttachmentCell(updateModelValue) } = useProvideAttachmentCell(updateModelValue)
watch( watch(
@ -95,22 +96,27 @@ const { state: rowState } = useSmartsheetRowStoreOrThrow()
const { isOverDropZone } = useDropZone(currentCellRef as any, onDrop) const { isOverDropZone } = useDropZone(currentCellRef as any, onDrop)
const getImgSrc = (item: Record<string, any>) => {
if (item.data) {
return item.data
} else if (item.path) {
return `${appInfo.value.ncSiteUrl}/${item.path}`
}
return item.url
}
const showFallback = (evt: any, item: Record<string, any>) => {
evt.onerror = null
evt.target.src = item.url
}
/** on new value, reparse our stored attachments */ /** on new value, reparse our stored attachments */
watch( watch(
() => modelValue, () => modelValue,
async (nextModel) => { async (nextModel) => {
if (nextModel) { if (nextModel) {
try { try {
let nextAttachments = ((typeof nextModel === 'string' ? JSON.parse(nextModel) : nextModel) || []).filter(Boolean) const nextAttachments = ((typeof nextModel === 'string' ? JSON.parse(nextModel) : nextModel) || []).filter(Boolean)
// reconstruct the url
// See /packages/nocodb/src/lib/version-upgrader/ncAttachmentUpgrader.ts for the details
nextAttachments = await Promise.all(
nextAttachments.map(async (attachment: any) => ({
...attachment,
url: await getAttachmentUrl(attachment),
})),
)
if (isPublic.value && isForm.value) { if (isPublic.value && isForm.value) {
storedFiles.value = nextAttachments storedFiles.value = nextAttachments
@ -230,15 +236,16 @@ useSelectedCellKeyupListener(inject(ActiveCellInj, ref(false)), (e) => {
<div class="text-center w-full">{{ item.title }}</div> <div class="text-center w-full">{{ item.title }}</div>
</template> </template>
<template v-if="isImage(item.title, item.mimetype ?? item.type) && (item.url || item.data)"> <template v-if="isImage(item.title, item.mimetype ?? item.type) && getImgSrc(item)">
<div class="nc-attachment flex items-center justify-center" @click.stop="selectedImage = item"> <div class="nc-attachment flex items-center justify-center" @click.stop="selectedImage = item">
<LazyNuxtImg <LazyNuxtImg
quality="75" quality="75"
placeholder placeholder
fit="cover" fit="cover"
:alt="item.title || `#${i}`" :alt="item.title || `#${i}`"
:src="item.url || item.data" :src="getImgSrc(item)"
class="max-w-full max-h-full" class="max-w-full max-h-full"
:onerror="(e) => showFallback(e, item)"
/> />
</div> </div>
</template> </template>

Loading…
Cancel
Save