mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 04:48:53 +00:00
fix(editor): paste when select multiple block texts (#10227)
[BS-2512](https://linear.app/affine-design/issue/BS-2512/选中多段粘贴,多段只有第一段会被replace,这个bug还在)
This commit is contained in:
24
tests/kit/src/utils/clipboard.ts
Normal file
24
tests/kit/src/utils/clipboard.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import type { Page } from '@playwright/test';
|
||||
|
||||
export async function pasteContent(
|
||||
page: Page,
|
||||
clipData: Record<string, unknown>
|
||||
) {
|
||||
await page.evaluate(
|
||||
({ clipData }) => {
|
||||
const e = new ClipboardEvent('paste', {
|
||||
clipboardData: new DataTransfer(),
|
||||
});
|
||||
Object.defineProperty(e, 'target', {
|
||||
writable: false,
|
||||
value: document,
|
||||
});
|
||||
Object.keys(clipData).forEach(key => {
|
||||
e.clipboardData?.setData(key, clipData[key] as string);
|
||||
});
|
||||
document.dispatchEvent(e);
|
||||
},
|
||||
{ clipData }
|
||||
);
|
||||
await page.waitForTimeout(100);
|
||||
}
|
||||
55
tests/kit/src/utils/selection.ts
Normal file
55
tests/kit/src/utils/selection.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import type { RichText } from '@blocksuite/affine-components/rich-text';
|
||||
import type { Page } from '@playwright/test';
|
||||
|
||||
export async function setSelection(
|
||||
page: Page,
|
||||
anchorBlockId: string,
|
||||
anchorOffset: number,
|
||||
focusBlockId: string,
|
||||
focusOffset: number
|
||||
) {
|
||||
await page.evaluate(
|
||||
({ anchorBlockId, anchorOffset, focusBlockId, focusOffset }) => {
|
||||
const editorHost = document.querySelector('editor-host');
|
||||
if (!editorHost) {
|
||||
throw new Error('Cannot find editor host');
|
||||
}
|
||||
|
||||
const anchorRichText = editorHost.querySelector<RichText>(
|
||||
`[data-block-id="${anchorBlockId}"] rich-text`
|
||||
)!;
|
||||
const anchorRichTextRange = anchorRichText.inlineEditor!.toDomRange({
|
||||
index: anchorOffset,
|
||||
length: 0,
|
||||
})!;
|
||||
const focusRichText = editorHost.querySelector<RichText>(
|
||||
`[data-block-id="${focusBlockId}"] rich-text`
|
||||
)!;
|
||||
const focusRichTextRange = focusRichText.inlineEditor!.toDomRange({
|
||||
index: focusOffset,
|
||||
length: 0,
|
||||
})!;
|
||||
|
||||
const sl = getSelection();
|
||||
if (!sl) throw new Error('Cannot get selection');
|
||||
|
||||
const range = document.createRange();
|
||||
range.setStart(
|
||||
anchorRichTextRange.startContainer,
|
||||
anchorRichTextRange.startOffset
|
||||
);
|
||||
range.setEnd(
|
||||
focusRichTextRange.startContainer,
|
||||
focusRichTextRange.startOffset
|
||||
);
|
||||
sl.removeAllRanges();
|
||||
sl.addRange(range);
|
||||
},
|
||||
{
|
||||
anchorBlockId,
|
||||
anchorOffset,
|
||||
focusBlockId,
|
||||
focusOffset,
|
||||
}
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user