forked from cinnyapp/cinny
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Prevent IME-exiting Enter press from sending message on Safari
On most browsers, pressing Enter to end IME composition produces this sequence of events: * keydown (keycode 229, key Processing/Unidentified, isComposing true) * compositionend * keyup (keycode 13, key Enter, isComposing false) On Safari, the sequence is different: * compositionend * keydown (keycode 229, key Enter, isComposing false) * keyup (keycode 13, key Enter, isComposing false) This causes Safari users to mistakenly send their messages when they press Enter to confirm their choice in an IME. The workaround is to treat the next keydown with keycode 229 as if it were part of the IME composition period if it occurs within a short time of the compositionend event. Fixes cinnyapp#2103, but needs confirmation from a Safari user.
1 parent
b524778
commit 5b5cd9a
Showing
12 changed files
with
89 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters