fix closestVisible nearby check

This commit is contained in:
tophf 2020-12-08 18:34:16 +03:00
parent 463c3b5139
commit 26a539bd62

View File

@ -209,23 +209,25 @@ function SectionsEditor() {
2. last active if visible 2. last active if visible
3. first visible 3. first visible
*/ */
function closestVisible(nearbyElement) { function closestVisible(el) {
const cm = // closest editor should have at least 2 lines visible
nearbyElement instanceof CodeMirror ? nearbyElement : const lineHeight = sections[0].cm.defaultTextHeight();
nearbyElement instanceof Node && getAssociatedEditor(nearbyElement) || getLastActivatedEditor(); const margin = 2 * lineHeight;
if (nearbyElement instanceof Node && cm) { const cm = el instanceof CodeMirror ? el :
const {left, top} = nearbyElement.getBoundingClientRect(); el instanceof Node && getAssociatedEditor(el) || getLastActivatedEditor();
const bounds = cm.display.wrapper.getBoundingClientRect(); if (el === cm) el = document.body;
if (top >= 0 && top >= bounds.top && if (el instanceof Node && cm) {
left >= 0 && left >= bounds.left) { const {wrapper} = cm.display;
if (!container.contains(el) || wrapper.closest('.section').contains(el)) {
const rect = wrapper.getBoundingClientRect();
if (rect.top < window.innerHeight - margin && rect.bottom > margin) {
return cm; return cm;
} }
} }
// closest editor should have at least 2 lines visible }
const lineHeight = sections[0].cm.defaultTextHeight();
const scrollY = window.scrollY; const scrollY = window.scrollY;
const windowBottom = scrollY + window.innerHeight - 2 * lineHeight; const windowBottom = scrollY + window.innerHeight - margin;
const allSectionsContainerTop = scrollY + $('#sections').getBoundingClientRect().top; const allSectionsContainerTop = scrollY + container.getBoundingClientRect().top;
const distances = []; const distances = [];
const alreadyInView = cm && offscreenDistance(null, cm) === 0; const alreadyInView = cm && offscreenDistance(null, cm) === 0;
return alreadyInView ? cm : findClosest(); return alreadyInView ? cm : findClosest();