If there's no image, use blurhash "image"
This commit is contained in:
parent
9071439c12
commit
402166e9b2
1 changed files with 18 additions and 3 deletions
|
@ -8,6 +8,7 @@ import {
|
||||||
MenuHeader,
|
MenuHeader,
|
||||||
MenuItem,
|
MenuItem,
|
||||||
} from '@szhsin/react-menu';
|
} from '@szhsin/react-menu';
|
||||||
|
import { decodeBlurHash } from 'fast-blurhash';
|
||||||
import mem from 'mem';
|
import mem from 'mem';
|
||||||
import pThrottle from 'p-throttle';
|
import pThrottle from 'p-throttle';
|
||||||
import { memo } from 'preact/compat';
|
import { memo } from 'preact/compat';
|
||||||
|
@ -1159,18 +1160,32 @@ function Card({ card, instance }) {
|
||||||
// );
|
// );
|
||||||
// }
|
// }
|
||||||
|
|
||||||
if (hasText && image) {
|
if (hasText && (image || (!type !== 'photo' && blurhash))) {
|
||||||
const domain = new URL(url).hostname.replace(/^www\./, '');
|
const domain = new URL(url).hostname.replace(/^www\./, '');
|
||||||
|
let blurhashImage;
|
||||||
|
if (!image) {
|
||||||
|
const w = 44;
|
||||||
|
const h = 44;
|
||||||
|
const blurhashPixels = decodeBlurHash(blurhash, w, h);
|
||||||
|
const canvas = document.createElement('canvas');
|
||||||
|
canvas.width = w;
|
||||||
|
canvas.height = h;
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
const imageData = ctx.createImageData(w, h);
|
||||||
|
imageData.data.set(blurhashPixels);
|
||||||
|
ctx.putImageData(imageData, 0, 0);
|
||||||
|
blurhashImage = canvas.toDataURL();
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<a
|
<a
|
||||||
href={cardStatusURL || url}
|
href={cardStatusURL || url}
|
||||||
target={cardStatusURL ? null : '_blank'}
|
target={cardStatusURL ? null : '_blank'}
|
||||||
rel="nofollow noopener noreferrer"
|
rel="nofollow noopener noreferrer"
|
||||||
class={`card link ${size}`}
|
class={`card link ${blurhashImage ? '' : size}`}
|
||||||
>
|
>
|
||||||
<div class="card-image">
|
<div class="card-image">
|
||||||
<img
|
<img
|
||||||
src={image}
|
src={image || blurhashImage}
|
||||||
width={width}
|
width={width}
|
||||||
height={height}
|
height={height}
|
||||||
loading="lazy"
|
loading="lazy"
|
||||||
|
|
Loading…
Add table
Reference in a new issue