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) { switch (section) {
case 'general': case 'general':
optsNew.scanLength = $('#scan-length').val(); optsNew.scanLength = parseInt($('#scan-length').val());
optsNew.activateOnStartup = $('#activate-on-startup').prop('checked'); optsNew.activateOnStartup = $('#activate-on-startup').prop('checked');
optsNew.loadEnamDict = $('#load-enamdict').prop('checked'); optsNew.loadEnamDict = $('#load-enamdict').prop('checked');
optsNew.selectMatchedText = $('#select-matched-text').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; return options;
} }

View File

@ -27,16 +27,29 @@ class Range {
} }
setLength(length) { setLength(length) {
const node = this.rng.startContainer; const end = this.findEnd(this.rng.startContainer, this.rng.startOffset, length);
const offset = this.rng.startOffset; this.rng.setEnd(end.node, end.offset);
length = Math.min(node.length - offset, length);
if (length === 0) {
return null;
} }
this.rng.setEnd(node, offset + length); findEnd(node, offset, length) {
return length; if (node.nodeType === 3) {
const remainder = node.data.length - offset;
if (remainder >= length) {
return {node, offset: offset + length};
}
length -= remainder;
}
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) { containsPoint(point) {