@chayns-components/typewriter 5.0.0-beta.112 → 5.0.0-beta.117

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.
@@ -28,6 +28,10 @@ export type TypewriterProps = {
28
28
  * Waiting time before the typewriter resets the content if multiple texts are given
29
29
  */
30
30
  resetDelay?: TypewriterResetDelay;
31
+ /**
32
+ * Specifies whether the cursor should be forced to animate even if no text is currently animated.
33
+ */
34
+ shouldForceCursorAnimation?: boolean;
31
35
  /**
32
36
  * Specifies whether the children should be sorted randomly if there are multiple texts.
33
37
  * This makes the typewriter start with a different text each time and also changes them
@@ -31,6 +31,7 @@ const Typewriter = _ref => {
31
31
  onFinish,
32
32
  pseudoChildren,
33
33
  resetDelay = TypewriterResetDelay.Medium,
34
+ shouldForceCursorAnimation = false,
34
35
  shouldSortChildrenRandomly = false,
35
36
  shouldUseResetAnimation = false,
36
37
  speed = TypewriterSpeed.Medium
@@ -53,7 +54,7 @@ const Typewriter = _ref => {
53
54
  const [isResetAnimationActive, setIsResetAnimationActive] = (0, _react.useState)(false);
54
55
  const [shownCharCount, setShownCharCount] = (0, _react.useState)(charactersCount > 0 ? 0 : textContent.length);
55
56
  const [shouldStopAnimation, setShouldStopAnimation] = (0, _react.useState)(false);
56
- const isAnimatingText = shownCharCount !== textContent.length || areMultipleChildrenGiven || textContent.length === 0;
57
+ const isAnimatingText = shownCharCount !== textContent.length || shouldForceCursorAnimation || areMultipleChildrenGiven || textContent.length === 0;
57
58
  const handleClick = (0, _react.useCallback)(() => {
58
59
  setShouldStopAnimation(true);
59
60
  }, []);
@@ -116,12 +117,6 @@ const Typewriter = _ref => {
116
117
  window.clearInterval(interval);
117
118
  };
118
119
  }, [shouldStopAnimation, speed, textContent.length, charactersCount, isResetAnimationActive, areMultipleChildrenGiven, resetDelay, childrenCount, handleSetNextChildrenIndex, shouldUseResetAnimation]);
119
- (0, _react.useEffect)(() => {
120
- if (charactersCount) {
121
- setIsResetAnimationActive(false);
122
- setShownCharCount(0);
123
- }
124
- }, [charactersCount]);
125
120
  (0, _react.useEffect)(() => {
126
121
  if (!isAnimatingText && typeof onFinish === 'function') {
127
122
  onFinish();
@@ -145,7 +140,7 @@ const Typewriter = _ref => {
145
140
  dangerouslySetInnerHTML: {
146
141
  __html: pseudoTextHTML
147
142
  }
148
- })), [handleClick, isAnimatingText, pseudoChildren, shownText, sortedChildren, textContent]);
143
+ })), [handleClick, isAnimatingText, pseudoTextHTML, shownText, sortedChildren]);
149
144
  };
150
145
  Typewriter.displayName = 'Typewriter';
151
146
  var _default = Typewriter;
@@ -1 +1 @@
1
- {"version":3,"file":"Typewriter.js","names":["_react","_interopRequireWildcard","require","_server","_Typewriter","_utils","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","obj","__esModule","default","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","TypewriterResetDelay","exports","TypewriterSpeed","Typewriter","_ref","children","onFinish","pseudoChildren","resetDelay","Medium","shouldSortChildrenRandomly","shouldUseResetAnimation","speed","currentChildrenIndex","setCurrentChildrenIndex","useState","sortedChildren","useMemo","Array","isArray","shuffleArray","areMultipleChildrenGiven","childrenCount","length","textContent","currentChildren","React","isValidElement","renderToString","charactersCount","getCharactersCount","isResetAnimationActive","setIsResetAnimationActive","shownCharCount","setShownCharCount","shouldStopAnimation","setShouldStopAnimation","isAnimatingText","handleClick","useCallback","handleSetNextChildrenIndex","newIndex","useEffect","interval","window","setInterval","prevState","nextState","clearInterval","setTimeout","shownText","getSubTextFromHTML","pseudoTextHTML","createElement","StyledTypewriter","onClick","StyledTypewriterText","dangerouslySetInnerHTML","__html","StyledTypewriterPseudoText","displayName","_default"],"sources":["../../../src/components/typewriter/Typewriter.tsx"],"sourcesContent":["import React, { FC, ReactElement, useCallback, useEffect, useMemo, useState } from 'react';\nimport { renderToString } from 'react-dom/server';\nimport {\n StyledTypewriter,\n StyledTypewriterPseudoText,\n StyledTypewriterText,\n} from './Typewriter.styles';\nimport { getCharactersCount, getSubTextFromHTML, shuffleArray } from './utils';\n\n// noinspection JSUnusedGlobalSymbols\nexport enum TypewriterResetDelay {\n Slow = 4000,\n Medium = 2000,\n Fast = 1000,\n}\n\n// noinspection JSUnusedGlobalSymbols\nexport enum TypewriterSpeed {\n Slow = 40,\n Medium = 30,\n Fast = 20,\n}\n\nexport type TypewriterProps = {\n /**\n * The text to type\n */\n children: ReactElement | ReactElement[] | string | string[];\n /**\n * Function that is executed when the typewriter animation has finished. This function will not\n * be executed if multiple texts are used.\n */\n onFinish?: VoidFunction;\n /**\n * Pseudo-element to be rendered invisible during animation to define the size of the element\n * for the typewriter effect. By default, the \"children\" is used for this purpose.\n */\n pseudoChildren?: ReactElement | string;\n /**\n * Waiting time before the typewriter resets the content if multiple texts are given\n */\n resetDelay?: TypewriterResetDelay;\n /**\n * Specifies whether the children should be sorted randomly if there are multiple texts.\n * This makes the typewriter start with a different text each time and also changes them\n * in a random order.\n */\n shouldSortChildrenRandomly?: boolean;\n /**\n * Specifies whether the reset of the text should be animated with a backspace animation for\n * multiple texts.\n */\n shouldUseResetAnimation?: boolean;\n /**\n * The speed of the animation. Use the TypewriterSpeed enum for this prop.\n */\n speed?: TypewriterSpeed;\n};\n\nconst Typewriter: FC<TypewriterProps> = ({\n children,\n onFinish,\n pseudoChildren,\n resetDelay = TypewriterResetDelay.Medium,\n shouldSortChildrenRandomly = false,\n shouldUseResetAnimation = false,\n speed = TypewriterSpeed.Medium,\n}) => {\n const [currentChildrenIndex, setCurrentChildrenIndex] = useState(0);\n\n const sortedChildren = useMemo(\n () =>\n Array.isArray(children) && shouldSortChildrenRandomly\n ? shuffleArray<ReactElement | string>(children)\n : children,\n [children, shouldSortChildrenRandomly]\n );\n\n const areMultipleChildrenGiven = Array.isArray(sortedChildren);\n const childrenCount = areMultipleChildrenGiven ? sortedChildren.length : 1;\n\n const textContent = useMemo(() => {\n if (areMultipleChildrenGiven) {\n const currentChildren = sortedChildren[currentChildrenIndex];\n\n if (currentChildren) {\n return React.isValidElement(currentChildren)\n ? renderToString(currentChildren)\n : currentChildren;\n }\n\n return '';\n }\n\n return React.isValidElement(sortedChildren)\n ? renderToString(sortedChildren)\n : sortedChildren;\n }, [areMultipleChildrenGiven, currentChildrenIndex, sortedChildren]);\n\n const charactersCount = useMemo(() => getCharactersCount(textContent), [textContent]);\n\n const [isResetAnimationActive, setIsResetAnimationActive] = useState(false);\n const [shownCharCount, setShownCharCount] = useState(\n charactersCount > 0 ? 0 : textContent.length\n );\n const [shouldStopAnimation, setShouldStopAnimation] = useState(false);\n\n const isAnimatingText =\n shownCharCount !== textContent.length ||\n areMultipleChildrenGiven ||\n textContent.length === 0;\n\n const handleClick = useCallback(() => {\n setShouldStopAnimation(true);\n }, []);\n\n const handleSetNextChildrenIndex = useCallback(\n () =>\n setCurrentChildrenIndex(() => {\n let newIndex = currentChildrenIndex + 1;\n\n if (newIndex > childrenCount - 1) {\n newIndex = 0;\n }\n\n return newIndex;\n }),\n [childrenCount, currentChildrenIndex]\n );\n\n useEffect(() => {\n let interval: number | undefined;\n\n if (shouldStopAnimation || charactersCount === 0) {\n setShownCharCount(textContent.length);\n } else if (isResetAnimationActive) {\n interval = window.setInterval(() => {\n setShownCharCount((prevState) => {\n const nextState = prevState - 1;\n\n if (nextState === 0) {\n window.clearInterval(interval);\n\n if (areMultipleChildrenGiven) {\n setTimeout(() => {\n setIsResetAnimationActive(false);\n handleSetNextChildrenIndex();\n }, resetDelay);\n }\n }\n\n return nextState;\n });\n }, speed);\n } else {\n interval = window.setInterval(() => {\n setShownCharCount((prevState) => {\n let nextState = prevState + 1;\n\n if (nextState === charactersCount) {\n window.clearInterval(interval);\n\n /**\n * At this point, the next value for \"shownCharCount\" is deliberately set to\n * the length of the textContent in order to correctly display HTML elements\n * after the last letter.\n */\n nextState = textContent.length;\n\n if (areMultipleChildrenGiven) {\n setTimeout(() => {\n if (shouldUseResetAnimation) {\n setIsResetAnimationActive(true);\n } else {\n setShownCharCount(0);\n setTimeout(handleSetNextChildrenIndex, resetDelay / 2);\n }\n }, resetDelay);\n }\n }\n\n return nextState;\n });\n }, speed);\n }\n\n return () => {\n window.clearInterval(interval);\n };\n }, [\n shouldStopAnimation,\n speed,\n textContent.length,\n charactersCount,\n isResetAnimationActive,\n areMultipleChildrenGiven,\n resetDelay,\n childrenCount,\n handleSetNextChildrenIndex,\n shouldUseResetAnimation,\n ]);\n\n useEffect(() => {\n if (charactersCount) {\n setIsResetAnimationActive(false);\n setShownCharCount(0);\n }\n }, [charactersCount]);\n\n useEffect(() => {\n if (!isAnimatingText && typeof onFinish === 'function') {\n onFinish();\n }\n }, [isAnimatingText, onFinish]);\n\n const shownText = useMemo(\n () => getSubTextFromHTML(textContent, shownCharCount),\n [shownCharCount, textContent]\n );\n\n const pseudoTextHTML = useMemo(() => {\n if (pseudoChildren) {\n return React.isValidElement(pseudoChildren)\n ? renderToString(pseudoChildren)\n : pseudoChildren;\n }\n\n return textContent || '&#8203;';\n }, [pseudoChildren, textContent]);\n\n return useMemo(\n () => (\n <StyledTypewriter onClick={handleClick}>\n {isAnimatingText ? (\n <StyledTypewriterText\n dangerouslySetInnerHTML={{ __html: shownText }}\n isAnimatingText\n />\n ) : (\n <StyledTypewriterText>{sortedChildren}</StyledTypewriterText>\n )}\n {isAnimatingText && (\n <StyledTypewriterPseudoText\n dangerouslySetInnerHTML={{ __html: pseudoTextHTML }}\n />\n )}\n </StyledTypewriter>\n ),\n [handleClick, isAnimatingText, pseudoChildren, shownText, sortedChildren, textContent]\n );\n};\n\nTypewriter.displayName = 'Typewriter';\n\nexport default Typewriter;\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AACA,IAAAC,OAAA,GAAAD,OAAA;AACA,IAAAE,WAAA,GAAAF,OAAA;AAKA,IAAAG,MAAA,GAAAH,OAAA;AAA+E,SAAAI,yBAAAC,WA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BC,uBAAuB,EAAE;MAAEC,MAAM,EAAER;IAAU,CAAE;IAC/Cb,eAAe;EAAA,EACjB,gBAEFjE,MAAA,CAAAa,OAAA,CAAAoE,aAAA,CAAC7E,WAAA,CAAAgF,oBAAoB,QAAExC,cAAc,CACxC,EACAqB,eAAe,iBACZjE,MAAA,CAAAa,OAAA,CAAAoE,aAAA,CAAC7E,WAAA,CAAAmF,0BAA0B;IACvBF,uBAAuB,EAAE;MAAEC,MAAM,EAAEN;IAAe;EAAE,EAE3D,CAER,EACD,CAACd,WAAW,EAAED,eAAe,EAAE9B,cAAc,EAAE2C,SAAS,EAAElC,cAAc,EAAEQ,WAAW,CAAC,CACzF;AACL,CAAC;AAEDrB,UAAU,CAACyD,WAAW,GAAG,YAAY;AAAC,IAAAC,QAAA,GAEvB1D,UAAU;AAAAF,OAAA,CAAAhB,OAAA,GAAA4E,QAAA"}
1
+ {"version":3,"file":"Typewriter.js","names":["_react","_interopRequireWildcard","require","_server","_Typewriter","_utils","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","obj","__esModule","default","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","TypewriterResetDelay","exports","TypewriterSpeed","Typewriter","_ref","children","onFinish","pseudoChildren","resetDelay","Medium","shouldForceCursorAnimation","shouldSortChildrenRandomly","shouldUseResetAnimation","speed","currentChildrenIndex","setCurrentChildrenIndex","useState","sortedChildren","useMemo","Array","isArray","shuffleArray","areMultipleChildrenGiven","childrenCount","length","textContent","currentChildren","React","isValidElement","renderToString","charactersCount","getCharactersCount","isResetAnimationActive","setIsResetAnimationActive","shownCharCount","setShownCharCount","shouldStopAnimation","setShouldStopAnimation","isAnimatingText","handleClick","useCallback","handleSetNextChildrenIndex","newIndex","useEffect","interval","window","setInterval","prevState","nextState","clearInterval","setTimeout","shownText","getSubTextFromHTML","pseudoTextHTML","createElement","StyledTypewriter","onClick","StyledTypewriterText","dangerouslySetInnerHTML","__html","StyledTypewriterPseudoText","displayName","_default"],"sources":["../../../src/components/typewriter/Typewriter.tsx"],"sourcesContent":["import React, { FC, ReactElement, useCallback, useEffect, useMemo, useState } from 'react';\nimport { renderToString } from 'react-dom/server';\nimport {\n StyledTypewriter,\n StyledTypewriterPseudoText,\n StyledTypewriterText,\n} from './Typewriter.styles';\nimport { getCharactersCount, getSubTextFromHTML, shuffleArray } from './utils';\n\n// noinspection JSUnusedGlobalSymbols\nexport enum TypewriterResetDelay {\n Slow = 4000,\n Medium = 2000,\n Fast = 1000,\n}\n\n// noinspection JSUnusedGlobalSymbols\nexport enum TypewriterSpeed {\n Slow = 40,\n Medium = 30,\n Fast = 20,\n}\n\nexport type TypewriterProps = {\n /**\n * The text to type\n */\n children: ReactElement | ReactElement[] | string | string[];\n /**\n * Function that is executed when the typewriter animation has finished. This function will not\n * be executed if multiple texts are used.\n */\n onFinish?: VoidFunction;\n /**\n * Pseudo-element to be rendered invisible during animation to define the size of the element\n * for the typewriter effect. By default, the \"children\" is used for this purpose.\n */\n pseudoChildren?: ReactElement | string;\n /**\n * Waiting time before the typewriter resets the content if multiple texts are given\n */\n resetDelay?: TypewriterResetDelay;\n /**\n * Specifies whether the cursor should be forced to animate even if no text is currently animated.\n */\n shouldForceCursorAnimation?: boolean;\n /**\n * Specifies whether the children should be sorted randomly if there are multiple texts.\n * This makes the typewriter start with a different text each time and also changes them\n * in a random order.\n */\n shouldSortChildrenRandomly?: boolean;\n /**\n * Specifies whether the reset of the text should be animated with a backspace animation for\n * multiple texts.\n */\n shouldUseResetAnimation?: boolean;\n /**\n * The speed of the animation. Use the TypewriterSpeed enum for this prop.\n */\n speed?: TypewriterSpeed;\n};\n\nconst Typewriter: FC<TypewriterProps> = ({\n children,\n onFinish,\n pseudoChildren,\n resetDelay = TypewriterResetDelay.Medium,\n shouldForceCursorAnimation = false,\n shouldSortChildrenRandomly = false,\n shouldUseResetAnimation = false,\n speed = TypewriterSpeed.Medium,\n}) => {\n const [currentChildrenIndex, setCurrentChildrenIndex] = useState(0);\n\n const sortedChildren = useMemo(\n () =>\n Array.isArray(children) && shouldSortChildrenRandomly\n ? shuffleArray<ReactElement | string>(children)\n : children,\n [children, shouldSortChildrenRandomly]\n );\n\n const areMultipleChildrenGiven = Array.isArray(sortedChildren);\n const childrenCount = areMultipleChildrenGiven ? sortedChildren.length : 1;\n\n const textContent = useMemo(() => {\n if (areMultipleChildrenGiven) {\n const currentChildren = sortedChildren[currentChildrenIndex];\n\n if (currentChildren) {\n return React.isValidElement(currentChildren)\n ? renderToString(currentChildren)\n : currentChildren;\n }\n\n return '';\n }\n\n return React.isValidElement(sortedChildren)\n ? renderToString(sortedChildren)\n : sortedChildren;\n }, [areMultipleChildrenGiven, currentChildrenIndex, sortedChildren]);\n\n const charactersCount = useMemo(() => getCharactersCount(textContent), [textContent]);\n\n const [isResetAnimationActive, setIsResetAnimationActive] = useState(false);\n const [shownCharCount, setShownCharCount] = useState(\n charactersCount > 0 ? 0 : textContent.length\n );\n const [shouldStopAnimation, setShouldStopAnimation] = useState(false);\n\n const isAnimatingText =\n shownCharCount !== textContent.length ||\n shouldForceCursorAnimation ||\n areMultipleChildrenGiven ||\n textContent.length === 0;\n\n const handleClick = useCallback(() => {\n setShouldStopAnimation(true);\n }, []);\n\n const handleSetNextChildrenIndex = useCallback(\n () =>\n setCurrentChildrenIndex(() => {\n let newIndex = currentChildrenIndex + 1;\n\n if (newIndex > childrenCount - 1) {\n newIndex = 0;\n }\n\n return newIndex;\n }),\n [childrenCount, currentChildrenIndex]\n );\n\n useEffect(() => {\n let interval: number | undefined;\n\n if (shouldStopAnimation || charactersCount === 0) {\n setShownCharCount(textContent.length);\n } else if (isResetAnimationActive) {\n interval = window.setInterval(() => {\n setShownCharCount((prevState) => {\n const nextState = prevState - 1;\n\n if (nextState === 0) {\n window.clearInterval(interval);\n\n if (areMultipleChildrenGiven) {\n setTimeout(() => {\n setIsResetAnimationActive(false);\n handleSetNextChildrenIndex();\n }, resetDelay);\n }\n }\n\n return nextState;\n });\n }, speed);\n } else {\n interval = window.setInterval(() => {\n setShownCharCount((prevState) => {\n let nextState = prevState + 1;\n\n if (nextState === charactersCount) {\n window.clearInterval(interval);\n\n /**\n * At this point, the next value for \"shownCharCount\" is deliberately set to\n * the length of the textContent in order to correctly display HTML elements\n * after the last letter.\n */\n nextState = textContent.length;\n\n if (areMultipleChildrenGiven) {\n setTimeout(() => {\n if (shouldUseResetAnimation) {\n setIsResetAnimationActive(true);\n } else {\n setShownCharCount(0);\n setTimeout(handleSetNextChildrenIndex, resetDelay / 2);\n }\n }, resetDelay);\n }\n }\n\n return nextState;\n });\n }, speed);\n }\n\n return () => {\n window.clearInterval(interval);\n };\n }, [\n shouldStopAnimation,\n speed,\n textContent.length,\n charactersCount,\n isResetAnimationActive,\n areMultipleChildrenGiven,\n resetDelay,\n childrenCount,\n handleSetNextChildrenIndex,\n shouldUseResetAnimation,\n ]);\n\n useEffect(() => {\n if (!isAnimatingText && typeof onFinish === 'function') {\n onFinish();\n }\n }, [isAnimatingText, onFinish]);\n\n const shownText = useMemo(\n () => getSubTextFromHTML(textContent, shownCharCount),\n [shownCharCount, textContent]\n );\n\n const pseudoTextHTML = useMemo(() => {\n if (pseudoChildren) {\n return React.isValidElement(pseudoChildren)\n ? renderToString(pseudoChildren)\n : pseudoChildren;\n }\n\n return textContent || '&#8203;';\n }, [pseudoChildren, textContent]);\n\n return useMemo(\n () => (\n <StyledTypewriter onClick={handleClick}>\n {isAnimatingText ? (\n <StyledTypewriterText\n dangerouslySetInnerHTML={{ __html: shownText }}\n isAnimatingText\n />\n ) : (\n <StyledTypewriterText>{sortedChildren}</StyledTypewriterText>\n )}\n {isAnimatingText && (\n <StyledTypewriterPseudoText\n dangerouslySetInnerHTML={{ __html: pseudoTextHTML }}\n />\n )}\n </StyledTypewriter>\n ),\n [handleClick, isAnimatingText, pseudoTextHTML, shownText, sortedChildren]\n );\n};\n\nTypewriter.displayName = 'Typ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BACZlE,MAAA,CAAAa,OAAA,CAAAqE,aAAA,CAAC9E,WAAA,CAAAiF,oBAAoB;IACjBC,uBAAuB,EAAE;MAAEC,MAAM,EAAER;IAAU,CAAE;IAC/Cb,eAAe;EAAA,EACjB,gBAEFlE,MAAA,CAAAa,OAAA,CAAAqE,aAAA,CAAC9E,WAAA,CAAAiF,oBAAoB,QAAExC,cAAc,CACxC,EACAqB,eAAe,iBACZlE,MAAA,CAAAa,OAAA,CAAAqE,aAAA,CAAC9E,WAAA,CAAAoF,0BAA0B;IACvBF,uBAAuB,EAAE;MAAEC,MAAM,EAAEN;IAAe;EAAE,EAE3D,CAER,EACD,CAACd,WAAW,EAAED,eAAe,EAAEe,cAAc,EAAEF,SAAS,EAAElC,cAAc,CAAC,CAC5E;AACL,CAAC;AAEDd,UAAU,CAAC0D,WAAW,GAAG,YAAY;AAAC,IAAAC,QAAA,GAEvB3D,UAAU;AAAAF,OAAA,CAAAhB,OAAA,GAAA6E,QAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chayns-components/typewriter",
3
- "version": "5.0.0-beta.112",
3
+ "version": "5.0.0-beta.117",
4
4
  "description": "A set of beautiful React components for developing your own applications with chayns.",
5
5
  "keywords": [
6
6
  "chayns",
@@ -49,7 +49,7 @@
49
49
  "typescript": "^4.9.5"
50
50
  },
51
51
  "dependencies": {
52
- "@chayns-components/core": "^5.0.0-beta.105",
52
+ "@chayns-components/core": "^5.0.0-beta.117",
53
53
  "@chayns/colors": "^2.0.0",
54
54
  "clsx": "^1.2.1",
55
55
  "framer-motion": "^6.5.1",
@@ -63,5 +63,5 @@
63
63
  "publishConfig": {
64
64
  "access": "public"
65
65
  },
66
- "gitHead": "30dabe725ea6b548c40e9549aed3e8cc1c469726"
66
+ "gitHead": "11e687f9cc1139b4f17337ed6aa2a8507b4f997d"
67
67
  }