-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #491 from 10up/feature/417-add-excerpt-generation-…
…to-classic-editor Added Generate Excerpt for the Classic Editor.
- Loading branch information
Showing
7 changed files
with
300 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import '../../scss/openai/classic-editor-title-generator.scss'; | ||
|
||
const classifaiExcerptData = window.classifaiGenerateExcerpt || {}; | ||
|
||
( function ( $ ) { | ||
$( document ).ready( () => { | ||
if ( document.getElementById( 'postexcerpt' ) ) { | ||
generateExcerptInit(); | ||
} | ||
} ); | ||
|
||
/** | ||
* This function is solely responsibe for rendering, generating | ||
* and applying the generated excerpt in the classic editor. | ||
*/ | ||
function generateExcerptInit() { | ||
const excerptContainer = document.getElementById( 'excerpt' ); | ||
|
||
// Boolean indicating whether generation is in progress. | ||
let isProcessing = false; | ||
|
||
// Creates and appends the "Generate excerpt" button. | ||
$( '<span />', { | ||
text: excerptContainer.value | ||
? classifaiExcerptData?.regenerateText ?? '' | ||
: classifaiExcerptData?.buttonText ?? '', | ||
class: 'classifai-openai__excerpt-generate-btn--text', | ||
} ) | ||
.wrap( | ||
'<div class="button" id="classifai-openai__excerpt-generate-btn" />' | ||
) | ||
.parent() | ||
.append( | ||
$( '<span />', { | ||
class: 'classifai-openai__excerpt-generate-btn--spinner', | ||
} ) | ||
) | ||
.insertAfter( excerptContainer ); | ||
|
||
// The current post ID. | ||
const postId = $( '#post_ID' ).val(); | ||
|
||
// Callback to generate the excerpt. | ||
const generateExcerpt = () => { | ||
if ( isProcessing ) { | ||
return; | ||
} | ||
|
||
const generateTextEl = $( | ||
'.classifai-openai__excerpt-generate-btn--text' | ||
); | ||
const spinnerEl = $( | ||
'.classifai-openai__excerpt-generate-btn--spinner' | ||
); | ||
|
||
generateTextEl.css( 'opacity', '0' ); | ||
spinnerEl.show(); | ||
isProcessing = true; | ||
|
||
const path = classifaiExcerptData?.path + postId; | ||
|
||
apiFetch( { | ||
path, | ||
} ).then( ( result ) => { | ||
generateTextEl.css( 'opacity', '1' ); | ||
spinnerEl.hide(); | ||
isProcessing = false; | ||
|
||
$( excerptContainer ).val( result ).trigger( 'input' ); | ||
generateTextEl.text( | ||
classifaiExcerptData?.regenerateText ?? '' | ||
); | ||
} ); | ||
}; | ||
|
||
// Event handler registration to generate the excerpt. | ||
$( document ).on( | ||
'click', | ||
'#classifai-openai__excerpt-generate-btn', | ||
generateExcerpt | ||
); | ||
} | ||
} )( jQuery ); |
Oops, something went wrong.