UNPKG

typing-flow

Version:

Npm package for typing animation for web

2 lines (1 loc) 4.39 kB
"use strict";var T=Object.defineProperty;var N=(n,e,o)=>e in n?T(n,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):n[e]=o;var d=(n,e,o)=>N(n,typeof e!="symbol"?e+"":e,o);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("./text-builder-ANLi6H6y.cjs");const a=require("./browser--HHLw8_C.cjs"),l=require("./index-o6_sHwN7.cjs"),_=(n,...e)=>{for(const o of n)o(...e)},f=(n,e,o)=>{const t=n.next(e);t.done?o==null||o():t.value.then(i=>f(n,i,o),i=>n.throw(i))};function y(n,e){return new Promise(o=>{f(n,e,o)})}const O={start:"onStart",finish:"onFinish"};class S{constructor(){d(this,"_hooks",{onStart:[],onFinish:[]})}on(e,o){return this._hooks[O[e]].push(o),this}get hooks(){return this._hooks}}const c=n=>{const e={indexOfSymbolWithCursor:l.MIN_POSSIBLE_CURSOR_POSITION,lastNonTagSymbolIndex:null,amountOfNonTagSymbols:0};for(let o=0;o<n.content.length;o++){const t=n.content[o];a.isTagString(t)||(++e.amountOfNonTagSymbols,e.lastNonTagSymbolIndex=o),e.amountOfNonTagSymbols===n.cursorPosition+1&&(e.indexOfSymbolWithCursor=o)}return n.cursorPosition<0&&(e.indexOfSymbolWithCursor=l.MIN_POSSIBLE_CURSOR_POSITION),e};function m(n,e){if(e.cursorPosition<0)return;const o=n.nodeBuilder(e);let t=0;const{amountOfNonTagSymbols:i,indexOfSymbolWithCursor:s}=c(e),r=Math.min(o.amount,i);if(r!==0){for(let u=0;t<r;u++){const h=s-u;if(h<0)break;const g=e.content[h];a.isTagString(g)||(e.content.splice(h,1),t++)}e.cursorPosition-=t}}function b(n,e){const o=n.nodeBuilder(e);e.cursorPosition=Math.max(e.cursorPosition-o.distance,l.MIN_POSSIBLE_CURSOR_POSITION)}function I(n,e){const o=n.nodeBuilder(e),{indexOfSymbolWithCursor:t}=c(e);let i=0;for(let s=t+1;s<e.content.length;s++){const r=e.content[s];if(!a.isTagString(r)){if(i>=o.distance)break;i++,e.cursorPosition++}}}function P(n,e){if(e.cursorPosition<l.MIN_POSSIBLE_CURSOR_POSITION)return;const o=n.nodeBuilder(e),{indexOfSymbolWithCursor:t}=c(e);let i=0,s=0;for(;i<o.amount;){const r=t+s+1;if(r>=e.content.length)break;const u=e.content[r];if(a.isTagString(u)){s++;continue}e.content.splice(r,1),i++}}function p(n){const{amountOfNonTagSymbols:e}=c(n);n.cursorPosition=e-1}function M(n){n.cursorPosition=l.MIN_POSSIBLE_CURSOR_POSITION}function x(n,e){const o=n.nodeBuilder(e),{indexOfSymbolWithCursor:t}=c(e);if(o.isTag&&t===l.MIN_POSSIBLE_CURSOR_POSITION){e.content.push(o.text);return}e.content.splice(t+1,0,o.text),o.isTag||(e.cursorPosition+=1)}class k{constructor(){d(this,"_typingSnapshot",{content:[],cursorPosition:l.MIN_POSSIBLE_CURSOR_POSITION});d(this,"_nodeHandlers",{text:this._handleTextTypingNode.bind(this),backspace:this._handleBackspaceTypingNode.bind(this),delete:this._handleDeleteTypingNode.bind(this),cursorMoveLeft:this._handleCursorMoveLeftTypingNode.bind(this),cursorMoveRight:this._handleCursorMoveRightTypingNode.bind(this),delay:this._handleDelayTypingNode.bind(this),home:this._handleHomeTypingNode.bind(this),end:this._handleEndTypingNode.bind(this)})}_handleTextTypingNode(e){x(e,this._typingSnapshot)}_handleBackspaceTypingNode(e){m(e,this._typingSnapshot)}_handleDeleteTypingNode(e){P(e,this._typingSnapshot)}_handleCursorMoveLeftTypingNode(e){b(e,this._typingSnapshot)}_handleCursorMoveRightTypingNode(e){I(e,this._typingSnapshot)}_handleDelayTypingNode(){}_handleHomeTypingNode(){M(this._typingSnapshot)}_handleEndTypingNode(){p(this._typingSnapshot)}get handlers(){return this._nodeHandlers}get typingSnapshot(){return this._typingSnapshot}}class C{constructor(e){d(this,"_config");d(this,"_typingNodes",[]);d(this,"_hooksModule",new S);d(this,"_nodeHandlersModule",new k);this._config=e}commands(e){for(let o of e)this._typingNodes=this._typingNodes.concat(o);return this}_handleTypingNode(e,o){return new Promise(t=>{const i=()=>{this._nodeHandlersModule.handlers[e.type](e,o),this._config.renderer(this._nodeHandlersModule.typingSnapshot),t()},s=e.nodeBuilder(this._nodeHandlersModule.typingSnapshot).delay;s?setTimeout(i,s):i()})}*_typing(){for(let e=0;e<this._typingNodes.length;e++)yield this._handleTypingNode(this._typingNodes[e],e)}async _executeFlow(){_(this._hooksModule.hooks.onStart),await y(this._typing()),_(this._hooksModule.hooks.onFinish)}on(...e){return this._hooksModule.on(...e),this}async start(){return this._config.loop&&this.on("finish",()=>this._executeFlow()),this._executeFlow(),this}}exports.TypingFlowError=l.TypingFlowError;exports.TypingFlow=C;