site.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. let charWidth = 0;
  2. let h1TotalChars = 0;
  3. // Remove the prefix '#' on the hash
  4. let skipTyping = window.location.hash.slice(1);
  5. function calculateCharWidth() {
  6. const span = document.createElement('span');
  7. span.style.visibility = 'hidden';
  8. span.style.position = 'absolute';
  9. span.style.fontFamily = 'Fira Mono, monospace';
  10. span.style.fontSize = '16px';
  11. span.textContent = '─';
  12. document.body.appendChild(span);
  13. charWidth = span.getBoundingClientRect().width || 1;
  14. document.body.removeChild(span);
  15. const containerWidth = document.getElementById('terminal-root').offsetWidth || 1;
  16. h1TotalChars = Math.floor(containerWidth / charWidth) - 3;
  17. }
  18. async function typeText(element, text) {
  19. if (skipTyping) {
  20. element.nodeValue = text;
  21. return;
  22. }
  23. element.nodeValue = '';
  24. element.parentNode.classList.add('caret');
  25. for (let i = 0; i < text.length; i++) {
  26. element.nodeValue += text[i];
  27. if (i % 5 == 0) {
  28. await new Promise(r => setTimeout(r, 1));
  29. }
  30. }
  31. element.parentNode.classList.remove('caret');
  32. }
  33. function generateSuffix(element, prefixText, suffixChar='─', endChar='┐') {
  34. const totalTextLength = prefixText.length + element.textContent.length;
  35. const remainingChars = Math.max(0, h1TotalChars - totalTextLength - 1);
  36. return suffixChar.repeat(remainingChars) + endChar;
  37. }
  38. async function typeHeader(clone, sourceNode, prefix, suffix, ghostId = null) {
  39. // prefix
  40. const prefixSpan = document.createElement('span');
  41. prefixSpan.classList.add('header-wrapper', 'shown');
  42. const prefixNode = document.createTextNode('');
  43. prefixSpan.appendChild(prefixNode);
  44. clone.appendChild(prefixSpan);
  45. await typeText(prefixNode, prefix);
  46. // optional anchor link
  47. if (ghostId) {
  48. const link = document.createElement('a');
  49. link.href = `#${ghostId}`;
  50. link.classList.add('header-link', 'shown');
  51. link.textContent = '🔗';
  52. clone.appendChild(link);
  53. }
  54. // header content (recursive, preserves <a>, <span>, etc)
  55. clone.classList.add('shown');
  56. await typeClone(sourceNode, clone);
  57. // suffix
  58. const suffixSpan = document.createElement('span');
  59. suffixSpan.classList.add('header-wrapper', 'shown');
  60. const suffixNode = document.createTextNode('');
  61. suffixSpan.appendChild(suffixNode);
  62. clone.appendChild(suffixSpan);
  63. await typeText(suffixNode, suffix);
  64. }
  65. async function typeClone(sourceNode, targetNode) {
  66. if (sourceNode.nodeType === 3) {
  67. await typeText(targetNode, sourceNode.nodeValue);
  68. return;
  69. }
  70. for (let i = 0; i < sourceNode.childNodes.length; i++) {
  71. const child = sourceNode.childNodes[i];
  72. let clone = (child.nodeType === 3) ? document.createTextNode('') : child.cloneNode(false);
  73. if(clone.classList) clone.classList.add('shown');
  74. targetNode.appendChild(clone);
  75. const id = child.id || null;
  76. if (id == skipTyping) {
  77. skipTyping = null;
  78. }
  79. switch (child.tagName) {
  80. case 'H1': {
  81. const suffix = ' )' + generateSuffix(child, '┌─( ');
  82. await typeHeader(clone, child, '┌─( ', suffix, id);
  83. break;
  84. }
  85. case 'H2':
  86. await typeHeader(clone, child, '└─( ', ' )──>', id);
  87. break;
  88. case 'H3':
  89. await typeHeader(clone, child, '<< ', ' >>', id);
  90. break;
  91. case 'H4':
  92. await typeHeader(clone, child, '{ ', ' }', id);
  93. break;
  94. default:
  95. await typeClone(child, clone);
  96. }
  97. }
  98. }
  99. window.addEventListener('DOMContentLoaded', () => {
  100. calculateCharWidth();
  101. const sourceRoot = document.getElementById('terminal-root');
  102. const visualRoot = document.createElement(sourceRoot.tagName);
  103. visualRoot.setAttribute('aria-hidden','true');
  104. document.getElementById('visual-container').appendChild(visualRoot);
  105. typeClone(sourceRoot, visualRoot);
  106. });