Skip to content

Instantly share code, notes, and snippets.

@mislav
Created March 8, 2021 18:06
Show Gist options
  • Save mislav/f63b3a8dcdb83c6b80803f8aef748217 to your computer and use it in GitHub Desktop.
Save mislav/f63b3a8dcdb83c6b80803f8aef748217 to your computer and use it in GitHub Desktop.
var keystrokeDelay = 60 // ms between keystrokes
var outputDelay = 300 // ms before command output is shown
var cycleDelay = 5000 // ms between going to next command
var cycleTimeout = null // timeout
var numCommands = document.querySelectorAll('.command-header .command').length
function showCommand(index, cycle = true) {
if (index > numCommands) index = 1
// clear all timeouts
clearTimeout(cycleTimeout)
// hide previous commands
Array.from(document.querySelectorAll('.command')).forEach(function (el) {
el.classList.add('d-none')
})
// remove hidden attribute from terminal
Array.from(document.querySelectorAll('.cli-window div[role=tabpanel]')).forEach(function (el) {
el.removeAttribute('hidden')
})
// show current command while respecting animated ones
Array.from(document.querySelectorAll('.command-' + index)).forEach(function (el) {
if (el.classList.contains('type-animate-done')) return
if (el.classList.contains('type-animate')) {
typeAnimate(el, function () {
var doneEl = el.nextElementSibling
if (doneEl && doneEl.classList.contains('type-animate-done')) {
setTimeout(function () {
doneEl.classList.remove('d-none')
}, outputDelay)
}
})
}
el.classList.remove('d-none')
})
// force "layout"
document.querySelector('.command-header').clientLeft
if(cycle) {
cycleTimeout = setTimeout(function () {
showCommand(index + 1);
}, cycleDelay);
}
}
function typeAnimate(el, callback) {
var chars = el.textContent.split('')
el.textContent = ''
var typeIndex = 1
var interval = setInterval(function () {
el.textContent = chars.slice(0, typeIndex++).join('')
if (typeIndex > chars.length) {
clearInterval(interval)
interval = null
callback()
}
}, keystrokeDelay)
}
// Toggle Terminal
var tabsTerminal = document.querySelectorAll(".btn-mktg-fluid");
tabsTerminal.forEach(function(btn) {
btn.addEventListener('click', function() {
showCommand(this.dataset.tab, false)
})
})
showCommand(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment