mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
separated image loading and resizing
This commit is contained in:
parent
3808f2f516
commit
29193e392b
1 changed files with 17 additions and 9 deletions
|
@ -44,24 +44,32 @@ const NodeImageTinySlider: FC<INodeComponentProps> = ({ node }) => {
|
||||||
const [current, setCurrent] = useState(0);
|
const [current, setCurrent] = useState(0);
|
||||||
const [height, setHeight] = useState(images[0]?.metadata?.height || 0);
|
const [height, setHeight] = useState(images[0]?.metadata?.height || 0);
|
||||||
|
|
||||||
const onResize = useCallback(() => {
|
const onImageLoaded = useCallback(() => {
|
||||||
if (!ref.current) return;
|
|
||||||
ref.current.slider.refresh();
|
|
||||||
const el = slides.current[current];
|
const el = slides.current[current];
|
||||||
if (!el) return;
|
if (!el) return;
|
||||||
const { height } = el.getBoundingClientRect();
|
const { height } = el.getBoundingClientRect();
|
||||||
setHeight(height);
|
setHeight(height);
|
||||||
}, [ref.current, slides.current, current]);
|
}, [current, slides.current]);
|
||||||
|
|
||||||
const onIndexChanged = useCallback(({ index }) => {
|
const onIndexChanged = useCallback(
|
||||||
setCurrent(index || 0);
|
({ index }) => {
|
||||||
}, []);
|
if (!slides.current) return;
|
||||||
|
setCurrent(index || 0);
|
||||||
|
},
|
||||||
|
[setCurrent, setHeight, slides.current, onImageLoaded]
|
||||||
|
);
|
||||||
|
|
||||||
|
const onResize = useCallback(() => {
|
||||||
|
if (!ref.current?.slider) return;
|
||||||
|
ref.current.slider.refresh();
|
||||||
|
}, [ref.current?.slider]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setCurrent(0);
|
setCurrent(0);
|
||||||
}, [node.id]);
|
}, [node.id]);
|
||||||
|
|
||||||
useEffect(onResize, [slides, current]);
|
useEffect(onResize, [slides]);
|
||||||
|
useEffect(onImageLoaded, [current]);
|
||||||
|
|
||||||
const onNext = useCallback(() => {
|
const onNext = useCallback(() => {
|
||||||
if (!ref.current || images.length <= 1 || current === images.length - 1) return;
|
if (!ref.current || images.length <= 1 || current === images.length - 1) return;
|
||||||
|
@ -81,7 +89,7 @@ const NodeImageTinySlider: FC<INodeComponentProps> = ({ node }) => {
|
||||||
<TinySlider settings={settings} ref={ref} onTransitionEnd={onIndexChanged}>
|
<TinySlider settings={settings} ref={ref} onTransitionEnd={onIndexChanged}>
|
||||||
{images.map((image, i) => (
|
{images.map((image, i) => (
|
||||||
<div className={styles.slide} key={image.id} ref={el => (slides.current[i] = el)}>
|
<div className={styles.slide} key={image.id} ref={el => (slides.current[i] = el)}>
|
||||||
<img src={getURL(image, PRESETS['1600'])} key={image.url} onLoad={onResize} />
|
<img src={getURL(image, PRESETS['1600'])} key={image.url} onLoad={onImageLoaded} />
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
</TinySlider>
|
</TinySlider>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue