UNPKG

filepond

Version:

FilePond, where files go to stretch their bits.

124 lines (123 loc) • 3.36 kB
/*! * FilePond v5.0.2 * Copyright (c) 2017-2026 Pqina B.V. * Released under the MIT License * https://filepond.com */ import { h as y } from "../utils/dom.js"; import { blobToFile as $ } from "../utils/file.js"; import { createRandomish as S, randomNumberBetween as b } from "../utils/math.js"; import { sleep as x } from "../utils/sleep.js"; const D = 1, u = S(D); function M(t, e) { return e ? /text/.test(e) : /\.(txt|md|markdown)$/.test(t); } function F(t, e) { return e ? /image/.test(e) : /\.(png|jpg|jpeg|webp)$/.test(t); } async function H(t) { const { name: e = "Untitled", type: i = "text/plain" } = t ?? {}; return M(e, i) ? await T({ ...t, name: e }) ?? null : F(e, i) ? await R({ ...t, name: e }) : null; } function v(t, e, i) { const { width: n, height: a } = t.canvas, p = t.createLinearGradient( n * u(), 0, n * u(), a ), d = 3, o = 30; let s = Math.round(b(0, 360 - o, u)); for (let c = 0; c < d; c++) s += Math.round(b(o, 90, u)), p.addColorStop(c / (d - 1), `hsl(${s} 90% 50%)`); t.fillStyle = e || p, t.fillRect(0, 0, n, a), t.fillStyle = "rgba(0,0,0,.35)", t.textAlign = "center", t.textBaseline = "middle", t.font = `bold ${n / 16}px sans-serif`, t.fillText(`${i} ${n} × ${a}`, n * 0.5, a * 0.5); } async function R(t) { const { name: e = "Untitled", width: i = 1280, height: n = 720, quality: a = 0.98, fillStyle: p = void 0, lastModified: d = Date.now() } = t ?? {}; let o = "image/png"; if (e && !t?.type) { const l = e.split(".").pop(); l && /jpeg|png|webp/.test(l) && (o = `image/${e.split(".").pop()}`); } const s = y("canvas", { width: i, height: n }), c = s.getContext("2d"); v(c, p, o); const g = await new Promise((l, r) => { try { s.toBlob( (f) => { f ? l(f) : r(new Error("Failed to create blob from canvas")); }, o, a ); } catch (f) { r(f); } }), m = /\./.test(e) ? e : `${e}.${o.split("/").pop()}`; return $(g, m, { type: o, lastModified: d }); } async function I(t) { const { name: e = "Untitled", width: i = 1280, height: n = 720, fps: a = 10, fillStyle: p = "silver", lastModified: d = Date.now() } = t ?? {}, o = 10, s = "video/webp", c = y("canvas", { width: i, height: n }), g = c.getContext("2d"), m = [], l = new MediaRecorder(c.captureStream(a), { mimeType: "video/webm" }); l.ondataavailable = (r) => { r.data.size && m.push(r.data); }, l.start(); for (let r = 0; r < o; r++) v(g, p, s), await x(1e3 / a); return await new Promise((r) => { l.onstop = r, setTimeout(() => { l.stop(); }, 1e3 / a); }), new File(m, `${e}.${s.split("/").pop()}`, { type: s, lastModified: d }); } async function T(t) { const { name: e = "Untitled", type: i = "text/plain", content: n = "Hello World", lastModified: a = Date.now() } = t ?? {}; if (i === "text/plain") return new File([n], e.endsWith(".txt") ? e : `${e}.txt`, { type: i, lastModified: a }); } let h, w; function N() { const t = performance.now(); h || (h = t), w || (w = t); const e = t - w; return w = t, { ms: t - h, step: e }; } export { T as generateDocument, H as generateFile, R as generateImage, I as generateVideo, N as now, x as sleep };