Skip to content

Commit

Permalink
Preview Button: Use preview link as href if available
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jun 6, 2018
1 parent 2134269 commit 7658b07
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
13 changes: 10 additions & 3 deletions editor/components/post-preview-button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,19 @@ export class PostPreviewButton extends Component {
}

saveForPreview( event ) {
const { isDirty, isNew } = this.props;
const { previewLink, isDirty, isNew } = this.props;

// Let default link behavior occur if no changes to saved post
if ( ! isDirty && ! isNew ) {
return;
}

// Likewise, if a preview URL is available and already assigned as
// the href of the clicked link, there's no need for the interstitial.
if ( previewLink && event.target.href === previewLink ) {
return;
}

// Save post prior to opening window
this.props.autosave();

Expand Down Expand Up @@ -100,13 +107,13 @@ export class PostPreviewButton extends Component {
}

render() {
const { currentPostLink, isSaveable } = this.props;
const { previewLink, currentPostLink, isSaveable } = this.props;

return (
<Button
className="editor-post-preview"
isLarge
href={ currentPostLink }
href={ previewLink || currentPostLink }
onClick={ this.saveForPreview }
target={ this.getWindowTarget() }
disabled={ ! isSaveable }
Expand Down
10 changes: 10 additions & 0 deletions editor/components/post-preview-button/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ describe( 'PostPreviewButton', () => {
expect( wrapper.prop( 'target' ) ).toBe( 'wp-preview-1' );
} );

it( 'should render with preview link as href if available', () => {
const wrapper = shallow(
<PostPreviewButton
currentPostLink="https://wordpress.org/?p=1"
previewLink="https://wordpress.org/?p=1&preview=true" />
);

expect( wrapper.prop( 'href' ) ).toBe( 'https://wordpress.org/?p=1&preview=true' );
} );

it( 'should be disabled if post is not saveable', () => {
const wrapper = shallow(
<PostPreviewButton
Expand Down

0 comments on commit 7658b07

Please sign in to comment.