数通智联化工云平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

832 lines
21 KiB

'use strict';
const ofetch = require('ofetch');
const defaultIconDimensions = Object.freeze(
{
left: 0,
top: 0,
width: 16,
height: 16
}
);
const defaultIconTransformations = Object.freeze({
rotate: 0,
vFlip: false,
hFlip: false
});
const defaultIconProps = Object.freeze({
...defaultIconDimensions,
...defaultIconTransformations
});
const defaultExtendedIconProps = Object.freeze({
...defaultIconProps,
body: "",
hidden: false
});
const defaultIconSizeCustomisations = Object.freeze({
width: null,
height: null
});
const defaultIconCustomisations = Object.freeze({
// Dimensions
...defaultIconSizeCustomisations,
// Transformations
...defaultIconTransformations
});
function mergeIconTransformations(obj1, obj2) {
const result = {};
if (!obj1.hFlip !== !obj2.hFlip) {
result.hFlip = true;
}
if (!obj1.vFlip !== !obj2.vFlip) {
result.vFlip = true;
}
const rotate = ((obj1.rotate || 0) + (obj2.rotate || 0)) % 4;
if (rotate) {
result.rotate = rotate;
}
return result;
}
function mergeIconData(parent, child) {
const result = mergeIconTransformations(parent, child);
for (const key in defaultExtendedIconProps) {
if (key in defaultIconTransformations) {
if (key in parent && !(key in result)) {
result[key] = defaultIconTransformations[key];
}
} else if (key in child) {
result[key] = child[key];
} else if (key in parent) {
result[key] = parent[key];
}
}
return result;
}
function getIconsTree(data, names) {
const icons = data.icons;
const aliases = data.aliases || /* @__PURE__ */ Object.create(null);
const resolved = /* @__PURE__ */ Object.create(null);
function resolve(name) {
if (icons[name]) {
return resolved[name] = [];
}
if (!(name in resolved)) {
resolved[name] = null;
const parent = aliases[name] && aliases[name].parent;
const value = parent && resolve(parent);
if (value) {
resolved[name] = [parent].concat(value);
}
}
return resolved[name];
}
(names || Object.keys(icons).concat(Object.keys(aliases))).forEach(resolve);
return resolved;
}
function internalGetIconData(data, name, tree) {
const icons = data.icons;
const aliases = data.aliases || /* @__PURE__ */ Object.create(null);
let currentProps = {};
function parse(name2) {
currentProps = mergeIconData(
icons[name2] || aliases[name2],
currentProps
);
}
parse(name);
tree.forEach(parse);
return mergeIconData(data, currentProps);
}
function getIconData(data, name) {
if (data.icons[name]) {
return internalGetIconData(data, name, []);
}
const tree = getIconsTree(data, [name])[name];
return tree ? internalGetIconData(data, name, tree) : null;
}
({
provider: "",
aliases: {},
not_found: {},
...defaultIconDimensions
});
Object.keys(defaultIconDimensions).concat([
"provider"
]);
const unitsSplit = /(-?[0-9.]*[0-9]+[0-9.]*)/g;
const unitsTest = /^-?[0-9.]*[0-9]+[0-9.]*$/g;
function calculateSize(size, ratio, precision) {
if (ratio === 1) {
return size;
}
precision = precision || 100;
if (typeof size === "number") {
return Math.ceil(size * ratio * precision) / precision;
}
if (typeof size !== "string") {
return size;
}
const oldParts = size.split(unitsSplit);
if (oldParts === null || !oldParts.length) {
return size;
}
const newParts = [];
let code = oldParts.shift();
let isNumber = unitsTest.test(code);
while (true) {
if (isNumber) {
const num = parseFloat(code);
if (isNaN(num)) {
newParts.push(code);
} else {
newParts.push(Math.ceil(num * ratio * precision) / precision);
}
} else {
newParts.push(code);
}
code = oldParts.shift();
if (code === void 0) {
return newParts.join("");
}
isNumber = !isNumber;
}
}
const isUnsetKeyword = (value) => value === "unset" || value === "undefined" || value === "none";
function iconToSVG(icon, customisations) {
const fullIcon = {
...defaultIconProps,
...icon
};
const fullCustomisations = {
...defaultIconCustomisations,
...customisations
};
const box = {
left: fullIcon.left,
top: fullIcon.top,
width: fullIcon.width,
height: fullIcon.height
};
let body = fullIcon.body;
[fullIcon, fullCustomisations].forEach((props) => {
const transformations = [];
const hFlip = props.hFlip;
const vFlip = props.vFlip;
let rotation = props.rotate;
if (hFlip) {
if (vFlip) {
rotation += 2;
} else {
transformations.push(
"translate(" + (box.width + box.left).toString() + " " + (0 - box.top).toString() + ")"
);
transformations.push("scale(-1 1)");
box.top = box.left = 0;
}
} else if (vFlip) {
transformations.push(
"translate(" + (0 - box.left).toString() + " " + (box.height + box.top).toString() + ")"
);
transformations.push("scale(1 -1)");
box.top = box.left = 0;
}
let tempValue;
if (rotation < 0) {
rotation -= Math.floor(rotation / 4) * 4;
}
rotation = rotation % 4;
switch (rotation) {
case 1:
tempValue = box.height / 2 + box.top;
transformations.unshift(
"rotate(90 " + tempValue.toString() + " " + tempValue.toString() + ")"
);
break;
case 2:
transformations.unshift(
"rotate(180 " + (box.width / 2 + box.left).toString() + " " + (box.height / 2 + box.top).toString() + ")"
);
break;
case 3:
tempValue = box.width / 2 + box.left;
transformations.unshift(
"rotate(-90 " + tempValue.toString() + " " + tempValue.toString() + ")"
);
break;
}
if (rotation % 2 === 1) {
if (box.left !== box.top) {
tempValue = box.left;
box.left = box.top;
box.top = tempValue;
}
if (box.width !== box.height) {
tempValue = box.width;
box.width = box.height;
box.height = tempValue;
}
}
if (transformations.length) {
body = '<g transform="' + transformations.join(" ") + '">' + body + "</g>";
}
});
const customisationsWidth = fullCustomisations.width;
const customisationsHeight = fullCustomisations.height;
const boxWidth = box.width;
const boxHeight = box.height;
let width;
let height;
if (customisationsWidth === null) {
height = customisationsHeight === null ? "1em" : customisationsHeight === "auto" ? boxHeight : customisationsHeight;
width = calculateSize(height, boxWidth / boxHeight);
} else {
width = customisationsWidth === "auto" ? boxWidth : customisationsWidth;
height = customisationsHeight === null ? calculateSize(width, boxHeight / boxWidth) : customisationsHeight === "auto" ? boxHeight : customisationsHeight;
}
const attributes = {};
const setAttr = (prop, value) => {
if (!isUnsetKeyword(value)) {
attributes[prop] = value.toString();
}
};
setAttr("width", width);
setAttr("height", height);
attributes.viewBox = box.left.toString() + " " + box.top.toString() + " " + boxWidth.toString() + " " + boxHeight.toString();
return {
attributes,
body
};
}
"IconifyId" + Date.now().toString(16) + (Math.random() * 16777216 | 0).toString(16);
function trimSVG(str) {
return str.replace(/(['"])\s*\n\s*([^>\\/\s])/g, "$1 $2").replace(/(["';{}><])\s*\n\s*/g, "$1").replace(/\s*\n\s*/g, " ").replace(/\s+"/g, '"').replace(/="\s+/g, '="').trim();
}
function add(keyword, colors) {
const type = "rgb";
const r = colors[0];
const length = colors.length;
({
type,
r,
g: length > 1 ? colors[1] : r,
b: length > 2 ? colors[2] : r,
alpha: length > 3 ? colors[3] : 1
});
}
add("silver", [192]);
add("gray", [128]);
add("white", [255]);
add("maroon", [128, 0, 0]);
add("red", [255, 0, 0]);
add("purple", [128, 0]);
add("fuchsia", [255, 0]);
add("green", [0, 128]);
add("lime", [0, 255]);
add("olive", [128, 128, 0]);
add("yellow", [255, 255, 0]);
add("navy", [0, 0, 128]);
add("blue", [0, 0, 255]);
add("teal", [0, 128, 128]);
add("aqua", [0, 255, 255]);
add("aliceblue", [240, 248, 255]);
add("antiquewhite", [250, 235, 215]);
add("aqua", [0, 255, 255]);
add("aquamarine", [127, 255, 212]);
add("azure", [240, 255, 255]);
add("beige", [245, 245, 220]);
add("bisque", [255, 228, 196]);
add("black", [0]);
add("blanchedalmond", [255, 235, 205]);
add("blue", [0, 0, 255]);
add("blueviolet", [138, 43, 226]);
add("brown", [165, 42, 42]);
add("burlywood", [222, 184, 135]);
add("cadetblue", [95, 158, 160]);
add("chartreuse", [127, 255, 0]);
add("chocolate", [210, 105, 30]);
add("coral", [255, 127, 80]);
add("cornflowerblue", [100, 149, 237]);
add("cornsilk", [255, 248, 220]);
add("crimson", [220, 20, 60]);
add("cyan", [0, 255, 255]);
add("darkblue", [0, 0, 139]);
add("darkcyan", [0, 139, 139]);
add("darkgoldenrod", [184, 134, 11]);
add("darkgray", [169]);
add("darkgreen", [0, 100]);
add("darkgrey", [169]);
add("darkkhaki", [189, 183, 107]);
add("darkmagenta", [139, 0]);
add("darkolivegreen", [85, 107, 47]);
add("darkorange", [255, 140, 0]);
add("darkorchid", [153, 50, 204]);
add("darkred", [139, 0, 0]);
add("darksalmon", [233, 150, 122]);
add("darkseagreen", [143, 188]);
add("darkslateblue", [72, 61, 139]);
add("darkslategray", [47, 79, 79]);
add("darkslategrey", [47, 79, 79]);
add("darkturquoise", [0, 206, 209]);
add("darkviolet", [148, 0, 211]);
add("deeppink", [255, 20, 147]);
add("deepskyblue", [0, 191, 255]);
add("dimgray", [105]);
add("dimgrey", [105]);
add("dodgerblue", [30, 144, 255]);
add("firebrick", [178, 34, 34]);
add("floralwhite", [255, 250, 240]);
add("forestgreen", [34, 139]);
add("fuchsia", [255, 0]);
add("gainsboro", [220]);
add("ghostwhite", [248, 248, 255]);
add("gold", [255, 215, 0]);
add("goldenrod", [218, 165, 32]);
add("gray", [128]);
add("green", [0, 128]);
add("greenyellow", [173, 255, 47]);
add("grey", [128]);
add("honeydew", [240, 255]);
add("hotpink", [255, 105, 180]);
add("indianred", [205, 92, 92]);
add("indigo", [75, 0, 130]);
add("ivory", [255, 255, 240]);
add("khaki", [240, 230, 140]);
add("lavender", [230, 230, 250]);
add("lavenderblush", [255, 240, 245]);
add("lawngreen", [124, 252, 0]);
add("lemonchiffon", [255, 250, 205]);
add("lightblue", [173, 216, 230]);
add("lightcoral", [240, 128, 128]);
add("lightcyan", [224, 255, 255]);
add("lightgoldenrodyellow", [250, 250, 210]);
add("lightgray", [211]);
add("lightgreen", [144, 238]);
add("lightgrey", [211]);
add("lightpink", [255, 182, 193]);
add("lightsalmon", [255, 160, 122]);
add("lightseagreen", [32, 178, 170]);
add("lightskyblue", [135, 206, 250]);
add("lightslategray", [119, 136, 153]);
add("lightslategrey", [119, 136, 153]);
add("lightsteelblue", [176, 196, 222]);
add("lightyellow", [255, 255, 224]);
add("lime", [0, 255]);
add("limegreen", [50, 205]);
add("linen", [250, 240, 230]);
add("magenta", [255, 0]);
add("maroon", [128, 0, 0]);
add("mediumaquamarine", [102, 205, 170]);
add("mediumblue", [0, 0, 205]);
add("mediumorchid", [186, 85, 211]);
add("mediumpurple", [147, 112, 219]);
add("mediumseagreen", [60, 179, 113]);
add("mediumslateblue", [123, 104, 238]);
add("mediumspringgreen", [0, 250, 154]);
add("mediumturquoise", [72, 209, 204]);
add("mediumvioletred", [199, 21, 133]);
add("midnightblue", [25, 25, 112]);
add("mintcream", [245, 255, 250]);
add("mistyrose", [255, 228, 225]);
add("moccasin", [255, 228, 181]);
add("navajowhite", [255, 222, 173]);
add("navy", [0, 0, 128]);
add("oldlace", [253, 245, 230]);
add("olive", [128, 128, 0]);
add("olivedrab", [107, 142, 35]);
add("orange", [255, 165, 0]);
add("orangered", [255, 69, 0]);
add("orchid", [218, 112, 214]);
add("palegoldenrod", [238, 232, 170]);
add("palegreen", [152, 251]);
add("paleturquoise", [175, 238, 238]);
add("palevioletred", [219, 112, 147]);
add("papayawhip", [255, 239, 213]);
add("peachpuff", [255, 218, 185]);
add("peru", [205, 133, 63]);
add("pink", [255, 192, 203]);
add("plum", [221, 160]);
add("powderblue", [176, 224, 230]);
add("purple", [128, 0]);
add("rebeccapurple", [102, 51, 153]);
add("red", [255, 0, 0]);
add("rosybrown", [188, 143, 143]);
add("royalblue", [65, 105, 225]);
add("saddlebrown", [139, 69, 19]);
add("salmon", [250, 128, 114]);
add("sandybrown", [244, 164, 96]);
add("seagreen", [46, 139, 87]);
add("seashell", [255, 245, 238]);
add("sienna", [160, 82, 45]);
add("silver", [192]);
add("skyblue", [135, 206, 235]);
add("slateblue", [106, 90, 205]);
add("slategray", [112, 128, 144]);
add("slategrey", [112, 128, 144]);
add("snow", [255, 250, 250]);
add("springgreen", [0, 255, 127]);
add("steelblue", [70, 130, 180]);
add("tan", [210, 180, 140]);
add("teal", [0, 128, 128]);
add("thistle", [216, 191]);
add("tomato", [255, 99, 71]);
add("turquoise", [64, 224, 208]);
add("violet", [238, 130]);
add("wheat", [245, 222, 179]);
add("white", [255]);
add("whitesmoke", [245]);
add("yellow", [255, 255, 0]);
add("yellowgreen", [154, 205, 50]);
const svgWidthRegex = /\swidth\s*=\s*["'](\w+)["']/;
const svgHeightRegex = /\sheight\s*=\s*["'](\w+)["']/;
const svgTagRegex = /<svg\s+/;
function configureSvgSize(svg, props, scale) {
const svgNode = svg.slice(0, svg.indexOf(">"));
const check = (prop, regex) => {
const result = regex.exec(svgNode);
const isSet = result != null;
const propValue = props[prop];
if (!propValue && !isUnsetKeyword(propValue)) {
if (typeof scale === "number") {
if (scale > 0) {
props[prop] = `${scale}em`;
}
} else if (result) {
props[prop] = result[1];
}
}
return isSet;
};
return [check("width", svgWidthRegex), check("height", svgHeightRegex)];
}
async function mergeIconProps(svg, collection, icon, options, propsProvider, afterCustomizations) {
const { scale, addXmlNs = false } = options ?? {};
const { additionalProps = {}, iconCustomizer } = options?.customizations ?? {};
const props = await propsProvider?.() ?? {};
await iconCustomizer?.(collection, icon, props);
Object.keys(additionalProps).forEach((p) => {
const v = additionalProps[p];
if (v !== void 0 && v !== null)
props[p] = v;
});
afterCustomizations?.(props);
const [widthOnSvg, heightOnSvg] = configureSvgSize(svg, props, scale);
if (addXmlNs) {
if (!svg.includes("xmlns=") && !props["xmlns"]) {
props["xmlns"] = "http://www.w3.org/2000/svg";
}
if (!svg.includes("xmlns:xlink=") && svg.includes("xlink:") && !props["xmlns:xlink"]) {
props["xmlns:xlink"] = "http://www.w3.org/1999/xlink";
}
}
const propsToAdd = Object.keys(props).map(
(p) => p === "width" && widthOnSvg || p === "height" && heightOnSvg ? null : `${p}="${props[p]}"`
).filter((p) => p != null);
if (propsToAdd.length) {
svg = svg.replace(svgTagRegex, `<svg ${propsToAdd.join(" ")} `);
}
if (options) {
const { defaultStyle, defaultClass } = options;
if (defaultClass && !svg.includes("class=")) {
svg = svg.replace(svgTagRegex, `<svg class="${defaultClass}" `);
}
if (defaultStyle && !svg.includes("style=")) {
svg = svg.replace(svgTagRegex, `<svg style="${defaultStyle}" `);
}
}
const usedProps = options?.usedProps;
if (usedProps) {
Object.keys(additionalProps).forEach((p) => {
const v = props[p];
if (v !== void 0 && v !== null)
usedProps[p] = v;
});
if (typeof props.width !== "undefined" && props.width !== null) {
usedProps.width = props.width;
}
if (typeof props.height !== "undefined" && props.height !== null) {
usedProps.height = props.height;
}
}
return svg;
}
async function getCustomIcon(custom, collection, icon, options) {
let result;
if (typeof custom === "function") {
result = await custom(icon);
} else {
const inline = custom[icon];
result = typeof inline === "function" ? await inline() : inline;
}
if (result) {
const cleanupIdx = result.indexOf("<svg");
if (cleanupIdx > 0)
result = result.slice(cleanupIdx);
const { transform } = options?.customizations ?? {};
result = typeof transform === "function" ? await transform(result, collection, icon) : result;
if (!result.startsWith("<svg")) {
console.warn(
`Custom icon "${icon}" in "${collection}" is not a valid SVG`
);
return result;
}
return await mergeIconProps(
options?.customizations?.trimCustomSvg === true ? trimSVG(result) : result,
collection,
icon,
options,
void 0
);
}
}
async function searchForIcon(iconSet, collection, ids, options) {
let iconData;
const { customize } = options?.customizations ?? {};
for (const id of ids) {
iconData = getIconData(iconSet, id);
if (iconData) {
let defaultCustomizations = { ...defaultIconCustomisations };
if (typeof customize === "function")
defaultCustomizations = customize(defaultCustomizations);
const {
attributes: { width, height, ...restAttributes },
body
} = iconToSVG(iconData, defaultCustomizations);
const scale = options?.scale;
return await mergeIconProps(
// DON'T remove space on <svg >
`<svg >${body}</svg>`,
collection,
id,
options,
() => {
return { ...restAttributes };
},
(props) => {
const check = (prop, defaultValue) => {
const propValue = props[prop];
let value;
if (!isUnsetKeyword(propValue)) {
if (propValue) {
return;
}
if (typeof scale === "number") {
if (scale) {
value = `${scale}em`;
}
} else {
value = defaultValue;
}
}
if (!value) {
delete props[prop];
} else {
props[prop] = value;
}
};
check("width", width);
check("height", height);
}
);
}
}
}
const loadIcon = async (collection, icon, options) => {
const custom = options?.customCollections?.[collection];
if (custom) {
if (typeof custom === "function") {
const result = await custom(icon);
if (result) {
if (typeof result === "string") {
return await getCustomIcon(
() => result,
collection,
icon,
options
);
}
if ("icons" in result) {
const ids = [
icon,
icon.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(),
icon.replace(/([a-z])(\d+)/g, "$1-$2")
];
return await searchForIcon(
result,
collection,
ids,
options
);
}
}
} else {
return await getCustomIcon(custom, collection, icon, options);
}
}
return void 0;
};
var collections = [
"academicons",
"akar-icons",
"ant-design",
"arcticons",
"basil",
"bi",
"bpmn",
"brandico",
"bx",
"bxl",
"bxs",
"bytesize",
"carbon",
"charm",
"ci",
"cib",
"cif",
"cil",
"circle-flags",
"circum",
"clarity",
"codicon",
"covid",
"cryptocurrency-color",
"cryptocurrency",
"dashicons",
"devicon-line",
"devicon-original",
"devicon-plain",
"devicon",
"ei",
"el",
"emblemicons",
"emojione-monotone",
"emojione-v1",
"emojione",
"entypo-social",
"entypo",
"eos-icons",
"ep",
"et",
"eva",
"fa-brands",
"fa-regular",
"fa-solid",
"fa",
"fa6-brands",
"fa6-regular",
"fa6-solid",
"fad",
"fe",
"feather",
"file-icons",
"flag",
"flagpack",
"flat-color-icons",
"flat-ui",
"fluent-emoji-flat",
"fluent-emoji-high-contrast",
"fluent-emoji",
"fluent-mdl2",
"fluent",
"fontelico",
"fontisto",
"foundation",
"fxemoji",
"gala",
"game-icons",
"geo",
"gg",
"gis",
"gridicons",
"grommet-icons",
"guidance",
"healthicons",
"heroicons-outline",
"heroicons-solid",
"heroicons",
"humbleicons",
"ic",
"icomoon-free",
"icon-park-outline",
"icon-park-solid",
"icon-park-twotone",
"icon-park",
"iconamoon",
"iconoir",
"icons8",
"il",
"ion",
"iwwa",
"jam",
"la",
"line-md",
"logos",
"ls",
"lucide",
"majesticons",
"maki",
"map",
"material-symbols",
"mdi-light",
"mdi",
"medical-icon",
"memory",
"mi",
"mingcute",
"mono-icons",
"nimbus",
"nonicons",
"noto-v1",
"noto",
"octicon",
"oi",
"ooui",
"openmoji",
"pajamas",
"pepicons-pop",
"pepicons-print",
"pepicons",
"ph",
"pixelarticons",
"prime",
"ps",
"quill",
"radix-icons",
"raphael",
"ri",
"si-glyph",
"simple-icons",
"simple-line-icons",
"skill-icons",
"solar",
"streamline-emojis",
"streamline",
"subway",
"svg-spinners",
"system-uicons",
"tabler",
"teenyicons",
"topcoat",
"twemoji",
"typcn",
"uil",
"uim",
"uis",
"uit",
"uiw",
"vaadin",
"vs",
"vscode-icons",
"websymbol",
"whh",
"wi",
"wpf",
"zmdi",
"zondicons"
];
function createCDNLoader(cdnBase) {
const cache = /* @__PURE__ */ new Map();
function fetchCollection(name) {
if (!collections.includes(name))
return void 0;
if (!cache.has(name))
cache.set(name, ofetch.$fetch(`${cdnBase}@iconify-json/${name}/icons.json`));
return cache.get(name);
}
return async (collection, icon, options) => {
let result = await loadIcon(collection, icon, options);
if (result)
return result;
const iconSet = await fetchCollection(collection);
if (iconSet) {
const ids = [
icon,
icon.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(),
icon.replace(/([a-z])(\d+)/g, "$1-$2")
];
result = await searchForIcon(iconSet, collection, ids, options);
}
return result;
};
}
exports.createCDNLoader = createCDNLoader;
exports.loadIcon = loadIcon;