UNPKG

@ayonli/jsext

Version:

A JavaScript extension package for building strong and modern applications.

104 lines (100 loc) 3.67 kB
'use strict'; var dialog_progress = require('./dialog/progress.js'); var dialog_file = require('./dialog/file.js'); var env = require('./env.js'); /** * Asynchronous dialog functions for both browsers and terminals. * * This includes `alert`, `confirm`, `prompt` and other non-standard dialogs. * @experimental * @module */ /** * Displays a dialog with a message, and to wait until the user dismisses the * dialog. * * @example * ```ts * import { alert } from "@ayonli/jsext/dialog"; * * await alert("Hello, world!"); * ``` */ async function alert(message, options = {}) { if (env.isBrowserWindow) { const { alertInBrowser } = await Promise.resolve().then(function () { return require('./dialog/browser/index.js'); }); await alertInBrowser(message); } else if (env.isDeno || env.isNodeLike) { const { default: alertInTerminal } = await Promise.resolve().then(function () { return require('./dialog/terminal/alert.js'); }); await alertInTerminal(message, options); } else { throw new Error("Unsupported runtime"); } } /** * Displays a dialog with a message, and to wait until the user either confirms * or cancels the dialog. * * @example * ```ts * import { confirm } from "@ayonli/jsext/dialog"; * * if (await confirm("Are you sure?")) { * console.log("Confirmed"); * } else { * console.log("Canceled"); * } * ``` */ async function confirm(message, options = {}) { if (env.isBrowserWindow) { const { confirmInBrowser } = await Promise.resolve().then(function () { return require('./dialog/browser/index.js'); }); return await confirmInBrowser(message); } else if (env.isDeno || env.isNodeLike) { const { default: confirmInTerminal } = await Promise.resolve().then(function () { return require('./dialog/terminal/confirm.js'); }); return await confirmInTerminal(message, options); } else { throw new Error("Unsupported runtime"); } } async function prompt(message, options = "") { var _a, _b, _c; const defaultValue = typeof options === "string" ? options : options.defaultValue; const type = typeof options === "object" ? (_a = options.type) !== null && _a !== void 0 ? _a : "text" : "text"; const mask = type === "password" ? typeof options === "object" ? ((_b = options.mask) !== null && _b !== void 0 ? _b : "*") : "*" : undefined; const gui = typeof options === "object" ? ((_c = options.gui) !== null && _c !== void 0 ? _c : false) : false; if (env.isBrowserWindow) { const { promptInBrowser } = await Promise.resolve().then(function () { return require('./dialog/browser/index.js'); }); return await promptInBrowser(message, { type, defaultValue }); } else if (env.isDeno || env.isNodeLike) { const { default: promptInTerminal } = await Promise.resolve().then(function () { return require('./dialog/terminal/prompt.js'); }); return await promptInTerminal(message, { defaultValue, type, mask, gui }); } else { throw new Error("Unsupported runtime"); } } exports.progress = dialog_progress.default; exports.downloadFile = dialog_file.downloadFile; exports.openDirectory = dialog_file.openDirectory; exports.openFile = dialog_file.openFile; exports.openFiles = dialog_file.openFiles; exports.pickDirectory = dialog_file.pickDirectory; exports.pickFile = dialog_file.pickFile; exports.pickFiles = dialog_file.pickFiles; exports.saveFile = dialog_file.saveFile; exports.alert = alert; exports.confirm = confirm; exports.prompt = prompt; //# sourceMappingURL=dialog.js.map