-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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: Added slug selector #4802
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,6 +156,17 @@ export function getCurrentPostType( state ) { | |
return state.currentPost.type; | ||
} | ||
|
||
/** | ||
* Returns the slug of the post currently being edited. | ||
* | ||
* @param {Object} state Global application state. | ||
* | ||
* @returns {string} Slug. | ||
*/ | ||
export function getCurrentPostSlug( state ) { | ||
return getEditedPostAttribute( state, 'slug' ); | ||
} | ||
|
||
/** | ||
* Returns the ID of the post currently being edited, or null if the post has | ||
* not yet been saved. | ||
|
@@ -200,7 +211,7 @@ export function getCurrentPostLastRevisionId( state ) { | |
* @returns {Object} Object of key value pairs comprising unsaved edits. | ||
*/ | ||
export function getPostEdits( state ) { | ||
return state.editor.present.edits; | ||
return get( state, [ 'editor', 'present', 'edits' ], {} ); | ||
} | ||
|
||
/** | ||
|
@@ -214,9 +225,10 @@ export function getPostEdits( state ) { | |
* @returns {*} Post attribute value. | ||
*/ | ||
export function getEditedPostAttribute( state, attributeName ) { | ||
return state.editor.present.edits[ attributeName ] === undefined ? | ||
const edits = getPostEdits( state ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice 👍 |
||
return edits[ attributeName ] === undefined ? | ||
state.currentPost[ attributeName ] : | ||
state.editor.present.edits[ attributeName ]; | ||
edits[ attributeName ]; | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import { | |
getCurrentPostLastRevisionId, | ||
getCurrentPostRevisionsCount, | ||
getCurrentPostType, | ||
getCurrentPostSlug, | ||
getPostEdits, | ||
getEditedPostTitle, | ||
getDocumentTitle, | ||
|
@@ -375,6 +376,31 @@ describe( 'selectors', () => { | |
} ); | ||
} ); | ||
|
||
describe( 'getCurrentPostSlug', () => { | ||
it( 'should return the current post\'s slug is no edits have been made', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: |
||
const state = { | ||
currentPost: { slug: 'post slug' }, | ||
}; | ||
|
||
expect( getCurrentPostSlug( state ) ).toBe( 'post slug' ); | ||
} ); | ||
|
||
it( 'should return the latest slug if edits have been made to the post', () => { | ||
const state = { | ||
currentPost: { slug: 'old slug' }, | ||
editor: { | ||
present: { | ||
edits: { | ||
slug: 'new slug', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
expect( getCurrentPostSlug( state ) ).toBe( 'new slug' ); | ||
} ); | ||
} ); | ||
|
||
describe( 'getCurrentPostLastRevisionId', () => { | ||
it( 'should return null if the post has not yet been saved', () => { | ||
const state = { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noting that you can use also string as 2nd param:
See 3rd example in here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gziolo I know, this is a small performance consideration because lodash won't have to parse the string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hehe, more keystrokes to save some CPU time. They should include
lodash
into JS engine by default and optimize that for us or teach Babel to do the transformation from string to array!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We seem to be encouraging the opposite thing, as I tend to leave the inverse as a suggestion whenever possible 😉 Beside the performance advantage, I find it trivially more legible by virtue of greater separation between the properties.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside: Why would
state.editor.present.edits
ever be undefined?(I hope the answer is not "because tests")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Further, if this triggers the default, since it's not a shared reference, two subsequent invocations of this function would not be strictly equal and therefore trigger re-renders on any
connect
ed components relying on the result of this function:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good catch about having
{}
as the default value. The same would apply to[]
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One example where I worked around this was in
getBlockOrder
:gutenberg/editor/store/selectors.js
Lines 33 to 40 in e2c824a
gutenberg/editor/store/selectors.js
Lines 788 to 801 in e2c824a
I'd not love if we start having many constants at the top of this file, but we also should try to avoid these fallbacks whenever possible (instead relying on the value reference living in state as the result of the reducer).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There could be 2 constants, one for an empty array and one for an empty object. However I second your opinion that reducers should handle it.