Skip to content
This repository was archived by the owner on Oct 17, 2025. It is now read-only.

Commit 4e13100

Browse files
stephanmaxSchalk Neethling
authored andcommitted
Issue#946 open links in new tab (#1009)
I added two helper functions that are called inside the editor's `render` method. `openLinksInNewTab` prevents the default click event on external links (all anchors with an `href` attribute that starts with `http`) and opens them in a new tab. While I was at it, I stumbled upon the issue that relative links (like `<a href="#someanchor">`) also didn't work. I didn't find a ticket for that so I thought it would make sense to fix in the scope of this ticket. `scrollToAnchors` prevents the default click event on relative links and scrolls the targeted anchor into view. I hope that the code structure adheres to your quality standards.
1 parent c977bc4 commit 4e13100

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

js/editor-libs/mce-utils.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ module.exports = {
3737

3838
return tmpElem.style[property] !== undefined;
3939
},
40+
/**
41+
* Interrupts the default click event on external links inside
42+
* the shadow dom and opens them in a new tab instead
43+
* @param {Array} externalLinks - all external links inside the shadow dom
44+
*/
45+
openLinksInNewTab: function(externalLinks) {
46+
externalLinks.forEach(function(externalLink) {
47+
externalLink.addEventListener('click', function(event) {
48+
event.preventDefault();
49+
window.open(externalLink.href);
50+
});
51+
});
52+
},
4053
/**
4154
* Posts a name to set as a mark to Kuma for
4255
* processing and beaconing to GA
@@ -45,6 +58,20 @@ module.exports = {
4558
postToKuma: function(perf) {
4659
window.parent.postMessage(perf, 'https://developer.mozilla.org');
4760
},
61+
/**
62+
* Interrupts the default click event on relative links inside
63+
* the shadow dom and scrolls to the targeted anchor
64+
* @param {Object} shadow - the shadow dom root
65+
* @param {Array} relativeLinks - all relative links inside the shadow dom
66+
*/
67+
scrollToAnchors: function(shadow, relativeLinks) {
68+
relativeLinks.forEach(function(relativeLink) {
69+
relativeLink.addEventListener('click', function(event) {
70+
event.preventDefault();
71+
shadow.querySelector(relativeLink.hash).scrollIntoView();
72+
});
73+
});
74+
},
4875
/**
4976
* Hides the default example and shows the custom block
5077
* @param {object} customBlock - The HTML section to show

js/editor.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080

8181
shadow.appendChild(document.importNode(content, true));
8282
setOutputHeight(shadow.querySelector('div'));
83+
mceUtils.openLinksInNewTab(shadow.querySelectorAll('a[href^="http"]'));
84+
mceUtils.scrollToAnchors(shadow, shadow.querySelectorAll('a[href^="#"]'));
8385
}
8486

8587
/**

0 commit comments

Comments
 (0)