UNPKG

@blocklet/ui-react

Version:

Some useful front-end web components that can be used in Blocklets.

87 lines (86 loc) 2.49 kB
import { mapRecursive as s, filterRecursive as a, isUrl as f } from "./utils.js"; const g = window?.blocklet?.groupPrefix || window?.blocklet?.prefix || "/", i = (o) => { const r = { ...o }, e = o?.background; return typeof e == "string" ? r.background = { header: e, footer: e, default: e } : e && typeof e == "object" && (r.background = { header: e.header || e.default, footer: e.footer || e.default, default: e.default }), r; }, c = (o, r = "en") => { if (typeof o == "string") { if (f(o)) { const t = new URL(o); return t.searchParams.set("locale", r), t.href; } const e = new URL(o, window.location.origin); return e.searchParams.set("locale", r), e.pathname + e.search; } return typeof o == "object" ? o[r] || o?.en || o?.zh : o; }, p = (o, r = "en") => { if (!o?.length) return o; const e = (t, n) => t && typeof t == "object" ? t[n] || t?.en || t?.zh : t; return s( o, (t) => ({ ...t, title: e(t.title, r), description: e(t.description, r), // 仅对叶结点进行处理 link: t.items?.length ? t.link : c(t.link, r), _rawLink: t.link }), "items" ); }, u = (o) => s( o, (r) => r.role ? { ...r, role: Array.isArray(r.role) ? r.role : [r.role] } : r, "items" ), l = (o) => { if (!o?.length) return null; const r = u(o), e = { header: [], footer: [], // 对应 footer social media social: [], // 对应 footer 底部 links bottom: [], // 对应 dashboard#sidenav 导航 dashboard: [], // session manager menus sessionManager: [], userCenter: [] }; return r.forEach((t) => { t.section ? Array.isArray(t.section) ? t.section.forEach((n) => { e[n]?.push(t); }) : typeof t.section == "string" && e[t.section]?.push(t) : e.header.push(t); }), e; }, b = (o) => { if (!o) return null; const r = { ...o }; return r.theme = i(r.theme), r.navigation = l(d(r.navigation)), r; }, d = (o = []) => a(o, (r, e) => !!r.link || e.filteredChildren?.length, "items"), y = (o, r) => a( o, (e, t) => { const n = !e.role || r && (e.role.includes(r) || e.role.includes("guest")); return t.isLeaf ? n : n && t.filteredChildren?.length; }, "items" ); export { y as filterNavByRole, d as filterValidNavItems, b as formatBlockletInfo, u as formatNavigation, i as formatTheme, c as getLink, p as getLocalizedNavigation, l as parseNavigation, g as publicPath };