| 1 |
/** |
| 2 |
* Blockenberg Editor Scripts |
| 3 |
* Common functionality for all blocks |
| 4 |
*/ |
| 5 |
wp.domReady(function() { |
| 6 |
|
| 7 |
// Track global drag state for showing drop zones |
| 8 |
var isDragging = false; |
| 9 |
var currentDropTarget = null; |
| 10 |
|
| 11 |
function addDraggingClass() { |
| 12 |
if (!isDragging) { |
| 13 |
isDragging = true; |
| 14 |
document.body.classList.add('bkbg-is-dragging'); |
| 15 |
|
| 16 |
// Also add to iframe if exists |
| 17 |
var editorCanvas = document.querySelector('iframe[name="editor-canvas"]'); |
| 18 |
if (editorCanvas && editorCanvas.contentDocument) { |
| 19 |
editorCanvas.contentDocument.body.classList.add('bkbg-is-dragging'); |
| 20 |
} |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
function removeDraggingClass() { |
| 25 |
isDragging = false; |
| 26 |
document.body.classList.remove('bkbg-is-dragging'); |
| 27 |
|
| 28 |
// Also remove from iframe if exists |
| 29 |
var editorCanvas = document.querySelector('iframe[name="editor-canvas"]'); |
| 30 |
if (editorCanvas && editorCanvas.contentDocument) { |
| 31 |
editorCanvas.contentDocument.body.classList.remove('bkbg-is-dragging'); |
| 32 |
} |
| 33 |
|
| 34 |
// Clear any drop target highlights |
| 35 |
clearDropTargetHighlight(); |
| 36 |
} |
| 37 |
|
| 38 |
function clearDropTargetHighlight() { |
| 39 |
if (currentDropTarget) { |
| 40 |
currentDropTarget.classList.remove('bkbg-drop-target'); |
| 41 |
currentDropTarget = null; |
| 42 |
} |
| 43 |
// Also clear all just in case |
| 44 |
document.querySelectorAll('.bkbg-drop-target').forEach(function(el) { |
| 45 |
el.classList.remove('bkbg-drop-target'); |
| 46 |
}); |
| 47 |
var editorCanvas = document.querySelector('iframe[name="editor-canvas"]'); |
| 48 |
if (editorCanvas && editorCanvas.contentDocument) { |
| 49 |
editorCanvas.contentDocument.querySelectorAll('.bkbg-drop-target').forEach(function(el) { |
| 50 |
el.classList.remove('bkbg-drop-target'); |
| 51 |
}); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
function handleDragOver(e) { |
| 56 |
if (!isDragging) return; |
| 57 |
|
| 58 |
// Find the closest drop zone (appender button, block-list-appender, or layout container) |
| 59 |
var target = e.target; |
| 60 |
var dropZone = target.closest('.block-editor-button-block-appender, .block-list-appender, .bkbg-column, .bkbg-row, .bkbg-section'); |
| 61 |
|
| 62 |
if (dropZone && dropZone !== currentDropTarget) { |
| 63 |
clearDropTargetHighlight(); |
| 64 |
currentDropTarget = dropZone; |
| 65 |
dropZone.classList.add('bkbg-drop-target'); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
function handleDragLeave(e) { |
| 70 |
var target = e.target; |
| 71 |
var dropZone = target.closest('.block-editor-button-block-appender, .block-list-appender, .bkbg-column, .bkbg-row, .bkbg-section'); |
| 72 |
|
| 73 |
if (dropZone && dropZone.classList.contains('bkbg-drop-target')) { |
| 74 |
// Check if we're leaving to a child element |
| 75 |
var relatedTarget = e.relatedTarget; |
| 76 |
if (relatedTarget && dropZone.contains(relatedTarget)) { |
| 77 |
return; // Still inside the drop zone |
| 78 |
} |
| 79 |
dropZone.classList.remove('bkbg-drop-target'); |
| 80 |
if (currentDropTarget === dropZone) { |
| 81 |
currentDropTarget = null; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// Listen for drag events on the document |
| 87 |
document.addEventListener('dragstart', addDraggingClass, true); |
| 88 |
document.addEventListener('dragend', removeDraggingClass, true); |
| 89 |
document.addEventListener('dragover', handleDragOver, true); |
| 90 |
document.addEventListener('dragleave', handleDragLeave, true); |
| 91 |
document.addEventListener('drop', function() { |
| 92 |
// Small delay to ensure drag operations complete |
| 93 |
setTimeout(removeDraggingClass, 100); |
| 94 |
}, true); |
| 95 |
|
| 96 |
// Also listen inside the editor iframe when it loads |
| 97 |
function setupIframeDragListeners() { |
| 98 |
var editorCanvas = document.querySelector('iframe[name="editor-canvas"]'); |
| 99 |
if (editorCanvas && editorCanvas.contentDocument) { |
| 100 |
var doc = editorCanvas.contentDocument; |
| 101 |
doc.addEventListener('dragstart', addDraggingClass, true); |
| 102 |
doc.addEventListener('dragend', removeDraggingClass, true); |
| 103 |
doc.addEventListener('dragover', handleDragOver, true); |
| 104 |
doc.addEventListener('dragleave', handleDragLeave, true); |
| 105 |
doc.addEventListener('drop', function() { |
| 106 |
setTimeout(removeDraggingClass, 100); |
| 107 |
}, true); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// Try to setup iframe listeners after a delay (iframe may not be ready immediately) |
| 112 |
setTimeout(setupIframeDragListeners, 1000); |
| 113 |
setTimeout(setupIframeDragListeners, 3000); |
| 114 |
|
| 115 |
// Also use MutationObserver to detect when iframe is added |
| 116 |
var observer = new MutationObserver(function(mutations) { |
| 117 |
mutations.forEach(function(mutation) { |
| 118 |
if (mutation.addedNodes.length) { |
| 119 |
var editorCanvas = document.querySelector('iframe[name="editor-canvas"]'); |
| 120 |
if (editorCanvas) { |
| 121 |
editorCanvas.addEventListener('load', setupIframeDragListeners); |
| 122 |
} |
| 123 |
} |
| 124 |
}); |
| 125 |
}); |
| 126 |
|
| 127 |
observer.observe(document.body, { childList: true, subtree: true }); |
| 128 |
}); |
| 129 |
|