UNPKG

@scalar/api-client

Version:

the open source API testing client

136 lines (135 loc) 4.55 kB
import { dereference as F } from "@scalar/openapi-parser"; import { getParentEntry as i } from "@scalar/sidebar"; import { getResolvedRef as b } from "@scalar/workspace-store/helpers/get-resolved-ref"; import { unpackProxyObject as x } from "@scalar/workspace-store/helpers/unpack-proxy"; import { toValue as w } from "vue"; const l = { /** Insert before the hovered item */ BEFORE: 0, /** Insert after the hovered item */ AFTER: 1, /** Drop into the hovered item */ INTO: 2 }, B = (n, t, e, o) => { if (t === -1 || e === -1 || t === e) return null; const c = [...n], [s] = c.splice(t, 1); if (s === void 0) return null; const u = A(t, e, o, c.length); return c.splice(u, 0, s), x(c, { depth: 1 }); }, A = (n, t, e, o) => { const c = n < t; return e === l.AFTER ? c ? t : t + 1 : e === l.BEFORE ? c ? t - 1 : t : e === l.INTO ? o : t; }, h = (n, t) => t.includes(n.type), p = (n) => n.offset < l.INTO, E = (n) => n.offset === l.INTO, y = (n, t) => !n.parent || !t.parent ? !1 : n.parent.id === t.parent.id, m = ({ store: n, entry: t }) => { const e = i("document", t); if (!e) return null; const o = n.workspace.documents[e.name]; return o ? t.type === "document" ? o : t.type === "tag" ? o.tags?.find((c) => c.name === t.name) ?? null : t.type === "operation" ? b(o.paths?.[t.path]?.[t.method]) ?? null : null : null; }, O = ({ store: n, entry: t }) => { const e = i("document", t); e && n.buildSidebar(e.name); }, P = (n, t) => n["x-scalar-order"] ?? t.children?.map((o) => o.id) ?? [], T = (n, t, e, o) => { const c = t.parent; if (!c || !h(c, ["tag", "document"])) return !1; const s = m({ store: n, entry: c }); if (!s) return !1; const u = P(s, c), r = u.findIndex((d) => d === t.id), a = u.findIndex((d) => d === e.id), f = B(u, r, a, o); return f ? (s["x-scalar-order"] = f, O({ store: n, entry: c }), !0) : !1; }, k = (n, t, e) => { const o = new Set(n.tags ?? []); e && o.add(e.name), t && o.delete(t.name), n.tags = Array.from(o); }, N = (n, t, e, o) => { n.paths?.[e.path]?.[e.method] && delete n.paths[e.path][e.method], t.paths ? (t.paths[e.path] || (t.paths[e.path] = {}), t.paths[e.path][e.method] = o) : t.paths = { [e.path]: { [e.method]: o } }; }, j = (n, t, e) => { const { schema: o } = F(n); return o.paths?.[t]?.[e]; }, M = (n, t, e) => { const o = i("document", t), c = i("document", e); if (!o || !c) return !1; const s = m({ store: n, entry: o }), u = m({ store: n, entry: c }); if (!s || !u) return !1; const r = j( x(s), t.path, t.method ); if (!r) return !1; const a = i("tag", t) ?? null, f = i("tag", e) ?? null; return k(r, a, f), N(s, u, t, r), O({ store: n, entry: t }), O({ store: n, entry: e }), !0; }, S = (n, t, e, o) => { const c = n.workspace["x-scalar-order"] ?? Object.keys(n.workspace.documents), s = c.findIndex((a) => a === t.id), u = c.findIndex((a) => a === e.id), r = B(c, s, u, o); return r ? (n.update("x-scalar-order", r), !0) : !1; }, z = ({ store: n, sidebarState: t }) => ({ handleDragEnd: (c, s) => { const u = w(n); if (!u) return !1; const r = t.getEntryById(c.id), a = t.getEntryById(s.id); if (!r || !a) return !1; if (r.type === "document") return a.type !== "document" || !p(s) ? !1 : S(u, r, a, s.offset); if (r.type === "tag") return a.type !== "tag" || !p(s) || !y(r, a) ? !1 : T( u, r, a, s.offset ); if (r.type === "operation") { if (p(s) && y(r, a)) return T( u, r, a, s.offset ); if (h(a, ["tag", "document"]) && E(s)) return M(u, r, a); } return !1; }, isDroppable: (c, s) => { const u = w(n); if (!u) return !1; const r = t.getEntryById(c.id), a = t.getEntryById(s.id); if (!r || !a || r.type === "example") return !1; if (r.type === "document") return a.type === "document" && p(s); if (r.type === "tag") return p(s) && y(r, a); if (r.type === "operation") { const f = i("document", a), d = i("document", r); if (!f || !d) return !1; const D = m({ store: u, entry: f }); if (!D) return !1; const R = y(r, a); return p(s) && !R ? !1 : R ? p(s) || E(s) && h(a, ["tag", "document"]) : E(s) && h(a, ["tag", "document"]) && (d.id === f.id || D.paths?.[r.path]?.[r.method] === void 0); } return !1; } }); export { z as dragHandleFactory };