Skip to content

Commit

Permalink
Merge pull request #626 from 10up/enhancement/590
Browse files Browse the repository at this point in the history
Add post autosave for title generation and content resizing.
  • Loading branch information
dkotter authored Dec 7, 2023
2 parents 7e900b0 + f0e5025 commit 1e4d2a0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/js/gutenberg-plugins/content-resizing-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ const ContentResizingPlugin = () => {
*
* @param {string} updateWith The content that will be used to replace the selection.
*/
function updateContent( updateWith ) {
async function updateContent( updateWith ) {
const isDirty = await select( 'core/editor' ).isEditedPostDirty();
const postId = select( 'core/editor' ).getCurrentPostId();
const postType = select( 'core/editor' ).getCurrentPostType();

dispatch( blockEditorStore ).updateBlockAttributes(
selectedBlock.clientId,
{
Expand All @@ -237,6 +241,15 @@ const ContentResizingPlugin = () => {
updateWith.length
);
resetStates();

// If no edited values in post trigger save.
if ( ! isDirty ) {
await dispatch( 'core' ).saveEditedEntityRecord(
'postType',
postType,
postId
);
}
}

// We don't want to use the reszing feature when multiple blocks are selected.
Expand Down
16 changes: 15 additions & 1 deletion src/js/gutenberg-plugins/post-status-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const PostStatusInfo = () => {
}

const postId = select( 'core/editor' ).getCurrentPostId();
const postType = select( 'core/editor' ).getCurrentPostType();
const postContent =
select( 'core/editor' ).getEditedPostAttribute( 'content' );
const openModal = () => setOpen( true );
Expand Down Expand Up @@ -85,11 +86,24 @@ const PostStatusInfo = () => {
</textarea>
<Button
variant="secondary"
onClick={ () => {
onClick={ async () => {
const isDirty =
select(
'core/editor'
).isEditedPostDirty();
dispatch( 'core/editor' ).editPost( {
title: data[ i ],
} );
closeModal();
if ( ! isDirty ) {
await dispatch(
'core'
).saveEditedEntityRecord(
'postType',
postType,
postId
);
}
} }
>
{ __( 'Select', 'classifai' ) }
Expand Down
22 changes: 21 additions & 1 deletion src/js/openai/classic-editor-title-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ const scriptData = classifaiChatGPTData.enabledFeatures.reduce(
}
} );

/**
* Returns whether the post has unsaved changes or not.
*
* @return {boolean} Whether the post has unsaved change or not.
*/
function isPostChanged() {
const editor = window.tinymce && window.tinymce.get( 'content' );
let changed = false;

if ( wp.autosave ) {
changed = wp.autosave.server.postChanged();
} else if ( editor ) {
changed = ! editor.isHidden() && editor.isDirty();
}
return changed;
}

/**
* This function is solely responsible for rendering, generating
* and applying the generated title for the classic editor.
Expand Down Expand Up @@ -57,8 +74,11 @@ const scriptData = classifaiChatGPTData.enabledFeatures.reduce(
const textarea = selectBtnEl
.closest( '.classifai-openai__result-item' )
.find( 'textarea' );

const isDirty = isPostChanged();
$( '#title' ).val( textarea.val() ).trigger( 'input' );
if ( ! isDirty && wp.autosave ) {
wp.autosave.server.triggerSave();
}
hidePopup();
};

Expand Down

0 comments on commit 1e4d2a0

Please sign in to comment.