-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better Samsung Andorid keyboard detection
- Loading branch information
Showing
3 changed files
with
47 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as config from "trix/config" | ||
|
||
// The Samsung keyboard on Android can enter a buggy state in which it emmits a flurry of confused events that, | ||
// if processed, corrupts the editor. This class detects when that flurry of events starts and ends so we can | ||
// ignore those events in the input controller. | ||
export default class SamsungAndroidKeyboardDetector { | ||
constructor(element) { | ||
this.element = element | ||
} | ||
|
||
process(event) { | ||
this.previousEvent = this.event | ||
this.event = event | ||
|
||
this.checkBuggyModeStart() | ||
this.checkBuggyModeEnd() | ||
} | ||
|
||
// The buggy mode always starts with an insertText event with the same text as the editor element, except for an extra new line | ||
// after the cursor | ||
checkBuggyModeStart() { | ||
if (config.browser.samsungAndroid && this.insertTextAfterUnidentifiedChar() && differsInOneCR(this.element.innerText, this.event.data)) { | ||
this.buggyMode = true | ||
this.event.preventDefault() | ||
} | ||
} | ||
|
||
// The flurry of buggy events are always insertText. If we see any other type, it means it is over. | ||
checkBuggyModeEnd() { | ||
if (this.buggyMode && this.event.inputType !== "insertText") { | ||
this.buggyMode = false | ||
} | ||
} | ||
|
||
insertTextAfterUnidentifiedChar() { | ||
return this.event.type === "beforeinput" && this.event.inputType === "insertText" && this.event.data && this.previousEvent.key === "Unidentified" | ||
} | ||
} | ||
|
||
const differsInOneCR = (text1, text2) => Math.abs(text1.length - text2.length) === 1 && normalize(text1) === normalize(text2) | ||
const normalize = (text) => text.replace(/\s+/g, " ") |