Skip to content

Commit

Permalink
feat: add keyboard shortcuts and scroll control for image preview
Browse files Browse the repository at this point in the history
  • Loading branch information
oljc committed Aug 6, 2023
1 parent c0c7710 commit e359bba
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/web-vue/components/image/preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
<div
v-if="mergedVisible"
ref="refWrapper"
tabindex="0"
:class="`${prefixCls}-wrapper`"
@click="onMaskClick"
@wheel.passive="onWheel"
@wheel.prevent.stop="onWheel"
>
<!-- img -->
<div
Expand Down Expand Up @@ -91,6 +92,7 @@ import {
h,
CSSProperties,
onBeforeUnmount,
nextTick,
} from 'vue';
import useMergeState from '../_hooks/use-merge-state';
import { getPrefixCls } from '../_utils/global-config';
Expand Down Expand Up @@ -338,6 +340,8 @@ export default defineComponent({
const handleKeyDown = (ev: KeyboardEvent) => {
ev.stopPropagation();
ev.preventDefault();
switch (ev.key) {
case KEYBOARD_KEY.ESC:
props.escToClose && close();
Expand All @@ -363,9 +367,11 @@ export default defineComponent({
};
const onWheel = throttleByRaf((e: WheelEvent) => {
e.preventDefault();
e.stopPropagation();
if (!props.wheelZoom) return;
e.stopPropagation();
const delta = e.deltaY || e.deltaX;
const action = delta > 0 ? 'zoomOut' : 'zoomIn';
const newScale = getScaleByRate(scale.value, zoomRate.value, action);
Expand All @@ -375,6 +381,9 @@ export default defineComponent({
let globalKeyDownListener = false;
const addGlobalKeyDownListener = () => {
nextTick(() => {
refWrapper?.value?.focus();
});
if (props.keyboard && !globalKeyDownListener) {
globalKeyDownListener = true;
on(container.value, 'keydown', handleKeyDown);
Expand Down Expand Up @@ -407,6 +416,7 @@ export default defineComponent({
}
function onMaskClick(e: MouseEvent) {
refWrapper?.value?.focus();
if (maskClosable.value && e.target === e.currentTarget) {
close();
}
Expand Down

0 comments on commit e359bba

Please sign in to comment.