custom.js
This file
Path
docs/js/custom.js
91 lines · 3.1 KiB · JavaScript
Last changed by
:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
History of this fileTake it
:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
lines 1–2why, one step back1document.querySelectorAll(".use-termynal").forEach(node => {2 node.style.display = "block";:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
lines 3–5why, one step back3 new Termynal(node, {4 lineDelay: 5005 });:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
lines 6–9why, one step back6});7const progressLiteralStart = "---> 100%";8const promptLiteralStart = "$ ";9const termynalActivateClass = "termy";:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
line 10why, one step back10let termynals = [];:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
line 11why, one step back11:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
line 12why, one step back12function createTermynals() {:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
lines 13–15why, one step back13 document14 .querySelectorAll(`.${termynalActivateClass} .codehilite`)15 .forEach(node => {:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
lines 16–25why, one step back16 const text = node.textContent;17 const lines = text.split("\n");18 const useLines = [];19 let buffer = [];20 function saveBuffer() {21 if (buffer.length) {22 let isBlankSpace = true;23 buffer.forEach(line => {24 if (line) {25 isBlankSpace = false;:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
line 26why, one step back26 }:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
lines 27–30why, one step back27 });28 dataValue = {};29 if (isBlankSpace) {30 dataValue["delay"] = 0;:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
line 31why, one step back31 }:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
lines 32–35why, one step back32 if (buffer.length > 1 && buffer[buffer.length - 1] === "") {33 // The last single <br> won't have effect34 // so put an additional one35 buffer.push("");:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
line 36why, one step back36 }:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
lines 37–40why, one step back37 const bufferValue = buffer.join("<br>");38 dataValue["value"] = bufferValue;39 useLines.push(dataValue);40 buffer = [];:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
lines 41–42why, one step back41 }42 }:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
lines 43–76why, one step back43 for (let line of lines) {44 if (line === progressLiteralStart) {45 saveBuffer();46 useLines.push({47 type: "progress"48 });49 } else if (line.startsWith(promptLiteralStart)) {50 saveBuffer();51 const value = line.replace(promptLiteralStart, "").trimEnd();52 useLines.push({53 type: "input",54 value: value55 });56 } else if (line.startsWith("// ")) {57 saveBuffer();58 const value = line.replace("// ", "").trimEnd();59 useLines.push({60 value: value,61 class: "termynal-comment",62 delay: 063 });64 } else {65 buffer.push(line);66 }67 }68 saveBuffer();69 const div = document.createElement("div");70 node.replaceWith(div);71 const termynal = new Termynal(div, {72 lineData: useLines,73 noInit: true,74 lineDelay: 50075 });76 termynals.push(termynal);:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
lines 77–78why, one step back77 });78}:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
lines 79–88why, one step back7980function loadVisibleTermynals() {81 termynals = termynals.filter(termynal => {82 if (termynal.container.getBoundingClientRect().top - innerHeight <= 0) {83 termynal.init();84 return false;85 }86 return true;87 });88}:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
line 89why, one step back89window.addEventListener("scroll", loadVisibleTermynals);:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
line 90why, one step back90createTermynals();:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
line 91why, one step back91loadVisibleTermynals();