| 1 |
/** |
| 2 |
* Blockenberg Editor Scripts |
| 3 |
* Common functionality for all blocks |
| 4 |
*/ |
| 5 |
wp.domReady(function() { |
| 6 |
|
| 7 |
/* ============================================= |
| 8 |
Color ToolsPanel → collapsible (PanelBody-style) |
| 9 |
============================================= */ |
| 10 |
function initColorPanelToggles(root) { |
| 11 |
var panels = (root || document).querySelectorAll( |
| 12 |
'.block-editor-panel-color-gradient-settings:not([data-bkbg-collapse-init])' |
| 13 |
); |
| 14 |
panels.forEach(function (panel) { |
| 15 |
panel.setAttribute('data-bkbg-collapse-init', '1'); |
| 16 |
// Start collapsed |
| 17 |
panel.classList.add('bkbg-panel-collapsed'); |
| 18 |
|
| 19 |
var header = panel.querySelector('.components-tools-panel-header'); |
| 20 |
if (!header) return; |
| 21 |
|
| 22 |
header.addEventListener('click', function (e) { |
| 23 |
// Don't toggle when clicking a child interactive element (buttons, inputs) |
| 24 |
if (e.target.closest('button, input, select, .components-dropdown-menu')) return; |
| 25 |
panel.classList.toggle('bkbg-panel-collapsed'); |
| 26 |
}); |
| 27 |
}); |
| 28 |
} |
| 29 |
|
| 30 |
// Re-run whenever the sidebar DOM changes (block selection, tab switch, etc.) |
| 31 |
var colorPanelObserver = new MutationObserver(function () { |
| 32 |
initColorPanelToggles(); |
| 33 |
}); |
| 34 |
colorPanelObserver.observe(document.body, { childList: true, subtree: true }); |
| 35 |
// Initial pass |
| 36 |
initColorPanelToggles(); |
| 37 |
|
| 38 |
// Track global drag state for showing drop zones |
| 39 |
var isDragging = false; |
| 40 |
var currentDropTarget = null; |
| 41 |
|
| 42 |
function addDraggingClass() { |
| 43 |
if (!isDragging) { |
| 44 |
isDragging = true; |
| 45 |
document.body.classList.add('bkbg-is-dragging'); |
| 46 |
|
| 47 |
// Also add to iframe if exists |
| 48 |
var editorCanvas = document.querySelector('iframe[name="editor-canvas"]'); |
| 49 |
if (editorCanvas && editorCanvas.contentDocument) { |
| 50 |
editorCanvas.contentDocument.body.classList.add('bkbg-is-dragging'); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
function removeDraggingClass() { |
| 56 |
isDragging = false; |
| 57 |
document.body.classList.remove('bkbg-is-dragging'); |
| 58 |
|
| 59 |
// Also remove from iframe if exists |
| 60 |
var editorCanvas = document.querySelector('iframe[name="editor-canvas"]'); |
| 61 |
if (editorCanvas && editorCanvas.contentDocument) { |
| 62 |
editorCanvas.contentDocument.body.classList.remove('bkbg-is-dragging'); |
| 63 |
} |
| 64 |
|
| 65 |
// Clear any drop target highlights |
| 66 |
clearDropTargetHighlight(); |
| 67 |
} |
| 68 |
|
| 69 |
function clearDropTargetHighlight() { |
| 70 |
if (currentDropTarget) { |
| 71 |
currentDropTarget.classList.remove('bkbg-drop-target'); |
| 72 |
currentDropTarget = null; |
| 73 |
} |
| 74 |
// Also clear all just in case |
| 75 |
document.querySelectorAll('.bkbg-drop-target').forEach(function(el) { |
| 76 |
el.classList.remove('bkbg-drop-target'); |
| 77 |
}); |
| 78 |
var editorCanvas = document.querySelector('iframe[name="editor-canvas"]'); |
| 79 |
if (editorCanvas && editorCanvas.contentDocument) { |
| 80 |
editorCanvas.contentDocument.querySelectorAll('.bkbg-drop-target').forEach(function(el) { |
| 81 |
el.classList.remove('bkbg-drop-target'); |
| 82 |
}); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
function handleDragOver(e) { |
| 87 |
if (!isDragging) return; |
| 88 |
|
| 89 |
// Find the closest drop zone (appender button, block-list-appender, or layout container) |
| 90 |
var target = e.target; |
| 91 |
var dropZone = target.closest('.block-editor-button-block-appender, .block-list-appender, .bkbg-column, .bkbg-row, .bkbg-section'); |
| 92 |
|
| 93 |
if (dropZone && dropZone !== currentDropTarget) { |
| 94 |
clearDropTargetHighlight(); |
| 95 |
currentDropTarget = dropZone; |
| 96 |
dropZone.classList.add('bkbg-drop-target'); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
function handleDragLeave(e) { |
| 101 |
var target = e.target; |
| 102 |
var dropZone = target.closest('.block-editor-button-block-appender, .block-list-appender, .bkbg-column, .bkbg-row, .bkbg-section'); |
| 103 |
|
| 104 |
if (dropZone && dropZone.classList.contains('bkbg-drop-target')) { |
| 105 |
// Check if we're leaving to a child element |
| 106 |
var relatedTarget = e.relatedTarget; |
| 107 |
if (relatedTarget && dropZone.contains(relatedTarget)) { |
| 108 |
return; // Still inside the drop zone |
| 109 |
} |
| 110 |
dropZone.classList.remove('bkbg-drop-target'); |
| 111 |
if (currentDropTarget === dropZone) { |
| 112 |
currentDropTarget = null; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// Listen for drag events on the document |
| 118 |
document.addEventListener('dragstart', addDraggingClass, true); |
| 119 |
document.addEventListener('dragend', removeDraggingClass, true); |
| 120 |
document.addEventListener('dragover', handleDragOver, true); |
| 121 |
document.addEventListener('dragleave', handleDragLeave, true); |
| 122 |
document.addEventListener('drop', function() { |
| 123 |
// Small delay to ensure drag operations complete |
| 124 |
setTimeout(removeDraggingClass, 100); |
| 125 |
}, true); |
| 126 |
|
| 127 |
// Also listen inside the editor iframe when it loads |
| 128 |
function setupIframeDragListeners() { |
| 129 |
var editorCanvas = document.querySelector('iframe[name="editor-canvas"]'); |
| 130 |
if (editorCanvas && editorCanvas.contentDocument) { |
| 131 |
var doc = editorCanvas.contentDocument; |
| 132 |
doc.addEventListener('dragstart', addDraggingClass, true); |
| 133 |
doc.addEventListener('dragend', removeDraggingClass, true); |
| 134 |
doc.addEventListener('dragover', handleDragOver, true); |
| 135 |
doc.addEventListener('dragleave', handleDragLeave, true); |
| 136 |
doc.addEventListener('drop', function() { |
| 137 |
setTimeout(removeDraggingClass, 100); |
| 138 |
}, true); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// Try to setup iframe listeners after a delay (iframe may not be ready immediately) |
| 143 |
setTimeout(setupIframeDragListeners, 1000); |
| 144 |
setTimeout(setupIframeDragListeners, 3000); |
| 145 |
|
| 146 |
// Also use MutationObserver to detect when iframe is added |
| 147 |
var observer = new MutationObserver(function(mutations) { |
| 148 |
mutations.forEach(function(mutation) { |
| 149 |
if (mutation.addedNodes.length) { |
| 150 |
var editorCanvas = document.querySelector('iframe[name="editor-canvas"]'); |
| 151 |
if (editorCanvas) { |
| 152 |
editorCanvas.addEventListener('load', setupIframeDragListeners); |
| 153 |
} |
| 154 |
} |
| 155 |
}); |
| 156 |
}); |
| 157 |
|
| 158 |
observer.observe(document.body, { childList: true, subtree: true }); |
| 159 |
}); |
| 160 |
|