one
Version:
One is a React framework for web, iOS, and Android apps.
112 lines (109 loc) • 3.32 kB
JavaScript
import { NitroModules } from "react-native-nitro-modules";
import { rethrowNativeError } from "../nativeError.native.js";
import { assertGetImageOptions, assertGetStringOptions, assertListener, assertSetImage, assertSetStringOptions, assertSetStringText, assertSetUrl } from "./validate.native.js";
var hybrid;
function native() {
hybrid !== null && hybrid !== void 0 ? hybrid : hybrid = NitroModules.createHybridObject("OneClipboard");
return hybrid;
}
function getString(options) {
assertGetStringOptions(options, "getString");
var nativeOptions = {};
if ((options === null || options === void 0 ? void 0 : options.preferredFormat) !== void 0) {
nativeOptions.preferredFormat = options.preferredFormat;
}
return native().getString(nativeOptions).catch(rethrowNativeError);
}
function setString(text, options) {
assertSetStringText(text);
assertSetStringOptions(options, "setString");
var nativeOptions = {};
if ((options === null || options === void 0 ? void 0 : options.inputFormat) !== void 0) {
nativeOptions.inputFormat = options.inputFormat;
}
return native().setString(text, nativeOptions).catch(rethrowNativeError);
}
function hasString() {
return native().hasString().catch(rethrowNativeError);
}
function getImage(options) {
assertGetImageOptions(options);
var nativeOptions = { format: options.format };
if (options.jpegQuality !== void 0) {
nativeOptions.jpegQuality = options.jpegQuality;
}
return native().getImage(nativeOptions).catch(rethrowNativeError).then(function(result) {
return result ? {
data: result.data,
size: {
width: result.width,
height: result.height
}
} : null;
});
}
function setImage(base64Image) {
assertSetImage(base64Image);
return native().setImage(base64Image).catch(rethrowNativeError);
}
function hasImage() {
return native().hasImage().catch(rethrowNativeError);
}
function getUrl() {
return native().getUrl().catch(rethrowNativeError).then(function(url) {
return url !== null && url !== void 0 ? url : null;
});
}
function setUrl(url) {
assertSetUrl(url);
return native().setUrl(url).catch(rethrowNativeError);
}
function hasUrl() {
return native().hasUrl().catch(rethrowNativeError);
}
var listeners = /* @__PURE__ */ new Set();
function toContentType(value) {
if (value === "plain-text" || value === "image" || value === "url" || value === "html") {
return value;
}
return null;
}
function addListener(listener) {
assertListener(listener);
if (listeners.size === 0) {
native().setClipboardListener(function(contentTypes) {
var event = { contentTypes: contentTypes.flatMap(function(type) {
var mapped = toContentType(type);
return mapped ? [mapped] : [];
}) };
listeners.forEach(function(entry) {
return entry.callback(event);
});
});
}
var subscription = { callback: listener };
listeners.add(subscription);
var removed = false;
return { remove: function remove() {
if (removed) return;
removed = true;
listeners.delete(subscription);
if (listeners.size === 0) {
hybrid === null || hybrid === void 0 ? void 0 : hybrid.clearClipboardListener();
}
} };
}
var Clipboard = Object.freeze({
getString,
setString,
hasString,
getImage,
setImage,
hasImage,
getUrl,
setUrl,
hasUrl,
addListener
});
export { Clipboard };
//# sourceMappingURL=index.native.js.map