mirror of
https://github.com/iconify/iconify.git
synced 2025-01-26 00:28:28 +00:00
Split common storage functions into separate file with reusable interface
This commit is contained in:
parent
79b4fa111a
commit
5f285fcf14
@ -8,7 +8,11 @@ import {
|
||||
} from '@iconify/iconify/lib/modules/finder';
|
||||
import { finder as iconifyFinder } from '@iconify/iconify/lib/finders/iconify-v1';
|
||||
import { finder as iconifyIconFinder } from '@iconify/iconify/lib/finders/iconify-v1-icon';
|
||||
import { getStorage, addIconSet, getIcon } from '@iconify/core/lib/storage';
|
||||
import {
|
||||
getStorage,
|
||||
addIconSet,
|
||||
getIcon,
|
||||
} from '@iconify/core/lib/storage/storage';
|
||||
import { renderIcon } from '@iconify/iconify/lib/modules/render';
|
||||
import { stringToIcon } from '@iconify/core/lib/icon/name';
|
||||
import { IconifyElement } from '@iconify/iconify/lib/modules/element';
|
||||
|
@ -8,7 +8,11 @@ import {
|
||||
} from '@iconify/iconify/lib/modules/finder';
|
||||
import { finder as iconifyFinder } from '@iconify/iconify/lib/finders/iconify';
|
||||
import { finder as iconifyIconFinder } from '@iconify/iconify/lib/finders/iconify-icon';
|
||||
import { getStorage, addIconSet, getIcon } from '@iconify/core/lib/storage';
|
||||
import {
|
||||
getStorage,
|
||||
addIconSet,
|
||||
getIcon,
|
||||
} from '@iconify/core/lib/storage/storage';
|
||||
import { renderIcon } from '@iconify/iconify/lib/modules/render';
|
||||
import { stringToIcon } from '@iconify/core/lib/icon/name';
|
||||
import { IconifyElement } from '@iconify/iconify/lib/modules/element';
|
||||
|
@ -5,7 +5,7 @@ import { getNode, setRoot } from './node';
|
||||
import { addFinder } from '@iconify/iconify/lib/modules/finder';
|
||||
import { finder as iconifyFinder } from '@iconify/iconify/lib/finders/iconify';
|
||||
import { finder as iconifyIconFinder } from '@iconify/iconify/lib/finders/iconify-icon';
|
||||
import { getStorage, addIconSet } from '@iconify/core/lib/storage';
|
||||
import { getStorage, addIconSet } from '@iconify/core/lib/storage/storage';
|
||||
import { listRootNodes } from '@iconify/iconify/lib/modules/root';
|
||||
import { scanDOM, scanElement } from '@iconify/iconify/lib/modules/scanner';
|
||||
import { removeObservedNode } from '@iconify/iconify/lib/modules/observer';
|
||||
|
@ -5,7 +5,7 @@ import { getNode, setRoot } from './node';
|
||||
import { addFinder } from '@iconify/iconify/lib/modules/finder';
|
||||
import { finder as iconifyFinder } from '@iconify/iconify/lib/finders/iconify';
|
||||
import { finder as iconifyIconFinder } from '@iconify/iconify/lib/finders/iconify-icon';
|
||||
import { getStorage, addIconSet } from '@iconify/core/lib/storage';
|
||||
import { getStorage, addIconSet } from '@iconify/core/lib/storage/storage';
|
||||
import { listRootNodes } from '@iconify/iconify/lib/modules/root';
|
||||
import { scanDOM } from '@iconify/iconify/lib/modules/scanner';
|
||||
import {
|
||||
|
@ -2,7 +2,7 @@ import {
|
||||
IconifyIconLoaderCallback,
|
||||
IconifyIconLoaderAbort,
|
||||
} from '../interfaces/loader';
|
||||
import { getStorage } from '../storage';
|
||||
import { getStorage } from '../storage/storage';
|
||||
import { SortedIcons } from '../icon/sort';
|
||||
import { IconifyIconSource } from '../icon/name';
|
||||
|
||||
|
@ -13,7 +13,7 @@ import { IsPending, IconifyAPI } from '../interfaces/api';
|
||||
import { storeCallback, updateCallbacks } from './callbacks';
|
||||
import { getAPIModule } from './modules';
|
||||
import { getAPIConfig, IconifyAPIConfig } from './config';
|
||||
import { getStorage, addIconSet } from '../storage';
|
||||
import { getStorage, addIconSet } from '../storage/storage';
|
||||
import { coreModules } from '../modules';
|
||||
import { IconifyIconName, IconifyIconSource } from '../icon/name';
|
||||
import { listToIcons } from '../icon/list';
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { getStorage, IconStorage } from '../storage';
|
||||
import { getStorage, IconStorage } from '../storage/storage';
|
||||
import { IconifyIconName } from './name';
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { IconifyJSON } from '@iconify/types';
|
||||
import { CacheIcons, LoadIconsCache } from '../interfaces/cache';
|
||||
import { getStorage, addIconSet } from '../storage';
|
||||
import { getStorage, addIconSet } from './storage';
|
||||
|
||||
interface StorageType<T> {
|
||||
local: T;
|
115
packages/core/src/storage/functions.ts
Normal file
115
packages/core/src/storage/functions.ts
Normal file
@ -0,0 +1,115 @@
|
||||
import { IconifyJSON } from '@iconify/types';
|
||||
import { FullIconifyIcon, IconifyIcon } from '../icon';
|
||||
import { IconifyIconName, stringToIcon, validateIcon } from '../icon/name';
|
||||
import { merge } from '../misc/merge';
|
||||
import { getStorage, getIcon, listIcons, addIcon, addIconSet } from './storage';
|
||||
|
||||
/**
|
||||
* Interface for exported storage functions
|
||||
*/
|
||||
export interface IconifyStorageFunctions {
|
||||
/**
|
||||
* Check if icon exists
|
||||
*/
|
||||
iconExists: (name: string) => boolean;
|
||||
|
||||
/**
|
||||
* Get icon data with all properties
|
||||
*/
|
||||
getIcon: (name: string) => Required<IconifyIcon> | null;
|
||||
|
||||
/**
|
||||
* List all available icons
|
||||
*/
|
||||
listIcons: (provider?: string, prefix?: string) => string[];
|
||||
|
||||
/* Add icons */
|
||||
/**
|
||||
* Add icon to storage
|
||||
*/
|
||||
addIcon: (name: string, data: IconifyIcon) => boolean;
|
||||
|
||||
/**
|
||||
* Add icon set to storage
|
||||
*/
|
||||
addCollection: (data: IconifyJSON, provider?: string) => boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get icon name
|
||||
*/
|
||||
export function getIconName(name: string): IconifyIconName | null {
|
||||
const icon = stringToIcon(name);
|
||||
if (!validateIcon(icon)) {
|
||||
return null;
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get icon data
|
||||
*/
|
||||
export function getIconData(
|
||||
name: string | IconifyIconName
|
||||
): FullIconifyIcon | null {
|
||||
const icon = typeof name === 'string' ? getIconName(name) : name;
|
||||
return icon
|
||||
? getIcon(getStorage(icon.provider, icon.prefix), icon.name)
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add icon set
|
||||
*/
|
||||
export function addCollection(data: IconifyJSON, provider?: string): boolean {
|
||||
if (typeof provider !== 'string') {
|
||||
provider = typeof data.provider === 'string' ? data.provider : '';
|
||||
}
|
||||
|
||||
if (
|
||||
typeof data !== 'object' ||
|
||||
// Prefix must be present
|
||||
typeof data.prefix !== 'string' ||
|
||||
// Validate provider and prefix
|
||||
!validateIcon({
|
||||
provider,
|
||||
prefix: data.prefix,
|
||||
name: 'a',
|
||||
})
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const storage = getStorage(provider, data.prefix);
|
||||
return !!addIconSet(storage, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export
|
||||
*/
|
||||
export const storageFunctions: IconifyStorageFunctions = {
|
||||
// Check if icon exists
|
||||
iconExists: (name) => getIconData(name) !== null,
|
||||
|
||||
// Get raw icon data
|
||||
getIcon: (name) => {
|
||||
const result = getIconData(name);
|
||||
return result ? merge(result) : null;
|
||||
},
|
||||
|
||||
// List icons
|
||||
listIcons,
|
||||
|
||||
// Add icon
|
||||
addIcon: (name, data) => {
|
||||
const icon = getIconName(name);
|
||||
if (!icon) {
|
||||
return false;
|
||||
}
|
||||
const storage = getStorage(icon.provider, icon.prefix);
|
||||
return addIcon(storage, icon.name, data);
|
||||
},
|
||||
|
||||
// Add icon set
|
||||
addCollection,
|
||||
};
|
@ -9,7 +9,7 @@ import {
|
||||
addIconSet,
|
||||
getStorage,
|
||||
listIcons,
|
||||
} from '../../lib/storage';
|
||||
} from '../../lib/storage/storage';
|
||||
import { FullIconifyIcon, IconifyIcon } from '../../lib/icon';
|
||||
|
||||
describe('Testing storage', () => {
|
||||
|
@ -8,7 +8,7 @@ import {
|
||||
storeCallback,
|
||||
} from '../../lib/api/callbacks';
|
||||
import { sortIcons } from '../../lib/icon/sort';
|
||||
import { getStorage, addIconSet } from '../../lib/storage';
|
||||
import { getStorage, addIconSet } from '../../lib/storage/storage';
|
||||
|
||||
describe('Testing API callbacks', () => {
|
||||
let prefixCounter = 0;
|
||||
|
@ -2,7 +2,7 @@
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { RedundancyPendingItem } from '@cyberalien/redundancy';
|
||||
import { PendingQueryItem } from '@cyberalien/redundancy';
|
||||
import {
|
||||
setAPIConfig,
|
||||
getAPIConfig,
|
||||
@ -40,7 +40,7 @@ describe('Testing API modules', () => {
|
||||
const sendQuery = (
|
||||
host: string,
|
||||
params: APIQueryParams,
|
||||
status: RedundancyPendingItem
|
||||
item: PendingQueryItem
|
||||
): void => {
|
||||
throw new Error('Unexpected API call');
|
||||
};
|
||||
|
@ -2,7 +2,7 @@
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { RedundancyPendingItem } from '@cyberalien/redundancy';
|
||||
import { PendingQueryItem } from '@cyberalien/redundancy';
|
||||
import { setAPIConfig } from '../../lib/api/config';
|
||||
import { setAPIModule, APIQueryParams } from '../../lib/api/modules';
|
||||
import { API } from '../../lib/api/';
|
||||
@ -56,7 +56,7 @@ describe('Testing API loadIcons', () => {
|
||||
const sendQuery = (
|
||||
host: string,
|
||||
params: APIQueryParams,
|
||||
status: RedundancyPendingItem
|
||||
item: PendingQueryItem
|
||||
): void => {
|
||||
// This callback should be called after prepareQuery
|
||||
expect(asyncCounter).to.be.equal(2);
|
||||
@ -72,7 +72,7 @@ describe('Testing API loadIcons', () => {
|
||||
expect(params).to.be.eql(expected);
|
||||
|
||||
// Send data
|
||||
status.done({
|
||||
item.done({
|
||||
prefix,
|
||||
icons: {
|
||||
icon1: {
|
||||
@ -188,7 +188,7 @@ describe('Testing API loadIcons', () => {
|
||||
const sendQuery = (
|
||||
host: string,
|
||||
params: APIQueryParams,
|
||||
status: RedundancyPendingItem
|
||||
item: PendingQueryItem
|
||||
): void => {
|
||||
// Test input
|
||||
expect(host).to.be.equal('https://api1.local');
|
||||
@ -209,7 +209,7 @@ describe('Testing API loadIcons', () => {
|
||||
body: '<path d="" />',
|
||||
};
|
||||
});
|
||||
status.done({
|
||||
item.done({
|
||||
prefix,
|
||||
icons,
|
||||
});
|
||||
@ -280,7 +280,7 @@ describe('Testing API loadIcons', () => {
|
||||
const sendQuery = (
|
||||
host: string,
|
||||
params: APIQueryParams,
|
||||
status: RedundancyPendingItem
|
||||
item: PendingQueryItem
|
||||
): void => {
|
||||
queryCounter++;
|
||||
switch (queryCounter) {
|
||||
@ -296,7 +296,7 @@ describe('Testing API loadIcons', () => {
|
||||
expect(host).to.be.equal('https://api2.local');
|
||||
|
||||
// Return result
|
||||
status.done({
|
||||
item.done({
|
||||
prefix,
|
||||
icons: {
|
||||
icon1: {
|
||||
@ -382,7 +382,7 @@ describe('Testing API loadIcons', () => {
|
||||
const sendQuery = (
|
||||
host: string,
|
||||
params: APIQueryParams,
|
||||
status: RedundancyPendingItem
|
||||
item: PendingQueryItem
|
||||
): void => {
|
||||
queryCounter++;
|
||||
switch (queryCounter) {
|
||||
@ -400,7 +400,7 @@ describe('Testing API loadIcons', () => {
|
||||
expect(host).to.be.equal('https://api2.local');
|
||||
|
||||
// Return result
|
||||
status.done({
|
||||
item.done({
|
||||
prefix,
|
||||
icons: {
|
||||
icon1: {
|
||||
@ -419,7 +419,7 @@ describe('Testing API loadIcons', () => {
|
||||
expect(host).to.be.equal('https://api2.local');
|
||||
|
||||
// Return result
|
||||
status.done({
|
||||
item.done({
|
||||
prefix,
|
||||
icons: {
|
||||
icon3: {
|
||||
@ -538,7 +538,7 @@ describe('Testing API loadIcons', () => {
|
||||
const sendQuery = (
|
||||
host: string,
|
||||
params: APIQueryParams,
|
||||
status: RedundancyPendingItem
|
||||
item: PendingQueryItem
|
||||
): void => {
|
||||
queryCounter++;
|
||||
switch (queryCounter) {
|
||||
@ -558,7 +558,7 @@ describe('Testing API loadIcons', () => {
|
||||
expect(host).to.be.equal('https://api2.local');
|
||||
|
||||
// Return result
|
||||
status.done({
|
||||
item.done({
|
||||
prefix: params.prefix,
|
||||
icons: {
|
||||
icon1: {
|
||||
@ -578,7 +578,7 @@ describe('Testing API loadIcons', () => {
|
||||
expect(host).to.be.equal('https://api2.local');
|
||||
|
||||
// Return result
|
||||
status.done({
|
||||
item.done({
|
||||
prefix: params.prefix,
|
||||
icons: {
|
||||
icon2: {
|
||||
|
@ -1,8 +1,6 @@
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars-experimental */
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { count, config, loadCache } from '../../lib/cache/storage';
|
||||
import { count, config, loadCache } from '../../lib/storage/browser';
|
||||
import {
|
||||
nextPrefix,
|
||||
createCache,
|
||||
|
@ -1,5 +1,3 @@
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars-experimental */
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import {
|
||||
@ -8,8 +6,8 @@ import {
|
||||
config,
|
||||
emptyList,
|
||||
StoredItem,
|
||||
} from '../../lib/cache/storage';
|
||||
import { getStorage, iconExists, getIcon } from '../../lib/storage';
|
||||
} from '../../lib/storage/browser';
|
||||
import { getStorage, iconExists } from '../../lib/storage/storage';
|
||||
import {
|
||||
nextPrefix,
|
||||
createCache,
|
||||
@ -21,7 +19,7 @@ import {
|
||||
hour,
|
||||
cacheExpiration,
|
||||
} from './fake_cache';
|
||||
import { IconifyIcon, IconifyJSON } from '@iconify/types';
|
||||
import { IconifyJSON } from '@iconify/types';
|
||||
|
||||
describe('Testing loading from localStorage', () => {
|
||||
const provider = '';
|
||||
|
@ -1,5 +1,3 @@
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars-experimental */
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import {
|
||||
@ -9,8 +7,8 @@ import {
|
||||
config,
|
||||
emptyList,
|
||||
StoredItem,
|
||||
} from '../../lib/cache/storage';
|
||||
import { getStorage, iconExists } from '../../lib/storage';
|
||||
} from '../../lib/storage/browser';
|
||||
import { getStorage, iconExists } from '../../lib/storage/storage';
|
||||
import {
|
||||
nextPrefix,
|
||||
createCache,
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { mock, count, config, emptyList } from '../../lib/cache/storage';
|
||||
import { mock, count, config, emptyList } from '../../lib/storage/browser';
|
||||
|
||||
/**
|
||||
* Get next icon set prefix for testing
|
||||
|
51
packages/core/tests/40-modules/10-storage-test.ts
Normal file
51
packages/core/tests/40-modules/10-storage-test.ts
Normal file
@ -0,0 +1,51 @@
|
||||
import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { getIconName, storageFunctions } from '../../lib/storage/functions';
|
||||
import { IconifyIconName } from '../../lib/icon/name';
|
||||
|
||||
describe('Testing IconifyStorageFunctions', () => {
|
||||
let count = 0;
|
||||
|
||||
function nextProvider(): string {
|
||||
return 'storage-test-' + count++;
|
||||
}
|
||||
|
||||
it('Getting icon name', () => {
|
||||
let expected: IconifyIconName;
|
||||
|
||||
expected = {
|
||||
provider: '',
|
||||
prefix: 'mdi',
|
||||
name: 'home',
|
||||
};
|
||||
expect(getIconName('mdi:home')).to.be.eql(expected);
|
||||
|
||||
expected = {
|
||||
provider: 'local-test',
|
||||
prefix: 'mdi',
|
||||
name: 'home',
|
||||
};
|
||||
expect(getIconName('@local-test:mdi:home')).to.be.eql(expected);
|
||||
|
||||
expect(getIconName('test')).to.be.equal(null);
|
||||
});
|
||||
|
||||
it('Storage functions', () => {
|
||||
const provider = nextProvider();
|
||||
const testName = `@${provider}:foo:bar`;
|
||||
|
||||
// Empty
|
||||
expect(storageFunctions.iconExists(testName)).to.be.equal(false);
|
||||
expect(storageFunctions.getIcon(testName)).to.be.equal(null);
|
||||
expect(storageFunctions.listIcons(provider)).to.be.eql([]);
|
||||
|
||||
// Add and test one icon
|
||||
expect(
|
||||
storageFunctions.addIcon(testName, {
|
||||
body: '<g />',
|
||||
})
|
||||
).to.be.equal(true);
|
||||
expect(storageFunctions.iconExists(testName)).to.be.equal(true);
|
||||
expect(storageFunctions.listIcons(provider)).to.be.eql([testName]);
|
||||
});
|
||||
});
|
@ -1,22 +1,13 @@
|
||||
import { IconifyJSON } from '@iconify/types';
|
||||
import { merge } from '@iconify/core/lib/misc/merge';
|
||||
import {
|
||||
stringToIcon,
|
||||
validateIcon,
|
||||
IconifyIconName,
|
||||
} from '@iconify/core/lib/icon/name';
|
||||
import { IconifyIcon, FullIconifyIcon } from '@iconify/core/lib/icon';
|
||||
import { stringToIcon } from '@iconify/core/lib/icon/name';
|
||||
import {
|
||||
IconifyIconCustomisations,
|
||||
fullCustomisations,
|
||||
} from '@iconify/core/lib/customisations';
|
||||
import {
|
||||
getStorage,
|
||||
getIcon,
|
||||
addIcon,
|
||||
addIconSet,
|
||||
listIcons,
|
||||
} from '@iconify/core/lib/storage';
|
||||
storageFunctions,
|
||||
getIconData,
|
||||
} from '@iconify/core/lib/storage/functions';
|
||||
import { iconToSVG, IconifyIconBuildResult } from '@iconify/core/lib/builder';
|
||||
import { replaceIDs } from '@iconify/core/lib/builder/ids';
|
||||
import { renderIcon } from './modules/render';
|
||||
@ -35,27 +26,6 @@ import { finder as iconifyFinder } from './finders/iconify';
|
||||
import { findRootNode, addBodyNode } from './modules/root';
|
||||
// import { finder as iconifyIconFinder } from './finders/iconify-icon';
|
||||
|
||||
/**
|
||||
* Get icon name
|
||||
*/
|
||||
function getIconName(name: string): IconifyIconName | null {
|
||||
const icon = stringToIcon(name);
|
||||
if (!validateIcon(icon)) {
|
||||
return null;
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get icon data
|
||||
*/
|
||||
function getIconData(name: string): FullIconifyIcon | null {
|
||||
const icon = getIconName(name);
|
||||
return icon
|
||||
? getIcon(getStorage(icon.provider, icon.prefix), icon.name)
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SVG data
|
||||
*/
|
||||
@ -107,67 +77,16 @@ function generateIcon(
|
||||
) as unknown) as SVGElement | string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add icon set
|
||||
*/
|
||||
export function addCollection(data: IconifyJSON, provider?: string) {
|
||||
if (typeof provider !== 'string') {
|
||||
provider = typeof data.provider === 'string' ? data.provider : '';
|
||||
}
|
||||
|
||||
if (
|
||||
typeof data !== 'object' ||
|
||||
typeof data.prefix !== 'string' ||
|
||||
!validateIcon({
|
||||
provider,
|
||||
prefix: data.prefix,
|
||||
name: 'a',
|
||||
})
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const storage = getStorage(provider, data.prefix);
|
||||
return !!addIconSet(storage, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iconify interface
|
||||
*/
|
||||
export interface IconifyGlobal {
|
||||
export interface IconifyCommonFunctions {
|
||||
/* General section */
|
||||
/**
|
||||
* Get version
|
||||
*/
|
||||
getVersion: () => string;
|
||||
|
||||
/* Getting icons */
|
||||
/**
|
||||
* Check if icon exists
|
||||
*/
|
||||
iconExists: (name: string) => boolean;
|
||||
|
||||
/**
|
||||
* Get icon data with all properties
|
||||
*/
|
||||
getIcon: (name: string) => Required<IconifyIcon> | null;
|
||||
|
||||
/**
|
||||
* List all available icons
|
||||
*/
|
||||
listIcons: (provider?: string, prefix?: string) => string[];
|
||||
|
||||
/* Add icons */
|
||||
/**
|
||||
* Add icon to storage
|
||||
*/
|
||||
addIcon: (name: string, data: IconifyIcon) => boolean;
|
||||
|
||||
/**
|
||||
* Add icon set to storage
|
||||
*/
|
||||
addCollection: (data: IconifyJSON, provider?: string) => boolean;
|
||||
|
||||
/* Render icons */
|
||||
/**
|
||||
* Render icons
|
||||
@ -226,35 +145,10 @@ export interface IconifyGlobal {
|
||||
/**
|
||||
* Global variable
|
||||
*/
|
||||
export const IconifyCommon: IconifyGlobal = {
|
||||
export const commonFunctions: IconifyCommonFunctions = {
|
||||
// Version
|
||||
getVersion: () => '__iconify_version__',
|
||||
|
||||
// Check if icon exists
|
||||
iconExists: (name) => getIconData(name) !== null,
|
||||
|
||||
// Get raw icon data
|
||||
getIcon: (name) => {
|
||||
const result = getIconData(name);
|
||||
return result ? merge(result) : null;
|
||||
},
|
||||
|
||||
// List icons
|
||||
listIcons,
|
||||
|
||||
// Add icon
|
||||
addIcon: (name, data) => {
|
||||
const icon = getIconName(name);
|
||||
if (!icon) {
|
||||
return false;
|
||||
}
|
||||
const storage = getStorage(icon.provider, icon.prefix);
|
||||
return addIcon(storage, icon.name, data);
|
||||
},
|
||||
|
||||
// Add icon set
|
||||
addCollection,
|
||||
|
||||
// Render SVG
|
||||
renderSVG: (name: string, customisations: IconifyIconCustomisations) => {
|
||||
return generateIcon(name, customisations, false) as SVGElement | null;
|
||||
@ -350,7 +244,7 @@ if (typeof document !== 'undefined' && typeof window !== 'undefined') {
|
||||
typeof item.icons !== 'object' ||
|
||||
typeof item.prefix !== 'string' ||
|
||||
// Add icon set
|
||||
!addCollection(item)
|
||||
!storageFunctions.addCollection(item)
|
||||
) {
|
||||
console.error(err);
|
||||
}
|
||||
|
@ -10,12 +10,20 @@ import {
|
||||
} from '@iconify/core/lib/customisations';
|
||||
import { IconifyIconBuildResult } from '@iconify/core/lib/builder';
|
||||
import { calcSize } from '@iconify/core/lib/builder/calc-size';
|
||||
import {
|
||||
IconifyStorageFunctions,
|
||||
storageFunctions,
|
||||
} from '@iconify/core/lib/storage/functions';
|
||||
|
||||
// Modules
|
||||
import { coreModules } from '@iconify/core/lib/modules';
|
||||
|
||||
// Cache
|
||||
import { storeCache, loadCache, config } from '@iconify/core/lib/cache/storage';
|
||||
import {
|
||||
storeCache,
|
||||
loadCache,
|
||||
config,
|
||||
} from '@iconify/core/lib/storage/browser';
|
||||
|
||||
// API
|
||||
import {
|
||||
@ -51,7 +59,7 @@ import {
|
||||
|
||||
// Other
|
||||
import { IconifyExposedCommonInternals } from './internals';
|
||||
import { IconifyGlobal as IconifyGlobal1, IconifyCommon } from './common';
|
||||
import { IconifyCommonFunctions, commonFunctions } from './common';
|
||||
|
||||
/**
|
||||
* Export required types
|
||||
@ -97,7 +105,7 @@ export interface IconifyExposedInternals
|
||||
/**
|
||||
* Exported functions
|
||||
*/
|
||||
export interface IconifyGlobal2 extends IconifyAPI {
|
||||
export interface IconifyFunctions extends IconifyAPI {
|
||||
/**
|
||||
* Expose internal functions
|
||||
*/
|
||||
@ -107,7 +115,10 @@ export interface IconifyGlobal2 extends IconifyAPI {
|
||||
/**
|
||||
* Iconify interface
|
||||
*/
|
||||
export interface IconifyGlobal extends IconifyGlobal1, IconifyGlobal2 {}
|
||||
export interface IconifyGlobal
|
||||
extends IconifyStorageFunctions,
|
||||
IconifyCommonFunctions,
|
||||
IconifyFunctions {}
|
||||
|
||||
// Export dependencies
|
||||
export { IconifyGlobal as IconifyGlobalCommon, IconifyAPI };
|
||||
@ -159,12 +170,14 @@ const Iconify: IconifyGlobal = ({
|
||||
// Get API module
|
||||
setAPIModule,
|
||||
},
|
||||
} as IconifyGlobal2) as IconifyGlobal;
|
||||
} as IconifyFunctions) as IconifyGlobal;
|
||||
|
||||
// Merge with common functions
|
||||
for (const key in IconifyCommon) {
|
||||
Iconify[key] = IconifyCommon[key];
|
||||
}
|
||||
[storageFunctions, commonFunctions].forEach((list) => {
|
||||
for (const key in list) {
|
||||
Iconify[key] = list[key];
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Initialise stuff
|
||||
|
@ -10,10 +10,14 @@ import {
|
||||
} from '@iconify/core/lib/customisations';
|
||||
import { IconifyIconBuildResult } from '@iconify/core/lib/builder';
|
||||
import { calcSize } from '@iconify/core/lib/builder/calc-size';
|
||||
import {
|
||||
IconifyStorageFunctions,
|
||||
storageFunctions,
|
||||
} from '@iconify/core/lib/storage/functions';
|
||||
|
||||
// Local code
|
||||
import { IconifyExposedCommonInternals } from './internals';
|
||||
import { IconifyGlobal as IconifyGlobal1, IconifyCommon } from './common';
|
||||
import { IconifyCommonFunctions, commonFunctions } from './common';
|
||||
|
||||
/**
|
||||
* Export required types
|
||||
@ -45,7 +49,7 @@ export interface IconifyExposedInternals
|
||||
/**
|
||||
* Exported functions
|
||||
*/
|
||||
export interface IconifyGlobal2 {
|
||||
export interface IconifyFunctions {
|
||||
/**
|
||||
* Expose internal functions
|
||||
*/
|
||||
@ -55,7 +59,10 @@ export interface IconifyGlobal2 {
|
||||
/**
|
||||
* Iconify interface
|
||||
*/
|
||||
export interface IconifyGlobal extends IconifyGlobal1, IconifyGlobal2 {}
|
||||
export interface IconifyGlobal
|
||||
extends IconifyStorageFunctions,
|
||||
IconifyCommonFunctions,
|
||||
IconifyFunctions {}
|
||||
|
||||
// Export dependencies
|
||||
export { IconifyGlobal as IconifyGlobalCommon };
|
||||
@ -69,11 +76,13 @@ const Iconify: IconifyGlobal = ({
|
||||
// Calculate size
|
||||
calculateSize: calcSize,
|
||||
},
|
||||
} as IconifyGlobal2) as IconifyGlobal;
|
||||
} as IconifyFunctions) as IconifyGlobal;
|
||||
|
||||
// Merge with common functions
|
||||
for (const key in IconifyCommon) {
|
||||
Iconify[key] = IconifyCommon[key];
|
||||
}
|
||||
[storageFunctions, commonFunctions].forEach((list) => {
|
||||
for (const key in list) {
|
||||
Iconify[key] = list[key];
|
||||
}
|
||||
});
|
||||
|
||||
export default Iconify;
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { IconifyIconName } from '@iconify/core/lib/icon/name';
|
||||
import { getStorage, getIcon } from '@iconify/core/lib/storage';
|
||||
import { getStorage, getIcon } from '@iconify/core/lib/storage/storage';
|
||||
import { coreModules } from '@iconify/core/lib/modules';
|
||||
import { FullIconifyIcon } from '@iconify/core/lib/icon';
|
||||
import { findPlaceholders } from './finder';
|
||||
|
@ -9,11 +9,7 @@ import {
|
||||
} from '@iconify/react/lib/icon';
|
||||
|
||||
// Core
|
||||
import {
|
||||
stringToIcon,
|
||||
validateIcon,
|
||||
IconifyIconName,
|
||||
} from '@iconify/core/lib/icon/name';
|
||||
import { IconifyIconName } from '@iconify/core/lib/icon/name';
|
||||
import {
|
||||
IconifyIconCustomisations,
|
||||
IconifyIconSize,
|
||||
@ -21,14 +17,12 @@ import {
|
||||
IconifyVerticalIconAlignment,
|
||||
} from '@iconify/core/lib/customisations';
|
||||
import {
|
||||
getStorage,
|
||||
getIcon as _getIcon,
|
||||
addIcon as addIconToStorage,
|
||||
addIconSet,
|
||||
listIcons,
|
||||
} from '@iconify/core/lib/storage';
|
||||
storageFunctions,
|
||||
getIconData,
|
||||
getIconName,
|
||||
} from '@iconify/core/lib/storage/functions';
|
||||
import { calcSize } from '@iconify/core/lib/builder/calc-size';
|
||||
import { IconifyIcon, FullIconifyIcon } from '@iconify/core/lib/icon';
|
||||
import { IconifyIcon } from '@iconify/core/lib/icon';
|
||||
|
||||
// Modules
|
||||
import { coreModules } from '@iconify/core/lib/modules';
|
||||
@ -62,7 +56,11 @@ import {
|
||||
} from '@iconify/core/lib/interfaces/loader';
|
||||
|
||||
// Cache
|
||||
import { storeCache, loadCache, config } from '@iconify/core/lib/cache/storage';
|
||||
import {
|
||||
storeCache,
|
||||
loadCache,
|
||||
config,
|
||||
} from '@iconify/core/lib/storage/browser';
|
||||
|
||||
/**
|
||||
* Export required types
|
||||
@ -155,81 +153,35 @@ export function disableCache(storage: IconifyCacheType): void {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get icon name
|
||||
* Check if icon exists
|
||||
*/
|
||||
function _getIconName(name: string): IconifyIconName | null {
|
||||
const icon = stringToIcon(name);
|
||||
if (!validateIcon(icon)) {
|
||||
return null;
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
export const iconExists = storageFunctions.iconExists;
|
||||
|
||||
/**
|
||||
* Get icon data
|
||||
*/
|
||||
function _getIconData(icon: IconifyIconName): FullIconifyIcon | null {
|
||||
return _getIcon(getStorage(icon.provider, icon.prefix), icon.name);
|
||||
}
|
||||
|
||||
export function getIcon(name: string): Required<IconifyIcon> | null {
|
||||
const icon = _getIconName(name);
|
||||
return icon ? _getIconData(icon) : null;
|
||||
}
|
||||
export const getIcon = storageFunctions.getIcon;
|
||||
|
||||
/**
|
||||
* Check if icon exists
|
||||
* List available icons
|
||||
*/
|
||||
export function iconExists(name: string): boolean {
|
||||
return getIcon(name) !== null;
|
||||
}
|
||||
export const listIcons = storageFunctions.listIcons;
|
||||
|
||||
/**
|
||||
* Add one icon
|
||||
*/
|
||||
export const addIcon = storageFunctions.addIcon;
|
||||
|
||||
/**
|
||||
* Add icon set
|
||||
*/
|
||||
export const addCollection = storageFunctions.addCollection;
|
||||
|
||||
/**
|
||||
* Load icons
|
||||
*/
|
||||
export const loadIcons: IconifyLoadIcons = API.loadIcons;
|
||||
|
||||
/**
|
||||
* List available icons
|
||||
*/
|
||||
export { listIcons };
|
||||
|
||||
/**
|
||||
* Add one icon
|
||||
*/
|
||||
export function addIcon(name: string, data: IconifyIcon): boolean {
|
||||
const icon = _getIconName(name);
|
||||
if (!icon) {
|
||||
return false;
|
||||
}
|
||||
const storage = getStorage(icon.provider, icon.prefix);
|
||||
return addIconToStorage(storage, icon.name, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add icon set
|
||||
*/
|
||||
export function addCollection(data: IconifyJSON, provider?: string) {
|
||||
if (typeof provider !== 'string') {
|
||||
provider = typeof data.provider === 'string' ? data.provider : '';
|
||||
}
|
||||
|
||||
if (
|
||||
typeof data !== 'object' ||
|
||||
typeof data.prefix !== 'string' ||
|
||||
!validateIcon({
|
||||
provider,
|
||||
prefix: data.prefix,
|
||||
name: 'a',
|
||||
})
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const storage = getStorage(provider, data.prefix);
|
||||
return !!addIconSet(storage, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add API provider
|
||||
*/
|
||||
@ -318,7 +270,7 @@ class DynamicComponent extends Component<StatefulProps, StatefulState> {
|
||||
*/
|
||||
_loaded(): boolean {
|
||||
if (!this.state.loaded) {
|
||||
const data = _getIconData(this.props.icon);
|
||||
const data = getIconData(this.props.icon);
|
||||
if (data) {
|
||||
this._abort = null;
|
||||
this.setState({
|
||||
@ -350,12 +302,12 @@ const component = (props: IconProps, func: typeof ReactIcon): JSX.Element => {
|
||||
|
||||
// Get icon data
|
||||
if (typeof props.icon === 'string') {
|
||||
const iconName = _getIconName(props.icon);
|
||||
const iconName = getIconName(props.icon);
|
||||
if (!iconName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
iconData = _getIconData(iconName);
|
||||
iconData = getIconData(iconName);
|
||||
|
||||
if (iconData) {
|
||||
let staticProps: IconProps = {} as IconProps;
|
||||
|
Loading…
x
Reference in New Issue
Block a user