From 73ed5743cc7e1e1dc77c73ed9406c8f9ebfa4b23 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Fri, 13 Jul 2018 12:26:20 +0800 Subject: [PATCH] If a more recent revision/autosave exists, store its state on editor setup Attempts to resolve an issue whereby the editor is unaware of the existence of a more recent revision, and so attempts to autosave fail. --- editor/store/effects.js | 29 ++++++++++++++++------------- editor/store/test/effects.js | 32 ++++++++++++++++++++++++++++++++ lib/client-assets.php | 3 +++ 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/editor/store/effects.js b/editor/store/effects.js index 75da4df7821d25..05586e58fa8d55 100644 --- a/editor/store/effects.js +++ b/editor/store/effects.js @@ -422,26 +422,29 @@ export default { } // Check the auto-save status - let autosaveAction; + let autosaveActions; if ( autosave ) { const noticeMessage = __( 'There is an autosave of this post that is more recent than the version below.' ); - autosaveAction = createWarningNotice( -

- { noticeMessage } - { ' ' } - { __( 'View the autosave' ) } -

, - { - id: AUTOSAVE_POST_NOTICE_ID, - spokenMessage: noticeMessage, - } - ); + autosaveActions = [ + createWarningNotice( +

+ { noticeMessage } + { ' ' } + { __( 'View the autosave' ) } +

, + { + id: AUTOSAVE_POST_NOTICE_ID, + spokenMessage: noticeMessage, + } + ), + resetAutosave( autosave ), + ]; } return [ setTemplateValidity( isValidTemplate ), setupEditorState( post, blocks, edits ), - ...( autosaveAction ? [ autosaveAction ] : [] ), + ...( autosaveActions ? autosaveActions : [] ), ]; }, SYNCHRONIZE_TEMPLATE( action, { getState } ) { diff --git a/editor/store/test/effects.js b/editor/store/test/effects.js index 8e135cb7c566b8..f5614db5f5d13b 100644 --- a/editor/store/test/effects.js +++ b/editor/store/test/effects.js @@ -35,6 +35,7 @@ import { convertBlockToShared, setTemplateValidity, editPost, + resetAutosave, } from '../actions'; import effects, { removeProvisionalBlock, @@ -558,6 +559,37 @@ describe( 'effects', () => { setupEditorState( post, [], { title: 'A History of Pork' } ), ] ); } ); + + it( 'it resets the autosave if the settings contains an autosave property', () => { + const post = { + id: 1, + title: { + raw: 'A History of Pork', + }, + content: { + raw: '', + }, + status: 'draft', + }; + const autosave = { + title: 'test title', + content: 'test content', + excerpt: 'test excerpt', + editLink: 'test edit link', + }; + const getState = () => ( { + settings: { + template: null, + templateLock: false, + }, + } ); + + const dispatch = jest.fn(); + + const result = handler( { post, autosave }, { getState, dispatch } ); + + expect( result ).toContainEqual( resetAutosave( autosave ) ); + } ); } ); describe( 'shared block effects', () => { diff --git a/lib/client-assets.php b/lib/client-assets.php index 106e426cca5f4c..70fda94080e4c1 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -1216,6 +1216,9 @@ function gutenberg_editor_scripts_and_styles( $hook ) { if ( $post_autosave ) { $editor_settings['autosave'] = array( 'editLink' => add_query_arg( 'gutenberg', true, get_edit_post_link( $post_autosave->ID ) ), + 'title' => $post_autosave->post_title, + 'content' => $post_autosave->post_content, + 'excerpt' => $post_autosave->post_excerpt, ); }