Browse Source

fix(nc-gui): attachments not emitted properly in form mode

pull/3669/head
braks 2 years ago committed by Raju Udava
parent
commit
8d05af59a1
  1. 34
      packages/nc-gui/components/cell/attachment/index.vue
  2. 51
      packages/nc-gui/components/cell/attachment/utils.ts

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

@ -3,7 +3,6 @@ import { onKeyDown } from '@vueuse/core'
import { useProvideAttachmentCell } from './utils' import { useProvideAttachmentCell } from './utils'
import { useSortable } from './sort' import { useSortable } from './sort'
import { import {
IsFormInj,
IsGalleryInj, IsGalleryInj,
inject, inject,
isImage, isImage,
@ -22,24 +21,26 @@ interface Props {
} }
interface Emits { interface Emits {
(event: 'update:modelValue', value: string | Record<string, any>): void (event: 'update:modelValue', value: string | Record<string, any>[]): void
} }
const { modelValue, rowIndex } = defineProps<Props>() const { modelValue, rowIndex } = defineProps<Props>()
const emits = defineEmits<Emits>() const emits = defineEmits<Emits>()
const isForm = inject(IsFormInj, ref(false))
const isGallery = inject(IsGalleryInj, ref(false)) const isGallery = inject(IsGalleryInj, ref(false))
const attachmentCellRef = ref<HTMLDivElement>() const attachmentCellRef = ref<HTMLDivElement>()
const sortableRef = ref<HTMLDivElement>() const sortableRef = ref<HTMLDivElement>()
const { cellRefs } = useSmartsheetStoreOrThrow()! const currentCellRef = ref()
const { cellRefs, isSharedForm } = useSmartsheetStoreOrThrow()!
const { const {
isPublic,
isForm,
column, column,
modalVisible, modalVisible,
attachments, attachments,
@ -53,8 +54,6 @@ const {
storedFiles, storedFiles,
} = useProvideAttachmentCell(updateModelValue) } = useProvideAttachmentCell(updateModelValue)
const currentCellRef = ref()
watch( watch(
[() => rowIndex, isForm, attachmentCellRef], [() => rowIndex, isForm, attachmentCellRef],
() => { () => {
@ -91,10 +90,20 @@ watch(
(nextModel) => { (nextModel) => {
if (nextModel) { if (nextModel) {
try { try {
attachments.value = ((typeof nextModel === 'string' ? JSON.parse(nextModel) : nextModel) || []).filter(Boolean) const nextAttachments = ((typeof nextModel === 'string' ? JSON.parse(nextModel) : nextModel) || []).filter(Boolean)
if (isPublic.value && isForm.value) {
storedFiles.value = nextAttachments
} else {
attachments.value = nextAttachments
}
} catch (e) { } catch (e) {
console.error(e) console.error(e)
attachments.value = [] if (isPublic.value && isForm.value) {
storedFiles.value = []
} else {
attachments.value = []
}
} }
} }
}, },
@ -102,8 +111,8 @@ watch(
) )
/** updates attachments array for autosave */ /** updates attachments array for autosave */
function updateModelValue(data: string | Record<string, any>) { function updateModelValue(data: string | Record<string, any>[]) {
emits('update:modelValue', typeof data !== 'string' ? JSON.stringify(data) : data) emits('update:modelValue', data)
} }
/** Close modal on escape press, disable dropzone as well */ /** Close modal on escape press, disable dropzone as well */
@ -119,8 +128,6 @@ watch(
rowState.value[column.value!.title!] = storedFiles.value rowState.value[column.value!.title!] = storedFiles.value
}, },
) )
const { isSharedForm } = useSmartsheetStoreOrThrow()
</script> </script>
<template> <template>
@ -159,6 +166,7 @@ const { isSharedForm } = useSmartsheetStoreOrThrow()
</div> </div>
</a-tooltip> </a-tooltip>
</div> </div>
<div v-else class="flex" /> <div v-else class="flex" />
<template v-if="visibleItems.length"> <template v-if="visibleItems.length">

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

@ -79,37 +79,37 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
async function onFileSelect(selectedFiles: FileList | File[]) { async function onFileSelect(selectedFiles: FileList | File[]) {
if (!selectedFiles.length) return if (!selectedFiles.length) return
if (isPublic.value) { if (isPublic.value && isForm.value) {
storedFiles.value.push( const newFiles = await Promise.all<AttachmentProps>(
...(await Promise.all<AttachmentProps>( Array.from(selectedFiles).map(
Array.from(selectedFiles).map( (file) =>
(file) => new Promise<AttachmentProps>((resolve) => {
new Promise<AttachmentProps>((resolve) => { const res: AttachmentProps = { ...file, file, title: file.name, mimetype: file.type }
const res: AttachmentProps = { ...file, file, title: file.name, mimetype: file.type }
if (isImage(file.name, (<any>file).mimetype ?? file.type)) {
const reader = new FileReader()
reader.onload = (e) => { if (isImage(file.name, (<any>file).mimetype ?? file.type)) {
res.data = e.target?.result const reader = new FileReader()
resolve(res) reader.onload = (e) => {
} res.data = e.target?.result
reader.onerror = () => { resolve(res)
resolve(res) }
}
reader.readAsDataURL(file) reader.onerror = () => {
} else {
resolve(res) resolve(res)
} }
}),
), reader.readAsDataURL(file)
)), } else {
resolve(res)
}
}),
),
) )
return updateModelValue(storedFiles.value.map((stored) => stored.file)) attachments.value = [...attachments.value, ...newFiles]
return updateModelValue(attachments.value)
} }
const newAttachments = [] const newAttachments = []
@ -132,7 +132,7 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
} }
} }
updateModelValue([...attachments.value, ...newAttachments]) updateModelValue(JSON.stringify([...attachments.value, ...newAttachments]))
} }
/** save files on drop */ /** save files on drop */
@ -163,7 +163,7 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
} }
} }
/** our currently visible items, either the locally stored or the ones from db, depending on isPublicForm status */ /** our currently visible items, either the locally stored or the ones from db, depending on isPublic & isForm status */
const visibleItems = computed<any[]>(() => (isPublic.value && isForm.value ? storedFiles.value : attachments.value)) const visibleItems = computed<any[]>(() => (isPublic.value && isForm.value ? storedFiles.value : attachments.value))
watch(files, (nextFiles) => nextFiles && onFileSelect(nextFiles)) watch(files, (nextFiles) => nextFiles && onFileSelect(nextFiles))
@ -172,6 +172,7 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
attachments, attachments,
visibleItems, visibleItems,
isPublic, isPublic,
isForm,
isReadonly, isReadonly,
meta, meta,
column, column,

Loading…
Cancel
Save