Better selection handling, fixing scan length being treated as a string.

This commit is contained in:
Alex Yatskov 2016-06-12 14:32:23 -07:00
parent 14fd0d6514
commit 061cbb0141
3 changed files with 23 additions and 8 deletions

View File

@ -40,7 +40,7 @@ function formToOptions(section, callback) {
switch (section) {
case 'general':
optsNew.scanLength = $('#scan-length').val();
optsNew.scanLength = parseInt($('#scan-length').val());
optsNew.activateOnStartup = $('#activate-on-startup').prop('checked');
optsNew.loadEnamDict = $('#load-enamdict').prop('checked');
optsNew.selectMatchedText = $('#select-matched-text').prop('checked');

View File

@ -38,6 +38,8 @@ function sanitizeOptions(options) {
}
}
options.scanLength = parseInt(options.scanLength);
return options;
}

View File

@ -27,16 +27,29 @@ class Range {
}
setLength(length) {
const node = this.rng.startContainer;
const offset = this.rng.startOffset;
const end = this.findEnd(this.rng.startContainer, this.rng.startOffset, length);
this.rng.setEnd(end.node, end.offset);
}
length = Math.min(node.length - offset, length);
if (length === 0) {
return null;
findEnd(node, offset, length) {
if (node.nodeType === 3) {
const remainder = node.data.length - offset;
if (remainder >= length) {
return {node, offset: offset + length};
}
length -= remainder;
}
this.rng.setEnd(node, offset + length);
return length;
if (node.childNodes.length > 0) {
return this.findEnd(node.childNodes[0], 0, length);
}
if (node.nextSibling !== null) {
return this.findEnd(node.nextSibling, 0, length);
}
return {node, offset: node.data.length};
}
containsPoint(point) {