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

Editor: Ignore save edit in undo history. #17452

Merged
merged 6 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,7 @@ been edited.
_Parameters_

- _edits_ `Object`: Post attributes to edit.
- _options_ `Object`: Options for the edit.

<a name="enablePublishSidebar" href="#enablePublishSidebar">#</a> **enablePublishSidebar**

Expand Down
2 changes: 2 additions & 0 deletions docs/designers-developers/developers/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ _Parameters_
- _name_ `string`: Name of the edited entity record.
- _recordId_ `number`: Record ID of the edited entity record.
- _edits_ `Object`: The edits.
- _options_ `Object`: Options for the edit.
- _options.undoIgnore_ `boolean`: Whether to ignore the edit in undo history or not.

_Returns_

Expand Down
2 changes: 2 additions & 0 deletions packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ _Parameters_
- _name_ `string`: Name of the edited entity record.
- _recordId_ `number`: Record ID of the edited entity record.
- _edits_ `Object`: The edits.
- _options_ `Object`: Options for the edit.
- _options.undoIgnore_ `boolean`: Whether to ignore the edit in undo history or not.

_Returns_

Expand Down
14 changes: 8 additions & 6 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,16 @@ export function receiveEmbedPreview( url, preview ) {
* Returns an action object that triggers an
* edit to an entity record.
*
* @param {string} kind Kind of the edited entity record.
* @param {string} name Name of the edited entity record.
* @param {number} recordId Record ID of the edited entity record.
* @param {Object} edits The edits.
* @param {string} kind Kind of the edited entity record.
* @param {string} name Name of the edited entity record.
* @param {number} recordId Record ID of the edited entity record.
* @param {Object} edits The edits.
* @param {Object} options Options for the edit.
* @param {boolean} options.undoIgnore Whether to ignore the edit in undo history or not.
*
* @return {Object} Action object.
*/
export function* editEntityRecord( kind, name, recordId, edits ) {
export function* editEntityRecord( kind, name, recordId, edits, options = {} ) {
const { transientEdits = {}, mergedEdits = {} } = yield select(
'getEntity',
kind,
Expand Down Expand Up @@ -167,7 +169,7 @@ export function* editEntityRecord( kind, name, recordId, edits ) {
type: 'EDIT_ENTITY_RECORD',
...edit,
meta: {
undo: {
undo: ! options.undoIgnore && {
...edit,
// Send the current values for things like the first undo stack entry.
edits: Object.keys( edits ).reduce( ( acc, key ) => {
Expand Down
6 changes: 6 additions & 0 deletions packages/core-data/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ export function undo( state = UNDO_INITIAL_STATE, action ) {
return nextState;
}

if ( ! action.meta.undo ) {
return state;
}

// Transient edits don't create an undo level, but are
// reachable in the next meaningful edit to which they
// are merged. They are defined in the entity's config.
Expand All @@ -333,6 +337,8 @@ export function undo( state = UNDO_INITIAL_STATE, action ) {
recordId: action.meta.undo.recordId,
edits: { ...state.flattenedUndo, ...action.meta.undo.edits },
} );
// When an edit is a function it's an optimization to avoid running some expensive operation.
// We can't rely on the function references being the same so we opt out of comparing them here.
const comparisonUndoEdits = Object.values( action.meta.undo.edits ).filter( ( edit ) => typeof edit !== 'function' );
const comparisonEdits = Object.values( action.edits ).filter( ( edit ) => typeof edit !== 'function' );
if ( ! isShallowEqual( comparisonUndoEdits, comparisonEdits ) ) {
Expand Down
19 changes: 19 additions & 0 deletions packages/e2e-tests/specs/undo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
selectBlockByClientId,
getAllBlocks,
saveDraft,
publishPost,
disableNavigationMode,
} from '@wordpress/e2e-test-utils';

Expand Down Expand Up @@ -109,6 +110,24 @@ describe( 'undo', () => {
expect( visibleContent ).toBe( 'original' );
} );

it( 'should not create undo levels when saving', async () => {
await clickBlockAppender();
await page.keyboard.type( '1' );
await saveDraft();
await pressKeyWithModifier( 'primary', 'z' );

expect( await getEditedPostContent() ).toBe( '' );
} );

it( 'should not create undo levels when publishing', async () => {
await clickBlockAppender();
await page.keyboard.type( '1' );
await publishPost();
await pressKeyWithModifier( 'primary', 'z' );

expect( await getEditedPostContent() ).toBe( '' );
} );

it( 'should immediately create an undo level on typing', async () => {
await clickBlockAppender();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export default compose( [
withDispatch( ( dispatch ) => {
const { editPost, savePost } = dispatch( 'core/editor' );
return {
onStatusChange: ( status ) => editPost( { status } ),
onStatusChange: ( status ) => editPost( { status }, { undoIgnore: true } ),
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if this is the right place to make this decision. Should status changes in general be ignored? Or publish status changes? Genuinely wondering...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It depends on the context and the entity.

This will cover publishing status changes for posts and it makes sense to ignore that.

It will be very context-dependent which is why I didn't make it part of the entity configuration.

Copy link
Member

Choose a reason for hiding this comment

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

Ok fair enough.

onSave: savePost,
};
} ),
Expand Down
17 changes: 13 additions & 4 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,22 @@ export function setupEditorState( post ) {
* Returns an action object used in signalling that attributes of the post have
* been edited.
*
* @param {Object} edits Post attributes to edit.
* @param {Object} edits Post attributes to edit.
* @param {Object} options Options for the edit.
*
* @yield {Object} Action object or control.
*/
export function* editPost( edits ) {
export function* editPost( edits, options ) {
const { id, type } = yield select( STORE_KEY, 'getCurrentPost' );
yield dispatch( 'core', 'editEntityRecord', 'postType', type, id, edits );
yield dispatch(
'core',
'editEntityRecord',
'postType',
type,
id,
edits,
options
);
}

/**
Expand Down Expand Up @@ -385,7 +394,7 @@ export function* savePost( options = {} ) {
content: yield select( STORE_KEY, 'getEditedPostContent' ),
};
if ( ! options.isAutosave ) {
yield dispatch( STORE_KEY, 'editPost', edits );
yield dispatch( STORE_KEY, 'editPost', edits, { undoIgnore: true } );
}

yield __experimentalRequestPostUpdateStart( options );
Expand Down