UNPKG

tldraw

Version:

A tiny little drawing editor.

42 lines (41 loc) 1.41 kB
import { sanitizeId } from "@tldraw/editor"; import { exportToBlob } from "./export.mjs"; async function exportAs(editor, ids, format = "png", name, opts = {}) { if (!name) { name = `shapes at ${getTimestamp()}`; if (ids.length === 1) { const first = editor.getShape(ids[0]); if (editor.isShapeOfType(first, "frame")) { name = first.props.name || "frame"; } else { name = `${sanitizeId(first.id)} at ${getTimestamp()}`; } } } name += `.${format}`; const blob = await exportToBlob({ editor, ids, format, opts }); const file = new File([blob], name, { type: blob.type }); downloadFile(file); } function getTimestamp() { const now = /* @__PURE__ */ new Date(); const year = String(now.getFullYear()).slice(2); const month = String(now.getMonth() + 1).padStart(2, "0"); const day = String(now.getDate()).padStart(2, "0"); const hours = String(now.getHours()).padStart(2, "0"); const minutes = String(now.getMinutes()).padStart(2, "0"); const seconds = String(now.getSeconds()).padStart(2, "0"); return `${year}-${month}-${day} ${hours}.${minutes}.${seconds}`; } function downloadFile(file) { const link = document.createElement("a"); const url = URL.createObjectURL(file); link.href = url; link.download = file.name; link.click(); URL.revokeObjectURL(url); } export { exportAs }; //# sourceMappingURL=exportAs.mjs.map