forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse.ts
More file actions
53 lines (43 loc) · 1.8 KB
/
mouse.ts
File metadata and controls
53 lines (43 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { Locator, Page } from "@playwright/test";
import { DRAG_HANDLE_SELECTOR } from "./const";
async function getElementLeftCoords(page: Page, element: Locator) {
const boundingBox = await element.boundingBox();
const centerY = boundingBox.y + boundingBox.height / 2;
return { x: boundingBox.x + 1, y: centerY };
}
async function getElementRightCoords(page: Page, element: Locator) {
const boundingBox = await element.boundingBox();
const centerY = boundingBox.y + boundingBox.height / 2;
return { x: boundingBox.x + boundingBox.width - 1, y: centerY };
}
async function getElementCenterCoords(page: Page, element: Locator) {
const boundingBox = await element.boundingBox();
const centerX = boundingBox.x + boundingBox.width / 2;
const centerY = boundingBox.y + boundingBox.height / 2;
return { x: centerX, y: centerY };
}
export async function moveMouseOverElement(page: Page, element: Locator) {
const boundingBox = await element.boundingBox();
const coords = { x: boundingBox.x, y: boundingBox.y };
await page.mouse.move(coords.x, coords.y, { steps: 5 });
}
export async function dragAndDropBlock(
page: Page,
dragTarget: Locator,
dropTarget: Locator,
dropAbove: boolean
) {
await moveMouseOverElement(page, dragTarget);
await page.waitForSelector(DRAG_HANDLE_SELECTOR);
const dragHandle = await page.locator(DRAG_HANDLE_SELECTOR);
const dragHandleCenterCoords = await getElementCenterCoords(page, dragHandle);
await page.mouse.move(dragHandleCenterCoords.x, dragHandleCenterCoords.y, {
steps: 5,
});
await page.mouse.down();
const dropTargetCoords = dropAbove
? await getElementLeftCoords(page, dropTarget)
: await getElementRightCoords(page, dropTarget);
await page.mouse.move(dropTargetCoords.x, dropTargetCoords.y, { steps: 5 });
await page.mouse.up();
}