From 4b490834a0f56d270b670dd0edc0f40f5c6bac20 Mon Sep 17 00:00:00 2001 From: MadCcc <1075746765@qq.com> Date: Fri, 18 Nov 2022 11:54:06 +0800 Subject: [PATCH] fix: preview-img style (#38662) * docs: PreviewImg * docs: add preview-img style * chore: style * chore: clean --- .dumi/rehypeAntd.ts | 2 +- .dumi/theme/builtins/ImagePreview/index.jsx | 163 ++++++++++++++++++++ .dumi/theme/common/GlobalStyles.tsx | 9 ++ 3 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 .dumi/theme/builtins/ImagePreview/index.jsx diff --git a/.dumi/rehypeAntd.ts b/.dumi/rehypeAntd.ts index c6827d63a6..a05398fafc 100644 --- a/.dumi/rehypeAntd.ts +++ b/.dumi/rehypeAntd.ts @@ -6,7 +6,7 @@ import { type HastRoot, type UnifiedTransformer, unistUtilVisit } from 'dumi'; */ function rehypeAntd(): UnifiedTransformer { return (tree, vFile) => { - unistUtilVisit.visit(tree, 'element', node => { + unistUtilVisit.visit(tree, 'element', (node) => { if (node.tagName === 'DumiDemoGrid') { // replace DumiDemoGrid to DemoWrapper, to implement demo toolbar node.tagName = 'DemoWrapper'; diff --git a/.dumi/theme/builtins/ImagePreview/index.jsx b/.dumi/theme/builtins/ImagePreview/index.jsx new file mode 100644 index 0000000000..331740e737 --- /dev/null +++ b/.dumi/theme/builtins/ImagePreview/index.jsx @@ -0,0 +1,163 @@ +import React from 'react'; +import classNames from 'classnames'; +import { Modal, Carousel } from 'antd'; + +function isGood(className) { + return /\bgood\b/i.test(className); +} + +function isBad(className) { + return /\bbad\b/i.test(className); +} + +function isInline(className) { + return /\binline\b/i.test(className); +} + +function PreviewImageBox({ + cover, + coverMeta, + imgs, + style, + previewVisible, + comparable, + onClick, + onCancel, +}) { + const onlyOneImg = comparable || imgs.length === 1; + const imageWrapperClassName = classNames('preview-image-wrapper', { + good: coverMeta.isGood, + bad: coverMeta.isBad, + }); + return ( +
+
+ {coverMeta.alt} +
+
{coverMeta.alt}
+
+ + + {comparable ? cover : imgs} + +
{coverMeta.alt}
+
+
+ ); +} + +function isGoodBadImg(imgMeta) { + return imgMeta.isGood || imgMeta.isBad; +} + +function isCompareImg(imgMeta) { + return isGoodBadImg(imgMeta) || imgMeta.inline; +} + +export default class ImagePreview extends React.Component { + constructor(props) { + super(props); + + this.state = { + previewVisible: {}, + }; + } + + handleClick = index => { + this.setState({ + previewVisible: { + [index]: true, + }, + }); + }; + + handleCancel = () => { + this.setState({ + previewVisible: {}, + }); + }; + + render() { + const { imgs } = this.props; + const imgsMeta = imgs.map(img => { + const { alt, description, src } = img; + const imgClassName = img.class; + return { + className: imgClassName, + alt, + description, + src, + isGood: isGood(imgClassName), + isBad: isBad(imgClassName), + inline: isInline(imgClassName), + }; + }); + + const imagesList = imgsMeta.map((meta, index) => { + const metaCopy = { ...meta }; + delete metaCopy.description; + delete metaCopy.isGood; + delete metaCopy.isBad; + return ( +
+
+ {meta.alt} +
+
+ ); + }); + + const comparable = + (imgs.length === 2 && imgsMeta.every(isCompareImg)) || + (imgs.length >= 2 && imgsMeta.every(isGoodBadImg)); + + const style = comparable ? { width: `${(100 / imgs.length).toFixed(3)}%` } : null; + + const hasCarousel = imgs.length > 1 && !comparable; + const previewClassName = classNames({ + 'preview-image-boxes': true, + clearfix: true, + 'preview-image-boxes-compare': comparable, + 'preview-image-boxes-with-carousel': hasCarousel, + }); + return ( +
+ {imagesList.map((_, index) => { + if (!comparable && index !== 0) { + return null; + } + + return ( + { + this.handleClick(index); + }} + onCancel={this.handleCancel} + /> + ); + })} +
+ ); + } +} diff --git a/.dumi/theme/common/GlobalStyles.tsx b/.dumi/theme/common/GlobalStyles.tsx index 11754922bf..d50c33457e 100644 --- a/.dumi/theme/common/GlobalStyles.tsx +++ b/.dumi/theme/common/GlobalStyles.tsx @@ -1892,6 +1892,15 @@ const GlobalStyles = () => { box-shadow: 1px 1px 6px rgba(0, 0, 0, 0.3); } +.preview-img { + max-width: 496px !important; + clear: both; + float: right; + margin: 0 0 70px 64px; + background-color: #f2f4f5; + padding: 16px; +} + .image-modal { text-align: center;