custom.js
This file
Path
docs/js/custom.js
118 lines · 4.5 KiB · JavaScript
Last changed by
🐛 Fix copy button in `custom.js` (#1488)Alejandrabefore adoption30b0743
History of this fileTake it
The walk stopped after 200 commits: lines older than 53e9b3c point at it.
🔧 Add PEP-639 license metadata (#1387)Sofie Van Landeghembefore adoption53e9b3c
lines 1–83why, one step back1function setupTermynal() {2 document.querySelectorAll(".use-termynal").forEach(node => {3 node.style.display = "block";4 new Termynal(node, {5 lineDelay: 5006 });7 });8 const progressLiteralStart = "---> 100%";9 const promptLiteralStart = "$ ";10 const customPromptLiteralStart = "# ";11 const termynalActivateClass = "termy";12 let termynals = [];1314 function createTermynals() {15 document16 .querySelectorAll(`.${termynalActivateClass} .highlight code`)17 .forEach(node => {18 const text = node.textContent;19 const lines = text.split("\n");20 const useLines = [];21 let buffer = [];22 function saveBuffer() {23 if (buffer.length) {24 let isBlankSpace = true;25 buffer.forEach(line => {26 if (line) {27 isBlankSpace = false;28 }29 });30 dataValue = {};31 if (isBlankSpace) {32 dataValue["delay"] = 0;33 }34 if (buffer[buffer.length - 1] === "") {35 // A last single <br> won't have effect36 // so put an additional one37 buffer.push("");38 }39 const bufferValue = buffer.join("<br>");40 dataValue["value"] = bufferValue;41 useLines.push(dataValue);42 buffer = [];43 }44 }45 for (let line of lines) {46 if (line === progressLiteralStart) {47 saveBuffer();48 useLines.push({49 type: "progress"50 });51 } else if (line.startsWith(promptLiteralStart)) {52 saveBuffer();53 const value = line.replace(promptLiteralStart, "").trimEnd();54 useLines.push({55 type: "input",56 value: value57 });58 } else if (line.startsWith("// ")) {59 saveBuffer();60 const value = "💬 " + line.replace("// ", "").trimEnd();61 useLines.push({62 value: value,63 class: "termynal-comment",64 delay: 065 });66 } else if (line.startsWith(customPromptLiteralStart)) {67 saveBuffer();68 const promptStart = line.indexOf(promptLiteralStart);69 if (promptStart === -1) {70 console.error("Custom prompt found but no end delimiter", line)71 }72 const prompt = line.slice(0, promptStart).replace(customPromptLiteralStart, "")73 let value = line.slice(promptStart + promptLiteralStart.length);74 useLines.push({75 type: "input",76 value: value,77 prompt: prompt78 });79 } else {80 buffer.push(line);81 }82 }83 saveBuffer();🐛 Fix copy button in `custom.js` (#1488)Alejandrabefore adoption30b0743
lines 84–85why, one step back84 const inputCommands = useLines.filter(line => line.type === "input").map(line => line.value).join("\n");85 node.textContent = inputCommands;🔧 Add PEP-639 license metadata (#1387)Sofie Van Landeghembefore adoption53e9b3c
line 86why, one step back86 const div = document.createElement("div");🐛 Fix copy button in `custom.js` (#1488)Alejandrabefore adoption30b0743
lines 87–88why, one step back87 node.style.display = "none";88 node.after(div);🔧 Add PEP-639 license metadata (#1387)Sofie Van Landeghembefore adoption53e9b3c
lines 89–118why, one step back89 const termynal = new Termynal(div, {90 lineData: useLines,91 noInit: true,92 lineDelay: 50093 });94 termynals.push(termynal);95 });96 }9798 function loadVisibleTermynals() {99 termynals = termynals.filter(termynal => {100 if (termynal.container.getBoundingClientRect().top - innerHeight <= 0) {101 termynal.init();102 return false;103 }104 return true;105 });106 }107 window.addEventListener("scroll", loadVisibleTermynals);108 createTermynals();109 loadVisibleTermynals();110}111112async function main() {113 setupTermynal()114}115116document$.subscribe(() => {117 main()118})