UNPKG

typing-flow

Version:

Npm package for typing animation for web

214 lines (213 loc) 5.35 kB
var y = Object.defineProperty; var m = (n, e, o) => e in n ? y(n, e, { enumerable: !0, configurable: !0, writable: !0, value: o }) : n[e] = o; var r = (n, e, o) => m(n, typeof e != "symbol" ? e + "" : e, o); import "./text-builder-Cg-a6DAX.js"; import { i as u } from "./browser-oWv6Ioy8.js"; import { M as l } from "./index-DSpjb_UQ.js"; import { T as R } from "./index-DSpjb_UQ.js"; const f = (n, ...e) => { for (const o of n) o(...e); }, _ = (n, e, o) => { const t = n.next(e); t.done ? o == null || o() : t.value.then( (i) => _(n, i, o), (i) => n.throw(i) ); }; function T(n, e) { return new Promise((o) => { _(n, e, o); }); } const N = { start: "onStart", finish: "onFinish" }; class b { constructor() { r(this, "_hooks", { onStart: [], onFinish: [] }); } on(e, o) { return this._hooks[N[e]].push(o), this; } get hooks() { return this._hooks; } } const c = (n) => { const e = { indexOfSymbolWithCursor: l, lastNonTagSymbolIndex: null, amountOfNonTagSymbols: 0 }; for (let o = 0; o < n.content.length; o++) { const t = n.content[o]; u(t) || (++e.amountOfNonTagSymbols, e.lastNonTagSymbolIndex = o), e.amountOfNonTagSymbols === n.cursorPosition + 1 && (e.indexOfSymbolWithCursor = o); } return n.cursorPosition < 0 && (e.indexOfSymbolWithCursor = l), e; }; function p(n, e) { if (e.cursorPosition < 0) return; const o = n.nodeBuilder(e); let t = 0; const { amountOfNonTagSymbols: i, indexOfSymbolWithCursor: s } = c(e), d = Math.min( o.amount, i ); if (d !== 0) { for (let a = 0; t < d; a++) { const h = s - a; if (h < 0) break; const g = e.content[h]; u(g) || (e.content.splice(h, 1), t++); } e.cursorPosition -= t; } } function x(n, e) { const o = n.nodeBuilder(e); e.cursorPosition = Math.max( e.cursorPosition - o.distance, l ); } function M(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 d = e.content[s]; if (!u(d)) { if (i >= o.distance) break; i++, e.cursorPosition++; } } } function O(n, e) { if (e.cursorPosition < l) return; const o = n.nodeBuilder(e), { indexOfSymbolWithCursor: t } = c(e); let i = 0, s = 0; for (; i < o.amount; ) { const d = t + s + 1; if (d >= e.content.length) break; const a = e.content[d]; if (u(a)) { s++; continue; } e.content.splice(d, 1), i++; } } function k(n) { const { amountOfNonTagSymbols: e } = c(n); n.cursorPosition = e - 1; } function P(n) { n.cursorPosition = l; } function H(n, e) { const o = n.nodeBuilder(e), { indexOfSymbolWithCursor: t } = c(e); if (o.isTag && t === l) { e.content.push(o.text); return; } e.content.splice( t + 1, 0, o.text ), o.isTag || (e.cursorPosition += 1); } class S { constructor() { r(this, "_typingSnapshot", { content: [], cursorPosition: l }); r(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) { H(e, this._typingSnapshot); } _handleBackspaceTypingNode(e) { p(e, this._typingSnapshot); } _handleDeleteTypingNode(e) { O(e, this._typingSnapshot); } _handleCursorMoveLeftTypingNode(e) { x(e, this._typingSnapshot); } _handleCursorMoveRightTypingNode(e) { M(e, this._typingSnapshot); } _handleDelayTypingNode() { } _handleHomeTypingNode() { P(this._typingSnapshot); } _handleEndTypingNode() { k(this._typingSnapshot); } get handlers() { return this._nodeHandlers; } get typingSnapshot() { return this._typingSnapshot; } } class v { constructor(e) { r(this, "_config"); r(this, "_typingNodes", []); r(this, "_hooksModule", new b()); r(this, "_nodeHandlersModule", new S()); 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() { f(this._hooksModule.hooks.onStart), await T(this._typing()), f(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; } } export { v as TypingFlow, R as TypingFlowError };