membershipworks: Use ClipboardItem API when available in upcoming-events

This commit is contained in:
Adam Goldsmith 2023-12-28 10:41:56 -05:00
parent 275de1e7e7
commit ba913154d6

View File

@ -119,17 +119,35 @@
{% endblock %} {% endblock %}
{% block script %} {% block script %}
<script> <script>
// TODO: This should use the newer Clipboard API, but Firefox doesn't support it yet async function copyToClipboard(event) {
// https://developer.mozilla.org/en-US/docs/Web/API/ClipboardItem const rich = document.getElementById("preview").innerHTML;
function copyToClipboard(event) { const plain = document.getElementById("preview").innerHTML;
const cb = e => {
e.clipboardData.setData("text/html", document.getElementById("preview").innerHTML); // from https://stackoverflow.com/a/77305170
e.clipboardData.setData("text/plain", document.getElementById("preview").innerHTML); if (typeof ClipboardItem !== "undefined") {
e.preventDefault(); // Shiny new Clipboard API, not fully supported in Firefox.
}; // https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API#browser_compatibility
document.addEventListener("copy", cb); const html = new Blob([rich], {
document.execCommand("copy"); type: "text/html"
document.removeEventListener("copy", cb); });
const text = new Blob([plain], {
type: "text/plain"
});
const data = new ClipboardItem({
"text/html": html,
"text/plain": text
});
await navigator.clipboard.write([data]);
} else {
const cb = e => {
e.clipboardData.setData("text/html", rich);
e.clipboardData.setData("text/plain", plain);
e.preventDefault();
};
document.addEventListener("copy", cb);
document.execCommand("copy");
document.removeEventListener("copy", cb);
}
bootstrap.Popover.getInstance(event.target).show(); bootstrap.Popover.getInstance(event.target).show();
setTimeout(() => bootstrap.Popover.getInstance(event.target).hide(), 1000); setTimeout(() => bootstrap.Popover.getInstance(event.target).hide(), 1000);