Skip to content

fix(Image): can not cancel previewMask #5531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/image/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Previewable image.
| placeholder | Load placeholder, use default placeholder when set `true` | boolean \| slot | - | 2.0.0 |
| preview | preview config, disabled when `false` | boolean \| [previewType](#previewType) | true | 2.0.0 |
| src | Image path | string | - | 2.0.0 |
| previewMask | custom mask | slot | - | 3.2.0 |
| previewMask | custom mask | boolean \| function \| slot | - | 3.2.0 |
| width | Image width | string \| number | - | 2.0.0 |

### events
Expand Down
17 changes: 10 additions & 7 deletions components/image/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ const Image = defineComponent<ImageProps>({
v-slots={{
...slots,
previewMask:
slots.previewMask ??
(() => (
<div class={`${prefixCls.value}-mask-info`}>
<EyeOutlined />
{imageLocale?.preview}
</div>
)),
props.previewMask === false
? null
: props.previewMask ??
slots.previewMask ??
(() => (
<div class={`${prefixCls.value}-mask-info`}>
<EyeOutlined />
{imageLocale?.preview}
</div>
)),
}}
></ImageInternal>
);
Expand Down
2 changes: 1 addition & 1 deletion components/image/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cover: https://gw.alipayobjects.com/zos/antfincdn/D1dXz9PZqa/image.svg
| placeholder | 加载占位, 为 `true` 时使用默认占位 | boolean \| slot | - | 2.0.0 |
| preview | 预览参数,为 `false` 时禁用 | boolean \| [previewType](#previewType) | true | 2.0.0 |
| src | 图片地址 | string | - | 2.0.0 |
| previewMask | 自定义 mask | slot | - | 3.2.0 |
| previewMask | 自定义 mask | boolean \| function \| slot | - | 3.2.0 |
| width | 图像宽度 | string \| number | - | 2.0.0 |

### 事件
Expand Down
21 changes: 5 additions & 16 deletions components/vc-image/src/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,17 @@ export type ImagePreviewType = Omit<
icons?: PreviewProps['icons'];
};

export interface ImagePropsType extends Omit<ImgHTMLAttributes, 'placeholder' | 'onClick'> {
// Original
src?: string;
wrapperClassName?: string;
wrapperStyle?: CSSProperties;
prefixCls?: string;
previewPrefixCls?: string;
placeholder?: boolean;
fallback?: string;
preview?: boolean | ImagePreviewType;
onClick?: MouseEventHandler;
onError?: HTMLImageElement['onerror'];
}
export const imageProps = () => ({
src: String,
wrapperClassName: String,
wrapperStyle: { type: Object as PropType<CSSProperties>, default: undefined as CSSProperties },
rootClassName: String,
prefixCls: String,
previewPrefixCls: String,
previewMask: {
type: [Boolean, Function] as PropType<boolean | (() => any)>,
default: undefined,
},
placeholder: PropTypes.any,
fallback: String,
preview: {
Expand Down Expand Up @@ -103,9 +94,7 @@ const ImageInternal = defineComponent({
value: previewVisible,
onChange: onPreviewVisibleChange,
});
watch(previewVisible, val => {
setShowPreview(Boolean(val));
});

watch(isShowPreview, (val, preVal) => {
onPreviewVisibleChange(val, preVal);
});
Expand Down
24 changes: 10 additions & 14 deletions components/vc-image/src/PreviewGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ref, provide, defineComponent, inject, watch, reactive, computed, watch
import { type ImagePreviewType, mergeDefaultValue } from './Image';
import Preview from './Preview';
import type { PreviewProps } from './Preview';
import useMergedState from '../../_util/hooks/useMergedState';

export interface PreviewGroupPreview
extends Omit<ImagePreviewType, 'icons' | 'mask' | 'maskClassName'> {
Expand Down Expand Up @@ -85,10 +86,14 @@ const Group = defineComponent({
const current = ref<number>();

const previewVisible = computed(() => preview.value.visible);
const onPreviewVisibleChange = computed(() => preview.value.onVisibleChange);
const getPreviewContainer = computed(() => preview.value.getContainer);

const isShowPreview = ref(!!previewVisible.value);
const onPreviewVisibleChange = (val, preval) => {
preview.value.onVisibleChange?.(val, preval);
};
const [isShowPreview, setShowPreview] = useMergedState(!!previewVisible.value, {
value: previewVisible,
onChange: onPreviewVisibleChange,
});

const mousePosition = ref<{ x: number; y: number }>(null);
const isControlled = computed(() => previewVisible.value !== undefined);
Expand All @@ -115,9 +120,6 @@ const Group = defineComponent({
const setMousePosition = (val: null | { x: number; y: number }) => {
mousePosition.value = val;
};
const setShowPreview = (val: boolean) => {
isShowPreview.value = val;
};

const registerImage = (id: number, url: string, canPreview = true) => {
const unRegister = () => {
Expand All @@ -132,16 +134,10 @@ const Group = defineComponent({

const onPreviewClose = (e: any) => {
e?.stopPropagation();
isShowPreview.value = false;
mousePosition.value = null;
setShowPreview(false);
setMousePosition(null);
};

watch(previewVisible, () => {
isShowPreview.value = !!previewVisible.value;
});
watch(isShowPreview, (val, preVal) => {
onPreviewVisibleChange.value(val, preVal);
});
watch(
currentControlledKey,
val => {
Expand Down