-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathshabdawali.min.js
1 lines (1 loc) · 8.3 KB
/
shabdawali.min.js
1
!function(f){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=f();else if("function"==typeof define&&define.amd)define([],f);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).shabdawali=f()}}(function(){return function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){return o(e[i][1][r]||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}({1:[function(require,module,exports){var shuffle=require("./util").shuffle,replaceOn=require("./util").replaceOn,commonStartingString=require("./util").commonStartingString;function Shabdawali(targetEl,opts){this.element=targetEl,opts||(opts={}),this.lines=opts.lines,this.playCount=-1,this.onChar=opts.onCharChange||function(){},this.onLine=opts.onLineChange||function(){},this.nextWord=opts.nextWord||function(){},this.speed=opts.typingSpeed||70,this.timeToReadAWord=80,this.dynamicPauseBeforeDelete=!1!==opts.dynamicPauseBeforeDelete,this.pauseBeforeDelete=opts.pauseBeforeDelete||2e3,this.pauseBeforeNext=opts.pauseBeforeNext||1e3,this.delay=opts.delay||0,this.typoEffect=opts.typoEffect||!1,this.dynamicPause=opts.dynamicPause||!1,this.deleteSpeed=opts.deleteSpeed||this.speed/2,this.deleteSpeedArr=[],!(this.deleteUpto=[])===opts.repeat?this.repeat=!1:this.repeat=!0,!1===opts.deleteEffect?this.deleteEffect=!1:this.deleteEffect=!0,opts.cursorEffect?(this.cursorEffect=!0,this.appendCursor(this.element),this.showCursor()):(this.cursorEffect=!1,this.hideCursor()),"start"===opts.deleteFrom?this.trimmedText=function(text,len){return text.substring(1)}:this.trimmedText=function(text,len){return text.substring(0,len)};for(var i=0;i<this.lines.length;i++){var line=this.lines[i];if(opts.replacable)if(i<this.lines.length-1){var commonUpto=commonStartingString(line,this.lines[i+1]);this.deleteUpto.push(commonUpto||0)}else this.deleteUpto.push(0);else this.deleteUpto.push(0);this.deleteSpeedArr.push(opts.deleteSpeed||this.deleteSpeed-(line.length-this.deleteUpto[i])),this.deleteSpeedArr[i]<5&&(this.deleteSpeedArr[i]=5)}this.typo={max:1,minWordLength:5,goAheadLimit:3,skip:2,randomFactor:4},this._pauseCallBack,this.events={pause:[],resume:[],finish:[]}}Shabdawali.prototype.makeTypo=function(word){return shuffle(word.substr(this.typo.skip))},Shabdawali.prototype.checkIfFitsForTypoEffect=function(word){return 2===Math.floor(Math.random()*this.typo.randomFactor+1)&&(word.length>=this.typo.minWordLength||void 0)},Shabdawali.prototype.start=function(count){this._stopped=!1,this.currentLineIndex=0,this.currentLetterIndex=0,this.nextWordIndex=0,this.typoCount=0,this.startCorrectingAt=-1,count&&count<=this.lines.length?this.playCount=count:this.playCount=-1,this.element.textContent="",this.typeNext()},Shabdawali.prototype.stop=function(){this._stopped=!0},Shabdawali.prototype.pause=function(){this._paused=!0,this._emit("pause")},Shabdawali.prototype.resume=function(count){count&&count<=this.lines.length?this.playCount=count:this.playCount=-1,this._paused=!1,this._pauseCallBack&&setTimeout(this._pauseCallBack,this.pauseUntil),this._pauseCallBack=null,this.currentLineIndex===this.lines.length&&this.start(count),this._emit("resume")},Shabdawali.prototype.deleteText=function(cLine){if(!this._stopped)if(this._paused){var that=this;this._pauseCallBack=function(){that.deleteText(cLine)}}else this.correctingText&&0===this.typoRange?(this.typeText(this.lines[this.currentLineIndex-1]),this.correctingText=!1):this.correctingText&&0<this.typoRange?(this.delete(cLine,this.speed),this.typoRange--):this.currentLetterIndex===this.deleteUpto[this.currentLineIndex-1]?this.typeNext():this.delete(cLine,this.deleteSpeedArr[this.currentLineIndex-1])},Shabdawali.prototype.delete=function(cLine,speed){this.onChar("BS"),this.element.textContent=this.trimmedText(cLine,--this.currentLetterIndex);var that=this;setTimeout(function(){that.deleteText(cLine)},speed)},Shabdawali.prototype.typeText=function(cLine){if(!this._stopped)if(this._paused){var that=this;this._pauseCallBack=function(){that.typeText(cLine)}}else if(cLine)if(this.currentLetterIndex===cLine.length)if(this.typoEffect&&0<this.startCorrectingAt&&this.startCorrectingAt===this.currentLetterIndex)this.startCorrectingAt=-1,this.correctingText=!0,this.deleteText(cLine);else if(this.deleteEffect){var gape=this.pauseBeforeDelete;this.dynamicPauseBeforeDelete&&(gape=this.timeToReadAWord*(cLine.length/4))<2e3&&(gape=2e3);that=this;setTimeout(function(){that.deleteText(cLine)},gape)}else this.typeNext();else{if(this.typoEffect&&this.currentLetterIndex===this.nextWordIndex&&this.typoCount<this.typo.max){var nextSpaceIndex=cLine.indexOf(" ",this.nextWordIndex);-1===nextSpaceIndex&&(nextSpaceIndex=cLine.length+1);var word=cLine.substr(this.nextWordIndex,nextSpaceIndex-this.nextWordIndex);if(this.nextWord(word),this.checkIfFitsForTypoEffect(word)){var typoWord=this.makeTypo(word);cLine=replaceOn(cLine,this.currentLetterIndex+this.typo.skip,typoWord),this.typoCount++,this.typoRange=word.length,this.startCorrectingAt=this.currentLetterIndex+this.typoRange}this.nextWordIndex=nextSpaceIndex+1}if(this.typoEffect&&0<this.startCorrectingAt&&this.startCorrectingAt===this.currentLetterIndex)this.startCorrectingAt=-1,this.correctingText=!0,this.deleteText(cLine);else{var char=cLine.substr(this.currentLetterIndex++,1);this.onChar(char),this.element.textContent=cLine.substr(0,this.currentLetterIndex);that=this;if(this.dynamicPause&&" "==char){var prevSpaceIndex=cLine.lastIndexOf(" ",this.currentLetterIndex-2);-1==prevSpaceIndex&&(prevSpaceIndex=0);var prevWord=cLine.substr(prevSpaceIndex,this.currentLetterIndex-prevSpaceIndex),complexity=wordComplexity(prevWord);complexity=1==complexity?1:complexity*(prevWord.length/2),setTimeout(function(){that.typeText(cLine)},this.speed*complexity)}else setTimeout(function(){that.typeText(cLine)},this.speed)}}};var wordComplexity=function(word){var complexity=1;return 10<word.length&&complexity++,"!"!=word.substr(word.length-1,1)&&"."!=word.substr(word.length-1,1)&&";"!=word.substr(word.length-1,1)||complexity++,5<countConsonants(word)&&complexity++,complexity},countConsonants=function(word){for(var numberOfConsonants=0,index=0;index<word.length;index++){["a","e","i","o","u"].indexOf(word.charAt(index).toLowerCase())<0&&numberOfConsonants++}return numberOfConsonants};Shabdawali.prototype.nextLine=function(){this.currentLineIndex===this.lines.length&&this.repeat&&(this.currentLineIndex=0);var line=this.lines[this.currentLineIndex];return this.onLine("CR",this.currentLineIndex,line),this.currentLineIndex++,line},Shabdawali.prototype.typeNext=function(){if(0!==this.playCount){0<this.playCount&&this.playCount--,this.nextWordIndex=0,this.typoCount=0;var line=this.nextLine();this.currentLetterIndex=this.deleteUpto[this.currentLineIndex-2]||0;that=this;line&&setTimeout(function(){that.element.textContent="",that.typeText(line)},this.pauseBeforeNext),line||this._emit("finish")}else{this.pause();var that=this;this._pauseCallBack=function(){that.typeNext()}}},Shabdawali.prototype._emit=function(eventName){for(var i=0;i<this.events[eventName].length;i++)this.events[eventName][i]()},Shabdawali.prototype.on=function(eventName,fn){this.events[eventName].push(fn)},Shabdawali.prototype.appendCursor=function(){this.cursorEl=document.createElement("span"),this.cursorEl.classList.add("shabdawali-cursor"),this.cursorEl.textContent="|",this.element.insertAdjacentElement("afterend",this.cursorEl)},Shabdawali.prototype.hideCursor=function(){this.cursorEffect&&(this.cursorEl.style.display="none")},Shabdawali.prototype.showCursor=function(){this.cursorEffect&&(this.cursorEl.style.display="inline")},module.exports=function(targetEl,opts){return new Shabdawali(targetEl,opts)}},{"./util":2}],2:[function(require,module,exports){module.exports.replaceOn=function(line,start,str){return line.substr(0,start)+str+line.substr(start+str.length)},module.exports.shuffle=function(word){for(var a=word.split(""),i=a.length-1;0<i;i--){var j=Math.floor(Math.random()*(i+1)),tmp=a[i];a[i]=a[j],a[j]=tmp}return a.join("")},module.exports.commonStartingString=function(line1,line2){for(var i=0;i<line1.length;i++)if(line1[i]!==line2[i])return i}},{}]},{},[1])(1)});