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: Restore isCleanNewPost selector #9668

Merged
merged 1 commit into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
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