Skip to content

Commit

Permalink
Tidy up website code
Browse files Browse the repository at this point in the history
  • Loading branch information
LeaVerou committed Apr 22, 2024
1 parent d082a4f commit 89dd6bd
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 73 deletions.
3 changes: 0 additions & 3 deletions _includes/plain.njk
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@
</nav>
</footer>

<script src="{{ page | relative }}/assets/js/prism.js"></script>
<script src="https://blissfuljs.com/bliss.shy.js"></script>
<script src="https://live.prismjs.com/src/prism-live.mjs?load=javascript" async type=module></script>
<script src="{{ page | relative }}/assets/js/index.js" type="module"></script>

</body>
Expand Down
47 changes: 47 additions & 0 deletions assets/js/colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
let root = document.documentElement;
let root_cs = getComputedStyle(root);
let colors = {
red: new Color(root_cs.getPropertyValue("--color-red")),
green: new Color(root_cs.getPropertyValue("--color-green")),
blue: new Color(root_cs.getPropertyValue("--color-blue")),
};
let supportsP3 = window.CSS && CSS.supports("color", "color(display-p3 0 1 0)");
let interpolationOptions = {steps: 5, space: "lch", outputSpace: supportsP3 ? "p3" : "hsl"};

if (!Color.DEBUGGING) {
let redGreen = colors.red.range(colors.green, interpolationOptions);
let greenBlue = colors.green.range(colors.blue, interpolationOptions);
let blueRed = colors.blue.range(colors.red, interpolationOptions);

let vars = {
"gradient-steps": [
...Color.steps(redGreen, interpolationOptions),
...Color.steps(greenBlue, interpolationOptions),
...Color.steps(blueRed, interpolationOptions),
],
"color-red-light": colors.red.clone().set({"lch.l": 80}),
"color-green-light": colors.green.clone().set({"lch.l": 80}),
"color-blue-light": colors.blue.clone().set({"lch.l": 80}),

"color-red-lighter": colors.red.clone().set({"lch.l": 94}),
"color-green-lighter": colors.green.clone().set({"lch.l": 95}),
"color-blue-lighter": colors.blue.clone().set({"lch.l": 94}),

"color-red-green": redGreen(.5),
"color-green-blue": greenBlue(.5),
"color-blue-red": blueRed(.5),

"color-red-green-light": redGreen(.5).set({"lch.l": 94}),
"color-green-blue-light": greenBlue(.5).set({"lch.l": 94}),
"color-blue-red-light": blueRed(.5).set({"lch.l": 94}),
};

window.vars = vars;

document.head.insertAdjacentHTML("beforeend",
`<style>
:root {
${Object.entries(vars).map(pair => `--${pair[0]}: ${pair[1]}`).join(";")};
}
</style>`);
}

Check warning on line 47 in assets/js/colors.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Newline required at end of file but not found
31 changes: 31 additions & 0 deletions assets/js/enhance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export function styleCallouts (
elements = document.querySelectorAll("p"),
labels = ["Tip", "Warning", "Note"]

Check warning on line 3 in assets/js/enhance.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Missing trailing comma
) {
let maxLabelLength = Math.max(...labels.map(l => l.length));
labels = new RegExp(`(${labels.join("|")}):`);

for (let p of elements) {
let callout = p.textContent.trimLeft().slice(0, maxLabelLength + 2).match(labels)?.[1];

if (callout) {
p.classList.add(callout.toLowerCase());
p.firstChild.textContent = p.firstChild.textContent.replace(callout + ":", "");
}
}
}

// Do not use, this is broken
// export function linkifyAPI () {
// for (let code of $$(":not(pre) > code")) {
// let text = code.textContent;
// let match = text.match(/([Cc]olor).(\w+)\(\)/);

// if (match) {
// $.create("a", {
// href: `/api/#Color${match[1] === "Color" ? "." : "#"}${match[2]}`,
// around: code,
// });
// }
// }
// }

Check warning on line 31 in assets/js/enhance.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Newline required at end of file but not found
78 changes: 8 additions & 70 deletions assets/js/index.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,21 @@
let $ = Bliss;
let $$ = $.$;

import {} from "../../notebook/color-notebook.js";
import "./prism.js";
import "https://blissfuljs.com/bliss.shy.js";
import "https://live.prismjs.com/src/prism-live.mjs?load=javascript";
import "../../notebook/color-notebook.js";
import "./colors.js";
import { styleCallouts } from "./enhance.js";

if (location.pathname.indexOf("/docs/") > -1 && window.toc) {
import("./docs.js");
}

let root = document.documentElement;
let root_cs = getComputedStyle(root);
let colors = {
red: new Color(root_cs.getPropertyValue("--color-red")),
green: new Color(root_cs.getPropertyValue("--color-green")),
blue: new Color(root_cs.getPropertyValue("--color-blue")),
};

let supportsP3 = window.CSS && CSS.supports("color", "color(display-p3 0 1 0)");
let interpolationOptions = {steps: 5, space: "lch", outputSpace: supportsP3 ? "p3" : "hsl"};

if (!Color.DEBUGGING) {
let redGreen = colors.red.range(colors.green, interpolationOptions);
let greenBlue = colors.green.range(colors.blue, interpolationOptions);
let blueRed = colors.blue.range(colors.red, interpolationOptions);

let vars = {
"gradient-steps": [
...Color.steps(redGreen, interpolationOptions),
...Color.steps(greenBlue, interpolationOptions),
...Color.steps(blueRed, interpolationOptions),
],
"color-red-light": colors.red.clone().set({"lch.l": 80}),
"color-green-light": colors.green.clone().set({"lch.l": 80}),
"color-blue-light": colors.blue.clone().set({"lch.l": 80}),

"color-red-lighter": colors.red.clone().set({"lch.l": 94}),
"color-green-lighter": colors.green.clone().set({"lch.l": 95}),
"color-blue-lighter": colors.blue.clone().set({"lch.l": 94}),

"color-red-green": redGreen(.5),
"color-green-blue": greenBlue(.5),
"color-blue-red": blueRed(.5),

"color-red-green-light": redGreen(.5).set({"lch.l": 94}),
"color-green-blue-light": greenBlue(.5).set({"lch.l": 94}),
"color-blue-red-light": blueRed(.5).set({"lch.l": 94}),
};
window.vars = vars;
$.create("style", {
inside: document.head,
textContent: `:root {
${Object.entries(vars).map(pair => `--${pair[0]}: ${pair[1]}`).join(";\n")};
--scrolltop: ${root.scrollTop};
}`,
});
}
styleCallouts();

document.addEventListener("scroll", evt => {
root.style.setProperty("--scrolltop", root.scrollTop);
}, {passive: true});

// Style callouts
for (let p of $$("p")) {
let callout = p.textContent.trimLeft().slice(0, 10).match(/(Tip|Warning|Note):/)?.[1];

if (callout) {
p.classList.add(callout.toLowerCase());
p.firstChild.textContent = p.firstChild.textContent.replace(callout + ":", "");
}
}

// Linkify API calls
for (let code of $$(":not(pre) > code")) {
let text = code.textContent;
let match = text.match(/([Cc]olor).(\w+)\(\)/);

if (match) {
$.create("a", {
href: `/api/#Color${match[1] === "Color" ? "." : "#"}${match[2]}`,
around: code,
});
}
}
}, {passive: true});

Check warning on line 21 in assets/js/index.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Newline required at end of file but not found

0 comments on commit 89dd6bd

Please sign in to comment.