Files
dbx/apps/desktop/src/lib/editor/queryEditorClipboardPaste.ts
T
zipgandzipg ed1881e323 fix(editor): 右键粘贴含 CRLF 的剪贴板文本不再报选区越界 (#9576)
SQL 编辑器里选中一百多行后,右键“粘贴”从 Excel 复制的表格数据会报
“读取剪贴板失败:Selection points outside of document”,文档内容不变;
先删除文本再粘贴则正常。

右键粘贴按剪贴板原文长度计算光标位置,而 CodeMirror 会把插入文本中的
\r\n、\r 归一化为 \n,实际写入的文本比原文短,光标落到文档末尾之后,
dispatch 抛出 RangeError: Selection points outside of document。

- 新增 queryEditorClipboardPasteChange:先归一行尾再按实际插入长度计算光标
- QueryEditor 右键粘贴改用它,选区长度不再影响粘贴是否成功

Co-authored-by: zipg <4047349+zipg@users.noreply.github.com>
2026-09-19 13:31:40 +08:00

23 lines
1.1 KiB
TypeScript

import { normalizeQueryEditorPasteText } from "./queryEditorLargePaste";
export interface QueryEditorPasteChange {
changes: { from: number; to: number; insert: string };
selection: { anchor: number; head: number };
}
/**
* Build the "replace [from, to) with clipboard text" transaction for the query editor.
*
* CodeMirror normalizes the line endings of inserted text (`\r\n` and `\r` become `\n`), so a
* caret derived from the raw clipboard length overshoots the new document whenever the clipboard
* holds CRLF text (Excel rows, Windows editors) and `dispatch` throws
* `RangeError: Selection points outside of document` -- it only survives while the replaced
* selection leaves at least that many characters behind it. Deriving the caret from the text
* that actually lands in the document keeps the paste working for any selection.
*/
export function queryEditorClipboardPasteChange(rawText: string, from: number, to: number): QueryEditorPasteChange {
const insert = normalizeQueryEditorPasteText(rawText);
const caret = from + insert.length;
return { changes: { from, to, insert }, selection: { anchor: caret, head: caret } };
}