-
Notifications
You must be signed in to change notification settings - Fork 46.7k
fix(tui): guard Enter submit against IME composition via timestamp #39246
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
Open
wuyang9311
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
wuyang9311:fix/tui-ime-enter-submit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -36,6 +36,14 @@ const PRINTABLE = /^[ -~\u00a0-\uffff]+$/ | |
| const BRACKET_PASTE = new RegExp(`${ESC}?\\[20[01]~`, 'g') | ||
| const FRAME_BATCH_MS = 16 | ||
| const MULTI_CLICK_MS = 500 | ||
|
|
||
| /** | ||
| * If the last printable input arrived within this many ms before Enter, | ||
| * the Enter is treated as IME composition confirmation (not submit). | ||
| * 30ms = well below human key-repeat (50ms+) but above IME commit + | ||
| * Enter arriving in the same event-loop tick (~0-5ms). | ||
| */ | ||
| const IME_GUARD_MS = 30 | ||
| type MinimalEnv = Record<string, string | undefined> | ||
|
|
||
| const invert = (s: string) => INV + s + INV_OFF | ||
|
|
@@ -465,6 +473,7 @@ export function TextInput({ | |
| const lineWidthRef = useRef(stringWidth(value.includes('\n') ? value.slice(value.lastIndexOf('\n') + 1) : value)) | ||
| const mouseAnchorRef = useRef<null | number>(null) | ||
| const lastClickRef = useRef<{ at: number; offset: number }>({ at: 0, offset: -1 }) | ||
| const lastInputAt = useRef(0) | ||
| const undo = useRef<{ cursor: number; value: string }[]>([]) | ||
| const redo = useRef<{ cursor: number; value: string }[]>([]) | ||
|
|
||
|
|
@@ -968,6 +977,12 @@ export function TextInput({ | |
| if (k.return) { | ||
| flushKeyBurst() | ||
|
|
||
| const imeGuard = Date.now() - lastInputAt.current < IME_GUARD_MS | ||
| if (imeGuard) { | ||
| lastInputAt.current = 0 | ||
| return | ||
| } | ||
|
|
||
| const sequence = (event.keypress as { sequence?: string }).sequence | ||
| const preserveBareLineFeed = shouldPreserveCtrlJNewline() && sequence === '\n' | ||
|
|
||
|
|
@@ -1112,6 +1127,10 @@ export function TextInput({ | |
| const bracketed = event.keypress.isPasted || inp.includes('[200~') | ||
| const text = inp.replace(BRACKET_PASTE, '').replace(/\r\n/g, '\n').replace(/\r/g, '\n') | ||
|
|
||
| if (!event.keypress.isPasted) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This timestamp is never reached when xterm delivers finalized IME text together with Return in one input event: the |
||
| lastInputAt.current = Date.now() | ||
| } | ||
|
|
||
| if (bracketed && emitPaste({ bracketed: true, cursor: c, text, value: v })) { | ||
| return | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This guard runs before the existing Shift/Ctrl/Meta Return branch. A printable input followed within 30ms by a modified Return will now be absorbed instead of inserting the configured newline; restrict this guard to unmodified plain Return.