UNPKG

3.06 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 DraftOffsetKey = require("./DraftOffsetKey");
14
15var nullthrows = require("fbjs/lib/nullthrows");
16
17function getUpdatedSelectionState(editorState, anchorKey, anchorOffset, focusKey, focusOffset) {
18 var selection = nullthrows(editorState.getSelection());
19
20 if (!anchorKey || !focusKey) {
21 // If we cannot make sense of the updated selection state, stick to the current one.
22 if (process.env.NODE_ENV !== "production") {
23 /* eslint-disable-next-line */
24 console.warn('Invalid selection state.', arguments, editorState.toJS());
25 }
26
27 return selection;
28 }
29
30 var anchorPath = DraftOffsetKey.decode(anchorKey);
31 var anchorBlockKey = anchorPath.blockKey;
32 var anchorLeafBlockTree = editorState.getBlockTree(anchorBlockKey);
33 var anchorLeaf = anchorLeafBlockTree && anchorLeafBlockTree.getIn([anchorPath.decoratorKey, 'leaves', anchorPath.leafKey]);
34 var focusPath = DraftOffsetKey.decode(focusKey);
35 var focusBlockKey = focusPath.blockKey;
36 var focusLeafBlockTree = editorState.getBlockTree(focusBlockKey);
37 var focusLeaf = focusLeafBlockTree && focusLeafBlockTree.getIn([focusPath.decoratorKey, 'leaves', focusPath.leafKey]);
38
39 if (!anchorLeaf || !focusLeaf) {
40 // If we cannot make sense of the updated selection state, stick to the current one.
41 if (process.env.NODE_ENV !== "production") {
42 /* eslint-disable-next-line */
43 console.warn('Invalid selection state.', arguments, editorState.toJS());
44 }
45
46 return selection;
47 }
48
49 var anchorLeafStart = anchorLeaf.get('start');
50 var focusLeafStart = focusLeaf.get('start');
51 var anchorBlockOffset = anchorLeaf ? anchorLeafStart + anchorOffset : null;
52 var focusBlockOffset = focusLeaf ? focusLeafStart + focusOffset : null;
53 var areEqual = selection.getAnchorKey() === anchorBlockKey && selection.getAnchorOffset() === anchorBlockOffset && selection.getFocusKey() === focusBlockKey && selection.getFocusOffset() === focusBlockOffset;
54
55 if (areEqual) {
56 return selection;
57 }
58
59 var isBackward = false;
60
61 if (anchorBlockKey === focusBlockKey) {
62 var anchorLeafEnd = anchorLeaf.get('end');
63 var focusLeafEnd = focusLeaf.get('end');
64
65 if (focusLeafStart === anchorLeafStart && focusLeafEnd === anchorLeafEnd) {
66 isBackward = focusOffset < anchorOffset;
67 } else {
68 isBackward = focusLeafStart < anchorLeafStart;
69 }
70 } else {
71 var startKey = editorState.getCurrentContent().getBlockMap().keySeq().skipUntil(function (v) {
72 return v === anchorBlockKey || v === focusBlockKey;
73 }).first();
74 isBackward = startKey === focusBlockKey;
75 }
76
77 return selection.merge({
78 anchorKey: anchorBlockKey,
79 anchorOffset: anchorBlockOffset,
80 focusKey: focusBlockKey,
81 focusOffset: focusBlockOffset,
82 isBackward: isBackward
83 });
84}
85
86module.exports = getUpdatedSelectionState;
\No newline at end of file