@scalar/api-client
Version:
the open source API testing client
121 lines (120 loc) • 4.29 kB
JavaScript
import { sortByOrder as B } from "@scalar/helpers/array/sort-by-order";
import { toJsonCompatible as k } from "@scalar/helpers/object/to-json-compatible";
import { dereference as j, escapeJsonPointer as A } from "@scalar/openapi-parser";
import { unpackProxyObject as x } from "@scalar/workspace-store/helpers/unpack-proxy";
import { getParentEntry as d, getOpenapiObject as h } from "@scalar/workspace-store/navigation";
import { toValue as b } from "vue";
const P = (r, t, n, c) => {
if (t === -1 || n === -1 || t === n)
return null;
const o = [...r], [e] = o.splice(t, 1);
if (e === void 0)
return null;
const u = R(t, n, c, o.length);
return o.splice(u, 0, e), x(o, { depth: 1 });
}, R = (r, t, n, c) => {
const o = r < t;
return n === "after" ? o ? t : t + 1 : n === "before" ? o ? t - 1 : t : n === "into" ? c : t;
}, y = (r, t) => t.includes(r.type), p = (r) => r.offset === "before" || r.offset === "after", m = (r) => r.offset === "into", l = (r, t) => !r.parent || !t.parent ? !1 : r.parent.id === t.parent.id, O = ({ store: r, entry: t }) => {
const n = d("document", t);
n && r.buildSidebar(n.name);
}, J = (r, t) => r["x-scalar-order"] ?? t.children?.map((c) => c.id) ?? [], w = (r, t, n, c) => {
const o = t.parent;
if (!o || !y(o, ["tag", "document"]))
return !1;
const e = h({ store: r, entry: o });
if (!e)
return !1;
const u = J(e, o), s = u.findIndex((f) => f === t.id), a = u.findIndex((f) => f === n.id), i = P(u, s, a, c);
return i ? (e["x-scalar-order"] = i, O({ store: r, entry: o }), !0) : !1;
}, M = (r, t, n) => {
const c = new Set(r.tags ?? []);
n && c.add(n.name), t && c.delete(t.name), r.tags = Array.from(c);
}, T = (r, t, n, c) => {
r.paths?.[n.path]?.[n.method] && delete r.paths[n.path][n.method], t.paths ? (t.paths[n.path] || (t.paths[n.path] = {}), t.paths[n.path][n.method] = c) : t.paths = {
[n.path]: {
[n.method]: c
}
};
}, $ = (r, t, n) => {
const o = j(r).schema.paths?.[t]?.[n];
return k(o, { prefix: `#/paths/${A(t)}/${n}` });
}, C = (r, t, n) => {
const c = d("document", t), o = d("document", n);
if (!c || !o)
return !1;
const e = h({ store: r, entry: c }), u = h({ store: r, entry: o });
if (!e || !u)
return !1;
const s = $(
x(e),
t.path,
t.method
);
if (!s)
return !1;
const a = d("tag", t) ?? null, i = d("tag", n) ?? null;
return M(s, a, i), T(e, u, t, s), O({ store: r, entry: t }), O({ store: r, entry: n }), !0;
}, F = (r, t, n, c) => {
const o = r.workspace["x-scalar-order"] ?? [], e = Object.keys(r.workspace.documents), u = B(e, o, (f) => f), s = u.findIndex((f) => f === t.id), a = u.findIndex((f) => f === n.id), i = P(u, s, a, c);
return i ? (r.update("x-scalar-order", i), !0) : !1;
}, G = ({
store: r,
sidebarState: t
}) => ({
handleDragEnd: (o, e) => {
const u = b(r);
if (!u)
return !1;
const s = t.getEntryById(o.id), a = t.getEntryById(e.id);
if (!s || !a)
return !1;
if (s.type === "document")
return a.type !== "document" || !p(e) ? !1 : F(u, s, a, e.offset);
if (s.type === "tag")
return a.type !== "tag" || !p(e) || !l(s, a) ? !1 : w(
u,
s,
a,
e.offset
);
if (s.type === "operation") {
if (p(e) && l(s, a))
return w(
u,
s,
a,
e.offset
);
if (y(a, ["tag", "document"]) && m(e))
return C(u, s, a);
}
return !1;
},
isDroppable: (o, e) => {
const u = b(r);
if (!u)
return !1;
const s = t.getEntryById(o.id), a = t.getEntryById(e.id);
if (!s || !a || s.type === "example")
return !1;
if (s.type === "document")
return a.type === "document" && p(e);
if (s.type === "tag")
return p(e) && l(s, a);
if (s.type === "operation") {
const i = d("document", a), f = d("document", s);
if (!i || !f)
return !1;
const E = h({ store: u, entry: i });
if (!E)
return !1;
const D = l(s, a);
return p(e) && !D ? !1 : D ? p(e) || m(e) && y(a, ["tag", "document"]) : m(e) && y(a, ["tag", "document"]) && (f.id === i.id || E.paths?.[s.path]?.[s.method] === void 0);
}
return !1;
}
});
export {
G as dragHandleFactory
};