mirror of
https://github.com/iconify/iconify.git
synced 2024-12-13 14:13:06 +00:00
Prettier recently updated files
This commit is contained in:
parent
c8da6958ab
commit
4487568ba4
@ -1,5 +1,9 @@
|
|||||||
import createDebugger from 'debug';
|
import createDebugger from 'debug';
|
||||||
import type { CustomIconLoader, IconCustomizations, InlineCollection } from './types';
|
import type {
|
||||||
|
CustomIconLoader,
|
||||||
|
IconCustomizations,
|
||||||
|
InlineCollection,
|
||||||
|
} from './types';
|
||||||
import { mergeIconProps } from './utils';
|
import { mergeIconProps } from './utils';
|
||||||
|
|
||||||
const debug = createDebugger('@iconify-loader:custom');
|
const debug = createDebugger('@iconify-loader:custom');
|
||||||
@ -11,7 +15,7 @@ export async function getCustomIcon(
|
|||||||
custom: CustomIconLoader | InlineCollection,
|
custom: CustomIconLoader | InlineCollection,
|
||||||
collection: string,
|
collection: string,
|
||||||
icon: string,
|
icon: string,
|
||||||
iconsCustomizations?: IconCustomizations,
|
iconsCustomizations?: IconCustomizations
|
||||||
): Promise<string | undefined> {
|
): Promise<string | undefined> {
|
||||||
let result: string | undefined | null;
|
let result: string | undefined | null;
|
||||||
|
|
||||||
@ -26,10 +30,16 @@ export async function getCustomIcon(
|
|||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
if (!result.startsWith('<svg ')) {
|
if (!result.startsWith('<svg ')) {
|
||||||
console.warn(`Custom icon "${icon}" in "${collection}" is not a valid SVG`)
|
console.warn(
|
||||||
return result
|
`Custom icon "${icon}" in "${collection}" is not a valid SVG`
|
||||||
|
);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
const { transform, additionalProps = {}, iconCustomizer } = iconsCustomizations || {}
|
const {
|
||||||
|
transform,
|
||||||
|
additionalProps = {},
|
||||||
|
iconCustomizer,
|
||||||
|
} = iconsCustomizations || {};
|
||||||
return await mergeIconProps(
|
return await mergeIconProps(
|
||||||
transform ? await transform(result) : result,
|
transform ? await transform(result) : result,
|
||||||
collection,
|
collection,
|
||||||
@ -37,7 +47,6 @@ export async function getCustomIcon(
|
|||||||
additionalProps,
|
additionalProps,
|
||||||
undefined,
|
undefined,
|
||||||
iconCustomizer
|
iconCustomizer
|
||||||
)
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,10 @@ const debugLegacy = createDebugger('@iconify-loader:legacy');
|
|||||||
const _collections: Record<string, Promise<IconifyJSON | undefined>> = {};
|
const _collections: Record<string, Promise<IconifyJSON | undefined>> = {};
|
||||||
const isLegacyExists = isPackageExists('@iconify/json');
|
const isLegacyExists = isPackageExists('@iconify/json');
|
||||||
|
|
||||||
export async function loadCollection(name: string, autoInstall = false): Promise<IconifyJSON | undefined> {
|
export async function loadCollection(
|
||||||
|
name: string,
|
||||||
|
autoInstall = false
|
||||||
|
): Promise<IconifyJSON | undefined> {
|
||||||
if (!_collections[name]) {
|
if (!_collections[name]) {
|
||||||
_collections[name] = task();
|
_collections[name] = task();
|
||||||
}
|
}
|
||||||
@ -43,8 +46,7 @@ export async function loadCollection(name: string, autoInstall = false): Promise
|
|||||||
|
|
||||||
if (jsonPath) {
|
if (jsonPath) {
|
||||||
return JSON.parse(await fs.readFile(jsonPath, 'utf8'));
|
return JSON.parse(await fs.readFile(jsonPath, 'utf8'));
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
debugModern(`failed to load ${name}`);
|
debugModern(`failed to load ${name}`);
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@ -55,18 +57,24 @@ export async function searchForIcon(
|
|||||||
iconSet: IconifyJSON,
|
iconSet: IconifyJSON,
|
||||||
collection: string,
|
collection: string,
|
||||||
ids: string[],
|
ids: string[],
|
||||||
iconCustomizations?: IconCustomizations,
|
iconCustomizations?: IconCustomizations
|
||||||
): Promise<string | undefined> {
|
): Promise<string | undefined> {
|
||||||
let iconData: FullIconifyIcon | null;
|
let iconData: FullIconifyIcon | null;
|
||||||
const { customize, additionalProps = {}, iconCustomizer } = iconCustomizations || {}
|
const {
|
||||||
|
customize,
|
||||||
|
additionalProps = {},
|
||||||
|
iconCustomizer,
|
||||||
|
} = iconCustomizations || {};
|
||||||
for (const id of ids) {
|
for (const id of ids) {
|
||||||
iconData = getIconData(iconSet, id, true);
|
iconData = getIconData(iconSet, id, true);
|
||||||
if (iconData) {
|
if (iconData) {
|
||||||
debug(`${collection}:${id}`);
|
debug(`${collection}:${id}`);
|
||||||
const defaultCustomizations = { ...DefaultIconCustomizations }
|
const defaultCustomizations = { ...DefaultIconCustomizations };
|
||||||
const { attributes, body } = iconToSVG(
|
const { attributes, body } = iconToSVG(
|
||||||
iconData,
|
iconData,
|
||||||
typeof customize === 'function' ? customize(defaultCustomizations) : defaultCustomizations
|
typeof customize === 'function'
|
||||||
|
? customize(defaultCustomizations)
|
||||||
|
: defaultCustomizations
|
||||||
);
|
);
|
||||||
return await mergeIconProps(
|
return await mergeIconProps(
|
||||||
`<svg>${body}</svg>`,
|
`<svg>${body}</svg>`,
|
||||||
@ -74,8 +82,8 @@ export async function searchForIcon(
|
|||||||
id,
|
id,
|
||||||
additionalProps,
|
additionalProps,
|
||||||
() => attributes,
|
() => attributes,
|
||||||
iconCustomizer,
|
iconCustomizer
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,11 @@ export type CustomIconLoader = (name: string) => Awaitable<string | undefined>;
|
|||||||
/**
|
/**
|
||||||
* Custom icon customizer, it will allow to customize all icons on a collection or individual icons.
|
* Custom icon customizer, it will allow to customize all icons on a collection or individual icons.
|
||||||
*/
|
*/
|
||||||
export type IconCustomizer = (collection: string, icon: string, props: Record<string, string>) => Awaitable<void>;
|
export type IconCustomizer = (
|
||||||
|
collection: string,
|
||||||
|
icon: string,
|
||||||
|
props: Record<string, string>
|
||||||
|
) => Awaitable<void>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Icon customizations: will be applied to all resolved icons.
|
* Icon customizations: will be applied to all resolved icons.
|
||||||
@ -29,24 +33,26 @@ export type IconCustomizations = {
|
|||||||
* @param svg The loaded `svg`
|
* @param svg The loaded `svg`
|
||||||
* @return The transformed `svg`.
|
* @return The transformed `svg`.
|
||||||
*/
|
*/
|
||||||
transform?: (svg: string) => Awaitable<string>
|
transform?: (svg: string) => Awaitable<string>;
|
||||||
/**
|
/**
|
||||||
* Change default icon customizations values.
|
* Change default icon customizations values.
|
||||||
*
|
*
|
||||||
* @param defaultCustomizations Default icon's customizations values.
|
* @param defaultCustomizations Default icon's customizations values.
|
||||||
* @return The modified icon's customizations values.
|
* @return The modified icon's customizations values.
|
||||||
*/
|
*/
|
||||||
customize?: (defaultCustomizations: FullIconCustomisations) => FullIconCustomisations
|
customize?: (
|
||||||
|
defaultCustomizations: FullIconCustomisations
|
||||||
|
) => FullIconCustomisations;
|
||||||
/**
|
/**
|
||||||
* Custom icon customizer.
|
* Custom icon customizer.
|
||||||
*/
|
*/
|
||||||
iconCustomizer?: IconCustomizer
|
iconCustomizer?: IconCustomizer;
|
||||||
/**
|
/**
|
||||||
* Additional icon properties.
|
* Additional icon properties.
|
||||||
*
|
*
|
||||||
* All properties without value will not be applied.
|
* All properties without value will not be applied.
|
||||||
*/
|
*/
|
||||||
additionalProps?: Record<string, string | undefined>
|
additionalProps?: Record<string, string | undefined>;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -21,17 +21,21 @@ export async function mergeIconProps(
|
|||||||
icon: string,
|
icon: string,
|
||||||
additionalProps: Record<string, string | undefined>,
|
additionalProps: Record<string, string | undefined>,
|
||||||
propsProvider?: () => Awaitable<Record<string, string>>,
|
propsProvider?: () => Awaitable<Record<string, string>>,
|
||||||
iconCustomizer?: IconCustomizer,
|
iconCustomizer?: IconCustomizer
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const props: Record<string, string> = await propsProvider?.() ?? {}
|
const props: Record<string, string> = (await propsProvider?.()) ?? {};
|
||||||
await iconCustomizer?.(collection, icon, props)
|
await iconCustomizer?.(collection, icon, props);
|
||||||
Object.keys(additionalProps).forEach((p) => {
|
Object.keys(additionalProps).forEach((p) => {
|
||||||
const v = additionalProps[p]
|
const v = additionalProps[p];
|
||||||
if (v !== undefined && v !== null)
|
if (v !== undefined && v !== null) props[p] = v;
|
||||||
props[p] = v
|
});
|
||||||
})
|
const replacement = svg.startsWith('<svg ') ? '<svg ' : '<svg';
|
||||||
const replacement = svg.startsWith('<svg ') ? '<svg ' : '<svg'
|
return svg.replace(
|
||||||
return svg.replace(replacement, `${replacement}${Object.keys(props).map(p => `${p}="${props[p]}"`).join(' ')}`)
|
replacement,
|
||||||
|
`${replacement}${Object.keys(props)
|
||||||
|
.map((p) => `${p}="${props[p]}"`)
|
||||||
|
.join(' ')}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function tryInstallPkg(name: string): Promise<void | undefined> {
|
export async function tryInstallPkg(name: string): Promise<void | undefined> {
|
||||||
|
@ -12,17 +12,11 @@ describe('Testing getCustomIcon', () => {
|
|||||||
|
|
||||||
test('CustomIconLoader with transform', async () => {
|
test('CustomIconLoader with transform', async () => {
|
||||||
const svg = await fs.readFile(fixturesDir + '/circle.svg', 'utf8');
|
const svg = await fs.readFile(fixturesDir + '/circle.svg', 'utf8');
|
||||||
const result = await getCustomIcon(
|
const result = await getCustomIcon(() => svg, 'a', 'b', {
|
||||||
() => svg,
|
|
||||||
'a',
|
|
||||||
'b',
|
|
||||||
{
|
|
||||||
transform(icon) {
|
transform(icon) {
|
||||||
return icon.replace('<svg ', '<svg width="1em" height="1em" ');
|
return icon.replace('<svg ', '<svg width="1em" height="1em" ');
|
||||||
}
|
},
|
||||||
}
|
});
|
||||||
|
|
||||||
);
|
|
||||||
expect(result && result.indexOf('width="1em"') > -1).toBeTruthy();
|
expect(result && result.indexOf('width="1em"') > -1).toBeTruthy();
|
||||||
expect(result && result.indexOf('height="1em"') > -1).toBeTruthy();
|
expect(result && result.indexOf('height="1em"') > -1).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user