Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent IME-exiting Enter press from sending message on Safari #2175

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions src/app/components/editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { RenderElement, RenderLeaf } from './Elements';
import { CustomElement } from './slate';
import * as css from './Editor.css';
import { toggleKeyboardShortcut } from './keyboard';
import { isComposing } from '../../utils/keyboard';

const initialValue: CustomElement[] = [
{
Expand Down Expand Up @@ -99,6 +100,9 @@ export const CustomEditor = forwardRef<HTMLDivElement, CustomEditorProps>(

const handleKeydown: KeyboardEventHandler = useCallback(
(evt) => {
if (isComposing(evt.nativeEvent)) {
return
}
onKeyDown?.(evt);
const shortcutToggled = toggleKeyboardShortcut(editor, evt);
if (shortcutToggled) evt.preventDefault();
Expand Down
3 changes: 2 additions & 1 deletion src/app/features/room/Room.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useKeyDown } from '../../hooks/useKeyDown';
import { markAsRead } from '../../../client/action/notifications';
import { useMatrixClient } from '../../hooks/useMatrixClient';
import { useRoomMembers } from '../../hooks/useRoomMembers';
import { isComposing } from '../../utils/keyboard';

export function Room() {
const { eventId } = useParams();
Expand All @@ -28,7 +29,7 @@ export function Room() {
window,
useCallback(
(evt) => {
if (isKeyHotkey('escape', evt)) {
if (!isComposing(evt) && isKeyHotkey('escape', evt)) {
markAsRead(mx, room.roomId);
}
},
Expand Down
7 changes: 7 additions & 0 deletions src/app/features/room/RoomInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ import { useElementSizeObserver } from '../../hooks/useElementSizeObserver';
import { ReplyLayout, ThreadIndicator } from '../../components/message';
import { roomToParentsAtom } from '../../state/room/roomToParents';
import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
import { isComposing } from '../../utils/keyboard';

interface RoomInputProps {
editor: Editor;
Expand Down Expand Up @@ -333,6 +334,9 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(

const handleKeyDown: KeyboardEventHandler = useCallback(
(evt) => {
if (isComposing(evt.nativeEvent)) {
return;
}
if (isKeyHotkey('mod+enter', evt) || (!enterForNewline && isKeyHotkey('enter', evt))) {
evt.preventDefault();
submit();
Expand All @@ -347,6 +351,9 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(

const handleKeyUp: KeyboardEventHandler = useCallback(
(evt) => {
if (isComposing(evt.nativeEvent)) {
return
}
if (isKeyHotkey('escape', evt)) {
evt.preventDefault();
return;
Expand Down
2 changes: 2 additions & 0 deletions src/app/features/room/RoomTimeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ import { useMentionClickHandler } from '../../hooks/useMentionClickHandler';
import { useSpoilerClickHandler } from '../../hooks/useSpoilerClickHandler';
import { useRoomNavigate } from '../../hooks/useRoomNavigate';
import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
import { isComposing } from '../../utils/keyboard';

const TimelineFloat = as<'div', css.TimelineFloatVariants>(
({ position, className, ...props }, ref) => (
Expand Down Expand Up @@ -702,6 +703,7 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli
useCallback(
(evt) => {
if (
!isComposing(evt) &&
isKeyHotkey('arrowup', evt) &&
editableActiveElement() &&
document.activeElement?.getAttribute('data-editable-name') === 'RoomInput' &&
Expand Down
4 changes: 3 additions & 1 deletion src/app/features/room/RoomView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { RoomViewHeader } from './RoomViewHeader';
import { useKeyDown } from '../../hooks/useKeyDown';
import { editableActiveElement } from '../../utils/dom';
import navigation from '../../../client/state/navigation';
import { isComposing } from '../../utils/keyboard';

const shouldFocusMessageField = (evt: KeyboardEvent): boolean => {
const { code } = evt;
Expand Down Expand Up @@ -76,7 +77,8 @@ export function RoomView({ room, eventId }: { room: Room; eventId?: string }) {
if (editableActiveElement()) return;
if (
document.body.lastElementChild?.className !== 'ReactModalPortal' ||
navigation.isRawModalVisible
navigation.isRawModalVisible ||
isComposing(evt)
) {
return;
}
Expand Down
7 changes: 7 additions & 0 deletions src/app/features/room/message/MessageEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
import { useMatrixClient } from '../../../hooks/useMatrixClient';
import { getEditedEvent, trimReplyFromFormattedBody } from '../../../utils/room';
import { mobileOrTablet } from '../../../utils/user-agent';
import { isComposing } from '../../../utils/keyboard';

type MessageEditorProps = {
roomId: string;
Expand Down Expand Up @@ -149,6 +150,9 @@ export const MessageEditor = as<'div', MessageEditorProps>(

const handleKeyDown: KeyboardEventHandler = useCallback(
(evt) => {
if (isComposing(evt.nativeEvent)) {
return;
}
if (isKeyHotkey('mod+enter', evt) || (!enterForNewline && isKeyHotkey('enter', evt))) {
evt.preventDefault();
handleSave();
Expand All @@ -163,6 +167,9 @@ export const MessageEditor = as<'div', MessageEditorProps>(

const handleKeyUp: KeyboardEventHandler = useCallback(
(evt) => {
if (isComposing(evt.nativeEvent)) {
return;
}
if (isKeyHotkey('escape', evt)) {
evt.preventDefault();
return;
Expand Down
33 changes: 33 additions & 0 deletions src/app/hooks/useSafariCompositionTaggingForKeyDown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useEffect } from 'react';

const actuallyComposingTag = Symbol("event is actually composing")

export function isTaggedAsComposing(x: object): boolean {
return actuallyComposingTag in x
}

export function useSafariCompositionTaggingForKeyDown(target: Window, {compositionEndThreshold = 500}: {compositionEndThreshold?: 500} = {}) {
useEffect(() => {
let compositionJustEndedAt: number | null = null

function recordCompositionEnd(evt: CompositionEvent) {
compositionJustEndedAt = evt.timeStamp
}

function interceptAndTagKeyDown(evt: KeyboardEvent) {
if (compositionJustEndedAt !== null
&& evt.keyCode === 229
&& (evt.timeStamp - compositionJustEndedAt) < compositionEndThreshold) {
Object.assign(evt, { [actuallyComposingTag]: true })
}
compositionJustEndedAt = null
}

target.addEventListener('compositionend', recordCompositionEnd, { capture: true })
target.addEventListener('keydown', interceptAndTagKeyDown, { capture: true })
return () => {
target.removeEventListener('compositionend', recordCompositionEnd, { capture: true })
target.removeEventListener('keydown', interceptAndTagKeyDown, { capture: true })
}
}, [target, compositionEndThreshold]);
}
4 changes: 4 additions & 0 deletions src/app/organisms/search/Search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { useKeyDown } from '../../hooks/useKeyDown';
import { openSearch } from '../../../client/action/navigation';
import { useMatrixClient } from '../../hooks/useMatrixClient';
import { factoryRoomIdByActivity } from '../../utils/sort';
import { isComposing } from '../../utils/keyboard.js';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { isComposing } from '../../utils/keyboard.js';
import { isComposing } from '../../utils/keyboard';


function useVisiblityToggle(setResult) {
const [isOpen, setIsOpen] = useState(false);
Expand Down Expand Up @@ -54,6 +55,9 @@ function useVisiblityToggle(setResult) {
useKeyDown(
window,
useCallback((event) => {
if (isComposing(event)) {
return;
}
// Ctrl/Cmd +
if (event.ctrlKey || event.metaKey) {
// open search modal
Expand Down
4 changes: 4 additions & 0 deletions src/app/organisms/settings/Settings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { settingsAtom } from '../../state/settings';
import { isMacOS } from '../../utils/user-agent';
import { KeySymbol } from '../../utils/key-symbol';
import { useMatrixClient } from '../../hooks/useMatrixClient';
import { isComposing } from '../../utils/keyboard.js';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { isComposing } from '../../utils/keyboard.js';
import { isComposing } from '../../utils/keyboard;


function AppearanceSection() {
const [, updateState] = useState({});
Expand Down Expand Up @@ -78,6 +79,9 @@ function AppearanceSection() {
};

const handleZoomEnter = (evt) => {
if (isComposing(evt.nativeEvent)) {
return;
}
if (isKeyHotkey('escape', evt)) {
evt.stopPropagation();
setCurrentZoom(pageZoom);
Expand Down
2 changes: 2 additions & 0 deletions src/app/pages/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import { ConfigConfigError, ConfigConfigLoading } from './ConfigConfig';
import { FeatureCheck } from './FeatureCheck';
import { createRouter } from './Router';
import { ScreenSizeProvider, useScreenSize } from '../hooks/useScreenSize';
import { useSafariCompositionTaggingForKeyDown } from '../hooks/useSafariCompositionTaggingForKeyDown';

const queryClient = new QueryClient();

function App() {
const screenSize = useScreenSize();
useSafariCompositionTaggingForKeyDown(window);

return (
<ScreenSizeProvider value={screenSize}>
Expand Down
5 changes: 4 additions & 1 deletion src/app/pages/auth/ServerPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import FocusTrap from 'focus-trap-react';

import { useDebounce } from '../../hooks/useDebounce';
import { stopPropagation } from '../../utils/keyboard';
import { isComposing, stopPropagation } from '../../utils/keyboard';

export function ServerPicker({
server,
Expand Down Expand Up @@ -53,6 +53,9 @@ export function ServerPicker({
};

const handleKeyDown: KeyboardEventHandler<HTMLInputElement> = (evt) => {
if (isComposing(evt.nativeEvent)) {
return;
}
if (evt.key === 'ArrowDown') {
evt.preventDefault();
setServerMenuAnchor(undefined);
Expand Down
20 changes: 17 additions & 3 deletions src/app/utils/keyboard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isKeyHotkey } from 'is-hotkey';
import { KeyboardEventHandler } from 'react';
import { isTaggedAsComposing } from '../hooks/useSafariCompositionTaggingForKeyDown';

export interface KeyboardEventLike {
key: string;
Expand All @@ -11,23 +12,36 @@ export interface KeyboardEventLike {
preventDefault(): void;
}

export function isComposing(evt: object): boolean {
if ('nativeEvent' in evt && typeof evt.nativeEvent === 'object' && evt.nativeEvent !== null) {
return isComposing(evt.nativeEvent)
}
if (isTaggedAsComposing(evt)) {
return true
}
if ('isComposing' in evt && typeof evt.isComposing === 'boolean') {
return evt.isComposing
}
return false
}

export const onTabPress = (evt: KeyboardEventLike, callback: () => void) => {
if (isKeyHotkey('tab', evt)) {
if (!isComposing(evt) && isKeyHotkey('tab', evt)) {
evt.preventDefault();
callback();
}
};

export const preventScrollWithArrowKey: KeyboardEventHandler = (evt) => {
if (isKeyHotkey(['arrowup', 'arrowright', 'arrowdown', 'arrowleft'], evt)) {
if (!isComposing(evt.nativeEvent) && isKeyHotkey(['arrowup', 'arrowright', 'arrowdown', 'arrowleft'], evt)) {
evt.preventDefault();
}
};

export const onEnterOrSpace =
<T>(callback: (evt: T) => void) =>
(evt: KeyboardEventLike) => {
if (isKeyHotkey('enter', evt) || isKeyHotkey('space', evt)) {
if (!isComposing(evt) && (isKeyHotkey('enter', evt) || isKeyHotkey('space', evt))) {
evt.preventDefault();
callback(evt as T);
}
Expand Down
Loading