forked from FortAwesome/angular-fontawesome
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced
FaIconLibrary
managed by Angular
Usage: ``` export class AppModule { constructor(library: FaIconLibrary) { library.addIcons(faCoffee); } } ``` Using DI instead of the syntax discussed in the issue (`FontAwesomeModule.withIcons(faUser)`) as this is more flexible and future proof given that future version of Angular renderer (Ivy) is going make usage of modules optional. --- Deprecated using library from `@fortawesome/fontawesome-svg-core` in favor of the new class. Previous library was problematic in several ways: - global variable, which was shared by all code on the page - more complicated for consumers as they need to know about existence of `fontawesome-svg-core` and that it is used by `angular-fontawesome` - library from `fontawesome-svg-core` implementation was pretty complex This deprecation is the first step on the way to make `fontawesome-svg-core` an implementation detail of `angular-fontawesome`, which consumers don't need to be aware about. Fixes FortAwesome#3
- Loading branch information
Showing
19 changed files
with
350 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Upgrading 0.4.0 to 0.5.0 | ||
|
||
## Migrate from global icon library to FaIconLibrary | ||
|
||
Icon library from `@fortawesome/fontawesome-svg-core` (referred as *global icon library*) is deprecated in favour of `FaIconLibrary` provided by `@fortawesome/angular-fontawesome` and managed by Angular (referred as *icon library*). | ||
|
||
The library will emit a warning when icon definition from a global icon library is used, so to migrate you'll need to replace all usages of the global icon library. | ||
|
||
```diff | ||
import { NgModule } from '@angular/core'; | ||
import { BrowserModule } from '@angular/platform-browser'; | ||
-import { FontAwesomeModule } from '@fortawesome/angular-fontawesome'; | ||
-import { library } from '@fortawesome/fontawesome-svg-core'; | ||
+import { FontAwesomeModule, FaIconLibrary } from '@fortawesome/angular-fontawesome'; | ||
import { faCoffee, fas } from '@fortawesome/free-solid-svg-icons'; | ||
import { AppComponent } from './app.component'; | ||
|
||
@NgModule({ | ||
declarations: [AppComponent], | ||
imports: [BrowserModule, FontAwesomeModule], | ||
bootstrap: [AppComponent] | ||
}) | ||
export class AppModule { | ||
- constructor() { | ||
+ constructor(library: FaIconLibrary) { | ||
- library.add(fas, faCoffee); | ||
+ library.addIconPacks(fas); | ||
+ library.addIcons(faCoffee); | ||
} | ||
} | ||
``` | ||
|
||
You can also use `FaConfig.globalLibrary` property to change the warning into the hard error or suppress warnings altogether (not recommended). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +0,0 @@ | ||
# Changing the default prefix | ||
|
||
The default prefix, `fas`, can be adjusted by injecting the `FaConfig` and modifying the `defaultPrefix` property. | ||
|
||
```typescript | ||
import { FaConfig } from '@fortawesome/angular-fontawesome'; | ||
|
||
export class AppComponent { | ||
constructor(private faConfig: FaConfig) { | ||
this.faConfig.defaultPrefix = 'far'; | ||
} | ||
} | ||
``` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,40 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { IconPrefix } from '@fortawesome/fontawesome-common-types'; | ||
import { FaIconLibrary } from './icon-library'; | ||
|
||
@Injectable({providedIn: 'root'}) | ||
export class FaConfig { | ||
/** | ||
* Default prefix to use, when one is not provided with the icon name. | ||
* | ||
* @default 'fas' | ||
*/ | ||
defaultPrefix: IconPrefix = 'fas'; | ||
|
||
/** | ||
* Whether components should lookup icon definitions in the global icon | ||
* library (the one available from | ||
* `import { library } from '@fortawesome/fontawesome-svg-core')`. | ||
* | ||
* See https://github.com/FortAwesome/angular-fontawesome/blob/master/docs/usage/icon-library.md | ||
* for detailed description of library modes. | ||
* | ||
* - 'unset' - Components should lookup icon definitions in the global library | ||
* and emit warning if they find a definition there. This option is a default | ||
* to assist existing applications with a migration. Applications are expected | ||
* to switch to using {@link FaIconLibrary}. | ||
* - true - Components should lookup icon definitions in the global library. | ||
* Note that global icon library is deprecated and support for it will be | ||
* removed. This option can be used to temporarily suppress warnings. | ||
* - false - Components should not lookup icon definitions in the global | ||
* library. Library will throw an error if missing icon is found in the global | ||
* library. | ||
* | ||
* @deprecated This option is deprecated since 0.5.0. In 0.6.0 default will | ||
* be changed to false. In 0.7.0 the option will be removed together with the | ||
* support for the global icon library. | ||
* | ||
* @default 'unset' | ||
*/ | ||
globalLibrary: boolean | 'unset' = 'unset'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { inject } from '@angular/core/testing'; | ||
import { far, faSun as farSun, faUser as farUser } from '@fortawesome/free-regular-svg-icons'; | ||
import { fas, faSun, faUser } from '@fortawesome/free-solid-svg-icons'; | ||
import { FaIconLibrary } from './icon-library'; | ||
|
||
describe('FaIconLibrary', () => { | ||
it('should be possible to add an icon', inject([FaIconLibrary], (library: FaIconLibrary) => { | ||
library.addIcons(faUser); | ||
expect(library.getIconDefinition('fas', 'user')).toBe(faUser); | ||
})); | ||
|
||
it('should be possible to add multiple icons', inject([FaIconLibrary], (library: FaIconLibrary) => { | ||
library.addIcons(faUser, farUser); | ||
expect(library.getIconDefinition('fas', 'user')).toBe(faUser); | ||
expect(library.getIconDefinition('far', 'user')).toBe(farUser); | ||
})); | ||
|
||
it('should be possible to add an icon pack', inject([FaIconLibrary], (library: FaIconLibrary) => { | ||
library.addIconPacks(far); | ||
expect(library.getIconDefinition('far', 'user')).toBe(farUser); | ||
})); | ||
|
||
it('should be possible to add multiple icon packs', inject([FaIconLibrary], (library: FaIconLibrary) => { | ||
library.addIconPacks(far, fas); | ||
expect(library.getIconDefinition('fas', 'sun')).toBe(faSun); | ||
expect(library.getIconDefinition('far', 'sun')).toBe(farSun); | ||
})); | ||
|
||
it('should be possible to get an icon', inject([FaIconLibrary], (library: FaIconLibrary) => { | ||
library.addIcons(faUser); | ||
expect(library.getIconDefinition('fas', 'user')).toBe(faUser); | ||
})); | ||
|
||
it('should return null if icon prefix was not registered', inject([FaIconLibrary], (library: FaIconLibrary) => { | ||
expect(library.getIconDefinition('fas', 'user')).toBeNull(); | ||
})); | ||
|
||
it('should return null if icon name is not registered', inject([FaIconLibrary], (library: FaIconLibrary) => { | ||
library.addIcons(faUser); | ||
expect(library.getIconDefinition('fas', 'sun')).toBeNull(); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { IconDefinition, IconName, IconPack, IconPrefix } from '@fortawesome/fontawesome-svg-core'; | ||
|
||
@Injectable({providedIn: 'root'}) | ||
export class FaIconLibrary { | ||
private definitions: { [prefix: string]: { [name: string]: IconDefinition } } = {}; | ||
|
||
addIcons(...icons: IconDefinition[]) { | ||
for (let i = 0; i < icons.length; i++) { | ||
const icon = icons[i]; | ||
if (!(icon.prefix in this.definitions)) { | ||
this.definitions[icon.prefix] = {}; | ||
} | ||
this.definitions[icon.prefix][icon.iconName] = icon; | ||
} | ||
} | ||
|
||
addIconPacks(...packs: IconPack[]) { | ||
for (let i = 0; i < packs.length; i++) { | ||
const pack = packs[i]; | ||
const icons = Object.keys(pack).map((key) => pack[key]); | ||
this.addIcons(...icons); | ||
} | ||
} | ||
|
||
getIconDefinition(prefix: IconPrefix, name: IconName): IconDefinition | null { | ||
if (prefix in this.definitions && name in this.definitions[prefix]) { | ||
return this.definitions[prefix][name]; | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.