Skip to content
Myr

custom.js

This file

Path

docs/js/custom.js

115 lines · 4.3 KiB · JavaScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 7217bf5 point at it.

⬆ [pre-commit.ci] pre-commit autoupdate (#1176)pre-commit-ci[bot]before adoption7217bf5

lines 1–115why, 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();84 const div = document.createElement("div");85 node.replaceWith(div);86 const termynal = new Termynal(div, {87 lineData: useLines,88 noInit: true,89 lineDelay: 50090 });91 termynals.push(termynal);92 });93 }9495 function loadVisibleTermynals() {96 termynals = termynals.filter(termynal => {97 if (termynal.container.getBoundingClientRect().top - innerHeight <= 0) {98 termynal.init();99 return false;100 }101 return true;102 });103 }104 window.addEventListener("scroll", loadVisibleTermynals);105 createTermynals();106 loadVisibleTermynals();107}108109async function main() {110 setupTermynal()111}112113document$.subscribe(() => {114 main()115})