Skip to content

Commit e856515

Browse files
feat(module:image): now supports horizontal and vertical flip (#8168)
1 parent d882eb9 commit e856515

5 files changed

Lines changed: 61 additions & 6 deletions

File tree

‎components/image/doc/index.en-US.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ Other attributes [<img\>](https://developer.mozilla.org/en-US/docs/Web/HTML/Elem
6161
| nzZoom | Zoom rate | `number` | 1 |
6262
| nzRotate | Rotate rate | `number` | 0 |
6363
| nzScaleStep | `1 + nzScaleStep` is the step to increase or decrease the scale | `number` | 0.5 |
64+
| nzFlipHorizontally | Flip image on horizontal vector | `boolean` | `false` |
65+
| nzFlipVertically | Flip image on vertical vector | `boolean` | `false` |
6466

6567
### NzImagePreviewRef
6668

‎components/image/doc/index.zh-CN.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ import { NzImageModule } from 'ng-zorro-antd/image';
6262
| nzZoom | 缩放比例 | `number` | 1 |
6363
| nzRotate | 旋转角度 | `number` | 0 |
6464
| nzScaleStep | `1 + nzScaleStep` 为缩放放大的每步倍数 | `number` | 0.5 |
65+
| nzFlipHorizontally | 在水平向量上翻转图像 | `boolean` | `false` |
66+
| nzFlipVertically | 在垂直向量上翻转图像 | `boolean` | `false` |
6567

6668
### NzImagePreviewRef
6769

‎components/image/image-preview-options.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export class NzImagePreviewOptions {
1313
nzZIndex?: number;
1414
nzZoom?: number;
1515
nzRotate?: number;
16+
nzFlipHorizontally?: boolean;
17+
nzFlipVertically?: boolean;
1618
nzScaleStep?: number;
1719
nzDirection?: Direction;
1820
}

‎components/image/image-preview.component.ts‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { getClientSize, getFitContentPosition, getOffset } from './utils';
3737
export interface NzImageContainerOperation {
3838
icon: string;
3939
type: string;
40-
40+
rotate?: number;
4141
onClick(): void;
4242
}
4343

@@ -67,7 +67,13 @@ const NZ_DEFAULT_ROTATE = 0;
6767
[class.ant-image-preview-operations-operation-disabled]="zoomOutDisabled && option.type === 'zoomOut'"
6868
(click)="option.onClick()"
6969
>
70-
<span class="ant-image-preview-operations-icon" nz-icon [nzType]="option.icon" nzTheme="outline"></span>
70+
<span
71+
class="ant-image-preview-operations-icon"
72+
nz-icon
73+
[nzType]="option.icon"
74+
[nzRotate]="option.rotate ?? 0"
75+
nzTheme="outline"
76+
></span>
7177
</li>
7278
}
7379
</ul>
@@ -185,6 +191,21 @@ export class NzImagePreviewComponent implements OnInit {
185191
this.onRotateLeft();
186192
},
187193
type: 'rotateLeft'
194+
},
195+
{
196+
icon: 'swap',
197+
onClick: () => {
198+
this.onHorizontalFlip();
199+
},
200+
type: 'flipHorizontally'
201+
},
202+
{
203+
icon: 'swap',
204+
onClick: () => {
205+
this.onVerticalFlip();
206+
},
207+
type: 'flipVertically',
208+
rotate: 90
188209
}
189210
];
190211

@@ -200,6 +221,8 @@ export class NzImagePreviewComponent implements OnInit {
200221
private zoom: number;
201222
private rotate: number;
202223
private scaleStep: number;
224+
private flipHorizontally: boolean;
225+
private flipVertically: boolean;
203226

204227
get animationDisabled(): boolean {
205228
return this.config.nzNoAnimation ?? false;
@@ -223,6 +246,8 @@ export class NzImagePreviewComponent implements OnInit {
223246
this.zoom = this.config.nzZoom ?? this._defaultNzZoom;
224247
this.scaleStep = this.config.nzScaleStep ?? this._defaultNzScaleStep;
225248
this.rotate = this.config.nzRotate ?? this._defaultNzRotate;
249+
this.flipHorizontally = this.config.nzFlipHorizontally ?? false;
250+
this.flipVertically = this.config.nzFlipVertically ?? false;
226251
this.updateZoomOutDisabled();
227252
this.updatePreviewImageTransform();
228253
this.updatePreviewImageWrapperTransform();
@@ -329,6 +354,16 @@ export class NzImagePreviewComponent implements OnInit {
329354
this.next();
330355
}
331356

357+
onHorizontalFlip(): void {
358+
this.flipHorizontally = !this.flipHorizontally;
359+
this.updatePreviewImageTransform();
360+
}
361+
362+
onVerticalFlip(): void {
363+
this.flipVertically = !this.flipVertically;
364+
this.updatePreviewImageTransform();
365+
}
366+
332367
onAnimationStart(event: AnimationEvent): void {
333368
if (event.toState === 'enter') {
334369
this.setEnterAnimationClass();
@@ -379,7 +414,9 @@ export class NzImagePreviewComponent implements OnInit {
379414
}
380415

381416
private updatePreviewImageTransform(): void {
382-
this.previewImageTransform = `scale3d(${this.zoom}, ${this.zoom}, 1) rotate(${this.rotate}deg)`;
417+
this.previewImageTransform = `scale3d(${this.zoom * (this.flipHorizontally ? -1 : 1)}, ${
418+
this.zoom * (this.flipVertically ? -1 : 1)
419+
}, 1) rotate(${this.rotate}deg)`;
383420
}
384421

385422
private updatePreviewImageWrapperTransform(): void {
@@ -416,6 +453,8 @@ export class NzImagePreviewComponent implements OnInit {
416453
this.zoom = this.config.nzZoom ?? this._defaultNzZoom;
417454
this.scaleStep = this.config.nzScaleStep ?? this._defaultNzScaleStep;
418455
this.rotate = this.config.nzRotate ?? this._defaultNzRotate;
456+
this.flipHorizontally = false;
457+
this.flipVertically = false;
419458
this.position = { ...initialPosition };
420459
}
421460
}

‎components/image/image.spec.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import {
2525
RotateLeftOutline,
2626
RotateRightOutline,
2727
ZoomInOutline,
28-
ZoomOutOutline
28+
ZoomOutOutline,
29+
SwapOutline
2930
} from '@ant-design/icons-angular/icons';
3031

3132
import { NzConfigService } from 'ng-zorro-antd/core/config';
@@ -206,7 +207,8 @@ describe('Preview', () => {
206207
LeftCircleOutline,
207208
RotateLeftOutline,
208209
RotateRightOutline,
209-
CloseCircleOutline
210+
CloseCircleOutline,
211+
SwapOutline
210212
]
211213
}
212214
]
@@ -289,7 +291,7 @@ describe('Preview', () => {
289291
});
290292

291293
describe('ImagePreview', () => {
292-
it('should rotate, zoom and close work', fakeAsync(() => {
294+
it('should rotate, zoom and close and flip work', fakeAsync(() => {
293295
context.firstSrc = QUICK_SRC;
294296
fixture.detectChanges();
295297
const image = debugElement.nativeElement.querySelector('img');
@@ -303,6 +305,8 @@ describe('Preview', () => {
303305
const zoomOut = operations[2];
304306
const rotateRight = operations[3];
305307
const rotateLeft = operations[4];
308+
const flipHorizontally = operations[5];
309+
const flipVertically = operations[6];
306310
dispatchFakeEvent(rotateLeft, 'click');
307311
tickChanges();
308312
expect(imageElement!.getAttribute('style')).toContain('transform: scale3d(1, 1, 1) rotate(-90deg)');
@@ -315,6 +319,12 @@ describe('Preview', () => {
315319
dispatchFakeEvent(zoomOut, 'click');
316320
tickChanges();
317321
expect(imageElement!.getAttribute('style')).toContain('transform: scale3d(1, 1, 1) rotate(0deg)');
322+
dispatchFakeEvent(flipHorizontally, 'click');
323+
expect(imageElement!.getAttribute('style')).toContain('transform: scale3d(-1, 1, 1) rotate(0deg)');
324+
tickChanges();
325+
dispatchFakeEvent(flipVertically, 'click');
326+
expect(imageElement!.getAttribute('style')).toContain('transform: scale3d(-1, -1, 1) rotate(0deg)');
327+
tickChanges();
318328
dispatchFakeEvent(close, 'click');
319329
tickChanges();
320330
previewElement = getPreviewElement();

0 commit comments

Comments
 (0)