|
| 1 | +import { wp, dtGutenberg } from 'window'; |
| 2 | +import PluginIcon from '../img/icon.svg'; // eslint-disable-line no-unused-vars |
| 3 | + |
| 4 | +const { Icon } = wp.components; // eslint-disable-line no-unused-vars |
| 5 | +const { select, useSelect } = wp.data; |
| 6 | +const { PluginDocumentSettingPanel } = wp.editPost; // eslint-disable-line no-unused-vars |
| 7 | +const { __, sprintf } = wp.i18n; |
| 8 | +const { registerPlugin } = wp.plugins; |
| 9 | + |
| 10 | +/** |
| 11 | + * Add ability to show the admin bar, if needed |
| 12 | + */ |
| 13 | +const RenderShowAdminBar = () => { // eslint-disable-line no-unused-vars |
| 14 | + const bodyClasses = document.body.classList; |
| 15 | + const isFullScreenMode = select( 'core/edit-post' ).isFeatureActive( 'fullscreenMode' ); |
| 16 | + const distributorTopMenu = document.querySelector( '#wp-admin-bar-distributor' ); |
| 17 | + const distributorAdminItem = document.querySelector( '#wp-admin-bar-distributor > a' ); |
| 18 | + |
| 19 | + // Don't show anything if this is a distributed item |
| 20 | + if ( 0 !== parseInt( dtGutenberg.syndicationTime ) ) { |
| 21 | + return null; |
| 22 | + } |
| 23 | + |
| 24 | + if ( ! distributorTopMenu || ! distributorAdminItem ) { |
| 25 | + return ( |
| 26 | + <div className="distributor-toggle"> |
| 27 | + <span>{ __( 'Refresh page to see distribution options', 'distributor' ) }</span> |
| 28 | + </div> |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + return ( |
| 33 | + <div className="distributor-toggle"> |
| 34 | + <button |
| 35 | + className="components-button is-secondary" |
| 36 | + type="button" |
| 37 | + onClick={ () => { |
| 38 | + const mouseEvent = new MouseEvent( 'mouseenter' ); |
| 39 | + |
| 40 | + if ( isFullScreenMode ) { |
| 41 | + bodyClasses.add( 'is-showing-distributor' ); |
| 42 | + } else { |
| 43 | + bodyClasses.remove( 'is-showing-distributor' ); |
| 44 | + } |
| 45 | + |
| 46 | + distributorTopMenu.classList.toggle( 'hover' ); |
| 47 | + distributorAdminItem.dispatchEvent( mouseEvent ); |
| 48 | + } } |
| 49 | + > |
| 50 | + { sprintf( __( 'Distribute %1$s', 'distributor' ), dtGutenberg.postTypeSingular || 'Content' ) } |
| 51 | + </button> |
| 52 | + </div> |
| 53 | + ); |
| 54 | +}; |
| 55 | + |
| 56 | +/** |
| 57 | + * Render the draft message |
| 58 | + */ |
| 59 | +const RenderDraftMessage = () => { // eslint-disable-line no-unused-vars |
| 60 | + if ( 0 !== parseInt( dtGutenberg.syndicationTime ) ) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + return ( |
| 65 | + <p> |
| 66 | + { __( 'Distribution options available once published', 'distributor' ) } |
| 67 | + </p> |
| 68 | + ); |
| 69 | +}; |
| 70 | + |
| 71 | +/** |
| 72 | + * Render the distribution information, if needed |
| 73 | + */ |
| 74 | +const RenderDistributionInfo = () => { // eslint-disable-line no-unused-vars |
| 75 | + if ( 0 < parseInt( dtGutenberg.syndicationCount ) ) { |
| 76 | + return <RenderDistributedTo />; |
| 77 | + } else if ( 0 !== parseInt( dtGutenberg.syndicationTime ) ) { |
| 78 | + return <RenderDistributedFrom />; |
| 79 | + } |
| 80 | + |
| 81 | + return null; |
| 82 | +}; |
| 83 | + |
| 84 | +/** |
| 85 | + * Render the distributed to component |
| 86 | + */ |
| 87 | +const RenderDistributedTo = () => { // eslint-disable-line no-unused-vars |
| 88 | + return( |
| 89 | + <span id='distributed-to'> |
| 90 | + { sprintf( __( 'Distributed to %1$s connection%2$s.', 'distributor' ), |
| 91 | + dtGutenberg.syndicationCount, |
| 92 | + '1' === dtGutenberg.syndicationCount ? '' : 's' ) } |
| 93 | + </span> |
| 94 | + ); |
| 95 | +}; |
| 96 | + |
| 97 | +/** |
| 98 | + * Render the distributed from component |
| 99 | + */ |
| 100 | +const RenderDistributedFrom = () => { // eslint-disable-line no-unused-vars |
| 101 | + return( |
| 102 | + <span id='distributed-from'> |
| 103 | + { __( 'Distributed on: ', 'distributor' ) } |
| 104 | + <strong> { dtGutenberg.syndicationTime } </strong> |
| 105 | + </span> |
| 106 | + ); |
| 107 | +}; |
| 108 | + |
| 109 | +/** |
| 110 | + * Create the Distributor icon |
| 111 | + */ |
| 112 | +const DistributorIcon = () => ( |
| 113 | + <Icon |
| 114 | + className="components-panel__icon" |
| 115 | + icon={ <PluginIcon /> } |
| 116 | + size={ 20 } |
| 117 | + /> |
| 118 | +); |
| 119 | + |
| 120 | +/** |
| 121 | + * Add the Distributor panel to Gutenberg |
| 122 | + */ |
| 123 | +const DistributorPlugin = () => { |
| 124 | + // Ensure the user has proper permissions |
| 125 | + if ( dtGutenberg.noPermissions && 1 === parseInt( dtGutenberg.noPermissions ) ) { |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | + const postType = useSelect( select => select( 'core/editor' ).getCurrentPostType() ); |
| 130 | + const postStatus = useSelect( select => select( 'core/editor' ).getCurrentPostAttribute( 'status' ) ); |
| 131 | + |
| 132 | + // Ensure we are on a supported post type |
| 133 | + if ( dtGutenberg.supportedPostTypes && dtGutenberg.supportedPostTypes[ postType ] === undefined ) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + |
| 137 | + // If we are on a non-supported post status, change what we show |
| 138 | + if ( dtGutenberg.supportedPostStati && ! dtGutenberg.supportedPostStati.includes( postStatus ) ) { |
| 139 | + return ( |
| 140 | + <PluginDocumentSettingPanel |
| 141 | + title={ __( 'Distributor', 'distributor' ) } |
| 142 | + icon={ DistributorIcon } |
| 143 | + className="distributor-panel" |
| 144 | + > |
| 145 | + <RenderDraftMessage /> |
| 146 | + <RenderDistributionInfo /> |
| 147 | + </PluginDocumentSettingPanel> |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + return ( |
| 152 | + <PluginDocumentSettingPanel |
| 153 | + title={ __( 'Distributor', 'distributor' ) } |
| 154 | + icon={ DistributorIcon } |
| 155 | + className="distributor-panel" |
| 156 | + > |
| 157 | + <RenderShowAdminBar /> |
| 158 | + <RenderDistributionInfo /> |
| 159 | + </PluginDocumentSettingPanel> |
| 160 | + ); |
| 161 | +}; |
| 162 | + |
| 163 | +registerPlugin( 'distributor-plugin', { render: DistributorPlugin } ); |
0 commit comments