Skip to content

Commit

Permalink
fix: create image using document method to avoid ReferenceError in SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
duongleh committed Apr 12, 2021
1 parent bc0e468 commit 107f0d3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion components/graph/core/minimap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export class Minimap {

zoomGSelection.attr('transform', zoomTransform);

const image = new Image();
const image = document.createElement('img');
image.onload = () => {
// Draw the svg content onto the buffer canvas.
const context = this.canvasBuffer.getContext('2d');
Expand Down
19 changes: 16 additions & 3 deletions components/image/image.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@
* found in the LICENSE file at /~https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
import { Direction, Directionality } from '@angular/cdk/bidi';
import { ChangeDetectorRef, Directive, ElementRef, Input, OnChanges, OnDestroy, OnInit, Optional, SimpleChanges } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import {
ChangeDetectorRef,
Directive,
ElementRef,
Inject,
Input,
OnChanges,
OnDestroy,
OnInit,
Optional,
SimpleChanges
} from '@angular/core';
import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
import { BooleanInput } from 'ng-zorro-antd/core/types';
import { BooleanInput, NzSafeAny } from 'ng-zorro-antd/core/types';
import { InputBoolean } from 'ng-zorro-antd/core/util';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
Expand Down Expand Up @@ -44,6 +56,7 @@ export class NzImageDirective implements OnInit, OnChanges, OnDestroy {
}

constructor(
@Inject(DOCUMENT) private document: NzSafeAny,
public nzConfigService: NzConfigService,
private elementRef: ElementRef,
private nzImageService: NzImageService,
Expand Down Expand Up @@ -107,7 +120,7 @@ export class NzImageDirective implements OnInit, OnChanges, OnDestroy {
* @private
*/
private backLoad(): void {
this.backLoadImage = new Image();
this.backLoadImage = this.document.createElement('img');
this.backLoadImage.src = this.nzSrc;
this.status = 'loading';

Expand Down
2 changes: 1 addition & 1 deletion components/upload/upload-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class NzUploadListComponent implements OnChanges {

private previewImage(file: File | Blob): Promise<string> {
return new Promise(resolve => {
if (!isImageFileType(file.type)) {
if (!isImageFileType(file.type) || !this.platform.isBrowser) {
resolve('');
return;
}
Expand Down
8 changes: 4 additions & 4 deletions components/upload/upload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ describe('upload', () => {
spyOn(window as any, 'Image').and.returnValue(new MockImage());

instance.listType = 'picture';
instance.items = [{ originFileObj: new File([''], '1.png', { type: 'image/' }), thgitumbUrl: undefined }];
instance.items = [{ originFileObj: new File([''], '1.png', { type: 'image/' }), thumbUrl: undefined }];
fixture.detectChanges();
tick();
expect(instance.items[0].thumbUrl.length).toBeGreaterThan(1);
Expand All @@ -959,12 +959,12 @@ describe('upload', () => {
spyOn(window as any, 'Image').and.returnValue(img);

instance.listType = 'picture';
instance.items = [{ originFileObj: new File([''], '1.png', { type: 'image/' }), thgitumbUrl: undefined }];
instance.items = [{ originFileObj: new File([''], '1.png', { type: 'image/' }), thumbUrl: undefined }];
fixture.detectChanges();
tick();
expect(instance.items[0].thumbUrl.length).toBeGreaterThan(1);
}));
it('should be ingore thumb when is invalid image data', () => {
it('should be ignore thumb when is invalid image data', () => {
instance.listType = 'picture';
instance.items = [{ originFileObj: new File([''], '1.pdf', { type: 'pdf' }), thumbUrl: undefined }];
fixture.detectChanges();
Expand All @@ -973,7 +973,7 @@ describe('upload', () => {
it('should be customize preview file', fakeAsync(() => {
instance.previewFile = () => of('11');
instance.listType = 'picture';
instance.items = [{ originFileObj: new File([''], '1.png', { type: 'image/' }), thgitumbUrl: undefined }];
instance.items = [{ originFileObj: new File([''], '1.png', { type: 'image/' }), thumbUrl: undefined }];
fixture.detectChanges();
tick();
expect(instance.items[0].thumbUrl).toBe('11');
Expand Down

0 comments on commit 107f0d3

Please sign in to comment.