Skip to content

Commit

Permalink
Add updateCaretOnSelectionChange. Fixes #2346
Browse files Browse the repository at this point in the history
  • Loading branch information
hodgef committed Sep 12, 2024
1 parent 1fd1f63 commit 536f3f7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/lib/components/Keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class SimpleKeyboard {
* @property {boolean} layoutCandidatesCaseSensitiveMatch Determines whether layout candidate match should be case sensitive.
* @property {boolean} disableCandidateNormalization Disables the automatic normalization for selected layout candidates
* @property {boolean} enableLayoutCandidatesKeyPress Enables onKeyPress triggering for layoutCandidate items
* @property {boolean} updateCaretOnSelectionChange Updates caret when selectionchange event is fired
*/
this.options = {
layoutName: "default",
Expand Down Expand Up @@ -1148,7 +1149,11 @@ class SimpleKeyboard {
document.addEventListener("keydown", this.handleKeyDown, physicalKeyboardHighlightPreventDefault);
document.addEventListener("mouseup", this.handleMouseUp);
document.addEventListener("touchend", this.handleTouchEnd);
document.addEventListener("selectionchange", this.handleSelectionChange);

if (this.options.updateCaretOnSelectionChange) {
document.addEventListener("selectionchange", this.handleSelectionChange);
}

document.addEventListener("select", this.handleSelect);
}
}
Expand Down Expand Up @@ -1326,7 +1331,13 @@ class SimpleKeyboard {
document.removeEventListener("mouseup", this.handleMouseUp);
document.removeEventListener("touchend", this.handleTouchEnd);
document.removeEventListener("select", this.handleSelect);
document.removeEventListener("selectionchange", this.handleSelectionChange);

// selectionchange is causing caret update issues on Chrome
// https://github.com/hodgef/simple-keyboard/issues/2346
if (this.options.updateCaretOnSelectionChange) {
document.removeEventListener("selectionchange", this.handleSelectionChange);
}

document.onpointerup = null;
document.ontouchend = null;
document.ontouchcancel = null;
Expand Down
5 changes: 5 additions & 0 deletions src/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ export interface KeyboardOptions {
*/
enableLayoutCandidatesKeyPress?: boolean;

/**
* Updates caret when selectionchange event is fired
*/
updateCaretOnSelectionChange?: boolean;

/**
* Executes the callback function every time simple-keyboard is rendered (e.g: when you change layouts).
*/
Expand Down

0 comments on commit 536f3f7

Please sign in to comment.