-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
143 lines (127 loc) · 3.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* External dependencies
*/
import { get } from 'lodash';
/**
* WordPress dependencies
*/
import { Component, compose } from '@wordpress/element';
import { Button, ifCondition } from '@wordpress/components';
import { _x } from '@wordpress/i18n';
import { withSelect, withDispatch } from '@wordpress/data';
export class PostPreviewButton extends Component {
constructor() {
super( ...arguments );
this.saveForPreview = this.saveForPreview.bind( this );
this.state = {
isAwaitingSave: false,
};
}
componentWillReceiveProps( nextProps ) {
const { modified, link } = nextProps;
const { isAwaitingSave } = this.state;
const hasFinishedSaving = (
isAwaitingSave &&
modified !== this.props.modified
);
if ( hasFinishedSaving && this.previewWindow ) {
this.previewWindow.location = link;
this.setState( { isAwaitingSave: false } );
}
}
getWindowTarget() {
const { postId } = this.props;
return `wp-preview-${ postId }`;
}
saveForPreview( event ) {
const { isDirty, isNew } = this.props;
// Let default link behavior occur if no changes to saved post
if ( ! isDirty && ! isNew ) {
return;
}
// Save post prior to opening window
this.props.autosave();
this.setState( {
isAwaitingSave: true,
} );
// Open a popup, BUT: Set it to a blank page until save completes. This
// is necessary because popups can only be opened in response to user
// interaction (click), but we must still wait for the post to save.
event.preventDefault();
this.previewWindow = window.open(
'about:blank',
this.getWindowTarget()
);
// When popup is closed, delete reference to avoid later assignment of
// location in a post update.
this.previewWindow.onbeforeunload = () => delete this.previewWindow;
const markup = `
<div>
<p>Please wait…</p>
<p>Generating preview.</p>
</div>
<style>
body {
margin: 0;
}
div {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
width: 100vw;
}
p {
text-align: center;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
}
</style>`;
this.previewWindow.document.write( markup );
this.previewWindow.document.close();
}
render() {
const { link, isSaveable } = this.props;
return (
<Button
className="editor-post-preview"
isLarge
href={ link }
onClick={ this.saveForPreview }
target={ this.getWindowTarget() }
disabled={ ! isSaveable }
>
{ _x( 'Preview', 'imperative verb' ) }
</Button>
);
}
}
export default compose( [
withSelect( ( select ) => {
const {
getCurrentPostId,
getEditedPostPreviewLink,
getEditedPostAttribute,
isEditedPostDirty,
isEditedPostNew,
isEditedPostSaveable,
} = select( 'core/editor' );
const {
getPostType,
} = select( 'core' );
const postType = getPostType( getEditedPostAttribute( 'type' ) );
return {
postId: getCurrentPostId(),
link: getEditedPostPreviewLink(),
isDirty: isEditedPostDirty(),
isNew: isEditedPostNew(),
isSaveable: isEditedPostSaveable(),
isViewable: get( postType, [ 'viewable' ], false ),
modified: getEditedPostAttribute( 'modified' ),
};
} ),
withDispatch( ( dispatch ) => ( {
autosave: dispatch( 'core/editor' ).autosave,
} ) ),
ifCondition( ( { isViewable } ) => isViewable ),
] )( PostPreviewButton );