Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 7afa636 point at it.

🔖 Release version 0.19.1Sebastián Ramírezbefore adoption7afa636

lines 1–83why, one step back
1function 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 back
84 const inputCommands = useLines.filter(line => line.type === "input").map(line => line.value).join("\n");
85 node.textContent = inputCommands;

🔖 Release version 0.19.1Sebastián Ramírezbefore adoption7afa636

line 86why, one step back
86 const div = document.createElement("div");

🐛 Fix copy button in `custom.js` (#1488)Alejandrabefore adoption30b0743

lines 87–88why, one step back
87 node.style.display = "none";
88 node.after(div);

🔖 Release version 0.19.1Sebastián Ramírezbefore adoption7afa636

lines 89–118why, one step back
89 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})