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