-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Update document.title when post title changes #1802
Changes from 9 commits
98303bb
b406fd2
0a18639
c428f3a
e0c47b8
37873da
b142f8b
f4145f3
7462db6
2309add
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
import { Component } from 'react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getDocumentTitle } from '../selectors'; | ||
|
||
class DocumentTitle extends Component { | ||
|
||
constructor( props ) { | ||
super( props ); | ||
this.originalDocumentTitle = document.title; | ||
} | ||
|
||
setDocumentTitle( title ) { | ||
document.title = title + ' | ' + this.originalDocumentTitle; | ||
} | ||
|
||
componentDidMount() { | ||
this.setDocumentTitle( this.props.title ); | ||
} | ||
|
||
componentWillReceiveProps( nextProps ) { | ||
if ( nextProps.title !== this.props.title ) { | ||
this.setDocumentTitle( nextProps.title ); | ||
} | ||
} | ||
|
||
render() { | ||
return null; | ||
} | ||
|
||
componentWillUnmount() { | ||
document.title = this.originalDocumentTitle; | ||
} | ||
} | ||
|
||
export default connect( | ||
( state ) => ( { | ||
title: getDocumentTitle( state ), | ||
} ) | ||
)( DocumentTitle ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,11 @@ import { getBlockType } from 'blocks'; | |
*/ | ||
import { addQueryArgs } from './utils/url'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from 'i18n'; | ||
|
||
/** | ||
* Returns the current editing mode. | ||
* | ||
|
@@ -88,6 +93,17 @@ export function isEditedPostDirty( state ) { | |
return state.editor.dirty; | ||
} | ||
|
||
/** | ||
* Returns true if there are no unsaved values for the current edit session and if | ||
* the currently edited post is new (and has never been saved before). | ||
* | ||
* @param {Object} state Global application state | ||
* @return {Boolean} Whether new post and unsaved values exist | ||
*/ | ||
export function isCleanNewPost( state ) { | ||
return ! isEditedPostDirty( state ) && isEditedPostNew( 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. Should we add a unit test for this selector? 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. Yes. Done in 1f6601a8568261ec92f8dabc8d2b853895e9abdc. |
||
|
||
/** | ||
* Returns the post currently being edited in its last known saved state, not | ||
* including unsaved edits. Returns an object containing relevant default post | ||
|
@@ -226,9 +242,26 @@ export function isEditedPostBeingScheduled( state ) { | |
* @return {String} Raw post title | ||
*/ | ||
export function getEditedPostTitle( state ) { | ||
return state.editor.edits.title === undefined | ||
? get( state.currentPost, 'title.raw' ) | ||
: state.editor.edits.title; | ||
const editedTitle = get( state.editor, 'edits.title' ); | ||
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. A few minor thoughts:
|
||
if ( editedTitle !== undefined ) { | ||
return editedTitle; | ||
} | ||
return get( state.currentPost, 'title.raw' ); | ||
} | ||
|
||
/** | ||
* Gets the document title to be used. | ||
* | ||
* @param {Object} state Global application state | ||
* @return {string} Document title | ||
*/ | ||
export function getDocumentTitle( state ) { | ||
let title = getEditedPostTitle( state ); | ||
|
||
if ( ! title || '' === title.trim() ) { | ||
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. Maybe |
||
title = isCleanNewPost( state ) ? __( 'New post' ) : __( '(Untitled)' ); | ||
} | ||
return title; | ||
} | ||
|
||
/** | ||
|
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.
Minor: I think we should stick with convention to have
render
be last method in component, roughly something like: