From 06760d688e00453b6924af0921821883d9206c72 Mon Sep 17 00:00:00 2001 From: Jason Chen Date: Mon, 8 May 2017 14:26:34 -0700 Subject: [PATCH] more selection refactoring --- core/selection.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/core/selection.js b/core/selection.js index c0efa19b2b..d08da8f1e1 100644 --- a/core/selection.js +++ b/core/selection.js @@ -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]); @@ -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;