2020-04-28 09:47:35 +00:00
|
|
|
// Core
|
2021-09-24 08:41:54 +00:00
|
|
|
import type { IconifyJSON, IconifyIcon } from '@iconify/types';
|
|
|
|
import type { IconifyIconName } from '@iconify/utils/lib/icon/name';
|
2022-06-19 15:22:42 +00:00
|
|
|
import type { IconifyIconSize } from '@iconify/utils/lib/customisations/defaults';
|
2021-09-24 08:41:54 +00:00
|
|
|
import type { IconifyIconBuildResult } from '@iconify/utils/lib/svg/build';
|
|
|
|
import type { IconifyStorageFunctions } from '@iconify/core/lib/storage/functions';
|
2021-09-24 16:03:49 +00:00
|
|
|
import {
|
|
|
|
iconExists,
|
|
|
|
getIcon,
|
|
|
|
addIcon,
|
|
|
|
addCollection,
|
|
|
|
} from '@iconify/core/lib/storage/functions';
|
2022-05-21 19:55:01 +00:00
|
|
|
import { listIcons } from '@iconify/core/lib/storage/storage';
|
2021-09-24 08:41:54 +00:00
|
|
|
import type { IconifyBuilderFunctions } from '@iconify/core/lib/builder/functions';
|
2022-07-01 19:12:18 +00:00
|
|
|
import { iconToSVG as buildIcon } from '@iconify/utils/lib/svg/build';
|
2021-09-24 16:03:49 +00:00
|
|
|
import { replaceIDs } from '@iconify/utils/lib/svg/id';
|
|
|
|
import { calculateSize } from '@iconify/utils/lib/svg/size';
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Cache
|
2022-06-28 19:37:23 +00:00
|
|
|
import { initBrowserStorage } from '@iconify/core/lib/browser-storage';
|
2021-09-24 08:41:54 +00:00
|
|
|
import type {
|
2020-12-22 12:49:02 +00:00
|
|
|
IconifyBrowserCacheFunctions,
|
|
|
|
IconifyBrowserCacheType,
|
|
|
|
} from '@iconify/core/lib/browser-storage/functions';
|
2021-09-24 08:41:54 +00:00
|
|
|
import { toggleBrowserCache } from '@iconify/core/lib/browser-storage/functions';
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// API
|
2021-09-24 08:41:54 +00:00
|
|
|
import type {
|
2020-12-22 12:49:02 +00:00
|
|
|
IconifyAPIFunctions,
|
|
|
|
IconifyAPIInternalFunctions,
|
2021-09-01 08:34:58 +00:00
|
|
|
IconifyAPIQueryParams,
|
|
|
|
IconifyAPICustomQueryParams,
|
2020-12-22 12:49:02 +00:00
|
|
|
} from '@iconify/core/lib/api/functions';
|
2021-09-24 08:41:54 +00:00
|
|
|
import type {
|
2020-06-10 19:15:40 +00:00
|
|
|
IconifyAPIModule,
|
|
|
|
IconifyAPISendQuery,
|
2021-09-01 08:34:58 +00:00
|
|
|
IconifyAPIPrepareIconsQuery,
|
2020-06-10 19:15:40 +00:00
|
|
|
} from '@iconify/core/lib/api/modules';
|
2021-09-24 08:41:54 +00:00
|
|
|
import { setAPIModule } from '@iconify/core/lib/api/modules';
|
|
|
|
import type {
|
2020-05-31 06:44:24 +00:00
|
|
|
PartialIconifyAPIConfig,
|
|
|
|
IconifyAPIConfig,
|
2020-06-10 19:15:40 +00:00
|
|
|
GetAPIConfig,
|
2020-05-31 06:44:24 +00:00
|
|
|
} from '@iconify/core/lib/api/config';
|
2021-09-24 16:03:49 +00:00
|
|
|
import {
|
|
|
|
addAPIProvider,
|
|
|
|
getAPIConfig,
|
|
|
|
listAPIProviders,
|
|
|
|
} from '@iconify/core/lib/api/config';
|
2021-01-12 12:04:19 +00:00
|
|
|
import {
|
2021-09-05 16:09:25 +00:00
|
|
|
fetchAPIModule,
|
|
|
|
getFetch,
|
2022-02-21 15:13:27 +00:00
|
|
|
setFetch,
|
2021-01-12 12:04:19 +00:00
|
|
|
} from '@iconify/core/lib/api/modules/fetch';
|
2021-09-24 08:41:54 +00:00
|
|
|
import type {
|
2020-04-28 14:35:35 +00:00
|
|
|
IconifyIconLoaderCallback,
|
|
|
|
IconifyIconLoaderAbort,
|
2021-09-05 16:09:25 +00:00
|
|
|
} from '@iconify/core/lib/api/icons';
|
2022-01-13 20:19:49 +00:00
|
|
|
import { loadIcons, loadIcon } from '@iconify/core/lib/api/icons';
|
2021-09-24 16:03:49 +00:00
|
|
|
import { sendAPIQuery } from '@iconify/core/lib/api/query';
|
2020-04-28 09:47:35 +00:00
|
|
|
|
2020-07-01 16:01:07 +00:00
|
|
|
// Other
|
2021-09-24 08:41:54 +00:00
|
|
|
import type { IconifyCommonFunctions } from './common';
|
2021-09-24 16:03:49 +00:00
|
|
|
import { getVersion, renderSVG, renderHTML, renderIcon, scan } from './common';
|
|
|
|
import {
|
|
|
|
observe,
|
|
|
|
stopObserving,
|
|
|
|
pauseObserver,
|
|
|
|
resumeObserver,
|
2022-04-07 12:52:57 +00:00
|
|
|
} from './observer/index';
|
2022-06-19 15:22:42 +00:00
|
|
|
import type {
|
|
|
|
IconifyRenderMode,
|
2022-06-19 16:16:11 +00:00
|
|
|
IconifyIconCustomisations,
|
2022-06-19 15:22:42 +00:00
|
|
|
} from './scanner/config';
|
2020-07-01 16:01:07 +00:00
|
|
|
|
2020-04-28 09:47:35 +00:00
|
|
|
/**
|
|
|
|
* Export required types
|
|
|
|
*/
|
2020-12-22 12:49:02 +00:00
|
|
|
// Function sets
|
|
|
|
export {
|
|
|
|
IconifyStorageFunctions,
|
|
|
|
IconifyBuilderFunctions,
|
|
|
|
IconifyBrowserCacheFunctions,
|
|
|
|
IconifyAPIFunctions,
|
|
|
|
IconifyAPIInternalFunctions,
|
|
|
|
};
|
|
|
|
|
2020-04-28 09:47:35 +00:00
|
|
|
// JSON stuff
|
2020-06-10 19:15:40 +00:00
|
|
|
export { IconifyIcon, IconifyJSON, IconifyIconName };
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Customisations
|
2022-04-30 20:12:34 +00:00
|
|
|
export { IconifyIconCustomisations, IconifyIconSize, IconifyRenderMode };
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Build
|
|
|
|
export { IconifyIconBuildResult };
|
|
|
|
|
|
|
|
// API
|
2020-06-03 19:01:45 +00:00
|
|
|
export {
|
|
|
|
IconifyAPIConfig,
|
|
|
|
IconifyIconLoaderCallback,
|
|
|
|
IconifyIconLoaderAbort,
|
2020-06-10 19:15:40 +00:00
|
|
|
IconifyAPIModule,
|
|
|
|
GetAPIConfig,
|
2021-09-01 08:34:58 +00:00
|
|
|
IconifyAPIPrepareIconsQuery,
|
2020-06-10 19:15:40 +00:00
|
|
|
IconifyAPISendQuery,
|
2021-05-01 20:40:22 +00:00
|
|
|
PartialIconifyAPIConfig,
|
2021-09-01 08:34:58 +00:00
|
|
|
IconifyAPIQueryParams,
|
|
|
|
IconifyAPICustomQueryParams,
|
2020-06-03 19:01:45 +00:00
|
|
|
};
|
2020-04-28 09:47:35 +00:00
|
|
|
|
2021-09-01 08:34:58 +00:00
|
|
|
// Cache
|
|
|
|
export { IconifyBrowserCacheType };
|
|
|
|
|
2020-05-31 17:03:13 +00:00
|
|
|
/**
|
2020-07-01 20:29:43 +00:00
|
|
|
* Iconify interface
|
2020-05-31 17:03:13 +00:00
|
|
|
*/
|
2020-12-21 15:15:45 +00:00
|
|
|
export interface IconifyGlobal
|
|
|
|
extends IconifyStorageFunctions,
|
2020-12-22 12:49:02 +00:00
|
|
|
IconifyBuilderFunctions,
|
2020-12-21 15:15:45 +00:00
|
|
|
IconifyCommonFunctions,
|
2020-12-22 12:49:02 +00:00
|
|
|
IconifyBrowserCacheFunctions,
|
|
|
|
IconifyAPIFunctions {
|
|
|
|
_api: IconifyAPIInternalFunctions;
|
2020-08-12 18:21:20 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 09:47:35 +00:00
|
|
|
/**
|
2021-09-24 16:03:49 +00:00
|
|
|
* Enable cache
|
2020-04-28 09:47:35 +00:00
|
|
|
*/
|
2021-09-24 16:03:49 +00:00
|
|
|
function enableCache(storage: IconifyBrowserCacheType, enable?: boolean): void {
|
|
|
|
toggleBrowserCache(storage, enable !== false);
|
|
|
|
}
|
2020-06-10 19:15:40 +00:00
|
|
|
|
2020-12-22 12:49:02 +00:00
|
|
|
/**
|
2021-09-24 16:03:49 +00:00
|
|
|
* Disable cache
|
2020-12-22 12:49:02 +00:00
|
|
|
*/
|
2021-09-24 16:03:49 +00:00
|
|
|
function disableCache(storage: IconifyBrowserCacheType): void {
|
|
|
|
toggleBrowserCache(storage, true);
|
|
|
|
}
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise stuff
|
|
|
|
*/
|
2021-09-05 16:09:25 +00:00
|
|
|
// Set API module
|
2022-02-21 15:13:27 +00:00
|
|
|
setAPIModule('', fetchAPIModule);
|
2021-04-27 21:06:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Browser stuff
|
|
|
|
*/
|
2020-06-03 19:01:45 +00:00
|
|
|
if (typeof document !== 'undefined' && typeof window !== 'undefined') {
|
|
|
|
// Set cache and load existing cache
|
2022-06-28 19:37:23 +00:00
|
|
|
initBrowserStorage();
|
2020-06-03 19:01:45 +00:00
|
|
|
|
2021-05-13 19:33:33 +00:00
|
|
|
interface WindowWithIconifyStuff {
|
|
|
|
IconifyProviders?: Record<string, PartialIconifyAPIConfig>;
|
|
|
|
}
|
|
|
|
const _window = window as WindowWithIconifyStuff;
|
2020-06-03 19:01:45 +00:00
|
|
|
|
|
|
|
// Set API from global "IconifyProviders"
|
2021-05-13 19:33:33 +00:00
|
|
|
if (_window.IconifyProviders !== void 0) {
|
|
|
|
const providers = _window.IconifyProviders;
|
2020-06-03 19:01:45 +00:00
|
|
|
if (typeof providers === 'object' && providers !== null) {
|
2021-09-24 08:41:54 +00:00
|
|
|
for (const key in providers) {
|
2020-06-03 19:01:45 +00:00
|
|
|
const err = 'IconifyProviders[' + key + '] is invalid.';
|
|
|
|
try {
|
|
|
|
const value = providers[key];
|
|
|
|
if (
|
|
|
|
typeof value !== 'object' ||
|
|
|
|
!value ||
|
|
|
|
value.resources === void 0
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-09-24 16:03:49 +00:00
|
|
|
if (!addAPIProvider(key, value)) {
|
2020-06-03 19:01:45 +00:00
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2020-05-31 06:44:24 +00:00
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 19:01:45 +00:00
|
|
|
}
|
2020-04-28 09:47:35 +00:00
|
|
|
|
2021-09-24 16:03:49 +00:00
|
|
|
/**
|
|
|
|
* Internal API
|
|
|
|
*/
|
|
|
|
const _api: IconifyAPIInternalFunctions = {
|
|
|
|
getAPIConfig,
|
|
|
|
setAPIModule,
|
|
|
|
sendAPIQuery,
|
|
|
|
setFetch,
|
2022-02-21 15:13:27 +00:00
|
|
|
getFetch,
|
2021-09-24 16:03:49 +00:00
|
|
|
listAPIProviders,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Global variable
|
|
|
|
*/
|
|
|
|
const Iconify: IconifyGlobal = {
|
|
|
|
// IconifyAPIInternalFunctions
|
|
|
|
_api,
|
|
|
|
|
|
|
|
// IconifyAPIFunctions
|
|
|
|
addAPIProvider,
|
|
|
|
loadIcons,
|
2022-01-13 20:19:49 +00:00
|
|
|
loadIcon,
|
2021-09-24 16:03:49 +00:00
|
|
|
|
|
|
|
// IconifyStorageFunctions
|
|
|
|
iconExists,
|
|
|
|
getIcon,
|
|
|
|
listIcons,
|
|
|
|
addIcon,
|
|
|
|
addCollection,
|
|
|
|
|
|
|
|
// IconifyBuilderFunctions
|
|
|
|
replaceIDs,
|
|
|
|
calculateSize,
|
|
|
|
buildIcon,
|
|
|
|
|
|
|
|
// IconifyCommonFunctions
|
|
|
|
getVersion,
|
|
|
|
renderSVG,
|
|
|
|
renderHTML,
|
|
|
|
renderIcon,
|
|
|
|
scan,
|
|
|
|
observe,
|
|
|
|
stopObserving,
|
|
|
|
pauseObserver,
|
|
|
|
resumeObserver,
|
|
|
|
|
|
|
|
// IconifyBrowserCacheFunctions
|
|
|
|
enableCache,
|
|
|
|
disableCache,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default export
|
|
|
|
*/
|
2020-04-28 09:47:35 +00:00
|
|
|
export default Iconify;
|
2021-09-24 16:03:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Named exports
|
|
|
|
*/
|
|
|
|
// IconifyAPIInternalFunctions
|
|
|
|
export { _api };
|
|
|
|
|
|
|
|
// IconifyAPIFunctions
|
2022-01-24 17:44:10 +00:00
|
|
|
export { addAPIProvider, loadIcons, loadIcon };
|
2021-09-24 16:03:49 +00:00
|
|
|
|
|
|
|
// IconifyStorageFunctions
|
2022-05-21 19:55:01 +00:00
|
|
|
export { iconExists, getIcon, listIcons, addIcon, addCollection };
|
2021-09-24 16:03:49 +00:00
|
|
|
|
|
|
|
// IconifyBuilderFunctions
|
|
|
|
export { replaceIDs, calculateSize, buildIcon };
|
|
|
|
|
|
|
|
// IconifyCommonFunctions
|
|
|
|
export {
|
|
|
|
getVersion,
|
|
|
|
renderSVG,
|
|
|
|
renderHTML,
|
|
|
|
renderIcon,
|
|
|
|
scan,
|
|
|
|
observe,
|
|
|
|
stopObserving,
|
|
|
|
pauseObserver,
|
|
|
|
resumeObserver,
|
|
|
|
};
|
|
|
|
|
|
|
|
// IconifyBrowserCacheFunctions
|
|
|
|
export { enableCache, disableCache };
|