nice-ui
Version:
React design system, components, and utilities
50 lines (49 loc) • 1.69 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.IconsGridState = void 0;
const rxjs_1 = require("rxjs");
const cdn_1 = require("./cdn");
class IconsGridState {
constructor() {
this.sets$ = new rxjs_1.BehaviorSubject([]);
this.icons$ = new rxjs_1.BehaviorSubject(new Map());
this.selected$ = new rxjs_1.BehaviorSubject(null);
this.select = (set, icon) => {
this.selected$.next([set, icon]);
};
this.unselect = () => {
this.selected$.next(null);
};
this.srcCache = new Map();
}
cdn(cdn = 'jsdelivr') {
return `${cdn_1.cdns.get(cdn).npm}iconista@2/`;
}
href(set, icon, cdn = 'jsdelivr') {
return `${cdn_1.cdns.get(cdn).npm}iconista@2/sets/${set}/${icon}.svg`;
}
async load() {
const sets = await fetch(`${this.cdn()}sets/index.json`);
const json = (await sets.json());
this.sets$.next(json);
await Promise.all(json.map(async (set) => {
const res = await fetch(`${this.cdn()}sets/${set}/index.json`);
const json = (await res.json());
const icons = this.icons$.getValue();
icons.set(set, json);
this.icons$.next(icons);
}));
}
async getIconSrc(set, icon) {
const key = `${set}/${icon}`;
const cached = this.srcCache.get(key);
if (cached)
return cached;
const href = this.href(set, icon);
const res = await fetch(href);
const src = await res.text();
this.srcCache.set(key, src);
return src;
}
}
exports.IconsGridState = IconsGridState;
;