-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: highlight js by moving header js
- Loading branch information
Nathan Rogan
committed
Oct 19, 2020
1 parent
9ca8fa5
commit 76f4802
Showing
3 changed files
with
301 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 42 additions & 35 deletions
77
src/www/_partials/component-example/_component-example.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,57 @@ | ||
function showCode() { | ||
function showCode(ele) { | ||
// for each button we create below | ||
document.querySelectorAll('.wmcads-js-show-code').forEach(ele => { | ||
function showMore(e) { | ||
const btn = e.target || e.srcElement; // Set btn to the element from which the click came | ||
|
||
// if the btn clicked contains this class, then it must be active, so reset it back to the norm | ||
if (btn.classList.contains('wmcads-js-show-code--active')) { | ||
btn.previousSibling.querySelector('.hljs').style.maxHeight = '200px'; | ||
btn.classList.remove('wmcads-js-show-code--active'); | ||
btn.innerText = 'Show more code'; | ||
} else { | ||
// Else expand the code preview to 100% | ||
btn.previousSibling.querySelector('.hljs').style.maxHeight = '100%'; | ||
btn.classList.add('wmcads-js-show-code--active'); | ||
btn.innerText = 'Show less code'; | ||
} | ||
function showMore(e) { | ||
const btn = e.target || e.srcElement; // Set btn to the element from which the click came | ||
|
||
// if the btn clicked contains this class, then it must be active, so reset it back to the norm | ||
if (btn.classList.contains('wmnds-js-show-code--active')) { | ||
btn.previousSibling.querySelector('.hljs').style.maxHeight = '200px'; | ||
btn.classList.remove('wmnds-js-show-code--active'); | ||
btn.innerText = 'Show more code'; | ||
} else { | ||
// Else expand the code preview to 100% | ||
btn.previousSibling.querySelector('.hljs').style.maxHeight = '100%'; | ||
btn.classList.add('wmnds-js-show-code--active'); | ||
btn.innerText = 'Show less code'; | ||
} | ||
} | ||
|
||
// when clicked | ||
ele.addEventListener('click', e => { | ||
e.preventDefault(); | ||
showMore(e); | ||
}); | ||
|
||
// when clicked | ||
ele.addEventListener('click', e => { | ||
// When keyboard "enter" pressed | ||
ele.addEventListener('keydown', e => { | ||
if (e.keyCode === 13) { | ||
e.preventDefault(); | ||
showMore(e); | ||
}); | ||
|
||
// When keyboard "enter" pressed | ||
ele.addEventListener('keydown', e => { | ||
if (e.keyCode === 13) { | ||
e.preventDefault(); | ||
showMore(e); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
export { showCode }; | ||
|
||
const displayShowMore = element => { | ||
// If the code preview is 192 height (without padding) then we need to display the 'show more code' button | ||
if (element.clientHeight >= 192 && !element.classList.contains('wmnds-show-more-ignore')) { | ||
const htmlString = '<a href="#" class="wmnds-link wmnds-js-show-code">Show more code</a>'; | ||
|
||
element.parentElement.insertAdjacentHTML('afterend', htmlString); | ||
} | ||
}; | ||
|
||
export { displayShowMore }; | ||
|
||
export default () => { | ||
const { hljs } = window; // declare higlightJS as global var | ||
document.querySelectorAll('pre code').forEach(element => { | ||
// Run highlightJS for each pre code element found */ | ||
hljs.highlightBlock(element); | ||
// If the code preview is 192 height (without padding) then we need to display the 'show more code' button | ||
if (element.clientHeight >= 192 && !element.classList.contains('wmcads-show-more-ignore')) { | ||
const htmlString = '<a href="#" class="wmcads-link wmcads-js-show-code">Show more code</a>'; | ||
|
||
element.parentElement.insertAdjacentHTML('afterend', htmlString); | ||
} | ||
displayShowMore(element); | ||
}); | ||
document.querySelectorAll('.wmnds-js-show-code').forEach(ele => { | ||
showCode(ele); // run show code function above when hljs has init | ||
}); | ||
|
||
showCode(); // run show code function above when hljs has init | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters