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(image-viewer): 修改键盘事件绑定对象 #2958

Merged
merged 1 commit into from
Dec 7, 2023
Merged
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
24 changes: 20 additions & 4 deletions src/image-viewer/image-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export default defineComponent({
setVisibleValue(false);

unmountContent();
window.removeEventListener('keydown', keydownHandler);

props.onClose?.(ctx);
emit('close', ctx);
Expand All @@ -114,6 +113,8 @@ export default defineComponent({
};

const keydownHandler = (e: KeyboardEvent) => {
e.stopPropagation();

switch (e.code) {
case EVENT_CODE.left:
prevImage();
Expand Down Expand Up @@ -148,14 +149,20 @@ export default defineComponent({
containerRef.value.unmountContent();
}
};

const divRef = ref<HTMLDivElement>();
const getFocus = () => {
if (divRef.value) {
// 只设置tabindex值无法自动获取到焦点,使用focus获取焦点
divRef.value.focus();
}
};
watch(
() => visibleValue.value,
(val) => {
if (val) {
onRest();
window.addEventListener('keydown', keydownHandler);
mountContent();
getFocus();
}
},
);
Expand Down Expand Up @@ -205,6 +212,8 @@ export default defineComponent({
scale,
isMultipleImg,
containerRef,
keydownHandler,
divRef,
};
},
methods: {
Expand Down Expand Up @@ -295,7 +304,14 @@ export default defineComponent({
},
renderViewer() {
return (
<div class={this.wrapClass} style={{ zIndex: this.zIndexValue }} onWheel={this.onWheel}>
<div
tabindex={-1}
onKeydown={this.keydownHandler}
ref="divRef"
class={this.wrapClass}
style={{ zIndex: this.zIndexValue }}
onWheel={this.onWheel}
>
{!!this.showOverlayValue && (
<div class={`${this.COMPONENT_NAME}__modal-mask`} onClick={this.clickOverlayHandler} />
)}
Expand Down
Loading