UNPKG

1.28 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 *
9 * @emails oncall+draft_js
10 */
11'use strict';
12
13var getDraftEditorSelectionWithNodes = require("./getDraftEditorSelectionWithNodes");
14/**
15 * Convert the current selection range to an anchor/focus pair of offset keys
16 * and values that can be interpreted by components.
17 */
18
19
20function getDraftEditorSelection(editorState, root) {
21 var selection = root.ownerDocument.defaultView.getSelection();
22 var anchorNode = selection.anchorNode,
23 anchorOffset = selection.anchorOffset,
24 focusNode = selection.focusNode,
25 focusOffset = selection.focusOffset,
26 rangeCount = selection.rangeCount;
27
28 if ( // No active selection.
29 rangeCount === 0 || // No selection, ever. As in, the user hasn't selected anything since
30 // opening the document.
31 anchorNode == null || focusNode == null) {
32 return {
33 selectionState: editorState.getSelection().set('hasFocus', false),
34 needsRecovery: false
35 };
36 }
37
38 return getDraftEditorSelectionWithNodes(editorState, root, anchorNode, anchorOffset, focusNode, focusOffset);
39}
40
41module.exports = getDraftEditorSelection;
\No newline at end of file