Skip to content
Open
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
29 changes: 29 additions & 0 deletions packages/vanilla/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
// If a single click is being performed, it's a single-click until the user dragged the mouse
private _singleClick = true;
private _frame: Frames;
private _pointer?: { id: number; button: number; x: number; y: number };
private _activePointerId?: number;

// Required data for scrolling
private _scrollAvailable = true;
Expand Down Expand Up @@ -160,15 +162,31 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
const { document, features } = this._options;
const fn = activate ? on : off;

fn(document, 'pointerdown', this._onPointerStart);
fn(document, 'mousedown', this._onTapStart);

if (features.touch) {
fn(document, 'touchstart', this._onTapStart, { passive: false });
}
}

_onPointerStart(evt: PointerEvent): void {
this._pointer =
evt.pointerType === 'mouse'
? { id: evt.pointerId, button: evt.button, x: evt.clientX, y: evt.clientY }
: undefined;
}

_onTapStart(evt: MouseEvent | TouchEvent, silent = false): void {
const { x, y, target } = simplifyEvent(evt);
const pointer =
evt instanceof MouseEvent &&
this._pointer?.button === evt.button &&
this._pointer.x === evt.clientX &&
this._pointer.y === evt.clientY
? this._pointer
: undefined;
this._pointer = undefined;
const { document, startAreas, boundaries, features, behaviour } = this._options;
const targetBoundingClientRect = target.getBoundingClientRect();

Expand Down Expand Up @@ -198,6 +216,7 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
return;
}

this._activePointerId = pointer?.id;
this._areaLocation = { x1: x, y1: y, x2: 0, y2: 0 };

// Lock scrolling in the target container
Expand Down Expand Up @@ -315,10 +334,15 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
off(document, ['mousemove', 'touchmove'], this._delayedTapMove, { passive: false });

if (this._emitEvent('beforedrag', evt) === false) {
this._activePointerId = undefined;
off(document, ['mouseup', 'touchcancel', 'touchend'], this._onTapStop);
return;
}

if (this._activePointerId !== undefined) {
this._targetElement!.setPointerCapture(this._activePointerId);
}

on(document, ['mousemove', 'touchmove'], this._onTapMove, { passive: false });

// Make area element visible
Expand Down Expand Up @@ -610,6 +634,11 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
const { document, features } = this._options;
const { _singleClick } = this;

if (this._activePointerId !== undefined && this._targetElement?.hasPointerCapture(this._activePointerId)) {
this._targetElement.releasePointerCapture(this._activePointerId);
}
this._activePointerId = undefined;

// Remove event handlers
off(this._targetElement, 'scroll', this._onStartAreaScroll);
off(document, ['mousemove', 'touchmove'], this._delayedTapMove);
Expand Down
48 changes: 48 additions & 0 deletions packages/vanilla/tests/interaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type TestWindow = Window & {
events?: string[];
started?: boolean;
beforeDrag?: number;
stopped?: boolean;
};

test('emits the drag lifecycle and maintains the selected elements', async ({ selection }) => {
Expand All @@ -26,6 +27,53 @@ test('emits the drag lifecycle and maintains the selected elements', async ({ se
expect(await selection.isAreaMounted()).toBe(false);
});

test('stops a drag released over native video controls', async ({ page, selection }) => {
await selection.setup();
await selection.run((instance) => {
const browserWindow = window as TestWindow;
browserWindow.stopped = false;
instance.on('stop', () => {
browserWindow.stopped = true;
});
});

await page.evaluate(() => {
const video = document.createElement('video');
video.id = 'native-video-controls';
video.controls = true;
Object.assign(video.style, {
position: 'fixed',
left: '320px',
top: '32px',
width: '320px',
height: '180px',
background: 'black'
});
document.body.append(video);
});

const nativeVideo = page.locator('#native-video-controls');
await expect(nativeVideo).toBeVisible();
expect(await nativeVideo.evaluate((element) => (element as HTMLVideoElement).controls)).toBe(true);

const start = await selection.items.first().boundingBox();
const video = await nativeVideo.boundingBox();

if (!start || !video) throw new Error('Could not measure drag targets');

await page.mouse.move(start.x + 10, start.y + 10);
await page.mouse.down();
await page.mouse.move(start.x + 40, start.y + 40, { steps: 10 });

await expect.poll(() => selection.isAreaMounted()).toBe(true);

await page.mouse.move(video.x + video.width / 2, video.y + video.height - 8, { steps: 20 });
await page.mouse.up();

expect(await selection.run(() => (window as TestWindow).stopped)).toBe(true);
expect(await selection.isAreaMounted()).toBe(false);
});

test('allows beforestart and beforedrag handlers to cancel interaction', async ({ selection }) => {
await selection.setup();
await selection.run((instance) => instance.on('beforestart', () => false));
Expand Down