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

feat(module:image): nz-image add press left or right to switch image #7321

Merged
merged 2 commits into from Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions components/image/doc/index.en-US.md
Expand Up @@ -60,6 +60,7 @@ Other attributes [<img\>](https://developer.mozilla.org/en-US/docs/Web/HTML/Elem
| nzZIndex | The z-index of the image preview | `number` | 1000 |
| nzZoom | Zoom rate | `number` | 1 |
| nzRotate | Rotate rate | `number` | 0 |
| nzEnableLeftRightArrow | Whether support press `left` or `right` to switch image | `boolean` | `true` |

### NzImagePreviewRef

Expand Down
1 change: 1 addition & 0 deletions components/image/doc/index.zh-CN.md
Expand Up @@ -60,6 +60,7 @@ import { NzImageModule } from 'ng-zorro-antd/image';
| nzZIndex | 设置预览层的 z-index | `number` | 1000 |
| nzZoom | 缩放比例 | `number` | 1 |
| nzRotate | 旋转角度 | `number` | 0 |
| nzEnableLeftRightArrow | 是否支持键盘方向键切换图片 | `boolean` | `true` |

### NzImagePreviewRef

Expand Down
1 change: 1 addition & 0 deletions components/image/image-preview-options.ts
Expand Up @@ -14,6 +14,7 @@ export class NzImagePreviewOptions {
nzZoom?: number;
nzRotate?: number;
nzDirection?: Direction;
nzEnableLeftRightArrow?: boolean = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不建议增加该字段, nzKeyboard 本身就是表示是否键盘操作,这里增加一个额外字段,倘若鼠标中间键切换时是不是也应该有另一个字段;因此容易产生歧义,以其如此,不如直接使用 nzKeyboard 来判断足够了。

}

export interface NzImage {
Expand Down
17 changes: 16 additions & 1 deletion components/image/image-preview-ref.ts
Expand Up @@ -3,7 +3,7 @@
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

import { ESCAPE, hasModifierKey } from '@angular/cdk/keycodes';
import { ESCAPE, hasModifierKey, LEFT_ARROW, RIGHT_ARROW } from '@angular/cdk/keycodes';
import { OverlayRef } from '@angular/cdk/overlay';
import { Subject } from 'rxjs';
import { filter, take, takeUntil } from 'rxjs/operators';
Expand All @@ -27,6 +27,21 @@ export class NzImagePreviewRef {
this.close();
});

overlayRef
amoycode marked this conversation as resolved.
Show resolved Hide resolved
.keydownEvents()
.pipe(
filter(
event =>
(this.config.nzEnableLeftRightArrow as boolean) &&
(event.keyCode === LEFT_ARROW || event.keyCode === RIGHT_ARROW) &&
!hasModifierKey(event)
)
)
.subscribe(event => {
event.preventDefault();
event.keyCode === LEFT_ARROW ? this.prev() : this.next();
});

overlayRef.detachments().subscribe(() => {
this.overlayRef.dispose();
});
Expand Down