Skip to content
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

fix(ImagePreview): incorrect image slot scaling #12377

Closed
wants to merge 2 commits into from
Closed
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
28 changes: 27 additions & 1 deletion packages/vant/src/image-preview/ImagePreviewItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
reactive,
defineComponent,
type CSSProperties,
VNode,
} from 'vue';

// Utils
Expand Down Expand Up @@ -118,6 +119,28 @@ export default defineComponent({
return 0;
});

const isImgElm = computed(() => {
if (!slots.image) return false;
const vNodeContent = slots.image({ src: props.src })[0];
return hasImgNode(vNodeContent);
});

const hasImgNode = (vNodeContent: VNode) => {
if (vNodeContent.type === 'img') {
return true;
} else if (
Array.isArray(vNodeContent.children) &&
vNodeContent.children.length !== 0
) {
for (const child of vNodeContent.children) {
if (hasImgNode(child as VNode)) {
return true;
}
}
}
return false;
};

const setScale = (scale: number, center?: { x: number; y: number }) => {
scale = clamp(scale, +props.minZoom, +props.maxZoom + 1);

Expand Down Expand Up @@ -389,7 +412,10 @@ export default defineComponent({
onTouchcancel={onTouchEnd}
>
{slots.image ? (
<div class={bem('image-wrap')}>
<div
class={bem('image-wrap')}
style={isImgElm.value ? imageStyle.value : {}}
>
{slots.image({ src: props.src })}
</div>
) : (
Expand Down
Loading