This commit is contained in:
Alex Yatskov 2016-07-23 12:47:42 -07:00
parent 6099de71d8
commit a889e1ffaa
2 changed files with 28 additions and 23 deletions

View File

@ -88,7 +88,17 @@ class Client {
}
textSourceFromPoint(point) {
return Range.fromPoint(point);
const element = document.elementFromPoint(point.x, point.y);
if (element !== null && element.nodeName === 'IMG') {
return new ImageSource(element);
}
const range = document.caretRangeFromPoint(point.x, point.y);
if (range !== null) {
return new RangeSource(range);
}
return null;
}
searchAt(point) {

View File

@ -17,7 +17,7 @@
*/
class Range {
class RangeSource {
constructor(range) {
this.rng = range;
}
@ -27,7 +27,7 @@ class Range {
}
setLength(length) {
const end = Range.seekEnd(this.rng.startContainer, this.rng.startOffset + length);
const end = RangeSource.seekEnd(this.rng.startContainer, this.rng.startOffset + length);
this.rng.setEnd(end.node, end.offset);
}
@ -70,18 +70,18 @@ class Range {
static seekEnd(node, length) {
const state = {node, offset: 0, length};
if (!Range.seekEndRecurse(node, state)) {
if (!RangeSource.seekEndRecurse(node, state)) {
return {node: state.node, offset: state.offset};
}
for (let sibling = node.nextSibling; sibling !== null; sibling = sibling.nextSibling) {
if (!Range.seekEndRecurse(sibling, state)) {
if (!RangeSource.seekEndRecurse(sibling, state)) {
return {node: state.node, offset: state.offset};
}
}
for (let sibling = node.parentElement.nextSibling; sibling !== null; sibling = sibling.nextSibling) {
if (!Range.seekEndRecurse(sibling, state)) {
if (!RangeSource.seekEndRecurse(sibling, state)) {
return {node: state.node, offset: state.offset};
}
}
@ -97,7 +97,7 @@ class Range {
state.length -= consumed;
} else {
for (let i = 0; i < node.childNodes.length; ++i) {
if (!Range.seekEndRecurse(node.childNodes[i], state)) {
if (!RangeSource.seekEndRecurse(node.childNodes[i], state)) {
break;
}
}
@ -105,9 +105,4 @@ class Range {
return state.length > 0;
}
static fromPoint(point) {
const range = document.caretRangeFromPoint(point.x, point.y);
return range === null ? null : new Range(range);
}
}