2020-09-09 10:28:38 -04:00
|
|
|
/*!
|
|
|
|
* Hugo Theme Stack
|
|
|
|
*
|
|
|
|
* @author: Jimmy Cai
|
|
|
|
* @website: https://jimmycai.com
|
|
|
|
* @link: https://github.com/CaiJimmy/hugo-theme-stack
|
|
|
|
*/
|
2020-12-22 13:35:37 -05:00
|
|
|
import StackGallery from "ts/gallery";
|
2020-12-21 16:34:54 -05:00
|
|
|
import { getColor } from 'ts/color';
|
|
|
|
import menu from 'ts/menu';
|
|
|
|
import createElement from 'ts/createElement';
|
2020-12-23 13:03:40 -05:00
|
|
|
import StackColorScheme from 'ts/colorScheme';
|
2022-01-22 04:35:08 -05:00
|
|
|
import { setupScrollspy } from 'ts/scrollspy';
|
|
|
|
import { setupSmoothAnchors } from "ts/smoothAnchors";
|
2020-08-22 07:20:08 -04:00
|
|
|
|
|
|
|
let Stack = {
|
|
|
|
init: () => {
|
|
|
|
/**
|
|
|
|
* Bind menu event
|
|
|
|
*/
|
|
|
|
menu();
|
|
|
|
|
2020-12-22 13:35:37 -05:00
|
|
|
const articleContent = document.querySelector('.article-content') as HTMLElement;
|
|
|
|
if (articleContent) {
|
|
|
|
new StackGallery(articleContent);
|
2022-01-22 04:35:08 -05:00
|
|
|
setupSmoothAnchors();
|
|
|
|
setupScrollspy();
|
2020-08-22 07:20:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add linear gradient background to tile style article
|
|
|
|
*/
|
|
|
|
const articleTile = document.querySelector('.article-list--tile');
|
|
|
|
if (articleTile) {
|
|
|
|
let observer = new IntersectionObserver(async (entries, observer) => {
|
|
|
|
entries.forEach(entry => {
|
|
|
|
if (!entry.isIntersecting) return;
|
|
|
|
observer.unobserve(entry.target);
|
|
|
|
|
|
|
|
const articles = entry.target.querySelectorAll('article.has-image');
|
|
|
|
articles.forEach(async articles => {
|
|
|
|
const image = articles.querySelector('img'),
|
|
|
|
imageURL = image.src,
|
2020-08-28 06:11:02 -04:00
|
|
|
key = image.getAttribute('data-key'),
|
2020-08-29 05:43:00 -04:00
|
|
|
hash = image.getAttribute('data-hash'),
|
2020-08-22 07:20:08 -04:00
|
|
|
articleDetails: HTMLDivElement = articles.querySelector('.article-details');
|
|
|
|
|
2020-08-29 05:43:00 -04:00
|
|
|
const colors = await getColor(key, hash, imageURL);
|
2020-08-22 07:20:08 -04:00
|
|
|
|
|
|
|
articleDetails.style.background = `
|
|
|
|
linear-gradient(0deg,
|
|
|
|
rgba(${colors.DarkMuted.rgb[0]}, ${colors.DarkMuted.rgb[1]}, ${colors.DarkMuted.rgb[2]}, 0.5) 0%,
|
|
|
|
rgba(${colors.Vibrant.rgb[0]}, ${colors.Vibrant.rgb[1]}, ${colors.Vibrant.rgb[2]}, 0.75) 100%)`;
|
|
|
|
})
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
observer.observe(articleTile)
|
|
|
|
}
|
2020-12-23 13:03:40 -05:00
|
|
|
|
2021-08-08 12:15:27 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add copy button to code block
|
|
|
|
*/
|
2022-02-06 14:32:37 -05:00
|
|
|
const highlights = document.querySelectorAll('.article-content div.highlight');
|
2021-08-08 12:15:27 -04:00
|
|
|
const copyText = `Copy`,
|
|
|
|
copiedText = `Copied!`;
|
2022-02-06 14:32:37 -05:00
|
|
|
|
|
|
|
highlights.forEach(highlight => {
|
2021-08-08 12:15:27 -04:00
|
|
|
const copyButton = document.createElement('button');
|
|
|
|
copyButton.innerHTML = copyText;
|
|
|
|
copyButton.classList.add('copyCodeButton');
|
2022-02-06 14:32:37 -05:00
|
|
|
highlight.appendChild(copyButton);
|
2021-08-08 12:15:27 -04:00
|
|
|
|
2022-02-06 14:32:37 -05:00
|
|
|
const codeBlock = highlight.querySelector('code[data-lang]');
|
|
|
|
if (!codeBlock) return;
|
2021-08-08 12:15:27 -04:00
|
|
|
|
|
|
|
copyButton.addEventListener('click', () => {
|
2022-02-06 14:32:37 -05:00
|
|
|
navigator.clipboard.writeText(codeBlock.textContent)
|
2021-08-08 12:15:27 -04:00
|
|
|
.then(() => {
|
|
|
|
copyButton.textContent = copiedText;
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
copyButton.textContent = copyText;
|
|
|
|
}, 1000);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
alert(err)
|
|
|
|
console.log('Something went wrong', err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-12-23 13:03:40 -05:00
|
|
|
new StackColorScheme(document.getElementById('dark-mode-toggle'));
|
2020-08-22 07:20:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('load', () => {
|
|
|
|
setTimeout(function () {
|
|
|
|
Stack.init();
|
|
|
|
}, 0);
|
|
|
|
})
|
|
|
|
|
2020-11-06 05:12:48 -05:00
|
|
|
declare global {
|
|
|
|
interface Window {
|
|
|
|
createElement: any;
|
|
|
|
Stack: any
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.Stack = Stack;
|
|
|
|
window.createElement = createElement;
|