Skip to content

Commit

Permalink
Editor: Add isCleanNewPost selector (#9668)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth authored Sep 6, 2018
1 parent 735ab8d commit 9a6bf39
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
16 changes: 16 additions & 0 deletions docs/data/data-core-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ false if the editing state matches the saved or new post.

Whether unsaved values exist.

### isCleanNewPost

Returns true if there are no unsaved values for the current edit session and
if the currently edited post is new (has never been saved before).

Note: This selector is not currently used by the editor package, but is made
available as an assumed-useful selector for external integrations.

*Parameters*

* state: Global application state.

*Returns*

Whether new post and unsaved values exist.

### getCurrentPost

Returns the post currently being edited in its last known saved state, not
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/deprecated.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gutenberg's deprecation policy is intended to support backwards-compatibility fo
- `wp.components.withContext` has been removed. Please use `wp.element.createContext` instead. See: https://reactjs.org/docs/context.html.
- `wp.coreBlocks.registerCoreBlocks` has been removed. Please use `wp.blockLibrary.registerCoreBlocks` instead.
- `wp.editor.DocumentTitle` component has been removed.
- `isCleanNewPost` and `getDocumentTitle` selectors (`core/editor`) have been removed.
- `getDocumentTitle` selector (`core/editor`) has been removed.

## 3.7.0

Expand Down
2 changes: 1 addition & 1 deletion packages/editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
- `editorMediaUpload` has been removed. Use `mediaUpload` instead.
- Change how required built-ins are polyfilled with Babel 7 ([#9171](https://github.com/WordPress/gutenberg/pull/9171)). If you're using an environment that has limited or no support for ES2015+ such as lower versions of IE then using [core-js](https://github.com/zloirock/core-js) or [@babel/polyfill](https://babeljs.io/docs/en/next/babel-polyfill) will add support for these methods.
- `wp.editor.DocumentTitle` component has been removed.
- `isCleanNewPost` and `getDocumentTitle` selectors (`core/editor`) have been removed.
- `getDocumentTitle` selector (`core/editor`) has been removed.

### Deprecation

Expand Down
15 changes: 15 additions & 0 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ export function isEditedPostDirty( state ) {
return state.editor.isDirty || inSomeHistory( state, isEditedPostDirty );
}

/**
* Returns true if there are no unsaved values for the current edit session and
* if the currently edited post is new (has never been saved before).
*
* Note: This selector is not currently used by the editor package, but is made
* available as an assumed-useful selector for external integrations.
*
* @param {Object} state Global application state.
*
* @return {boolean} Whether new post and unsaved values exist.
*/
export function isCleanNewPost( state ) {
return ! isEditedPostDirty( state ) && isEditedPostNew( state );
}

/**
* Returns the post currently being edited in its last known saved state, not
* including unsaved edits. Returns an object containing relevant default post
Expand Down
54 changes: 54 additions & 0 deletions packages/editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
hasEditorRedo,
isEditedPostNew,
isEditedPostDirty,
isCleanNewPost,
getCurrentPost,
getCurrentPostId,
getCurrentPostLastRevisionId,
Expand Down Expand Up @@ -282,6 +283,59 @@ describe( 'selectors', () => {
} );
} );

describe( 'isCleanNewPost', () => {
it( 'should return true when the post is not dirty and has not been saved before', () => {
const state = {
editor: {
isDirty: false,
},
currentPost: {
id: 1,
status: 'auto-draft',
},
saving: {
requesting: false,
},
};

expect( isCleanNewPost( state ) ).toBe( true );
} );

it( 'should return false when the post is not dirty but the post has been saved', () => {
const state = {
editor: {
isDirty: false,
},
currentPost: {
id: 1,
status: 'draft',
},
saving: {
requesting: false,
},
};

expect( isCleanNewPost( state ) ).toBe( false );
} );

it( 'should return false when the post is dirty but the post has not been saved', () => {
const state = {
editor: {
isDirty: true,
},
currentPost: {
id: 1,
status: 'auto-draft',
},
saving: {
requesting: false,
},
};

expect( isCleanNewPost( state ) ).toBe( false );
} );
} );

describe( 'getCurrentPost', () => {
it( 'should return the current post', () => {
const state = {
Expand Down

0 comments on commit 9a6bf39

Please sign in to comment.