diff --git a/packages/nc-gui/components/smartsheet/Cell.vue b/packages/nc-gui/components/smartsheet/Cell.vue index f746c15655..6fe1f0b035 100644 --- a/packages/nc-gui/components/smartsheet/Cell.vue +++ b/packages/nc-gui/components/smartsheet/Cell.vue @@ -17,6 +17,7 @@ import { useDebounceFn, useVModel, } from '#imports' +import { useSmartsheetRowStoreOrThrow } from '~/composables/useSmartsheetRowStore' import { NavigateDir } from '~/lib' interface Props { @@ -57,13 +58,14 @@ const isPublic = inject(IsPublicInj, ref(false)) const isLocked = inject(IsLockedInj, ref(false)) -let changed = $ref(false) +const { currentRow } = useSmartsheetRowStoreOrThrow() const syncValue = useDebounceFn(function () { - changed = false + currentRow.value.rowMeta.changed = false emit('save') }, 1000) + const isAutoSaved = $computed(() => { return [ UITypes.SingleLineText, @@ -89,13 +91,13 @@ const vModel = computed({ get: () => props.modelValue, set: (val) => { if (val !== props.modelValue) { - changed = true + currentRow.value.rowMeta.changed = true emit('update:modelValue', val) if (isAutoSaved) { syncValue() } else if (!isManualSaved) { emit('save') - changed = true + currentRow.value.rowMeta.changed = true } } }, @@ -129,9 +131,9 @@ const { const syncAndNavigate = (dir: NavigateDir) => { if (isJSON.value) return - if (changed) { + if (currentRow.value.rowMeta.changed) { emit('save') - changed = false + currentRow.value.rowMeta.changed = false } emit('navigate', dir) } diff --git a/packages/nc-gui/components/smartsheet/Grid.vue b/packages/nc-gui/components/smartsheet/Grid.vue index 7d2eee0c0d..f8faa99b39 100644 --- a/packages/nc-gui/components/smartsheet/Grid.vue +++ b/packages/nc-gui/components/smartsheet/Grid.vue @@ -248,36 +248,35 @@ const onKeyDown = async (e: KeyboardEvent) => { e.preventDefault() if (selected.row < data.value.length - 1) selected.row++ break - default: - { - const rowObj = data.value[selected.row] - const columnObj = fields.value[selected.col] - - if ((!editEnabled && e.metaKey) || e.ctrlKey) { - switch (e.keyCode) { - // copy - ctrl/cmd +c - case 67: - await copy(rowObj.row[columnObj.title] || '') - break - } + default: { + const rowObj = data.value[selected.row] + const columnObj = fields.value[selected.col] + + if ((!editEnabled && e.metaKey) || e.ctrlKey) { + switch (e.keyCode) { + // copy - ctrl/cmd +c + case 67: + await copy(rowObj.row[columnObj.title] || '') + break } + } - if (editEnabled || e.ctrlKey || e.altKey || e.metaKey) { - return - } + if (editEnabled || e.ctrlKey || e.altKey || e.metaKey) { + return + } - /** on letter key press make cell editable and empty */ - if (e?.key?.length === 1) { - if (!isPkAvail && !rowObj.rowMeta.new) { - // Update not allowed for table which doesn't have primary Key - return message.info(t('msg.info.updateNotAllowedWithoutPK')) - } - if (makeEditable(rowObj, columnObj)) { - rowObj.row[columnObj.title] = '' - } - // editEnabled = true + /** on letter key press make cell editable and empty */ + if (e?.key?.length === 1) { + if (!isPkAvail && !rowObj.rowMeta.new) { + // Update not allowed for table which doesn't have primary Key + return message.info(t('msg.info.updateNotAllowedWithoutPK')) + } + if (makeEditable(rowObj, columnObj)) { + rowObj.row[columnObj.title] = '' } + // editEnabled = true } + } break } } @@ -324,6 +323,31 @@ const showContextMenu = (e: MouseEvent, target?: { row: number; col: number }) = contextMenuTarget.value = target } } + +const rowRefs = $ref() + +onBeforeUnmount(async () => { + console.log(rowRefs) + for (const [index, currentRow] of Object.entries(data.value)) { + if (currentRow.rowMeta.new) { + const syncLTARRefs = rowRefs[index]!.syncLTARRefs + const savedRow = await updateOrSaveRow(currentRow, null) + await syncLTARRefs(savedRow) + currentRow.rowMeta.changed = false + continue + } + if (currentRow.rowMeta.changed) { + currentRow.rowMeta.changed = false + for (const field of meta?.value.columns ?? []) { + if (isVirtualCol(field)) continue + if (currentRow.row[field.title!] !== currentRow.oldRow[field.title!]) { + await updateOrSaveRow(currentRow, field.title!) + } + } + } + } +}) +