diff --git a/packages/nc-gui/composables/useMultiSelect/convertCellData.ts b/packages/nc-gui/composables/useMultiSelect/convertCellData.ts index 5a6c01530a..97bad12d8a 100644 --- a/packages/nc-gui/composables/useMultiSelect/convertCellData.ts +++ b/packages/nc-gui/composables/useMultiSelect/convertCellData.ts @@ -251,12 +251,20 @@ export default function convertCellData( return undefined } - const parsedVal = typeof value === 'string' ? JSON.parse(value) : value - if (!(parsedVal && typeof parsedVal === 'object' && !Array.isArray(parsedVal) && Object.keys(parsedVal))) { + if ((column.colOptions as LinkToAnotherRecordType)?.type === RelationTypes.MANY_TO_MANY) { + const parsedVal = typeof value === 'string' ? JSON.parse(value) : value + + if ( + !(parsedVal && typeof parsedVal === 'object' && !Array.isArray(parsedVal) && Object.keys(parsedVal)) || + parsedVal?.fk_related_model_id !== (column.colOptions as LinkToAnotherRecordType)?.fk_related_model_id + ) { + throw new Error(`Unsupported conversion for ${to}`) + } + + return parsedVal + } else { throw new Error(`Unsupported conversion for ${to}`) } - - return parsedVal } case UITypes.Links: { if (isMultiple) { @@ -265,6 +273,7 @@ export default function convertCellData( if ((column.colOptions as LinkToAnotherRecordType)?.type === RelationTypes.MANY_TO_MANY) { const parsedVal = typeof value === 'string' ? JSON.parse(value) : value + if ( !( parsedVal && diff --git a/packages/nc-gui/composables/useMultiSelect/index.ts b/packages/nc-gui/composables/useMultiSelect/index.ts index d41ac0f826..398f7f4e8c 100644 --- a/packages/nc-gui/composables/useMultiSelect/index.ts +++ b/packages/nc-gui/composables/useMultiSelect/index.ts @@ -137,6 +137,15 @@ export function useMultiSelect( } } + if ( + typeof textToCopy === 'object' && + columnObj.uidt === UITypes.LinkToAnotherRecord && + (columnObj.colOptions as LinkToAnotherRecordType).type === RelationTypes.BELONGS_TO + ) { + // fk_related_model_id is used to prevent paste operation in different fk_related_model_id cell + textToCopy = { ...textToCopy, fk_related_model_id: (columnObj.colOptions as LinkToAnotherRecordType).fk_related_model_id } + } + if (typeof textToCopy === 'object') { textToCopy = JSON.stringify(textToCopy) } else { @@ -871,10 +880,9 @@ export function useMultiSelect( columnObj.uidt === UITypes.LinkToAnotherRecord && (columnObj.colOptions as LinkToAnotherRecordType)?.type === RelationTypes.BELONGS_TO ) { - const clipboardContext = JSON.parse(clipboardData!) const pasteVal = convertCellData( { - value: clipboardContext, + value: clipboardData, to: columnObj.uidt as UITypes, column: columnObj, appInfo: unref(appInfo), @@ -894,7 +902,7 @@ export function useMultiSelect( if (!foreignKeyColumn) return - rowObj.row[foreignKeyColumn.title!] = extractPkFromRow(clipboardContext, (relatedTableMeta as any)!.columns!) + rowObj.row[foreignKeyColumn.title!] = extractPkFromRow(pasteVal, (relatedTableMeta as any)!.columns!) return await syncCellData?.({ ...activeCell, updatedColumnTitle: foreignKeyColumn.title }) }