typing-flow
Version:
Npm package for typing animation for web
2 lines (1 loc) • 2.1 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./index-o6_sHwN7.cjs"),E=require("./browser--HHLw8_C.cjs"),T=({selector:o,attributes:u})=>{const d=document.querySelector(o);if(!d)throw s.TypingFlowError.ContainerNotFoundException(o);return t=>{const e=E.typingFlowDOMParser.parseFromString(t.content.join(""),"text/html");t.content=e.body.textContent.split("");for(const n of u)d.setAttribute(n,t.content.join(""))}};function h({selector:o,baseNodeClasses:u=["typing-node"],nodeWithCursorClasses:d=["typing-node_with-cursor"]}){const t=document.querySelector(o);if(!t)throw s.TypingFlowError.ContainerNotFoundException(o);return e=>{t.innerHTML="",e.cursorPosition===s.MIN_POSSIBLE_CURSOR_POSITION&&(e={content:[s.ZERO_WIDTH_SPACE,...e.content],cursorPosition:0});for(let n=0;n<e.content.length;n++){const i=document.createElement("span"),c=e.content[n];i.innerHTML=c,i.style.whiteSpace="pre",e.cursorPosition===n?i.classList.add(...d):i.classList.add(...u),t.appendChild(i)}}}function C({selector:o,baseNodeClasses:u=["typing-node"],nodeWithCursorClasses:d=["typing-node_with-cursor"]}){const t=document.querySelector(o);if(!t)throw s.TypingFlowError.ContainerNotFoundException(o);return e=>{let n=0;t.innerHTML="";const i=r=>{if(r.nodeType===Node.TEXT_NODE){const f=r.textContent,m=[];for(let a=0;a<f.length;a++){const p=f[a],l=document.createElement("span");l.innerHTML=p,l.style.whiteSpace="pre",l.setAttribute("data-tf-node",""),e.cursorPosition===n?l.classList.add(...d):l.classList.add(...u),n+=p.length,m.push(l)}for(const a of m)r.parentNode.insertBefore(a,r);r.parentNode.removeChild(r)}if(r.nodeType===Node.ELEMENT_NODE){if(r.hasAttribute("data-tf-node"))return;for(const f of r.childNodes)i(f)}},c=E.typingFlowDOMParser.parseFromString(e.content.join(""),"text/html").cloneNode(!0).body;e.cursorPosition===s.MIN_POSSIBLE_CURSOR_POSITION&&(c.innerHTML=s.ZERO_WIDTH_SPACE+c.innerHTML,e.cursorPosition=0),i(c),c.style.display="contents",t.appendChild(c)}}exports.attributeRenderer=T;exports.htmlRenderer=C;exports.simpleBrowserRenderer=h;