|
1 | 1 | import { css, cx } from '@linaria/core'; |
2 | | -import React, { memo, useRef, useState } from 'react'; |
| 2 | +import type { AnimationItem } from 'lottie-web'; |
| 3 | +import React, { memo, useEffect, useRef, useState } from 'react'; |
3 | 4 | import { LuInfo, LuX } from 'react-icons/lu'; |
4 | 5 | import { AnimationFileDataViewerJsonViewer } from './AnimationFileDataViewerJsonViewer'; |
5 | 6 | import { IconButton } from './ComponentButtons'; |
6 | 7 | import type { MarkerUnprocessed } from './types/Animation'; |
7 | 8 |
|
8 | 9 | interface AnimationFileDataViewerProps { |
9 | | - animationData: object | null | undefined; |
| 10 | + animation: AnimationItem | null; |
10 | 11 | } |
11 | 12 |
|
12 | 13 | interface Data { |
@@ -39,9 +40,8 @@ interface Data { |
39 | 40 | w: number; |
40 | 41 | } |
41 | 42 |
|
42 | | - |
43 | | -export const AnimationFileDataViewer: React.FC<AnimationFileDataViewerProps> = memo(({ animationData: _animationData }) => { |
44 | | - const animationData = _animationData as Data; |
| 43 | +export const AnimationFileDataViewer: React.FC<AnimationFileDataViewerProps> = memo(({ animation }) => { |
| 44 | + const animationData = useAnimationData(animation) |
45 | 45 | const modalRef = useRef <HTMLDialogElement>(null); |
46 | 46 | const [open, setOpen] = useState(false); |
47 | 47 |
|
@@ -111,6 +111,34 @@ export const AnimationFileDataViewer: React.FC<AnimationFileDataViewerProps> = m |
111 | 111 | ); |
112 | 112 | }); |
113 | 113 |
|
| 114 | +const tryRun = (fn: () => void) => { |
| 115 | + try { |
| 116 | + return fn() |
| 117 | + } catch (e) { |
| 118 | + // ignore |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +const useAnimationData = (animation: AnimationItem | null | undefined) => { |
| 123 | + const [animationData, setAnimationData] = useState<Data | null>(null) ; |
| 124 | + |
| 125 | + useEffect(() => { |
| 126 | + const handler = () => { |
| 127 | + setAnimationData((animation as unknown as { animationData: Data }).animationData); |
| 128 | + } |
| 129 | + tryRun(() => { |
| 130 | + animation?.addEventListener("config_ready", handler); |
| 131 | + }) |
| 132 | + return () => { |
| 133 | + tryRun(() => { |
| 134 | + animation?.removeEventListener("config_ready", handler); |
| 135 | + }); |
| 136 | + } |
| 137 | + }, [animation]); |
| 138 | + |
| 139 | + return animationData; |
| 140 | +} |
| 141 | + |
114 | 142 | const cssDialog = css` |
115 | 143 | padding: 0; |
116 | 144 | height: 100%; |
|
0 commit comments