1
1
import { log } from '$root/logger.js' ;
2
- import type { IconifyJSON } from '@iconify/types' ;
2
+ import type { ExtendedIconifyIcon , IconifyIcon , IconifyJSON } from '@iconify/types' ;
3
3
import type { IconifyIconCustomisations } from '@iconify/utils' ;
4
4
import { getIconData , iconToHTML , iconToSVG , replaceIDs , stringToIcon } from '@iconify/utils' ;
5
5
6
+ export const unknownIcon : IconifyIcon = {
7
+ body : '<g><rect width="80" height="80" style="fill: #087ebf; stroke-width: 0px;"/><text transform="translate(21.16 64.67)" style="fill: #fff; font-family: ArialMT, Arial; font-size: 67.75px;"><tspan x="0" y="0">?</tspan></text></g>' ,
8
+ height : 80 ,
9
+ width : 80 ,
10
+ } ;
11
+
6
12
export const iconsStore = new Map < string , IconifyJSON > ( ) ;
7
13
8
14
export const registerIconPacks = ( ...iconPacks : IconifyJSON [ ] ) => {
@@ -11,26 +17,47 @@ export const registerIconPacks = (...iconPacks: IconifyJSON[]) => {
11
17
}
12
18
} ;
13
19
14
- export const getIconSVG = ( iconName : string , customisations ?: IconifyIconCustomisations ) => {
20
+ const getRegisteredIconData = ( iconName : string , fallbackPrefix ?: string ) => {
21
+ const data = stringToIcon ( iconName , true , fallbackPrefix !== undefined ) ;
22
+ if ( ! data ) {
23
+ throw new Error ( `Invalid icon name: ${ iconName } ` ) ;
24
+ }
25
+ const prefix = data . prefix || fallbackPrefix ;
26
+ if ( ! prefix ) {
27
+ throw new Error ( `Icon name must contain a prefix: ${ iconName } ` ) ;
28
+ }
29
+ const icons = iconsStore . get ( prefix ) ;
30
+ if ( ! icons ) {
31
+ throw new Error ( `Icon set not found: ${ data . prefix } ` ) ;
32
+ }
33
+ const iconData = getIconData ( icons , data . name ) ;
34
+ if ( ! iconData ) {
35
+ throw new Error ( `Icon not found: ${ iconName } ` ) ;
36
+ }
37
+ return iconData ;
38
+ } ;
39
+
40
+ export const isIconAvailable = ( iconName : string ) => {
41
+ try {
42
+ getRegisteredIconData ( iconName ) ;
43
+ return true ;
44
+ } catch {
45
+ return false ;
46
+ }
47
+ } ;
48
+
49
+ export const getIconSVG = (
50
+ iconName : string ,
51
+ customisations ?: IconifyIconCustomisations & { fallbackPrefix ?: string }
52
+ ) => {
53
+ let iconData : ExtendedIconifyIcon ;
15
54
try {
16
- const data = stringToIcon ( iconName , true , true ) ;
17
- if ( ! data ) {
18
- throw new Error ( `Invalid icon name: ${ iconName } ` ) ;
19
- }
20
- const icons = iconsStore . get ( data . prefix || 'default' ) ;
21
- if ( ! icons ) {
22
- throw new Error ( `Icon set not found: ${ data . prefix } ` ) ;
23
- }
24
- const iconData = getIconData ( icons , data . name ) ;
25
- if ( ! iconData ) {
26
- throw new Error ( `Icon not found: ${ iconName } ` ) ;
27
- }
28
- const renderData = iconToSVG ( iconData , customisations ) ;
29
- const svg = iconToHTML ( replaceIDs ( renderData . body ) , renderData . attributes ) ;
30
- return svg ;
55
+ iconData = getRegisteredIconData ( iconName , customisations ?. fallbackPrefix ) ;
31
56
} catch ( e ) {
32
57
log . error ( e ) ;
33
- // Return unknown icon svg.
34
- return '<g><rect width="80" height="80" style="fill: #087ebf; stroke-width: 0px;"/><text transform="translate(21.16 64.67)" style="fill: #fff; font-family: ArialMT, Arial; font-size: 67.75px;"><tspan x="0" y="0">?</tspan></text></g>' ;
58
+ iconData = unknownIcon ;
35
59
}
60
+ const renderData = iconToSVG ( iconData , customisations ) ;
61
+ const svg = iconToHTML ( replaceIDs ( renderData . body ) , renderData . attributes ) ;
62
+ return svg ;
36
63
} ;
0 commit comments