Skip to content

feat(image): add new features #5479

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 13 commits into from
Apr 14, 2022
14 changes: 9 additions & 5 deletions components/vc-image/src/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import isNumber from 'lodash-es/isNumber';
import cn from '../../_util/classNames';
import PropTypes from '../../_util/vue-types';
import { getOffset } from '../../vc-util/Dom/css';
import useMergedState from '../../_util/hooks/useMergedState';
import Preview from './Preview';

import type { MouseEventHandler } from '../../_util/EventInterface';
Expand Down Expand Up @@ -95,9 +96,12 @@ const ImageInternal = defineComponent({
const getPreviewContainer = computed(() => preview.value.getContainer);

const isControlled = computed(() => previewVisible.value !== undefined);
const isShowPreview = ref(!!previewVisible.value);
const [isShowPreview, setShowPreview] = useMergedState(!!previewVisible.value, {
value: previewVisible,
onChange: onPreviewVisibleChange.value,
});
watch(previewVisible, val => {
isShowPreview.value = Boolean(val);
setShowPreview(Boolean(val));
});
watch(isShowPreview, (val, preVal) => {
onPreviewVisibleChange.value(val, preVal);
Expand Down Expand Up @@ -149,13 +153,13 @@ const ImageInternal = defineComponent({
if (isPreviewGroup.value) {
setGroupShowPreview(true);
} else {
isShowPreview.value = true;
setShowPreview(true);
}
emit('click', e);
};

const onPreviewClose = () => {
isShowPreview.value = false;
setShowPreview(false);
if (!isControlled.value) {
mousePosition.value = null;
}
Expand Down Expand Up @@ -247,7 +251,7 @@ const ImageInternal = defineComponent({
<div
class={wrappperClass}
onClick={
canPreview.value && !Object.is(previewVisible.value, false)
canPreview.value
? onPreview
: e => {
emit('click', e);
Expand Down
21 changes: 12 additions & 9 deletions components/vc-image/src/PreviewGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PropType, Ref, ComputedRef } from 'vue';
import { ref, provide, defineComponent, inject, watch, reactive, computed } from 'vue';
import { ref, provide, defineComponent, inject, watch, reactive, computed, watchEffect } from 'vue';
import { type ImagePreviewType, mergeDefaultValue } from './Image';
import Preview from './Preview';
import type { PreviewProps } from './Preview';
Expand Down Expand Up @@ -81,7 +81,7 @@ const Group = defineComponent({
: defaultValues;
});
const previewUrls = reactive<Map<number, PreviewUrl>>(new Map());
const current = ref(preview.value.current);
const current = ref<number>();

const previewVisible = computed(() => preview.value.visible);
const onPreviewVisibleChange = computed(() => preview.value.onVisibleChange);
Expand Down Expand Up @@ -135,12 +135,6 @@ const Group = defineComponent({
mousePosition.value = null;
};

const onPreviewAfterClose = () => {
if (!isShowPreview.value && isControlled.value) {
setCurrent(currentControlledKey.value);
}
};

watch(previewVisible, () => {
isShowPreview.value = !!previewVisible.value;
});
Expand All @@ -150,6 +144,16 @@ const Group = defineComponent({
watch(currentControlledKey, val => {
setCurrent(val);
});
watchEffect(
() => {
if (!isShowPreview.value && isControlled.value) {
setCurrent(currentControlledKey.value);
}
},
{
flush: 'post',
},
);

context.provide({
isPreviewGroup: ref(true),
Expand All @@ -173,7 +177,6 @@ const Group = defineComponent({
visible={isShowPreview.value}
prefixCls={props.previewPrefixCls}
onClose={onPreviewClose}
onAfterClose={onPreviewAfterClose}
mousePosition={mousePosition.value}
src={canPreviewUrls.value.get(current.value)}
icons={props.icons}
Expand Down