1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var DraftOffsetKey = require("./DraftOffsetKey");
|
14 |
|
15 | var nullthrows = require("fbjs/lib/nullthrows");
|
16 |
|
17 | function getUpdatedSelectionState(editorState, anchorKey, anchorOffset, focusKey, focusOffset) {
|
18 | var selection = nullthrows(editorState.getSelection());
|
19 |
|
20 | if (!anchorKey || !focusKey) {
|
21 |
|
22 | if (process.env.NODE_ENV !== "production") {
|
23 |
|
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 |
|
41 | if (process.env.NODE_ENV !== "production") {
|
42 |
|
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 |
|
86 | module.exports = getUpdatedSelectionState; |
\ | No newline at end of file |