-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix: prevent IME enter from sending chat #7845
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
Merged
+96
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -112,6 +112,10 @@ | |
| ref="inputField" | ||
| v-model="localPrompt" | ||
| @keydown="handleKeyDown" | ||
| @compositionstart="handleCompositionStart" | ||
| @compositionend="handleCompositionEnd" | ||
| @compositioncancel="handleCompositionEnd" | ||
| @blur="clearCompositionState()" | ||
| :disabled="disabled" | ||
| placeholder="Ask AstrBot..." | ||
| class="chat-textarea" | ||
|
|
@@ -307,6 +311,7 @@ import { | |
| import { useDisplay } from "vuetify"; | ||
| import { useModuleI18n } from "@/i18n/composables"; | ||
| import { useCustomizerStore } from "@/stores/customizer"; | ||
| import { isComposingEnter } from "@/utils/imeInput.mjs"; | ||
|
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. |
||
| import ConfigSelector from "./ConfigSelector.vue"; | ||
| import ProviderModelMenu from "./ProviderModelMenu.vue"; | ||
| import StyledMenu from "@/components/shared/StyledMenu.vue"; | ||
|
|
@@ -379,6 +384,8 @@ const providerModelMenuRef = ref<InstanceType<typeof ProviderModelMenu> | null>( | |
| const showProviderSelector = ref(true); | ||
| const isReplyClosing = ref(false); | ||
| const isDragging = ref(false); | ||
| const isComposing = ref(false); | ||
| const lastCompositionEndAt = ref<number | null>(null); | ||
| let dragLeaveTimeout: number | null = null; | ||
|
|
||
| const localPrompt = computed({ | ||
|
|
@@ -514,6 +521,10 @@ function handleKeyDown(e: KeyboardEvent) { | |
| return; | ||
| } | ||
|
|
||
| if (isComposingEnter(e, isComposing.value, lastCompositionEndAt.value)) { | ||
| return; | ||
| } | ||
|
|
||
| const isSendHotkey = | ||
| e.ctrlKey || | ||
| e.metaKey || | ||
|
|
@@ -533,6 +544,23 @@ function handleKeyDown(e: KeyboardEvent) { | |
| } | ||
| } | ||
|
|
||
| function handleCompositionStart() { | ||
| isComposing.value = true; | ||
| lastCompositionEndAt.value = null; | ||
| } | ||
|
|
||
| function handleCompositionEnd(e: CompositionEvent) { | ||
| lastCompositionEndAt.value = e.timeStamp; | ||
| clearCompositionState({ keepLastEndAt: true }); | ||
| } | ||
|
|
||
| function clearCompositionState({ keepLastEndAt = false } = {}) { | ||
| isComposing.value = false; | ||
| if (!keepLastEndAt) { | ||
| lastCompositionEndAt.value = null; | ||
| } | ||
| } | ||
|
|
||
| function handleKeyUp(e: KeyboardEvent) { | ||
| if (e.keyCode === 66) { | ||
| ctrlKeyDown.value = false; | ||
|
|
@@ -634,6 +662,7 @@ onBeforeUnmount(() => { | |
| if (inputField.value) { | ||
| inputField.value.removeEventListener("paste", handlePaste); | ||
| } | ||
| clearCompositionState(); | ||
| document.removeEventListener("keyup", handleKeyUp); | ||
| }); | ||
|
|
||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Some IMEs emit Enter right after compositionend; treat that same-keystroke | ||
| // window as composition so selecting a candidate does not send the message. | ||
| const RECENT_COMPOSITION_END_THRESHOLD_MS = 100; | ||
|
|
||
| /** | ||
| * @param {KeyboardEvent} event | ||
| * @param {boolean} compositionActive | ||
| * @param {number | null} lastCompositionEndAt | ||
| */ | ||
| export function isComposingEnter( | ||
| event, | ||
| compositionActive, | ||
| lastCompositionEndAt = null, | ||
| ) { | ||
| const hasLegacyCompositionKeyCode = | ||
| typeof event.keyCode === "number" && event.keyCode === 229; | ||
| const isAfterRecentCompositionEnd = | ||
| typeof event.timeStamp === "number" && | ||
| typeof lastCompositionEndAt === "number" && | ||
| event.timeStamp >= lastCompositionEndAt && | ||
| event.timeStamp - lastCompositionEndAt < | ||
| RECENT_COMPOSITION_END_THRESHOLD_MS; | ||
|
|
||
| return ( | ||
| event.key === "Enter" && | ||
| (compositionActive || | ||
| event.isComposing || | ||
| hasLegacyCompositionKeyCode || | ||
| isAfterRecentCompositionEnd) | ||
| ); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
|
|
||
| import { isComposingEnter } from "../src/utils/imeInput.mjs"; | ||
|
|
||
| test("detects Enter while an IME composition is active", () => { | ||
| assert.equal(isComposingEnter({ key: "Enter", isComposing: true }, false), true); | ||
| assert.equal(isComposingEnter({ key: "Enter", isComposing: false }, true), true); | ||
| }); | ||
|
|
||
| test("does not treat normal Enter as IME composition", () => { | ||
| assert.equal(isComposingEnter({ key: "Enter", isComposing: false }, false), false); | ||
| assert.equal(isComposingEnter({ key: "a", isComposing: true }, true), false); | ||
| }); | ||
|
|
||
| test("detects Enter fired immediately after composition ended", () => { | ||
| assert.equal( | ||
| isComposingEnter( | ||
| { key: "Enter", isComposing: false, timeStamp: 105 }, | ||
| false, | ||
| 100, | ||
| ), | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| test("does not treat delayed Enter after composition ended as IME composition", () => { | ||
| assert.equal( | ||
| isComposingEnter( | ||
| { key: "Enter", isComposing: false, timeStamp: 250 }, | ||
| false, | ||
| 100, | ||
| ), | ||
| false, | ||
| ); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.