From 96573d960315e7f91b2583547fd3fa1d9a42a4eb Mon Sep 17 00:00:00 2001 From: Pranav C Date: Sun, 27 Nov 2022 00:11:10 +0530 Subject: [PATCH] feat: convert type when copy paste data within grid Signed-off-by: Pranav C --- .../composables/useMultiSelect/index.ts | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/packages/nc-gui/composables/useMultiSelect/index.ts b/packages/nc-gui/composables/useMultiSelect/index.ts index e70e415908..f6cb716f7e 100644 --- a/packages/nc-gui/composables/useMultiSelect/index.ts +++ b/packages/nc-gui/composables/useMultiSelect/index.ts @@ -5,6 +5,35 @@ import { CellRange } from './cellRange' import { copyTable, message, reactive, ref, unref, useCopy, useEventListener, useI18n } from '#imports' import type { Row } from '~/lib' +function convertCellData(args: { from: UITypes; to: UITypes; value: any }) { + const { from, to, value } = args + if (from === to) { + return value + } + + switch (to) { + case UITypes.Number: + return Number(value) + case UITypes.Checkbox: + return Boolean(value) + case UITypes.Date: + return new Date(value) + case UITypes.Attachment: + try { + return typeof value === 'string' ? JSON.parse(value) : value + } catch (e) { + return [] + } + case UITypes.LinkToAnotherRecord: + case UITypes.Lookup: + case UITypes.Rollup: + case UITypes.Formula: + throw new Error(`Unsupported conversion from ${from} to ${to}`) + default: + return value + } +} + /** * Utility to help with multi-selecting rows/cells in the smartsheet */ @@ -22,6 +51,8 @@ export function useMultiSelect( const { copy } = useCopy() + let clipboardContext = $ref<{ value: any; uidt: UITypes } | null>(null) + const editEnabled = ref(_editEnabled) const selectedCell = reactive({ row: null, col: null }) @@ -224,8 +255,18 @@ export function useMultiSelect( await copyValue() break case 86: - clearCell(selected as { row: number; col: number }, true) - makeEditable(rowObj, columnObj) + if (clipboardContext) { + rowObj.row[columnObj.title] = convertCellData({ + value: clipboardContext.value, + from: clipboardContext.uidt, + to: columnObj.uidt, + }) + e.preventDefault() + makeEditable(rowObj,columnObj) + } else { + clearCell(selectedCell as { row: number; col: number }, true) + makeEditable(rowObj, columnObj) + } } }