2023-08-27 15:59:52 +02:00
|
|
|
// This is definitely not a three.js ripoff.
|
|
|
|
|
2024-03-03 21:23:37 +01:00
|
|
|
import { addSpell } from "treehouse/spells.js";
|
2024-03-02 20:53:44 +01:00
|
|
|
import * as ulid from "treehouse/ulid.js";
|
2023-08-27 14:50:46 +02:00
|
|
|
|
2023-09-03 11:45:14 +02:00
|
|
|
/* Branch persistence */
|
|
|
|
|
2023-08-22 19:23:31 +02:00
|
|
|
const branchStateKey = "treehouse.openBranches";
|
2023-08-29 14:28:18 +02:00
|
|
|
let branchState = JSON.parse(sessionStorage.getItem(branchStateKey)) || {};
|
2023-08-22 19:23:31 +02:00
|
|
|
|
|
|
|
function saveBranchIsOpen(branchID, state) {
|
|
|
|
branchState[branchID] = state;
|
2023-08-29 14:28:18 +02:00
|
|
|
sessionStorage.setItem(branchStateKey, JSON.stringify(branchState));
|
2023-08-22 19:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function branchIsOpen(branchID) {
|
|
|
|
return branchState[branchID];
|
|
|
|
}
|
|
|
|
|
2024-03-03 21:23:37 +01:00
|
|
|
export class Branch {
|
2024-01-18 22:46:57 +01:00
|
|
|
static branchesByNamedID = new Map();
|
2024-02-21 23:17:19 +01:00
|
|
|
static onAdded = [];
|
2024-01-18 22:46:57 +01:00
|
|
|
|
2024-03-03 21:23:37 +01:00
|
|
|
constructor(element) {
|
|
|
|
this.element = element;
|
2024-02-07 13:09:44 +01:00
|
|
|
|
2024-03-03 21:23:37 +01:00
|
|
|
this.isLeaf = element.classList.contains("leaf");
|
|
|
|
|
|
|
|
this.details = element.childNodes[0];
|
2023-08-20 15:05:59 +02:00
|
|
|
this.innerUL = this.details.childNodes[1];
|
|
|
|
|
2024-02-07 13:09:44 +01:00
|
|
|
if (this.isLeaf) {
|
2024-03-03 21:23:37 +01:00
|
|
|
this.contentContainer = element.childNodes[0];
|
2024-02-07 13:09:44 +01:00
|
|
|
} else {
|
|
|
|
this.contentContainer = this.details.childNodes[0];
|
|
|
|
}
|
|
|
|
this.bulletPoint = this.contentContainer.childNodes[0];
|
|
|
|
this.branchContent = this.contentContainer.childNodes[1];
|
|
|
|
this.buttonBar = this.contentContainer.childNodes[2];
|
|
|
|
|
2024-03-03 21:23:37 +01:00
|
|
|
let doPersist = !element.hasAttribute("data-th-do-not-persist");
|
|
|
|
let isOpen = branchIsOpen(element.id);
|
2023-08-22 22:32:40 +02:00
|
|
|
if (doPersist && isOpen !== undefined) {
|
2023-08-22 19:23:31 +02:00
|
|
|
this.details.open = isOpen;
|
|
|
|
}
|
2024-02-07 13:09:44 +01:00
|
|
|
if (!this.isLeaf) {
|
2024-03-31 18:52:43 +02:00
|
|
|
this.details.addEventListener("toggle", (_) => {
|
2024-03-03 21:23:37 +01:00
|
|
|
saveBranchIsOpen(element.id, this.details.open);
|
2024-02-07 13:09:44 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-31 18:52:43 +02:00
|
|
|
this.namedID = element.id.split(":")[1];
|
2024-03-03 21:23:37 +01:00
|
|
|
Branch.branchesByNamedID.set(this.namedID, element);
|
2024-01-18 22:46:57 +01:00
|
|
|
|
2024-02-21 23:17:19 +01:00
|
|
|
if (ulid.isCanonicalUlid(this.namedID)) {
|
|
|
|
let timestamp = ulid.getTimestamp(this.namedID);
|
2024-02-07 13:09:44 +01:00
|
|
|
let date = document.createElement("span");
|
|
|
|
date.classList.add("branch-date");
|
|
|
|
date.innerText = timestamp.toLocaleDateString();
|
|
|
|
this.buttonBar.insertBefore(date, this.buttonBar.firstChild);
|
|
|
|
}
|
2024-02-21 23:17:19 +01:00
|
|
|
|
|
|
|
for (let callback of Branch.onAdded) {
|
2024-03-03 21:23:37 +01:00
|
|
|
callback(element, this);
|
2024-02-21 23:17:19 +01:00
|
|
|
}
|
2023-08-22 19:23:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-03 21:23:37 +01:00
|
|
|
addSpell("b", Branch);
|
2023-08-22 19:23:31 +02:00
|
|
|
|
2023-09-03 11:45:14 +02:00
|
|
|
/* Linked branches */
|
|
|
|
|
2023-08-22 19:23:31 +02:00
|
|
|
class LinkedBranch extends Branch {
|
2023-08-27 14:50:46 +02:00
|
|
|
static byLink = new Map();
|
|
|
|
|
2024-03-03 21:23:37 +01:00
|
|
|
constructor(element) {
|
|
|
|
super(element);
|
2023-08-22 19:23:31 +02:00
|
|
|
|
2024-03-03 21:23:37 +01:00
|
|
|
this.linkedTree = element.getAttribute("data-th-link");
|
2023-08-27 14:50:46 +02:00
|
|
|
LinkedBranch.byLink.set(this.linkedTree, this);
|
2023-08-22 19:23:31 +02:00
|
|
|
|
2023-08-20 15:05:59 +02:00
|
|
|
this.loadingText = document.createElement("p");
|
|
|
|
{
|
|
|
|
this.loadingText.className = "link-loading";
|
|
|
|
let linkedTreeName = document.createElement("code");
|
|
|
|
linkedTreeName.innerText = this.linkedTree;
|
|
|
|
this.loadingText.append("Loading ", linkedTreeName, "...");
|
|
|
|
}
|
|
|
|
this.innerUL.appendChild(this.loadingText);
|
|
|
|
|
|
|
|
// This produces a warning during static generation but we still want to handle that
|
2023-08-22 19:23:31 +02:00
|
|
|
// correctly, as Branch saves the state in localStorage. Having an expanded-by-default
|
|
|
|
// linked block can be useful in development.
|
2023-08-20 15:05:59 +02:00
|
|
|
if (this.details.open) {
|
2023-08-27 14:50:46 +02:00
|
|
|
this.loadTree("constructor");
|
2023-08-20 15:05:59 +02:00
|
|
|
}
|
|
|
|
|
2024-03-31 18:52:43 +02:00
|
|
|
this.details.addEventListener("toggle", (_) => {
|
2023-08-20 15:05:59 +02:00
|
|
|
if (this.details.open) {
|
2023-08-27 14:50:46 +02:00
|
|
|
this.loadTree("toggle");
|
2023-08-20 15:05:59 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-27 14:50:46 +02:00
|
|
|
async loadTreePromise(_initiator) {
|
|
|
|
try {
|
2024-11-23 21:21:28 +01:00
|
|
|
let response = await fetch(`${TREEHOUSE_SITE}/${this.linkedTree}`);
|
2023-08-27 14:50:46 +02:00
|
|
|
if (response.status == 404) {
|
|
|
|
throw `Hmm, seems like the tree "${this.linkedTree}" does not exist.`;
|
|
|
|
}
|
2023-08-20 15:05:59 +02:00
|
|
|
|
2023-08-27 14:50:46 +02:00
|
|
|
let text = await response.text();
|
|
|
|
let parser = new DOMParser();
|
|
|
|
let linkedDocument = parser.parseFromString(text, "text/html");
|
|
|
|
let main = linkedDocument.getElementsByTagName("main")[0];
|
|
|
|
let ul = main.getElementsByTagName("ul")[0];
|
2024-02-12 19:56:06 +01:00
|
|
|
let styles = main.getElementsByTagName("link");
|
|
|
|
let scripts = main.getElementsByTagName("script");
|
|
|
|
|
2023-08-27 14:50:46 +02:00
|
|
|
this.loadingText.remove();
|
|
|
|
this.innerUL.innerHTML = ul.innerHTML;
|
2024-02-14 23:31:39 +01:00
|
|
|
|
2024-03-03 21:23:37 +01:00
|
|
|
this.element.append(...styles);
|
2024-02-14 23:31:39 +01:00
|
|
|
for (let script of scripts) {
|
|
|
|
// No need to await for the import because we don't use the resulting module.
|
|
|
|
// Just fire and forger 💀
|
|
|
|
// and let them run in parallel.
|
2024-11-23 21:21:28 +01:00
|
|
|
let url = URL.createObjectURL(
|
|
|
|
new Blob([script.textContent], { type: "text/javascript" }),
|
|
|
|
);
|
2024-07-19 20:05:17 +02:00
|
|
|
import(url);
|
2024-02-14 23:31:39 +01:00
|
|
|
}
|
2023-08-27 14:50:46 +02:00
|
|
|
} catch (error) {
|
|
|
|
this.loadingText.innerText = error.toString();
|
2023-08-20 15:05:59 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-27 14:50:46 +02:00
|
|
|
|
|
|
|
loadTree() {
|
|
|
|
if (!this.loading) {
|
|
|
|
this.loading = this.loadTreePromise();
|
|
|
|
}
|
|
|
|
return this.loading;
|
|
|
|
}
|
2023-08-20 15:05:59 +02:00
|
|
|
}
|
|
|
|
|
2024-03-03 21:23:37 +01:00
|
|
|
addSpell("b-linked", LinkedBranch);
|
2023-08-21 21:12:06 +02:00
|
|
|
|
2023-09-03 11:45:14 +02:00
|
|
|
/* Fragment navigation */
|
|
|
|
|
2023-08-21 21:12:06 +02:00
|
|
|
function expandDetailsRecursively(element) {
|
|
|
|
while (element && element.tagName != "MAIN") {
|
|
|
|
if (element.tagName == "DETAILS") {
|
|
|
|
element.open = true;
|
|
|
|
}
|
|
|
|
element = element.parentElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 22:46:57 +01:00
|
|
|
function getCurrentlyHighlightedBranch() {
|
|
|
|
if (window.location.pathname == "/b" && window.location.search.length > 0) {
|
|
|
|
let shortID = window.location.search.substring(1);
|
|
|
|
return Branch.branchesByNamedID.get(shortID).id;
|
|
|
|
} else {
|
|
|
|
return window.location.hash.substring(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function highlightCurrentBranch() {
|
|
|
|
let branch = document.getElementById(getCurrentlyHighlightedBranch());
|
|
|
|
if (branch != null) {
|
2024-11-23 22:49:58 +01:00
|
|
|
branch.scrollIntoView();
|
|
|
|
|
|
|
|
// Expand the linked branch so that the person entering the link doesn't have to click on it.
|
2024-11-23 22:43:32 +01:00
|
|
|
expandDetailsRecursively(branch);
|
2024-11-23 22:49:58 +01:00
|
|
|
if (branch.children.length > 0 && branch.children[0].tagName == "DETAILS") {
|
|
|
|
expandDetailsRecursively(branch.children[0]);
|
|
|
|
}
|
|
|
|
|
2024-01-18 22:46:57 +01:00
|
|
|
branch.classList.add("target");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addEventListener("DOMContentLoaded", highlightCurrentBranch);
|