Better selection handling, fixing scan length being treated as a string.
This commit is contained in:
parent
14fd0d6514
commit
061cbb0141
@ -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');
|
||||
|
@ -38,6 +38,8 @@ function sanitizeOptions(options) {
|
||||
}
|
||||
}
|
||||
|
||||
options.scanLength = parseInt(options.scanLength);
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
|
@ -27,16 +27,29 @@ class Range {
|
||||
}
|
||||
|
||||
setLength(length) {
|
||||
const node = this.rng.startContainer;
|
||||
const offset = this.rng.startOffset;
|
||||
|
||||
length = Math.min(node.length - offset, length);
|
||||
if (length === 0) {
|
||||
return null;
|
||||
const end = this.findEnd(this.rng.startContainer, this.rng.startOffset, length);
|
||||
this.rng.setEnd(end.node, end.offset);
|
||||
}
|
||||
|
||||
this.rng.setEnd(node, offset + length);
|
||||
return length;
|
||||
findEnd(node, offset, 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) {
|
||||
|
Loading…
Reference in New Issue
Block a user