@formant/ava-react
Version:
React components of AVA.
79 lines (77 loc) • 2.82 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);
// src/NarrativeTextVis/chore/exporter/helpers/copy.ts
var copy_exports = {};
__export(copy_exports, {
copyToClipboard: () => copyToClipboard,
getSelectionContentForCopy: () => getSelectionContentForCopy
});
module.exports = __toCommonJS(copy_exports);
var import_transformHtml = require("./transformHtml");
function execCopyCommand(htmlStr, plainText, onSuccess, onError) {
const listener = (e) => {
var _a, _b;
try {
(_a = e.clipboardData) == null ? void 0 : _a.setData("text/html", htmlStr);
(_b = e.clipboardData) == null ? void 0 : _b.setData("text/plain", plainText || htmlStr);
e.preventDefault();
onSuccess == null ? void 0 : onSuccess();
} catch (err) {
onError == null ? void 0 : onError();
}
};
document.addEventListener("copy", listener);
document.execCommand("copy");
document.removeEventListener("copy", listener);
}
function copyToClipboard(htmlString, plainText, onSuccess, onError) {
try {
const html = new Blob([htmlString], { type: "text/html" });
const text = new Blob([plainText || htmlString], { type: "text/plain" });
const dataForCopy = [
new window.ClipboardItem({
"text/html": html,
"text/plain": text
})
];
navigator.clipboard.write(dataForCopy).then(() => {
onSuccess == null ? void 0 : onSuccess();
});
} catch (err) {
execCopyCommand(htmlString, plainText, onSuccess, onError);
}
}
async function getSelectionContentForCopy() {
var _a;
const doms = (_a = window.getSelection()) == null ? void 0 : _a.getRangeAt(0).cloneContents();
const container = document.createElement("div");
if (doms) {
container.appendChild(doms);
}
const plainText = container.innerText;
const transformedHtml = await (0, import_transformHtml.transformHtml)({ elements: [container] });
return {
plainText,
html: transformedHtml
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
copyToClipboard,
getSelectionContentForCopy
});