From 6d82045e1eb276398fce271ff9f97b534290ed1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Niedzi=C3=B3=C5=82ka?= Date: Thu, 10 Nov 2022 21:38:41 +0100 Subject: [PATCH] Correct behavior for URL fragment links (#914) * Correct behavior for URL fragment links * Forgotten file --- editor/js/editor-libs/mce-utils.js | 9 +++++++-- editor/js/editor.js | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/editor/js/editor-libs/mce-utils.js b/editor/js/editor-libs/mce-utils.js index 4cbd8c40a..e5b95b4d6 100644 --- a/editor/js/editor-libs/mce-utils.js +++ b/editor/js/editor-libs/mce-utils.js @@ -62,14 +62,19 @@ export function openLinksInNewTab(externalLinks) { /** * Interrupts the default click event on relative links inside * the iframe and scrolls to the targeted anchor + * @param {Object} contentWindow - window object, that contains rootElement & relativeLinks * @param {Object} rootElement - root or body element, that contains referenced links * @param {Array} relativeLinks - all relative links inside the iframe */ -export function scrollToAnchors(rootElement, relativeLinks) { +export function scrollToAnchors(contentWindow, rootElement, relativeLinks) { relativeLinks.forEach((relativeLink) => { relativeLink.addEventListener("click", (event) => { event.preventDefault(); - rootElement.querySelector(relativeLink.hash).scrollIntoView(); + let element = rootElement.querySelector(relativeLink.hash); + if(element) { + element.scrollIntoView(); + contentWindow.location.hash = relativeLink.hash; + } }); }); } diff --git a/editor/js/editor.js b/editor/js/editor.js index a49ba2fa3..b30d644e4 100644 --- a/editor/js/editor.js +++ b/editor/js/editor.js @@ -107,7 +107,7 @@ import "../css/tabbed-editor.css"; const contentBody = contentWindow.document.body; mceUtils.openLinksInNewTab(contentBody.querySelectorAll('a[href^="http"]')); - mceUtils.scrollToAnchors(contentBody, contentBody.querySelectorAll('a[href^="#"]')); + mceUtils.scrollToAnchors(contentWindow, contentBody, contentBody.querySelectorAll('a[href^="#"]')); adjustFrameHeight();