Skip to content

Commit 63e65a8

Browse files
authored
Merge pull request #156 from czeckd/update-to-ng18
Update to ng18
2 parents 26a64b5 + bef42a6 commit 63e65a8

File tree

11 files changed

+241
-268
lines changed

11 files changed

+241
-268
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2023 David Czeck.
3+
Copyright (c) 2024 David Czeck.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Angular SVG Icon
44
=========
55

6-
The **angular-svg-icon** is an Angular 17 service and component that provides a
6+
The **angular-svg-icon** is an Angular 18 service and component that provides a
77
means to inline SVG files to allow for them to be easily styled by CSS and code.
88

99
The service provides an icon registery that loads and caches a SVG indexed by
@@ -18,7 +18,17 @@ This [demo](https://czeckd.github.io/angular-svg-icon/) shows this module in act
1818
```
1919
$ npm i angular-svg-icon --save
2020
```
21+
22+
## Versions
23+
24+
The latest version of the package is for Angular 18.
25+
26+
:grey_exclamation: **BREAKING CHANGE**: as of [email protected], the package was converted to use
27+
`inject` and `signal` from `@common/core` for improved performance. Thus method calls that are inputs
28+
should be avoided. Inputs are now signal inputs.
29+
2130
**Note on earlier versions of Angular:**
31+
- For Angular 17, use [email protected]
2232
- For Angular 16, use [email protected]
2333
- For Angular 15, use [email protected]
2434
- For Angular 14, use [email protected]
@@ -67,7 +77,7 @@ import { SvgIconComponent, provideAngularSvgIcon } from 'angular-svg-icon';
6777
export class AppModule {}
6878
```
6979

70-
**BREAKING CHANGE**: as of [email protected], an explicit call to `forRoot()`
80+
:grey_exclamation: **BREAKING CHANGE**: as of [email protected], an explicit call to `forRoot()`
7181
must be made on the module's import.
7282

7383
### Child Modules
@@ -323,5 +333,6 @@ MIT
323333

324334

325335
## Author
326-
- David Czeck [@czeckd](https://github.com/czeckd)
336+
- David Czeck [@czeckd](https://github.com/czeckd) and
337+
[community contributors](https://github.com/czeckd/angular-svg-icon/graphs/contributors). Thank you!
327338

angular.json

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@
4848
"src/styles.scss"
4949
],
5050
"scripts": [],
51-
"extractLicenses": false,
52-
"sourceMap": true,
53-
"optimization": false
51+
"extractLicenses": false
5452
},
5553
"configurations": {
5654
"production": {
@@ -60,10 +58,7 @@
6058
"with": "src/environments/environment.prod.ts"
6159
}
6260
],
63-
"optimization": true,
6461
"outputHashing": "all",
65-
"sourceMap": false,
66-
"extractLicenses": true,
6762
"budgets": [
6863
{
6964
"type": "initial",
@@ -75,19 +70,26 @@
7570
"maximumWarning": "6kb"
7671
}
7772
]
78-
}
79-
}
73+
},
74+
"development": {
75+
"optimization": false,
76+
"extractLicenses": false,
77+
"sourceMap": true
78+
}
79+
},
80+
"defaultConfiguration": "production"
8081
},
8182
"serve": {
8283
"builder": "@angular-devkit/build-angular:dev-server",
83-
"options": {
84-
"buildTarget": "svg-icon:build"
85-
},
8684
"configurations": {
8785
"production": {
8886
"buildTarget": "svg-icon:build:production"
89-
}
90-
}
87+
},
88+
"development": {
89+
"buildTarget": "svg-icon:build:development"
90+
}
91+
},
92+
"defaultConfiguration": "development"
9193
},
9294
"extract-i18n": {
9395
"builder": "@angular-devkit/build-angular:extract-i18n",
@@ -99,7 +101,7 @@
99101
"builder": "@angular-devkit/build-angular:karma",
100102
"options": {
101103
"main": "src/test.ts",
102-
"polyfills": "src/polyfills.ts",
104+
"polyfills": ["src/polyfills.ts"],
103105
"tsConfig": "tsconfig.spec.json",
104106
"karmaConfig": "karma.conf.js",
105107
"assets": [
@@ -137,13 +139,13 @@
137139
"options": {
138140
"tsConfig": "projects/angular-svg-icon/tsconfig.lib.json",
139141
"project": "projects/angular-svg-icon/ng-package.json"
140-
}
141-
, "configurations": {
142+
},
143+
"configurations": {
142144
"production": {
143145
"tsConfig": "projects/angular-svg-icon/tsconfig.lib.prod.json"
144146
}
145147
}
146-
},
148+
},
147149
"test": {
148150
"builder": "@angular-devkit/build-angular:karma",
149151
"options": {

package.json

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "svg-icon",
3-
"description": "Angular 17 component and service for inlining SVGs allowing them to be easily styled with CSS.",
4-
"version": "17.0.0",
3+
"description": "Angular 18 component and service for inlining SVGs allowing them to be easily styled with CSS.",
4+
"version": "18.0.0",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/czeckd/angular-svg-icon.git"
@@ -15,7 +15,7 @@
1515
],
1616
"scripts": {
1717
"ng": "ng",
18-
"start": "ng serve --configuration production",
18+
"start": "ng serve",
1919
"build": "ng build --configuration production",
2020
"dist": "ng build --configuration production angular-svg-icon && cp README.md LICENSE ./dist/angular-svg-icon/",
2121
"test": "ng test --source-map=false angular-svg-icon",
@@ -25,31 +25,32 @@
2525
},
2626
"private": true,
2727
"dependencies": {
28-
"@angular/animations": "^17.0.2",
29-
"@angular/common": "^17.0.2",
30-
"@angular/compiler": "^17.0.2",
31-
"@angular/core": "^17.0.2",
32-
"@angular/forms": "^17.0.2",
33-
"@angular/platform-browser": "^17.0.2",
34-
"@angular/platform-browser-dynamic": "^17.0.2",
35-
"@angular/router": "^17.0.2",
28+
"@angular/animations": "^18.0.2",
29+
"@angular/common": "^18.0.2",
30+
"@angular/compiler": "^18.0.2",
31+
"@angular/core": "^18.0.2",
32+
"@angular/forms": "^18.0.2",
33+
"@angular/platform-browser": "^18.0.2",
34+
"@angular/platform-browser-dynamic": "^18.0.2",
35+
"@angular/router": "^18.0.2",
3636
"rxjs": "~6.6.3",
3737
"tslib": "^2.3.1",
3838
"zone.js": "~0.14.2"
3939
},
4040
"devDependencies": {
41-
"@angular-devkit/build-angular": "^17.0.0",
42-
"@angular/cli": "^17.0.0",
43-
"@angular/compiler-cli": "^17.0.2",
41+
"@angular-devkit/build-angular": "^18.0.3",
42+
"@angular/cli": "^18.0.3",
43+
"@angular/compiler-cli": "^18.0.2",
4444
"@types/jasmine": "~4.0.0",
4545
"@types/node": "^12.11.1",
4646
"jasmine-core": "~4.1.0",
4747
"karma": "~6.3.0",
4848
"karma-chrome-launcher": "~3.1.0",
4949
"karma-coverage": "~2.2.0",
50+
"karma-coverage-istanbul-reporter": "^3.0.3",
5051
"karma-jasmine": "~5.0.0",
5152
"karma-jasmine-html-reporter": "~1.7.0",
52-
"ng-packagr": "^17.0.0",
53-
"typescript": "~5.2.2"
53+
"ng-packagr": "^18.0.0",
54+
"typescript": "~5.4.5"
5455
}
5556
}

projects/angular-svg-icon/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular-svg-icon",
3-
"description": "Angular 17 component and service for inlining SVGs allowing them to be easily styled with CSS.",
4-
"version": "17.0.0",
3+
"description": "Angular 18 component and service for inlining SVGs allowing them to be easily styled with CSS.",
4+
"version": "18.0.0",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/czeckd/angular-svg-icon.git"
@@ -14,8 +14,8 @@
1414
"icon"
1515
],
1616
"peerDependencies": {
17-
"@angular/core": ">=17.0.0",
18-
"@angular/common": ">=17.0.0",
17+
"@angular/core": ">=18.0.0",
18+
"@angular/common": ">=18.0.0",
1919
"rxjs": ">=6.6.3"
2020
},
2121
"dependencies": {

projects/angular-svg-icon/src/lib/svg-icon-registry.service.spec.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { Inject, Optional, PLATFORM_ID } from '@angular/core';
2-
import { TestBed, getTestBed } from '@angular/core/testing';
1+
import { TestBed } from '@angular/core/testing';
32
import { DOCUMENT } from '@angular/common';
43

54
import { of } from 'rxjs';
65

76
import { SvgLoader } from './svg-loader';
8-
import { SvgIconRegistryService } from './svg-icon-registry.service';
7+
import { SERVER_URL, SvgIconRegistryService } from './svg-icon-registry.service';
98

109
describe('SvgIconRegistryService', () => {
1110
let service: SvgIconRegistryService;
@@ -18,75 +17,72 @@ describe('SvgIconRegistryService', () => {
1817
beforeEach(() => {
1918
TestBed.configureTestingModule({
2019
providers: [
21-
{ provide: SvgLoader, userValue: mockSvgLoader },
22-
{ provide: PLATFORM_ID, useValue: 'browser' }
20+
{ provide: SvgLoader, useValue: mockSvgLoader },
21+
{ provide: SERVER_URL, useValue: serverUrl },
22+
SvgIconRegistryService
2323
]
2424
});
2525

26+
service = TestBed.inject(SvgIconRegistryService);
2627
document = TestBed.inject(DOCUMENT);
2728

2829
});
2930

3031
it ('should add svg that does not exist', () => {
31-
service = new SvgIconRegistryService(mockSvgLoader, PLATFORM_ID, serverUrl, document);
3232
const div = document.createElement('DIV');
3333
div.innerHTML = SVG;
3434
const svg = div.querySelector('svg') as SVGElement;
3535

3636
service.addSvg('svg', SVG);
3737

38-
service.getSvgByName('svg').subscribe(data => {
38+
service.getSvgByName('svg')?.subscribe(data => {
3939
expect(data).toEqual(svg);
4040
});
4141

4242
});
4343

4444
it ('should load svg by url', () => {
45-
service = new SvgIconRegistryService(mockSvgLoader, PLATFORM_ID, serverUrl, document);
4645
const div = document.createElement('DIV');
4746
div.innerHTML = SVG;
4847
const svg = div.querySelector('svg') as SVGElement;
4948
mockSvgLoader.getSvg.and.returnValue(of(SVG));
5049

51-
service.loadSvg('http://svg').subscribe(data => {
50+
service.loadSvg('http://svg')?.subscribe(data => {
5251
expect(data).toEqual(svg);
5352
});
5453
});
5554

5655
it ('should load svg only once', () => {
57-
service = new SvgIconRegistryService(mockSvgLoader, PLATFORM_ID, serverUrl, document);
5856
mockSvgLoader.getSvg.and.returnValue(of(SVG));
5957
mockSvgLoader.getSvg.calls.reset();
6058

61-
service.loadSvg('svg').subscribe(data => {});
62-
service.loadSvg('svg').subscribe(data => {});
59+
service.loadSvg('svg')?.subscribe(data => {});
60+
service.loadSvg('svg')?.subscribe(data => {});
6361

6462
expect(mockSvgLoader.getSvg).toHaveBeenCalledTimes(1);
6563
});
6664

6765
it ('should load svg by url and assign name', () => {
68-
service = new SvgIconRegistryService(mockSvgLoader, PLATFORM_ID, serverUrl, document);
6966
const div = document.createElement('DIV');
7067
div.innerHTML = SVG;
7168
const svg = div.querySelector('svg') as SVGElement;
7269
mockSvgLoader.getSvg.and.returnValue(of(SVG));
7370

74-
service.loadSvg('svg', 'sample').subscribe(data => {});
71+
service.loadSvg('svg', 'sample')?.subscribe(data => {});
7572

76-
service.getSvgByName('sample').subscribe(data => {
73+
service.getSvgByName(serverUrl + 'svg')?.subscribe(data => {
7774
expect(data).toEqual(svg);
7875
});
7976
});
8077

8178
it ('should remove svg', () => {
82-
service = new SvgIconRegistryService(mockSvgLoader, PLATFORM_ID, serverUrl, document);
8379
service.addSvg('svg', SVG);
8480

8581
service.unloadSvg('svg');
8682

87-
service.getSvgByName('svg').subscribe(data => {
88-
}, err => {
89-
expect(err).toBe(`No svg with name 'svg' has been loaded`);
83+
service.getSvgByName('svg')?.subscribe({
84+
error: (err) =>
85+
expect(err).toBe(`No svg with name 'svg' has been loaded`)
9086
});
9187
});
9288

projects/angular-svg-icon/src/lib/svg-icon-registry.service.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Inject, Injectable, InjectionToken, Optional, PLATFORM_ID, SkipSelf } from '@angular/core';
1+
import { Injectable, InjectionToken, Optional, SkipSelf, inject } from '@angular/core';
22

33
import { Observable, of as observableOf, throwError as observableThrowError } from 'rxjs';
44
import { catchError, finalize, map, share, tap } from 'rxjs/operators';
@@ -9,19 +9,13 @@ export const SERVER_URL = new InjectionToken<string>('SERVER_URL');
99

1010
@Injectable()
1111
export class SvgIconRegistryService {
12+
private loader = inject(SvgLoader);
13+
protected serverUrl = inject< string | undefined>(SERVER_URL, {optional: true});
14+
private document = inject(DOCUMENT);
1215

13-
private document: Document;
1416
private iconsByUrl = new Map<string, SVGElement>();
1517
private iconsLoadingByUrl = new Map<string, Observable<SVGElement>>();
1618

17-
constructor(
18-
private loader: SvgLoader,
19-
@Inject(PLATFORM_ID) private platformId: Object,
20-
@Optional() @Inject(SERVER_URL) protected serverUrl: string | undefined,
21-
@Optional() @Inject(DOCUMENT) private _document: any) {
22-
this.document = this._document;
23-
}
24-
2519
/** Add a SVG to the registry by passing a name and the SVG. */
2620
addSvg(name: string, data: string) {
2721
if (!this.iconsByUrl.has(name)) {
@@ -86,18 +80,12 @@ export class SvgIconRegistryService {
8680
}
8781

8882
export function SVG_ICON_REGISTRY_PROVIDER_FACTORY(
89-
parentRegistry: SvgIconRegistryService,
90-
loader: SvgLoader,
91-
platformId: object,
92-
serverUrl?: string,
93-
document?: any) {
94-
return parentRegistry || new SvgIconRegistryService(loader, platformId, serverUrl, document);
83+
parentRegistry: SvgIconRegistryService) {
84+
return parentRegistry || new SvgIconRegistryService();
9585
}
9686

9787
export const SVG_ICON_REGISTRY_PROVIDER = {
9888
provide: SvgIconRegistryService,
99-
deps: [ [new Optional(), new SkipSelf(), SvgIconRegistryService], SvgLoader, [PLATFORM_ID as InjectionToken<any>],
100-
[new Optional(), SERVER_URL as InjectionToken<string>], [new Optional(), DOCUMENT as InjectionToken<any>]
101-
],
89+
deps: [ [new Optional(), new SkipSelf(), SvgIconRegistryService] ],
10290
useFactory: SVG_ICON_REGISTRY_PROVIDER_FACTORY
10391
};

0 commit comments

Comments
 (0)