blocks
3 years ago
components
3 years ago
extend
4 years ago
hoc
4 years ago
hooks
4 years ago
shared
4 years ago
utils
4 years ago
blocks.js
4 years ago
dashboard.js
4 years ago
dashboard.scss
4 years ago
dashboard.js
248 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { |
| 5 | __, |
| 6 | } from '@wordpress/i18n'; |
| 7 | |
| 8 | import { |
| 9 | BaseControl, |
| 10 | Button, |
| 11 | PanelBody, |
| 12 | PanelRow, |
| 13 | Placeholder, |
| 14 | Spinner, |
| 15 | ToggleControl, |
| 16 | SelectControl, |
| 17 | TextControl, |
| 18 | } from '@wordpress/components'; |
| 19 | |
| 20 | import { |
| 21 | render, |
| 22 | Component, |
| 23 | Fragment, |
| 24 | } from '@wordpress/element'; |
| 25 | |
| 26 | import apiFetch from '@wordpress/api-fetch'; |
| 27 | |
| 28 | import { |
| 29 | applyFilters, |
| 30 | } from '@wordpress/hooks'; |
| 31 | |
| 32 | /** |
| 33 | * Internal dependencies |
| 34 | */ |
| 35 | import './dashboard.scss'; |
| 36 | |
| 37 | class App extends Component { |
| 38 | constructor() { |
| 39 | super( ...arguments ); |
| 40 | |
| 41 | this.state = { |
| 42 | isAPILoaded: false, |
| 43 | isAPISaving: false, |
| 44 | isRegeneratingCSS: false, |
| 45 | settings: generateBlocksSettings.settings, |
| 46 | }; |
| 47 | |
| 48 | this.getSetting = this.getSetting.bind( this ); |
| 49 | this.updateSettings = this.updateSettings.bind( this ); |
| 50 | } |
| 51 | |
| 52 | componentDidMount() { |
| 53 | this.setState( { |
| 54 | isAPILoaded: true, |
| 55 | } ); |
| 56 | } |
| 57 | |
| 58 | getSetting( name, defaultVal ) { |
| 59 | let result = defaultVal; |
| 60 | |
| 61 | if ( 'undefined' !== typeof this.state.settings[ name ] ) { |
| 62 | result = this.state.settings[ name ]; |
| 63 | } |
| 64 | |
| 65 | return result; |
| 66 | } |
| 67 | |
| 68 | updateSettings( e ) { |
| 69 | this.setState( { isAPISaving: true } ); |
| 70 | const message = e.target.nextElementSibling; |
| 71 | |
| 72 | apiFetch( { |
| 73 | path: '/generateblocks/v1/settings', |
| 74 | method: 'POST', |
| 75 | data: { |
| 76 | settings: this.state.settings, |
| 77 | }, |
| 78 | } ).then( ( result ) => { |
| 79 | this.setState( { isAPISaving: false } ); |
| 80 | message.classList.add( 'gblocks-action-message--show' ); |
| 81 | message.textContent = result.response; |
| 82 | |
| 83 | if ( ! result.success || ! result.response ) { |
| 84 | message.classList.add( 'gblocks-action-message--error' ); |
| 85 | } else { |
| 86 | setTimeout( function() { |
| 87 | message.classList.remove( 'gblocks-action-message--show' ); |
| 88 | }, 3000 ); |
| 89 | } |
| 90 | } ); |
| 91 | } |
| 92 | |
| 93 | render() { |
| 94 | if ( ! this.state.isAPILoaded ) { |
| 95 | return ( |
| 96 | <Placeholder className="gblocks-settings-placeholder"> |
| 97 | <Spinner /> |
| 98 | </Placeholder> |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | return ( |
| 103 | <Fragment> |
| 104 | <div className="generateblocks-settings-main"> |
| 105 | { applyFilters( 'generateblocks.dashboard.beforeSettings', '', this ) } |
| 106 | |
| 107 | <PanelBody |
| 108 | title={ __( 'Settings', 'generateblocks' ) } |
| 109 | > |
| 110 | <div className="gblocks-dashboard-panel-row-wrapper"> |
| 111 | <PanelRow className="gblocks-container-width"> |
| 112 | <div className="gblocks-units">px</div> |
| 113 | |
| 114 | <TextControl |
| 115 | type="number" |
| 116 | label={ __( 'Default Container Width', 'generateblocks' ) } |
| 117 | help={ !! generateBlocksSettings.gpContainerWidth |
| 118 | ? __( 'The default width of the Container block is set by GeneratePress in the Customizer.', 'generateblocks' ) |
| 119 | : __( 'The default width of the Container block.', 'generateblocks' ) |
| 120 | } |
| 121 | disabled={ !! generateBlocksSettings.gpContainerWidth } |
| 122 | value={ generateBlocksSettings.gpContainerWidth || this.getSetting( 'container_width' ) } |
| 123 | onChange={ ( value ) => { |
| 124 | this.setState( { |
| 125 | settings: { |
| 126 | ...this.state.settings, |
| 127 | container_width: value, |
| 128 | }, |
| 129 | } ); |
| 130 | } } |
| 131 | /> |
| 132 | |
| 133 | { !! generateBlocksSettings.gpContainerWidthLink && |
| 134 | <a href={ generateBlocksSettings.gpContainerWidthLink } target="_blank" rel="noopener noreferrer" style={ { fontSize: '12px' } }> |
| 135 | { __( 'Go to option →', 'generateblocks' ) } |
| 136 | </a> |
| 137 | } |
| 138 | </PanelRow> |
| 139 | |
| 140 | <PanelRow className="gblocks-css-print-method"> |
| 141 | <SelectControl |
| 142 | label={ __( 'CSS Print Method', 'generateblocks' ) } |
| 143 | help={ __( 'Generating your CSS in external files is better for overall performance.', 'generateblocks' ) } |
| 144 | value={ this.getSetting( 'css_print_method' ) } |
| 145 | options={ [ |
| 146 | { label: __( 'External File', 'generateblocks' ), value: 'file' }, |
| 147 | { label: __( 'Inline Embedding', 'generateblocks' ), value: 'inline' }, |
| 148 | ] } |
| 149 | onChange={ ( value ) => { |
| 150 | this.setState( { |
| 151 | settings: { |
| 152 | ...this.state.settings, |
| 153 | css_print_method: value, |
| 154 | }, |
| 155 | } ); |
| 156 | } } |
| 157 | /> |
| 158 | </PanelRow> |
| 159 | |
| 160 | { 'file' === this.getSetting( 'css_print_method' ) && |
| 161 | <PanelRow> |
| 162 | <BaseControl |
| 163 | id="gblocks-regenerate-css" |
| 164 | className="gblocks-regenerate-css" |
| 165 | help={ __( 'Force your external CSS files to regenerate next time their page is loaded.', 'generateblocks' ) } |
| 166 | > |
| 167 | <div className="gblocks-action-button"> |
| 168 | <Button |
| 169 | isSecondary |
| 170 | onClick={ ( e ) => { |
| 171 | this.setState( { isRegeneratingCSS: true } ); |
| 172 | const message = e.target.nextElementSibling; |
| 173 | |
| 174 | apiFetch( { |
| 175 | path: '/generateblocks/v1/regenerate_css_files', |
| 176 | method: 'POST', |
| 177 | } ).then( ( result ) => { |
| 178 | this.setState( { isRegeneratingCSS: false } ); |
| 179 | message.classList.add( 'gblocks-action-message--show' ); |
| 180 | message.textContent = result.response; |
| 181 | |
| 182 | if ( ! result.success || ! result.response ) { |
| 183 | message.classList.add( 'gblocks-action-message--error' ); |
| 184 | } else { |
| 185 | setTimeout( function() { |
| 186 | message.classList.remove( 'gblocks-action-message--show' ); |
| 187 | }, 3000 ); |
| 188 | } |
| 189 | } ); |
| 190 | } } |
| 191 | > |
| 192 | { this.state.isRegeneratingCSS && <Spinner /> } |
| 193 | { ! this.state.isRegeneratingCSS && __( 'Regenerate CSS Files', 'generateblocks' ) } |
| 194 | </Button> |
| 195 | |
| 196 | <span className="gblocks-action-message"></span> |
| 197 | </div> |
| 198 | </BaseControl> |
| 199 | </PanelRow> |
| 200 | } |
| 201 | |
| 202 | <PanelRow> |
| 203 | <ToggleControl |
| 204 | label={ __( 'Sync Responsive Previews', 'generateblocks' ) } |
| 205 | help={ __( 'Sync our responsive preview controls with the editor responsive previews.', 'generateblocks' ) } |
| 206 | checked={ this.getSetting( 'sync_responsive_previews' ) } |
| 207 | onChange={ ( value ) => { |
| 208 | this.setState( { |
| 209 | settings: { |
| 210 | ...this.state.settings, |
| 211 | sync_responsive_previews: value, |
| 212 | }, |
| 213 | } ); |
| 214 | } } |
| 215 | /> |
| 216 | </PanelRow> |
| 217 | |
| 218 | { applyFilters( 'generateblocks.dashboard.settings', '', this ) } |
| 219 | |
| 220 | <div className="gblocks-action-button"> |
| 221 | <Button |
| 222 | isPrimary |
| 223 | disabled={ this.state.isAPISaving } |
| 224 | onClick={ ( e ) => this.updateSettings( e ) } |
| 225 | > |
| 226 | { this.state.isAPISaving && <Spinner /> } |
| 227 | { ! this.state.isAPISaving && __( 'Save' ) } |
| 228 | </Button> |
| 229 | |
| 230 | <span className="gblocks-action-message"></span> |
| 231 | </div> |
| 232 | </div> |
| 233 | </PanelBody> |
| 234 | |
| 235 | { applyFilters( 'generateblocks.dashboard.afterSettings', '', this ) } |
| 236 | </div> |
| 237 | </Fragment> |
| 238 | ); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | window.addEventListener( 'DOMContentLoaded', () => { |
| 243 | render( |
| 244 | <App />, |
| 245 | document.getElementById( 'gblocks-block-default-settings' ) |
| 246 | ); |
| 247 | } ); |
| 248 |