|
| 1 | +import React, { useCallback, useEffect, useRef, useState } from 'react'; |
| 2 | + |
| 3 | +import type { Delay, TypedChildren, TypistProps } from '../types/TypistProps'; |
| 4 | +import insertCursor from '../utils/insertCursor'; |
| 5 | +import { defaultSplitter, emptyFunc } from '../utils/defaultFuncs'; |
| 6 | + |
| 7 | +import getTypedChildrenArray from '../utils/getTypedChildrenArray'; |
| 8 | +import getActions from '../utils/getActions'; |
| 9 | + |
| 10 | +const Main = ({ |
| 11 | + cursor, |
| 12 | + disabled = false, |
| 13 | + restartKey, |
| 14 | + children, |
| 15 | + splitter = defaultSplitter, |
| 16 | + typingDelay = 75, |
| 17 | + backspaceDelay = typingDelay, |
| 18 | + onTypingDone, |
| 19 | + startDelay = 0, |
| 20 | + finishDelay = 0, |
| 21 | + loop = false, |
| 22 | + pause = false, |
| 23 | +}: TypistProps) => { |
| 24 | + const [typedChildrenArray, setTypedChildrenArray] = useState<TypedChildren[]>([]); |
| 25 | + const [currentIndex, setCurrentIndex] = useState(-1); |
| 26 | + const clearTimerRef = useRef(emptyFunc); |
| 27 | + const loopRef = useRef(loop); |
| 28 | + const pauseRef = useRef(pause); |
| 29 | + |
| 30 | + const timeoutPromise = useCallback((delay: Delay) => { |
| 31 | + return new Promise((resolve, reject) => { |
| 32 | + const ms = typeof delay === 'number' ? delay : delay(); |
| 33 | + const timeoutId = setTimeout(resolve, ms); |
| 34 | + clearTimerRef.current = () => { |
| 35 | + clearTimeout(timeoutId); |
| 36 | + reject(); |
| 37 | + }; |
| 38 | + }); |
| 39 | + }, []); |
| 40 | + |
| 41 | + const pausePromise = useCallback(() => { |
| 42 | + return new Promise<void>((resolve, reject) => { |
| 43 | + const intervalId = setInterval(() => { |
| 44 | + if (!pauseRef.current) { |
| 45 | + clearInterval(intervalId); |
| 46 | + resolve(); |
| 47 | + } |
| 48 | + }); |
| 49 | + clearTimerRef.current = () => { |
| 50 | + clearInterval(intervalId); |
| 51 | + reject(); |
| 52 | + }; |
| 53 | + }); |
| 54 | + }, []); |
| 55 | + |
| 56 | + const loopPromise = useCallback(() => { |
| 57 | + return new Promise<void>((resolve, reject) => { |
| 58 | + const intervalId = setInterval(() => { |
| 59 | + if (loopRef.current) { |
| 60 | + clearInterval(intervalId); |
| 61 | + resolve(); |
| 62 | + } |
| 63 | + }); |
| 64 | + clearTimerRef.current = () => { |
| 65 | + clearInterval(intervalId); |
| 66 | + reject(); |
| 67 | + }; |
| 68 | + }); |
| 69 | + }, []); |
| 70 | + |
| 71 | + useEffect(() => { |
| 72 | + loopRef.current = loop; |
| 73 | + pauseRef.current = pause; |
| 74 | + }, [loop, pause]); |
| 75 | + |
| 76 | + useEffect(() => { |
| 77 | + const typedChildrenArray = getTypedChildrenArray(children, splitter); |
| 78 | + setTypedChildrenArray(typedChildrenArray); |
| 79 | + if (disabled) { |
| 80 | + setCurrentIndex(typedChildrenArray.length - 1); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + (async () => { |
| 85 | + try { |
| 86 | + do { |
| 87 | + setCurrentIndex(-1); |
| 88 | + const actions = getActions(children, splitter); |
| 89 | + await timeoutPromise(startDelay); |
| 90 | + for (const { type, payload } of actions) { |
| 91 | + if (type === 'TYPE_TOKEN') { |
| 92 | + await pausePromise(); |
| 93 | + setCurrentIndex(prev => prev + 1); |
| 94 | + await timeoutPromise(typingDelay); |
| 95 | + } else if (type === 'BACKSPACE') { |
| 96 | + let amount = payload; |
| 97 | + while (amount--) { |
| 98 | + await pausePromise(); |
| 99 | + setCurrentIndex(prev => prev + 1); |
| 100 | + await timeoutPromise(backspaceDelay); |
| 101 | + } |
| 102 | + } else if (type === 'PASTE') { |
| 103 | + setCurrentIndex(prev => prev + 1); |
| 104 | + } else if (type === 'PAUSE') { |
| 105 | + await timeoutPromise(payload); |
| 106 | + } |
| 107 | + } |
| 108 | + onTypingDone?.(); |
| 109 | + await timeoutPromise(finishDelay); |
| 110 | + await loopPromise(); |
| 111 | + } while (loopRef.current); |
| 112 | + } catch (error) { |
| 113 | + // do nothing |
| 114 | + } |
| 115 | + })(); |
| 116 | + |
| 117 | + return () => { |
| 118 | + clearTimerRef.current(); |
| 119 | + }; |
| 120 | + |
| 121 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 122 | + }, [restartKey, disabled]); |
| 123 | + |
| 124 | + const typedChildren = typedChildrenArray[currentIndex]; |
| 125 | + return <>{cursor ? insertCursor(typedChildren, cursor) : typedChildren}</>; |
| 126 | +}; |
| 127 | + |
| 128 | +export default Main; |
0 commit comments