Don't position caret if selection exists

This commit is contained in:
Artur Shaikhullin 2017-11-29 21:01:47 +06:00
parent 70b58d1928
commit f965c0daec

View File

@ -1070,7 +1070,7 @@ window._qutebrowser.caret = (function() {
return false; return false;
}; };
function addCaretStyle() { function injectCaretStyles() {
var style = '.CaretBrowsing_Caret {' + var style = '.CaretBrowsing_Caret {' +
' position: absolute;' + ' position: absolute;' +
' z-index: 2147483647;' + ' z-index: 2147483647;' +
@ -1104,10 +1104,12 @@ window._qutebrowser.caret = (function() {
*/ */
funcs.setInitialCursor = () => { funcs.setInitialCursor = () => {
if (!CaretBrowsing.initiated) { if (!CaretBrowsing.initiated) {
addCaretStyle(); injectCaretStyles();
CaretBrowsing.setInitialCursor(); CaretBrowsing.setInitialCursor();
} else { } else {
positionCaret(); if (!window.getSelection().toString()) {
positionCaret();
}
CaretBrowsing.toggle(); CaretBrowsing.toggle();
} }
} }
@ -1116,6 +1118,35 @@ window._qutebrowser.caret = (function() {
CaretBrowsing.toggle(); CaretBrowsing.toggle();
} }
function positionCaret() {
const walker = document.createTreeWalker(document.body, -1);
let node;
const textNodes = [];
let el;
while ((node = walker.nextNode())) {
if (node.nodeType === 3 && node.nodeValue.trim() !== "") {
textNodes.push(node);
}
}
for (let i = 0; i < textNodes.length; i++) {
const element = textNodes[i].parentElement;
if (isElementInViewport(element)) {
el = element;
break;
}
}
if (el !== undefined) {
var start = new Cursor(el, 0, '');
var end = new Cursor(el, 0, '');
var nodesCrossed = [];
var result = TraverseUtil.getNextChar(start, end, nodesCrossed, true);
if (result == null) {
return;
}
CaretBrowsing.setAndValidateSelection(start, start);
}
}
CaretBrowsing.setInitialCursor = function() { CaretBrowsing.setInitialCursor = function() {
var sel = window.getSelection(); var sel = window.getSelection();
if (sel.rangeCount > 0) { if (sel.rangeCount > 0) {
@ -2356,34 +2387,6 @@ window._qutebrowser.caret = (function() {
return boundingRect.top >= -20; return boundingRect.top >= -20;
} }
function positionCaret() {
const walker = document.createTreeWalker(document.body, -1);
let node;
const textNodes = [];
let el;
while ((node = walker.nextNode())) {
if (node.nodeType === 3 && node.nodeValue.trim() !== "") {
textNodes.push(node);
}
}
for (let i = 0; i < textNodes.length; i++) {
const element = textNodes[i].parentElement;
if (isElementInViewport(element)) {
el = element;
break;
}
}
if (el !== undefined) {
var start = new Cursor(el, 0, '');
var end = new Cursor(el, 0, '');
var nodesCrossed = [];
var result = TraverseUtil.getNextChar(start, end, nodesCrossed, true);
if (result == null) {
return;
}
CaretBrowsing.setAndValidateSelection(start, start);
}
}
return funcs; return funcs;
})(); })();