Browse Source

feat: revised convertCellData

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/5847/head
mertmit 1 year ago
parent
commit
3181721e3d
  1. 9
      packages/nc-gui/composables/useMultiSelect/convertCellData.ts
  2. 170
      packages/nc-gui/composables/useMultiSelect/index.ts

9
packages/nc-gui/composables/useMultiSelect/convertCellData.ts

@ -5,13 +5,10 @@ import type { AppInfo } from '~/composables/useGlobal'
import { parseProp } from '#imports'
export default function convertCellData(
args: { from: UITypes; to: UITypes; value: any; column: ColumnType; appInfo: AppInfo },
args: { to: UITypes; value: any; column: ColumnType; appInfo: AppInfo },
isMysql = false,
) {
const { from, to, value } = args
if (from === to && ![UITypes.Attachment, UITypes.Date, UITypes.DateTime, UITypes.Time, UITypes.Year].includes(to)) {
return value
}
const { to, value } = args
const dateFormat = isMysql ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD HH:mm:ssZ'
@ -145,7 +142,7 @@ export default function convertCellData(
case UITypes.Rollup:
case UITypes.Formula:
case UITypes.QrCode:
throw new Error(`Unsupported conversion from ${from} to ${to}`)
throw new Error(`Unsupported conversion for ${to}`)
default:
return value
}

170
packages/nc-gui/composables/useMultiSelect/index.ts

@ -59,8 +59,6 @@ export function useMultiSelect(
const { isMysql } = useProject()
let clipboardContext = $ref<{ value: any; uidt: UITypes } | null>(null)
const editEnabled = ref(_editEnabled)
let isMouseDown = $ref(false)
@ -333,16 +331,6 @@ export function useMultiSelect(
switch (e.keyCode) {
// copy - ctrl/cmd +c
case 67:
// set clipboard context only if single cell selected
// or if selected range is empty
if (selectedRange.isSingleCell() || (selectedRange.isEmpty() && rowObj && columnObj)) {
clipboardContext = {
value: rowObj.row[columnObj.title!],
uidt: columnObj.uidt as UITypes,
}
} else {
clipboardContext = null
}
await copyValue()
break
}
@ -381,113 +369,101 @@ export function useMultiSelect(
return
}
const clipboardData = e.clipboardData?.getData('text/plain')
if (activeCell.row === null || activeCell.row === undefined || activeCell.col === null || activeCell.col === undefined) {
return
}
e.preventDefault()
const clipboardData = e.clipboardData?.getData('text/plain') || ''
const rowObj = unref(data)[activeCell.row]
const columnObj = unref(fields)[activeCell.col]
try {
// handle belongs to column
if (
columnObj.uidt === UITypes.LinkToAnotherRecord &&
(columnObj.colOptions as LinkToAnotherRecordType)?.type === RelationTypes.BELONGS_TO
) {
if (!clipboardContext || typeof clipboardContext.value !== 'object') {
return message.info('Invalid data')
}
rowObj.row[columnObj.title!] = convertCellData(
{
value: clipboardContext.value,
from: clipboardContext.uidt,
to: columnObj.uidt as UITypes,
column: columnObj,
appInfo: unref(appInfo),
},
isMysql(meta.value?.base_id),
)
e.preventDefault()
const foreignKeyColumn = meta.value?.columns?.find(
(column: ColumnType) => column.id === (columnObj.colOptions as LinkToAnotherRecordType)?.fk_child_column_id,
)
if (clipboardData?.includes('\n') || clipboardData?.includes('\t')) {
// if the clipboard data contains new line or tab, then it is a matrix
const pasteMatrix = clipboardData.split('\n').map((row) => row.split('\t'))
const relatedTableMeta = await getMeta((columnObj.colOptions as LinkToAnotherRecordType).fk_related_model_id!)
const pasteMatrixRows = pasteMatrix.length
const pasteMatrixCols = pasteMatrix[0].length
if (!foreignKeyColumn) return
const colsToPaste = unref(fields).slice(activeCell.col, activeCell.col + pasteMatrixCols)
const rowsToPaste = unref(data).slice(activeCell.row, activeCell.row + pasteMatrixRows)
rowObj.row[foreignKeyColumn.title!] = extractPkFromRow(clipboardContext.value, (relatedTableMeta as any)!.columns!)
for (let i = 0; i < pasteMatrixRows; i++) {
for (let j = 0; j < pasteMatrixCols; j++) {
const pasteRow = rowsToPaste[i]
const pasteCol = colsToPaste[j]
return await syncCellData?.({ ...activeCell, updatedColumnTitle: foreignKeyColumn.title })
}
// if it's a virtual column excluding belongs to cell type skip paste
if (isVirtualCol(columnObj)) {
return message.info(t('msg.info.pasteNotSupported'))
}
if (!pasteRow || !pasteCol) {
continue
}
if (clipboardContext) {
rowObj.row[columnObj.title!] = convertCellData(
{
value: clipboardContext.value,
from: clipboardContext.uidt,
to: columnObj.uidt as UITypes,
column: columnObj,
appInfo: unref(appInfo),
},
isMysql(meta.value?.base_id),
)
e.preventDefault()
syncCellData?.(activeCell)
} else {
e.preventDefault()
if (clipboardData?.includes('\n') || clipboardData?.includes('\t')) {
const pasteMatrix = clipboardData.split('\n').map((row) => row.split('\t'))
const pasteMatrixRows = pasteMatrix.length
const pasteMatrixCols = pasteMatrix[0].length
const colsToPaste = unref(fields).slice(activeCell.col, activeCell.col + pasteMatrixCols)
const rowsToPaste = unref(data).slice(activeCell.row, activeCell.row + pasteMatrixRows)
for (let i = 0; i < pasteMatrixRows; i++) {
for (let j = 0; j < pasteMatrixCols; j++) {
const pasteRow = rowsToPaste[i]
const pasteCol = colsToPaste[j]
if (!pasteRow || !pasteCol) {
continue
}
pasteRow.row[pasteCol.title!] = convertCellData(
{
value: pasteMatrix[i][j],
from: UITypes.SingleLineText,
to: pasteCol.uidt as UITypes,
column: pasteCol,
appInfo: unref(appInfo),
},
isMysql(meta.value?.base_id),
)
// skip pasting virtual columns for now
if (isVirtualCol(columnObj)) {
continue
}
pasteRow.row[pasteCol.title!] = convertCellData(
{
value: pasteMatrix[i][j],
to: pasteCol.uidt as UITypes,
column: pasteCol,
appInfo: unref(appInfo),
},
isMysql(meta.value?.base_id),
)
}
console.log(pasteMatrix)
}
await updateMultipleRows?.(rowsToPaste)
} else {
// handle belongs to column
if (
columnObj.uidt === UITypes.LinkToAnotherRecord &&
(columnObj.colOptions as LinkToAnotherRecordType)?.type === RelationTypes.BELONGS_TO
) {
const clipboardContext = JSON.parse(clipboardData!)
await updateMultipleRows?.(rowsToPaste)
} else {
rowObj.row[columnObj.title!] = convertCellData(
{
value: clipboardData,
from: UITypes.SingleLineText,
value: clipboardContext,
to: columnObj.uidt as UITypes,
column: columnObj,
appInfo: unref(appInfo),
},
isMysql(meta.value?.base_id),
)
syncCellData?.(activeCell)
const foreignKeyColumn = meta.value?.columns?.find(
(column: ColumnType) => column.id === (columnObj.colOptions as LinkToAnotherRecordType)?.fk_child_column_id,
)
const relatedTableMeta = await getMeta((columnObj.colOptions as LinkToAnotherRecordType).fk_related_model_id!)
if (!foreignKeyColumn) return
rowObj.row[foreignKeyColumn.title!] = extractPkFromRow(clipboardContext, (relatedTableMeta as any)!.columns!)
return await syncCellData?.({ ...activeCell, updatedColumnTitle: foreignKeyColumn.title })
}
// if it's a virtual column excluding belongs to cell type skip paste
if (isVirtualCol(columnObj)) {
return message.info(t('msg.info.pasteNotSupported'))
}
// clearCell(activeCell as { row: number; col: number }, true)
// makeEditable(rowObj, columnObj)
rowObj.row[columnObj.title!] = convertCellData(
{
value: clipboardData,
to: columnObj.uidt as UITypes,
column: columnObj,
appInfo: unref(appInfo),
},
isMysql(meta.value?.base_id),
)
await syncCellData?.(activeCell)
}
} catch (error: any) {
message.error(await extractSdkResponseErrorMsg(error))

Loading…
Cancel
Save