1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-10-30 03:56:01 -04:00

clean up some client JS

This commit is contained in:
2021-10-31 12:48:50 -04:00
parent b894f96bbe
commit 257dff937c
4 changed files with 18 additions and 17 deletions

View File

@@ -1,7 +1,7 @@
import SimpleAnchor from "simple-anchor"; import SimpleAnchor from "simple-anchor";
const anchors = new SimpleAnchor({ const anchors = new SimpleAnchor({
icon: "", icon: null,
}); });
anchors.add("div#content h2, div#content h3, div#content h4"); anchors.add("div#content h2, div#content h3, div#content h4");

View File

@@ -5,8 +5,8 @@ import trimNewlines from "trim-newlines";
const defaultTerm = "Copy"; const defaultTerm = "Copy";
const successTerm = "Copied!"; const successTerm = "Copied!";
// immediately give up if not supported // immediately give up if not supported or if there are no code blocks
if (ClipboardJS.isSupported()) { if (ClipboardJS.isSupported() && document.querySelector("div.highlight")) {
// loop through each code fence on page (if any) // loop through each code fence on page (if any)
document.querySelectorAll("div.highlight").forEach((highlightDiv) => { document.querySelectorAll("div.highlight").forEach((highlightDiv) => {
const wrapperDiv = document.createElement("div"); const wrapperDiv = document.createElement("div");
@@ -22,10 +22,10 @@ if (ClipboardJS.isSupported()) {
wrapperDiv.insertBefore(button, wrapperDiv.firstChild); wrapperDiv.insertBefore(button, wrapperDiv.firstChild);
}); });
// now that all the buttons are in place, bind them to the copy action
new ClipboardJS("button.copy-button", { new ClipboardJS("button.copy-button", {
// actual code element will (should) have class "language-*", even if plaintext
text: (trigger) => text: (trigger) =>
// eslint-disable-next-line quotes // actual code element will have class "language-*", even if plaintext
trimNewlines(trigger.parentElement.querySelector('code[class^="language-"]').innerText), trimNewlines(trigger.parentElement.querySelector('code[class^="language-"]').innerText),
}).on("success", (e) => { }).on("success", (e) => {
// show a subtle indication of success // show a subtle indication of success

View File

@@ -8,18 +8,18 @@ const HITS_ENDPOINT = "/api/hits/";
// don't continue if there isn't a span#meta-hits element on this page // don't continue if there isn't a span#meta-hits element on this page
const wrapper = document.getElementById("meta-hits"); const wrapper = document.getElementById("meta-hits");
// use <link rel="canonical"> to deduce a consistent identifier for this page // page must have both span#meta-hits and canonical URL to enter
const canonical = canonicalUrl({ if (wrapper) {
// use <link rel="canonical"> to deduce a consistent identifier for this page
const canonical = canonicalUrl({
normalize: true, normalize: true,
normalizeOptions: { normalizeOptions: {
removeTrailingSlash: true, removeTrailingSlash: true,
removeQueryParameters: true, removeQueryParameters: true,
stripHash: true, stripHash: true,
}, },
}); });
// page must have both span#meta-hits and canonical URL to enter
if (wrapper && canonical) {
// javascript is enabled so show the loading indicator // javascript is enabled so show the loading indicator
wrapper.style.display = "inline-flex"; wrapper.style.display = "inline-flex";

View File

@@ -17,7 +17,8 @@ initDarkMode({
// make toggle visible now that we know JS is enabled // make toggle visible now that we know JS is enabled
t.style.display = "block"; t.style.display = "block";
// HACK: re-enable theme transitions after a very short delay, otherwise there's a weird race condition (2/2) // HACK: re-enable theme transitions after a very short delay, otherwise
// there's a weird race condition (2/2)
setTimeout(() => { setTimeout(() => {
document.head.removeChild(disableTransitionCSSHack); document.head.removeChild(disableTransitionCSSHack);
}, 500); }, 500);