Exactly, Karl. I'll share the rest of my code in full to give you more insight.
This is in the code in files_galley/js/custom.js:
var observer = new MutationObserver(function(mutationsList, observer) {
mutationsList.forEach(function(mutation) {
if (mutation.type === 'childList' || mutation.type === 'characterData') {
console.log('The content of the div has changed:', mutation);
//document.querySelectorAll(".svg-check-select, .files-a").forEach(function (element) {
document.querySelectorAll(".svg-check-select").forEach(function (element) {
element.removeEventListener("click", handleClickSelect);
element.addEventListener("click", handleClickSelect);
});
}
});
});
// Specifies which types of mutations to observe
var config = { childList: true, subtree: true, characterData: true };
// Starts observation
observer.observe(targetNode, config);
// This function is applied when clicking on selection objects and communicates with TinyMCE
function handleClickSelect(event) {
let clickedElement = event.target; // The element that was clicked
let parentLink = clickedElement.closest("a"); // Finds the closest ancestor <a> element
if (parentLink) {
var fileUrl = parentLink.getAttribute("href");
console.log(fileUrl);
// Communicates the file URL to the TinyMCE window
if (fileUrl) {
// Case when using a browser window as the container for files_gallery
if (window.opener) {
window.opener.postMessage({ fileUrl: fileUrl }, "*");
window.close(); // Closes the file manager after selection
}
// Case when files_gallery is inside a Bootstrap modal
else {
window.parent.postMessage({ fileUrl: fileUrl }, "*");
if (window.parent.$) {
window.parent.$('#fileManagerModal').modal('hide'); // Closes the Bootstrap modal after selection
}
}
}
} else {
console.log("No <a> element found as an ancestor.");
}
}
And this the code from the tinymce side:
tinymce.init({
license_key: 'gpl',
selector: 'textarea',
.......
file_picker_callback: function(callback, value, meta) { // Functionality through Bootstrap modal
var fileManagerUrl = '/path/files_gallery/'; // Path to the file manager
// Set the iframe URL
$('#fileManagerIframe').attr('src', fileManagerUrl);
// Open the Bootstrap modal
$('#fileManagerModal').modal('show');
// Listen for the return message from the file manager
window.addEventListener('message', function(event) {
if (event.data && event.data.fileUrl) {
callback(event.data.fileUrl);
// Close the modal
$('#fileManagerModal').modal('hide');
}
}, { once: true }); // Removes the event listener after the first use
}