Skip to content

Commit

Permalink
Process SVG on attr changing even if SVG url not changed arkon#134
Browse files Browse the repository at this point in the history
  • Loading branch information
brnomendes committed Dec 29, 2020
1 parent f044649 commit 75002a0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
13 changes: 13 additions & 0 deletions demo/src/demo/demo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { Component, OnInit } from '@angular/core';
<div [inlineSVG]="'img/symbol.svg#fish'"></div>
<div [inlineSVG]="'#fish'"></div>
<div [inlineSVG]="'#fish'" [injectComponent]="true"></div>
<div><button (click)="updateSize(10)">Increase</button><button (click)="updateSize(-10)">Decrease</button></div>
<div [inlineSVG]="'#fish'" [setSVGAttributes]="_changeAttrs"></div>
<div [inlineSVG]="'img/nope.svg'" [fallbackImgUrl]="'https://nodei.co/npm/ng-inline-svg.png?compact=true'"></div>
`
})
Expand All @@ -18,6 +20,10 @@ export class DemoComponent implements OnInit {
'width': '50',
'height': '50'
};
private _changeAttrs = {
'width': '50',
'height': '50'
};

ngOnInit() {
setTimeout(() => {
Expand All @@ -30,4 +36,11 @@ export class DemoComponent implements OnInit {
svg.setAttribute('width', '100');
return svg;
}

updateSize(value: number): void {
this._changeAttrs = {
'width': (parseInt(this._changeAttrs['width'], 10) + value).toString(),
'height': (parseInt(this._changeAttrs['height'], 10) + value).toString(),
};
}
}
9 changes: 5 additions & 4 deletions src/inline-svg.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ export class InlineSVGDirective implements OnInit, OnChanges, OnDestroy {
ngOnChanges(changes: SimpleChanges): void {
if (!this._isValidPlatform() || this._isSSRDisabled()) { return; }

if (changes['inlineSVG'] || changes['setSVGAttributes']) {
this._insertSVG();
const setSVGAttributesChanged = Boolean(changes['setSVGAttributes']);
if (changes['inlineSVG'] || setSVGAttributesChanged) {
this._insertSVG(setSVGAttributesChanged);
}
}

Expand All @@ -92,7 +93,7 @@ export class InlineSVGDirective implements OnInit, OnChanges, OnDestroy {
}
}

private _insertSVG(): void {
private _insertSVG(force = false): void {
if (!isPlatformServer(this.platformId) && !this._supportsSVG) { return; }

// Check if a URL was actually passed into the directive
Expand All @@ -102,7 +103,7 @@ export class InlineSVGDirective implements OnInit, OnChanges, OnDestroy {
}

// Short circuit if SVG URL hasn't changed
if (this.inlineSVG === this._prevUrl) {
if (!force && this.inlineSVG === this._prevUrl) {
return;
}
this._prevUrl = this.inlineSVG;
Expand Down

0 comments on commit 75002a0

Please sign in to comment.