-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-shortcode-filter.js
More file actions
52 lines (43 loc) · 1.82 KB
/
remove-shortcode-filter.js
File metadata and controls
52 lines (43 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
wp.domReady(() => {
const allowedPlainTextBlocks = [
'core/paragraph',
'core/heading',
'core/quote',
'core/list',
'core/preformatted'
];
wp.data.subscribe(() => {
const { isTyping, getSelectedBlock } = wp.data.select('core/block-editor');
if (!isTyping()) {
const block = getSelectedBlock();
if (block && allowedPlainTextBlocks.includes(block.name)) {
const blockElement = document.querySelector(`[data-block="${block.clientId}"]`);
if (blockElement) {
// Remove any existing paste listener first to avoid duplicates
blockElement.removeEventListener('paste', handlePaste);
// Add the paste event listener
blockElement.addEventListener('paste', handlePaste, { once: true });
}
}
}
});
function handlePaste(event) {
const clipboardData = event.clipboardData.getData('text');
// Check if the pasted content contains a shortcode
if (/\[[^\]]+\]/.test(clipboardData)) {
event.preventDefault();
const selection = window.getSelection();
if (selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
range.deleteContents();
range.insertNode(document.createTextNode(clipboardData));
// Trigger the block content update
const blockId = event.target.closest('[data-block]').dataset.block;
const blockElement = document.querySelector(`[data-block="${blockId}"]`);
wp.data.dispatch('core/block-editor').updateBlockAttributes(blockId, {
content: blockElement.innerText
});
}
}
}
});