Skip to content

Commit

Permalink
Store original document title on DocumentTitle; restore when unmounted
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jul 19, 2017
1 parent 37873da commit b142f8b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
10 changes: 9 additions & 1 deletion editor/document-title/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ import { Component } from 'react';
import { getDocumentTitle } from '../selectors';

class DocumentTitle extends Component {
componentWillMount() {
this.originalDocumentTitle = document.title;
}

render() {
document.title = this.props.title;
document.title = this.props.title + ' | ' + this.originalDocumentTitle;
return null;
}

componentWillUnmount() {
document.title = this.originalDocumentTitle;
}
}

export default connect(
Expand Down
7 changes: 1 addition & 6 deletions editor/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,6 @@ export function getEditedPostTitle( state ) {
: state.editor.edits.title;
}

let originalDocumentTitle = null;

/**
* Gets the document title to be used.
*
Expand All @@ -257,14 +255,11 @@ let originalDocumentTitle = null;
*/
export function getDocumentTitle( state ) {
let title = getEditedPostTitle( state );
if ( null === originalDocumentTitle ) {
originalDocumentTitle = document.title || __( 'Gutenberg' );
}

if ( ! title || '' === title.trim() ) {
title = isCleanNewPost( state ) ? __( 'New post' ) : __( '(Untitled)' );
}
return title + ' | ' + originalDocumentTitle;
return title;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions editor/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ describe( 'selectors', () => {
},
};

expect( getDocumentTitle( state ) ).toBe( 'The Title | ' + __( 'Gutenberg' ) );
expect( getDocumentTitle( state ) ).toBe( 'The Title' );
} );

it( 'should return current title for edited existing post', () => {
Expand All @@ -330,7 +330,7 @@ describe( 'selectors', () => {
},
};

expect( getDocumentTitle( state ) ).toBe( 'Modified Title | ' + __( 'Gutenberg' ) );
expect( getDocumentTitle( state ) ).toBe( 'Modified Title' );
} );

it( 'should return new post title when new post is clean', () => {
Expand All @@ -343,7 +343,7 @@ describe( 'selectors', () => {
},
};

expect( getDocumentTitle( state ) ).toBe( __( 'New post' ) + ' | ' + __( 'Gutenberg' ) );
expect( getDocumentTitle( state ) ).toBe( __( 'New post' ) );
} );

it( 'should return untitled title when new post is dirty', () => {
Expand All @@ -356,7 +356,7 @@ describe( 'selectors', () => {
},
};

expect( getDocumentTitle( state ) ).toBe( __( '(Untitled)' ) + ' | ' + __( 'Gutenberg' ) );
expect( getDocumentTitle( state ) ).toBe( __( '(Untitled)' ) );
} );

it( 'should return untitled title', () => {
Expand All @@ -370,7 +370,7 @@ describe( 'selectors', () => {
},
};

expect( getDocumentTitle( state ) ).toBe( __( '(Untitled)' ) + ' | ' + __( 'Gutenberg' ) );
expect( getDocumentTitle( state ) ).toBe( __( '(Untitled)' ) );
} );
} );

Expand Down

0 comments on commit b142f8b

Please sign in to comment.