UNPKG

@yookue/ts-lang-utils

Version:

Common lang utilities for typescript

126 lines (124 loc) 2.92 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/util/ElementUtils/downloadUrlByElement.ts var downloadUrlByElement_exports = {}; __export(downloadUrlByElement_exports, { downloadUrlByElement: () => downloadUrlByElement }); module.exports = __toCommonJS(downloadUrlByElement_exports); var specialExtensions = [ // Microsoft Office "doc", "docx", "docm", "dot", "dotx", "dotm", "xls", "xlsx", "xlsm", "xlsb", "xlam", "xlt", "xltx", "xltm", "ppt", "pptx", "pptm", "pot", "potx", "potm", "ppsx", "ppsm", "pps", // WPS Office "wps", "wpt", "et", "ett", "dps", "dpt", // China Office "eio", "eti", "edp", "uof", "uot", "uos", "uop", "smd", "smt", "smp", // PDF "pdf", // Archive "zip", "rar", "7z", "tar", "gz", "bz2", // Other packages "exe", "msi", "dmg", "iso" ]; function downloadUrlByElement(url, fileName) { var _a; if (!url) { return; } const sameOrigin = new URL(url, window.location.href).origin === window.location.origin; const fileExtension = (_a = fileName == null ? void 0 : fileName.split(".").pop()) == null ? void 0 : _a.toLowerCase(); const blobHandle = !sameOrigin || specialExtensions.includes(fileExtension ?? ""); if (!blobHandle) { const link = document.createElement("a"); if (fileName) { link.download = fileName; } link.href = url; document.body.appendChild(link); link.click(); document.body.removeChild(link); return; } window.fetch(url).then((response) => { if (!response.ok) { throw new Error("Network response is not ok"); } return response.blob(); }).then((blob) => { const blobUrl = window.URL.createObjectURL(blob); const link = document.createElement("a"); link.href = blobUrl; if (fileName) { link.download = fileName; } document.body.appendChild(link); link.click(); document.body.removeChild(link); window.URL.revokeObjectURL(blobUrl); }).catch(() => { window.open(url, "_blank"); }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { downloadUrlByElement });