Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

fix: pass clipboard cell data to column helper

Ramesh Maned266868f84a2
diff --git a/packages/nc-gui/components/smartsheet/grid/canvas/composables/useCopyPaste.ts b/packages/nc-gui/components/smartsheet/grid/canvas/composables/useCopyPaste.ts
index e8c33428a1..0271f1c364 100644
--- a/packages/nc-gui/components/smartsheet/grid/canvas/composables/useCopyPaste.ts
+++ b/packages/nc-gui/components/smartsheet/grid/canvas/composables/useCopyPaste.ts
@@ -123,7 +123,13 @@ export function useCopyPaste({
const { isAllowed } = usePermissions()
const { maxAttachmentsAllowedInCell, showUpgradeToAddMoreAttachmentsInCell } = useEeConfig()
const { batchUploadFiles } = useAttachment()
- const { setCellClipboardDataItem, getClipboardItemId } = useNcClipboardData()
+ const {
+ setCellClipboardDataItem,
+ getClipboardItemId,
+ getCurrentCopiedCellClipboardData,
+ extractCellClipboardData,
+ waitingCellClipboardDataIds,
+ } = useNcClipboardData()
const reloadViewDataHook = inject(ReloadViewDataHookInj, createEventHook())
const isPublic = inject(IsPublicInj, ref(false))
@@ -221,6 +227,13 @@ export function useCopyPaste({
// Replace \" with " in clipboard data
let clipboardData = e.clipboardData?.getData('text/plain') || ''
+ const storedCopiedData = getCurrentCopiedCellClipboardData(clipboardData)
+
+ // Add the waiting clipboard data id so that if setClipboardData fn is called during this operation, it will not remove the currentCopiedCellClipboardDataItem from the clipboard data
+ if (storedCopiedData && !waitingCellClipboardDataIds.value.includes(storedCopiedData.id)) {
+ waitingCellClipboardDataIds.value.push(storedCopiedData.id)
+ }
+
if (clipboardData?.endsWith('\n')) {
// Remove '\n' from the end of the clipboardData
// When copying from XLS/XLSX files, there is an extra '\n' appended to the end
@@ -231,6 +244,7 @@ export function useCopyPaste({
try {
if (clipboardData?.includes('\n') || clipboardData?.includes('\t')) {
// if the clipboard data contains new line or tab, then it is a matrix or LongText
+
const parsedClipboard = parse(clipboardData, {
delimiter: '\t',
escapeChar: '\\',
@@ -389,6 +403,7 @@ export function useCopyPaste({
markInfoShown: () => {
isColInfoShown[column.title!] = true
},
+ clipboardItem: extractCellClipboardData(storedCopiedData, clipboardRowIndex, j),
},
isMysql(meta.value?.source_id),
true,
@@ -454,6 +469,7 @@ export function useCopyPaste({
to: columnObj.uidt as UITypes,
column: columnObj,
appInfo: unref(appInfo),
+ clipboardItem: extractCellClipboardData(storedCopiedData, 0, 0),
},
isMysql(meta.value?.source_id),
)
@@ -493,6 +509,7 @@ export function useCopyPaste({
to: columnObj.uidt as UITypes,
column: columnObj,
appInfo: unref(appInfo),
+ clipboardItem: extractCellClipboardData(storedCopiedData, 0, 0),
},
isMysql(meta.value?.source_id),
)
@@ -649,6 +666,7 @@ export function useCopyPaste({
oldValue: rowObj.row[columnObj.title!],
maxAttachmentsAllowedInCell: maxAttachmentsAllowedInCell.value,
showUpgradeToAddMoreAttachmentsInCell,
+ clipboardItem: extractCellClipboardData(storedCopiedData, 0, 0),
},
isMysql(meta.value?.source_id),
)
@@ -759,6 +777,7 @@ export function useCopyPaste({
markInfoShown: () => {
isColInfoShown[col.title!] = true
},
+ clipboardItem: extractCellClipboardData(storedCopiedData, 0, 0),
},
isMysql(meta.value?.source_id),
true,
@@ -797,6 +816,11 @@ export function useCopyPaste({
console.error(error, (error as SuppressedError).isErrorSuppressed)
message.error(await extractSdkResponseErrorMsg(error))
}
+ } finally {
+ // After paste operation is completed, remove the waiting clipboard data id so that on setClipboardDateItem can remove the item from the clipboard data
+ if (storedCopiedData && waitingCellClipboardDataIds.value.includes(storedCopiedData.id)) {
+ waitingCellClipboardDataIds.value = waitingCellClipboardDataIds.value.filter((id) => id !== storedCopiedData.id)
+ }
}
}
@@ -1050,7 +1074,7 @@ export function useCopyPaste({
await copy(plainTextValue)
const clipboardItem: NcClipboardDataItemType = {
- dbCellValue: [[cellValue]],
+ dbCellValueArr: [[cellValue]],
columns: [clipboardColumn],
copiedPlainText: plainTextValue,
rowIds: [rowId],
diff --git a/packages/nc-gui/composables/useMultiSelect/convertCellData.ts b/packages/nc-gui/composables/useMultiSelect/convertCellData.ts
index f8ea87cb9d..de7d0b6461 100644
--- a/packages/nc-gui/composables/useMultiSelect/convertCellData.ts
+++ b/packages/nc-gui/composables/useMultiSelect/convertCellData.ts
@@ -1,4 +1,4 @@
-import type { AttachmentType, ColumnType } from 'nocodb-sdk'
+import type { AttachmentType, ColumnType, SerializerOrParserFnProps } from 'nocodb-sdk'
import { ColumnHelper, UITypes, populateUniqueFileName } from 'nocodb-sdk'
import type { AppInfo } from '~/composables/useGlobal/types'
@@ -18,6 +18,7 @@ export default function convertCellData(
}) => boolean | undefined
isInfoShown?: boolean
markInfoShown?: () => void
+ clipboardItem?: SerializerOrParserFnProps['params']['clipboardItem']
},
isMysql = false,
isMultiple = false,
@@ -73,6 +74,7 @@ export default function convertCellData(
col: column,
isMysql: (_sourceId) => isMysql,
isMultipleCellPaste: isMultiple,
+ clipboardItem: args.clipboardItem,
})
serializedValue = handlePostSerialize(serializedValue, value)
diff --git a/packages/nc-gui/composables/useMultiSelect/index.ts b/packages/nc-gui/composables/useMultiSelect/index.ts
index 3b878fe688..3497f91a13 100644
--- a/packages/nc-gui/composables/useMultiSelect/index.ts
+++ b/packages/nc-gui/composables/useMultiSelect/index.ts
@@ -241,7 +241,7 @@ export function useMultiSelect(
await copy(plainTextValue)
const clipboardItem: NcClipboardDataItemType = {
- dbCellValue: [[cellValue]],
+ dbCellValueArr: [[cellValue]],
columns: [clipboardColumn],
copiedPlainText: plainTextValue,
rowIds: [rowId],
diff --git a/packages/nc-gui/composables/useNcClipboardData.ts b/packages/nc-gui/composables/useNcClipboardData.ts
index 74dfc849da..e89ca06f25 100644
--- a/packages/nc-gui/composables/useNcClipboardData.ts
+++ b/packages/nc-gui/composables/useNcClipboardData.ts
@@ -1,4 +1,5 @@
import { useStorage } from '@vueuse/core'
+import type { SerializerOrParserFnProps } from 'nocodb-sdk'
import { extractProps } from 'nocodb-sdk'
/**
@@ -18,17 +19,16 @@ const useNcClipboardData = () => {
const waitingCellClipboardDataIds = useStorage<string[]>(NcClipboardDataKey.ncWaitingClipboardDataIds, [])
- const getCurrentCopiedCellClipboardData = (): NcClipboardDataItemType | null => {
- if (
- !cellClipboardData.value ||
- !currentCellClipboardDataId.value ||
- ncIsObject(cellClipboardData.value) ||
- !cellClipboardData.value[currentCellClipboardDataId.value]
- ) {
+ const getCurrentCopiedCellClipboardData = (clipboardData: string): NcClipboardDataItemType | null => {
+ if (!currentCellClipboardDataId.value || !cellClipboardData.value?.[currentCellClipboardDataId.value]) {
return null
}
- return cellClipboardData.value[currentCellClipboardDataId.value]
+ const currentClipboardDataItem = cellClipboardData.value?.[currentCellClipboardDataId.value] as NcClipboardDataItemType
+
+ return currentClipboardDataItem?.copiedPlainText === clipboardData && currentClipboardDataItem.dbCellValueArr.length
+ ? currentClipboardDataItem
+ : null
}
const getClipboardItemId = (): string => {
@@ -55,6 +55,20 @@ const useNcClipboardData = () => {
return item
}
+ const extractCellClipboardData = (
+ storedClipboardData: NcClipboardDataItemType | null,
+ rowIndex: number,
+ columnIndex: number,
+ ): SerializerOrParserFnProps['params']['clipboardItem'] | undefined => {
+ if (!storedClipboardData || !storedClipboardData.columns?.[columnIndex]) return
+
+ return {
+ dbCellValue: storedClipboardData.dbCellValueArr?.[rowIndex]?.[columnIndex],
+ column: storedClipboardData.columns?.[columnIndex],
+ rowId: storedClipboardData.rowIds?.[rowIndex],
+ }
+ }
+
const resetCellClipboard = () => {
cellClipboardData.value = {}
currentCellClipboardDataId.value = ''
@@ -69,6 +83,7 @@ const useNcClipboardData = () => {
setCellClipboardDataItem,
resetCellClipboard,
getClipboardItemId,
+ extractCellClipboardData,
}
}
diff --git a/packages/nc-gui/lib/types.ts b/packages/nc-gui/lib/types.ts
index f548c6af9d..28c45c6191 100644
--- a/packages/nc-gui/lib/types.ts
+++ b/packages/nc-gui/lib/types.ts
@@ -922,7 +922,7 @@ interface NcClipboardDataItemType {
rowIds: string[]
copiedPlainText: string
copiedHtml?: string
- dbCellValue: Array<any[]>
+ dbCellValueArr: Array<any[]>
columns: Partial<ColumnType>[]
}
diff --git a/packages/nc-gui/utils/pasteUtils.ts b/packages/nc-gui/utils/pasteUtils.ts
index 72d9095b64..64c5043951 100644
--- a/packages/nc-gui/utils/pasteUtils.ts
+++ b/packages/nc-gui/utils/pasteUtils.ts
@@ -81,7 +81,7 @@ export const serializeRange = (
const json: string[][] = []
const clipboardItemConfig: Pick<
NcClipboardDataItemType,
- 'columns' | 'rowIds' | 'dbCellValue' | 'copiedPlainText' | 'copiedHtml'
+ 'columns' | 'rowIds' | 'dbCellValueArr' | 'copiedPlainText' | 'copiedHtml'
> = {
columns: cols.map((col) => {
return ColumnHelper.getClipboardConfig({
@@ -89,7 +89,7 @@ export const serializeRange = (
}).column
}),
rowIds: [],
- dbCellValue: [],
+ dbCellValueArr: [],
copiedPlainText: '',
copiedHtml: '',
}
@@ -120,7 +120,7 @@ export const serializeRange = (
json.push(jsonRow)
- clipboardItemConfig.dbCellValue!.push(clipboardCellValue)
+ clipboardItemConfig.dbCellValueArr!.push(clipboardCellValue)
clipboardItemConfig.rowIds!.push(recordId)
})
html += '</table>'
diff --git a/packages/nocodb-sdk/src/lib/columnHelper/column.interface.ts b/packages/nocodb-sdk/src/lib/columnHelper/column.interface.ts
index 31656a3459..46504a46e2 100644
--- a/packages/nocodb-sdk/src/lib/columnHelper/column.interface.ts
+++ b/packages/nocodb-sdk/src/lib/columnHelper/column.interface.ts
@@ -97,17 +97,9 @@ export interface SerializerOrParserFnProps {
export type NcClipboardDataType = Record<string, NcClipboardItemType>;
export interface NcClipboardItemType {
- /**
- * Unique clipboard item id
- */
- id: string;
- workspaceId: string;
- tableId: string;
- rowIds: string[];
- copiedPlainText: string;
- copiedHtml: string;
+ rowId: string;
dbCellValue: any;
- columns: Partial<ColumnType>[];
+ column: Partial<ColumnType>;
}
export interface NcClipboardItemForColumnType {}
diff --git a/packages/nocodb-sdk/src/lib/columnHelper/columns/Date.ts b/packages/nocodb-sdk/src/lib/columnHelper/columns/Date.ts
index f177ab0af0..77940e70c6 100644
--- a/packages/nocodb-sdk/src/lib/columnHelper/columns/Date.ts
+++ b/packages/nocodb-sdk/src/lib/columnHelper/columns/Date.ts
@@ -26,7 +26,7 @@ export class DateHelper extends AbstractColumnHelper {
params: SerializerOrParserFnProps['params']
): string | null {
if (params.serializeSearchQuery) return null;
- value = serializeDateOrDateTimeValue(value, params.col);
+ value = serializeDateOrDateTimeValue(value, params);
if (value === null) {
if (params.isMultipleCellPaste) {
diff --git a/packages/nocodb-sdk/src/lib/columnHelper/columns/DateTime.ts b/packages/nocodb-sdk/src/lib/columnHelper/columns/DateTime.ts
index e4520c7625..40d1388dc1 100644
--- a/packages/nocodb-sdk/src/lib/columnHelper/columns/DateTime.ts
+++ b/packages/nocodb-sdk/src/lib/columnHelper/columns/DateTime.ts
@@ -33,7 +33,7 @@ export class DateTimeHelper extends AbstractColumnHelper {
): string | null {
if (params.serializeSearchQuery) return null;
- value = serializeDateOrDateTimeValue(value, params.col);
+ value = serializeDateOrDateTimeValue(value, params);
if (value === null) {
if (params.isMultipleCellPaste) {
diff --git a/packages/nocodb-sdk/src/lib/columnHelper/utils/date-time.spec.ts b/packages/nocodb-sdk/src/lib/columnHelper/utils/date-time.spec.ts
index b9de25920e..085e367371 100644
--- a/packages/nocodb-sdk/src/lib/columnHelper/utils/date-time.spec.ts
+++ b/packages/nocodb-sdk/src/lib/columnHelper/utils/date-time.spec.ts
@@ -32,9 +32,9 @@ describe('parse date', () => {
it('should serialize a valid date value for Date type', () => {
const col = { uidt: UITypes.Date, meta: null };
- expect(serializeDateOrDateTimeValue('2023-10-27', col as any)).toBe(
- '2023-10-27'
- );
+ expect(
+ serializeDateOrDateTimeValue('2023-10-27', { col: col as any })
+ ).toBe('2023-10-27');
});
});
@@ -101,14 +101,18 @@ describe('serialize', () => {
const value = '2023-10-27 10:00:00';
const expected = dayjs(value).utc().format('YYYY-MM-DD HH:mm:ssZ');
- expect(serializeDateOrDateTimeValue(value, col as any)).toBe(expected);
+ expect(serializeDateOrDateTimeValue(value, { col: col as any })).toBe(
+ expected
+ );
});
it('should serialize a date string to datetime format for DateTime type', () => {
const col = { uidt: UITypes.DateTime, meta: {} };
const value = '2023-10-27';
const expected = dayjs(value).utc().format('YYYY-MM-DD HH:mm:ssZ');
- expect(serializeDateOrDateTimeValue(value, col as any)).toBe(expected);
+ expect(serializeDateOrDateTimeValue(value, { col: col as any })).toBe(
+ expected
+ );
});
it('should serialize a date string to datetime format for custom date format (1)', () => {
@@ -118,7 +122,9 @@ describe('serialize', () => {
};
const value = '09/05/1980';
const expected = dayjs(value, 'MM/DD/YYYY').format('YYYY-MM-DD');
- expect(serializeDateOrDateTimeValue(value, col as any)).toBe(expected);
+ expect(serializeDateOrDateTimeValue(value, { col: col as any })).toBe(
+ expected
+ );
});
it('should serialize a date string to datetime format for custom date format (2)', () => {
@@ -128,6 +134,8 @@ describe('serialize', () => {
};
const value = '09/05/1980';
const expected = dayjs(value, 'DD/MM/YYYY').format('YYYY-MM-DD');
- expect(serializeDateOrDateTimeValue(value, col as any)).toBe(expected);
+ expect(serializeDateOrDateTimeValue(value, { col: col as any })).toBe(
+ expected
+ );
});
});
diff --git a/packages/nocodb-sdk/src/lib/columnHelper/utils/date-time.ts b/packages/nocodb-sdk/src/lib/columnHelper/utils/date-time.ts
index 014693f3e1..b8a2b14739 100644
--- a/packages/nocodb-sdk/src/lib/columnHelper/utils/date-time.ts
+++ b/packages/nocodb-sdk/src/lib/columnHelper/utils/date-time.ts
@@ -81,7 +81,7 @@ export const parseDateTimeValue = (
export const serializeDateOrDateTimeValue = (
value: string | null,
- col: ColumnType
+ params: SerializerOrParserFnProps['params']
) => {
if (!value) return null;
@@ -91,8 +91,8 @@ export const serializeDateOrDateTimeValue = (
isDateOnly = true;
}
const formatting = isDateOnly
- ? parseProp(col.meta).date_format ?? 'YYYY-MM-DD'
- : constructDateTimeFormat(col);
+ ? parseProp(params.col.meta).date_format ?? 'YYYY-MM-DD'
+ : constructDateTimeFormat(params.col);
let parsedDateOrDateTime = dayjs(value, formatting);
@@ -107,7 +107,7 @@ export const serializeDateOrDateTimeValue = (
return null;
}
- return col.uidt === UITypes.Date
+ return params.col.uidt === UITypes.Date
? parsedDateOrDateTime.format('YYYY-MM-DD')
: parsedDateOrDateTime.utc().format('YYYY-MM-DD HH:mm:ssZ');
};