完成世界书、骰子、apiconfig页面处理

This commit is contained in:
2026-04-30 01:35:10 +08:00
parent a3e3711b2b
commit ba9b925c32
4602 changed files with 785225 additions and 23 deletions

View File

@@ -0,0 +1,6 @@
import { CustomIconLoader, IconifyLoaderOptions, InlineCollection } from "./types.js";
/**
* Get custom icon from inline collection or using loader
*/
declare function getCustomIcon(custom: CustomIconLoader | InlineCollection, collection: string, icon: string, options?: IconifyLoaderOptions): Promise<string | undefined>;
export { getCustomIcon };

View File

@@ -0,0 +1,32 @@
import { trimSVG } from "../svg/trim.js";
import { mergeIconProps } from "./utils.js";
/**
* Get custom icon from inline collection or using loader
*/
async function getCustomIcon(custom, collection, icon, options) {
let result;
try {
if (typeof custom === "function") result = await custom(icon);
else {
const inline = custom[icon];
result = typeof inline === "function" ? await inline() : inline;
}
} catch (err) {
console.warn(`Failed to load custom icon "${icon}" in "${collection}":`, err);
return;
}
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);
}
}
export { getCustomIcon };

View File

@@ -0,0 +1,28 @@
import { warnOnce } from "./warn.js";
import { installPackage } from "@antfu/install-pkg";
import { styleText } from "node:util";
let pending;
const tasks = {};
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function tryInstallPkg(name, autoInstall) {
if (pending) await pending;
if (!tasks[name]) {
console.log(styleText("cyan", `Installing ${name}...`));
if (typeof autoInstall === "function") tasks[name] = pending = autoInstall(name).then(() => sleep(300)).finally(() => {
pending = void 0;
});
else tasks[name] = pending = installPackage(name, {
dev: true,
preferOffline: true
}).then(() => sleep(300)).catch((e) => {
warnOnce(`Failed to install ${name}`);
console.error(e);
}).finally(() => {
pending = void 0;
});
}
return tasks[name];
}
export { tryInstallPkg };

View File

@@ -0,0 +1,3 @@
import { UniversalIconLoader } from "./types.js";
declare const loadIcon: UniversalIconLoader;
export { loadIcon };

View File

@@ -0,0 +1,28 @@
import { getCustomIcon } from "./custom.js";
import { searchForIcon } from "./modern.js";
const loadIcon = async (collection, icon, options) => {
const custom = options?.customCollections?.[collection];
if (custom) if (typeof custom === "function") {
let result;
try {
result = await custom(icon);
} catch (err) {
console.warn(`Failed to load custom icon "${icon}" in "${collection}":`, err);
return;
}
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);
};
export { loadIcon };

View File

@@ -0,0 +1,42 @@
import { defaultIconCustomisations } from "../customisations/defaults.js";
import { getIconData } from "../icon-set/get-icon.js";
import { calculateSize } from "../svg/size.js";
import { iconToSVG, isUnsetKeyword } from "../svg/build.js";
import { mergeIconProps } from "./utils.js";
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") {
iconData = Object.assign({}, iconData);
defaultCustomizations = customize(defaultCustomizations, iconData, `${collection}:${id}`) ?? defaultCustomizations;
}
const { attributes: { width, height, ...restAttributes }, body } = iconToSVG(iconData, defaultCustomizations);
const scale = options?.scale;
return await mergeIconProps(`<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 = calculateSize(defaultValue ?? "1em", scale);
} else value = defaultValue;
}
if (!value) delete props[prop];
else props[prop] = value;
};
check("width", width);
check("height", height);
});
}
}
}
export { searchForIcon };

View File

@@ -0,0 +1,12 @@
import { resolvePath } from "mlly";
/**
* Resolve path to package
*/
async function resolvePathAsync(packageName, cwd) {
try {
return await resolvePath(packageName, { url: cwd });
} catch {}
}
export { resolvePathAsync };

View File

@@ -0,0 +1,63 @@
import { calculateSize } from "../svg/size.js";
import { isUnsetKeyword } from "../svg/build.js";
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] = calculateSize(result?.[1] ?? "1em", scale);
} 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;
}
function getPossibleIconNames(icon) {
return [
icon,
icon.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(),
icon.replace(/([a-z])(\d+)/g, "$1-$2")
];
}
export { getPossibleIconNames, mergeIconProps };

View File

@@ -0,0 +1,2 @@
declare function warnOnce(msg: string): void;
export { warnOnce };