membershipworks: Simplify WordPress post generator clipboard JS
All checks were successful
Ruff / ruff (push) Successful in 28s
Test / test (push) Successful in 5m32s

Firefox supports the newer Clipboard API now, and WordPress actually
seems to behave better with just `text/plain`. `text/html` was causing
annoying behaviour by wrapping the raw html with `html` and `body`
tags, which confused WordPress.
This commit is contained in:
Adam Goldsmith 2024-07-19 00:09:06 -04:00
parent 8689d14fc1
commit b8070e48d7

View File

@ -139,34 +139,14 @@
{% block script %}
<script>
async function copyToClipboard(event) {
const rich = document.getElementById("preview").innerHTML;
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 => {
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);
}
// https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API
const data = new ClipboardItem({
"text/plain": new Blob(
[document.getElementById("preview").innerHTML], {
type: "text/plain"
})
});
await navigator.clipboard.write([data]);
bootstrap.Popover.getInstance(event.target).show();
setTimeout(() => bootstrap.Popover.getInstance(event.target).hide(), 1000);