d2-ui
Version:
163 lines (142 loc) • 5.63 kB
JavaScript
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule DraftEditorCompositionHandler
*
*/
;
var DraftModifier = require('./DraftModifier');
var EditorState = require('./EditorState');
var Keys = require('fbjs/lib/Keys');
var getEntityKeyForSelection = require('./getEntityKeyForSelection');
var isSelectionAtLeafStart = require('./isSelectionAtLeafStart');
/**
* Millisecond delay to allow `compositionstart` to fire again upon
* `compositionend`.
*
* This is used for Korean input to ensure that typing can continue without
* the editor trying to render too quickly. More specifically, Safari 7.1+
* triggers `compositionstart` a little slower than Chrome/FF, which
* leads to composed characters being resolved and re-render occurring
* sooner than we want.
*/
var RESOLVE_DELAY = 20;
/**
* A handful of variables used to track the current composition and its
* resolution status. These exist at the module level because it is not
* possible to have compositions occurring in multiple editors simultaneously,
* and it simplifies state management with respect to the DraftEditor component.
*/
var resolved = false;
var stillComposing = false;
var textInputData = '';
var DraftEditorCompositionHandler = {
onBeforeInput: function onBeforeInput(e) {
textInputData = (textInputData || '') + e.data;
},
/**
* A `compositionstart` event has fired while we're still in composition
* mode. Continue the current composition session to prevent a re-render.
*/
onCompositionStart: function onCompositionStart() {
stillComposing = true;
},
/**
* Attempt to end the current composition session.
*
* Defer handling because browser will still insert the chars into active
* element after `compositionend`. If a `compositionstart` event fires
* before `resolveComposition` executes, our composition session will
* continue.
*
* The `resolved` flag is useful because certain IME interfaces fire the
* `compositionend` event multiple times, thus queueing up multiple attempts
* at handling the composition. Since handling the same composition event
* twice could break the DOM, we only use the first event. Example: Arabic
* Google Input Tools on Windows 8.1 fires `compositionend` three times.
*/
onCompositionEnd: function onCompositionEnd() {
var _this = this;
resolved = false;
stillComposing = false;
setTimeout(function () {
if (!resolved) {
DraftEditorCompositionHandler.resolveComposition.call(_this);
}
}, RESOLVE_DELAY);
},
/**
* In Safari, keydown events may fire when committing compositions. If
* the arrow keys are used to commit, prevent default so that the cursor
* doesn't move, otherwise it will jump back noticeably on re-render.
*/
onKeyDown: function onKeyDown(e) {
if (e.which === Keys.RIGHT || e.which === Keys.LEFT) {
e.preventDefault();
}
},
/**
* Keypress events may fire when committing compositions. In Firefox,
* pressing RETURN commits the composition and inserts extra newline
* characters that we do not want. `preventDefault` allows the composition
* to be committed while preventing the extra characters.
*/
onKeyPress: function onKeyPress(e) {
if (e.which === Keys.RETURN) {
e.preventDefault();
}
},
/**
* Attempt to insert composed characters into the document.
*
* If we are still in a composition session, do nothing. Otherwise, insert
* the characters into the document and terminate the composition session.
*
* If no characters were composed -- for instance, the user
* deleted all composed characters and committed nothing new --
* force a re-render. We also re-render when the composition occurs
* at the beginning of a leaf, to ensure that if the browser has
* created a new text node for the composition, we will discard it.
*
* Resetting innerHTML will move focus to the beginning of the editor,
* so we update to force it back to the correct place.
*/
resolveComposition: function resolveComposition() {
if (stillComposing) {
return;
}
resolved = true;
var composedChars = textInputData;
textInputData = '';
var editorState = EditorState.set(this.props.editorState, {
inCompositionMode: false
});
var currentStyle = editorState.getCurrentInlineStyle();
var entityKey = getEntityKeyForSelection(editorState.getCurrentContent(), editorState.getSelection());
var mustReset = !composedChars || isSelectionAtLeafStart(editorState) || currentStyle.size > 0 || entityKey !== null;
if (mustReset) {
this.restoreEditorDOM();
}
this.exitCurrentMode();
this.removeRenderGuard();
if (composedChars) {
// If characters have been composed, re-rendering with the update
// is sufficient to reset the editor.
var contentState = DraftModifier.replaceText(editorState.getCurrentContent(), editorState.getSelection(), composedChars, currentStyle, entityKey);
this.update(EditorState.push(editorState, contentState, 'insert-characters'));
return;
}
if (mustReset) {
this.update(EditorState.set(editorState, {
nativelyRenderedContent: null,
forceSelection: true
}));
}
}
};
module.exports = DraftEditorCompositionHandler;