/** * 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 getDraftEditorSelectionWithNodes * @format * @flow */ 'use strict'; import type { DOMDerivedSelection } from './DOMDerivedSelection'; import type EditorState from './EditorState'; var findAncestorOffsetKey = require('./findAncestorOffsetKey'); var getSelectionOffsetKeyForNode = require('./getSelectionOffsetKeyForNode'); var getUpdatedSelectionState = require('./getUpdatedSelectionState'); var invariant = require('fbjs/lib/invariant'); var nullthrows = require('fbjs/lib/nullthrows'); type SelectionPoint = { key: string; offset: number; }; /** * Convert the current selection range to an anchor/focus pair of offset keys * and values that can be interpreted by components. */ function getDraftEditorSelectionWithNodes(editorState: EditorState, root: ?HTMLElement, anchorNode: Node, anchorOffset: number, focusNode: Node, focusOffset: number): DOMDerivedSelection { var anchorIsTextNode = anchorNode.nodeType === Node.TEXT_NODE; var focusIsTextNode = focusNode.nodeType === Node.TEXT_NODE; // If the selection range lies only on text nodes, the task is simple. // Find the nearest offset-aware elements and use the // offset values supplied by the selection range. if (anchorIsTextNode && focusIsTextNode) { return { selectionState: getUpdatedSelectionState(editorState, nullthrows(findAncestorOffsetKey(anchorNode)), anchorOffset, nullthrows(findAncestorOffsetKey(focusNode)), focusOffset), needsRecovery: false }; } var anchorPoint = null; var focusPoint = null; var needsRecovery = true; // An element is selected. Convert this selection range into leaf offset // keys and offset values for consumption at the component level. This // is common in Firefox, where select-all and triple click behavior leads // to entire elements being selected. // // Note that we use the `needsRecovery` parameter in the callback here. This // is because when certain elements are selected, the behavior for subsequent // cursor movement (e.g. via arrow keys) is uncertain and may not match // expectations at the component level. For example, if an entire