@atlaskit/renderer
Version:
Renderer component
50 lines • 2.06 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
export var ElementSelection = /*#__PURE__*/function () {
function ElementSelection(selection) {
_classCallCheck(this, ElementSelection);
this.type = selection ? selection.type : 'None';
if (selection && this.type !== 'None') {
this.range = selection.getRangeAt(0);
}
}
return _createClass(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());
}
}]);
}();