diff --git a/packages/ui/preview/src/Preview.tsx b/packages/ui/preview/src/Preview.tsx index 4b952b946..dc34e2cbd 100644 --- a/packages/ui/preview/src/Preview.tsx +++ b/packages/ui/preview/src/Preview.tsx @@ -2,6 +2,7 @@ import React, { forwardRef, useCallback, useState, useEffect, useRef, useMemo } import { cx, getPrefixCls } from '@hi-ui/classname' import { __DEV__ } from '@hi-ui/env' import { Portal } from '@hi-ui/portal' +import { Watermark, WatermarkProps } from '@hi-ui/watermark' import { CSSTransition } from 'react-transition-group' import { useUncontrolledState } from '@hi-ui/use-uncontrolled-state' import { HiBaseHTMLProps } from '@hi-ui/core' @@ -43,6 +44,8 @@ export const Preview = forwardRef( onError, onClose, src, + previewWatermarkProps, + ifAllowDownload = false, }, ref ) => { @@ -65,6 +68,27 @@ export const Preview = forwardRef( const isMultiple = useMemo(() => Array.isArray(src) && src.length > 1, [src]) + // 图片加水印 + const [waterMarkElement, setWaterMarkElement] = useState(undefined) + useEffect(() => { + if (visible) { + setWaterMarkElement(document.querySelector(`.${prefixCls}__box`) as HTMLDivElement) + } + }, [visible]) + + // 图片是否允许下载 + useEffect(() => { + const handleContextMenu = (evt: MouseEvent) => { + evt.preventDefault() + } + if (imgRef.current && !ifAllowDownload && visible) { + imgRef.current.addEventListener('contextmenu', handleContextMenu) + } + return () => { + imgRef.current?.removeEventListener('contextmenu', handleContextMenu) + } + }, [imgRef, visible, ifAllowDownload]) + // 重置图片 const resetTransform = useCallback(() => { updateImgTransform(defaultTransform) @@ -226,20 +250,29 @@ export const Preview = forwardRef( onMouseMove={onMoving} onKeyDown={handleKeyDown} > - { - setIsLoaded(true) - }} - onError={onError} - onMouseDown={onMoveStart} - onMouseUp={onMoveEnd} - src={Array.isArray(src) ? src[active] : src} - className={`${prefixCls}__image`} +
+ > + { + setIsLoaded(true) + }} + onError={onError} + onMouseDown={onMoveStart} + onMouseUp={onMoveEnd} + src={Array.isArray(src) ? src[active] : src} + className={`${prefixCls}__image`} + // style={{ + // transform: `scale(${imgTransform.scale}, ${imgTransform.scale}) translate(${imgTransform.translateX}px,${imgTransform.translateY}px) rotate(${imgTransform.rotate}deg)`, + // }} + /> +
{ @@ -286,6 +319,20 @@ export const Preview = forwardRef( )}
+ )} @@ -315,6 +362,14 @@ export interface PreviewProps extends Omit, 'onError'> { * 当前预览图片索引 */ defaultCurrent?: number + /** + * 设置图片水印 + */ + previewWatermarkProps?: Omit + /** + * 是否允许右键下载图片 + */ + ifAllowDownload?: boolean /** * 加载错误回调 */