mirror of
https://github.com/docmost/docmost.git
synced 2026-08-26 00:07:04 +08:00
load page media more accurately
This commit is contained in:
@@ -8,7 +8,7 @@ import Download from "yet-another-react-lightbox/plugins/download";
|
|||||||
import Fullscreen from "yet-another-react-lightbox/plugins/fullscreen";
|
import Fullscreen from "yet-another-react-lightbox/plugins/fullscreen";
|
||||||
import Video from "yet-another-react-lightbox/plugins/video";
|
import Video from "yet-another-react-lightbox/plugins/video";
|
||||||
import Zoom from "yet-another-react-lightbox/plugins/zoom";
|
import Zoom from "yet-another-react-lightbox/plugins/zoom";
|
||||||
import { useMemo } from "react";
|
import { useEffect, useMemo, useState } from "react";
|
||||||
import i18n from "@/i18n.ts";
|
import i18n from "@/i18n.ts";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
@@ -95,12 +95,37 @@ export default function LightboxView({
|
|||||||
}: LightboxViewProps) {
|
}: LightboxViewProps) {
|
||||||
const { i18n: i18nInstance } = useTranslation();
|
const { i18n: i18nInstance } = useTranslation();
|
||||||
|
|
||||||
const slides = useMemo(
|
const selectedSlide = useMemo(
|
||||||
() => getPageMedia(editor),
|
() => getMedia(src, type),
|
||||||
[editor, open, i18nInstance.language]
|
[src, type, i18nInstance.language]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const [pageSlides, setPageSlides] = useState<Slide[]>([]);
|
||||||
|
const [loadedMediaKey, setLoadedMediaKey] = useState<string | null>(null);
|
||||||
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!open) return;
|
||||||
|
|
||||||
|
setLoadedMediaKey(null);
|
||||||
|
|
||||||
|
const frame = requestAnimationFrame(() => {
|
||||||
|
const slides = getPageMedia(editor);
|
||||||
|
|
||||||
|
setPageSlides(slides);
|
||||||
|
setLoadedMediaKey(`${type}:${src}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => cancelAnimationFrame(frame);
|
||||||
|
}, [editor, open, type, src]);
|
||||||
|
|
||||||
|
const slides = loadedMediaKey === `${type}:${src}` ? pageSlides : [selectedSlide];
|
||||||
|
|
||||||
const index = useMemo(() => {
|
const index = useMemo(() => {
|
||||||
|
if (!(pageSlides.length > 0)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
const idx = slides.findIndex((slide) =>
|
const idx = slides.findIndex((slide) =>
|
||||||
type === "video"
|
type === "video"
|
||||||
? "sources" in slide && slide.sources.some((s) => s.src === src)
|
? "sources" in slide && slide.sources.some((s) => s.src === src)
|
||||||
@@ -113,7 +138,7 @@ export default function LightboxView({
|
|||||||
<Lightbox
|
<Lightbox
|
||||||
open={open}
|
open={open}
|
||||||
close={onClose}
|
close={onClose}
|
||||||
index={index >= 0 ? index : 0}
|
index={index}
|
||||||
slides={slides}
|
slides={slides}
|
||||||
plugins={[Captions, Download, Fullscreen, Video, Zoom]}
|
plugins={[Captions, Download, Fullscreen, Video, Zoom]}
|
||||||
captions={{ descriptionTextAlign: "center" }}
|
captions={{ descriptionTextAlign: "center" }}
|
||||||
|
|||||||
Reference in New Issue
Block a user