iconplease 0.1.0 → 0.2.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.
- package/README.md +14 -0
- package/dist/config.d.ts +2 -0
- package/dist/config.js +6 -0
- package/dist/iconplease.min.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/store.d.ts +4 -0
- package/dist/store.js +124 -3
- package/dist/types.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -55,6 +55,20 @@ const svg = renderSVG("heart", { size: 32 }); // string, or undefined if unkno
|
|
|
55
55
|
const names = iconNames();
|
|
56
56
|
```
|
|
57
57
|
|
|
58
|
+
## Your own set
|
|
59
|
+
|
|
60
|
+
Create a publishable key for a set in your iconplease account (Account → API keys), then:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { init } from "iconplease";
|
|
64
|
+
init({ apiKey: "ip_pk_…" });
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
`<Iconplease icon="heart" />` now uses your set's version of `heart` if it has one, and the public
|
|
68
|
+
catalogue otherwise. The set loads in one request, is cached in the browser and re-checked at most
|
|
69
|
+
once an hour. Limit a key to your own sites when you create it; a page on any other site gets a
|
|
70
|
+
console warning and the public icons instead. Publishable keys are safe to ship in front-end code.
|
|
71
|
+
|
|
58
72
|
## How loading works
|
|
59
73
|
|
|
60
74
|
- The SDK downloads a small **index** of icon names (about 1.5 KB for the whole catalogue), then
|
package/dist/config.d.ts
CHANGED
|
@@ -3,3 +3,5 @@ export declare const init: (next?: Config) => Config;
|
|
|
3
3
|
export declare const getConfig: () => Config;
|
|
4
4
|
export declare const cdnBase: () => string;
|
|
5
5
|
export declare const revalidateMs: () => number;
|
|
6
|
+
export declare const apiBase: () => string;
|
|
7
|
+
export declare const apiKey: () => string | undefined;
|
package/dist/config.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const DEFAULTS = {
|
|
2
2
|
cdnUrl: "https://cdn.iconplease.com",
|
|
3
|
+
apiUrl: "https://api.iconplease.com",
|
|
3
4
|
revalidate: 3600,
|
|
4
5
|
};
|
|
5
6
|
let config = { ...DEFAULTS };
|
|
@@ -7,9 +8,14 @@ export const init = (next = {}) => {
|
|
|
7
8
|
if (next.apiKey && next.apiKey.startsWith("ip_sk_")) {
|
|
8
9
|
throw new Error("iconplease: that is a secret key. init() runs in the browser, where the key is readable by anyone — use the publishable key (ip_pk_…).");
|
|
9
10
|
}
|
|
11
|
+
if (next.apiKey && !/^ip_pk_[A-Za-z0-9]{32}$/.test(next.apiKey)) {
|
|
12
|
+
console.warn("iconplease: that does not look like a publishable key (ip_pk_ followed by 32 characters). Set icons will not load.");
|
|
13
|
+
}
|
|
10
14
|
config = { ...DEFAULTS, ...next };
|
|
11
15
|
return config;
|
|
12
16
|
};
|
|
13
17
|
export const getConfig = () => config;
|
|
14
18
|
export const cdnBase = () => (config.cdnUrl ?? DEFAULTS.cdnUrl).replace(/\/+$/, "");
|
|
15
19
|
export const revalidateMs = () => Math.max(0, config.revalidate ?? DEFAULTS.revalidate) * 1000;
|
|
20
|
+
export const apiBase = () => (config.apiUrl ?? DEFAULTS.apiUrl).replace(/\/+$/, "");
|
|
21
|
+
export const apiKey = () => config.apiKey || undefined;
|
package/dist/iconplease.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";(()=>{var
|
|
1
|
+
"use strict";(()=>{var w={cdnUrl:"https://cdn.iconplease.com",apiUrl:"https://api.iconplease.com",revalidate:3600},g={...w},C=(e={})=>{if(e.apiKey&&e.apiKey.startsWith("ip_sk_"))throw new Error("iconplease: that is a secret key. init() runs in the browser, where the key is readable by anyone \u2014 use the publishable key (ip_pk_\u2026).");return e.apiKey&&!/^ip_pk_[A-Za-z0-9]{32}$/.test(e.apiKey)&&console.warn("iconplease: that does not look like a publishable key (ip_pk_ followed by 32 characters). Set icons will not load."),g={...w,...e},g};var P=()=>{var e;return((e=g.cdnUrl)!=null?e:w.cdnUrl).replace(/\/+$/,"")},j=()=>{var e;return Math.max(0,(e=g.revalidate)!=null?e:w.revalidate)*1e3},X=()=>{var e;return((e=g.apiUrl)!=null?e:w.apiUrl).replace(/\/+$/,"")},x=()=>g.apiKey||void 0;var me=/<\s*(script|foreignObject|iframe|object|embed|style)\b[\s\S]*?(<\s*\/\s*\1\s*>|\/>)/gi,he=/\s+on[a-z]+\s*=\s*("[^"]*"|'[^']*'|[^\s>]+)/gi,ye=/\s+(?:xlink:)?href\s*=\s*("\s*javascript:[^"]*"|'\s*javascript:[^']*')/gi,be=/^[a-z][a-z0-9-]*$/,we=/^[^"<>]*$/,xe=e=>e.replace(me,"").replace(he,"").replace(ye,""),D=e=>{var o;if(!e||typeof e!="object")return;let t=e;if(typeof t.body!="string"||typeof t.viewBox!="string"||!/^[0-9.\s-]+$/.test(t.viewBox))return;let n={};for(let[s,r]of Object.entries((o=t.attrs)!=null?o:{}))be.test(s)&&!s.startsWith("on")&&typeof r=="string"&&we.test(r)&&(n[s]=r);return{viewBox:t.viewBox,attrs:n,body:xe(t.body)}};var te="iconplease:index:v2",ne="iconplease:icons:v2",oe="iconplease:set:v1",ve=1e3,A=/^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$/,c={status:"idle",revision:0},O=0,I,Y=!1,Z=!1,M,m=new Map,u=new Map,T,k=0,h=!1,v,_=!1,R=new Map,z=new Set,S=()=>{try{return typeof localStorage=="undefined"?void 0:localStorage}catch{return}},p=(e={})=>{c={...c,...e,revision:c.revision+1};for(let t of z)t()},E=(e,t)=>`${e}@${t}`,se=e=>{if(!e||typeof e!="object")return;let t=e;if(t.format!==2||typeof t.hash!="string"||!t.icons||typeof t.icons!="object")return;let n=Object.entries(t.icons).filter(([o,s])=>A.test(o)&&Number.isInteger(s)&&s>0);return{format:2,hash:t.hash,icons:Object.fromEntries(n)}},L=e=>e.slice(0,14),ke=()=>{var n,o,s;let e=x(),t=e?L(e):void 0;if(t!==T&&(T=t,u.clear(),k=0,h=!1,v=void 0,_=!1,!!t))try{let r=JSON.parse((o=(n=S())==null?void 0:n.getItem(oe))!=null?o:"null");if(!r||r.tag!==t)return;for(let[i,f]of Object.entries((s=r.icons)!=null?s:{})){let a=A.test(i)&&typeof(f==null?void 0:f.svg)=="string"?K(f.svg):void 0;a&&u.set(i,a)}k=typeof r.checkedAt=="number"?r.checkedAt:0,h=!0}catch{return}},y=()=>{var t,n;if(ke(),Y)return;Y=!0;let e=S();if(e){try{let o=JSON.parse((t=e.getItem(te))!=null?t:"null"),s=se(o==null?void 0:o.index);s&&(c={status:"ready",index:s,revision:c.revision},O=typeof(o==null?void 0:o.checkedAt)=="number"?o.checkedAt:0)}catch{return}try{let o=JSON.parse((n=e.getItem(ne))!=null?n:"{}");for(let[s,r]of Object.entries(o)){let i=D(r);i&&m.set(s,i)}}catch{return}}},Q=()=>{var e;try{(e=S())==null||e.setItem(te,JSON.stringify({index:c.index,checkedAt:O}))}catch{return}},re=()=>{M||(M=setTimeout(()=>{var n,o,s;M=void 0;let e=(o=(n=c.index)==null?void 0:n.icons)!=null?o:{},t=[...m.entries()].filter(([r])=>{let i=r.lastIndexOf("@");return e[r.slice(0,i)]===Number(r.slice(i+1))});try{(s=S())==null||s.setItem(ne,JSON.stringify(Object.fromEntries(t.slice(-ve))))}catch{return}},0))},ee=async e=>{let t=await fetch(e);if(!t.ok)throw new Error(`${t.status} for ${e}`);return t.json()},ie=e=>{Z||(Z=!0,console.warn("iconplease: could not reach the icon CDN.",e))},Se=async()=>{var s;let e=P(),t=await ee(`${e}/catalogue/latest.json`);if((t==null?void 0:t.format)!==2||typeof t.hash!="string"||typeof t.index!="string")throw new Error("iconplease: the catalogue pointer is malformed");if(O=Date.now(),((s=c.index)==null?void 0:s.hash)===t.hash){Q();return}let n=t.index.replace(/^\/+/,"");if(!/^catalogue\/index-[a-z0-9]+\.json$/.test(n))throw new Error("iconplease: unexpected index location");let o=se(await ee(`${e}/${n}`));if(!o)throw new Error("iconplease: the icon index is malformed");p({status:"ready",index:o}),Q(),re()},B=(e={})=>{if(y(),c.index&&Date.now()-O<j()&&!e.force)return Promise.resolve();if(I)return I;let n=Se().catch(o=>{c.index||p({status:"error"}),ie(o)}).finally(()=>{I=void 0});return I=n,!c.index&&c.status!=="loading"&&p({status:"loading"}),n},K=e=>{let t=/<svg\b([^>]*)>([\s\S]*)<\/svg>\s*$/.exec(e.trim());if(!t)return;let n={},o="0 0 24 24";for(let[,s,r]of t[1].matchAll(/([a-zA-Z:-]+)="([^"]*)"/g))s==="viewBox"?o=r:["xmlns","width","height","aria-hidden","role","class","style"].includes(s)||(n[s]=r);return D({viewBox:o,attrs:n,body:t[2]})},Ie=(e,t)=>{let n=E(e,t),o=R.get(n);if(o)return o;let s=fetch(`${P()}/v${t}/${e}.svg`).then(async r=>{if(!r.ok)throw new Error(`${r.status} for ${e}`);let i=K(await r.text());if(!i)throw new Error(`iconplease: "${e}" is not a valid SVG`);m.set(n,i),re(),p()}).catch(ie).finally(()=>{R.delete(n)});return R.set(n,s),s},Ee=async e=>{var r,i;let t=await fetch(`${X()}/sdk/set?key=${encodeURIComponent(e)}`),n=await t.json().catch(()=>({}));if(!t.ok)throw new Error(n.error||`HTTP ${t.status}`);let o=new Map,s={};for(let[f,a]of Object.entries((r=n.icons)!=null?r:{})){if(!A.test(f)||typeof(a==null?void 0:a.svg)!="string")continue;let l=K(a.svg);l&&(o.set(f,l),s[f]={version:Number(a.version)||1,svg:a.svg})}if(L(e)===T){u.clear();for(let[f,a]of o)u.set(f,a);k=Date.now(),h=!0;try{(i=S())==null||i.setItem(oe,JSON.stringify({tag:L(e),checkedAt:k,icons:s}))}catch{p();return}p()}},U=(e={})=>{y();let t=x();if(!t||h&&Date.now()-k<j()&&!e.force)return Promise.resolve();if(v)return v;let o=Ee(t).catch(s=>{h=!0,_||(_=!0,console.warn(`iconplease: could not load your icon set \u2014 ${s instanceof Error?s.message:String(s)} Falling back to public icons.`)),p()}).finally(()=>{v=void 0});return v=o,o},F=async e=>{var o,s;if(!A.test(e))return;if(y(),x()){if(u.has(e)){U();return}if(await U(),u.has(e))return}let t=(o=c.index)==null?void 0:o.icons[e];if(t&&m.has(E(e,t))){B();return}await B();let n=(s=c.index)==null?void 0:s.icons[e];n&&!m.has(E(e,n))&&await Ie(e,n)},J=async(e={})=>{let{icons:t=[],force:n=!1}=Array.isArray(e)?{icons:e}:e;await Promise.all([B({force:n}),U({force:n})]),await Promise.all(t.map(F))},ae=e=>(y(),z.add(e),()=>z.delete(e));var $=e=>{var o;y();let t=u.get(e);if(t)return t;let n=(o=c.index)==null?void 0:o.icons[e];return n?m.get(E(e,n)):void 0},ce=e=>(y(),u.has(e)||x()&&!h?!1:c.status==="ready"&&!!c.index&&!(e in c.index.icons));var Ae=/^[#a-zA-Z0-9(),.%\s-]{1,64}$/,Oe=/[<>"&]/g,$e={"<":"<",">":">",'"':""","&":"&"},Ne=e=>e.replace(Oe,t=>$e[t]),de=e=>String(Number(e.toFixed(3))),Ce=(e,t)=>e.replace(/\b(fill|stroke)="currentColor"/g,`$1="${t}"`),Pe=(e,t)=>e.replace(/\bstroke-width="([0-9]*\.?[0-9]+)"/g,(n,o)=>`stroke-width="${de(Number(o)*t)}"`),q=(e,t={})=>{var G;let{size:n=24,color:o,strokeScale:s,motion:r,label:i,className:f}=t,a=o&&Ae.test(o)?o:void 0,l=typeof s=="number"&&Number.isFinite(s)&&s>0&&s!==1?s:void 0,d={...(G=e.attrs)!=null?G:{}};if(a)for(let W of["fill","stroke"])d[W]==="currentColor"&&(d[W]=a);l&&d["stroke-width"]&&(d["stroke-width"]=de(Number(d["stroke-width"])*l));let b=e.body;return a&&(b=Ce(b,a)),l&&(b=Pe(b,l)),d.width=String(n),d.height=String(n),d.viewBox=e.viewBox,r&&r!=="none"&&(d["data-motion"]=r),i?(d.role="img",d["aria-label"]=i):d["aria-hidden"]="true",f&&(d.class=f),{attrs:d,body:b}},H=({attrs:e,body:t})=>`<svg xmlns="http://www.w3.org/2000/svg" ${Object.entries(e).map(([o,s])=>`${o}="${Ne(s)}"`).join(" ")}>${t}</svg>`,fe=(e,t={})=>{let n=typeof e=="string"?$(e):e;return n?H(q(n,t)):void 0};var le="[data-icon], i.ip[class*='ip-']",je=new Set(["none","wiggle","pop","bounce","draw"]),ue=new Set,De=e=>{let t=e.getAttribute("data-icon");if(t)return t;for(let n of Array.from(e.classList))if(n.startsWith("ip-")&&n.length>3)return n.slice(3)},pe=e=>{if(e===null)return;let t=Number(e);return Number.isFinite(t)&&t>0?t:void 0},V=e=>{var f,a,l,d;let t=De(e);if(!t)return;let n=$(t),o=e.getAttribute("data-motion"),s=o&&je.has(o)?o:void 0,r={size:(f=pe(e.getAttribute("data-size")))!=null?f:24,color:(a=e.getAttribute("data-color"))!=null?a:void 0,strokeScale:pe(e.getAttribute("data-stroke")),motion:s,label:(l=e.getAttribute("data-label"))!=null?l:void 0},i=JSON.stringify([t,r,n?"1":"0"]);if(e.getAttribute("data-ip-rendered")!==i){if(!n){ce(t)?ue.has(t)||(ue.add(t),console.warn(`iconplease: no icon named "${t}".`)):F(t);return}e.innerHTML=H(q(n,r)),e.setAttribute("data-ip-rendered",i),e.tagName==="I"&&((d=e.style).display||(d.display="inline-flex"))}},N=(e=document)=>{e instanceof Element&&e.matches(le)&&V(e),e.querySelectorAll(le).forEach(V)},ge=()=>{let e=window.iconpleaseConfig;e&&C(e),ae(()=>N()),N(),J(),new MutationObserver(t=>{for(let n of t)n.type==="attributes"&&n.target instanceof Element&&V(n.target),n.addedNodes.forEach(o=>{o instanceof Element&&N(o)})}).observe(document.documentElement,{childList:!0,subtree:!0,attributes:!0,attributeFilter:["data-icon","data-size","data-color","data-stroke","data-motion","data-label","class"]})};window.iconplease={init:C,load:J,renderSVG:fe,refresh:()=>N()};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",ge,{once:!0}):ge();})();
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { init, getConfig } from "./config.js";
|
|
2
|
-
export { load, requestIcon, subscribe, getIcon, getStatus, iconNames, isUnknown } from "./store.js";
|
|
2
|
+
export { load, loadSet, requestIcon, subscribe, getIcon, getStatus, iconNames, setIconNames, isUnknown } from "./store.js";
|
|
3
3
|
export { renderSVG, prepare } from "./render.js";
|
|
4
4
|
export type { RenderOptions, Prepared } from "./render.js";
|
|
5
5
|
export type { LoadOptions } from "./store.js";
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { init, getConfig } from "./config.js";
|
|
2
|
-
export { load, requestIcon, subscribe, getIcon, getStatus, iconNames, isUnknown } from "./store.js";
|
|
2
|
+
export { load, loadSet, requestIcon, subscribe, getIcon, getStatus, iconNames, setIconNames, isUnknown } from "./store.js";
|
|
3
3
|
export { renderSVG, prepare } from "./render.js";
|
package/dist/store.d.ts
CHANGED
|
@@ -8,6 +8,9 @@ export declare const loadIndex: (options?: {
|
|
|
8
8
|
force?: boolean;
|
|
9
9
|
}) => Promise<void>;
|
|
10
10
|
export declare const parseSVG: (text: string) => IconData | undefined;
|
|
11
|
+
export declare const loadSet: (options?: {
|
|
12
|
+
force?: boolean;
|
|
13
|
+
}) => Promise<void>;
|
|
11
14
|
export declare const requestIcon: (name: string) => Promise<void>;
|
|
12
15
|
export type LoadOptions = {
|
|
13
16
|
icons?: string[];
|
|
@@ -19,5 +22,6 @@ export declare const getSnapshot: () => State;
|
|
|
19
22
|
export declare const getStatus: () => Status;
|
|
20
23
|
export declare const getIcon: (name: string) => IconData | undefined;
|
|
21
24
|
export declare const isUnknown: (name: string) => boolean;
|
|
25
|
+
export declare const setIconNames: () => string[];
|
|
22
26
|
export declare const iconNames: () => string[];
|
|
23
27
|
export declare const resetForTests: () => void;
|
package/dist/store.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { cdnBase, revalidateMs } from "./config.js";
|
|
1
|
+
import { apiBase, apiKey, cdnBase, revalidateMs } from "./config.js";
|
|
2
2
|
import { cleanIcon } from "./sanitize.js";
|
|
3
3
|
const INDEX_KEY = "iconplease:index:v2";
|
|
4
4
|
const ICONS_KEY = "iconplease:icons:v2";
|
|
5
|
+
const SET_KEY = "iconplease:set:v1";
|
|
5
6
|
const MAX_CACHED_ICONS = 1000;
|
|
6
7
|
const NAME = /^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$/;
|
|
7
8
|
let state = { status: "idle", revision: 0 };
|
|
@@ -11,6 +12,12 @@ let hydrated = false;
|
|
|
11
12
|
let warnedOffline = false;
|
|
12
13
|
let persistTimer;
|
|
13
14
|
const icons = new Map();
|
|
15
|
+
const setIcons = new Map();
|
|
16
|
+
let setFor;
|
|
17
|
+
let setCheckedAt = 0;
|
|
18
|
+
let setLoaded = false;
|
|
19
|
+
let setInflight;
|
|
20
|
+
let warnedSet = false;
|
|
14
21
|
const iconInflight = new Map();
|
|
15
22
|
const listeners = new Set();
|
|
16
23
|
const storage = () => {
|
|
@@ -37,7 +44,38 @@ const cleanIndex = (raw) => {
|
|
|
37
44
|
const entries = Object.entries(data.icons).filter(([name, version]) => NAME.test(name) && Number.isInteger(version) && version > 0);
|
|
38
45
|
return { format: 2, hash: data.hash, icons: Object.fromEntries(entries) };
|
|
39
46
|
};
|
|
47
|
+
const keyTag = (key) => key.slice(0, 14);
|
|
48
|
+
const syncSetKey = () => {
|
|
49
|
+
const key = apiKey();
|
|
50
|
+
const tag = key ? keyTag(key) : undefined;
|
|
51
|
+
if (tag === setFor)
|
|
52
|
+
return;
|
|
53
|
+
setFor = tag;
|
|
54
|
+
setIcons.clear();
|
|
55
|
+
setCheckedAt = 0;
|
|
56
|
+
setLoaded = false;
|
|
57
|
+
setInflight = undefined;
|
|
58
|
+
warnedSet = false;
|
|
59
|
+
if (!tag)
|
|
60
|
+
return;
|
|
61
|
+
try {
|
|
62
|
+
const cached = JSON.parse(storage()?.getItem(SET_KEY) ?? "null");
|
|
63
|
+
if (!cached || cached.tag !== tag)
|
|
64
|
+
return;
|
|
65
|
+
for (const [name, entry] of Object.entries(cached.icons ?? {})) {
|
|
66
|
+
const icon = NAME.test(name) && typeof entry?.svg === "string" ? parseSVG(entry.svg) : undefined;
|
|
67
|
+
if (icon)
|
|
68
|
+
setIcons.set(name, icon);
|
|
69
|
+
}
|
|
70
|
+
setCheckedAt = typeof cached.checkedAt === "number" ? cached.checkedAt : 0;
|
|
71
|
+
setLoaded = true;
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
40
77
|
const hydrate = () => {
|
|
78
|
+
syncSetKey();
|
|
41
79
|
if (hydrated)
|
|
42
80
|
return;
|
|
43
81
|
hydrated = true;
|
|
@@ -184,10 +222,76 @@ const fetchIcon = (name, version) => {
|
|
|
184
222
|
iconInflight.set(key, request);
|
|
185
223
|
return request;
|
|
186
224
|
};
|
|
225
|
+
const fetchSet = async (key) => {
|
|
226
|
+
const response = await fetch(`${apiBase()}/sdk/set?key=${encodeURIComponent(key)}`);
|
|
227
|
+
const body = (await response.json().catch(() => ({})));
|
|
228
|
+
if (!response.ok)
|
|
229
|
+
throw new Error(body.error || `HTTP ${response.status}`);
|
|
230
|
+
const next = new Map();
|
|
231
|
+
const raw = {};
|
|
232
|
+
for (const [name, entry] of Object.entries(body.icons ?? {})) {
|
|
233
|
+
if (!NAME.test(name) || typeof entry?.svg !== "string")
|
|
234
|
+
continue;
|
|
235
|
+
const icon = parseSVG(entry.svg);
|
|
236
|
+
if (!icon)
|
|
237
|
+
continue;
|
|
238
|
+
next.set(name, icon);
|
|
239
|
+
raw[name] = { version: Number(entry.version) || 1, svg: entry.svg };
|
|
240
|
+
}
|
|
241
|
+
if (keyTag(key) !== setFor)
|
|
242
|
+
return;
|
|
243
|
+
setIcons.clear();
|
|
244
|
+
for (const [name, icon] of next)
|
|
245
|
+
setIcons.set(name, icon);
|
|
246
|
+
setCheckedAt = Date.now();
|
|
247
|
+
setLoaded = true;
|
|
248
|
+
try {
|
|
249
|
+
storage()?.setItem(SET_KEY, JSON.stringify({ tag: keyTag(key), checkedAt: setCheckedAt, icons: raw }));
|
|
250
|
+
}
|
|
251
|
+
catch {
|
|
252
|
+
notify();
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
notify();
|
|
256
|
+
};
|
|
257
|
+
export const loadSet = (options = {}) => {
|
|
258
|
+
hydrate();
|
|
259
|
+
const key = apiKey();
|
|
260
|
+
if (!key)
|
|
261
|
+
return Promise.resolve();
|
|
262
|
+
const fresh = setLoaded && Date.now() - setCheckedAt < revalidateMs();
|
|
263
|
+
if (fresh && !options.force)
|
|
264
|
+
return Promise.resolve();
|
|
265
|
+
if (setInflight)
|
|
266
|
+
return setInflight;
|
|
267
|
+
const pending = fetchSet(key)
|
|
268
|
+
.catch((error) => {
|
|
269
|
+
setLoaded = true;
|
|
270
|
+
if (!warnedSet) {
|
|
271
|
+
warnedSet = true;
|
|
272
|
+
console.warn(`iconplease: could not load your icon set — ${error instanceof Error ? error.message : String(error)} Falling back to public icons.`);
|
|
273
|
+
}
|
|
274
|
+
notify();
|
|
275
|
+
})
|
|
276
|
+
.finally(() => {
|
|
277
|
+
setInflight = undefined;
|
|
278
|
+
});
|
|
279
|
+
setInflight = pending;
|
|
280
|
+
return pending;
|
|
281
|
+
};
|
|
187
282
|
export const requestIcon = async (name) => {
|
|
188
283
|
if (!NAME.test(name))
|
|
189
284
|
return;
|
|
190
285
|
hydrate();
|
|
286
|
+
if (apiKey()) {
|
|
287
|
+
if (setIcons.has(name)) {
|
|
288
|
+
void loadSet();
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
await loadSet();
|
|
292
|
+
if (setIcons.has(name))
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
191
295
|
const known = state.index?.icons[name];
|
|
192
296
|
if (known && icons.has(keyOf(name, known))) {
|
|
193
297
|
void loadIndex();
|
|
@@ -200,7 +304,7 @@ export const requestIcon = async (name) => {
|
|
|
200
304
|
};
|
|
201
305
|
export const load = async (options = {}) => {
|
|
202
306
|
const { icons: names = [], force = false } = Array.isArray(options) ? { icons: options } : options;
|
|
203
|
-
await loadIndex({ force });
|
|
307
|
+
await Promise.all([loadIndex({ force }), loadSet({ force })]);
|
|
204
308
|
await Promise.all(names.map(requestIcon));
|
|
205
309
|
};
|
|
206
310
|
export const subscribe = (listener) => {
|
|
@@ -215,14 +319,25 @@ export const getSnapshot = () => {
|
|
|
215
319
|
export const getStatus = () => getSnapshot().status;
|
|
216
320
|
export const getIcon = (name) => {
|
|
217
321
|
hydrate();
|
|
322
|
+
const own = setIcons.get(name);
|
|
323
|
+
if (own)
|
|
324
|
+
return own;
|
|
218
325
|
const version = state.index?.icons[name];
|
|
219
326
|
return version ? icons.get(keyOf(name, version)) : undefined;
|
|
220
327
|
};
|
|
221
328
|
export const isUnknown = (name) => {
|
|
222
329
|
hydrate();
|
|
330
|
+
if (setIcons.has(name))
|
|
331
|
+
return false;
|
|
332
|
+
if (apiKey() && !setLoaded)
|
|
333
|
+
return false;
|
|
223
334
|
return state.status === "ready" && !!state.index && !(name in state.index.icons);
|
|
224
335
|
};
|
|
225
|
-
export const
|
|
336
|
+
export const setIconNames = () => {
|
|
337
|
+
hydrate();
|
|
338
|
+
return [...setIcons.keys()];
|
|
339
|
+
};
|
|
340
|
+
export const iconNames = () => [...new Set([...setIconNames(), ...Object.keys(getSnapshot().index?.icons ?? {})])];
|
|
226
341
|
export const resetForTests = () => {
|
|
227
342
|
state = { status: "idle", revision: 0 };
|
|
228
343
|
checkedAt = 0;
|
|
@@ -231,6 +346,12 @@ export const resetForTests = () => {
|
|
|
231
346
|
warnedOffline = false;
|
|
232
347
|
icons.clear();
|
|
233
348
|
iconInflight.clear();
|
|
349
|
+
setIcons.clear();
|
|
350
|
+
setFor = undefined;
|
|
351
|
+
setCheckedAt = 0;
|
|
352
|
+
setLoaded = false;
|
|
353
|
+
setInflight = undefined;
|
|
354
|
+
warnedSet = false;
|
|
234
355
|
if (persistTimer)
|
|
235
356
|
clearTimeout(persistTimer);
|
|
236
357
|
persistTimer = undefined;
|
package/dist/types.d.ts
CHANGED