Skip to content

Commit

Permalink
Fix autocomplete behavior for Android chrome.
Browse files Browse the repository at this point in the history
Based off facebookarchive#175, this commit
fixes some issues with autocomplete behavior on Android Chrome.
  • Loading branch information
AndrewSouthpaw committed Feb 16, 2017
1 parent 1893a1a commit c365cfa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
/website/src/draft-js/lib/
/website/src/draft-js/docs/
npm-debug.log
.idea/
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const RESOLVE_DELAY = 20;
let resolved = false;
let stillComposing = false;
let textInputData = '';
let formerTextInputData = '';

var DraftEditorCompositionHandler = {
onBeforeInput: function(editor: DraftEditor, e: SyntheticInputEvent): void {
Expand All @@ -52,7 +53,8 @@ var DraftEditorCompositionHandler = {
* A `compositionstart` event has fired while we're still in composition
* mode. Continue the current composition session to prevent a re-render.
*/
onCompositionStart: function(editor: DraftEditor): void {
onCompositionStart: function(editor: DraftEditor, e: SyntheticInputEvent): void {
formerTextInputData = e.data;
stillComposing = true;
},

Expand Down Expand Up @@ -136,6 +138,9 @@ var DraftEditorCompositionHandler = {
const composedChars = textInputData;
textInputData = '';

const formerComposedChars = formerTextInputData;
formerTextInputData = '';

const editorState = EditorState.set(editor._latestEditorState, {
inCompositionMode: false,
});
Expand All @@ -159,12 +164,28 @@ var DraftEditorCompositionHandler = {

editor.exitCurrentMode();

let contentState = editorState.getCurrentContent();
let selection = editorState.getSelection();
if (formerComposedChars && selection.isCollapsed()) {
let anchorOffset = selection.getAnchorOffset() - formerComposedChars.length;
if (anchorOffset < 0) {
anchorOffset = 0;
}
const toRemoveSel = selection.merge({anchorOffset});
contentState = DraftModifier.removeRange(
editorState.getCurrentContent(),
toRemoveSel,
'backward',
);
selection = contentState.getSelectionAfter();
}

if (composedChars) {
// If characters have been composed, re-rendering with the update
// is sufficient to reset the editor.
const contentState = DraftModifier.replaceText(
editorState.getCurrentContent(),
editorState.getSelection(),
contentState = DraftModifier.replaceText(
contentState,
selection,
composedChars,
currentStyle,
entityKey
Expand Down
5 changes: 3 additions & 2 deletions src/component/handlers/edit/editOnInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var nullthrows = require('nullthrows');
import type DraftEditor from 'DraftEditor.react';

var isGecko = UserAgent.isEngine('Gecko');
var isAndroid = UserAgent.isPlatform('Android');

var DOUBLE_NEWLINE = '\n\n';

Expand All @@ -47,7 +48,7 @@ function editOnInput(editor: DraftEditor): void {
var domSelection = global.getSelection();

var {anchorNode, isCollapsed} = domSelection;
if (anchorNode.nodeType !== Node.TEXT_NODE) {
if (anchorNode.nodeType !== Node.TEXT_NODE && anchorNode.nodeType !== Node.ELEMENT_NODE) {
return;
}

Expand Down Expand Up @@ -107,7 +108,7 @@ function editOnInput(editor: DraftEditor): void {

var anchorOffset, focusOffset, startOffset, endOffset;

if (isGecko) {
if (isAndroid || isGecko) {
// Firefox selection does not change while the context menu is open, so
// we preserve the anchor and focus values of the DOM selection.
anchorOffset = domSelection.anchorOffset;
Expand Down

0 comments on commit c365cfa

Please sign in to comment.