134 lines
4.5 KiB
JavaScript
134 lines
4.5 KiB
JavaScript
/**
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
* @providesModule SelectionState
|
|
* @format
|
|
*
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
|
|
|
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
|
|
|
var Immutable = require('immutable');
|
|
|
|
var Record = Immutable.Record;
|
|
|
|
|
|
var defaultRecord = {
|
|
anchorKey: '',
|
|
anchorOffset: 0,
|
|
focusKey: '',
|
|
focusOffset: 0,
|
|
isBackward: false,
|
|
hasFocus: false
|
|
};
|
|
|
|
var SelectionStateRecord = Record(defaultRecord);
|
|
|
|
var SelectionState = function (_SelectionStateRecord) {
|
|
_inherits(SelectionState, _SelectionStateRecord);
|
|
|
|
function SelectionState() {
|
|
_classCallCheck(this, SelectionState);
|
|
|
|
return _possibleConstructorReturn(this, _SelectionStateRecord.apply(this, arguments));
|
|
}
|
|
|
|
SelectionState.prototype.serialize = function serialize() {
|
|
return 'Anchor: ' + this.getAnchorKey() + ':' + this.getAnchorOffset() + ', ' + 'Focus: ' + this.getFocusKey() + ':' + this.getFocusOffset() + ', ' + 'Is Backward: ' + String(this.getIsBackward()) + ', ' + 'Has Focus: ' + String(this.getHasFocus());
|
|
};
|
|
|
|
SelectionState.prototype.getAnchorKey = function getAnchorKey() {
|
|
return this.get('anchorKey');
|
|
};
|
|
|
|
SelectionState.prototype.getAnchorOffset = function getAnchorOffset() {
|
|
return this.get('anchorOffset');
|
|
};
|
|
|
|
SelectionState.prototype.getFocusKey = function getFocusKey() {
|
|
return this.get('focusKey');
|
|
};
|
|
|
|
SelectionState.prototype.getFocusOffset = function getFocusOffset() {
|
|
return this.get('focusOffset');
|
|
};
|
|
|
|
SelectionState.prototype.getIsBackward = function getIsBackward() {
|
|
return this.get('isBackward');
|
|
};
|
|
|
|
SelectionState.prototype.getHasFocus = function getHasFocus() {
|
|
return this.get('hasFocus');
|
|
};
|
|
|
|
/**
|
|
* Return whether the specified range overlaps with an edge of the
|
|
* SelectionState.
|
|
*/
|
|
|
|
|
|
SelectionState.prototype.hasEdgeWithin = function hasEdgeWithin(blockKey, start, end) {
|
|
var anchorKey = this.getAnchorKey();
|
|
var focusKey = this.getFocusKey();
|
|
|
|
if (anchorKey === focusKey && anchorKey === blockKey) {
|
|
var selectionStart = this.getStartOffset();
|
|
var selectionEnd = this.getEndOffset();
|
|
return start <= selectionEnd && selectionStart <= end;
|
|
}
|
|
|
|
if (blockKey !== anchorKey && blockKey !== focusKey) {
|
|
return false;
|
|
}
|
|
|
|
var offsetToCheck = blockKey === anchorKey ? this.getAnchorOffset() : this.getFocusOffset();
|
|
|
|
return start <= offsetToCheck && end >= offsetToCheck;
|
|
};
|
|
|
|
SelectionState.prototype.isCollapsed = function isCollapsed() {
|
|
return this.getAnchorKey() === this.getFocusKey() && this.getAnchorOffset() === this.getFocusOffset();
|
|
};
|
|
|
|
SelectionState.prototype.getStartKey = function getStartKey() {
|
|
return this.getIsBackward() ? this.getFocusKey() : this.getAnchorKey();
|
|
};
|
|
|
|
SelectionState.prototype.getStartOffset = function getStartOffset() {
|
|
return this.getIsBackward() ? this.getFocusOffset() : this.getAnchorOffset();
|
|
};
|
|
|
|
SelectionState.prototype.getEndKey = function getEndKey() {
|
|
return this.getIsBackward() ? this.getAnchorKey() : this.getFocusKey();
|
|
};
|
|
|
|
SelectionState.prototype.getEndOffset = function getEndOffset() {
|
|
return this.getIsBackward() ? this.getAnchorOffset() : this.getFocusOffset();
|
|
};
|
|
|
|
SelectionState.createEmpty = function createEmpty(key) {
|
|
return new SelectionState({
|
|
anchorKey: key,
|
|
anchorOffset: 0,
|
|
focusKey: key,
|
|
focusOffset: 0,
|
|
isBackward: false,
|
|
hasFocus: false
|
|
});
|
|
};
|
|
|
|
return SelectionState;
|
|
}(SelectionStateRecord);
|
|
|
|
module.exports = SelectionState; |