|
| 1 | +/** |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * Wordpress Dependencies |
| 19 | + */ |
| 20 | +import { registerPlugin } from '@wordpress/plugins'; |
| 21 | +import { PluginDocumentSettingPanel } from '@wordpress/edit-post'; |
| 22 | +import { Icon, CheckboxControl } from '@wordpress/components'; |
| 23 | +import { __ } from '@wordpress/i18n'; |
| 24 | +import { compose, ifCondition } from '@wordpress/compose'; |
| 25 | +import { withSelect, withDispatch } from '@wordpress/data'; |
| 26 | +import { useState } from '@wordpress/element'; |
| 27 | + |
| 28 | +function HideSection( { settings, updateSectionsSettings } ) { |
| 29 | + /** |
| 30 | + * If you add a new property here, make sure to also add it |
| 31 | + * in the "properties" array at block-editor.php. |
| 32 | + */ |
| 33 | + const settingsOptionsInitial = [ |
| 34 | + { |
| 35 | + label: __( 'Hide Title', 'material' ), |
| 36 | + key: 'title', |
| 37 | + }, |
| 38 | + ]; |
| 39 | + |
| 40 | + settingsOptionsInitial.forEach( settingOption => { |
| 41 | + settingOption.value = settings[ settingOption.key ] |
| 42 | + ? settings[ settingOption.key ] |
| 43 | + : false; |
| 44 | + } ); |
| 45 | + |
| 46 | + const [ settingsOptions, setSettingOptions ] = useState( |
| 47 | + settingsOptionsInitial |
| 48 | + ); |
| 49 | + |
| 50 | + return ( |
| 51 | + <PluginDocumentSettingPanel |
| 52 | + name="sections-control" |
| 53 | + title={ __( 'Hide Sections', 'material' ) } |
| 54 | + className="custom-panel" |
| 55 | + > |
| 56 | + { settingsOptions.map( settingOption => { |
| 57 | + return ( |
| 58 | + <CheckboxControl |
| 59 | + checked={ |
| 60 | + settings[ settingOption.key ] |
| 61 | + ? settings[ settingOption.key ] |
| 62 | + : false |
| 63 | + } |
| 64 | + onChange={ state => { |
| 65 | + const newSettings = { ...settings }; |
| 66 | + newSettings[ settingOption.key ] = state; |
| 67 | + const newSettingsOptions = settingsOptions; |
| 68 | + newSettingsOptions.forEach( newSettingOption => { |
| 69 | + if ( newSettingOption.key === settingOption.key ) { |
| 70 | + newSettingOption.value = state; |
| 71 | + } |
| 72 | + } ); |
| 73 | + updateSectionsSettings( newSettings ); |
| 74 | + setSettingOptions( newSettingsOptions ); |
| 75 | + } } |
| 76 | + label={ settingOption.label } |
| 77 | + key={ settingOption.key } |
| 78 | + /> |
| 79 | + ); |
| 80 | + } ) } |
| 81 | + </PluginDocumentSettingPanel> |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +const WrappedSectionControl = compose( [ |
| 86 | + withSelect( select => { |
| 87 | + const { getCurrentPostType } = select( 'core/editor' ); |
| 88 | + const { getPostType } = select( 'core' ); |
| 89 | + return { |
| 90 | + postTypeObj: getPostType( getCurrentPostType() ), |
| 91 | + }; |
| 92 | + } ), |
| 93 | + ifCondition( ( { postTypeObj } ) => { |
| 94 | + return postTypeObj?.supports?.[ 'custom-fields' ]; |
| 95 | + } ), |
| 96 | + withSelect( select => { |
| 97 | + const { getEditedPostAttribute } = select( 'core/editor' ); |
| 98 | + return { |
| 99 | + settings: getEditedPostAttribute( 'meta' )[ 'material-hide-sections' ], |
| 100 | + }; |
| 101 | + } ), |
| 102 | + withDispatch( dispatch => { |
| 103 | + return { |
| 104 | + updateSectionsSettings( settings ) { |
| 105 | + dispatch( 'core/editor' ).editPost( { |
| 106 | + meta: { 'material-hide-sections': settings }, |
| 107 | + } ); |
| 108 | + }, |
| 109 | + }; |
| 110 | + } ), |
| 111 | + ifCondition( ( { settings } ) => { |
| 112 | + return !! settings; |
| 113 | + } ), |
| 114 | +] )( HideSection ); |
| 115 | + |
| 116 | +registerPlugin( 'material-hide-sections', { |
| 117 | + render: WrappedSectionControl, |
| 118 | + icon: <Icon icon="admin-customizer" />, |
| 119 | +} ); |
0 commit comments