UNPKG

filepond

Version:

FilePond, where files go to stretch their bits.

289 lines (288 loc) • 7.25 kB
/*! * FilePond v5.0.2 * Copyright (c) 2017-2026 Pqina B.V. * Released under the MIT License * https://filepond.com */ import { sortTree as tt, eachTree as q, filterTree as et, mapTree as w, findTree as nt } from "../utils/tree.js"; import { deepMerge as rt, deepOverlap as it } from "../utils/object.js"; import { pubsub as st } from "../utils/pubsub.js"; import { arrayRemoveFalsy as m, arrayWrap as ot } from "../utils/array.js"; import { isArray as U, isDirectoryEntry as h, isUndefined as I, isNumber as b, isString as P, isNullOrUndefined as at, isObject as ft, isFileEntry as D, isBlobOrFile as ct, isFile as ut } from "../utils/test.js"; import { getUniqueId as dt } from "../utils/string.js"; import { passthrough as lt } from "../utils/placeholder.js"; function xt(O) { const { beforeInsertEntries: A = lt, beforeOnboardEntry: N, beforeUpdateEntryWithProps: R } = O ?? {}, { on: W, pub: x } = st(), a = { entries: [] }, p = /* @__PURE__ */ new Map(); let u = !1; function S(t) { const n = N ? N(t) : D(t) || h(t) ? t : !1; if (n !== !1) return n.id = n.id ?? dt(), n; } function T(t) { x("insertEntry", t), !(D(t) && ct(t.file) && F(t)) && z(t); } function C(t) { x("removeEntry", t); } function z(t) { x("updateEntry", t); } function F(t) { let n = !1; const e = W("updateEntry", () => { e(), n = !0; }); return x("updateEntryData", t), n; } function d() { x("updateEntries", a.entries); } function E(t, n = []) { t || (p.clear(), t = a.entries), t.forEach((e, r) => { p.set(e.id, [...n, r]), h(e) && E(e.entries, [...n, r]); }); } function _() { p.clear(); } function k(t) { return U(t) ? t : [t]; } function y(t, n = a) { const e = t; for (let r = 0; r < e.length; r++) { const i = e[r], o = n.entries[i]; if (h(o) && r < e.length) { n = o; continue; } return o; } return n; } function v(t) { const n = t.slice(0, t.length - 1); return y(n); } function g(t) { if (!at(t)) { if (b(t)) return a.entries[t] ? [t] : void 0; if (U(t) && t.every(b)) return t; if (P(t) || P(t.id)) { const n = t.id ?? t; return p.get(n); } if (ft(t)) { const n = nt(a.entries, (e) => it(t, e)); return n && p.get(n.id); } } } function M(t, ...n) { let e = !1; for (const r of n) { let i = !1; // we're comparing files D(t) && D(r) && // new entry carries a file object ut(r.file) && // these files are different so we're gonna update data t.file !== r.file && (i = !0), R && R(t, r, i), i && (e = !0), t = rt(t, r); } return [t, e]; } function G(t, ...n) { const e = g(t); if (!e) return; const r = v(e); if (!r) return; const i = m(n), o = e.at(-1); if (I(o)) return; const [f, s] = M( r.entries[o], ...i ); r.entries = r.entries.toSpliced(o, 1, f); let c = !1; return s && (c = F(f)), c || z(f), u || d(), f; } function H(t, n) { let e = ot(t), r = a, i = -1; n = U(n) || b(n) && n > -1 ? n : a.entries.length; const o = k(n); for (let s = 0; s < o.length; s++) { const c = o[s]; if (c < 0) return; if (h(r.entries[c]) && s < o.length) { r = r.entries[c]; continue; } i = c; break; } const f = A( m(e.map((s) => S(s))), r.entries ); u = !0, r.entries = r.entries.toSpliced(i, 0, ...f), E(), f.forEach((s) => T(s)), d(), u = !1; } function J(t, ...n) { const e = g(t); if (!e) return; const r = v(e); if (!r || !h(r)) return; const i = e.at(-1); if (I(i)) return; u = !0; const o = y(e), f = A( m(n.flat().map(S)), r.entries.filter((s) => s.id !== o.id) ); r.entries = r.entries.toSpliced(i, 1, ...f), E(), C({ entry: o, index: [-1] }), f.forEach(T), d(), u = !1; } function K(t, n) { const e = g(t); if (!e) return; const r = v(e); if (!r || !h(r)) return; const i = k(n), o = v(i); if (!o || !h(o)) return; const f = e.at(-1); if (I(f)) return; const s = i.at(-1); if (I(s)) return; const c = r.entries[f]; r.entries = r.entries.toSpliced(f, 1), o.entries = o.entries.toSpliced(s, 0, c), E(), d(); } function L(...t) { const n = m(t).map((e) => { if (b(e)) return a.entries[e]; if (U(e) && e.every(b)) return y(e); if (P(e) || P(e.id)) { const r = e.id ?? e, i = p.get(r); return I(i) ? void 0 : y(i); } }); return t.length === 1 ? n[0] : n; } function Q(...t) { const n = t[0] === -1 ? ( // last item [[a.entries.length - 1]] ) : ( // no params -> remove all t.length === 0 || t[0] === void 0 ? Array.from(p.values()) : ( // else find item [...arguments].map(g) ) ), e = [], r = []; for (const i of n) { if (!i) { e.push(void 0); continue; } const o = v(i); if (!o || !h(o)) { e.push(void 0); continue; } const f = i.at(-1); if (I(f)) return; const s = o.entries[f]; s && (e.push({ entry: s, index: i }), o.entries = o.entries.toSpliced(f, 1, void 0), r.push(o)); } return r.forEach((i) => { i.entries = m(i.entries); }), e.length && (E(), u = !0, m(e).forEach((i) => C(i)), d(), u = !1), arguments.length === 1 ? e[0] : e; } function V(t) { a.entries = tt(a.entries, t), E(), d(); } function X(t) { u = !0; const n = [], e = [], r = [], i = new Set(p.keys()), o = /* @__PURE__ */ new Set(), f = w(t, (s) => { const c = P(s.id) && g(s.id); if (c) { o.add(s.id); const l = s, B = y(c); if (l === B) return B; const [j, $] = M( B, l ); return r.push([j, $]), j; } else { const l = S(s); return l ? (e.push(l), l) : void 0; } }); Array.from(i).filter((s) => !o.has(s)).forEach((s) => { const c = g(s), l = y(c); n.push({ entry: l, index: [-1] }); }), a.entries = f, E(), n.forEach(C), e.forEach(T), r.forEach(([s, c]) => { c && F(s) || z(s); }), d(), u = !1; } function Y(t) { u = !0; const n = et(w(t, S), Boolean); a.entries = n, E(), q(a.entries, T), d(), u = !1; } function Z() { a.entries.length && (u = !0, q(a.entries, (t, n) => C({ entry: t, index: [-1] })), a.entries = [], _(), d(), u = !1); } return { on: W, insertEntries: H, findEntries: L, removeEntries: Q, sortEntries: V, updateEntry: G, replaceEntry: J, moveEntry: K, set entries(t) { if (!(!U(t) || a.entries === t)) { if (!t.length) { Z(); return; } if (!a.entries.length) { Y(t); return; } X(t); } }, get entries() { return a.entries; }, destroy() { } }; } export { xt as createEntryTree };