@chayns-components/typewriter 5.0.52 → 5.0.53-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/components/typewriter/Typewriter.js +66 -216
- package/lib/cjs/components/typewriter/Typewriter.js.map +1 -1
- package/lib/cjs/components/typewriter/typewrite-view/TypewriterView.js +55 -0
- package/lib/cjs/components/typewriter/typewrite-view/TypewriterView.js.map +1 -0
- package/lib/cjs/components/typewriter/{Typewriter.styles.js → typewrite-view/TypewriterView.styles.js} +2 -2
- package/lib/cjs/components/typewriter/typewrite-view/TypewriterView.styles.js.map +1 -0
- package/lib/cjs/components/typewriter/{AnimatedTypewriterText.js → typewrite-view/animated-typewriter-text/AnimatedTypewriterText.js} +2 -2
- package/lib/cjs/components/typewriter/typewrite-view/animated-typewriter-text/AnimatedTypewriterText.js.map +1 -0
- package/lib/cjs/hooks/useChunkStreamingSpeed.js +36 -0
- package/lib/cjs/hooks/useChunkStreamingSpeed.js.map +1 -0
- package/lib/cjs/hooks/useTypewriterAnimation.js +185 -0
- package/lib/cjs/hooks/useTypewriterAnimation.js.map +1 -0
- package/lib/cjs/{components/typewriter → utils}/utils.js +7 -24
- package/lib/cjs/utils/utils.js.map +1 -0
- package/lib/esm/components/typewriter/Typewriter.js +69 -219
- package/lib/esm/components/typewriter/Typewriter.js.map +1 -1
- package/lib/esm/components/typewriter/typewrite-view/TypewriterView.js +48 -0
- package/lib/esm/components/typewriter/typewrite-view/TypewriterView.js.map +1 -0
- package/lib/esm/components/typewriter/{Typewriter.styles.js → typewrite-view/TypewriterView.styles.js} +2 -2
- package/lib/esm/components/typewriter/typewrite-view/TypewriterView.styles.js.map +1 -0
- package/lib/esm/components/typewriter/{AnimatedTypewriterText.js → typewrite-view/animated-typewriter-text/AnimatedTypewriterText.js} +1 -1
- package/lib/esm/components/typewriter/typewrite-view/animated-typewriter-text/AnimatedTypewriterText.js.map +1 -0
- package/lib/esm/hooks/useChunkStreamingSpeed.js +30 -0
- package/lib/esm/hooks/useChunkStreamingSpeed.js.map +1 -0
- package/lib/esm/hooks/useTypewriterAnimation.js +178 -0
- package/lib/esm/hooks/useTypewriterAnimation.js.map +1 -0
- package/lib/esm/{components/typewriter → utils}/utils.js +5 -22
- package/lib/esm/utils/utils.js.map +1 -0
- package/lib/types/components/typewriter/typewrite-view/TypewriterView.d.ts +19 -0
- package/lib/types/components/typewriter/{Typewriter.styles.d.ts → typewrite-view/TypewriterView.styles.d.ts} +1 -1
- package/lib/types/hooks/useChunkStreamingSpeed.d.ts +7 -0
- package/lib/types/hooks/useTypewriterAnimation.d.ts +35 -0
- package/lib/types/{components/typewriter → utils}/utils.d.ts +1 -4
- package/package.json +3 -3
- package/lib/cjs/components/typewriter/AnimatedTypewriterText.js.map +0 -1
- package/lib/cjs/components/typewriter/Typewriter.styles.js.map +0 -1
- package/lib/cjs/components/typewriter/utils.js.map +0 -1
- package/lib/esm/components/typewriter/AnimatedTypewriterText.js.map +0 -1
- package/lib/esm/components/typewriter/Typewriter.styles.js.map +0 -1
- package/lib/esm/components/typewriter/utils.js.map +0 -1
- /package/lib/types/components/typewriter/{AnimatedTypewriterText.d.ts → typewrite-view/animated-typewriter-text/AnimatedTypewriterText.d.ts} +0 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _react = require("react");
|
|
8
|
+
var _utils = require("../utils/utils");
|
|
9
|
+
const getInitialChunkSpeed = (charactersCount, autoSpeedBaseFactor) => charactersCount / (autoSpeedBaseFactor / 1000);
|
|
10
|
+
const useChunkStreamingSpeed = ({
|
|
11
|
+
autoSpeedBaseFactor,
|
|
12
|
+
charactersCount,
|
|
13
|
+
shouldCalcAutoSpeed
|
|
14
|
+
}) => {
|
|
15
|
+
const autoSpeed = (0, _react.useRef)();
|
|
16
|
+
const chunkStreamingSpeed = (0, _react.useRef)({
|
|
17
|
+
lastLength: charactersCount,
|
|
18
|
+
ema: getInitialChunkSpeed(charactersCount, autoSpeedBaseFactor)
|
|
19
|
+
});
|
|
20
|
+
(0, _react.useEffect)(() => {
|
|
21
|
+
chunkStreamingSpeed.current = (0, _utils.updateChunkStreamingSpeedEMA)({
|
|
22
|
+
currentLength: charactersCount,
|
|
23
|
+
state: chunkStreamingSpeed.current
|
|
24
|
+
});
|
|
25
|
+
}, [autoSpeedBaseFactor, charactersCount]);
|
|
26
|
+
(0, _react.useEffect)(() => {
|
|
27
|
+
if (!shouldCalcAutoSpeed) {
|
|
28
|
+
autoSpeed.current = undefined;
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
autoSpeed.current = (0, _utils.getSafeAutoSpeed)(chunkStreamingSpeed.current.ema);
|
|
32
|
+
}, [charactersCount, shouldCalcAutoSpeed]);
|
|
33
|
+
return autoSpeed;
|
|
34
|
+
};
|
|
35
|
+
var _default = exports.default = useChunkStreamingSpeed;
|
|
36
|
+
//# sourceMappingURL=useChunkStreamingSpeed.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useChunkStreamingSpeed.js","names":["_react","require","_utils","getInitialChunkSpeed","charactersCount","autoSpeedBaseFactor","useChunkStreamingSpeed","shouldCalcAutoSpeed","autoSpeed","useRef","chunkStreamingSpeed","lastLength","ema","useEffect","current","updateChunkStreamingSpeedEMA","currentLength","state","undefined","getSafeAutoSpeed","_default","exports","default"],"sources":["../../../src/hooks/useChunkStreamingSpeed.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\nimport {\n ChunkStreamingSpeedState,\n getSafeAutoSpeed,\n updateChunkStreamingSpeedEMA,\n} from '../utils/utils';\n\ntype UseChunkStreamingSpeedProps = {\n autoSpeedBaseFactor: number;\n charactersCount: number;\n shouldCalcAutoSpeed: boolean;\n};\n\nconst getInitialChunkSpeed = (charactersCount: number, autoSpeedBaseFactor: number) =>\n charactersCount / (autoSpeedBaseFactor / 1000);\n\nconst useChunkStreamingSpeed = ({\n autoSpeedBaseFactor,\n charactersCount,\n shouldCalcAutoSpeed,\n}: UseChunkStreamingSpeedProps) => {\n const autoSpeed = useRef<number>();\n const chunkStreamingSpeed = useRef<ChunkStreamingSpeedState>({\n lastLength: charactersCount,\n ema: getInitialChunkSpeed(charactersCount, autoSpeedBaseFactor),\n });\n\n useEffect(() => {\n chunkStreamingSpeed.current = updateChunkStreamingSpeedEMA({\n currentLength: charactersCount,\n state: chunkStreamingSpeed.current,\n });\n }, [autoSpeedBaseFactor, charactersCount]);\n\n useEffect(() => {\n if (!shouldCalcAutoSpeed) {\n autoSpeed.current = undefined;\n return;\n }\n\n autoSpeed.current = getSafeAutoSpeed(chunkStreamingSpeed.current.ema);\n }, [charactersCount, shouldCalcAutoSpeed]);\n\n return autoSpeed;\n};\n\nexport default useChunkStreamingSpeed;\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AAYA,MAAME,oBAAoB,GAAGA,CAACC,eAAuB,EAAEC,mBAA2B,KAC9ED,eAAe,IAAIC,mBAAmB,GAAG,IAAI,CAAC;AAElD,MAAMC,sBAAsB,GAAGA,CAAC;EAC5BD,mBAAmB;EACnBD,eAAe;EACfG;AACyB,CAAC,KAAK;EAC/B,MAAMC,SAAS,GAAG,IAAAC,aAAM,EAAS,CAAC;EAClC,MAAMC,mBAAmB,GAAG,IAAAD,aAAM,EAA2B;IACzDE,UAAU,EAAEP,eAAe;IAC3BQ,GAAG,EAAET,oBAAoB,CAACC,eAAe,EAAEC,mBAAmB;EAClE,CAAC,CAAC;EAEF,IAAAQ,gBAAS,EAAC,MAAM;IACZH,mBAAmB,CAACI,OAAO,GAAG,IAAAC,mCAA4B,EAAC;MACvDC,aAAa,EAAEZ,eAAe;MAC9Ba,KAAK,EAAEP,mBAAmB,CAACI;IAC/B,CAAC,CAAC;EACN,CAAC,EAAE,CAACT,mBAAmB,EAAED,eAAe,CAAC,CAAC;EAE1C,IAAAS,gBAAS,EAAC,MAAM;IACZ,IAAI,CAACN,mBAAmB,EAAE;MACtBC,SAAS,CAACM,OAAO,GAAGI,SAAS;MAC7B;IACJ;IAEAV,SAAS,CAACM,OAAO,GAAG,IAAAK,uBAAgB,EAACT,mBAAmB,CAACI,OAAO,CAACF,GAAG,CAAC;EACzE,CAAC,EAAE,CAACR,eAAe,EAAEG,mBAAmB,CAAC,CAAC;EAE1C,OAAOC,SAAS;AACpB,CAAC;AAAC,IAAAY,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEahB,sBAAsB","ignoreList":[]}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _react = require("react");
|
|
8
|
+
var _cursor = require("../types/cursor");
|
|
9
|
+
var _useChunkStreamingSpeed = _interopRequireDefault(require("./useChunkStreamingSpeed"));
|
|
10
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
|
+
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? _react.useLayoutEffect : _react.useEffect;
|
|
12
|
+
const useTypewriterAnimation = ({
|
|
13
|
+
autoSpeedBaseFactor,
|
|
14
|
+
charactersCount,
|
|
15
|
+
childrenKey,
|
|
16
|
+
childrenCount,
|
|
17
|
+
currentTextLength,
|
|
18
|
+
cursorType,
|
|
19
|
+
nextTextDelay,
|
|
20
|
+
onAdvanceChild,
|
|
21
|
+
onFinish,
|
|
22
|
+
onResetAnimationEnd,
|
|
23
|
+
onResetAnimationStart,
|
|
24
|
+
onTypingAnimationEnd,
|
|
25
|
+
onTypingAnimationStart,
|
|
26
|
+
resetDelay,
|
|
27
|
+
resetSpeed,
|
|
28
|
+
shouldCalcAutoSpeed,
|
|
29
|
+
shouldForceCursorAnimation,
|
|
30
|
+
shouldUseResetAnimation,
|
|
31
|
+
shouldWaitForContent,
|
|
32
|
+
speed,
|
|
33
|
+
startDelay
|
|
34
|
+
}) => {
|
|
35
|
+
const [hasRenderedChildrenOnce, setHasRenderedChildrenOnce] = (0, _react.useState)(false);
|
|
36
|
+
const [shouldPreventBlinkingCursor, setShouldPreventBlinkingCursor] = (0, _react.useState)(false);
|
|
37
|
+
const [isResetAnimationActive, setIsResetAnimationActive] = (0, _react.useState)(false);
|
|
38
|
+
const [shouldStopAnimation, setShouldStopAnimation] = (0, _react.useState)(false);
|
|
39
|
+
const [shownCharCount, setShownCharCount] = (0, _react.useState)(charactersCount > 0 ? 0 : currentTextLength);
|
|
40
|
+
const autoSpeed = (0, _useChunkStreamingSpeed.default)({
|
|
41
|
+
autoSpeedBaseFactor,
|
|
42
|
+
charactersCount,
|
|
43
|
+
shouldCalcAutoSpeed
|
|
44
|
+
});
|
|
45
|
+
useIsomorphicLayoutEffect(() => {
|
|
46
|
+
setHasRenderedChildrenOnce(false);
|
|
47
|
+
}, [childrenKey]);
|
|
48
|
+
if (!hasRenderedChildrenOnce) setHasRenderedChildrenOnce(true);
|
|
49
|
+
const shouldShowFullTextImmediately = shouldStopAnimation || charactersCount === 0;
|
|
50
|
+
const effectiveShownCharCount = shouldShowFullTextImmediately ? currentTextLength : shownCharCount;
|
|
51
|
+
const isTypingAnimationActive = !shouldShowFullTextImmediately && (effectiveShownCharCount < currentTextLength || childrenCount > 1);
|
|
52
|
+
const isAnimatingText = isTypingAnimationActive || shouldForceCursorAnimation;
|
|
53
|
+
const handleClick = event => {
|
|
54
|
+
event.stopPropagation();
|
|
55
|
+
event.preventDefault();
|
|
56
|
+
setShouldStopAnimation(true);
|
|
57
|
+
};
|
|
58
|
+
(0, _react.useEffect)(() => {
|
|
59
|
+
let frameId;
|
|
60
|
+
let lastTimeRendered;
|
|
61
|
+
let accumulatedTime = 0;
|
|
62
|
+
let timeoutId;
|
|
63
|
+
const safeCancelFrame = () => {
|
|
64
|
+
if (typeof frameId === 'number') {
|
|
65
|
+
cancelAnimationFrame(frameId);
|
|
66
|
+
frameId = undefined;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
const safeClearTimeout = () => {
|
|
70
|
+
if (typeof timeoutId === 'number') {
|
|
71
|
+
clearTimeout(timeoutId);
|
|
72
|
+
timeoutId = undefined;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const startAnimationFrameLoop = (onTick, speedParam) => {
|
|
76
|
+
lastTimeRendered = undefined;
|
|
77
|
+
accumulatedTime = 0;
|
|
78
|
+
const loop = timestamp => {
|
|
79
|
+
if (lastTimeRendered === undefined) lastTimeRendered = timestamp;
|
|
80
|
+
const deltaTime = timestamp - lastTimeRendered;
|
|
81
|
+
accumulatedTime += deltaTime;
|
|
82
|
+
const rate = autoSpeed.current ?? speedParam;
|
|
83
|
+
const charactersToChange = Math.floor(accumulatedTime / rate);
|
|
84
|
+
if (charactersToChange === 0) {
|
|
85
|
+
lastTimeRendered = timestamp;
|
|
86
|
+
frameId = requestAnimationFrame(loop);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
onTick(charactersToChange);
|
|
90
|
+
accumulatedTime -= charactersToChange * rate;
|
|
91
|
+
lastTimeRendered = timestamp;
|
|
92
|
+
frameId = requestAnimationFrame(loop);
|
|
93
|
+
};
|
|
94
|
+
frameId = requestAnimationFrame(loop);
|
|
95
|
+
};
|
|
96
|
+
if (shouldShowFullTextImmediately) {
|
|
97
|
+
return () => {
|
|
98
|
+
safeCancelFrame();
|
|
99
|
+
safeClearTimeout();
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
if (isResetAnimationActive) {
|
|
103
|
+
if (typeof onResetAnimationStart === 'function') {
|
|
104
|
+
onResetAnimationStart();
|
|
105
|
+
}
|
|
106
|
+
startAnimationFrameLoop(charactersToRemove => {
|
|
107
|
+
setShownCharCount(prev => {
|
|
108
|
+
const nextShownCharCount = Math.max(0, prev - charactersToRemove);
|
|
109
|
+
if (nextShownCharCount <= 0) {
|
|
110
|
+
safeCancelFrame();
|
|
111
|
+
if (typeof onResetAnimationEnd === 'function') {
|
|
112
|
+
onResetAnimationEnd();
|
|
113
|
+
}
|
|
114
|
+
if (childrenCount > 1) {
|
|
115
|
+
timeoutId = window.setTimeout(() => {
|
|
116
|
+
setIsResetAnimationActive(false);
|
|
117
|
+
onAdvanceChild();
|
|
118
|
+
}, nextTextDelay);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return nextShownCharCount;
|
|
122
|
+
});
|
|
123
|
+
}, resetSpeed);
|
|
124
|
+
} else {
|
|
125
|
+
const startTypingAnimation = () => {
|
|
126
|
+
if (cursorType === _cursor.CursorType.Thin) {
|
|
127
|
+
setShouldPreventBlinkingCursor(true);
|
|
128
|
+
}
|
|
129
|
+
if (typeof onTypingAnimationStart === 'function') {
|
|
130
|
+
onTypingAnimationStart();
|
|
131
|
+
}
|
|
132
|
+
startAnimationFrameLoop(charactersToAdd => {
|
|
133
|
+
setShownCharCount(prevState => {
|
|
134
|
+
let nextState = prevState + charactersToAdd;
|
|
135
|
+
if (nextState >= charactersCount && !shouldWaitForContent) {
|
|
136
|
+
if (cursorType === _cursor.CursorType.Thin) {
|
|
137
|
+
setShouldPreventBlinkingCursor(false);
|
|
138
|
+
}
|
|
139
|
+
if (typeof onTypingAnimationEnd === 'function') {
|
|
140
|
+
onTypingAnimationEnd();
|
|
141
|
+
}
|
|
142
|
+
nextState = currentTextLength;
|
|
143
|
+
safeCancelFrame();
|
|
144
|
+
if (childrenCount > 1) {
|
|
145
|
+
timeoutId = window.setTimeout(() => {
|
|
146
|
+
if (shouldUseResetAnimation) {
|
|
147
|
+
setIsResetAnimationActive(true);
|
|
148
|
+
} else {
|
|
149
|
+
setShownCharCount(0);
|
|
150
|
+
timeoutId = window.setTimeout(onAdvanceChild, nextTextDelay);
|
|
151
|
+
}
|
|
152
|
+
}, resetDelay);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return nextState;
|
|
156
|
+
});
|
|
157
|
+
}, speed);
|
|
158
|
+
};
|
|
159
|
+
if (startDelay) {
|
|
160
|
+
timeoutId = window.setTimeout(startTypingAnimation, startDelay);
|
|
161
|
+
} else {
|
|
162
|
+
startTypingAnimation();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return () => {
|
|
166
|
+
safeCancelFrame();
|
|
167
|
+
safeClearTimeout();
|
|
168
|
+
};
|
|
169
|
+
}, [childrenCount, charactersCount, currentTextLength, cursorType, isResetAnimationActive, nextTextDelay, onAdvanceChild, onResetAnimationEnd, onResetAnimationStart, onTypingAnimationEnd, onTypingAnimationStart, resetDelay, resetSpeed, shouldShowFullTextImmediately, autoSpeed, shouldUseResetAnimation, shouldWaitForContent, speed, startDelay]);
|
|
170
|
+
(0, _react.useEffect)(() => {
|
|
171
|
+
if (!isTypingAnimationActive && typeof onFinish === 'function') {
|
|
172
|
+
onFinish();
|
|
173
|
+
}
|
|
174
|
+
}, [isTypingAnimationActive, onFinish]);
|
|
175
|
+
return {
|
|
176
|
+
effectiveShownCharCount,
|
|
177
|
+
handleClick,
|
|
178
|
+
hasRenderedChildrenOnce,
|
|
179
|
+
isAnimatingText,
|
|
180
|
+
isTypingAnimationActive,
|
|
181
|
+
shouldPreventBlinkingCursor
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
var _default = exports.default = useTypewriterAnimation;
|
|
185
|
+
//# sourceMappingURL=useTypewriterAnimation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTypewriterAnimation.js","names":["_react","require","_cursor","_useChunkStreamingSpeed","_interopRequireDefault","e","__esModule","default","useIsomorphicLayoutEffect","window","useLayoutEffect","useEffect","useTypewriterAnimation","autoSpeedBaseFactor","charactersCount","childrenKey","childrenCount","currentTextLength","cursorType","nextTextDelay","onAdvanceChild","onFinish","onResetAnimationEnd","onResetAnimationStart","onTypingAnimationEnd","onTypingAnimationStart","resetDelay","resetSpeed","shouldCalcAutoSpeed","shouldForceCursorAnimation","shouldUseResetAnimation","shouldWaitForContent","speed","startDelay","hasRenderedChildrenOnce","setHasRenderedChildrenOnce","useState","shouldPreventBlinkingCursor","setShouldPreventBlinkingCursor","isResetAnimationActive","setIsResetAnimationActive","shouldStopAnimation","setShouldStopAnimation","shownCharCount","setShownCharCount","autoSpeed","useChunkStreamingSpeed","shouldShowFullTextImmediately","effectiveShownCharCount","isTypingAnimationActive","isAnimatingText","handleClick","event","stopPropagation","preventDefault","frameId","lastTimeRendered","accumulatedTime","timeoutId","safeCancelFrame","cancelAnimationFrame","undefined","safeClearTimeout","clearTimeout","startAnimationFrameLoop","onTick","speedParam","loop","timestamp","deltaTime","rate","current","charactersToChange","Math","floor","requestAnimationFrame","charactersToRemove","prev","nextShownCharCount","max","setTimeout","startTypingAnimation","CursorType","Thin","charactersToAdd","prevState","nextState","_default","exports"],"sources":["../../../src/hooks/useTypewriterAnimation.ts"],"sourcesContent":["import type { MouseEvent } from 'react';\nimport { useEffect, useLayoutEffect, useState } from 'react';\nimport { CursorType } from '../types/cursor';\nimport useChunkStreamingSpeed from './useChunkStreamingSpeed';\n\nconst useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;\n\ntype UseTypewriterAnimationProps = {\n autoSpeedBaseFactor: number;\n charactersCount: number;\n childrenKey: unknown;\n childrenCount: number;\n currentTextLength: number;\n cursorType: CursorType;\n nextTextDelay: number;\n onAdvanceChild: VoidFunction;\n onFinish?: VoidFunction;\n onResetAnimationEnd?: VoidFunction;\n onResetAnimationStart?: VoidFunction;\n onTypingAnimationEnd?: VoidFunction;\n onTypingAnimationStart?: VoidFunction;\n resetDelay: number;\n resetSpeed: number;\n shouldCalcAutoSpeed: boolean;\n shouldForceCursorAnimation: boolean;\n shouldUseResetAnimation: boolean;\n shouldWaitForContent?: boolean;\n speed: number;\n startDelay: number;\n};\n\nexport type UseTypewriterAnimationResult = {\n effectiveShownCharCount: number;\n handleClick: (event: MouseEvent) => void;\n hasRenderedChildrenOnce: boolean;\n isAnimatingText: boolean;\n isTypingAnimationActive: boolean;\n shouldPreventBlinkingCursor: boolean;\n};\n\nconst useTypewriterAnimation = ({\n autoSpeedBaseFactor,\n charactersCount,\n childrenKey,\n childrenCount,\n currentTextLength,\n cursorType,\n nextTextDelay,\n onAdvanceChild,\n onFinish,\n onResetAnimationEnd,\n onResetAnimationStart,\n onTypingAnimationEnd,\n onTypingAnimationStart,\n resetDelay,\n resetSpeed,\n shouldCalcAutoSpeed,\n shouldForceCursorAnimation,\n shouldUseResetAnimation,\n shouldWaitForContent,\n speed,\n startDelay,\n}: UseTypewriterAnimationProps): UseTypewriterAnimationResult => {\n const [hasRenderedChildrenOnce, setHasRenderedChildrenOnce] = useState(false);\n const [shouldPreventBlinkingCursor, setShouldPreventBlinkingCursor] = useState(false);\n const [isResetAnimationActive, setIsResetAnimationActive] = useState(false);\n const [shouldStopAnimation, setShouldStopAnimation] = useState(false);\n const [shownCharCount, setShownCharCount] = useState(\n charactersCount > 0 ? 0 : currentTextLength,\n );\n const autoSpeed = useChunkStreamingSpeed({\n autoSpeedBaseFactor,\n charactersCount,\n shouldCalcAutoSpeed,\n });\n\n useIsomorphicLayoutEffect(() => {\n setHasRenderedChildrenOnce(false);\n }, [childrenKey]);\n\n if (!hasRenderedChildrenOnce) setHasRenderedChildrenOnce(true);\n\n const shouldShowFullTextImmediately = shouldStopAnimation || charactersCount === 0;\n const effectiveShownCharCount = shouldShowFullTextImmediately\n ? currentTextLength\n : shownCharCount;\n\n const isTypingAnimationActive =\n !shouldShowFullTextImmediately &&\n (effectiveShownCharCount < currentTextLength || childrenCount > 1);\n\n const isAnimatingText = isTypingAnimationActive || shouldForceCursorAnimation;\n\n const handleClick = (event: MouseEvent) => {\n event.stopPropagation();\n event.preventDefault();\n\n setShouldStopAnimation(true);\n };\n\n useEffect(() => {\n let frameId: number | undefined;\n let lastTimeRendered: number | undefined;\n let accumulatedTime = 0;\n let timeoutId: number | undefined;\n\n const safeCancelFrame = () => {\n if (typeof frameId === 'number') {\n cancelAnimationFrame(frameId);\n frameId = undefined;\n }\n };\n\n const safeClearTimeout = () => {\n if (typeof timeoutId === 'number') {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n };\n\n const startAnimationFrameLoop = (\n onTick: (charactersToChange: number) => void,\n speedParam: number,\n ) => {\n lastTimeRendered = undefined;\n accumulatedTime = 0;\n const loop = (timestamp: number) => {\n if (lastTimeRendered === undefined) lastTimeRendered = timestamp;\n const deltaTime = timestamp - lastTimeRendered;\n accumulatedTime += deltaTime;\n const rate = autoSpeed.current ?? speedParam;\n const charactersToChange = Math.floor(accumulatedTime / rate);\n\n if (charactersToChange === 0) {\n lastTimeRendered = timestamp;\n frameId = requestAnimationFrame(loop);\n return;\n }\n\n onTick(charactersToChange);\n accumulatedTime -= charactersToChange * rate;\n\n lastTimeRendered = timestamp;\n frameId = requestAnimationFrame(loop);\n };\n\n frameId = requestAnimationFrame(loop);\n };\n\n if (shouldShowFullTextImmediately) {\n return () => {\n safeCancelFrame();\n safeClearTimeout();\n };\n }\n\n if (isResetAnimationActive) {\n if (typeof onResetAnimationStart === 'function') {\n onResetAnimationStart();\n }\n\n startAnimationFrameLoop((charactersToRemove) => {\n setShownCharCount((prev) => {\n const nextShownCharCount = Math.max(0, prev - charactersToRemove);\n\n if (nextShownCharCount <= 0) {\n safeCancelFrame();\n\n if (typeof onResetAnimationEnd === 'function') {\n onResetAnimationEnd();\n }\n\n if (childrenCount > 1) {\n timeoutId = window.setTimeout(() => {\n setIsResetAnimationActive(false);\n onAdvanceChild();\n }, nextTextDelay);\n }\n }\n\n return nextShownCharCount;\n });\n }, resetSpeed);\n } else {\n const startTypingAnimation = () => {\n if (cursorType === CursorType.Thin) {\n setShouldPreventBlinkingCursor(true);\n }\n\n if (typeof onTypingAnimationStart === 'function') {\n onTypingAnimationStart();\n }\n\n startAnimationFrameLoop((charactersToAdd) => {\n setShownCharCount((prevState) => {\n let nextState = prevState + charactersToAdd;\n\n if (nextState >= charactersCount && !shouldWaitForContent) {\n if (cursorType === CursorType.Thin) {\n setShouldPreventBlinkingCursor(false);\n }\n\n if (typeof onTypingAnimationEnd === 'function') {\n onTypingAnimationEnd();\n }\n\n nextState = currentTextLength;\n\n safeCancelFrame();\n\n if (childrenCount > 1) {\n timeoutId = window.setTimeout(() => {\n if (shouldUseResetAnimation) {\n setIsResetAnimationActive(true);\n } else {\n setShownCharCount(0);\n timeoutId = window.setTimeout(\n onAdvanceChild,\n nextTextDelay,\n );\n }\n }, resetDelay);\n }\n }\n\n return nextState;\n });\n }, speed);\n };\n\n if (startDelay) {\n timeoutId = window.setTimeout(startTypingAnimation, startDelay);\n } else {\n startTypingAnimation();\n }\n }\n\n return () => {\n safeCancelFrame();\n safeClearTimeout();\n };\n }, [\n childrenCount,\n charactersCount,\n currentTextLength,\n cursorType,\n isResetAnimationActive,\n nextTextDelay,\n onAdvanceChild,\n onResetAnimationEnd,\n onResetAnimationStart,\n onTypingAnimationEnd,\n onTypingAnimationStart,\n resetDelay,\n resetSpeed,\n shouldShowFullTextImmediately,\n autoSpeed,\n shouldUseResetAnimation,\n shouldWaitForContent,\n speed,\n startDelay,\n ]);\n\n useEffect(() => {\n if (!isTypingAnimationActive && typeof onFinish === 'function') {\n onFinish();\n }\n }, [isTypingAnimationActive, onFinish]);\n\n return {\n effectiveShownCharCount,\n handleClick,\n hasRenderedChildrenOnce,\n isAnimatingText,\n isTypingAnimationActive,\n shouldPreventBlinkingCursor,\n };\n};\n\nexport default useTypewriterAnimation;\n"],"mappings":";;;;;;AACA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,OAAA,GAAAD,OAAA;AACA,IAAAE,uBAAA,GAAAC,sBAAA,CAAAH,OAAA;AAA8D,SAAAG,uBAAAC,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EA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,MAAM;QACHgD,oBAAoB,CAAC,CAAC;MAC1B;IACJ;IAEA,OAAO,MAAM;MACTtB,eAAe,CAAC,CAAC;MACjBG,gBAAgB,CAAC,CAAC;IACtB,CAAC;EACL,CAAC,EAAE,CACC9C,aAAa,EACbF,eAAe,EACfG,iBAAiB,EACjBC,UAAU,EACVqB,sBAAsB,EACtBpB,aAAa,EACbC,cAAc,EACdE,mBAAmB,EACnBC,qBAAqB,EACrBC,oBAAoB,EACpBC,sBAAsB,EACtBC,UAAU,EACVC,UAAU,EACVoB,6BAA6B,EAC7BF,SAAS,EACTf,uBAAuB,EACvBC,oBAAoB,EACpBC,KAAK,EACLC,UAAU,CACb,CAAC;EAEF,IAAAtB,gBAAS,EAAC,MAAM;IACZ,IAAI,CAACsC,uBAAuB,IAAI,OAAO5B,QAAQ,KAAK,UAAU,EAAE;MAC5DA,QAAQ,CAAC,CAAC;IACd;EACJ,CAAC,EAAE,CAAC4B,uBAAuB,EAAE5B,QAAQ,CAAC,CAAC;EAEvC,OAAO;IACH2B,uBAAuB;IACvBG,WAAW;IACXjB,uBAAuB;IACvBgB,eAAe;IACfD,uBAAuB;IACvBZ;EACJ,CAAC;AACL,CAAC;AAAC,IAAAkD,QAAA,GAAAC,OAAA,CAAAjF,OAAA,GAEaK,sBAAsB","ignoreList":[]}
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.updateChunkStreamingSpeedEMA = exports.shuffleArray = exports.getSubTextFromHTML = exports.
|
|
7
|
-
var _speed = require("
|
|
6
|
+
exports.updateChunkStreamingSpeedEMA = exports.shuffleArray = exports.getSubTextFromHTML = exports.getSafeAutoSpeed = exports.getCharactersCount = exports.calculateEMA = void 0;
|
|
7
|
+
var _speed = require("../types/speed");
|
|
8
8
|
/**
|
|
9
9
|
* Returns a substring of an HTML string while preserving HTML structure.
|
|
10
10
|
*
|
|
@@ -156,34 +156,17 @@ const shuffleArray = array => {
|
|
|
156
156
|
return result;
|
|
157
157
|
};
|
|
158
158
|
exports.shuffleArray = shuffleArray;
|
|
159
|
-
const
|
|
160
|
-
// nested timer calls are clamped to a 4ms minimum
|
|
161
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout#reasons_for_longer_delays_than_specified
|
|
162
|
-
const MINIMUM_TIMEOUT = 4;
|
|
159
|
+
const getSafeAutoSpeed = ema => {
|
|
163
160
|
if (ema <= 0) {
|
|
164
|
-
return
|
|
165
|
-
speed: _speed.TypewriterSpeed.ExtraSlow,
|
|
166
|
-
steps: 1
|
|
167
|
-
};
|
|
161
|
+
return _speed.TypewriterSpeed.ExtraSlow;
|
|
168
162
|
}
|
|
169
|
-
|
|
170
|
-
if (msPerChar >= MINIMUM_TIMEOUT) {
|
|
171
|
-
return {
|
|
172
|
-
speed: msPerChar,
|
|
173
|
-
steps: 1
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
const steps = Math.max(1, MINIMUM_TIMEOUT / msPerChar);
|
|
177
|
-
return {
|
|
178
|
-
speed: MINIMUM_TIMEOUT,
|
|
179
|
-
steps
|
|
180
|
-
};
|
|
163
|
+
return 1000 / ema;
|
|
181
164
|
};
|
|
182
|
-
exports.
|
|
165
|
+
exports.getSafeAutoSpeed = getSafeAutoSpeed;
|
|
183
166
|
const calculateEMA = ({
|
|
184
167
|
currentEMA,
|
|
185
168
|
newValue,
|
|
186
|
-
alpha = 0.
|
|
169
|
+
alpha = 0.75
|
|
187
170
|
}) => alpha * newValue + (1 - alpha) * currentEMA;
|
|
188
171
|
exports.calculateEMA = calculateEMA;
|
|
189
172
|
const updateChunkStreamingSpeedEMA = ({
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","names":["_speed","require","getSubTextFromHTML","html","length","div","document","createElement","innerHTML","text","currLength","escapeText","value","replace","escapeAttr","String","VOID_ELEMENTS","Set","traverse","node","nodeType","textContent","nodeText","remaining","substring","element","nodeName","toLowerCase","attributes","attribute","name","isVoid","has","i","childNodes","childNode","exports","getCharactersCount","count","trim","Array","from","forEach","shuffleArray","array","result","j","Math","floor","random","getSafeAutoSpeed","ema","TypewriterSpeed","ExtraSlow","calculateEMA","currentEMA","newValue","alpha","updateChunkStreamingSpeedEMA","currentLength","state","now","Date","deltaTime","lastTimestamp","deltaLength","lastLength","charsPerSecond","max","newEMA"],"sources":["../../../src/utils/utils.ts"],"sourcesContent":["import { TypewriterSpeed } from '../types/speed';\n\n/**\n * Returns a substring of an HTML string while preserving HTML structure.\n *\n * Core rules:\n * - Element nodes are re-serialized as tags (start/end) to keep structure.\n * - Text nodes are always HTML-escaped on output. This prevents that previously\n * escaped text (like \"<div>\") turns into real tags during the DOM round trip.\n * - Attribute values are HTML-escaped on output.\n * - Void elements are serialized without closing tags.\n * - For TWIGNORE/TW-IGNORE elements, the innerHTML is passed through so that\n * their content (including real HTML) remains untouched.\n * - On early cutoff (once the length limit is reached), already opened tags are\n * properly closed to keep the result valid HTML.\n *\n * Note on length counting:\n * - The length is based on the decoded textContent length (as the DOM provides),\n * not on byte length nor escaped entity length. This mirrors how the text is perceived.\n *\n * @param html The input HTML string; may contain a mix of real HTML and already escaped HTML.\n * @param length The maximum number of text characters (based on textContent) to include.\n * @returns A valid HTML string containing up to the specified number of text characters,\n * preserving HTML tags and keeping escaped text escaped.\n */\nexport const getSubTextFromHTML = (html: string, length: number): string => {\n const div = document.createElement('div');\n\n div.innerHTML = html;\n\n let text = '';\n let currLength = 0;\n\n // Escape text node content to ensure that decoded \"<\" and \">\" do not become real tags.\n const escapeText = (value: string): string =>\n value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');\n\n // Escape attribute values safely.\n const escapeAttr = (value: string): string =>\n String(value)\n .replace(/&/g, '&')\n .replace(/\"/g, '"')\n .replace(/</g, '<')\n .replace(/>/g, '>');\n\n // HTML void elements (must not have closing tags)\n const VOID_ELEMENTS = new Set([\n 'area',\n 'base',\n 'br',\n 'col',\n 'embed',\n 'hr',\n 'img',\n 'input',\n 'link',\n 'meta',\n 'param',\n 'source',\n 'track',\n 'wbr',\n ]);\n\n // Traverses nodes and appends to \"text\".\n // Returns false to signal \"stop traversal\" once the length limit is reached.\n const traverse = (node: Node): boolean => {\n // Text node\n if (node.nodeType === 3 && typeof node.textContent === 'string') {\n const nodeText = node.textContent;\n const remaining = length - currLength;\n\n if (remaining <= 0) {\n return false;\n }\n\n if (nodeText.length <= remaining) {\n // Always escape text before writing to output\n text += escapeText(nodeText);\n currLength += nodeText.length;\n } else {\n // Cut the text and stop traversal\n text += escapeText(nodeText.substring(0, remaining));\n currLength += remaining;\n return false;\n }\n\n return true;\n }\n\n // Element node\n if (node.nodeType === 1) {\n const element = node as Element;\n\n // Pass-through for TWIGNORE/TW-IGNORE: keep their HTML as-is.\n if (element.nodeName === 'TWIGNORE' || element.nodeName === 'TW-IGNORE') {\n // element.innerHTML serializes children; escaped text stays escaped,\n // real HTML stays HTML — exactly what we want here.\n text += element.innerHTML;\n return true;\n }\n\n const nodeName = element.nodeName.toLowerCase();\n\n // Serialize attributes safely\n let attributes = '';\n // @ts-expect-error: attributes is a NodeListOf<Attr>\n // eslint-disable-next-line no-restricted-syntax\n for (const attribute of element.attributes) {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/restrict-template-expressions,@typescript-eslint/no-unsafe-argument\n attributes += ` ${attribute.name}=\"${escapeAttr(attribute.value)}\"`;\n }\n\n // Open tag\n text += `<${nodeName}${attributes}>`;\n\n // Void elements: do not recurse children and do not emit a closing tag\n const isVoid = VOID_ELEMENTS.has(nodeName);\n if (!isVoid) {\n // Recurse through children until limit is reached\n for (let i = 0; i < element.childNodes.length; i++) {\n const childNode = element.childNodes[i];\n if (childNode && !traverse(childNode)) {\n // On early stop: close this tag to keep valid HTML, then bubble stop.\n text += `</${nodeName}>`;\n return false;\n }\n }\n\n // Close tag after all children\n text += `</${nodeName}>`;\n }\n\n return true;\n }\n\n // Other node types (comments, etc.) are ignored for text length\n return true;\n };\n\n // Traverse top-level children\n for (let i = 0; i < div.childNodes.length; i++) {\n const childNode = div.childNodes[i];\n if (childNode && !traverse(childNode)) {\n return text;\n }\n }\n\n return text;\n};\n\nexport const getCharactersCount = (html: string): number => {\n const div = document.createElement('div');\n\n div.innerHTML = html;\n\n let count = 0;\n\n const traverse = (node: Node): void => {\n if (node.nodeName === 'TWIGNORE') {\n count += 1;\n } else if (node.nodeType === 3 && typeof node.textContent === 'string') {\n count += node.textContent.trim().length;\n } else if (node.nodeType === 1) {\n if (node.nodeName === 'CODE' && node.textContent !== null) {\n count += node.textContent.length;\n\n return;\n }\n\n Array.from(node.childNodes).forEach(traverse);\n }\n };\n\n Array.from(div.childNodes).forEach(traverse);\n\n return count;\n};\n\nexport const shuffleArray = <T>(array: T[]): T[] => {\n const result = Array.from(array);\n\n for (let i = result.length - 1; i > 0; i--) {\n const j = Math.floor(Math.random() * (i + 1));\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n [result[i], result[j]] = [result[j]!, result[i]!];\n }\n\n return result;\n};\n\nexport const getSafeAutoSpeed = (ema: number): number => {\n if (ema <= 0) {\n return TypewriterSpeed.ExtraSlow;\n }\n\n return 1000 / ema;\n};\n\ninterface CalculateEMAProps {\n currentEMA: number;\n newValue: number;\n alpha?: number;\n}\n\nexport const calculateEMA = ({ currentEMA, newValue, alpha = 0.75 }: CalculateEMAProps): number =>\n alpha * newValue + (1 - alpha) * currentEMA;\n\nexport interface ChunkStreamingSpeedState {\n lastTimestamp?: number;\n lastLength: number;\n ema: number;\n}\n\ninterface ChunkStreamingSpeedProps {\n currentLength: number;\n state: ChunkStreamingSpeedState;\n}\n\nexport const updateChunkStreamingSpeedEMA = ({\n currentLength,\n state,\n}: ChunkStreamingSpeedProps): ChunkStreamingSpeedState => {\n const now = Date.now();\n const deltaTime = now - (state?.lastTimestamp ?? now);\n\n if (deltaTime <= 0) return { ...state, lastTimestamp: now };\n\n const deltaLength = currentLength - state.lastLength;\n\n const charsPerSecond = Math.max(0, (deltaLength / deltaTime) * 1000);\n\n const newEMA = calculateEMA({\n currentEMA: state.ema,\n newValue: charsPerSecond,\n });\n\n return {\n lastTimestamp: now,\n lastLength: currentLength,\n ema: newEMA,\n };\n};\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMC,kBAAkB,GAAGA,CAACC,IAAY,EAAEC,MAAc,KAAa;EACxE,MAAMC,GAAG,GAAGC,QAAQ,CAACC,aAAa,CAAC,KAAK,CAAC;EAEzCF,GAAG,CAACG,SAAS,GAAGL,IAAI;EAEpB,IAAIM,IAAI,GAAG,EAAE;EACb,IAAIC,UAAU,GAAG,CAAC;;EAElB;EACA,MAAMC,UAAU,GAAIC,KAAa,IAC7BA,KAAK,CAACC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAACA,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAACA,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;;EAE5E;EACA,MAAMC,UAAU,GAAIF,KAAa,IAC7BG,MAAM,CAACH,KAAK,CAAC,CACRC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CACtBA,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CACvBA,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CACrBA,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;;EAE9B;EACA,MAAMG,aAAa,GAAG,IAAIC,GAAG,CAAC,CAC1B,MAAM,EACN,MAAM,EACN,IAAI,EACJ,KAAK,EACL,OAAO,EACP,IAAI,EACJ,KAAK,EACL,OAAO,EACP,MAAM,EACN,MAAM,EACN,OAAO,EACP,QAAQ,EACR,OAAO,EACP,KAAK,CACR,CAAC;;EAEF;EACA;EACA,MAA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