Created
January 20, 2025 16:28
-
-
Save karamansky/a7f629a4838d3ad8193d290bf06e7870 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wp.domReady(() => { | |
const deleteBlockQuestion = "Are you sure to delete this block?"; | |
document.addEventListener("click", (event) => { | |
const deleteButton = event.target.closest('.components-button.components-menu-item__button'); | |
if ( deleteButton && ( deleteButton.textContent.includes("Delete") || deleteButton.textContent.includes("Remove") ) ) { | |
const userConfirmed = confirm( deleteBlockQuestion ) ; | |
const { dispatch, select } = wp.data; | |
const blockId = select("core/block-editor").getSelectedBlockClientId(); | |
if ( blockId ) { | |
if ( ! userConfirmed ) { | |
event.stopImmediatePropagation(); | |
event.stopPropagation(); | |
event.preventDefault(); | |
} else { | |
dispatch("core/block-editor").removeBlock(blockId); | |
} | |
} | |
} | |
}, true); | |
//Shift+Alt+Z | |
document.addEventListener("keydown", function(event) { | |
if ( (event.shiftKey && event.altKey && event.code === "KeyZ") || event.code === "Delete" || event.code === "Backspace" ) { //46 - delete, 8 - backspace | |
event.stopImmediatePropagation(); | |
event.stopPropagation(); | |
event.preventDefault(); | |
const { dispatch, select } = wp.data; | |
const selectedBlock = select("core/block-editor").getSelectedBlockClientId(); | |
if ( !selectedBlock ) return; | |
if ( confirm( deleteBlockQuestion ) ) { | |
dispatch("core/block-editor").removeBlock(selectedBlock); | |
} | |
} | |
}, true); // 'true' - event catch before WordPress | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment