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 %} {% block script %}
<script> <script>
async function copyToClipboard(event) { async function copyToClipboard(event) {
const rich = document.getElementById("preview").innerHTML; // https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API
const plain = document.getElementById("preview").innerHTML; const data = new ClipboardItem({
"text/plain": new Blob(
// from https://stackoverflow.com/a/77305170 [document.getElementById("preview").innerHTML], {
if (typeof ClipboardItem !== "undefined") { type: "text/plain"
// 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], { await navigator.clipboard.write([data]);
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);
}
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);