Up And Down : première version
This commit is contained in:
commit
89c5430308
76 files changed
+20730
No files matched your search
@@ -0,0 +1,64 @@
|
||||
import { useState } from "react";
|
||||
import {
|
||||
FileCodeIcon,
|
||||
FileIcon,
|
||||
FileImageIcon,
|
||||
FileMusicIcon,
|
||||
FilePdfIcon,
|
||||
FileSheetIcon,
|
||||
FileSlidesIcon,
|
||||
FileTextIcon,
|
||||
FileVideoIcon,
|
||||
FileZipIcon,
|
||||
} from "./icons";
|
||||
import { browserImage, kindOf, serverThumb, type Kind } from "./filetypes";
|
||||
|
||||
const glyphs: Record<Kind, ReturnType<typeof FileIcon>["type"] | ((p: { size?: number }) => React.ReactElement)> = {
|
||||
image: FileImageIcon,
|
||||
video: FileVideoIcon,
|
||||
audio: FileMusicIcon,
|
||||
pdf: FilePdfIcon,
|
||||
text: FileTextIcon,
|
||||
code: FileCodeIcon,
|
||||
archive: FileZipIcon,
|
||||
doc: FileTextIcon,
|
||||
sheet: FileSheetIcon,
|
||||
slides: FileSlidesIcon,
|
||||
other: FileIcon,
|
||||
};
|
||||
|
||||
/** Icône d'un fichier d'après son type, colorée par famille (classe k-<type>). */
|
||||
export function FileGlyph({ name, size = 20 }: { name: string; size?: number }) {
|
||||
const kind = kindOf(name);
|
||||
const Icon = glyphs[kind] as (p: { size?: number }) => React.ReactElement;
|
||||
return (
|
||||
<span className={`fileglyph k-${kind}`} aria-hidden>
|
||||
<Icon size={size} />
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
/** Taille au-delà de laquelle une image n'est pas chargée telle quelle comme miniature. */
|
||||
const MAX_DIRECT = 12 << 20;
|
||||
|
||||
/**
|
||||
* Adresse d'une miniature : miniature du serveur pour JPEG, PNG et GIF, l'image
|
||||
* elle-même pour les autres formats du navigateur (WebP, SVG...) si elle est
|
||||
* raisonnable, sinon rien (icône de type).
|
||||
*/
|
||||
export const thumbSource = (name: string, size: number, thumb: () => string, direct: () => string): string | null => {
|
||||
if (serverThumb(name)) return thumb();
|
||||
if (browserImage(name) && size <= MAX_DIRECT) return direct();
|
||||
return null;
|
||||
};
|
||||
|
||||
/** Miniature d'image avec repli sur l'icône du type (format illisible, erreur de chargement). */
|
||||
export function Thumb({ name, src, glyph = 40, className = "" }: { name: string; src: string | null; glyph?: number; className?: string }) {
|
||||
const [failed, setFailed] = useState<string | null>(null);
|
||||
const ok = src && failed !== src;
|
||||
return (
|
||||
<span className={`thumb ${ok ? "" : "ph"} ${className}`} aria-hidden>
|
||||
{ok ? <img src={src} alt="" loading="lazy" decoding="async" draggable={false} onError={() => setFailed(src)} /> : <FileGlyph name={name} size={glyph} />}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user