mirror of
https://github.com/hansjone/mind-map.git
synced 2026-10-09 23:53:18 +08:00
Initial commit: DSH mind-map canvas monorepo
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
commit
2ecd300ada
92 changed files with 20486 additions and 0 deletions
31
apps/web/src/image-cache.ts
Normal file
31
apps/web/src/image-cache.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/** Shared image loader for canvas card thumbnails. */
|
||||
const cache = new Map<string, HTMLImageElement | "loading" | "error">();
|
||||
|
||||
export function getCachedImage(
|
||||
url: string,
|
||||
onUpdate: () => void,
|
||||
): HTMLImageElement | null {
|
||||
const hit = cache.get(url);
|
||||
if (hit instanceof HTMLImageElement) return hit;
|
||||
if (hit === "loading" || hit === "error") return null;
|
||||
|
||||
cache.set(url, "loading");
|
||||
const img = new Image();
|
||||
img.decoding = "async";
|
||||
// Best-effort; some CDNs block canvas without CORS — we still draw when allowed.
|
||||
img.crossOrigin = "anonymous";
|
||||
img.onload = () => {
|
||||
cache.set(url, img);
|
||||
onUpdate();
|
||||
};
|
||||
img.onerror = () => {
|
||||
cache.set(url, "error");
|
||||
onUpdate();
|
||||
};
|
||||
img.src = url;
|
||||
return null;
|
||||
}
|
||||
|
||||
export function imageLoadFailed(url: string): boolean {
|
||||
return cache.get(url) === "error";
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue