Skip to content

Commit

Permalink
Rework PR 4218
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Silverstein committed Apr 13, 2018
1 parent 4c0521e commit cc1a856
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
7 changes: 6 additions & 1 deletion editor/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
/**
* Internal Dependencies
*/
import { setupEditor, undo, redo, createUndoLevel } from '../../store/actions';
import { setupEditor, undo, redo, showAutosaveNotice, createUndoLevel } from '../../store/actions';
import store from '../../store';

class EditorProvider extends Component {
Expand All @@ -37,6 +37,11 @@ class EditorProvider extends Component {
} )
);
}

// Display a notice if an autosave exists.
if ( props.settings.autosave ) {
this.store.dispatch( showAutosaveNotice( props.settings.autosave ) );
}
}

render() {
Expand Down
13 changes: 13 additions & 0 deletions editor/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,19 @@ export function autosave() {
};
}

/**
* Returns an action to show the autosave notice.
*
* @param {boolean} autosaveStatus Autosave status and data including a link to the autosave.
* @return {Object} Action object
*/
export function showAutosaveNotice( autosaveStatus ) {
return {
type: 'REQUEST_AUTOSAVE_NOTICE',
autosaveStatus,
};
}

/**
* Returns an action object used in signalling that undo history should
* restore last popped state.
Expand Down
21 changes: 21 additions & 0 deletions editor/store/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
replaceBlocks,
createSuccessNotice,
createErrorNotice,
createWarningNotice,
removeNotice,
savePost,
saveSharedBlock,
Expand Down Expand Up @@ -71,6 +72,7 @@ import {
* Module Constants
*/
const SAVE_POST_NOTICE_ID = 'SAVE_POST_NOTICE_ID';
const AUTOSAVE_POST_NOTICE_ID = 'AUTOSAVE_POST_NOTICE_ID';
const TRASH_POST_NOTICE_ID = 'TRASH_POST_NOTICE_ID';
const SHARED_BLOCK_NOTICE_ID = 'SHARED_BLOCK_NOTICE_ID';

Expand Down Expand Up @@ -109,6 +111,7 @@ export default {
optimist: { type: BEGIN, id: POST_UPDATE_TRANSACTION_ID },
} );
dispatch( removeNotice( SAVE_POST_NOTICE_ID ) );
dispatch( removeNotice( AUTOSAVE_POST_NOTICE_ID ) );
const basePath = wp.api.getPostTypeRoute( getCurrentPostType( state ) );
wp.apiRequest( { path: `/wp/v2/${ basePath }/${ post.id }`, method: 'PUT', data: toSend } ).then(
( newPost ) => {
Expand Down Expand Up @@ -201,6 +204,24 @@ export default {
__( 'Updating failed' );
dispatch( createErrorNotice( noticeMessage, { id: SAVE_POST_NOTICE_ID } ) );
},
REQUEST_AUTOSAVE_NOTICE( action, store ) {
const { autosaveStatus } = action;
const { dispatch } = store;
const noticeMessage = __( 'There is an autosave of this post that is more recent than the version below.' );
if ( autosaveStatus ) {
dispatch( createWarningNotice(
<p>
<span>{ noticeMessage }</span>
{ ' ' }
{ <a href={ autosaveStatus.editLink }>{ __( 'View the autosave' ) }</a> }
</p>,
{
id: AUTOSAVE_POST_NOTICE_ID,
spokenMessage: noticeMessage,
}
) );
}
},
TRASH_POST( action, store ) {
const { dispatch, getState } = store;
const { postId } = action;
Expand Down
18 changes: 18 additions & 0 deletions editor/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,24 @@ export function notices( state = [], action ) {
return state;
}

/**
* Reducer returning the currently autosave message.
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Object} Updated state.
*/
export function autosave( state = { message: '' }, action ) {
switch ( action.type ) {
case 'UPDATE_AUTOSAVE_STATUS_MESSAGE':
return {
...state,
message: action.message,
};
}
}

export const sharedBlocks = combineReducers( {
data( state = {}, action ) {
switch ( action.type ) {
Expand Down
42 changes: 42 additions & 0 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,41 @@ function gutenberg_capture_code_editor_settings( $settings ) {
$gutenberg_captured_code_editor_settings = $settings;
return false;
}
/**
* Retrieve a stored autosave that is newer than the post save.
*
* Deletes autosaves that are older than the post save.
*
* @param WP_Post $post Post object.
* @return WP_Post|boolean The post autosave. False if none found.
*/
function get_autosave_newer_than_post_save( $post ) {
// Add autosave data if it is newer and changed.
$autosave = wp_get_post_autosave( $post->ID );
$show_autosave = false;

// Check if we have an autosave that is newer than the post and different from the current post.
if (
$autosave &&
mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false )
) {
// Iterate thru revisioned fields checking for any changes.
foreach ( _wp_post_revision_fields( $post ) as $autosave_field => $_autosave_field ) {
if ( normalize_whitespace( $autosave->$autosave_field ) != normalize_whitespace( $post->$autosave_field ) ) {
$show_autosave = true;
break;
}
}
}

// If this autosave isn't newer and different from the current post, remove.
if ( $autosave && ! $show_autosave ) {
wp_delete_post_revision( $autosave->ID );
return false;
}

return $autosave;
}

/**
* Scripts & Styles.
Expand Down Expand Up @@ -950,6 +985,13 @@ function gutenberg_editor_scripts_and_styles( $hook ) {
'bodyPlaceholder' => apply_filters( 'write_your_story', __( 'Write your story', 'gutenberg' ), $post ),
);

$post_autosave = get_autosave_newer_than_post_save( $post );
if ( $post_autosave ) {
$editor_settings['autosave'] = array(
'editLink' => add_query_arg( 'gutenberg', true, get_edit_post_link( $post_autosave->ID ) ),
);
}

if ( ! empty( $color_palette ) ) {
$editor_settings['colors'] = $color_palette;
}
Expand Down

0 comments on commit cc1a856

Please sign in to comment.