document.addEventListener("DOMContentLoaded", function() { var modal = document.getElementById('softaculous-pro-photopea-modal'); var iframe = document.getElementById('softaculous-pro-photopea-iframe'); var saveBtn = document.getElementById('softaculous-pro-save-photopea'); var saveCloseBtn = document.getElementById('softaculous-pro-save-close-photopea'); var cancelBtn = document.getElementById('softaculous-pro-cancel-photopea'); var saveAsBtn = document.getElementById('softaculous-pro-save-as-photopea'); var qualityInput = document.getElementById('softaculous-pro-quality-input'); var qualityInputContainer = document.getElementById('softaculous-pro-quality-input-container'); var imageUrl = null; // Cache the image URL to prevent repeated fetch requests function openPhotopea() { event.preventDefault(); // Prevent form submission if (!imageUrl) { imageUrl = this.getAttribute('data-image-url'); iframe.src = ''; var customIO = { save: 'app.echoToOE("Save");' }; var photopeaConfig = { files: [], environment: { lang: "en", customIO: customIO } } const allowedTypes = [ 'image/jpeg', 'image/png', 'image/gif', 'image/svg+xml', 'image/x-ms-bmp', 'image/tiff', 'image/webp', 'image/vnd.adobe.photoshop', 'application/pdf', 'image/x-portable-pixmap', 'image/x-icon', 'image/vnd-ms.dds' ]; // Fetch the image and convert it to base64 fetch(imageUrl) .then(response => { const contentType = response.headers.get('Content-Type'); if (!allowedTypes.includes(contentType)) { throw new Error(`Unsupported image type: ${contentType}`); } const qualityAllowedTypes = ['image/jpeg', 'image/png', 'image/webp']; if (qualityAllowedTypes.includes(contentType)) { qualityInputContainer.style.display = 'block'; } else { qualityInputContainer.style.display = 'none'; } return response.arrayBuffer(); }) .then(arrayBuffer => { var blob = new Blob([arrayBuffer]); var objectURL = URL.createObjectURL(blob); // Convert ArrayBuffer to a URL and pass to Photopea iframe.src = ''; var photopeaUrl = 'https://www.photopea.com/#' + encodeURIComponent(JSON.stringify(photopeaConfig)); // Send the ArrayBuffer to Photopea using postMessage iframe.onload = function() { iframe.contentWindow.postMessage(arrayBuffer, '*'); }; iframe.src = photopeaUrl; modal.style.display = 'block'; }) .catch(error => console.error('Error loading image:', error)); } } // Add event listener for Photopea button in both List View and Attachment Modal document.addEventListener('click', function(event) { if (event.target && event.target.id === 'edit_with_photopea') { event.preventDefault(); var photopeaButton = event.target; // Ensure photopeaButton exists and has the data-image-url attribute if (photopeaButton && photopeaButton.getAttribute) { var imageUrl = photopeaButton.getAttribute('data-image-url'); openPhotopea.call(photopeaButton); } else { console.error('Photopea button or image URL not found.'); } } }); function closePhotopea() { modal.style.display = 'none'; // Hide the modal iframe.src = ''; // Clear the iframe source imageUrl = null; // Reset the image URL setTimeout(function() { const imgElement = document.querySelector('.wp_attachment_image img'); if (imgElement) { imgElement.src = imgElement.src.split('?')[0] + '?v=' + new Date().getTime(); } const imgElement2 = document.querySelector('.details-image'); if (imgElement2) { imgElement2.src = imgElement2.src.split('?')[0] + '?v=' + new Date().getTime(); } }, 2000); } cancelBtn.addEventListener('click', function() { closePhotopea() }); saveBtn.addEventListener('click', function() { photopeaButton = document.querySelector('#edit_with_photopea'); // Dynamically query the button var imageUrl = photopeaButton.getAttribute('data-image-url'); var fileExtension = imageUrl ? imageUrl.split('.').pop() : 'jpg'; const origin = 'https://www.photopea.com'; if (iframe && iframe.contentWindow) { iframe.contentWindow.postMessage('app.activeDocument.saveToOE("' + fileExtension + '")', origin); } else { console.error('iframe or its contentWindow is undefined'); } }); saveCloseBtn.addEventListener('click', function(event) { photopeaButton = document.querySelector('#edit_with_photopea'); // Dynamically query the button var imageUrl = photopeaButton.getAttribute('data-image-url'); var fileExtension = imageUrl ? imageUrl.split('.').pop() : 'jpg'; const origin = 'https://www.photopea.com'; if (iframe && iframe.contentWindow) { iframe.contentWindow.postMessage('app.activeDocument.saveToOE("' + fileExtension + '")', origin); } else { console.error('iframe or its contentWindow is undefined'); } function handlePhotopeaMessage(event) { if (event.origin === origin) { const data = typeof event.data === 'string' ? event.data : ''; if (data.includes('done')) { closePhotopea(); window.removeEventListener('message', handlePhotopeaMessage); } } } window.addEventListener('message', handlePhotopeaMessage); }); saveAsBtn.addEventListener('click', function() { photopeaButton = document.querySelector('#edit_with_photopea'); // Dynamically query the button modal.style.display = 'none'; var imageUrl = photopeaButton.getAttribute('data-image-url'); var baseFileName = imageUrl ? imageUrl.substring(imageUrl.lastIndexOf('/') + 1, imageUrl.lastIndexOf('.')) : 'image'; var fileExtension = imageUrl ? imageUrl.split('.').pop() : 'jpg'; const origin = 'https://www.photopea.com'; function photopea_generate_randname(baseFileName, fileExtension) { return new Promise((resolve) => { const chars = 'abcdefghijklmnopqrstuvwxyz0123456789'; let suffix = ''; for (let i = 0; i < 5; i++) { suffix += chars.charAt(Math.floor(Math.random() * chars.length)); } resolve(baseFileName + '-' + suffix + '.' + fileExtension); }); } // Check for an existing file name photopea_generate_randname(baseFileName, fileExtension).then(uniqueFileName => { var saveAsModalHtml = `