membershipworks: Simplify WordPress post generator clipboard JS
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:
parent
8689d14fc1
commit
b8070e48d7
@ -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"
|
||||
});
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API
|
||||
const data = new ClipboardItem({
|
||||
"text/html": html,
|
||||
"text/plain": text
|
||||
"text/plain": new Blob(
|
||||
[document.getElementById("preview").innerHTML], {
|
||||
type: "text/plain"
|
||||
})
|
||||
});
|
||||
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();
|
||||
setTimeout(() => bootstrap.Popover.getInstance(event.target).hide(), 1000);
|
||||
|
Loading…
Reference in New Issue
Block a user