mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-21 09:31:17 -08:00
fix(composer): send from pointerup on touch so iOS cannot drop the tap
This commit is contained in:
2 files changed
+71
-2
No files matched your search
@@ -781,6 +781,51 @@ describe('RoomInput submit regressions', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('sends on touch pointerup when the tap never produces a click', async () => {
|
||||
render(<RoomInputHarness />);
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Compose text' }));
|
||||
|
||||
fireEvent.pointerDown(sendButton(), { pointerType: 'touch' });
|
||||
fireEvent.pointerUp(sendButton(), { pointerType: 'touch' });
|
||||
|
||||
await waitFor(() => expect(testState.matrix.sendMessage).toHaveBeenCalledOnce());
|
||||
});
|
||||
|
||||
it('sends once when a touch tap produces both a pointerup and a click', async () => {
|
||||
render(<RoomInputHarness />);
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Compose text' }));
|
||||
|
||||
const submit = sendButton();
|
||||
fireEvent.pointerDown(submit, { pointerType: 'touch' });
|
||||
fireEvent.pointerUp(submit, { pointerType: 'touch' });
|
||||
fireEvent.click(submit);
|
||||
|
||||
await waitFor(() => expect(testState.matrix.sendMessage).toHaveBeenCalledOnce());
|
||||
});
|
||||
|
||||
it('cancels a touch send released outside the button', () => {
|
||||
render(<RoomInputHarness />);
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Compose text' }));
|
||||
|
||||
fireEvent.pointerDown(sendButton(), { pointerType: 'touch' });
|
||||
fireEvent.pointerUp(sendButton(), { pointerType: 'touch', clientX: 200, clientY: 200 });
|
||||
|
||||
expect(testState.matrix.sendMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('leaves mouse taps on the click path', async () => {
|
||||
render(<RoomInputHarness />);
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Compose text' }));
|
||||
|
||||
const submit = sendButton();
|
||||
fireEvent.pointerDown(submit, { pointerType: 'mouse' });
|
||||
fireEvent.pointerUp(submit, { pointerType: 'mouse' });
|
||||
expect(testState.matrix.sendMessage).not.toHaveBeenCalled();
|
||||
|
||||
fireEvent.click(submit);
|
||||
await waitFor(() => expect(testState.matrix.sendMessage).toHaveBeenCalledOnce());
|
||||
});
|
||||
|
||||
it('waits for an attachment transaction instead of sending text independently', async () => {
|
||||
const upload = deferred<{ content_uri: string }>();
|
||||
const file = new File(['attachment'], 'attachment.txt', { type: 'text/plain' });
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
import type { KeyboardEventHandler, MouseEvent, ReactElement, RefObject } from 'react';
|
||||
import type {
|
||||
KeyboardEventHandler,
|
||||
MouseEvent,
|
||||
PointerEvent,
|
||||
ReactElement,
|
||||
RefObject,
|
||||
} from 'react';
|
||||
import {
|
||||
forwardRef,
|
||||
Fragment,
|
||||
@@ -408,6 +414,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
const uploadBoardHandlers = useRef<UploadBoardImperativeHandlers>();
|
||||
const longPressTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const isLongPress = useRef(false);
|
||||
const sentOnPointerUpRef = useRef(false);
|
||||
const suppressBlurRefocusRef = useRef(false);
|
||||
const editorRafIdsRef = useRef(new Set<number>());
|
||||
const scheduleEditorRaf = useCallback((callback: () => void) => {
|
||||
@@ -2518,6 +2525,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
isLongPress.current = false;
|
||||
return;
|
||||
}
|
||||
if (sentOnPointerUpRef.current) return;
|
||||
submit();
|
||||
return;
|
||||
}
|
||||
@@ -2530,6 +2538,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
if (hasContent) e.preventDefault();
|
||||
}}
|
||||
onPointerDown={() => {
|
||||
sentOnPointerUpRef.current = false;
|
||||
if (showAudioRecorder) return;
|
||||
if (hasContent) {
|
||||
isLongPress.current = false;
|
||||
@@ -2577,11 +2586,26 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
window.addEventListener('pointerup', onUp);
|
||||
window.addEventListener('pointercancel', discardRecording);
|
||||
}}
|
||||
onPointerUp={() => {
|
||||
onPointerUp={(evt: PointerEvent<HTMLButtonElement>) => {
|
||||
if (longPressTimer.current !== null) {
|
||||
clearTimeout(longPressTimer.current);
|
||||
longPressTimer.current = null;
|
||||
}
|
||||
// iOS drops the synthesized click when the page mutates during a tap.
|
||||
if (evt.pointerType === 'mouse') return;
|
||||
if (showAudioRecorder || !hasContent || isLongPress.current) return;
|
||||
// Touch implicitly captures the pointer, so a release off the button lands here too.
|
||||
const rect = evt.currentTarget.getBoundingClientRect();
|
||||
if (
|
||||
evt.clientX < rect.left ||
|
||||
evt.clientX > rect.right ||
|
||||
evt.clientY < rect.top ||
|
||||
evt.clientY > rect.bottom
|
||||
) {
|
||||
return;
|
||||
}
|
||||
sentOnPointerUpRef.current = true;
|
||||
submit();
|
||||
}}
|
||||
onPointerCancel={() => {
|
||||
if (longPressTimer.current !== null) {
|
||||
|
||||
Reference in new issue
Block a user