UNPKG

3.93 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
13function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
14
15var Immutable = require("immutable");
16
17var Record = Immutable.Record;
18var defaultRecord = {
19 anchorKey: '',
20 anchorOffset: 0,
21 focusKey: '',
22 focusOffset: 0,
23 isBackward: false,
24 hasFocus: false
25};
26/* $FlowFixMe[unclear-type] This comment suppresses an error found when
27 * automatically adding a type annotation with the codemod Komodo/Annotate_
28 * exports. To see the error delete this comment and run Flow. */
29
30var SelectionStateRecord = Record(defaultRecord);
31
32var SelectionState = /*#__PURE__*/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 * Return whether the specified range overlaps with an edge of the
70 * SelectionState.
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 || // selectionStart is between start and end, or
82 start <= selectionEnd && selectionEnd <= end // selectionEnd is between start and 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
128module.exports = SelectionState;
\No newline at end of file