feat: support img no-optimization option

This commit is contained in:
DIYgod 2023-02-06 17:32:38 +00:00
parent 170689610d
commit 659fd7e2c9
No known key found for this signature in database
2 changed files with 28 additions and 1 deletions

View File

@ -18,8 +18,27 @@ export const Image: React.FC<
const [paddingTop, setPaddingTop] = React.useState("0")
const [autoWidth, setAutoWidth] = React.useState(0)
const autoSize = !width && !height && !fill
const noOptimization = className?.includes("no-optimization")
return (
return noOptimization ? (
// eslint-disable-next-line @next/next/no-img-element
<img
{...props}
src={toGateway(src)}
className={className}
alt={alt}
width={width}
height={height}
onLoad={({ target }) => {
if (autoSize) {
const { naturalWidth, naturalHeight } = target as HTMLImageElement
setPaddingTop(`calc(100% / (${naturalWidth} / ${naturalHeight})`)
setAutoWidth(naturalWidth)
}
}}
ref={imageRef}
/>
) : (
<span
className="inline-block w-full h-full overflow-hidden"
style={

View File

@ -18,6 +18,14 @@ const scheme = {
attributes: {
...defaultSchema.attributes,
div: [...(defaultSchema.attributes?.div || []), "className", "style", "id"],
img: [
...(defaultSchema.attributes?.img || []),
"className",
"style",
"id",
"width",
"height",
],
code: ["className"],
blockquote: allowedBlockquoteAttrs,
video: [