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

pr: handle the input event for composition input methods like Chinese #46

Merged
merged 2 commits into from
Jul 3, 2023
Merged
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
29 changes: 24 additions & 5 deletions interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,33 @@ promptEl.addEventListener('keydown', function (event) {
/* Input Event Listener */
/* ========================================================================== */

promptEl.addEventListener('input', function (event) {
const sanitizedInput = promptEl.value
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n');
let isComposing = false;

// for Chinese https://github.com/smol-ai/menubar/pull/46
promptEl.addEventListener('compositionstart', function() {
isComposing = true;
});

promptEl.addEventListener('compositionend', function() {
isComposing = false;
// The composition has ended, now we can safely handle the input:
const sanitizedInput = promptEl.value
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n');
enabledProviders.forEach(provider => provider.handleInput(sanitizedInput));
});

enabledProviders.forEach(provider => provider.handleInput(sanitizedInput));
promptEl.addEventListener('input', function() {
// Skip handling the input event if we're in the middle of a composition:
if (!isComposing) {
const sanitizedInput = promptEl.value
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n');
enabledProviders.forEach(provider => provider.handleInput(sanitizedInput));
}
});


/* ========================================================================== */
/* Submit Event Listener */
/* ========================================================================== */
Expand Down