Skip to content

Commit

Permalink
Correct behavior for URL fragment links (#914)
Browse files Browse the repository at this point in the history
* Correct behavior for URL fragment links

* Forgotten file
  • Loading branch information
NiedziolkaMichal authored Nov 10, 2022
1 parent 78e022e commit 6d82045
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions editor/js/editor-libs/mce-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
});
}
2 changes: 1 addition & 1 deletion editor/js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 6d82045

Please sign in to comment.