1
- import { registerIcon , registerCollectionPromise } from "../SVGIconRegistry.js" ;
2
- import { fetchJsonOnce } from "../util/FetchHelper.js" ;
3
- import { getEffectiveAssetPath } from "../util/EffectiveAssetPath.js" ;
1
+ import getSharedResource from "../getSharedResource.js" ;
4
2
3
+ const loaders = new Map ( ) ;
4
+ const registry = getSharedResource ( "SVGIcons.registry" , new Map ( ) ) ;
5
+ const iconCollectionPromises = getSharedResource ( "SVGIcons.promises" , new Map ( ) ) ;
6
+
7
+ const ICON_NOT_FOUND = "ICON_NOT_FOUND" ;
8
+ const DEFAULT_COLLECTION = "SAP-icons" ;
9
+
10
+ /**
11
+ * @deprecated
12
+ */
5
13
const registerIconBundle = async ( collectionName , bundleData ) => {
6
- let resolveFn ;
7
- const collectionFetched = new Promise ( resolve => {
8
- resolveFn = resolve ;
9
- } ) ;
10
- registerCollectionPromise ( collectionName , collectionFetched ) ;
14
+ throw new Error ( "This method has been removed. Use `registerIconLoader` instead." ) ;
15
+ } ;
11
16
12
- if ( typeof bundleData !== "object" ) { // not inlined from build -> fetch it
13
- bundleData = await fetchJsonOnce ( getEffectiveAssetPath ( bundleData ) ) ;
17
+ const registerIconLoader = async ( collectionName , loader ) => {
18
+ loaders . set ( collectionName , loader ) ;
19
+ } ;
20
+
21
+ const _loadIconCollectionOnce = async collectionName => {
22
+ if ( ! iconCollectionPromises . has ( collectionName ) ) {
23
+ const loadIcons = loaders . get ( collectionName ) ;
24
+ iconCollectionPromises . set ( collectionName , loadIcons ( collectionName ) ) ;
14
25
}
15
- fillRegistry ( bundleData ) ;
16
- resolveFn ( ) ;
26
+
27
+ return iconCollectionPromises . get ( collectionName ) ;
17
28
} ;
18
29
19
- const fillRegistry = bundleData => {
30
+ const _fillRegistry = bundleData => {
20
31
Object . keys ( bundleData . data ) . forEach ( iconName => {
21
32
const iconData = bundleData . data [ iconName ] ;
22
33
@@ -29,4 +40,73 @@ const fillRegistry = bundleData => {
29
40
} ) ;
30
41
} ;
31
42
32
- export { registerIconBundle } ; // eslint-disable-line
43
+ // set
44
+ const registerIcon = ( name , { pathData, ltr, accData, collection } = { } ) => { // eslint-disable-line
45
+ if ( ! collection ) {
46
+ collection = DEFAULT_COLLECTION ;
47
+ }
48
+
49
+ const key = `${ collection } /${ name } ` ;
50
+ registry . set ( key , { pathData, ltr, accData } ) ;
51
+ } ;
52
+
53
+ const _parseName = name => {
54
+ // silently support ui5-compatible URIs
55
+ if ( name . startsWith ( "sap-icon://" ) ) {
56
+ name = name . replace ( "sap-icon://" , "" ) ;
57
+ }
58
+
59
+ let collection ;
60
+ [ name , collection ] = name . split ( "/" ) . reverse ( ) ;
61
+ collection = collection || DEFAULT_COLLECTION ;
62
+ // hardcoded alias in case icon explorer is used, resolve `SAP-icons-TNT` to `tnt`
63
+ // aliases can be made a feature in the future if more collections need it or more aliases are needed.
64
+ if ( collection === "SAP-icons-TNT" ) {
65
+ collection = "tnt" ;
66
+ }
67
+ const registryKey = `${ collection } /${ name } ` ;
68
+ return { name, collection, registryKey } ;
69
+ } ;
70
+
71
+ const getIconDataSync = nameProp => {
72
+ const { registryKey } = _parseName ( nameProp ) ;
73
+ return registry . get ( registryKey ) ;
74
+ } ;
75
+
76
+ const getIconData = async nameProp => {
77
+ const { collection, registryKey } = _parseName ( nameProp ) ;
78
+
79
+ let iconData = ICON_NOT_FOUND ;
80
+ try {
81
+ iconData = await _loadIconCollectionOnce ( collection ) ;
82
+ } catch ( e ) {
83
+ console . error ( e . message ) ; /* eslint-disable-line */
84
+ }
85
+
86
+ if ( iconData === ICON_NOT_FOUND ) {
87
+ return iconData ;
88
+ }
89
+
90
+ if ( ! registry . has ( registryKey ) ) {
91
+ // not filled by another await. many getters will await on the same loader, but fill only once
92
+ _fillRegistry ( iconData ) ;
93
+ }
94
+ return registry . get ( registryKey ) ;
95
+ } ;
96
+
97
+ // test page usage only
98
+ const _getRegisteredNames = async ( ) => {
99
+ // fetch one icon of each collection to trigger the bundle load
100
+ await getIconData ( "edit" ) ;
101
+ await getIconData ( "tnt/arrow" ) ;
102
+ return Array . from ( registry . keys ( ) ) ;
103
+ } ;
104
+
105
+ export {
106
+ registerIconBundle ,
107
+ registerIconLoader ,
108
+ getIconData ,
109
+ getIconDataSync ,
110
+ registerIcon ,
111
+ _getRegisteredNames ,
112
+ } ;
0 commit comments