1
Some checks failed
CI / Test (ubuntu-latest) (push) Has been cancelled
CI / Test (windows-latest) (push) Has been cancelled
CI / Lint (ubuntu-latest) (push) Has been cancelled
CI / Lint (windows-latest) (push) Has been cancelled
CI / Check (ubuntu-latest) (push) Has been cancelled
CI / Check (windows-latest) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
Deploy Website on push / Deploy Push Playground Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Docs Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Antd Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Element Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Naive Ftp (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
CI / CI OK (push) Has been cancelled
Deploy Website on push / Rerun on failure (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Issue Close Require / close-issues (push) Has been cancelled
Close stale issues / stale (push) Has been cancelled
Some checks failed
CI / Test (ubuntu-latest) (push) Has been cancelled
CI / Test (windows-latest) (push) Has been cancelled
CI / Lint (ubuntu-latest) (push) Has been cancelled
CI / Lint (windows-latest) (push) Has been cancelled
CI / Check (ubuntu-latest) (push) Has been cancelled
CI / Check (windows-latest) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
Deploy Website on push / Deploy Push Playground Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Docs Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Antd Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Element Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Naive Ftp (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
CI / CI OK (push) Has been cancelled
Deploy Website on push / Rerun on failure (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Issue Close Require / close-issues (push) Has been cancelled
Close stale issues / stale (push) Has been cancelled
This commit is contained in:
79
packages/icons/src/svg/load.ts
Normal file
79
packages/icons/src/svg/load.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import type { IconifyIconStructure } from '@vben-core/icons';
|
||||
|
||||
import { addIcon } from '@vben-core/icons';
|
||||
|
||||
let loaded = false;
|
||||
if (!loaded) {
|
||||
loadSvgIcons();
|
||||
loaded = true;
|
||||
}
|
||||
|
||||
function parseSvg(svgData: string): IconifyIconStructure {
|
||||
const parser = new DOMParser();
|
||||
const xmlDoc = parser.parseFromString(svgData, 'image/svg+xml');
|
||||
const svgElement = xmlDoc.documentElement;
|
||||
|
||||
// 提取 SVG 根元素的关键样式属性
|
||||
const getAttrs = (el: Element, attrs: string[]) =>
|
||||
attrs
|
||||
.map((attr) =>
|
||||
el.hasAttribute(attr) ? `${attr}="${el.getAttribute(attr)}"` : '',
|
||||
)
|
||||
.filter(Boolean)
|
||||
.join(' ');
|
||||
|
||||
const rootAttrs = getAttrs(svgElement, [
|
||||
'fill',
|
||||
'stroke',
|
||||
'fill-rule',
|
||||
'stroke-width',
|
||||
]);
|
||||
|
||||
const svgContent = [...svgElement.childNodes]
|
||||
.filter((node) => node.nodeType === Node.ELEMENT_NODE)
|
||||
.map((node) => new XMLSerializer().serializeToString(node))
|
||||
.join('');
|
||||
// 若根有属性,用一个 g 标签包裹内容并继承属性
|
||||
const body = rootAttrs ? `<g ${rootAttrs}>${svgContent}</g>` : svgContent;
|
||||
|
||||
const viewBoxValue = svgElement.getAttribute('viewBox') || '';
|
||||
const [left, top, width, height] = viewBoxValue.split(' ').map((val) => {
|
||||
const num = Number(val);
|
||||
return Number.isNaN(num) ? undefined : num;
|
||||
});
|
||||
|
||||
return {
|
||||
body,
|
||||
height,
|
||||
left,
|
||||
top,
|
||||
width,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义的svg图片转化为组件
|
||||
* @example ./svg/avatar.svg
|
||||
* <Icon icon="svg:avatar"></Icon>
|
||||
*/
|
||||
async function loadSvgIcons() {
|
||||
const svgEagers = import.meta.glob('./icons/**', {
|
||||
eager: true,
|
||||
query: '?raw',
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
Object.entries(svgEagers).map((svg) => {
|
||||
const [key, body] = svg as [string, string | { default: string }];
|
||||
|
||||
// ./icons/xxxx.svg => xxxxxx
|
||||
const start = key.lastIndexOf('/') + 1;
|
||||
const end = key.lastIndexOf('.');
|
||||
const iconName = key.slice(start, end);
|
||||
|
||||
return addIcon(`svg:${iconName}`, {
|
||||
...parseSvg(typeof body === 'object' ? body.default : body),
|
||||
});
|
||||
}),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user