summaryrefslogtreecommitdiff
blob: 96502863394e8995bfe397f646c634ab62ae728f (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
 * External dependencies
 */
import { get } from 'lodash';
import { css } from 'emotion'

import { __ } from '@wordpress/i18n';
import {
	CheckboxControl,
	ClipboardButton,
	Path,
	SVG,
} from '@wordpress/components';
import {
	Component,
	createRef,
	Fragment,
} from '@wordpress/element';
import { withSelect } from '@wordpress/data';
import { PluginPostStatusInfo } from '@wordpress/edit-post';
import { ifCondition, compose } from '@wordpress/compose';

const {
	ajaxurl,
	DSPublicPostPreviewData,
} = window;

const pluginPostStatusInfoPreviewUrl = css`
	flex-direction: column;
	align-items: stretch;
	margin-top: 10px;
`

const pluginPostStatusInfoPreviewUrlInput = css`
	width: 100%;
	margin-right: 12px;
`

const pluginPostStatusInfoPreviewDescription = css`
	font-style: italic;
	color: #666;
	margin: .2em 0 0 !important;
`

const pluginPostStatusInfoPreviewUrlInputWrapper = css`
	display: flex;
	justify-content: flex-start;
	align-items: center;
	margin: 0;
`

class PreviewToggle extends Component {

	constructor( props ) {
		super( props )

		this.state = {
			previewEnabled: DSPublicPostPreviewData.previewEnabled,
			previewUrl: DSPublicPostPreviewData.previewUrl,
			hasCopied: false,
		}

		this.previewUrlInput = createRef();

		this.onChange = this.onChange.bind( this );
		this.onPreviewUrlInputFocus = this.onPreviewUrlInputFocus.bind( this );
	}

	onChange( checked ) {
		this.request( {
			checked,
			post_ID: this.props.postId
		}, () => {
			this.setState( { previewEnabled: ! this.state.previewEnabled } );
		} )
	}

	onPreviewUrlInputFocus() {
		this.previewUrlInput.current.focus();
		this.previewUrlInput.current.select();
	}

	request( data, callback ) {
		jQuery.ajax( {
			type: 'POST',
			url: ajaxurl,
			data: {
				action: 'public-post-preview',
				_ajax_nonce: DSPublicPostPreviewData.nonce,
				...data
			},
			success: callback,
		} );
	}

	render() {
		const {
			previewEnabled,
			previewUrl,
			hasCopied
		} = this.state;

		const ariaCopyLabel = hasCopied ? __( 'Preview URL copied', 'public-post-preview' ) : __( 'Copy the preview URL', 'public-post-preview' );

		return (
			<Fragment>
				<PluginPostStatusInfo>
					<CheckboxControl
						label={ __( 'Enable public preview', 'public-post-preview' ) }
						checked={ previewEnabled }
						onChange={ this.onChange }
					/>
				</PluginPostStatusInfo>
				{ previewEnabled &&
					<PluginPostStatusInfo className={ pluginPostStatusInfoPreviewUrl }>
						<p className={ pluginPostStatusInfoPreviewUrlInputWrapper }>
							<label htmlFor="public-post-preview-url" className="screen-reader-text">{ __( 'Preview URL', 'public-post-preview' ) }</label>
							<input
								ref={ this.previewUrlInput }
								type="text"
								id="public-post-preview-url"
								className={ pluginPostStatusInfoPreviewUrlInput }
								value={ previewUrl }
								readOnly
								onFocus={ this.onPreviewUrlInputFocus }
							/>
							<ClipboardButton
								text={ previewUrl }
								label={ ariaCopyLabel }
								onCopy={ () => this.setState( { hasCopied: true } ) }
								onFinishCopy={ () => this.setState( { hasCopied: false } ) }
								aria-disabled={ hasCopied }
								icon={ <SVG width="20" height="20" viewBox="0 0 14 16" xmlns="http://www.w3.org/2000/svg" focusable="false" ><Path fillRule="evenodd" d="M2 13h4v1H2v-1zm5-6H2v1h5V7zm2 3V8l-3 3 3 3v-2h5v-2H9zM4.5 9H2v1h2.5V9zM2 12h2.5v-1H2v1zm9 1h1v2c-.02.28-.11.52-.3.7-.19.18-.42.28-.7.3H1c-.55 0-1-.45-1-1V4c0-.55.45-1 1-1h3c0-1.11.89-2 2-2 1.11 0 2 .89 2 2h3c.55 0 1 .45 1 1v5h-1V6H1v9h10v-2zM2 5h8c0-.55-.45-1-1-1H8c-.55 0-1-.45-1-1s-.45-1-1-1-1 .45-1 1-.45 1-1 1H3c-.55 0-1 .45-1 1z"/></SVG> }
							/>
						</p>
						<p className={ pluginPostStatusInfoPreviewDescription }>
							{ __( 'Copy and share this preview URL.', 'public-post-preview' ) }
						</p>
					</PluginPostStatusInfo>
				}
			</Fragment>
		);
	}
}

export default compose( [
	withSelect( ( select ) => {
		const {
			getPostType,
		} = select( 'core' );
		const {
			getCurrentPostId,
			getEditedPostAttribute,
		} = select( 'core/editor' );
		const postType = getPostType( getEditedPostAttribute( 'type' ) );

		return {
			postId: getCurrentPostId(),
			status: getEditedPostAttribute( 'status' ),
			isViewable: get( postType, [ 'viewable' ], false ),
		};
	} ),
	ifCondition( ( { isViewable } ) => isViewable ),
	ifCondition( ( { status } ) => {
		return [
			'auto-draft',
			'publish',
			'private',
		].indexOf( status ) === -1;
	} ),
] )( PreviewToggle );