Skip to content

Commit

Permalink
more selection refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed May 8, 2017
1 parent b9a7660 commit 06760d6
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,23 @@ class Selection {
if (selection == null || selection.rangeCount <= 0) return null;
let nativeRange = selection.getRangeAt(0);
if (nativeRange == null) return null;
let range = this.nativeToRange(nativeRange);
let range = this.normalizeNative(nativeRange);
debug.info('getNativeRange', range);
return range;
}

getRange() {
let range = this.getNativeRange();
if (range == null) return [null, null];
let normalized = this.getNativeRange();
if (normalized == null) return [null, null];
let range = this.normalizedToRange(normalized);
return [range, normalized];
}

hasFocus() {
return document.activeElement === this.root;
}

normalizedToRange(range) {
let positions = [[range.start.node, range.start.offset]];
if (!range.native.collapsed) {
positions.push([range.end.node, range.end.offset]);
Expand All @@ -160,14 +169,10 @@ class Selection {
});
let start = Math.min(...indexes), end = Math.max(...indexes);
end = Math.min(end, this.scroll.length() - 1);
return [new Range(start, end-start), range];
}

hasFocus() {
return document.activeElement === this.root;
return new Range(start, end-start);
}

nativeToRange(nativeRange) {
normalizeNative(nativeRange) {
if (!contains(this.root, nativeRange.startContainer) ||
(!nativeRange.collapsed && !contains(this.root, nativeRange.endContainer))) {
return null;
Expand Down

0 comments on commit 06760d6

Please sign in to comment.