@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,178 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect, useState } from 'react';
|
|
2
|
+
import { CursorType } from '../types/cursor';
|
|
3
|
+
import useChunkStreamingSpeed from './useChunkStreamingSpeed';
|
|
4
|
+
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
|
|
5
|
+
const useTypewriterAnimation = ({
|
|
6
|
+
autoSpeedBaseFactor,
|
|
7
|
+
charactersCount,
|
|
8
|
+
childrenKey,
|
|
9
|
+
childrenCount,
|
|
10
|
+
currentTextLength,
|
|
11
|
+
cursorType,
|
|
12
|
+
nextTextDelay,
|
|
13
|
+
onAdvanceChild,
|
|
14
|
+
onFinish,
|
|
15
|
+
onResetAnimationEnd,
|
|
16
|
+
onResetAnimationStart,
|
|
17
|
+
onTypingAnimationEnd,
|
|
18
|
+
onTypingAnimationStart,
|
|
19
|
+
resetDelay,
|
|
20
|
+
resetSpeed,
|
|
21
|
+
shouldCalcAutoSpeed,
|
|
22
|
+
shouldForceCursorAnimation,
|
|
23
|
+
shouldUseResetAnimation,
|
|
24
|
+
shouldWaitForContent,
|
|
25
|
+
speed,
|
|
26
|
+
startDelay
|
|
27
|
+
}) => {
|
|
28
|
+
const [hasRenderedChildrenOnce, setHasRenderedChildrenOnce] = useState(false);
|
|
29
|
+
const [shouldPreventBlinkingCursor, setShouldPreventBlinkingCursor] = useState(false);
|
|
30
|
+
const [isResetAnimationActive, setIsResetAnimationActive] = useState(false);
|
|
31
|
+
const [shouldStopAnimation, setShouldStopAnimation] = useState(false);
|
|
32
|
+
const [shownCharCount, setShownCharCount] = useState(charactersCount > 0 ? 0 : currentTextLength);
|
|
33
|
+
const autoSpeed = useChunkStreamingSpeed({
|
|
34
|
+
autoSpeedBaseFactor,
|
|
35
|
+
charactersCount,
|
|
36
|
+
shouldCalcAutoSpeed
|
|
37
|
+
});
|
|
38
|
+
useIsomorphicLayoutEffect(() => {
|
|
39
|
+
setHasRenderedChildrenOnce(false);
|
|
40
|
+
}, [childrenKey]);
|
|
41
|
+
if (!hasRenderedChildrenOnce) setHasRenderedChildrenOnce(true);
|
|
42
|
+
const shouldShowFullTextImmediately = shouldStopAnimation || charactersCount === 0;
|
|
43
|
+
const effectiveShownCharCount = shouldShowFullTextImmediately ? currentTextLength : shownCharCount;
|
|
44
|
+
const isTypingAnimationActive = !shouldShowFullTextImmediately && (effectiveShownCharCount < currentTextLength || childrenCount > 1);
|
|
45
|
+
const isAnimatingText = isTypingAnimationActive || shouldForceCursorAnimation;
|
|
46
|
+
const handleClick = event => {
|
|
47
|
+
event.stopPropagation();
|
|
48
|
+
event.preventDefault();
|
|
49
|
+
setShouldStopAnimation(true);
|
|
50
|
+
};
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
let frameId;
|
|
53
|
+
let lastTimeRendered;
|
|
54
|
+
let accumulatedTime = 0;
|
|
55
|
+
let timeoutId;
|
|
56
|
+
const safeCancelFrame = () => {
|
|
57
|
+
if (typeof frameId === 'number') {
|
|
58
|
+
cancelAnimationFrame(frameId);
|
|
59
|
+
frameId = undefined;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
const safeClearTimeout = () => {
|
|
63
|
+
if (typeof timeoutId === 'number') {
|
|
64
|
+
clearTimeout(timeoutId);
|
|
65
|
+
timeoutId = undefined;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const startAnimationFrameLoop = (onTick, speedParam) => {
|
|
69
|
+
lastTimeRendered = undefined;
|
|
70
|
+
accumulatedTime = 0;
|
|
71
|
+
const loop = timestamp => {
|
|
72
|
+
if (lastTimeRendered === undefined) lastTimeRendered = timestamp;
|
|
73
|
+
const deltaTime = timestamp - lastTimeRendered;
|
|
74
|
+
accumulatedTime += deltaTime;
|
|
75
|
+
const rate = autoSpeed.current ?? speedParam;
|
|
76
|
+
const charactersToChange = Math.floor(accumulatedTime / rate);
|
|
77
|
+
if (charactersToChange === 0) {
|
|
78
|
+
lastTimeRendered = timestamp;
|
|
79
|
+
frameId = requestAnimationFrame(loop);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
onTick(charactersToChange);
|
|
83
|
+
accumulatedTime -= charactersToChange * rate;
|
|
84
|
+
lastTimeRendered = timestamp;
|
|
85
|
+
frameId = requestAnimationFrame(loop);
|
|
86
|
+
};
|
|
87
|
+
frameId = requestAnimationFrame(loop);
|
|
88
|
+
};
|
|
89
|
+
if (shouldShowFullTextImmediately) {
|
|
90
|
+
return () => {
|
|
91
|
+
safeCancelFrame();
|
|
92
|
+
safeClearTimeout();
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
if (isResetAnimationActive) {
|
|
96
|
+
if (typeof onResetAnimationStart === 'function') {
|
|
97
|
+
onResetAnimationStart();
|
|
98
|
+
}
|
|
99
|
+
startAnimationFrameLoop(charactersToRemove => {
|
|
100
|
+
setShownCharCount(prev => {
|
|
101
|
+
const nextShownCharCount = Math.max(0, prev - charactersToRemove);
|
|
102
|
+
if (nextShownCharCount <= 0) {
|
|
103
|
+
safeCancelFrame();
|
|
104
|
+
if (typeof onResetAnimationEnd === 'function') {
|
|
105
|
+
onResetAnimationEnd();
|
|
106
|
+
}
|
|
107
|
+
if (childrenCount > 1) {
|
|
108
|
+
timeoutId = window.setTimeout(() => {
|
|
109
|
+
setIsResetAnimationActive(false);
|
|
110
|
+
onAdvanceChild();
|
|
111
|
+
}, nextTextDelay);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return nextShownCharCount;
|
|
115
|
+
});
|
|
116
|
+
}, resetSpeed);
|
|
117
|
+
} else {
|
|
118
|
+
const startTypingAnimation = () => {
|
|
119
|
+
if (cursorType === CursorType.Thin) {
|
|
120
|
+
setShouldPreventBlinkingCursor(true);
|
|
121
|
+
}
|
|
122
|
+
if (typeof onTypingAnimationStart === 'function') {
|
|
123
|
+
onTypingAnimationStart();
|
|
124
|
+
}
|
|
125
|
+
startAnimationFrameLoop(charactersToAdd => {
|
|
126
|
+
setShownCharCount(prevState => {
|
|
127
|
+
let nextState = prevState + charactersToAdd;
|
|
128
|
+
if (nextState >= charactersCount && !shouldWaitForContent) {
|
|
129
|
+
if (cursorType === CursorType.Thin) {
|
|
130
|
+
setShouldPreventBlinkingCursor(false);
|
|
131
|
+
}
|
|
132
|
+
if (typeof onTypingAnimationEnd === 'function') {
|
|
133
|
+
onTypingAnimationEnd();
|
|
134
|
+
}
|
|
135
|
+
nextState = currentTextLength;
|
|
136
|
+
safeCancelFrame();
|
|
137
|
+
if (childrenCount > 1) {
|
|
138
|
+
timeoutId = window.setTimeout(() => {
|
|
139
|
+
if (shouldUseResetAnimation) {
|
|
140
|
+
setIsResetAnimationActive(true);
|
|
141
|
+
} else {
|
|
142
|
+
setShownCharCount(0);
|
|
143
|
+
timeoutId = window.setTimeout(onAdvanceChild, nextTextDelay);
|
|
144
|
+
}
|
|
145
|
+
}, resetDelay);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return nextState;
|
|
149
|
+
});
|
|
150
|
+
}, speed);
|
|
151
|
+
};
|
|
152
|
+
if (startDelay) {
|
|
153
|
+
timeoutId = window.setTimeout(startTypingAnimation, startDelay);
|
|
154
|
+
} else {
|
|
155
|
+
startTypingAnimation();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return () => {
|
|
159
|
+
safeCancelFrame();
|
|
160
|
+
safeClearTimeout();
|
|
161
|
+
};
|
|
162
|
+
}, [childrenCount, charactersCount, currentTextLength, cursorType, isResetAnimationActive, nextTextDelay, onAdvanceChild, onResetAnimationEnd, onResetAnimationStart, onTypingAnimationEnd, onTypingAnimationStart, resetDelay, resetSpeed, shouldShowFullTextImmediately, autoSpeed, shouldUseResetAnimation, shouldWaitForContent, speed, startDelay]);
|
|
163
|
+
useEffect(() => {
|
|
164
|
+
if (!isTypingAnimationActive && typeof onFinish === 'function') {
|
|
165
|
+
onFinish();
|
|
166
|
+
}
|
|
167
|
+
}, [isTypingAnimationActive, onFinish]);
|
|
168
|
+
return {
|
|
169
|
+
effectiveShownCharCount,
|
|
170
|
+
handleClick,
|
|
171
|
+
hasRenderedChildrenOnce,
|
|
172
|
+
isAnimatingText,
|
|
173
|
+
isTypingAnimationActive,
|
|
174
|
+
shouldPreventBlinkingCursor
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
export default useTypewriterAnimation;
|
|
178
|
+
//# sourceMappingURL=useTypewriterAnimation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTypewriterAnimation.js","names":["useEffect","useLayoutEffect","useState","CursorType","useChunkStreamingSpeed","useIsomorphicLayoutEffect","window","useTypewriterAnimation","autoSpeedBaseFactor","charactersCount","childrenKey","childrenCount","currentTextLength","cursorType","nextTextDelay","onAdvanceChild","onFinish","onResetAnimationEnd","onResetAnimationStart","onTypingAnimationEnd","onTypingAnimationStart","resetDelay","resetSpeed","shouldCalcAutoSpeed","shouldForceCursorAnimation","shouldUseResetAnimation","shouldWaitForContent","speed","startDelay","hasRenderedChildrenOnce","setHasRenderedChildrenOnce","shouldPreventBlinkingCursor","setShouldPreventBlinkingCursor","isResetAnimationActive","setIsResetAnimationActive","shouldStopAnimation","setShouldStopAnimation","shownCharCount","setShownCharCount","autoSpeed","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","Thin","charactersToAdd","prevState","nextState"],"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,SAASA,SAAS,EAAEC,eAAe,EAAEC,QAAQ,QAAQ,OAAO;AAC5D,SAASC,UAAU,QAAQ,iBAAiB;AAC5C,OAAOC,sBAAsB,MAAM,0BAA0B;AAE7D,MAAMC,yBAAyB,GAAG,OAAOC,MAAM,KAAK,WAAW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B,CAAC,CAAC;IACtB,CAAC;EACL,CAAC,EAAE,CACC5C,aAAa,EACbF,eAAe,EACfG,iBAAiB,EACjBC,UAAU,EACVoB,sBAAsB,EACtBnB,aAAa,EACbC,cAAc,EACdE,mBAAmB,EACnBC,qBAAqB,EACrBC,oBAAoB,EACpBC,sBAAsB,EACtBC,UAAU,EACVC,UAAU,EACVkB,6BAA6B,EAC7BD,SAAS,EACTd,uBAAuB,EACvBC,oBAAoB,EACpBC,KAAK,EACLC,UAAU,CACb,CAAC;EAEF5B,SAAS,CAAC,MAAM;IACZ,IAAI,CAAC0C,uBAAuB,IAAI,OAAO1B,QAAQ,KAAK,UAAU,EAAE;MAC5DA,QAAQ,CAAC,CAAC;IACd;EACJ,CAAC,EAAE,CAAC0B,uBAAuB,EAAE1B,QAAQ,CAAC,CAAC;EAEvC,OAAO;IACHyB,uBAAuB;IACvBG,WAAW;IACXf,uBAAuB;IACvBc,eAAe;IACfD,uBAAuB;IACvBX;EACJ,CAAC;AACL,CAAC;AAED,eAAexB,sBAAsB","ignoreList":[]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TypewriterSpeed } from '
|
|
1
|
+
import { TypewriterSpeed } from '../types/speed';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Returns a substring of an HTML string while preserving HTML structure.
|
|
@@ -148,33 +148,16 @@ export const shuffleArray = array => {
|
|
|
148
148
|
}
|
|
149
149
|
return result;
|
|
150
150
|
};
|
|
151
|
-
export const
|
|
152
|
-
// nested timer calls are clamped to a 4ms minimum
|
|
153
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout#reasons_for_longer_delays_than_specified
|
|
154
|
-
const MINIMUM_TIMEOUT = 4;
|
|
151
|
+
export const getSafeAutoSpeed = ema => {
|
|
155
152
|
if (ema <= 0) {
|
|
156
|
-
return
|
|
157
|
-
speed: TypewriterSpeed.ExtraSlow,
|
|
158
|
-
steps: 1
|
|
159
|
-
};
|
|
153
|
+
return TypewriterSpeed.ExtraSlow;
|
|
160
154
|
}
|
|
161
|
-
|
|
162
|
-
if (msPerChar >= MINIMUM_TIMEOUT) {
|
|
163
|
-
return {
|
|
164
|
-
speed: msPerChar,
|
|
165
|
-
steps: 1
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
const steps = Math.max(1, MINIMUM_TIMEOUT / msPerChar);
|
|
169
|
-
return {
|
|
170
|
-
speed: MINIMUM_TIMEOUT,
|
|
171
|
-
steps
|
|
172
|
-
};
|
|
155
|
+
return 1000 / ema;
|
|
173
156
|
};
|
|
174
157
|
export const calculateEMA = ({
|
|
175
158
|
currentEMA,
|
|
176
159
|
newValue,
|
|
177
|
-
alpha = 0.
|
|
160
|
+
alpha = 0.75
|
|
178
161
|
}) => alpha * newValue + (1 - alpha) * currentEMA;
|
|
179
162
|
export const updateChunkStreamingSpeedEMA = ({
|
|
180
163
|
currentLength,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","names":["TypewriterSpeed","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","getCharactersCount","count","trim","Array","from","forEach","shuffleArray","array","result","j","Math","floor","random","getSafeAutoSpeed","ema","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,SAASA,eAAe,QAAQ,gBAAgB;;AAEhD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,kBAAkB,GAAGA,CAACC,IAAY,EAAEC,MAAc,KAAa;EACxE,MAAMC,GAAG,GAAGC,QAAQ,CAACC,aAAa,CAAC,KAAK,CAAC;EAEzCF,GAAG,CAACG,SAAS,GAAGL,IAA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D,MAAMI,WAAW,GAAGN,aAAa,GAAGC,KAAK,CAACM,UAAU;EAEpD,MAAMC,cAAc,GAAGnB,IAAI,CAACoB,GAAG,CAAC,CAAC,EAAGH,WAAW,GAAGF,SAAS,GAAI,IAAI,CAAC;EAEpE,MAAMM,MAAM,GAAGf,YAAY,CAAC;IACxBC,UAAU,EAAEK,KAAK,CAACR,GAAG;IACrBI,QAAQ,EAAEW;EACd,CAAC,CAAC;EAEF,OAAO;IACHH,aAAa,EAAEH,GAAG;IAClBK,UAAU,EAAEP,aAAa;IACzBP,GAAG,EAAEiB;EACT,CAAC;AACL,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React, { FC, ReactElement } from 'react';
|
|
2
|
+
import { CSSPropertiesWithVars } from 'styled-components/dist/types';
|
|
3
|
+
import { CursorType } from '../../../types/cursor';
|
|
4
|
+
export type TypewriterContent = ReactElement | string | Array<ReactElement | string>;
|
|
5
|
+
type TypewriterViewProps = {
|
|
6
|
+
children: TypewriterContent;
|
|
7
|
+
cursorType: CursorType;
|
|
8
|
+
handleClick?: (event: React.MouseEvent) => void;
|
|
9
|
+
hasRenderedChildrenOnce: boolean;
|
|
10
|
+
isAnimatingText: boolean;
|
|
11
|
+
pseudoTextHTML: string;
|
|
12
|
+
shouldHideCursor: boolean;
|
|
13
|
+
shouldPreventBlinkingCursor: boolean;
|
|
14
|
+
shouldRemainSingleLine: boolean;
|
|
15
|
+
shownText: string;
|
|
16
|
+
textStyle?: CSSPropertiesWithVars;
|
|
17
|
+
};
|
|
18
|
+
declare const TypewriterView: FC<TypewriterViewProps>;
|
|
19
|
+
export default TypewriterView;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { WithTheme } from '@chayns-components/core';
|
|
2
|
-
import type { TypewriterProps } from '
|
|
2
|
+
import type { TypewriterProps } from '../Typewriter';
|
|
3
3
|
type StyledTypewriterProps = WithTheme<{
|
|
4
4
|
$cursorType: TypewriterProps['cursorType'];
|
|
5
5
|
$isAnimatingText: boolean;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
type UseChunkStreamingSpeedProps = {
|
|
2
|
+
autoSpeedBaseFactor: number;
|
|
3
|
+
charactersCount: number;
|
|
4
|
+
shouldCalcAutoSpeed: boolean;
|
|
5
|
+
};
|
|
6
|
+
declare const useChunkStreamingSpeed: ({ autoSpeedBaseFactor, charactersCount, shouldCalcAutoSpeed, }: UseChunkStreamingSpeedProps) => import("react").MutableRefObject<number | undefined>;
|
|
7
|
+
export default useChunkStreamingSpeed;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { MouseEvent } from 'react';
|
|
2
|
+
import { CursorType } from '../types/cursor';
|
|
3
|
+
type UseTypewriterAnimationProps = {
|
|
4
|
+
autoSpeedBaseFactor: number;
|
|
5
|
+
charactersCount: number;
|
|
6
|
+
childrenKey: unknown;
|
|
7
|
+
childrenCount: number;
|
|
8
|
+
currentTextLength: number;
|
|
9
|
+
cursorType: CursorType;
|
|
10
|
+
nextTextDelay: number;
|
|
11
|
+
onAdvanceChild: VoidFunction;
|
|
12
|
+
onFinish?: VoidFunction;
|
|
13
|
+
onResetAnimationEnd?: VoidFunction;
|
|
14
|
+
onResetAnimationStart?: VoidFunction;
|
|
15
|
+
onTypingAnimationEnd?: VoidFunction;
|
|
16
|
+
onTypingAnimationStart?: VoidFunction;
|
|
17
|
+
resetDelay: number;
|
|
18
|
+
resetSpeed: number;
|
|
19
|
+
shouldCalcAutoSpeed: boolean;
|
|
20
|
+
shouldForceCursorAnimation: boolean;
|
|
21
|
+
shouldUseResetAnimation: boolean;
|
|
22
|
+
shouldWaitForContent?: boolean;
|
|
23
|
+
speed: number;
|
|
24
|
+
startDelay: number;
|
|
25
|
+
};
|
|
26
|
+
export type UseTypewriterAnimationResult = {
|
|
27
|
+
effectiveShownCharCount: number;
|
|
28
|
+
handleClick: (event: MouseEvent) => void;
|
|
29
|
+
hasRenderedChildrenOnce: boolean;
|
|
30
|
+
isAnimatingText: boolean;
|
|
31
|
+
isTypingAnimationActive: boolean;
|
|
32
|
+
shouldPreventBlinkingCursor: boolean;
|
|
33
|
+
};
|
|
34
|
+
declare const useTypewriterAnimation: ({ autoSpeedBaseFactor, charactersCount, childrenKey, childrenCount, currentTextLength, cursorType, nextTextDelay, onAdvanceChild, onFinish, onResetAnimationEnd, onResetAnimationStart, onTypingAnimationEnd, onTypingAnimationStart, resetDelay, resetSpeed, shouldCalcAutoSpeed, shouldForceCursorAnimation, shouldUseResetAnimation, shouldWaitForContent, speed, startDelay, }: UseTypewriterAnimationProps) => UseTypewriterAnimationResult;
|
|
35
|
+
export default useTypewriterAnimation;
|
|
@@ -24,10 +24,7 @@
|
|
|
24
24
|
export declare const getSubTextFromHTML: (html: string, length: number) => string;
|
|
25
25
|
export declare const getCharactersCount: (html: string) => number;
|
|
26
26
|
export declare const shuffleArray: <T>(array: T[]) => T[];
|
|
27
|
-
export declare const
|
|
28
|
-
speed: number;
|
|
29
|
-
steps: number;
|
|
30
|
-
};
|
|
27
|
+
export declare const getSafeAutoSpeed: (ema: number) => number;
|
|
31
28
|
interface CalculateEMAProps {
|
|
32
29
|
currentEMA: number;
|
|
33
30
|
newValue: number;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chayns-components/typewriter",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.53-alpha.0",
|
|
4
4
|
"description": "A set of beautiful React components for developing your own applications with chayns.",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"browserslist": [
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
"typescript": "^5.9.3"
|
|
71
71
|
},
|
|
72
72
|
"dependencies": {
|
|
73
|
-
"@chayns-components/core": "^5.0.
|
|
73
|
+
"@chayns-components/core": "^5.0.53-alpha.0",
|
|
74
74
|
"react-compiler-runtime": "^1.0.0"
|
|
75
75
|
},
|
|
76
76
|
"peerDependencies": {
|
|
@@ -83,5 +83,5 @@
|
|
|
83
83
|
"publishConfig": {
|
|
84
84
|
"access": "public"
|
|
85
85
|
},
|
|
86
|
-
"gitHead": "
|
|
86
|
+
"gitHead": "4a819fc73541f384d410156bf1820699cdd5e183"
|
|
87
87
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"AnimatedTypewriterText.js","names":["_react","_interopRequireWildcard","require","_Typewriter","e","t","WeakMap","r","n","__esModule","o","i","f","__proto__","default","has","get","set","hasOwnProperty","call","Object","defineProperty","getOwnPropertyDescriptor","AnimatedTypewriterText","shouldHideCursor","shownText","shouldRemainSingleLine","textStyle","updateTypewriterCursor","useCallback","ref","traverseNodes","node","_node$textContent","nodeType","Node","TEXT_NODE","textContent","trim","parentElement","childNodes","Array","from","length","result","lastParentWithContent","classList","remove","querySelectorAll","forEach","element","add","useMemo","createElement","StyledTypewriterText","dangerouslySetInnerHTML","__html","$shouldRemainSingleLine","style","$isAnimatingText","_default","exports"],"sources":["../../../../src/components/typewriter/AnimatedTypewriterText.tsx"],"sourcesContent":["import React, { FC, useCallback, useMemo } from 'react';\nimport { CSSPropertiesWithVars } from 'styled-components/dist/types';\nimport { StyledTypewriterText } from './Typewriter.styles';\n\ntype AnimatedTypewriterTextProps = {\n shouldHideCursor: boolean;\n shownText: string;\n textStyle?: CSSPropertiesWithVars;\n shouldRemainSingleLine: boolean;\n};\n\nconst AnimatedTypewriterText: FC<AnimatedTypewriterTextProps> = ({\n shouldHideCursor,\n shownText,\n shouldRemainSingleLine,\n textStyle,\n}) => {\n const updateTypewriterCursor = useCallback(\n (ref: HTMLSpanElement | null) => {\n if (ref && !shouldHideCursor) {\n // Finds the last text node with content.\n const traverseNodes = (node: Node): HTMLElement | null => {\n if (node.nodeType === Node.TEXT_NODE && node.textContent?.trim()) {\n return node.parentElement;\n }\n\n const childNodes = Array.from(node.childNodes);\n for (let i = childNodes.length - 1; i >= 0; i--) {\n const result = traverseNodes(childNodes[i] as Node);\n if (result) {\n return result;\n }\n }\n\n return null;\n };\n\n const lastParentWithContent = traverseNodes(ref);\n\n // Removes lastWithContent class from all elements\n ref.classList.remove('typewriter-lastWithContent');\n ref.querySelectorAll('.lastWithContent').forEach((element) => {\n element.classList.remove('typewriter-lastWithContent');\n });\n\n // Adds lastWithContent class to the last element with content\n if (lastParentWithContent) {\n lastParentWithContent.classList.add('typewriter-lastWithContent');\n } else {\n ref.classList.add('typewriter-lastWithContent');\n }\n }\n },\n [shouldHideCursor],\n );\n\n return useMemo(\n () => (\n <StyledTypewriterText\n ref={(ref) => updateTypewriterCursor(ref)}\n dangerouslySetInnerHTML={{ __html: shownText }}\n $shouldRemainSingleLine={shouldRemainSingleLine}\n style={textStyle}\n $isAnimatingText\n />\n ),\n [shownText, shouldRemainSingleLine, textStyle, updateTypewriterCursor],\n );\n};\n\nexport default AnimatedTypewriterText;\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AAEA,IAAAC,WAAA,GAAAD,OAAA;AAA2D,SAAAD,wBAAAG,CAAA,EAAAC,CAAA,6BAAAC,OAAA,MAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAL,uBAAA,YAAAA,CAAAG,CAAA,EAAAC,CAAA,SAAAA,CAAA,IAAAD,CAAA,IAAAA,CAAA,CAAAK,UAAA,SAAAL,CAAA,MAAAM,CAAA,EAAAC,CAAA,EAAAC,CAAA,KAAAC,SAAA,QAAAC,OAAA,EAAAV,CAAA,iBAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA,SAAAQ,CAAA,MAAAF,CAAA,GAAAL,CAAA,GAAAG,CAAA,GAAAD,CAAA,QAAAG,CAAA,CAAAK,GAAA,CAAAX,CAAA,UAAAM,CAAA,CAAAM,GAAA,CAAAZ,CAAA,GAAAM,CAAA,CAAAO,GAAA,CAAAb,CAAA,EAAAQ,CAAA,gBAAAP,CAAA,IAAAD,CAAA,gBAAAC,CAAA,OAAAa,cAA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;MAEhD;MACAA,GAAG,CAACgB,SAAS,CAACC,MAAM,CAAC,4BAA4B,CAAC;MAClDjB,GAAG,CAACkB,gBAAgB,CAAC,kBAAkB,CAAC,CAACC,OAAO,CAAEC,OAAO,IAAK;QAC1DA,OAAO,CAACJ,SAAS,CAACC,MAAM,CAAC,4BAA4B,CAAC;MAC1D,CAAC,CAAC;;MAEF;MACA,IAAIF,qBAAqB,EAAE;QACvBA,qBAAqB,CAACC,SAAS,CAACK,GAAG,CAAC,4BAA4B,CAAC;MACrE,CAAC,MAAM;QACHrB,GAAG,CAACgB,SAAS,CAACK,GAAG,CAAC,4BAA4B,CAAC;MACnD;IACJ;EACJ,CAAC,EACD,CAAC3B,gBAAgB,CACrB,CAAC;EAED,OAAO,IAAA4B,cAAO,EACV,mBACIpD,MAAA,CAAAc,OAAA,CAAAuC,aAAA,CAAClD,WAAA,CAAAmD,oBAAoB;IACjBxB,GAAG,EAAGA,GAAG,IAAKF,sBAAsB,CAACE,GAAG,CAAE;IAC1CyB,uBAAuB,EAAE;MAAEC,MAAM,EAAE/B;IAAU,CAAE;IAC/CgC,uBAAuB,EAAE/B,sBAAuB;IAChDgC,KAAK,EAAE/B,SAAU;IACjBgC,gBAAgB;EAAA,CACnB,CACJ,EACD,CAAClC,SAAS,EAAEC,sBAAsB,EAAEC,SAAS,EAAEC,sBAAsB,CACzE,CAAC;AACL,CAAC;AAAC,IAAAgC,QAAA,GAAAC,OAAA,CAAA/C,OAAA,GAEaS,sBAAsB","ignoreList":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Typewriter.styles.js","names":["_styledComponents","_interopRequireWildcard","require","_cursor","e","t","WeakMap","r","n","__esModule","o","i","f","__proto__","default","has","get","set","hasOwnProperty","call","Object","defineProperty","getOwnPropertyDescriptor","typewriterCursorElement","$cursorType","$isAnimatingText","$shouldHideCursor","$shouldPreventBlinkAnimation","CursorType","Thin","css","blinkAnimation","theme","text","StyledTypewriter","exports","styled","div","keyframes","StyledTypewriterPseudoText","span","StyledTypewriterText","$shouldRemainSingleLine"],"sources":["../../../../src/components/typewriter/Typewriter.styles.ts"],"sourcesContent":["import type { WithTheme } from '@chayns-components/core';\nimport styled, { css, keyframes } from 'styled-components';\nimport { CursorType } from '../../types/cursor';\nimport type { TypewriterProps } from './Typewriter';\n\ntype StyledTypewriterProps = WithTheme<{\n $cursorType: TypewriterProps['cursorType'];\n $isAnimatingText: boolean;\n $shouldHideCursor: TypewriterProps['shouldHideCursor'];\n $shouldPreventBlinkAnimation: boolean;\n}>;\n\nconst typewriterCursorElement = ({\n $cursorType,\n $isAnimatingText,\n $shouldHideCursor,\n $shouldPreventBlinkAnimation,\n}: StyledTypewriterProps) => {\n if (!$isAnimatingText || $shouldHideCursor) {\n return '';\n }\n\n if ($cursorType === CursorType.Thin) {\n return css`\n .typewriter-lastWithContent {\n &:after {\n animation: ${$shouldPreventBlinkAnimation ? 'none' : blinkAnimation} 1s steps(2, start) infinite;\n color: inherit;\n content: '|';\n font-size: 25px;\n position: relative;\n line-height: 0;\n vertical-align: baseline;\n }\n `;\n }\n\n return css`\n .typewriter-lastWithContent {\n &:after {\n animation: ${blinkAnimation} 1s steps(2, start) infinite;\n color: ${({ theme }) => (theme as { text: string }).text};\n content: '▋';\n margin-left: 0.25rem;\n opacity: 0.85;\n position: relative;\n vertical-align: baseline;\n }\n }\n `;\n};\n\nexport const StyledTypewriter = styled.div<StyledTypewriterProps>`\n align-items: inherit;\n display: flex;\n position: relative;\n width: 100%;\n ${typewriterCursorElement}\n`;\n\nconst blinkAnimation = keyframes`\n 100% {\n visibility: hidden;\n }\n`;\n\ntype StyledTypewriterPseudoTextProps = WithTheme<{\n $isAnimatingText?: boolean;\n $shouldHideCursor: TypewriterProps['shouldHideCursor'];\n}>;\n\nexport const StyledTypewriterPseudoText = styled.span<StyledTypewriterPseudoTextProps>`\n opacity: 0;\n pointer-events: none;\n user-select: none;\n width: fit-content;\n\n ${({ $isAnimatingText, $shouldHideCursor }) =>\n $isAnimatingText &&\n !$shouldHideCursor &&\n css`\n &:after {\n animation: ${blinkAnimation} 1s steps(2, start) infinite;\n color: inherit;\n content: '|';\n font-size: 25px;\n position: relative;\n line-height: 0;\n vertical-align: baseline;\n }\n `}\n`;\n\ntype StyledTypewriterTextProps = WithTheme<{\n $isAnimatingText?: boolean;\n $shouldRemainSingleLine: boolean;\n}>;\n\nexport const StyledTypewriterText = styled.span<StyledTypewriterTextProps>`\n color: inherit;\n position: ${({ $isAnimatingText }) => ($isAnimatingText ? 'absolute' : 'relative')};\n width: fit-content;\n\n ${({ $isAnimatingText }) =>\n $isAnimatingText &&\n css`\n pointer-events: none;\n `}\n\n ${({ $shouldRemainSingleLine }) =>\n $shouldRemainSingleLine &&\n css`\n white-space: nowrap;\n overflow: hidden;\n text-overflow: ellipsis;\n width: 100%;\n `}\n`;\n"],"mappings":";;;;;;AACA,IAAAA,iBAAA,GAAAC,uBAAA,CAAAC,OAAA;AACA,IAAAC,OAAA,GAAAD,OAAA;AAAgD,SAAAD,wBAAAG,CAAA,EAAAC,CAAA,6BAAAC,OAAA,MAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAL,uBAAA,YAAAA,CAAAG,CAAA,EAAAC,CAAA,SAAAA,CAAA,IAAAD,CAAA,IAAAA,CAAA,CAAAK,UAAA,SAAAL,CAAA,MAAAM,CAAA,EAAAC,CAAA,EAAAC,CAAA,KAAAC,SAAA,QAAAC,OAAA,EAAAV,CAAA,iBAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA,SAAAQ,CAAA,MAAAF,CAAA,GAAAL,CAAA,GAAAG,CAAA,GAAAD,CAAA,QAAAG,CAAA,CAAAK,GAAA,CAAAX,CAAA,UAAAM,CAAA,CAAAM,GAAA,CAAAZ,CAAA,GAAAM,CAAA,CAAAO,GAAA,CAAAb,CAAA,EAAAQ,CAAA,gBAAAP,CAAA,IAAAD,CAAA,gBAAAC,CAAA,OAAAa,cAAA,CA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AAAkB,CAAC,KACtCD,gBAAgB,IAChB,CAACC,iBAAiB,IAClB,IAAAI,qBAAG;AACX;AACA,6BAA6BC,cAAc;AAC3C;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT,CAAC;AAOM,MAAMU,oBAAoB,GAAAN,OAAA,CAAAM,oBAAA,GAAGL,yBAAM,CAACI,IAA+B;AAC1E;AACA,gBAAgB,CAAC;EAAEf;AAAiB,CAAC,KAAMA,gBAAgB,GAAG,UAAU,GAAG,UAAW;AACtF;AACA;AACA,MAAM,CAAC;EAAEA;AAAiB,CAAC,KACnBA,gBAAgB,IAChB,IAAAK,qBAAG;AACX;AACA,SAAS;AACT;AACA,MAAM,CAAC;EAAEY;AAAwB,CAAC,KAC1BA,uBAAuB,IACvB,IAAAZ,qBAAG;AACX;AACA;AACA;AACA;AACA,SAAS;AACT,CAAC","ignoreList":[]}
|
|
@@ -1 +0,0 @@
|
|
|
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","calculateAutoSpeed","ema","MINIMUM_TIMEOUT","speed","TypewriterSpeed","ExtraSlow","steps","msPerChar","min","max","calculateEMA","currentEMA","newValue","alpha","updateChunkStreamingSpeedEMA","currentLength","state","now","Date","deltaTime","lastTimestamp","deltaLength","lastLength","charsPerSecond","newEMA"],"sources":["../../../../src/components/typewriter/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 calculateAutoSpeed = (ema: number): { speed: number; steps: number } => {\n // nested timer calls are clamped to a 4ms minimum\n // https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout#reasons_for_longer_delays_than_specified\n const MINIMUM_TIMEOUT = 4;\n\n if (ema <= 0) {\n return { speed: TypewriterSpeed.ExtraSlow, steps: 1 };\n }\n\n const msPerChar = Math.min(1000 / ema, TypewriterSpeed.ExtraSlow);\n\n if (msPerChar >= MINIMUM_TIMEOUT) {\n return { speed: msPerChar, steps: 1 };\n }\n\n const steps = Math.max(1, MINIMUM_TIMEOUT / msPerChar);\n return { speed: MINIMUM_TIMEOUT, steps };\n};\n\ninterface CalculateEMAProps {\n currentEMA: number;\n newValue: number;\n alpha?: number;\n}\n\nexport const calculateEMA = ({ currentEMA, newValue, alpha = 0.25 }: 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: curr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AQ,EAAEW;EACd,CAAC,CAAC;EAEF,OAAO;IACHH,aAAa,EAAEH,GAAG;IAClBK,UAAU,EAAEP,aAAa;IACzBd,GAAG,EAAEuB;EACT,CAAC;AACL,CAAC;AAACtC,OAAA,CAAA4B,4BAAA,GAAAA,4BAAA","ignoreList":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"AnimatedTypewriterText.js","names":["React","useCallback","useMemo","StyledTypewriterText","AnimatedTypewriterText","shouldHideCursor","shownText","shouldRemainSingleLine","textStyle","updateTypewriterCursor","ref","traverseNodes","node","nodeType","Node","TEXT_NODE","textContent","trim","parentElement","childNodes","Array","from","i","length","result","lastParentWithContent","classList","remove","querySelectorAll","forEach","element","add","createElement","dangerouslySetInnerHTML","__html","$shouldRemainSingleLine","style","$isAnimatingText"],"sources":["../../../../src/components/typewriter/AnimatedTypewriterText.tsx"],"sourcesContent":["import React, { FC, useCallback, useMemo } from 'react';\nimport { CSSPropertiesWithVars } from 'styled-components/dist/types';\nimport { StyledTypewriterText } from './Typewriter.styles';\n\ntype AnimatedTypewriterTextProps = {\n shouldHideCursor: boolean;\n shownText: string;\n textStyle?: CSSPropertiesWithVars;\n shouldRemainSingleLine: boolean;\n};\n\nconst AnimatedTypewriterText: FC<AnimatedTypewriterTextProps> = ({\n shouldHideCursor,\n shownText,\n shouldRemainSingleLine,\n textStyle,\n}) => {\n const updateTypewriterCursor = useCallback(\n (ref: HTMLSpanElement | null) => {\n if (ref && !shouldHideCursor) {\n // Finds the last text node with content.\n const traverseNodes = (node: Node): HTMLElement | null => {\n if (node.nodeType === Node.TEXT_NODE && node.textContent?.trim()) {\n return node.parentElement;\n }\n\n const childNodes = Array.from(node.childNodes);\n for (let i = childNodes.length - 1; i >= 0; i--) {\n const result = traverseNodes(childNodes[i] as Node);\n if (result) {\n return result;\n }\n }\n\n return null;\n };\n\n const lastParentWithContent = traverseNodes(ref);\n\n // Removes lastWithContent class from all elements\n ref.classList.remove('typewriter-lastWithContent');\n ref.querySelectorAll('.lastWithContent').forEach((element) => {\n element.classList.remove('typewriter-lastWithContent');\n });\n\n // Adds lastWithContent class to the last element with content\n if (lastParentWithContent) {\n lastParentWithContent.classList.add('typewriter-lastWithContent');\n } else {\n ref.classList.add('typewriter-lastWithContent');\n }\n }\n },\n [shouldHideCursor],\n );\n\n return useMemo(\n () => (\n <StyledTypewriterText\n ref={(ref) => updateTypewriterCursor(ref)}\n dangerouslySetInnerHTML={{ __html: shownText }}\n $shouldRemainSingleLine={shouldRemainSingleLine}\n style={textStyle}\n $isAnimatingText\n />\n ),\n [shownText, shouldRemainSingleLine, textStyle, updateTypewriterCursor],\n );\n};\n\nexport default AnimatedTypewriterText;\n"],"mappings":"AAAA,OAAOA,KAAK,IAAQC,WAAW,EAAEC,OAAO,QAAQ,OAAO;AAEvD,SAASC,oBAAoB,QAAQ,qBAAqB;AAS1D,MAAMC,sBAAuD,GAAGA,CAAC;EAC7DC,gBAAgB;EAChBC,SAAS;EACTC,sBAAsB;EACtBC;AACJ,CAAC,KAAK;EACF,MAAMC,sBAAsB,GAAGR,WAAW,CACrCS,GAA2B,IAAK;IAC7B,IAAIA,GAAG,IAAI,CAACL,gBAAgB,EAAE;MAC1B;MACA,MAAMM,aAAa,GAAIC,IAAU,IAAyB;QACtD,IAAIA,IAAI,CAACC,QAAQ,KAAKC,IAAI,CAACC,SAAS,IAAIH,IAAI,CAACI,WAAW,EAAEC,IAAI,CAAC,CAAC,EAAE;UAC9D,OAAOL,IAAI,CAACM,aAAa;QAC7B;QAEA,MAAMC,UAAU,GAAGC,KAAK,CAACC,IAAI,CAACT,IAAI,CAACO,UAAU,CAAC;QAC9C,KAAK,IAAIG,CAAC,GAAGH,UAAU,CAACI,MAAM,GAAG,CAAC,EAAED,CAAC,IAAI,CAAC,EAAEA,CAAC,EAAE,EAAE;UAC7C,MAAME,MAAM,GAAGb,aAAa,CAACQ,UAAU,CAACG,CAAC,CAAS,CAAC;UACnD,IAAIE,MAAM,EAAE;YACR,OAAOA,MAAM;UACjB;QACJ;QAEA,OAAO,IAAI;MACf,CAAC;MAED,MAAMC,qBAAqB,GAAGd,aAAa,CAACD,GAAG,CAAC;;MAEhD;MACAA,GAAG,CAACgB,SAAS,CAACC,MAAM,CAAC,4BAA4B,CAAC;MAClDjB,GAAG,CAACkB,gBAAgB,CAAC,kBAAkB,CAAC,CAACC,OAAO,CAAEC,OAAO,IAAK;QAC1DA,OAAO,CAACJ,SAAS,CAACC,MAAM,CAAC,4BAA4B,CAAC;MAC1D,CAAC,CAAC;;MAEF;MACA,IAAIF,qBAAqB,EAAE;QACvBA,qBAAqB,CAACC,SAAS,CAACK,GAAG,CAAC,4BAA4B,CAAC;MACrE,CAAC,MAAM;QACHrB,GAAG,CAACgB,SAAS,CAACK,GAAG,CAAC,4BAA4B,CAAC;MACnD;IACJ;EACJ,CAAC,EACD,CAAC1B,gBAAgB,CACrB,CAAC;EAED,OAAOH,OAAO,CACV,mBACIF,KAAA,CAAAgC,aAAA,CAAC7B,oBAAoB;IACjBO,GAAG,EAAGA,GAAG,IAAKD,sBAAsB,CAACC,GAAG,CAAE;IAC1CuB,uBAAuB,EAAE;MAAEC,MAAM,EAAE5B;IAAU,CAAE;IAC/C6B,uBAAuB,EAAE5B,sBAAuB;IAChD6B,KAAK,EAAE5B,SAAU;IACjB6B,gBAAgB;EAAA,CACnB,CACJ,EACD,CAAC/B,SAAS,EAAEC,sBAAsB,EAAEC,SAAS,EAAEC,sBAAsB,CACzE,CAAC;AACL,CAAC;AAED,eAAeL,sBAAsB","ignoreList":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Typewriter.styles.js","names":["styled","css","keyframes","CursorType","typewriterCursorElement","$cursorType","$isAnimatingText","$shouldHideCursor","$shouldPreventBlinkAnimation","Thin","blinkAnimation","theme","text","StyledTypewriter","div","StyledTypewriterPseudoText","span","StyledTypewriterText","$shouldRemainSingleLine"],"sources":["../../../../src/components/typewriter/Typewriter.styles.ts"],"sourcesContent":["import type { WithTheme } from '@chayns-components/core';\nimport styled, { css, keyframes } from 'styled-components';\nimport { CursorType } from '../../types/cursor';\nimport type { TypewriterProps } from './Typewriter';\n\ntype StyledTypewriterProps = WithTheme<{\n $cursorType: TypewriterProps['cursorType'];\n $isAnimatingText: boolean;\n $shouldHideCursor: TypewriterProps['shouldHideCursor'];\n $shouldPreventBlinkAnimation: boolean;\n}>;\n\nconst typewriterCursorElement = ({\n $cursorType,\n $isAnimatingText,\n $shouldHideCursor,\n $shouldPreventBlinkAnimation,\n}: StyledTypewriterProps) => {\n if (!$isAnimatingText || $shouldHideCursor) {\n return '';\n }\n\n if ($cursorType === CursorType.Thin) {\n return css`\n .typewriter-lastWithContent {\n &:after {\n animation: ${$shouldPreventBlinkAnimation ? 'none' : blinkAnimation} 1s steps(2, start) infinite;\n color: inherit;\n content: '|';\n font-size: 25px;\n position: relative;\n line-height: 0;\n vertical-align: baseline;\n }\n `;\n }\n\n return css`\n .typewriter-lastWithContent {\n &:after {\n animation: ${blinkAnimation} 1s steps(2, start) infinite;\n color: ${({ theme }) => (theme as { text: string }).text};\n content: '▋';\n margin-left: 0.25rem;\n opacity: 0.85;\n position: relative;\n vertical-align: baseline;\n }\n }\n `;\n};\n\nexport const StyledTypewriter = styled.div<StyledTypewriterProps>`\n align-items: inherit;\n display: flex;\n position: relative;\n width: 100%;\n ${typewriterCursorElement}\n`;\n\nconst blinkAnimation = keyframes`\n 100% {\n visibility: hidden;\n }\n`;\n\ntype StyledTypewriterPseudoTextProps = WithTheme<{\n $isAnimatingText?: boolean;\n $shouldHideCursor: TypewriterProps['shouldHideCursor'];\n}>;\n\nexport const StyledTypewriterPseudoText = styled.span<StyledTypewriterPseudoTextProps>`\n opacity: 0;\n pointer-events: none;\n user-select: none;\n width: fit-content;\n\n ${({ $isAnimatingText, $shouldHideCursor }) =>\n $isAnimatingText &&\n !$shouldHideCursor &&\n css`\n &:after {\n animation: ${blinkAnimation} 1s steps(2, start) infinite;\n color: inherit;\n content: '|';\n font-size: 25px;\n position: relative;\n line-height: 0;\n vertical-align: baseline;\n }\n `}\n`;\n\ntype StyledTypewriterTextProps = WithTheme<{\n $isAnimatingText?: boolean;\n $shouldRemainSingleLine: boolean;\n}>;\n\nexport const StyledTypewriterText = styled.span<StyledTypewriterTextProps>`\n color: inherit;\n position: ${({ $isAnimatingText }) => ($isAnimatingText ? 'absolute' : 'relative')};\n width: fit-content;\n\n ${({ $isAnimatingText }) =>\n $isAnimatingText &&\n css`\n pointer-events: none;\n `}\n\n ${({ $shouldRemainSingleLine }) =>\n $shouldRemainSingleLine &&\n css`\n white-space: nowrap;\n overflow: hidden;\n text-overflow: ellipsis;\n width: 100%;\n `}\n`;\n"],"mappings":"AACA,OAAOA,MAAM,IAAIC,GAAG,EAAEC,SAAS,QAAQ,mBAAmB;AAC1D,SAASC,UAAU,QAAQ,oBAAoB;AAU/C,MAAMC,uBAAuB,GAAGA,CAAC;EAC7BC,WAAW;EACXC,gBAAgB;EAChBC,iBAAiB;EACjBC;AACmB,CAAC,KAAK;EACzB,IAAI,CAACF,gBAAgB,IAAIC,iBAAiB,EAAE;IACxC,OAAO,EAAE;EACb;EAEA,IAAIF,WAAW,KAAKF,UAAU,CAACM,IAAI,EAAE;IACjC,OAAOR,GAAG;AAClB;AACA;AACA,iCAAiCO,4BAA4B,GAAG,MAAM,GAAGE,cAAc;AACvF;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;EACL;EAEA,OAAOT,GAAG;AACd;AACA;AACA,6BAA6BS,cAAc;AAC3C,yBAAyB,CAAC;IAAEC;EAAM,CAAC,KAAMA,KAAK,CAAsBC,IAAI;AACxE;AACA;AACA;AACA;AACA;AACA;AACA;AACA,KAAK;AACL,CAAC;AAED,OAAO,MAAMC,gBAAgB,GAAGb,MAAM,CAACc,GAA0B;AACjE;AACA;AACA;AACA;AACA,MAAMV,uBAAuB;AAC7B,CAAC;AAED,MAAMM,cAAc,GAAGR,SAAS;AAChC;AACA;AACA;AACA,CAAC;AAOD,OAAO,MAAMa,0BAA0B,GAAGf,MAAM,CAACgB,IAAqC;AACtF;AACA;AACA;AACA;AACA;AACA,MAAM,CAAC;EAAEV,gBAAgB;EAAEC;AAAkB,CAAC,KACtCD,gBAAgB,IAChB,CAACC,iBAAiB,IAClBN,GAAG;AACX;AACA,6BAA6BS,cAAc;AAC3C;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT,CAAC;AAOD,OAAO,MAAMO,oBAAoB,GAAGjB,MAAM,CAACgB,IAA+B;AAC1E;AACA,gBAAgB,CAAC;EAAEV;AAAiB,CAAC,KAAMA,gBAAgB,GAAG,UAAU,GAAG,UAAW;AACtF;AACA;AACA,MAAM,CAAC;EAAEA;AAAiB,CAAC,KACnBA,gBAAgB,IAChBL,GAAG;AACX;AACA,SAAS;AACT;AACA,MAAM,CAAC;EAAEiB;AAAwB,CAAC,KAC1BA,uBAAuB,IACvBjB,GAAG;AACX;AACA;AACA;AACA;AACA,SAAS;AACT,CAAC","ignoreList":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","names":["TypewriterSpeed","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","getCharactersCount","count","trim","Array","from","forEach","shuffleArray","array","result","j","Math","floor","random","calculateAutoSpeed","ema","MINIMUM_TIMEOUT","speed","ExtraSlow","steps","msPerChar","min","max","calculateEMA","currentEMA","newValue","alpha","updateChunkStreamingSpeedEMA","currentLength","state","now","Date","deltaTime","lastTimestamp","deltaLength","lastLength","charsPerSecond","newEMA"],"sources":["../../../../src/components/typewriter/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 calculateAutoSpeed = (ema: number): { speed: number; steps: number } => {\n // nested timer calls are clamped to a 4ms minimum\n // https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout#reasons_for_longer_delays_than_specified\n const MINIMUM_TIMEOUT = 4;\n\n if (ema <= 0) {\n return { speed: TypewriterSpeed.ExtraSlow, steps: 1 };\n }\n\n const msPerChar = Math.min(1000 / ema, TypewriterSpeed.ExtraSlow);\n\n if (msPerChar >= MINIMUM_TIMEOUT) {\n return { speed: msPerChar, steps: 1 };\n }\n\n const steps = Math.max(1, MINIMUM_TIMEOUT / msPerChar);\n return { speed: MINIMUM_TIMEOUT, steps };\n};\n\ninterface CalculateEMAProps {\n currentEMA: number;\n newValue: number;\n alpha?: number;\n}\n\nexport const calculateEMA = ({ currentEMA, newValue, alpha = 0.25 }: 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,SAASA,eAAe,QAAQ,mBAAmB;;AAEnD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,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,MAAMC,QAAQ,GAAIC,IAAU,IAAc;IACtC;IACA,IAAIA,IAAI,CAACC,QAAQ,K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