From 931ce89842bfd22a7cdb4cef51116fdbd3830633 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Trzci=C5=84ski?= Date: Wed, 25 Sep 2024 08:28:25 +0200 Subject: [PATCH] [#66130] add function for checking previous elements --- src/extensions/syncDualPane.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/extensions/syncDualPane.js b/src/extensions/syncDualPane.js index 567e99d..022dd92 100644 --- a/src/extensions/syncDualPane.js +++ b/src/extensions/syncDualPane.js @@ -56,15 +56,11 @@ export function handlePreviewClickToScroll(/** @type {{ target: HTMLElement }} * let id = ev.target.getAttribute("data-line-id"); let elem = ev.target; if (!id) { - // check parents - outer: while (elem.tagName !== "HTML-CHUNK") { + // check parents and siblings + while (elem.tagName !== "HTML-CHUNK") { const parent = elem.parentElement; - // check siblings - while (elem != null) { - id = elem.getAttribute("data-line-id"); - if (id) break outer; - elem = elem.previousElementSibling; - } + [id, elem] = findSoruceMappedPreviousElement(elem); + if (id) break; elem = parent; } } @@ -102,3 +98,14 @@ export function handlePreviewClickToScroll(/** @type {{ target: HTMLElement }} * setCursor(); } } + +function findSoruceMappedPreviousElement(startingElem) { + let elem = startingElem; + while (elem != null) { + const id = elem.getAttribute("data-line-id"); + if (id) return [id, elem]; + elem = elem.previousElementSibling; + } + + return [undefined, elem]; +}