lemmy-ui/src/shared/components/common/pictrs-image.tsx

87 lines
2.3 KiB
TypeScript
Raw Normal View History

import classNames from "classnames";
2021-02-22 02:39:04 +00:00
import { Component } from "inferno";
const iconThumbnailSize = 96;
const thumbnailSize = 256;
interface PictrsImageProps {
src: string;
alt?: string;
icon?: boolean;
2022-01-06 16:26:32 +00:00
banner?: boolean;
thumbnail?: boolean;
nsfw?: boolean;
iconOverlay?: boolean;
pushup?: boolean;
}
export class PictrsImage extends Component<PictrsImageProps, any> {
constructor(props: any, context: any) {
super(props, context);
}
render() {
return (
2023-06-22 16:52:47 +00:00
<picture>
2021-02-22 02:39:04 +00:00
<source srcSet={this.src("webp")} type="image/webp" />
<source srcSet={this.props.src} />
2021-02-22 02:39:04 +00:00
<source srcSet={this.src("jpg")} type="image/jpeg" />
<img
src={this.props.src}
alt={this.alt()}
title={this.alt()}
loading="lazy"
2023-06-22 16:52:47 +00:00
className={classNames("overflow-hidden pictrs-image", {
"img-fluid": !this.props.icon && !this.props.iconOverlay,
banner: this.props.banner,
"thumbnail rounded":
this.props.thumbnail && !this.props.icon && !this.props.banner,
"img-expanded slight-radius":
!this.props.thumbnail && !this.props.icon,
"img-blur": this.props.thumbnail && this.props.nsfw,
feat: Bootstrap 5 (#1378) * feat: Use Bootstrap 5; remove Bootstrap 4 * feat: Add link decoration override global var * fix: Change sr-only to visually-hidden * fix: Fix missing toggle button classes * fix: Use darker green to pass 3:1 contrast and allow foreground color generation * fix: Replace all mr- and ml- classes with me- (end) and ms- (start) classes * fix: Replace all pr- and pl- classes with pe- (end) and ps- (start) classes * fix: Replace custom-select with form-select d-inline-block * fix: Change max-width to Bootstrap 4's max-width * fix: Fix badge colors * fix: Replace deprecated btn-block class with d-block * fix: Temporary fix for missing btn-block styles * fix: Fix margin-left auto and margin-right auto * fix: Fix default border color * fix: Fix some button widths * fix: Fix form row margins * fix: Remove theme color maps; no longer necessary in Bootstrap 5 * fix: Remove unused gray * test commit * fix: Fix deprecated input-group-append usage * fix: Add missing col-form-label classes * fix: Fix some column widths * fix: Fix language dropdown style regression * fix: Fix toast background color * fix: Fix missing colors in red themes * fix: Fix default radio button appearance for toggles * fix: Fix missing margin in search form * fix: Fix search form widths * fix: Fix rate limit form columns * fix: Fix search filters layout * fix: Fix weird table background issue; re-compile from main updates * fix: Fix modlog filter layout * fix: Fix some horizontal margins * fix: Fix incorrect usage of input-group * chore: Empty commit to re-trigger Woodpecker job * fix: Fix incorrect Bootstrap 5 padding class * fix: Tighten up the home filter bars for the hell of it * fix: Fix home filter bar gap --------- Co-authored-by: SleeplessOne1917 <abias1122@gmail.com> Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
2023-06-20 12:01:29 +00:00
"rounded-circle img-cover img-icon me-2": this.props.icon,
"ms-2 mb-0 rounded-circle img-cover avatar-overlay":
2023-06-12 17:09:50 +00:00
this.props.iconOverlay,
"avatar-pushup": this.props.pushup,
})}
/>
</picture>
);
}
src(format: string): string {
// sample url:
// http://localhost:8535/pictrs/image/file.png?thumbnail=256&format=jpg
2023-06-05 21:31:12 +00:00
const split = this.props.src.split("/pictrs/image/");
// If theres not multiple, then its not a pictrs image
if (split.length == 1) {
return this.props.src;
}
2023-06-05 21:31:12 +00:00
const host = split[0];
const path = split[1];
2023-06-05 21:31:12 +00:00
const params = { format };
if (this.props.thumbnail) {
2021-02-22 02:39:04 +00:00
params["thumbnail"] = thumbnailSize;
} else if (this.props.icon) {
2021-02-22 02:39:04 +00:00
params["thumbnail"] = iconThumbnailSize;
}
2023-06-05 21:31:12 +00:00
const paramsStr = new URLSearchParams(params).toString();
const out = `${host}/pictrs/image/${path}?${paramsStr}`;
return out;
}
alt(): string {
if (this.props.icon) {
2021-02-22 02:39:04 +00:00
return "";
}
2021-02-22 02:39:04 +00:00
return this.props.alt || "";
}
}