UNPKG

@bizjs/biz-utils

Version:

The biz utils for web develpoment.

118 lines (116 loc) 4.76 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/browserUtils.ts var browserUtils_exports = {}; __export(browserUtils_exports, { copyText: () => copyText, download: () => download, downloadBlob: () => downloadBlob, getQuery: () => getQuery, openUrl: () => openUrl }); module.exports = __toCommonJS(browserUtils_exports); var import_file_saver = require("file-saver"); var import_clipboard = __toESM(require("clipboard")); var import_internalUtils = require("./_internalUtils"); var import_errors = require("./errors"); var import_biz_utils_common = require("@bizjs/biz-utils-common"); function getQuery(search) { const searchStr = search === void 0 ? location.search : search; return (0, import_biz_utils_common.parseQuery)(searchStr); } function openUrl(url, options) { return (0, import_internalUtils._openUrl)(url, options || {}); } function download(url, options) { const filename = String((options == null ? void 0 : options.filename) || "download"); const opt = Object.assign({ newWindow: true }, options || {}, { download: filename }); return (0, import_internalUtils._openUrl)(url, opt); } function downloadBlob(url, options) { const opt = Object.assign({ filename: "download" }, options || {}); const finalUrl = (0, import_biz_utils_common.updateUrl)(url, { query: options == null ? void 0 : options.query }); return new Promise((resolve, reject) => { var _a, _b; const downloadXhr = new XMLHttpRequest(); downloadXhr.open("GET", finalUrl, true); downloadXhr.withCredentials = Boolean((_a = opt == null ? void 0 : opt.xhrOptions) == null ? void 0 : _a.withCredenticals); downloadXhr.responseType = "blob"; const headers = ((_b = opt == null ? void 0 : opt.xhrOptions) == null ? void 0 : _b.headers) || {}; Object.keys(headers).forEach((key) => { downloadXhr.setRequestHeader(key, headers[key]); }); const progressFn = (0, import_internalUtils._ensureFunction)(options == null ? void 0 : options.onProgress); downloadXhr.onprogress = (ev) => { progressFn(ev.total, ev.loaded, ev); }; downloadXhr.onerror = () => { const err = new Error("Download blob failed."); reject(err); }; downloadXhr.onload = () => { if (downloadXhr.status < 200 || downloadXhr.status >= 300) { return reject(new Error(`Download blob failed. status = ${downloadXhr.status}`)); } (0, import_file_saver.saveAs)(downloadXhr.response, String(opt == null ? void 0 : opt.filename)); resolve(true); }; downloadXhr.send(); }); } function copyText(content) { if (!(0, import_internalUtils._isString)(content)) { throw new import_errors.ArgumentError("content must be string.", "content"); } return new Promise((resolve, reject) => { try { const copyBtnEl = document.createElement("button"); copyBtnEl.setAttribute("data-clipboard-text", content); const clipboardIns = new import_clipboard.default(copyBtnEl); clipboardIns.on("success", () => { clipboardIns.destroy(); resolve(); }); clipboardIns.on("error", (err) => { reject(err); }); copyBtnEl.click(); copyBtnEl == null ? void 0 : copyBtnEl.remove(); } catch (ex) { reject(ex); } }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { copyText, download, downloadBlob, getQuery, openUrl });