UNPKG

@scalar/api-client

Version:

the open source API testing client

79 lines (78 loc) 2.49 kB
import { PathId as k } from "../../routes.js"; import { useActiveEntities as F } from "../../store/active-entities.js"; import { isDefined as I, shouldIgnoreEntity as S } from "@scalar/oas-utils/helpers"; import H from "fuse.js"; import { ref as l, watch as y, computed as T, nextTick as L } from "vue"; import { useRouter as M } from "vue-router"; import { useWorkspace as P } from "../../store/store.js"; function N() { const h = M(), { activeWorkspace: W, activeWorkspaceRequests: f, activeWorkspaceCollections: b } = F(), { requests: E, tags: w } = P(), n = l([]), c = l([]), s = l(0), o = l(""), r = l(null), m = l([]), p = new H(n.value, { keys: ["title", "description", "body"] }), x = () => { o.value = "", s.value = 0, c.value = [], r.value instanceof HTMLInputElement && r.value.blur(); }, d = (e) => { n.value = e.filter((t) => !S(t)).filter((t) => !!!b.value?.find( (a) => a.requests.includes(t.uid) )?.tags.map((a) => w[a]).filter(I).filter((a) => t.tags?.includes(a.name)).filter((a) => S(a)).length).map((t) => ({ id: t.uid, title: t.summary ?? t.method, description: t.description ?? "", httpVerb: t.method, path: t.path, link: h?.resolve({ name: "request", params: { [k.Request]: t.uid, [k.Workspace]: W.value?.uid } })?.href })), p.setCollection(n.value); }, R = () => { s.value = 0, c.value = p.search(o.value); }; y(o, (e) => { e.length ? R() : c.value = []; }); const A = (e) => { const t = e === "up" ? -1 : 1, i = u.value.length; s.value = (s.value + t + i) % i, L(() => { const v = m.value[s.value]; v instanceof HTMLElement && v.scrollIntoView({ behavior: "smooth", block: "center" }); }); }, C = () => { if (s.value >= 0) { const e = u.value[s.value]; e && g(e); } }, D = T(() => f.value.map((e) => E[e]).filter(I)); y( f, () => { d(D.value); }, { immediate: !0 } ); const g = (e) => { h.push(e.item.id), x(); }, u = T(() => o.value.length === 0 ? n.value.map((e) => ({ item: e })) : c.value); return { searchText: o, searchResultsWithPlaceholderResults: u, selectedSearchResult: s, onSearchResultClick: g, fuseSearch: R, searchInputRef: r, searchResultRefs: m, navigateSearchResults: A, selectSearchResult: C, populateFuseDataArray: d }; } export { N as useSearch };