UNPKG

glitched-writer

Version:

Glitched, text writing module. Highly customizable settings. Decoding, decrypting, scrambling, or simply spelling out text.

2 lines 22.7 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.GlitchedWriter=e():t.GlitchedWriter=e()}(self,(function(){return(()=>{"use strict";var t={d:(e,s)=>{for(var i in s)t.o(s,i)&&!t.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:s[i]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};function s(t,e,s){const i=Math.random()*(e-t)+t;if(s)switch(s){case"floor":return Math.floor(i);case"round":return Math.round(i);case"ceil":return Math.ceil(i)}return i}t.r(e),t.d(e,{create:()=>_,default:()=>q,glyphs:()=>f,presets:()=>m,queueWrite:()=>F,wait:()=>n,write:()=>I});const i=t=>t.splice(s(0,t.length,"floor"),1).length>0,n=t=>new Promise((e=>setTimeout((()=>e(t)),t)));function r(t,e){const s=()=>t()?e().then(s):Promise.resolve();return s()}const h=(t,e)=>new Array(e).fill(t),o=(t,e)=>{t.classList.remove(e),t.offsetWidth,t.classList.add(e)};function a(t,e=!0){return"number"==typeof t?t:s(...t,e?"round":void 0)}const l=(t=.5)=>Math.random()<t,c=t=>({value:t}),p=t=>[...t].map(c),u="(&(?:[a-z0-9]+|#[0-9]{1,6}|#x[0-9a-fA-F]{1,6});)|(<style.+?>.+?</style>|<script.+?>.+?<\/script>|<(?:!|/?[a-zA-Z]+).*?/?>)",g=/(&(?:[a-z0-9]+|#[0-9]{1,6}|#x[0-9a-fA-F]{1,6});)|.\w+,*\.*"*\s?|\s+|\S+/gi;function d(t){const e=new RegExp(u,"g");return t.replace(e,"")}const f={nier:"一二三四五六七八九十百千上下左右中大小月日年早木林山川土空田天生花草虫犬人名女男子目耳口手足見音力気円入出立休先夕本文字学校村町森正水火玉王石竹糸貝車金雨赤青白数多少万半形太細広長点丸交光角計直線矢弱強高同親母父姉兄弟妹自友体毛頭顔首心時曜朝昼夜分週春夏秋冬今新古間方北南東西遠近前後内外場地国園谷野原里市京風雪雲池海岩星室戸家寺通門道話言答声聞語読書記紙画絵図工教晴思考知才理算作元食肉馬牛魚鳥羽鳴麦米茶色黄黒来行帰歩走止活店買売午汽弓回会組船明社切電毎合当台楽公引科歌刀番用何",full:"ABCDĐEFGHIJKLMNOPQRSTUVWXYZabcdđefghijklmnopqrstuvwxyzĄąĆćŻżŹźŃńóŁłАБВГҐДЂЕЁЄЖЗЅИІЇЙЈКЛЉМНЊОПРСТЋУЎФХЦЧЏШЩЪЫЬЭЮЯабвгґдђеёєжзѕиіїйјклљмнњопрстћуўфхцчџшщъыьэюяΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρστυφχψωάΆέΈέΉίϊΐΊόΌύΰϋΎΫΏĂÂÊÔƠƯăâêôơư一二三四五六七八九十百千上下左右中大小月日年早木林山川土空田天生花草虫犬人名女男子目耳口手足見音力気円入出立休先夕本文字学校村町森正水火玉王石竹糸貝車金雨赤青白数多少万半形太細広長点丸交光角計直線矢弱強高同親母父姉兄弟妹自友体毛頭顔首心時曜朝昼夜分週春夏秋冬今新古間方北南東西遠近前後内外場地国園谷野原里市京風雪雲池海岩星室戸家寺通門道話言答声聞語読書記紙画絵図工教晴思考知才理算作元食肉馬牛魚鳥羽鳴麦米茶色黄黒来行帰歩走止活店買売午汽弓回会組船明社切電毎合当台楽公引科歌刀番用何ĂÂÊÔƠƯăâêôơư1234567890‘?’“!”(%)[#]{@}/\\&<-+÷×=>$€£¥¢:;,.*•°·…±†‡æ«»¦¯—–~˜¨_øÞ¿▬▭▮▯┐└╛░▒▓○‼⁇⁈⁉‽ℴℵℶℷℸℲ℮ℯ⅁⅂⅃⅄₠₡₢₣₤₥₦₧₨₩₪₫€₭₮₯₰₱₲₳₴₵₶₷₸₹₺₻₼₽₾₿ ",letterlike:"ABCDĐEFGHIJKLMNOPQRSTUVWXYZabcdđefghijklmnopqrstuvwxyzĄąĆćŻżŹźŃńóŁłАБВГҐДЂЕЁЄЖЗЅИІЇЙЈКЛЉМНЊОПРСТЋУЎФХЦЧЏШЩЪЫЬЭЮЯабвгґдђеёєжзѕиіїйјклљмнњопрстћуўфхцчџшщъыьэюяΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρστυφχψωάΆέΈέΉίϊΐΊόΌύΰϋΎΫΏĂÂÊÔƠƯăâêôơưĂÂÊÔƠƯăâêôơư1234567890",numbers:"0123456789",zalgo:"̴̵̶̷̸̡̢̧̨̛̖̗̘̙̜̝̞̟̠̣̤̥̦̩̪̫̬̭̮̯̰̱̲̳̹̺̻̼͇͈͉͍͎̀́̂̃̄̅̆̇̈̉̊̋̌̍̎̏̐̑̒̓̔̽̾̿̀́͂̓̈́͆͊͋͌̕̚ͅ ͓͔͕͖͙͚͐͑͒͗͛ͣͤͥͦͧͨͩͪͫͬͭͮͯ͘͜͟͢͝͞͠͡͏҉",neo:"!<>-_\\/[]{}—=+*^?#________",uppercase:"1234567890QWERTYUIOPASDFGHJKLZXCVBNM#$%"},m={default:{steps:[1,8],interval:[60,170],delay:[0,2e3],changeChance:.6,ghostChance:.2,maxGhosts:.2,oneAtATime:0,glyphs:f.full+f.zalgo,glyphsFromText:!1,fillSpace:!0,mode:"matching",html:!1,letterize:!1,endless:!1,fps:60},nier:{maxGhosts:0,ghostChance:0,changeChance:.8,steps:3,interval:10,delay:0,oneAtATime:1,glyphs:f.nier,fillSpace:!1,glyphsFromText:!0,mode:"clear"},typewriter:{interval:[50,150],delay:0,steps:0,changeChance:0,maxGhosts:0,oneAtATime:1,glyphs:"",glyphsFromText:!1,fillSpace:!1,mode:"erase_smart"},terminal:{interval:[25,30],delay:[0,0],steps:0,changeChance:.5,maxGhosts:0,oneAtATime:1,glyphs:"",fillSpace:!1,glyphsFromText:!1,mode:"clear"},zalgo:{delay:[0,3e3],interval:[10,35],steps:[0,30],maxGhosts:4.6,changeChance:.5,ghostChance:.7,glyphs:f.zalgo,glyphsFromText:!0,fillSpace:!1},neo:{interval:[30,100],delay:[0,1300],steps:[4,7],maxGhosts:0,ghostChance:0,changeChance:1,glyphs:f.neo,mode:"normal"},encrypted:{interval:[50,90],delay:[0,1300],steps:[5,8],maxGhosts:0,ghostChance:0,changeChance:1,glyphs:f.uppercase,fillSpace:!1,mode:"normal"},bitbybit:{interval:[35,65],delay:180,steps:1,maxGhosts:1,ghostChance:.1,changeChance:.7,oneAtATime:"word",glyphs:"",glyphsFromText:!0,fillSpace:!1,mode:"erase"},cosmic:{steps:[0,1],interval:30,delay:[400,2400],ghostChance:0,changeChance:.3,maxGhosts:0,glyphs:"QWERTYUIOPASDFGHJKLZXCVBNM",glyphsFromText:!1,fillSpace:!0,mode:"erase"}};class y{constructor(t,e){this.writer=t,this.set(e)}set(t){this.options=Object.assign(Object.assign({},m.default),this.parseOptions(t)),this.updateInternal()}extend(t){this.options=Object.assign(Object.assign({},this.options),this.parseOptions(t)),this.updateInternal()}parseOptions(t){var e;return t?"string"==typeof t?null!==(e=m[t])&&void 0!==e?e:{}:t:{}}updateInternal(){const{options:t}=this;this.glyphs=function(t){let e;return e="string"==typeof t?t:t.length?t.join(""):Array.from(t).join(""),e}(t.glyphs),this.setCharset(),this.space=t.fillSpace?" ":"",Number.isInteger(t.oneAtATime)?this.oneAtATime=t.oneAtATime:"word"===t.oneAtATime?this.oneAtATime="word":this.oneAtATime=t.oneAtATime?1:0}setCharset(){const{writer:t}=this;let{glyphs:e}=this;this.glyphsFromText&&(e+=function(t){const e="string"==typeof t,s=[];return new Set(t).forEach((t=>s.push(t))),e?s.join(""):s}(t.previousString+(this.html?d(t.goalText):t.goalText))),this.charset=[...e].filter((t=>!["\t","\n","\r","\f","\v"].includes(t))),this.setMaxGhosts()}setMaxGhosts(){const{writer:{charTable:t},options:{maxGhosts:e}}=this;Number.isInteger(e)&&(this.maxGhosts=e);const{length:s}=t.filter((t=>"tag"!==t.specialType));this.maxGhosts=Math.round((s||20)*e)}getGlyph(t){const{options:e}=this;return e.genGlyph?e.genGlyph(t,this.baseGetGlyph):this.baseGetGlyph()}baseGetGlyph(){var t,e;return null!==(t=(e=this.charset)[s(0,e.length,"floor")])&&void 0!==t?t:""}get steps(){return a(this.options.steps)}getInterval(t){const{options:e,baseGetInterval:s}=this;return e.genInterval?e.genInterval(t,s.bind(this,t)):s.call(this,t)}baseGetInterval(t){let e=a(this.options.interval);return"whitespace"===t.specialType&&(e/=1.8),e}getDelay(t){const{options:e}=this;return e.genDelay?e.genDelay(t,this.baseGetDelay):this.baseGetDelay()}baseGetDelay(){return a(this.options.delay)}get mode(){return this.options.mode}get html(){return this.options.html}get endless(){return this.options.endless}get fps(){return this.options.fps}get letterize(){return this.options.letterize}get ghostChance(){return this.options.ghostChance}get changeChance(){return this.options.changeChance}get glyphsFromText(){return this.options.glyphsFromText}}class v{constructor(t){this.nGhosts=0,this.progress={percent:0,done:0,todo:0,increase(){this.done++,this.percent=this.done/this.todo},reset(t){this.percent=0,this.done=0,this.todo=t},finish(){this.done=this.todo,this.percent=1}},this.isTyping=!1,this.isPaused=!1,this.finished=!0,this.erasing=!1,this.writer=t,this.maxGhosts=this.writer.options.maxGhosts}get ghostsInLimit(){return this.nGhosts<this.maxGhosts}play(){this.isTyping=!0,this.isPaused=!1,this.finished=!1,this.addClass(),this.erasing&&this.addClass("gw-erasing"),this.maxGhosts=this.writer.options.maxGhosts,this.writer.animator.run(),this.writer.emiter.callback("start",this.writer.goalText,this.writer.writerData)}pause(){this.isTyping=!1,this.isPaused=!0,this.removeClasses()}finish(){this.progress.finish(),this.isTyping=!1,this.finished=!0,this.removeClasses()}addClass(t="gw-writing"){o(this.writer.htmlElement,t)}removeClasses(){this.writer.htmlElement.classList.remove("gw-writing","gw-erasing")}}class w{constructor(t){this.callbacks={start:[],step:[],finish:[]},this.writer=t}addCallback(t,e){this.callbacks[t].push(e)}removeCallback(t,e){const s=this.callbacks[t],i=s.indexOf(e);return-1!==i&&(s.splice(i,1),!0)}callback(t,...e){this.callbacks[t].forEach((t=>t(...e)))}call(t){const{writer:e}=this;e.updateString();const{writerData:s,string:i}=e;if(e.options.letterize&&e.htmlElement.setAttribute("data-gw-string",e.options.html?d(i):i),"step"===t)return this.callback("step",i,s);e.state.finish(),e.state.erasing||(this.callback("finish",i,s),this.emitEvent())}emitEvent(){const{htmlElement:t,writerData:e}=this.writer;"undefined"!=typeof CustomEvent&&t.dispatchEvent(new CustomEvent("gw-finished",{detail:e}))}}var T=function(t,e,s,i){return new(s||(s=Promise))((function(n,r){function h(t){try{a(i.next(t))}catch(t){r(t)}}function o(t){try{a(i.throw(t))}catch(t){r(t)}}function a(t){var e;t.done?n(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(h,o)}a((i=i.apply(t,e||[])).next())}))};class x{constructor(t,e,s,i="",n,r){this.ghosts=[[],[]],this.stop=!1,this.afterGlitchChance=0,this.writer=t;const{options:h}=t;this.index=r,this.l=e,this.gl=s,this.specialType=n,this.ghosts[0]=[...i],this.writer.state.nGhosts+=i.length,this.stepsLeft=h.steps,"tag"===n?this.stepsLeft=0:(t=>["\t","\n","\r","\f","\v",""," "].includes(t))(s)&&(this.specialType="whitespace"),this.afterGlitchChance=(h.ghostChance+h.changeChance)/3.7,t.options.letterize&&(this.els={ghostsBeforeEl:document.createElement("span"),letterEl:document.createElement("span"),ghostsAfterEl:document.createElement("span")},this.els.ghostsBeforeEl.className="gw-ghosts",this.els.ghostsAfterEl.className="gw-ghosts",this.els.letterEl.className="gw-letter")}get string(){const{l:t,ghosts:e}=this;return e[0].join("")+t+e[1].join("")}get finished(){const{l:t,gl:e,ghosts:s}=this;return t===e&&0===s[0].length&&0===s[1].length||"tag"===this.specialType}writeToElement(){if(!this.els)return;const{ghostsBeforeEl:t,ghostsAfterEl:e,letterEl:s,charEl:i}=this.els;s.innerHTML=this.l,t.textContent=this.ghosts[0].join(""),e.textContent=this.ghosts[1].join(""),i&&o(i,"gw-changed")}set spanElement(t){this.els&&(this.els.charEl=t,this.appendChildren())}appendChildren(){var t,e;null===(e=null===(t=this.els)||void 0===t?void 0:t.charEl)||void 0===e||e.append(this.els.ghostsBeforeEl,this.els.letterEl,this.els.ghostsAfterEl),this.writeToElement()}type(){var t,e,s;return T(this,void 0,void 0,(function*(){const{options:i,state:h,emiter:o}=this.writer;return"tag"===this.specialType?(this.l=this.gl,o.call("step"),h.progress.increase(),!0):(yield n(i.getDelay(this)),yield r((()=>(!this.finished||i.endless)&&!h.isPaused&&!this.stop),(()=>T(this,void 0,void 0,(function*(){yield n(i.getInterval(this));const t=this.string;this.step(),t!==this.string&&(o.call("step"),this.writeToElement()),!i.endless&&this.stepsLeft--})))),this.finished&&(h.progress.increase(),null===(e=null===(t=this.els)||void 0===t?void 0:t.charEl)||void 0===e||e.classList.add("gw-finished"),null===(s=this.els)||void 0===s||s.letterEl.classList.remove("gw-glitched")),this.finished)}))}step(){var t,e;const{writer:s}=this;if(this.stepsLeft>0&&this.l!==this.gl||l(this.afterGlitchChance)&&"whitespace"!==this.specialType||s.options.endless){const{ghostChance:e,changeChance:i}=s.options;l(e)&&(s.state.ghostsInLimit?this.addGhost():this.removeGhost()),l(i)&&(null===(t=this.els)||void 0===t||t.letterEl.classList.add("gw-glitched"),this.l=s.options.getGlyph(this))}else this.finished||(null===(e=this.els)||void 0===e||e.letterEl.classList.remove("gw-glitched"),this.l=this.gl,this.removeGhost())}addGhost(){const{writer:t,ghosts:e}=this,s=t.options.getGlyph(this);t.state.nGhosts++,l()?b(e[0],s):b(e[1],s)}removeGhost(){const{writer:t,ghosts:e}=this;(l()&&e[0].length>0?i(e[0]):i(e[1]))&&t.state.nGhosts--}}function b(t,e){const{length:i}=t;t.splice(s(0,i,"floor"),0,e)}function E(){const{charTable:t,options:e}=this,s="clear"===e.mode&&this.state.finished?"":this.previousString;t.forEach((t=>t.stop=!0)),this.charTable=[],"matching"===e.mode?C.call(this,s):G.call(this,s)}function C(t){const e=Math.min(Math.ceil(this.options.maxGhosts/2),5),s=A.call(this,t);let i=-1;s.forEach(((s,n)=>{if(i++,"tag"===s.type)return i--,void S.call(this,n,"",s);const r=""!==s.value?t.indexOf(s.value,i):-1;if(-1!==r&&r-i<=e){const e=t.substring(i,r);S.call(this,n,s.value,s,e),i=r}else S.call(this,n,t[i],s)})),P(this.charTable,s.length)}function G(t){const e=A.call(this,t);let s=-1;e.forEach(((e,i)=>{if(s++,"tag"===e.type)return s--,void S.call(this,i,"",e);S.call(this,i,t[s],e)})),P(this.charTable,e.length)}function A(t){const{options:e,goalText:s}=this,i=e.html?function(t){const e=new RegExp(u,"gi"),s=[];let i,n=0;for(;i=e.exec(t);){const r=i.index,h=e.lastIndex,o=t.slice(n,r);n=h,o&&s.push(...p(o));const a={value:i[0],type:void 0!==i[1]?"html_entity":"tag"};s.push(a)}return t.length>n&&s.push(...p(t.slice(n))),s}(s):p(s),n=Math.max(0,t.length-i.length);if(e.oneAtATime)return i.concat(p(h("",n)));const r=Math.ceil(n/2),o=Math.floor(n/2);return p(h("",r)).concat(i,p(h("",o)))}function S(t,e,s,i){const{charTable:n,options:r}=this;n.push(new x(this,null!=e?e:"",s.value||r.space,i,s.type,t))}function P(t,e){t.splice(e,t.length-e)}function W(){if(!this.options.letterize)return;const t=this.charTable.map((({specialType:t,gl:e})=>"tag"===t?e:'<span class="gw-char"></span>')).join("");this.htmlElement.innerHTML=t;const e=this.htmlElement.querySelectorAll("span.gw-char");let s=0;this.charTable.forEach((t=>{"tag"!==t.specialType&&(t.spanElement=e[s],s++)}))}var j=function(t,e,s,i){return new(s||(s=Promise))((function(n,r){function h(t){try{a(i.next(t))}catch(t){r(t)}}function o(t){try{a(i.throw(t))}catch(t){r(t)}}function a(t){var e;t.done?n(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(h,o)}a((i=i.apply(t,e||[])).next())}))};function D(t){const{charTable:e,state:s}=this,i=[];let n;n=e.filter(((t,e)=>"tag"!==t.specialType&&"html_entity"!==t.specialType&&(i.push(e),!0))).map((t=>t.gl)).join("").match(g);const h=[];{let t=-1,s=-1,r=-1;const o=()=>h[h.length-1];null==n||n.forEach((n=>{h.push([]),[...n].forEach((()=>{if(t++,s=i[t],r++,r!==s){for(let t=r;t<s;t++)o().push(e[t]);r=s}o().push(e[s])}))})),h.length||h.push([]);for(let t=s+1;t<e.length;t++)o().push(e[t])}h.reverse();let o=!0,a=!1;const l=()=>j(this,void 0,void 0,(function*(){var t;const e=h.pop();if(!e)return a=!0;const s=e.map((t=>t.type()));o=null!==(t=(yield Promise.all(s)).every((t=>t)))&&void 0!==t&&t}));t.push((()=>j(this,void 0,void 0,(function*(){return yield r((()=>!a&&o&&!s.isPaused),l),a&&o&&!s.isPaused})))())}var L=function(t,e,s,i){return new(s||(s=Promise))((function(n,r){function h(t){try{a(i.next(t))}catch(t){r(t)}}function o(t){try{a(i.throw(t))}catch(t){r(t)}}function a(t){var e;t.done?n(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(h,o)}a((i=i.apply(t,e||[])).next())}))};function M(t,e){var s;const{charTable:i,state:n,options:h}=this,o=null!==(s=null==e?void 0:e.reverse)&&void 0!==s&&s?[...i]:[...i].reverse(),a=()=>L(this,void 0,void 0,(function*(){let t=!0,e=!1;return yield r((()=>!e&&t&&!n.isPaused),(()=>L(this,void 0,void 0,(function*(){var s;const i=o.pop();i?t=null!==(s=yield i.type())&&void 0!==s&&s:e=!0})))),e&&t&&!n.isPaused}));for(let e=0;e<h.oneAtATime;e++)t.push(a())}class O{constructor(t){this.last=0,this.rate=16,this.running=!1,this.writer=t}run(){this.running||this.writer.options.letterize||(this.rate=Math.floor(1e3/this.writer.options.fps),this.running=!0,requestAnimationFrame(this.frame.bind(this)))}frame(t){return this.writer.state.isTyping?(this.last||(this.last=t),t-this.last<this.rate||(this.animate.call(this),this.last=t),requestAnimationFrame(this.frame.bind(this))):(this.animate.call(this),this.running=!1)}animate(){const{htmlElement:t,string:e}=this.writer;this.writer.options.html?t.innerHTML=e:t.textContent=e,t.setAttribute("data-gw-string",this.writer.options.html?d(e):e)}}class k{constructor(t,e,s=800,i=!1){if(this.isStopped=!1,this.isLooping=!1,this.loopInterval=0,this.index=-1,this.writer=t,this.interval=s,Array.isArray(e))this.texts=e;else{let t;t="object"==typeof e?e:document.querySelector(e),this.texts=[],null==t||t.childNodes.forEach((t=>{const{tagName:e,innerHTML:s}=t;"P"===e&&void 0!==s&&this.texts.push(s)}))}"boolean"==typeof i?this.isLooping=i:"function"==typeof i?this.endCallback=i:(this.isLooping=!0,this.loopInterval=i),this.loop()}stop(){this.isStopped=!0}resume(){this.index--,this.isStopped=!1,this.writer.state.isPaused=!1,this.loop()}loop(){var t,e,s,i,r;return e=this,s=void 0,r=function*(){if(!this.texts.length)return;if(this.index++,this.index>=this.texts.length){if(!this.isLooping)return null===(t=this.endCallback)||void 0===t?void 0:t.call(this,this.writer.string,this.writer.getWriterData("SUCCESS","The queue has reached it's end."));yield n(this.loopInterval),this.index=0}if(this.isStopped||this.writer.state.isPaused)return;const e=yield this.writer.manageWriting(this.texts[this.index]);"SUCCESS"!==(null==e?void 0:e.status)||this.writer.state.isPaused||(yield n(this.interval),this.loop())},new((i=void 0)||(i=Promise))((function(t,n){function h(t){try{a(r.next(t))}catch(t){n(t)}}function o(t){try{a(r.throw(t))}catch(t){n(t)}}function a(e){var s;e.done?t(e.value):(s=e.value,s instanceof i?s:new i((function(t){t(s)}))).then(h,o)}a((r=r.apply(e,s||[])).next())}))}}var R=function(t,e,s,i){return new(s||(s=Promise))((function(n,r){function h(t){try{a(i.next(t))}catch(t){r(t)}}function o(t){try{a(i.throw(t))}catch(t){r(t)}}function a(t){var e;t.done?n(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(h,o)}a((i=i.apply(t,e||[])).next())}))};class q{constructor(t,e,s){var i;this.charTable=[],this.goalText="",this.lastText="",this.string="",this.htmlElement=t?"string"==typeof t?null!==(i=document.querySelector(t))&&void 0!==i?i:document.createElement("span"):t:document.createElement("span"),this.htmlElement.$writer=this,this.options=new y(this,e),this.state=new v(this),this.emiter=new w(this),s&&this.emiter.addCallback("finish",s),this.animator=new O(this),this.string=this.previousString}updateString(){this.string=this.charTable.map((t=>t.string)).join("")}get previousString(){let t=this.htmlElement.textContent;return"string"!=typeof t&&(t=this.options.html?d(this.string):this.string),t=t.trim(),t}get writerData(){const{options:t,state:e,string:s}=this;return{string:s,writer:this,options:t,state:e}}write(t){return R(this,void 0,void 0,(function*(){return this.queue&&(this.queue.stop(),delete this.queue),this.manageWriting(t)}))}queueWrite(t,e,s){this.queue&&(this.queue.stop(),delete this.queue),this.queue=new k(this,t,e,s)}add(t){return R(this,void 0,void 0,(function*(){const{previousString:e}=this;return this.write(e+t)}))}remove(t){return R(this,void 0,void 0,(function*(){const{previousString:e}=this,s=Array.from(e);return s.splice(-t),this.write(s.join(""))}))}play(){return R(this,void 0,void 0,(function*(){return this.state.isPaused?this.queue?(this.queue.resume(),this.getWriterData("SUCCESS","The queue was resumed")):this.manageWriting(null):this.getWriterData("ERROR","The writer isn't paused.")}))}pause(){this.state.pause()}endless(t){this.options.extend({endless:t})}addCallback(t,e){return this.emiter.addCallback(t,e),this}removeCallback(t,e){return this.emiter.removeCallback(t,e),this}manageWriting(t){return R(this,void 0,void 0,(function*(){if(null!==t&&(this.lastText=t),["erase_smart","erase"].includes(this.options.mode)&&(this.state.finished||this.state.erasing)){this.state.erasing=!0;const t=this.genGoalStringToErase(this.lastText);if(this.preparePropertiesBeforeWrite(t),yield this.playChT({reverse:0!==this.options.oneAtATime}),!this.state.finished)return this.getWriterData("ERROR","Erasing did not finish.");this.state.erasing=!1}return this.preparePropertiesBeforeWrite(this.lastText),this.pause(),this.playChT()}))}preparePropertiesBeforeWrite(t){this.goalText=t,this.state.nGhosts=0,this.options.setCharset(),E.call(this),this.state.progress.reset(this.charTable.length),W.call(this)}playChT(t){return R(this,void 0,void 0,(function*(){const e=[],{charTable:s,state:i,options:n}=this;if(i.isTyping)return this.getWriterData("ERROR","The writer is already typing.");i.play(),n.oneAtATime>0?M.call(this,e,t):"word"===n.oneAtATime?D.call(this,e):s.forEach((t=>e.push(t.type())));try{const t=(yield Promise.all(e)).every((t=>t));return this.returnResult(t)}catch(t){return this.getWriterData("ERROR","Writer encountered an error.",t)}}))}returnResult(t){return t?this.emiter.call("finish"):this.emiter.call("step"),t?this.getWriterData("SUCCESS","The writer finished typing."):this.getWriterData("ERROR","Writer failed to finish typing.")}getWriterData(t,e,s){const{writerData:i}=this;return Object.assign(Object.assign({},i),{status:t,message:e,error:s})}genGoalStringToErase(t){var e;const{previousString:s}=this;let i="";if("erase_smart"===this.options.mode)for(let n=0;n<t.length;n++){const r=t[n],h=null!==(e=s[n])&&void 0!==e?e:"";if(r!==h)break;i+=h}const n=Math.max(t.length-i.length,0);return n>0&&" "===this.options.space&&(i=i.padEnd(n+i.length," ")),i}}function I(t,e,s,i,n){return R(this,void 0,void 0,(function*(){const r=new q(e,s,n);return i&&r.addCallback("step",i),r.write(t)}))}function F(t,e,s,i,n,r,h){const o=new q(e,s,h);return r&&o.addCallback("step",r),o.queueWrite(t,i,n),o}const _=(t,e,s)=>new q(t,e,s);return e})()})); //# sourceMappingURL=index.min.map