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;
// from https://stackoverflow.com/a/77305170
if (typeof ClipboardItem !== "undefined") {
// Shiny new Clipboard API, not fully supported in Firefox.
// https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API#browser_compatibility
const html = new Blob([rich], {
type: "text/html"
});
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 => { const cb = e => {
e.clipboardData.setData("text/html", document.getElementById("preview").innerHTML); e.clipboardData.setData("text/html", rich);
e.clipboardData.setData("text/plain", document.getElementById("preview").innerHTML); e.clipboardData.setData("text/plain", plain);
e.preventDefault(); e.preventDefault();
}; };
document.addEventListener("copy", cb); document.addEventListener("copy", cb);
document.execCommand("copy"); document.execCommand("copy");
document.removeEventListener("copy", cb); 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);