Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed May 8, 2017
1 parent 488af3f commit b9a7660
Showing 1 changed file with 46 additions and 36 deletions.
82 changes: 46 additions & 36 deletions core/selection.js
Original file line number Diff line number Diff line change
@@ -134,30 +134,7 @@ class Selection {
if (selection == null || selection.rangeCount <= 0) return null;
let nativeRange = selection.getRangeAt(0);
if (nativeRange == null) return null;
if (!contains(this.root, nativeRange.startContainer) ||
(!nativeRange.collapsed && !contains(this.root, nativeRange.endContainer))) {
return null;
}
let range = {
start: { node: nativeRange.startContainer, offset: nativeRange.startOffset },
end: { node: nativeRange.endContainer, offset: nativeRange.endOffset },
native: nativeRange
};
[range.start, range.end].forEach(function(position) {
let node = position.node, offset = position.offset;
while (!(node instanceof Text) && node.childNodes.length > 0) {
if (node.childNodes.length > offset) {
node = node.childNodes[offset];
offset = 0;
} else if (node.childNodes.length === offset) {
node = node.lastChild;
offset = node instanceof Text ? node.data.length : node.childNodes.length + 1;
} else {
break;
}
}
position.node = node, position.offset = offset;
});
let range = this.nativeToRange(nativeRange);
debug.info('getNativeRange', range);
return range;
}
@@ -190,6 +167,50 @@ class Selection {
return document.activeElement === this.root;
}

nativeToRange(nativeRange) {
if (!contains(this.root, nativeRange.startContainer) ||
(!nativeRange.collapsed && !contains(this.root, nativeRange.endContainer))) {
return null;
}
let range = {
start: { node: nativeRange.startContainer, offset: nativeRange.startOffset },
end: { node: nativeRange.endContainer, offset: nativeRange.endOffset },
native: nativeRange
};
[range.start, range.end].forEach(function(position) {
let node = position.node, offset = position.offset;
while (!(node instanceof Text) && node.childNodes.length > 0) {
if (node.childNodes.length > offset) {
node = node.childNodes[offset];
offset = 0;
} else if (node.childNodes.length === offset) {
node = node.lastChild;
offset = node instanceof Text ? node.data.length : node.childNodes.length + 1;
} else {
break;
}
}
position.node = node, position.offset = offset;
});
return range;
}

rangeToNative(range) {
let indexes = range.collapsed ? [range.index] : [range.index, range.index + range.length];
let args = [];
let scrollLength = this.scroll.length();
indexes.forEach((index, i) => {
index = Math.min(scrollLength - 1, index);
let node, [leaf, offset] = this.scroll.leaf(index);
[node, offset] = leaf.position(offset, i !== 0);
args.push(node, offset);
});
if (args.length < 2) {
args = args.concat(args);
}
return args;
}

scrollIntoView(range = this.lastRange) {
if (range == null) return;
let bounds = this.getBounds(range.index, range.length);
@@ -254,18 +275,7 @@ class Selection {
}
debug.info('setRange', range);
if (range != null) {
let indexes = range.collapsed ? [range.index] : [range.index, range.index + range.length];
let args = [];
let scrollLength = this.scroll.length();
indexes.forEach((index, i) => {
index = Math.min(scrollLength - 1, index);
let node, [leaf, offset] = this.scroll.leaf(index);
[node, offset] = leaf.position(offset, i !== 0);
args.push(node, offset);
});
if (args.length < 2) {
args = args.concat(args);
}
let args = this.rangeToNative(range);
this.setNativeRange(...args, force);
} else {
this.setNativeRange(null);

0 comments on commit b9a7660

Please sign in to comment.