UNPKG

@omnedia/rxjs-typewriter

Version:

A simple typewriter effect to animate text.

79 lines (78 loc) 2.9 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from2, except, desc) => { if (from2 && typeof from2 === "object" || typeof from2 === "function") { for (let key of __getOwnPropNames(from2)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from2[key], enumerable: !(desc = __getOwnPropDesc(from2, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { TypeWriter: () => TypeWriter }); module.exports = __toCommonJS(src_exports); var import_rxjs = require("rxjs"); var TypeWriter = class { constructor() { this.writeSpeed = 40; this.waitAfterTyped = 3500; this.waitAfterDeleted = 50; this.loop = true; this.wordsCount = 0; } typeWriteEffect(words, loop, writeSpeed, deleteDelay, writeDelay) { this.writeSpeed = writeSpeed != null ? writeSpeed : this.writeSpeed; this.waitAfterTyped = deleteDelay != null ? deleteDelay : this.waitAfterTyped; this.waitAfterDeleted = writeDelay != null ? writeDelay : this.waitAfterDeleted; this.loop = loop != null ? loop : this.loop; this.wordsCount = words.length; return this.startTypewriter(words); } startTypewriter(words) { if (this.loop === false) { return (0, import_rxjs.from)(words).pipe((0, import_rxjs.concatMap)((word) => this.typeEffect(word))); } return (0, import_rxjs.from)(words).pipe( (0, import_rxjs.concatMap)((word) => this.typeEffect(word)), (0, import_rxjs.repeat)() ); } typeEffect(word) { if (this.loop === false) { this.wordsCount -= 1; } return (0, import_rxjs.concat)( this.typeWord(word), (0, import_rxjs.of)("").pipe((0, import_rxjs.delay)(this.waitAfterTyped), (0, import_rxjs.ignoreElements)()), this.typeWord(word, true), (0, import_rxjs.of)("").pipe((0, import_rxjs.delay)(this.waitAfterDeleted), (0, import_rxjs.ignoreElements)()) ); } typeWord(word, backwards) { if (this.loop === false && this.wordsCount <= 0 && backwards) { return (0, import_rxjs.of)(word); } return (0, import_rxjs.interval)(this.writeSpeed).pipe( (0, import_rxjs.map)((x) => { return backwards ? word.substring(0, word.length - x) : word.substring(0, x + 1); }), (0, import_rxjs.take)(word.length + 1) ); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { TypeWriter }); //# sourceMappingURL=index.js.map