{"version":3,"file":"icon-registry.context-BQuTjP6G.js","sources":["../../../src/packages/core/icon-registry/icon-registry.context-token.ts","../../../src/packages/core/icon-registry/icon.registry.ts","../../../src/packages/core/icon-registry/icon-registry.context.ts"],"sourcesContent":["import type { UmbIconRegistryContext } from './icon-registry.context.js';\nimport { UmbContextToken } from '@umbraco-cms/backoffice/context-api';\n\nexport const UMB_ICON_REGISTRY_CONTEXT = new UmbContextToken('UmbIconRegistryContext');\n","import type { UmbIconDefinition, UmbIconModule } from './types.js';\nimport { loadManifestPlainJs } from '@umbraco-cms/backoffice/extension-api';\nimport { type UUIIconHost, UUIIconRegistry } from '@umbraco-cms/backoffice/external/uui';\n\n/**\n * @class UmbIconRegistry\n * @augments {UUIIconRegistry}\n * @description - Icon Registry. Provides icons from the icon manifest. Icons are loaded on demand. All icons are prefixed with 'icon-'\n */\nexport class UmbIconRegistry extends UUIIconRegistry {\n\t#initResolve?: () => void;\n\t#init: Promise = new Promise((resolve) => {\n\t\tthis.#initResolve = resolve;\n\t});\n\n\t#icons: UmbIconDefinition[] = [];\n\t#unhandledProviders: Map = new Map();\n\n\tsetIcons(icons: UmbIconDefinition[]) {\n\t\tconst oldIcons = this.#icons;\n\t\tthis.#icons = icons;\n\t\tif (this.#initResolve) {\n\t\t\tthis.#initResolve();\n\t\t\tthis.#initResolve = undefined;\n\t\t}\n\t\t// Go figure out which of the icons are new.\n\t\tconst newIcons = this.#icons.filter((i) => !oldIcons.find((o) => o.name === i.name));\n\t\tnewIcons.forEach((icon) => {\n\t\t\t// Do we already have a request for this one, then lets initiate the load for those:\n\t\t\tconst unhandled = this.#unhandledProviders.get(icon.name);\n\t\t\tif (unhandled) {\n\t\t\t\tthis.#loadIcon(icon.name, unhandled).then(() => {\n\t\t\t\t\tthis.#unhandledProviders.delete(icon.name);\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\t}\n\tappendIcons(icons: UmbIconDefinition[]) {\n\t\tthis.#icons = [...this.#icons, ...icons];\n\t}\n\t/**\n\t * @param {string} iconName\n\t * @returns {*} {boolean}\n\t * @memberof UmbIconStore\n\t */\n\toverride acceptIcon(iconName: string): boolean {\n\t\tconst iconProvider = this.provideIcon(iconName);\n\t\tthis.#loadIcon(iconName, iconProvider);\n\n\t\treturn true;\n\t}\n\n\tasync #loadIcon(iconName: string, iconProvider: UUIIconHost): Promise {\n\t\tawait this.#init;\n\t\tconst iconManifest = this.#icons.find((i: UmbIconDefinition) => i.name === iconName);\n\t\t// Icon not found, so lets add it to a list of unhandled requests.\n\t\tif (!iconManifest) {\n\t\t\tthis.#unhandledProviders.set(iconName, iconProvider);\n\t\t\treturn false;\n\t\t}\n\n\t\ttry {\n\t\t\tconst iconModule = await loadManifestPlainJs(iconManifest.path);\n\t\t\tif (!iconModule) throw new Error(`Failed to load icon ${iconName}`);\n\t\t\tif (!iconModule.default) throw new Error(`Icon ${iconName} is missing a default export`);\n\t\t\ticonProvider.svg = iconModule.default;\n\t\t} catch (error: any) {\n\t\t\tconsole.error(`Failed to load icon ${iconName}`, error.message);\n\t\t}\n\n\t\treturn true;\n\t}\n}\n","import { UmbIconRegistry } from './icon.registry.js';\nimport type { UmbIconDefinition } from './types.js';\nimport { UMB_ICON_REGISTRY_CONTEXT } from './icon-registry.context-token.js';\nimport { UmbContextBase } from '@umbraco-cms/backoffice/class-api';\nimport type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';\nimport { loadManifestPlainJs } from '@umbraco-cms/backoffice/extension-api';\nimport { UmbArrayState } from '@umbraco-cms/backoffice/observable-api';\nimport { type ManifestIcons, umbExtensionsRegistry } from '@umbraco-cms/backoffice/extension-registry';\n\nexport class UmbIconRegistryContext extends UmbContextBase {\n\t#registry: UmbIconRegistry;\n\t#manifestMap = new Map();\n\t#icons = new UmbArrayState([], (x) => x.name);\n\treadonly icons = this.#icons.asObservable();\n\treadonly approvedIcons = this.#icons.asObservablePart((icons) => icons.filter((x) => x.legacy !== true));\n\n\tconstructor(host: UmbControllerHost) {\n\t\tsuper(host, UMB_ICON_REGISTRY_CONTEXT);\n\t\tthis.#registry = new UmbIconRegistry();\n\t\tthis.#registry.attach(host.getHostElement());\n\n\t\tthis.observe(this.icons, (icons) => {\n\t\t\t//if (icons.length > 0) {\n\t\t\tthis.#registry.setIcons(icons);\n\t\t\t//}\n\t\t});\n\n\t\tthis.observe(umbExtensionsRegistry.byType('icons'), (manifests) => {\n\t\t\tmanifests.forEach((manifest) => {\n\t\t\t\tif (this.#manifestMap.has(manifest.alias)) return;\n\t\t\t\tthis.#manifestMap.set(manifest.alias, manifest);\n\t\t\t\t// TODO: Should we unInit a entry point if is removed?\n\t\t\t\tthis.instantiateIcons(manifest);\n\t\t\t});\n\t\t});\n\t}\n\n\tasync instantiateIcons(manifest: ManifestIcons) {\n\t\tif (manifest.js) {\n\t\t\tconst js = await loadManifestPlainJs<{ default?: any }>(manifest.js);\n\t\t\tif (!js || !js.default || !Array.isArray(js.default)) {\n\t\t\t\tthrow new Error('Icon manifest JS-file must export an array of icons as the default export.');\n\t\t\t}\n\t\t\tthis.#icons.append(js.default);\n\t\t}\n\t}\n}\n\nexport { UmbIconRegistryContext as api };\n"],"names":["UMB_ICON_REGISTRY_CONTEXT","UmbContextToken","UmbIconRegistry","UUIIconRegistry","#initResolve","#init","resolve","#icons","#unhandledProviders","icons","oldIcons","i","icon","unhandled","#loadIcon","iconName","iconProvider","iconManifest","iconModule","loadManifestPlainJs","error","UmbIconRegistryContext","UmbContextBase","host","#manifestMap","UmbArrayState","x","#registry","umbExtensionsRegistry","manifests","manifest","js"],"mappings":";;;;;;AAGa,MAAAA,IAA4B,IAAIC,EAAwC,wBAAwB;ACMtG,MAAMC,UAAwBC,EAAgB;AAAA,EACpDC;AAAA,EACAC,KAAuB,IAAI,QAAQ,CAACC,MAAY;AAC/C,SAAKF,KAAeE;AAAA,EAAA,CACpB;AAAA,EAEDC,KAA8B,CAAA;AAAA,EAC9BC,yBAAoD;EAEpD,SAASC,GAA4B;AACpC,UAAMC,IAAW,KAAK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