Skip to content

Commit

Permalink
If a more recent revision/autosave exists, store its state on editor …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
talldan committed Jul 13, 2018
1 parent b96bfff commit 73ed574
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
29 changes: 16 additions & 13 deletions editor/store/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<p>
{ noticeMessage }
{ ' ' }
<a href={ autosave.editLink }>{ __( 'View the autosave' ) }</a>
</p>,
{
id: AUTOSAVE_POST_NOTICE_ID,
spokenMessage: noticeMessage,
}
);
autosaveActions = [
createWarningNotice(
<p>
{ noticeMessage }
{ ' ' }
<a href={ autosave.editLink }>{ __( 'View the autosave' ) }</a>
</p>,
{
id: AUTOSAVE_POST_NOTICE_ID,
spokenMessage: noticeMessage,
}
),
resetAutosave( autosave ),
];
}

return [
setTemplateValidity( isValidTemplate ),
setupEditorState( post, blocks, edits ),
...( autosaveAction ? [ autosaveAction ] : [] ),
...( autosaveActions ? autosaveActions : [] ),
];
},
SYNCHRONIZE_TEMPLATE( action, { getState } ) {
Expand Down
32 changes: 32 additions & 0 deletions editor/store/test/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
convertBlockToShared,
setTemplateValidity,
editPost,
resetAutosave,
} from '../actions';
import effects, {
removeProvisionalBlock,
Expand Down Expand Up @@ -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', () => {
Expand Down
3 changes: 3 additions & 0 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

Expand Down

0 comments on commit 73ed574

Please sign in to comment.