init
This commit is contained in:
66
themes/godocs-3/assets/js/script.js
Normal file
66
themes/godocs-3/assets/js/script.js
Normal file
@@ -0,0 +1,66 @@
|
||||
'use strict';
|
||||
|
||||
// searchToggler keyboard shortcut
|
||||
const searchToggler = document.querySelectorAll('[data-search-toggler]');
|
||||
searchToggler.forEach((item) => {
|
||||
let userAgentData = navigator?.userAgentData?.platform || navigator?.platform || 'unknown';
|
||||
|
||||
if (userAgentData == 'macOS') {
|
||||
item.innerText = `⌘ + K`
|
||||
} else {
|
||||
item.innerText = `Ctrl + K`
|
||||
}
|
||||
});
|
||||
|
||||
// Navbar fixed
|
||||
window.onscroll = function () {
|
||||
if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 50) {
|
||||
document.querySelector(".navigation").classList.add("nav-bg");
|
||||
} else {
|
||||
document.querySelector(".navigation").classList.remove("nav-bg");
|
||||
}
|
||||
};
|
||||
|
||||
// masonry
|
||||
window.onload = function () {
|
||||
let masonryWrapper = document.querySelector('.masonry-wrapper');
|
||||
// if masonryWrapper is not null, then initialize masonry
|
||||
if (masonryWrapper) {
|
||||
let masonry = new Masonry(masonryWrapper, {
|
||||
columnWidth: 1
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// copy to clipboard
|
||||
let blocks = document.querySelectorAll("pre");
|
||||
blocks.forEach((block) => {
|
||||
if (navigator.clipboard) {
|
||||
let button = document.createElement("span");
|
||||
button.innerText = "copy";
|
||||
button.className = "copy-to-clipboard";
|
||||
block.appendChild(button);
|
||||
button.addEventListener("click", async () => {
|
||||
await copyCode(block, button);
|
||||
});
|
||||
}
|
||||
});
|
||||
async function copyCode(block, button) {
|
||||
let code = block.querySelector("code");
|
||||
let text = code.innerText;
|
||||
await navigator.clipboard.writeText(text);
|
||||
button.innerText = "copied";
|
||||
setTimeout(() => {
|
||||
button.innerText = "copy";
|
||||
}, 700);
|
||||
}
|
||||
|
||||
// table of content
|
||||
let toc = document.querySelector("#TableOfContents a");
|
||||
if (toc) {
|
||||
new ScrollMenu("#TableOfContents a", {
|
||||
duration: 50,
|
||||
activeOffset: 110,
|
||||
scrollOffset: 110,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user