index.ts 1.1 KB

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