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

374 lines (373 loc) • 10.6 kB
import { createStore as e } from "./store.js"; //#region src/core/controller.ts var t = { ArrowUp: "up", ArrowDown: "down", ArrowLeft: "left", ArrowRight: "right" }, n = (e, t) => !e || !t || e === t ? 0 : e.compareDocumentPosition(t) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1, r = class { store; draggables = /* @__PURE__ */ new Map(); droppables = /* @__PURE__ */ new Map(); callbacks = {}; instructionIds; #e; #t = null; constructor(t, n) { this.instructionIds = { list: `${n}-instructions`, grid: `${n}-grid-instructions` }, this.store = e({ operation: null, announcement: { text: "", key: 0 }, focusId: null, engine: null, messages: t }); } setCallbacks(e) { this.callbacks = e; } get messages() { return this.store.get().messages; } announce(e, t = !1) { clearTimeout(this.#e); let n = () => { let { announcement: t } = this.store.get(); this.store.set({ announcement: { text: e, key: t.key + 1 } }); }; t ? this.#e = setTimeout(n, 300) : n(); } item(e) { let t = { id: e.id, label: e.label, data: e.data }; return e.kind === "list" && (t.list = e.list, t.index = e.index), t; } lists() { let e = /* @__PURE__ */ new Map(), t = (t) => { let n = e.get(t); return n || (n = { id: t, label: t, orientation: "vertical", items: [] }, e.set(t, n)), n; }; for (let e of this.droppables.values()) { if (e.list === void 0) continue; let n = t(e.list); n.label = e.label, n.orientation = e.orientation; } for (let e of this.draggables.values()) e.kind === "list" && t(e.list).items.push(e); for (let t of e.values()) t.items.sort((e, t) => e.index - t.index); let r = (e) => this.containerOf(e.id)?.element ?? e.items[0]?.element ?? null; return [...e.values()].sort((e, t) => n(r(e), r(t))); } containerOf(e) { for (let t of this.droppables.values()) if (t.list === e) return t; } zonesFor(e) { let t = this.item(e); return [...this.droppables.values()].filter((e) => e.list === void 0 && !e.disabled).filter((e) => e.accept?.(t) ?? !0).sort((e, t) => n(e.element, t.element)); } accepts(e, t) { let n = this.droppables.get(e), r = this.draggables.get(t); return !n || !r || n.disabled ? !1 : n.list === void 0 ? n.accept?.(this.item(r)) ?? !0 : r.kind === "list"; } others(e, t) { return e.items.filter((e) => e.id !== t); } publicTarget(e) { if (!e || e.kind === "custom") return null; if (e.kind === "list") return { kind: "list", list: e.list, index: e.index }; let t = this.droppables.get(e.id); return t ? { kind: "zone", id: t.id, label: t.label, data: t.data } : null; } #n(e, t) { let n = this.lists(), r = t?.kind === "list" ? t : { kind: "list", list: e.list, index: e.index }, i = n.find((e) => e.id === r.list), a = i ? this.others(i, e.id).length + 1 : 1; return { item: e.label, index: r.index, count: a, ...n.length > 1 && i ? { list: i.label } : {} }; } #r(e, t) { let n = t?.kind === "zone" ? this.droppables.get(t.id) : void 0; return { item: e.label, zone: n?.label ?? null }; } #i(e, t, n) { let r = this.messages; return e.kind === "list" && t?.kind !== "zone" ? r[`list${n}`](this.#n(e, t)) : r[`zone${n}`](this.#r(e, t)); } get operation() { return this.store.get().operation; } start(e, t) { let n = this.draggables.get(e); if (!n || n.disabled || this.operation) return; let r = n.kind === "list" ? { kind: "list", list: n.list, index: n.index } : n.kind === "custom" ? { kind: "custom" } : null; this.store.set({ operation: { id: e, input: t, target: r }, focusId: null }), this.announce(n.custom ? n.custom.lift(t) : this.#i(n, r, "Lifted")), t === "keyboard" && this.#l(n), this.callbacks.onDragStart?.({ item: this.item(n), input: t }); } retarget(e) { let t = this.operation, n = t && this.draggables.get(t.id); t && n && (JSON.stringify(e) !== JSON.stringify(t.target) || t.input !== "pointer") && (this.store.set({ operation: { ...t, target: e } }), this.announce(this.#i(n, e, "Moved"), t.input === "pointer"), this.callbacks.onDragOver?.({ item: this.item(n), target: this.publicTarget(e), input: t.input })); } drop() { let e = this.operation, t = e && this.draggables.get(e.id); if (!e || !t) return; this.#u(); let { target: n, input: r } = e; if (t.custom) { let e = t.custom.drop(); this.#a(r, t.id), this.announce(e); return; } let i = !n || n.kind === "list" && n.list === t.list && n.index === t.index; if (t.kind === "list" && i) { this.#a(r, t.id), this.announce(this.#i(t, null, n ? "Dropped" : "Canceled")), this.callbacks.onDragCancel?.({ item: this.item(t), input: r }); return; } let a = this.item(t), o = this.#i(t, n, "Dropped"); this.#a(r, t.id), this.announce(o), n?.kind === "zone" && this.droppables.get(n.id)?.onDrop?.(a), this.callbacks.onDragEnd?.({ item: a, target: this.publicTarget(n), input: r }); } cancel() { let e = this.operation, t = e && this.draggables.get(e.id); if (!e || !t) return; this.#u(); let n = t.custom ? t.custom.cancel() : this.#i(t, t.kind === "list" ? null : e.target, "Canceled"); this.#a(e.input, t.id), this.announce(n), this.callbacks.onDragCancel?.({ item: this.item(t), input: e.input }); } #a(e, t) { this.store.set({ operation: null, focusId: e === "keyboard" ? t : null }); } keyDown(e, n) { let r = this.operation, i = n.key === " " || n.key === "Enter"; if (!r) { if (!i || n.repeat) return !1; let t = this.draggables.get(e); if (!t) return !1; if (t.disabled) { let e = t.custom?.blocked?.(); return e ? (n.preventDefault(), this.announce(e), !0) : !1; } return n.preventDefault(), this.start(e, "keyboard"), !0; } if (r.id !== e || r.input !== "keyboard") return !1; if (i) return n.preventDefault(), n.repeat || this.drop(), !0; if (n.key === "Escape") return n.preventDefault(), this.cancel(), !0; let a = t[n.key]; return a ? (n.preventDefault(), this.#o(e, a, n.shiftKey), !0) : !1; } #o(e, t, n) { let r = this.draggables.get(e), i = this.operation; if (r && i) { if (r.custom) { let e = r.custom.key(t, n); e && this.announce(e); return; } if (r.kind === "list") { this.retarget(this.#s(r, i.target, t)); return; } this.retarget(this.#c(r, i.target, t)); } } #s(e, t, n) { let r = this.lists(), i = n === "down" || n === "right"; if (t?.kind === "zone") { let n = this.zonesFor(e), a = n[n.findIndex((e) => e.id === t.id) + (i ? 1 : -1)]; if (a) return { kind: "zone", id: a.id }; let o = r.at(-1); if (i || !o) return t; let s = Math.min(e.index, this.others(o, e.id).length); return { kind: "list", list: o.id, index: s }; } let a = t?.kind === "list" ? t : { kind: "list", list: e.list, index: e.index }, o = r.findIndex((e) => e.id === a.list), s = r[o]; if (!s) return a; if (s.orientation === "vertical" ? n === "up" || n === "down" : n !== "up" && n !== "down") { let t = this.others(s, e.id).length, n = Math.max(0, Math.min(t, a.index + (i ? 1 : -1))); return { kind: "list", list: s.id, index: n }; } let c = r[o + (i ? 1 : -1)]; if (!c) { let t = i ? this.zonesFor(e)[0] : void 0; return t ? { kind: "zone", id: t.id } : a; } let l = this.others(c, e.id).length; return { kind: "list", list: c.id, index: Math.min(a.index, l) }; } #c(e, t, n) { let r = this.zonesFor(e); if (r.length === 0) return null; let i = t?.kind === "zone" ? r.findIndex((e) => e.id === t.id) : -1, a = n === "down" || n === "right", o = r[i === -1 ? a ? 0 : r.length - 1 : (i + (a ? 1 : -1) + r.length) % r.length]; return o ? { kind: "zone", id: o.id } : null; } #l(e) { let t = e.handle ?? e.element; t && (this.#t = t, t.addEventListener("focusout", this.#d)); } #u() { this.#t?.removeEventListener("focusout", this.#d), this.#t = null; } #d = (e) => { let t = this.#t; t && e.relatedTarget instanceof Node && t.contains(e.relatedTarget) || this.operation?.input === "keyboard" && this.cancel(); }; pointer = { start: (e) => { this.start(e, "pointer"); }, move: (e) => { let t = this.operation, n = t && this.draggables.get(t.id); if (t && n && t.input === "pointer") { if (n.custom) { let t = n.custom.pointer(e); t && this.announce(t, !0); return; } this.retarget(n.kind === "list" ? this.#p(n, e) : this.#f(n, e)); } }, end: (e, t) => { this.operation?.input === "pointer" && (t || this.pointer.move(e), t ? this.cancel() : this.drop()); } }; #f(e, t) { let n = t.targetId; if (n === null || !this.accepts(n, e.id)) return null; let r = this.droppables.get(n); return r && r.list === void 0 ? { kind: "zone", id: n } : null; } #p(e, t) { let n = t.targetId; if (n === null) return null; let r = this.droppables.get(n); if (r && r.list === void 0) return this.#f(e, t); let i = r?.list ?? this.draggables.get(n)?.list, a = this.lists().find((e) => e.id === i); if (!a) return null; let o = a.orientation === "vertical", s = o ? t.pointer.y : t.pointer.x, c = 0; for (let t of this.others(a, e.id)) { let e = t.element?.getBoundingClientRect(); e && (o ? e.top + e.height / 2 : e.left + e.width / 2) < s && c++; } return { kind: "list", list: a.id, index: c }; } dropEdge(e, t) { let n = e.operation, r = n?.target; if (!n || r?.kind !== "list" || n.id === t) return null; let i = this.draggables.get(n.id), a = this.draggables.get(t); if (!i || a?.kind !== "list" || a.list !== r.list || i.list === r.list && i.index === r.index) return null; let o = this.lists().find((e) => e.id === r.list); if (!o) return null; let s = this.others(o, i.id); return s[r.index]?.id === t ? "before" : r.index === s.length && s.at(-1)?.id === t ? "after" : null; } isOver(e, t) { let n = e.operation?.target; if (!n || !e.operation) return !1; if (n.kind === "zone") return n.id === t; if (n.kind !== "list" || this.droppables.get(t)?.list !== n.list) return !1; let r = this.lists().find((e) => e.id === n.list); return r ? this.others(r, e.operation.id).length === 0 : !1; } destroy() { clearTimeout(this.#e), this.#u(), this.store.get().engine?.destroy(); } }; //#endregion export { r as DndController }; //# sourceMappingURL=controller.js.map