@scalar/api-client
Version:
the open source API testing client
79 lines (78 loc) • 2.49 kB
JavaScript
import { isDefined as k, shouldIgnoreEntity as I } from "@scalar/oas-utils/helpers";
import F from "fuse.js";
import { ref as l, watch as S, computed as y, nextTick as H } from "vue";
import { useRouter as L } from "vue-router";
import { PathId as T } from "../../routes.js";
import { useActiveEntities as M } from "../../store/active-entities.js";
import { useWorkspace as P } from "../../store/store.js";
function N() {
const h = L(), { activeWorkspace: W, activeWorkspaceRequests: f, activeWorkspaceCollections: b } = M(), { requests: E, tags: w } = P(), n = l([]), c = l([]), s = l(0), o = l(""), r = l(null), m = l([]), p = new F(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) => !I(t)).filter((t) => !!!b.value?.find(
(a) => a.requests.includes(t.uid)
)?.tags.map((a) => w[a]).filter(k).filter((a) => t.tags?.includes(a.name)).filter((a) => I(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: {
[T.Request]: t.uid,
[T.Workspace]: W.value?.uid
}
})?.href
})), p.setCollection(n.value);
}, R = () => {
s.value = 0, c.value = p.search(o.value);
};
S(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, H(() => {
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 = y(() => f.value.map((e) => E[e]).filter(k));
S(
f,
() => {
d(D.value);
},
{ immediate: !0 }
);
const g = (e) => {
h.push(e.item.id), x();
}, u = y(() => 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
};