@atlaskit/renderer
Version:
Renderer component
37 lines • 1.5 kB
JavaScript
export class ElementSelection {
constructor(selection) {
this.type = selection ? selection.type : 'None';
if (selection && this.type !== 'None') {
this.range = selection.getRangeAt(0);
}
}
eq(selection) {
if (!selection) {
return false;
}
if (this.range && selection.range) {
var _this$range, _this$range2;
const startMatches = ((_this$range = this.range) === null || _this$range === void 0 ? void 0 : _this$range.compareBoundaryPoints(Range.START_TO_START, selection.range)) === 0;
const 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';
}
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);
}
select(el) {
const range = document.createRange();
range.selectNodeContents(el);
const selection = window.getSelection();
if (selection) {
selection.removeAllRanges();
selection.addRange(range);
}
return ElementSelection.fromWindow();
}
static fromWindow(win = window) {
return new ElementSelection(win.getSelection());
}
}