Skip to content

Commit aa62843

Browse files
committed
Merge meta#8670 08b5a2a (meta#8665: a claimed mouse or pen drag holds selection off document-wide) into the meta#8228 helper
2 parents 64b1601 + 2f9ccca commit aa62843

5 files changed

Lines changed: 184 additions & 7 deletions

File tree

‎.changeset/gestures-every-pointer.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
'@astryxdesign/core': patch
33
---
44

5-
[chore] Internal: gestures attach for every pointer, a mouse, a pen and a finger alike (AST-094 FR43). Under a fine primary pointer an element that uses a gesture keeps one passive `pointerdown` listener (a recognizer that claims a moving finger also keeps its blocking `touchmove`), and the page's listeners last only a press. A right click or a ctrl-click on macOS starts no press. A mouse or pen drag that a recognizer claims takes no text selection from then on, and a finger is read once where the browser sends Touch Events without exposing `ontouchstart`. A press tracker takes a mouse press as a press, painting at once with no scroll brake. The screen-edge pan stays off under a mouse, a trackpad and a pen unless its caller allows one, and `perEventPointer` is accepted and does nothing. Not exported from the package entry point.
5+
[chore] Internal: gestures attach for every pointer, a mouse, a pen and a finger alike (AST-094 FR43). Under a fine primary pointer an element that uses a gesture keeps one passive `pointerdown` listener (a recognizer that claims a moving finger also keeps its blocking `touchmove`), and the page's listeners last only a press. A right click or a ctrl-click on macOS starts no press. A mouse or pen drag that a recognizer claims takes no text selection from then on, anywhere on the page, and a finger is read once where the browser sends Touch Events without exposing `ontouchstart`. A press tracker takes a mouse press as a press, painting at once with no scroll brake. The screen-edge pan stays off under a mouse, a trackpad and a pen unless its caller allows one, and `perEventPointer` is accepted and does nothing. Not exported from the package entry point.
66

77
@vjeux

‎packages/core/src/gestures/README.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,10 @@ presses nothing.
248248
barrel button and a ctrl-click on macOS (its context click) start no press
249249
(`isPrimaryClick`, `../utils/primaryClick.ts`). A mouse or a pen drag
250250
that a recognizer claims takes no text selection from then on: the
251-
selection the drag started goes, and the claiming elements refuse one
252-
until the lift (FR41); a click keeps its selection. The host follows the
251+
selection the drag started goes, the claiming elements refuse one, and
252+
the document refuses one too until the press's mouseup and click have
253+
passed, since WebKit extends a drag's selection past them (FR41); a click
254+
keeps its selection. The host follows the
253255
query (a tablet docking or undocking a mouse): turning fine cancels every
254256
touch in flight. `perEventPointer`, an option of `attach`,
255257
`attachConsumer`, `useGestureRecognizer` and `usePressTracker` from

‎packages/core/src/gestures/__tests__/GestureHost.test.ts‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3709,6 +3709,50 @@ describe('gestures attach for every pointer (spec:AST-094 FR43, FR19, FR24)', ()
37093709
}
37103710
});
37113711

3712+
it('holds selection off document-wide while a mouse pan claims, as WebKit extends a drag selection past the pan: a selection begun elsewhere goes, until the press is over', async () => {
3713+
const {box, outside} = mount(
3714+
'<p id="outside">Text outside the pan</p><div id="box">Pan</div>',
3715+
);
3716+
const fine = fineHost();
3717+
const pan = new PanGestureRecognizer({direction: 'horizontal'});
3718+
detachers.push(fine.attach(box, pan));
3719+
const selection = window.getSelection() as Selection;
3720+
let time = 0;
3721+
for (const [type, x] of [
3722+
['pointerdown', 10],
3723+
['pointermove', 30],
3724+
['pointermove', 50],
3725+
] as const) {
3726+
time += 16;
3727+
pointer(type, box, {pointerType: 'mouse', x, y: 10, buttons: 1, time});
3728+
}
3729+
expect(['began', 'changed']).toContain(pan.state);
3730+
const elsewhere = new Event('selectstart', {
3731+
bubbles: true,
3732+
cancelable: true,
3733+
});
3734+
outside.dispatchEvent(elsewhere);
3735+
expect(elsewhere.defaultPrevented).toBe(true);
3736+
// WebKit extends the drag's selection over the page without a selectstart.
3737+
const range = document.createRange();
3738+
range.selectNodeContents(outside);
3739+
selection.addRange(range);
3740+
document.dispatchEvent(new Event('selectionchange'));
3741+
expect(selection.rangeCount).toBe(0);
3742+
pointer('pointerup', box, {
3743+
pointerType: 'mouse',
3744+
x: 50,
3745+
y: 10,
3746+
time: time + 16,
3747+
});
3748+
// The press's own mouseup and click come in the same task: the hold
3749+
// lasts past them.
3750+
await new Promise(resolve => setTimeout(resolve, 0));
3751+
const later = new Event('selectstart', {bubbles: true, cancelable: true});
3752+
outside.dispatchEvent(later);
3753+
expect(later.defaultPrevented).toBe(false);
3754+
});
3755+
37123756
it("leaves a mouse click's selection alone: a tap claims only at its lift", () => {
37133757
const {box} = mount('<div id="box">Some selected text</div>');
37143758
const fine = fineHost();
@@ -3781,6 +3825,63 @@ describe('gestures attach for every pointer (spec:AST-094 FR43, FR19, FR24)', ()
37813825
expect(consumer.events.length).toBeGreaterThan(0);
37823826
});
37833827

3828+
it("ends a fine-pointer finger's press on an action group at its lift, and the group's next press begins and ends as well (FR43, FR19)", () => {
3829+
vi.useFakeTimers();
3830+
try {
3831+
const {group, a} = mount(
3832+
'<div id="group"><button id="a">A</button><button id="b">B</button></div>',
3833+
);
3834+
const laptop = new GestureHost(document, {
3835+
isPrimaryPointerCoarse: () => false,
3836+
});
3837+
// The action-group adapter's recognizer: a long press with no minimum
3838+
// duration and no movement limit, attached as an action group.
3839+
const press = new LongPressGestureRecognizer({
3840+
minimumPressDuration: 0,
3841+
allowableMovement: Number.POSITIVE_INFINITY,
3842+
});
3843+
const states: string[] = [];
3844+
press.addTransitionListener((_recognizer, _from, to) => states.push(to));
3845+
detachers.push(laptop.attach(group, press, {actionGroup: true}));
3846+
// Chromium on a touchscreen laptop: pointerdown, then touchstart; at
3847+
// the lift, pointerup, then touchend.
3848+
const fingerPress = (
3849+
pointerId: number,
3850+
touchId: number,
3851+
time: number,
3852+
) => {
3853+
const finger = {id: touchId, x: 10, y: 10};
3854+
pointer('pointerdown', a, {
3855+
id: pointerId,
3856+
pointerType: 'touch',
3857+
x: 10,
3858+
y: 10,
3859+
buttons: 1,
3860+
time,
3861+
});
3862+
touch('touchstart', a, [finger], [finger], time);
3863+
vi.advanceTimersByTime(20);
3864+
pointer('pointerup', a, {
3865+
id: pointerId,
3866+
pointerType: 'touch',
3867+
x: 10,
3868+
y: 10,
3869+
time: time + 50,
3870+
});
3871+
touch('touchend', a, [finger], [], time + 50);
3872+
vi.advanceTimersByTime(20);
3873+
};
3874+
fingerPress(31, 0, 0);
3875+
const first = states.splice(0);
3876+
fingerPress(32, 1, 1000);
3877+
expect(first).toEqual(expect.arrayContaining(['began', 'ended']));
3878+
expect(states).toEqual(expect.arrayContaining(['began', 'ended']));
3879+
expect(press.state).toBe('possible');
3880+
} finally {
3881+
vi.useRealTimers();
3882+
}
3883+
});
3884+
37843885
it('takes perEventPointer as it takes any attach, and warns for nothing', () => {
37853886
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
37863887
detachers.push(() => warn.mockRestore());

‎packages/core/src/gestures/__tests__/browser/gestures.browser.spec.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,45 @@ test('a long press begins at touch-down + 500 ms on the event clock, even when t
12121212
test.describe('where the primary pointer is fine', () => {
12131213
test.use({hasTouch: false, isMobile: false});
12141214

1215+
test("a mouse drag a pan claimed selects no page text, though it carries past the pan's element over text (spec:AST-094 FR43, FR41)", async ({
1216+
page,
1217+
}) => {
1218+
await page.goto('about:blank');
1219+
await page.setContent(
1220+
'<!doctype html><meta name="viewport" content="width=device-width, initial-scale=1"><body></body>',
1221+
);
1222+
await page.addScriptTag({content: bareBundle});
1223+
await page.evaluate(() => {
1224+
const {host, PanGestureRecognizer} = window.__api;
1225+
document.body.style.font = '16px sans-serif';
1226+
document.body.innerHTML =
1227+
'<h1 id="heading">A story heading with words</h1><p>A description with several words in it.</p><button>One</button><button>Two</button><div id="card" style="margin-top: 24px; width: 320px; height: 120px; background: #ccd">Card text a drag would select</div><p>Text below the card, with words.</p>';
1228+
host.attach(
1229+
document.getElementById('card') as HTMLElement,
1230+
new PanGestureRecognizer(),
1231+
);
1232+
});
1233+
const card = await page.locator('#card').boundingBox();
1234+
if (card == null) {
1235+
throw new Error('no box for #card');
1236+
}
1237+
const x = card.x + 40;
1238+
const y = card.y + card.height / 2;
1239+
await page.mouse.move(x, y);
1240+
await page.mouse.down();
1241+
// Past the pan's start, then up over the text above the card.
1242+
await page.mouse.move(x + 30, y - 10, {steps: 4});
1243+
await page.mouse.move(x + 60, card.y - 80, {steps: 12});
1244+
const during = await page.evaluate(() =>
1245+
String(window.getSelection() ?? '').trim(),
1246+
);
1247+
await page.mouse.up();
1248+
const after = await page.evaluate(() =>
1249+
String(window.getSelection() ?? '').trim(),
1250+
);
1251+
expect([during, after]).toEqual(['', '']);
1252+
});
1253+
12151254
test("the host registers no page touch listener while idle and warns for nothing, though the page attaches recognizers: an element keeps only a claiming recognizer's blocking touchmove; a mouse drag pans (spec:AST-094 FR43, FR24, FR19)", async ({
12161255
page,
12171256
}) => {

‎packages/core/src/utils/selectionLock.ts‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,32 @@ export function lockSelection(
114114
}
115115

116116
/**
117-
* Refuse a selection on `elements` with inline styles, on every engine, until
118-
* the returned function runs: a mouse or a pen drag a recognizer claimed has
119-
* already started selecting when it claims, which only the styles stop, so
120-
* the selection it started is cleared first (`spec:AST-094` FR43, FR41).
117+
* Refuse a selection while a mouse or a pen drag a recognizer claimed runs,
118+
* until the returned function runs (`spec:AST-094` FR43, FR41). The drag has
119+
* already started selecting when it claims, so that selection goes first.
120+
* `elements` refuse one with inline styles, on every engine. The document
121+
* refuses one too, as WebKit extends a drag's selection over text outside
122+
* the elements without another `selectstart`: a `selectstart` anywhere is
123+
* refused and a selection the drag extends is cleared. The press's own
124+
* `mouseup` and `click` come after its `pointerup` in the same task, and
125+
* WebKit settles the drag's selection there, so the document's hold ends a
126+
* task after the release. Page listeners only during a press (FR24).
121127
* Idempotent release.
122128
*/
123129
export function lockDragSelection(
124130
elements: Iterable<Element>,
125131
view: Window | null | undefined,
126132
): () => void {
133+
const clear = () => {
134+
const selection = view?.getSelection();
135+
if (
136+
selection != null &&
137+
selection.rangeCount > 0 &&
138+
!selection.isCollapsed
139+
) {
140+
selection.removeAllRanges();
141+
}
142+
};
127143
view?.getSelection()?.removeAllRanges();
128144
const set = new Set(elements);
129145
const refuse = (event: Event) => {
@@ -134,6 +150,15 @@ export function lockDragSelection(
134150
for (const element of set) {
135151
element.addEventListener('selectstart', refuse);
136152
}
153+
const doc = view?.document ?? null;
154+
// The focused field keeps its own selection (a drag never starts in one).
155+
const clearOutsideEntry = () => {
156+
if (!isTextEntry(doc?.activeElement ?? null)) {
157+
clear();
158+
}
159+
};
160+
doc?.addEventListener('selectstart', refuse, true);
161+
doc?.addEventListener('selectionchange', clearOutsideEntry);
137162
const unlockStyles = lockStyles(set);
138163
let isReleased = false;
139164
return () => {
@@ -145,6 +170,16 @@ export function lockDragSelection(
145170
element.removeEventListener('selectstart', refuse);
146171
}
147172
unlockStyles();
173+
const release = () => {
174+
clearOutsideEntry();
175+
doc?.removeEventListener('selectstart', refuse, true);
176+
doc?.removeEventListener('selectionchange', clearOutsideEntry);
177+
};
178+
if (view == null) {
179+
release();
180+
} else {
181+
view.setTimeout(release, 0);
182+
}
148183
};
149184
}
150185

0 commit comments

Comments
 (0)