UNPKG

@mitcsutt/kiln-dnd

Version:

Kiln drag and drop: sortable lists, boards, drop zones and grid layouts with one keyboard model and one announcer, rendered through kiln-ui

84 lines (83 loc) • 2.65 kB
import { getAllCollisions as e } from "../grid/engine/collision.js"; import { bottom as t, cloneLayout as n, getLayoutItem as r, moveElement as i } from "../grid/engine/layout.js"; import { getCompactor as a } from "../grid/engine/compactors.js"; //#region src/data/layout.ts var o = (e = "vertical") => e === "none" ? null : e; function s(e) { return e.map((e) => { let t = { ...e }; return delete t.moved, t; }); } function c(e, t) { let { columns: r, compaction: i } = t, c = n(e).map((e) => d(e, r)); return s(a(o(i)).compact(c, r)); } function l(e, n, r) { let i = { ...n, x: n.x ?? 0, y: t(e) }; return c([...e, i], r); } function u(e, t, n) { return c(e.filter((e) => e.i !== t), n); } function d(e, t) { let n = Math.max(1, Math.min(e.w, t)), r = Math.max(0, Math.min(e.x, t - n)); return { ...e, w: n, x: r, y: Math.max(0, e.y) }; } function f(e, t) { let n = Math.max(1, Math.min(e.minW ?? 1, t)), r = Math.max(n, Math.min(e.maxW ?? t, t)), i = Math.max(1, e.minH ?? 1); return { minW: n, maxW: r, minH: i, maxH: Math.max(i, e.maxH ?? Infinity) }; } function p(e, t, c, l, u) { let { columns: d, compaction: f } = u, p = n(e), m = r(p, t); if (!m) return [...e]; let h = o(f), g = Math.max(0, Math.min(c, d - m.w)), _ = i(p, m, g, Math.max(0, l), !0, !1, h, d); return s(a(h).compact(_, d)); } function m(t, c, l, u, d) { let { columns: p, compaction: m } = d, h = n(t), g = r(h, c); if (!g) return [...t]; let _ = f(g, p); g.w = Math.max(_.minW, Math.min(l, _.maxW, p - g.x)), g.h = Math.max(_.minH, Math.min(u, _.maxH)); let v = o(m); if (v === null) for (let t of e(h, g)) i(h, t, t.x, g.y + g.h, !1, !1, null, p); return s(a(v).compact(h, p)); } function h(e, t, n, r) { let i = e.find((e) => e.i === t); if (!i) return [...e]; let a = p(e, t, n.x, n.y, r); return n.w === i.w && n.h === i.h ? a : m(a, t, n.w, n.h, r); } function g(e, n, r, i, a) { let o = h(e, n, r, a).find((e) => e.i === n); if (!o) return null; let s = t(e) + r.h + 1; for (let t = 1; t <= s; t++) { let s = { ...r }; if (i === "down" && (s.y = o.y + t), i === "up" && (s.y = o.y - t), i === "left" && (s.x = o.x - t), i === "right" && (s.x = o.x + t), s.y < 0 || s.x < 0 || s.x + r.w > a.columns) return null; let c = h(e, n, s, a), l = c.find((e) => e.i === n); if (l && (l.x !== o.x || l.y !== o.y)) return { request: s, layout: c }; } return null; } //#endregion export { l as appendItem, c as compact, o as compactType, p as moveInLayout, h as previewLayout, u as removeItem, m as resizeInLayout, f as sizeLimits, g as stepInLayout, s as toPublic }; //# sourceMappingURL=layout.js.map