Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Generate/regenerate/rescan buttons do not work in media modal when editing a post #295

Merged
merged 5 commits into from
Aug 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 58 additions & 44 deletions src/js/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,63 +44,77 @@
);
};

$( document ).ready( function() {
if ( wp.media.frame ) {
wp.media.frame.on( 'edit:attachment', () => {

const altTagsButton = document.getElementById( 'classifai-rescan-alt-tags' );
const imageTagsButton = document.getElementById( 'classifai-rescan-image-tags' );
const ocrScanButton = document.getElementById( 'classifai-rescan-ocr' );
const smartCropButton = document.getElementById( 'classifai-rescan-smart-crop' );
/**
* Handle click events for Image Processing buttons added to media modal.
*/
const handleButtonsClick = () => {
const altTagsButton = document.getElementById( 'classifai-rescan-alt-tags' );
const imageTagsButton = document.getElementById( 'classifai-rescan-image-tags' );
const ocrScanButton = document.getElementById( 'classifai-rescan-ocr' );
const smartCropButton = document.getElementById( 'classifai-rescan-smart-crop' );

if ( altTagsButton ) {
altTagsButton.addEventListener( 'click', e => handleClick(
{
button: e.target,
endpoint: '/classifai/v1/alt-tags/',
callback: resp => {
if ( resp ) {
const textField = document.getElementById( 'attachment-details-two-column-alt-text' );
textField.value = resp;
}
if ( altTagsButton ) {
altTagsButton.addEventListener( 'click', e => handleClick(
{
button: e.target,
endpoint: '/classifai/v1/alt-tags/',
callback: resp => {
if ( resp ) {
const textField = document.getElementById( 'attachment-details-two-column-alt-text' ) ?? document.getElementById( 'attachment-details-alt-text' );
if ( textField ) {
textField.value = resp;
}
}
) );
}
}
) );
}

if ( imageTagsButton ) {
imageTagsButton.addEventListener( 'click', e => handleClick(
{
button: e.target,
endpoint: '/classifai/v1/image-tags/'
}
) );
if ( imageTagsButton ) {
imageTagsButton.addEventListener( 'click', e => handleClick(
{
button: e.target,
endpoint: '/classifai/v1/image-tags/'
}
) );
}

if ( ocrScanButton ) {
ocrScanButton.addEventListener( 'click', e => handleClick(
{
button: e.target,
endpoint: '/classifai/v1/ocr/',
callback: resp => {
if ( resp ) {
const textField = document.getElementById( 'attachment-details-two-column-description' );
textField.value = resp;
}
if ( ocrScanButton ) {
ocrScanButton.addEventListener( 'click', e => handleClick(
{
button: e.target,
endpoint: '/classifai/v1/ocr/',
callback: resp => {
if ( resp ) {
const textField = document.getElementById( 'attachment-details-two-column-description' ) ?? document.getElementById( 'attachment-details-description' );
if ( textField ) {
textField.value = resp;
}
}
) );
}
}
) );
}

if ( smartCropButton ) {
smartCropButton.addEventListener( 'click', e => handleClick(
{
button: e.target,
endpoint: '/classifai/v1/smart-crop/'
}
) );
if ( smartCropButton ) {
smartCropButton.addEventListener( 'click', e => handleClick(
{
button: e.target,
endpoint: '/classifai/v1/smart-crop/'
}
) );
}
};

$( document ).ready( function() {
if ( wp.media ) {
wp.media.view.Modal.prototype.on( 'open', function() {
wp.media.frame.on( 'selection:toggle', handleButtonsClick );
} );
}

if ( wp.media.frame ) {
wp.media.frame.on( 'edit:attachment', handleButtonsClick );
}
} );
} )( jQuery ) ;