Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chat2db-community-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"test:monaco-lifecycle": "tsx src/utils/monaco.test.ts && tsx src/components/SingleFileMonacoEditor/refAdapter.test.ts",
"test:mcp-lifecycle": "tsx src/blocks/Setting/McpSetting/mcpLifecycle.test.ts",
"test:sql-completion-context": "tsx src/components/SQLEditor/core/sqlCompletionContext.test.ts && tsx src/components/SQLEditor/core/sqlCompletionRequestError.test.ts",
"test:sql-in-clipboard": "tsx src/utils/sqlInClipboard.test.ts && tsx src/components/SQLEditor/helper/sqlInsertValueDefaults.test.ts",
"test:sql-in-clipboard": "tsx src/utils/clipboardText.test.ts && tsx src/utils/internalClipboard.test.ts && tsx src/utils/sqlInClipboard.test.ts && tsx src/components/SQLEditor/helper/sqlInsertValueDefaults.test.ts",
"test:console-tab-name": "tsx src/store/workspace/utils/consoleTabName.test.ts && tsx src/utils/workspaceObjectTabTitle.test.ts",
"test:workspace-tab-scroll": "tsx src/store/workspace/utils/workspaceTabScrollRequest.test.ts && tsx src/components/Tabs/activeTabScroll.test.ts && tsx src/components/Tabs/wheelScroll.test.ts && tsx src/components/Tabs/tabAccentColor.test.ts",
"test:workspace-split-lifecycle": "tsx src/pages/main/workspace/components/WorkspaceTabs/workspaceTabContentLayout.test.ts && tsx src/pages/main/workspace/components/WorkspaceTabs/workspaceTabLayout.test.ts",
Expand Down
34 changes: 34 additions & 0 deletions chat2db-community-client/src/utils/clipboardText.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import assert from 'node:assert/strict';
import { normalizeClipboardText } from './clipboardText';
import { getInternalResultGridClipboard, setInternalResultGridClipboard, clearInternalClipboard } from './internalClipboard';

const windowsAgents = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
'Mozilla/5.0 (Windows NT 6.1)',
'Win32',
];

for (const userAgent of windowsAgents) {
assert.equal(normalizeClipboardText('1001\n1002\n1003', userAgent), '1001\r\n1002\r\n1003');
assert.equal(normalizeClipboardText('a\r\nb\nc\rd\n', userAgent), 'a\r\nb\r\nc\r\nd\r\n');
assert.equal(normalizeClipboardText('a\r\nb', userAgent), 'a\r\nb', 'existing CRLF must not acquire extra CR');
assert.equal(normalizeClipboardText('中文\t001\n\t002', userAgent), '中文\t001\r\n\t002');
assert.equal(normalizeClipboardText('single value', userAgent), 'single value');
assert.equal(normalizeClipboardText('', userAgent), '');
}

for (const userAgent of ['Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', 'Mozilla/5.0 (X11; Linux x86_64)']) {
const text = '中文\t001\r\nvalue\nnext\rlast';
assert.equal(normalizeClipboardText(text, userAgent), text, 'non-Windows copy preserves the original text');
}

const rows = [['first\nsecond', 'value\twith tab'], ['CRLF\r\nvalue', 'CR\rvalue']];
setInternalResultGridClipboard(rows);
const windowsText = normalizeClipboardText(rows.map((row) => row.join('\t')).join('\n'), windowsAgents[0]);
assert.deepEqual(getInternalResultGridClipboard(windowsText), rows, 'Windows paste retains original cell boundaries and values');
assert.deepEqual(getInternalResultGridClipboard(windowsText.replace(/\r\n/g, '\n')), rows, 'browser LF normalization retains grid metadata');
assert.equal(getInternalResultGridClipboard(`${windowsText}x`), null, 'different text must not reuse grid metadata');
clearInternalClipboard();
assert.equal(getInternalResultGridClipboard(windowsText), null);

console.log('Clipboard text and Windows result-grid round-trip tests passed');
4 changes: 4 additions & 0 deletions chat2db-community-client/src/utils/clipboardText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function normalizeClipboardText(text: string, userAgent: string) {
// Windows text clipboard formats require CRLF, including for legacy native applications.
return /Windows|Win32|Win64/i.test(userAgent) ? text.replace(/\r\n|\r|\n/g, '\r\n') : text;
}
3 changes: 2 additions & 1 deletion chat2db-community-client/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import queryString from 'query-string';
import React from 'react';
import { v4 as uuid } from 'uuid';
import { isDesktop } from './env';
import { normalizeClipboardText } from './clipboardText';
import { clearInternalClipboard } from './internalClipboard';
import { findNode } from './treeNodeLookup';

Expand Down Expand Up @@ -321,7 +322,7 @@ export function copyToClipboard(
_copyToClipboard(' ', { format: 'text/plain' });
return _copyToClipboard('', { format: 'text/plain' });
}
return _copyToClipboard(text, { format: 'text/plain' });
return _copyToClipboard(normalizeClipboardText(text, navigator.userAgent), { format: 'text/plain' });

// staticMessage.success('Copied to clipboard');
} catch {
Expand Down
4 changes: 2 additions & 2 deletions chat2db-community-client/src/utils/internalClipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ export function clearInternalClipboard() {

export function setInternalResultGridClipboard(rows: string[][]) {
internalResultGridClipboard = {
text: serializeGrid(rows),
text: serializeGrid(rows).replace(/\r\n?/g, '\n'),
rows: rows.map((row) => [...row]),
};
}

export function getInternalResultGridClipboard(text: string) {
if (internalResultGridClipboard?.text !== text) {
if (internalResultGridClipboard?.text !== text.replace(/\r\n?/g, '\n')) {
return null;
}
return internalResultGridClipboard.rows.map((row) => [...row]);
Expand Down
Loading