/** * WordPress dependencies */ import { dispatch, select } from '@safe-wordpress/data'; import { addFilter } from '@safe-wordpress/hooks'; import { registerPlugin } from '@safe-wordpress/plugins'; /** * External dependencies */ import { injectGlobal } from '@nelio/popups/css'; import { hasPopupSettings, getPopupSettings } from '@nelio/popups/utils'; import type { PopupSettings } from '@nelio/popups/types'; /** * Internal dependencies */ import { PopupCloseControl, PopupEditorExtensions } from './components'; // WARNING! Don’t add a dependency with “@wordpress/edit-post”, as it // should only be enqueued when the user is in the classic editor. const EDIT_POST = 'core/edit-post'; const EDITOR = 'core/editor'; // Init if ( hasPopupSettings( window ) ) { init( getPopupSettings() ); } // ======= // HELPERS // ======= function init( settings: PopupSettings ) { registerPlugin( 'nelio-popups', { icon: () => null, render: PopupEditorExtensions, } ); addFilter( 'editor.BlockEdit', 'nelio-popups/close-control', PopupCloseControl( settings.popupCloseBlocks ) ); openPanels(); const buttons = { switchToDraft: '.editor-post-switch-to-draft', saveDraft: '.edit-post-header__settings > .components-button:first-child', snackbarViewLink: '.components-snackbar__content > a', preview: '.editor-post-preview', previewIcon: '.edit-post-header__settings > a[target="_blank"]', previewDropdown: '.block-editor-post-preview__dropdown', }; const postStatus = '.edit-post-post-status'; injectGlobal( ` ${ buttons.switchToDraft }, ${ buttons.saveDraft }, ${ buttons.previewDropdown }, ${ buttons.previewIcon }, ${ buttons.snackbarViewLink }, ${ postStatus } { display: none !important; } ${ buttons.preview } { display: flex !important; } ` ); } function openPanels(): void { if ( ! select( EDIT_POST ) ) { return; } const { isEditorPanelOpened } = select( EDITOR ) as { isEditorPanelOpened: ( panel: string ) => boolean; }; const { toggleEditorPanelOpened } = dispatch( EDIT_POST ) as { toggleEditorPanelOpened: ( panel: string ) => void; }; const panels = [ 'nelio-popups/nelio-popups-status-panel', 'nelio-popups/nelio-popups-settings-panel', ]; panels.forEach( ( p ) => { if ( ! isEditorPanelOpened( p ) ) { toggleEditorPanelOpened( p ); } } ); }