1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
|
14 |
|
15 | var Immutable = require("immutable");
|
16 |
|
17 | var Record = Immutable.Record;
|
18 | var defaultRecord = {
|
19 | anchorKey: '',
|
20 | anchorOffset: 0,
|
21 | focusKey: '',
|
22 | focusOffset: 0,
|
23 | isBackward: false,
|
24 | hasFocus: false
|
25 | };
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | var SelectionStateRecord = Record(defaultRecord);
|
31 |
|
32 | var SelectionState = function (_SelectionStateRecord) {
|
33 | _inheritsLoose(SelectionState, _SelectionStateRecord);
|
34 |
|
35 | function SelectionState() {
|
36 | return _SelectionStateRecord.apply(this, arguments) || this;
|
37 | }
|
38 |
|
39 | var _proto = SelectionState.prototype;
|
40 |
|
41 | _proto.serialize = function serialize() {
|
42 | return 'Anchor: ' + this.getAnchorKey() + ':' + this.getAnchorOffset() + ', ' + 'Focus: ' + this.getFocusKey() + ':' + this.getFocusOffset() + ', ' + 'Is Backward: ' + String(this.getIsBackward()) + ', ' + 'Has Focus: ' + String(this.getHasFocus());
|
43 | };
|
44 |
|
45 | _proto.getAnchorKey = function getAnchorKey() {
|
46 | return this.get('anchorKey');
|
47 | };
|
48 |
|
49 | _proto.getAnchorOffset = function getAnchorOffset() {
|
50 | return this.get('anchorOffset');
|
51 | };
|
52 |
|
53 | _proto.getFocusKey = function getFocusKey() {
|
54 | return this.get('focusKey');
|
55 | };
|
56 |
|
57 | _proto.getFocusOffset = function getFocusOffset() {
|
58 | return this.get('focusOffset');
|
59 | };
|
60 |
|
61 | _proto.getIsBackward = function getIsBackward() {
|
62 | return this.get('isBackward');
|
63 | };
|
64 |
|
65 | _proto.getHasFocus = function getHasFocus() {
|
66 | return this.get('hasFocus');
|
67 | }
|
68 | |
69 |
|
70 |
|
71 |
|
72 | ;
|
73 |
|
74 | _proto.hasEdgeWithin = function hasEdgeWithin(blockKey, start, end) {
|
75 | var anchorKey = this.getAnchorKey();
|
76 | var focusKey = this.getFocusKey();
|
77 |
|
78 | if (anchorKey === focusKey && anchorKey === blockKey) {
|
79 | var selectionStart = this.getStartOffset();
|
80 | var selectionEnd = this.getEndOffset();
|
81 | return start <= selectionStart && selectionStart <= end ||
|
82 | start <= selectionEnd && selectionEnd <= end
|
83 | ;
|
84 | }
|
85 |
|
86 | if (blockKey !== anchorKey && blockKey !== focusKey) {
|
87 | return false;
|
88 | }
|
89 |
|
90 | var offsetToCheck = blockKey === anchorKey ? this.getAnchorOffset() : this.getFocusOffset();
|
91 | return start <= offsetToCheck && end >= offsetToCheck;
|
92 | };
|
93 |
|
94 | _proto.isCollapsed = function isCollapsed() {
|
95 | return this.getAnchorKey() === this.getFocusKey() && this.getAnchorOffset() === this.getFocusOffset();
|
96 | };
|
97 |
|
98 | _proto.getStartKey = function getStartKey() {
|
99 | return this.getIsBackward() ? this.getFocusKey() : this.getAnchorKey();
|
100 | };
|
101 |
|
102 | _proto.getStartOffset = function getStartOffset() {
|
103 | return this.getIsBackward() ? this.getFocusOffset() : this.getAnchorOffset();
|
104 | };
|
105 |
|
106 | _proto.getEndKey = function getEndKey() {
|
107 | return this.getIsBackward() ? this.getAnchorKey() : this.getFocusKey();
|
108 | };
|
109 |
|
110 | _proto.getEndOffset = function getEndOffset() {
|
111 | return this.getIsBackward() ? this.getAnchorOffset() : this.getFocusOffset();
|
112 | };
|
113 |
|
114 | SelectionState.createEmpty = function createEmpty(key) {
|
115 | return new SelectionState({
|
116 | anchorKey: key,
|
117 | anchorOffset: 0,
|
118 | focusKey: key,
|
119 | focusOffset: 0,
|
120 | isBackward: false,
|
121 | hasFocus: false
|
122 | });
|
123 | };
|
124 |
|
125 | return SelectionState;
|
126 | }(SelectionStateRecord);
|
127 |
|
128 | module.exports = SelectionState; |
\ | No newline at end of file |