Skip to content

Commit

Permalink
feat: enable the left or right keyboard to switch image (#5642), close
Browse files Browse the repository at this point in the history
…#5630

* feat: enable the left or right keyboard to switch image

* fix: eslint error
  • Loading branch information
buqiyuan authored May 25, 2022
1 parent dead1a3 commit c7e079b
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions components/vc-image/src/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Dialog from '../../vc-dialog';
import { type IDialogChildProps, dialogPropTypes } from '../../vc-dialog/IDialogPropTypes';
import { getOffset } from '../../vc-util/Dom/css';
import addEventListener from '../../vc-util/Dom/addEventListener';
import KeyCode from '../../_util/KeyCode';
import { warning } from '../../vc-util/warning';
import useFrameSetState from './hooks/useFrameSetState';
import getFixScaleEleTransPosition from './getFixScaleEleTransPosition';
Expand Down Expand Up @@ -221,6 +222,32 @@ const Preview = defineComponent({
lastWheelZoomDirection.value = { wheelDirection };
};

const onKeyDown = (event: KeyboardEvent) => {
if (!props.visible || !showLeftOrRightSwitches.value) return;

event.preventDefault();
if (event.keyCode === KeyCode.LEFT) {
if (currentPreviewIndex.value > 0) {
setCurrent(previewUrlsKeys.value[currentPreviewIndex.value - 1]);
}
} else if (event.keyCode === KeyCode.RIGHT) {
if (currentPreviewIndex.value < previewGroupCount.value - 1) {
setCurrent(previewUrlsKeys.value[currentPreviewIndex.value + 1]);
}
}
};

const onDoubleClick = () => {
if (props.visible) {
if (scale.value !== 1) {
scale.value = 1;
}
if (position.x !== initialPosition.x || position.y !== initialPosition.y) {
setPosition(initialPosition);
}
}
};

let removeListeners = () => {};
onMounted(() => {
watch(
Expand All @@ -235,6 +262,7 @@ const Preview = defineComponent({
const onScrollWheelListener = addEventListener(window, 'wheel', onWheelMove, {
passive: false,
});
const onKeyDownListener = addEventListener(window, 'keydown', onKeyDown, false);

try {
// Resolve if in iframe lost event
Expand All @@ -257,6 +285,7 @@ const Preview = defineComponent({
onMouseUpListener.remove();
onMouseMoveListener.remove();
onScrollWheelListener.remove();
onKeyDownListener.remove();

/* istanbul ignore next */
if (onTopMouseUpListener) onTopMouseUpListener.remove();
Expand Down Expand Up @@ -310,6 +339,7 @@ const Preview = defineComponent({
>
<img
onMousedown={onMouseDown}
onDblclick={onDoubleClick}
ref={imgRef}
class={`${props.prefixCls}-img`}
src={combinationSrc.value}
Expand Down

0 comments on commit c7e079b

Please sign in to comment.