@atlaskit/renderer
Version:
Renderer component
57 lines (56 loc) • 2.36 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ElementSelection = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var ElementSelection = exports.ElementSelection = /*#__PURE__*/function () {
function ElementSelection(selection) {
(0, _classCallCheck2.default)(this, ElementSelection);
this.type = selection ? selection.type : 'None';
if (selection && this.type !== 'None') {
this.range = selection.getRangeAt(0);
}
}
return (0, _createClass2.default)(ElementSelection, [{
key: "eq",
value: function eq(selection) {
if (!selection) {
return false;
}
if (this.range && selection.range) {
var _this$range, _this$range2;
var startMatches = ((_this$range = this.range) === null || _this$range === void 0 ? void 0 : _this$range.compareBoundaryPoints(Range.START_TO_START, selection.range)) === 0;
var endMatches = ((_this$range2 = this.range) === null || _this$range2 === void 0 ? void 0 : _this$range2.compareBoundaryPoints(Range.END_TO_END, selection.range)) === 0;
return startMatches && endMatches;
}
return this.type === 'None' && selection.type === 'None';
}
}, {
key: "inside",
value: function inside(el) {
var _this$range$commonAnc, _this$range3;
return el.contains((_this$range$commonAnc = (_this$range3 = this.range) === null || _this$range3 === void 0 ? void 0 : _this$range3.commonAncestorContainer) !== null && _this$range$commonAnc !== void 0 ? _this$range$commonAnc : null);
}
}, {
key: "select",
value: function select(el) {
var range = document.createRange();
range.selectNodeContents(el);
var selection = window.getSelection();
if (selection) {
selection.removeAllRanges();
selection.addRange(range);
}
return ElementSelection.fromWindow();
}
}], [{
key: "fromWindow",
value: function fromWindow() {
var win = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window;
return new ElementSelection(win.getSelection());
}
}]);
}();