index.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { Mask } from "../store/mask";
  2. import { type BuiltinMask } from "./typing";
  3. export { type BuiltinMask } from "./typing";
  4. export const BUILTIN_MASK_ID = 100000;
  5. export const BUILTIN_MASK_STORE = {
  6. buildinId: BUILTIN_MASK_ID,
  7. masks: {} as Record<string, BuiltinMask>,
  8. get(id?: string) {
  9. if (!id) return undefined;
  10. return this.masks[id] as Mask | undefined;
  11. },
  12. add(m: BuiltinMask) {
  13. const mask = { ...m, id: this.buildinId++, builtin: true };
  14. this.masks[mask.id] = mask;
  15. return mask;
  16. },
  17. };
  18. export const BUILTIN_MASKS: BuiltinMask[] = [];
  19. if (typeof window != "undefined") {
  20. // run in browser skip in next server
  21. fetch("/masks.json")
  22. .then((res) => res.json())
  23. .catch((error) => {
  24. console.error("[Fetch] failed to fetch masks", error);
  25. return { cn: [], tw: [], en: [] };
  26. })
  27. .then((masks) => {
  28. const { cn = [], tw = [], en = [] } = masks;
  29. return [...cn, ...tw, ...en].map((m) => {
  30. BUILTIN_MASKS.push(BUILTIN_MASK_STORE.add(m));
  31. });
  32. });
  33. }