Skip to content
This repository was archived by the owner on Oct 17, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Move helper functions to utilities module
  • Loading branch information
stephanmax committed Jul 4, 2018
commit 296b9ac09d5cf9fa50c6f7e032f77a3d86102922
27 changes: 27 additions & 0 deletions js/editor-libs/mce-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ module.exports = {

return tmpElem.style[property] !== undefined;
},
/**
* Interrupts the default click event on external links inside
* the shadow dom and opens them in a new tab instead
* @param {Array} externalLinks - all external links inside the shadow dom
*/
openLinksInNewTab: function(externalLinks) {
externalLinks.forEach(function(externalLink) {
externalLink.addEventListener('click', function(event) {
event.preventDefault();
window.open(externalLink.href);
});
});
},
/**
* Posts a name to set as a mark to Kuma for
* processing and beaconing to GA
Expand All @@ -45,6 +58,20 @@ module.exports = {
postToKuma: function(perf) {
window.parent.postMessage(perf, 'https://developer.mozilla.org');
},
/**
* Interrupts the default click event on relative links inside
* the shadow dom and scrolls to the targeted anchor
* @param {Object} shadow - the shadow dom root
* @param {Array} relativeLinks - all relative links inside the shadow dom
*/
scrollToAnchors: function(shadow, relativeLinks) {
relativeLinks.forEach(function(relativeLink) {
relativeLink.addEventListener('click', function(event) {
event.preventDefault();
shadow.querySelector(relativeLink.hash).scrollIntoView();
});
});
},
/**
* Hides the default example and shows the custom block
* @param {object} customBlock - The HTML section to show
Expand Down
33 changes: 2 additions & 31 deletions js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,6 @@
}
}

/**
* Interrupts the default click event on external links inside
* the shadow dom and opens them in a new tab instead
* @param {Array} externalLinks - all external links inside the shadow dom
*/
function openLinksInNewTab(externalLinks) {
externalLinks.forEach(function(externalLink) {
externalLink.addEventListener('click', function(event) {
event.preventDefault();
window.open(externalLink.href);
});
});
}

/**
* Interrupts the default click event on relative links inside
* the shadow dom and scrolls to the targeted anchor
* @param {Object} shadow - the shadow dom root
* @param {Array} relativeLinks - all relative links inside the shadow dom
*/
function scrollToAnchors(shadow, relativeLinks) {
relativeLinks.forEach(function(relativeLink) {
relativeLink.addEventListener('click', function(event) {
event.preventDefault();
shadow.querySelector(relativeLink.hash).scrollIntoView();
});
});
}

/**
* Set or update the CSS and HTML in the output pane.
* @param {Object} content - The content of the template element.
Expand All @@ -109,8 +80,8 @@

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

/**
Expand Down