@donglegeyu/company-ui 2.3.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -37,12 +37,14 @@ var import_antd = require("antd");
37
37
  var import_IconGallery = __toESM(require("../IconGallery"));
38
38
  var import_IconParkGallery = __toESM(require("../IconParkGallery"));
39
39
  var import_icons = require("../SvgIcon/icons");
40
+ var import_useIconPark = require("../IconParkGallery/useIconPark");
40
41
  var import_theme = require("../../theme");
41
42
  var import_index = require("./index.scss");
42
43
  var import_jsx_runtime = require("react/jsx-runtime");
43
44
  function IconExplorer() {
44
45
  const { token } = import_antd.theme.useToken();
45
46
  const [activeKey, setActiveKey] = (0, import_react.useState)("curated");
47
+ const { data } = (0, import_useIconPark.useIconPark)();
46
48
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_antd.ConfigProvider, { theme: import_theme.companyTheme, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "company-icon-explorer", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
47
49
  import_antd.ConfigProvider,
48
50
  {
@@ -70,7 +72,7 @@ function IconExplorer() {
70
72
  },
71
73
  {
72
74
  key: "full",
73
- label: "图标库 Full (2662)",
75
+ label: `图标库 Full${data ? ` (${data.count})` : ""}`,
74
76
  forceRender: true,
75
77
  children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { id: "icon-full", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_IconParkGallery.default, {}) })
76
78
  }
@@ -128,7 +128,7 @@ function IconParkGallery() {
128
128
  if (loading) {
129
129
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "company-iconpark-gallery-loading", children: [
130
130
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: { color: token.colorPrimary, fontSize: 24, marginBottom: 12 }, children: "○" }),
131
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: { color: token.colorTextSecondary, fontSize: 14 }, children: "加载 2658 个图标中..." })
131
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: { color: token.colorTextSecondary, fontSize: 14 }, children: "加载图标数据中..." })
132
132
  ] });
133
133
  }
134
134
  if (error || !data) {
@@ -147,12 +147,20 @@ function IconParkGallery() {
147
147
  style: { maxWidth: 360 }
148
148
  }
149
149
  ),
150
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
150
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
151
151
  "span",
152
152
  {
153
153
  className: "company-iconpark-gallery-hint",
154
154
  style: { color: token.colorTextQuaternary },
155
- children: "图标库基于 IconPark,运行时按需加载 sprite(约 2MB,浏览器缓存),适合图标选型与 IconSelect 场景。"
155
+ children: [
156
+ "图标库基于 IconPark,业务环境使用",
157
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("code", { children: [
158
+ "<SvgIcon name=",
159
+ '"xxx"',
160
+ " />"
161
+ ] }),
162
+ "渲染内置精选;文档站图标库页通过 sprite 加载全量图标。"
163
+ ]
156
164
  }
157
165
  )
158
166
  ] }),
@@ -23,56 +23,54 @@ __export(useIconPark_exports, {
23
23
  });
24
24
  module.exports = __toCommonJS(useIconPark_exports);
25
25
  var import_react = require("react");
26
+ var cachedData = null;
26
27
  var spriteLoadPromise = null;
28
+ async function fetchIconParkData(metaUrl) {
29
+ if (cachedData)
30
+ return cachedData;
31
+ const res = await fetch(metaUrl);
32
+ if (!res.ok) {
33
+ throw new Error(`加载图标元数据失败: ${res.status}`);
34
+ }
35
+ cachedData = await res.json();
36
+ return cachedData;
37
+ }
38
+ function injectSprite(spriteUrl) {
39
+ if (document.getElementById("company-iconpark-sprite"))
40
+ return Promise.resolve(true);
41
+ if (spriteLoadPromise)
42
+ return spriteLoadPromise;
43
+ spriteLoadPromise = new Promise((resolvePromise) => {
44
+ try {
45
+ fetch(spriteUrl).then((res) => res.text()).then((text) => {
46
+ const div = document.createElement("div");
47
+ div.id = "company-iconpark-sprite";
48
+ div.style.display = "none";
49
+ div.innerHTML = text;
50
+ document.body.insertBefore(div, document.body.firstChild);
51
+ resolvePromise(true);
52
+ }).catch(() => resolvePromise(false));
53
+ } catch {
54
+ resolvePromise(false);
55
+ }
56
+ });
57
+ return spriteLoadPromise;
58
+ }
27
59
  function useIconPark(spriteUrl = "/iconpark-sprite.svg", metaUrl = "/iconpark-meta.json") {
28
60
  const [data, setData] = (0, import_react.useState)(null);
29
61
  const [loading, setLoading] = (0, import_react.useState)(true);
30
62
  const [error, setError] = (0, import_react.useState)(null);
31
- const injectSprite = (0, import_react.useCallback)(async () => {
32
- if (document.getElementById("company-iconpark-sprite"))
33
- return true;
34
- if (spriteLoadPromise)
35
- return spriteLoadPromise;
36
- spriteLoadPromise = new Promise((resolvePromise) => {
37
- try {
38
- fetch(spriteUrl).then((res) => res.text()).then((text) => {
39
- const div = document.createElement("div");
40
- div.id = "company-iconpark-sprite";
41
- div.style.display = "none";
42
- div.innerHTML = text;
43
- document.body.insertBefore(div, document.body.firstChild);
44
- resolvePromise(true);
45
- }).catch(() => resolvePromise(false));
46
- } catch {
47
- resolvePromise(false);
48
- }
49
- });
50
- return spriteLoadPromise;
51
- }, [spriteUrl]);
52
63
  (0, import_react.useEffect)(() => {
53
64
  let cancelled = false;
54
- setLoading(true);
55
- Promise.all([
56
- injectSprite(),
57
- fetch(metaUrl).then((res) => res.json()).then((json) => {
58
- if (cancelled)
59
- return null;
60
- return json;
61
- })
62
- ]).then(([ok, json]) => {
65
+ Promise.all([injectSprite(spriteUrl), fetchIconParkData(metaUrl).catch(() => null)]).then(([spriteOk, json]) => {
63
66
  if (cancelled)
64
67
  return;
65
- if (!ok) {
66
- setError("图标 sprite 加载失败");
67
- }
68
68
  if (json) {
69
69
  setData(json);
70
+ setError(spriteOk ? null : "图标 sprite 加载失败");
70
71
  } else {
71
72
  setError("图标元数据加载失败");
72
73
  }
73
- }).catch((e) => {
74
- if (!cancelled)
75
- setError(String(e));
76
74
  }).finally(() => {
77
75
  if (!cancelled)
78
76
  setLoading(false);
@@ -80,7 +78,7 @@ function useIconPark(spriteUrl = "/iconpark-sprite.svg", metaUrl = "/iconpark-me
80
78
  return () => {
81
79
  cancelled = true;
82
80
  };
83
- }, [injectSprite, metaUrl]);
81
+ }, [spriteUrl, metaUrl]);
84
82
  return { data, loading, error };
85
83
  }
86
84
  // Annotate the CommonJS export names for ESM import in node: