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

Add post autosave for title generation and content resizing. #626

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
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
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 @@ -153,7 +153,7 @@
setIsModalOpen( true );
} )();
}
}, [ isResizing, selectedBlock ] );

Check warning on line 156 in src/js/gutenberg-plugins/content-resizing-plugin.js

View workflow job for this annotation

GitHub Actions / eslint

React Hook useEffect has a missing dependency: 'getResizedContent'. Either include it or remove the dependency array

// Resets to default states.
function resetStates() {
Expand Down Expand Up @@ -222,7 +222,11 @@
*
* @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 @@
updateWith.length
);
resetStates();

// If no edited values in post trigger save.
if ( ! isDirty ) {
await dispatch( 'core' ).saveEditedEntityRecord(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this trigger an autosave or a normal save? Or maybe in Gutenberg it's the same thing? I'm seeing the saveEntityRecord function has an isAutosave option but don't see the same in this function, so just want to ensure we're using the right thing here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this trigger an autosave or a normal save?

@dkotter this is a normal save. As Gutenberg provides information on whether the post is dirty or not, we can make sure that the post doesn't contain any other changes than our changes, we are good to trigger the normal save here. Please let me know if you think we should trigger autosave here instead of the normal save.

Or maybe in Gutenberg it's the same thing?

Normal save is different from autosave in Gutenberg as well, like the classic editor.

'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
Loading