Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Fix autocomplete behavior for Android chrome. #1013

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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
3 changes: 2 additions & 1 deletion src/component/handlers/edit/editOnInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 Down Expand Up @@ -120,7 +121,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