You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have a requirement from our customer that the Paste and Drop events have a very particular behavior on the wp-admin when editing posts. They should only add one paragraph of content in text only format.
I have created js event handler functions that work fine on regular html:
function dropOneTextLineEventHandler(e) {
e.preventDefault();
let text = e.dataTransfer.getData("text/plain");
e.target.innerText = getFirstParagraph(text);
}
function pasteOneTextLineEventHandler(e) {
replaceClipboardContent()
.then((_) => {
return;
})
.catch((error) => console.error(error));
}
In the case of paste I am using the Clipboard API to replace the clipboard contents with only one paragraph in text format before the event bubbles up:
async function replaceClipboardContent() {
let text = await navigator.clipboard.readText();
text = getFirstParagraph(text);
await navigator.clipboard.writeText(text);
}
And I have an even simpler function to extract the first paragraph from text:
I also tried setting up a global paste event listener on the root container for the editor, as it seems users can paste things directly in the editor, and not directly on existing blocks:
function setGlobalEditorPasteEventListener() {
var gutenbergContainer = document.querySelector(".is-root-container");
gutenbergContainer.removeEventListener(
"paste",
pasteOneTextLineEventHandler
);
gutenbergContainer.addEventListener("paste", pasteOneTextLineEventHandler);
}
I also tried, with partial success, to subscribe to the wp.data events, and manually add the event listeners to the element being created, but since the element is not rendered yet, I had to add a timeout to run the code in the future, a solution that lacks elegance:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
We have a requirement from our customer that the Paste and Drop events have a very particular behavior on the wp-admin when editing posts. They should only add one paragraph of content in text only format.
I have created js event handler functions that work fine on regular html:
In the case of paste I am using the Clipboard API to replace the clipboard contents with only one paragraph in text format before the event bubbles up:
And I have an even simpler function to extract the first paragraph from text:
On the Gutenberg side I am struggling to add the event listeners using the React components.
I have tried adding them by using a Higher Order Component on top of the Block Editors but that does not seem to work:
I also tried setting up a global paste event listener on the root container for the editor, as it seems users can paste things directly in the editor, and not directly on existing blocks:
I also tried, with partial success, to subscribe to the wp.data events, and manually add the event listeners to the element being created, but since the element is not rendered yet, I had to add a timeout to run the code in the future, a solution that lacks elegance:
I feel there is a simpler way to add the event listeners to all blocks (or at least the ones where users can paste text), but I could not find it.
Beta Was this translation helpful? Give feedback.
All reactions