From dca252434b3e776f541a5f2d77b526ac712d0479 Mon Sep 17 00:00:00 2001 From: Danborad Date: Tue, 1 Sep 2026 11:16:42 +0800 Subject: [PATCH] =?UTF-8?q?fix(canvas):=20=E7=94=BB=E5=B8=83=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E4=B8=8E=E4=BA=91=E7=AB=AF=E5=90=8C=E6=AD=A5=20-=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=A7=A3=E5=8E=8B=E5=AA=92=E4=BD=93=E7=9B=B4?= =?UTF-8?q?=E4=BC=A0=E5=AF=B9=E8=B1=A1=E5=AD=98=E5=82=A8=E4=B8=8E=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E7=BC=93=E5=AD=98=E8=87=AA=E6=84=88=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=B0=81=E9=9D=A2=E5=AE=9E=E6=97=B6=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E8=BF=9B=E5=BA=A6=E4=B8=8E=E9=98=B2=E8=AF=AF=E5=85=B3=E6=8F=90?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/components/cached-resource-image.tsx | 78 +++++-- .../components/canvas/canvas-project-card.tsx | 73 +++++-- web/src/pages/canvas/index.tsx | 204 ++++++++++++------ web/src/services/user-data-sync.ts | 159 +++++++++++--- web/src/stores/use-sync-progress-store.ts | 68 ++++++ 5 files changed, 458 insertions(+), 124 deletions(-) create mode 100644 web/src/stores/use-sync-progress-store.ts diff --git a/web/src/components/cached-resource-image.tsx b/web/src/components/cached-resource-image.tsx index efda66929..fe0213322 100644 --- a/web/src/components/cached-resource-image.tsx +++ b/web/src/components/cached-resource-image.tsx @@ -2,6 +2,7 @@ import { useEffect, useRef, useState, type ImgHTMLAttributes, type ReactNode } f import { resourceIdFromStorageKey } from "@/services/api/resources"; import { cacheResourceObjectUrl } from "@/services/resource-blob-cache"; +import { resolveImageUrl } from "@/services/image-storage"; type CachedResourceImageProps = Omit, "src"> & { storageKey?: string; @@ -12,10 +13,11 @@ type CachedResourceImageProps = Omit, "src"> /** * 资源图片优先读取按用户隔离的本地 Blob 缓存,避免刷新后再次从对象存储下载。 - * 普通外链、data URL 和本地 Blob URL 不经过资源缓存,保持原有行为。 + * 本地 image: 类型的 storageKey 也会自动从 LocalForage 恢复有效的 Object URL。 */ export function CachedResourceImage({ storageKey, src = "", fallback = null, eager = false, onError, ...props }: CachedResourceImageProps) { const remoteResource = Boolean(resourceIdFromStorageKey(storageKey)); + const localImageResource = Boolean(storageKey && storageKey.startsWith("image:")); const targetRef = useRef(null); const [nearViewport, setNearViewport] = useState(eager || !remoteResource); const [cachedSrc, setCachedSrc] = useState(remoteResource ? "" : src); @@ -44,32 +46,64 @@ export function CachedResourceImage({ storageKey, src = "", fallback = null, eag useEffect(() => { let cancelled = false; setCacheFailed(false); - if (!remoteResource || !storageKey) { - setCachedSrc(src); - return () => { cancelled = true; }; - } - if (!nearViewport) { - setCachedSrc(""); - return () => { cancelled = true; }; - } - setCachedSrc(""); - const resolve = cacheResourceObjectUrl(storageKey); - void resolve.then((url) => { - if (!cancelled) setCachedSrc(url || src); - }).catch(() => { - if (!cancelled) { - setCacheFailed(true); - setCachedSrc(src); + if (remoteResource && storageKey) { + if (!nearViewport) { + setCachedSrc(""); + return () => { cancelled = true; }; } - }); - return () => { cancelled = true; }; - }, [nearViewport, remoteResource, src, storageKey]); + setCachedSrc(""); + const resolve = cacheResourceObjectUrl(storageKey); + void resolve.then((url) => { + if (!cancelled) setCachedSrc(url || src); + }).catch(() => { + if (!cancelled) { + setCacheFailed(true); + setCachedSrc(src); + } + }); + return () => { cancelled = true; }; + } - if (!remoteResource) return ; + if (localImageResource && storageKey) { + void resolveImageUrl(storageKey, src).then((url) => { + if (!cancelled) setCachedSrc(url || src); + }).catch(() => { + if (!cancelled) setCachedSrc(src); + }); + return () => { cancelled = true; }; + } + + setCachedSrc(src); + return () => { cancelled = true; }; + }, [localImageResource, nearViewport, remoteResource, src, storageKey]); + + const handleImgError = (e: React.SyntheticEvent) => { + if (localImageResource && storageKey && cachedSrc.startsWith("blob:")) { + void resolveImageUrl(storageKey).then((url) => { + if (url && url !== cachedSrc) { + setCachedSrc(url); + return; + } + setCacheFailed(true); + onError?.(e); + }).catch(() => { + setCacheFailed(true); + onError?.(e); + }); + return; + } + setCacheFailed(true); + onError?.(e); + }; + + if (!remoteResource) { + if (cacheFailed && fallback) return <>{fallback}; + return ; + } return ( - {cachedSrc && !(cacheFailed && !src) ? : fallback} + {cachedSrc && !(cacheFailed && !src) ? : fallback} ); } diff --git a/web/src/components/canvas/canvas-project-card.tsx b/web/src/components/canvas/canvas-project-card.tsx index 87a70d28a..df63add13 100644 --- a/web/src/components/canvas/canvas-project-card.tsx +++ b/web/src/components/canvas/canvas-project-card.tsx @@ -1,4 +1,4 @@ -import { Check, Clapperboard, Download, FileText, Frame, Image as ImageIcon, MoreHorizontal, Music2, Pencil, Plus, Settings2, Sparkles, Trash2, Video, X } from "lucide-react"; +import { Check, Clapperboard, CloudUpload, Download, FileText, Frame, Image as ImageIcon, MoreHorizontal, Music2, Pencil, Plus, Settings2, Sparkles, Trash2, Video, X } from "lucide-react"; import type { ReactNode } from "react"; import { useNavigate, useSearchParams } from "react-router"; import { Dropdown, Input } from "antd"; @@ -11,6 +11,7 @@ import { resourceFileUrl, resourceIdFromStorageKey } from "@/services/api/resour import { resolveBackendApiUrl } from "@/stores/use-config-store"; import { CachedResourceImage } from "@/components/cached-resource-image"; import { cn } from "@/lib/utils"; +import { useSyncProgressStore } from "@/stores/use-sync-progress-store"; export function CanvasCreateCard({ disabled, onClick }: { disabled?: boolean; onClick: () => void }) { return