one
Version:
One is a React framework for web, iOS, and Android apps.
71 lines (70 loc) • 2.79 kB
JavaScript
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);
var share_exports = {};
__export(share_exports, { Share: () => Share });
module.exports = __toCommonJS(share_exports);
var import_unavailable = require("./unavailable.cjs");
let busy = false;
async function share(items) {
if (typeof window === "undefined" || !navigator.share) return import_unavailable.Share.share(items);
if (busy) throw new Error("Share.share: a share sheet is already open");
if (!items.length) throw new Error("Share.share: at least one item is required");
busy = true;
try {
const data = {}, text = [], files = [];
for (const item of items) {
if (item.type === "text") {
if (!item.value.trim()) throw new Error("Share.share: text cannot be empty");
text.push(item.value);
} else if (item.type === "url") {
const url = new URL(item.value);
if (url.protocol === "file:") throw new Error("Share.share: expected a browser URL");
if (data.url) throw new Error("Share.share: the browser supports one URL per share");
data.url = url.href;
} else if (item.type === "file") {
const url = new URL(item.value);
if (![
"blob:",
"data:",
"https:",
"http:"
].includes(url.protocol)) throw new Error("Share.share: expected a browser file URL");
const response = await fetch(url);
if (!response.ok) throw new Error("Share.share: file could not be read");
const blob = await response.blob();
files.push(new File([blob], decodeURIComponent(url.pathname.split("/").at(-1) || "file"), { type: blob.type }));
} else throw new Error("Share.share: invalid item type");
}
if (text.length) data.text = text.join("\n");
if (files.length) data.files = files;
if (navigator.canShare && !navigator.canShare(data)) throw new Error("Share.share: these items cannot be shared");
try {
await navigator.share(data);
return { completed: true };
} catch (error) {
if (error.name === "AbortError") return { completed: false };
throw error;
}
} finally {
busy = false;
}
}
const Share = Object.freeze({ share });