Skip to content

Commit

Permalink
Post Editor: Rely on the editor store for the template mode state (#5…
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored Dec 5, 2023
1 parent 86bd451 commit 6305cf7
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 117 deletions.
20 changes: 4 additions & 16 deletions docs/reference-guides/data/data-core-edit-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,9 @@ _Returns_

### isEditingTemplate

Returns true if the template editing mode is enabled.

_Parameters_

- _state_ `Object`: Global application state.

_Returns_
> **Deprecated**
- `boolean`: Whether we're editing the template.
Returns true if the template editing mode is enabled.

### isEditorPanelEnabled

Expand Down Expand Up @@ -438,15 +432,9 @@ _Parameters_

### setIsEditingTemplate

Returns an action object used to switch to template editing.

_Parameters_

- _value_ `boolean`: Is editing template.
> **Deprecated**
_Returns_

- `Object`: Action object.
Returns an action object used to switch to template editing.

### setIsInserterOpened

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { layout, chevronLeftSmall, chevronRightSmall } from '@wordpress/icons';
import { store as commandsStore } from '@wordpress/commands';
import { displayShortcut } from '@wordpress/keycodes';
import { store as editorStore } from '@wordpress/editor';

/**
* Internal dependencies
Expand All @@ -28,7 +29,7 @@ function DocumentActions() {
};
}, [] );
const { clearSelectedBlock } = useDispatch( blockEditorStore );
const { setIsEditingTemplate } = useDispatch( editPostStore );
const { setRenderingMode } = useDispatch( editorStore );
const { open: openCommandCenter } = useDispatch( commandsStore );

if ( ! template ) {
Expand All @@ -48,7 +49,7 @@ function DocumentActions() {
className="edit-post-document-actions__back"
onClick={ () => {
clearSelectedBlock();
setIsEditingTemplate( false );
setRenderingMode( 'post-only' );
} }
icon={ isRTL() ? chevronRightSmall : chevronLeftSmall }
>
Expand Down
9 changes: 7 additions & 2 deletions packages/edit-post/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
privateApis as blockEditorPrivateApis,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { PostSavedState, PostPreviewButton } from '@wordpress/editor';
import {
PostSavedState,
PostPreviewButton,
store as editorStore,
} from '@wordpress/editor';
import { useEffect, useRef, useState } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
Expand Down Expand Up @@ -73,7 +77,8 @@ function Header( {
blockSelectionStart:
select( blockEditorStore ).getBlockSelectionStart(),
hasActiveMetaboxes: select( editPostStore ).hasMetaBoxes(),
isEditingTemplate: select( editPostStore ).isEditingTemplate(),
isEditingTemplate:
select( editorStore ).getRenderingMode() !== 'post-only',
isPublishSidebarOpened:
select( editPostStore ).isPublishSidebarOpened(),
hasFixedToolbar: getPreference( 'core/edit-post', 'fixedToolbar' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ function ModeSwitcher() {
select( editorStore ).getEditorSettings().richEditingEnabled,
isCodeEditingEnabled:
select( editorStore ).getEditorSettings().codeEditingEnabled,
isEditingTemplate: select( editPostStore ).isEditingTemplate(),
isEditingTemplate:
select( editorStore ).getRenderingMode() !== 'post-only',
mode: select( editPostStore ).getEditorMode(),
} ),
[]
Expand Down
3 changes: 2 additions & 1 deletion packages/edit-post/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ function Layout() {
const postTypeLabel = getPostTypeLabel();

return {
isTemplateMode: select( editPostStore ).isEditingTemplate(),
isTemplateMode:
select( editorStore ).getRenderingMode() !== 'post-only',
hasFixedToolbar:
select( editPostStore ).isFeatureActive( 'fixedToolbar' ),
sidebarIsOpened: !! (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ const SettingsHeader = ( { sidebarName } ) => {
const openBlockSettings = () => openGeneralSidebar( 'edit-post/block' );

const { documentLabel, isTemplateMode } = useSelect( ( select ) => {
const postTypeLabel = select( editorStore ).getPostTypeLabel();
const { getPostTypeLabel, getRenderingMode } = select( editorStore );

return {
// translators: Default label for the Document sidebar tab, not selected.
documentLabel: postTypeLabel || _x( 'Document', 'noun' ),
isTemplateMode: select( editPostStore ).isEditingTemplate(),
documentLabel: getPostTypeLabel() || _x( 'Document', 'noun' ),
isTemplateMode: getRenderingMode() !== 'post-only',
};
}, [] );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { isRTL, __ } from '@wordpress/i18n';
import { drawerLeft, drawerRight } from '@wordpress/icons';
import { store as interfaceStore } from '@wordpress/interface';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import { store as editorStore } from '@wordpress/editor';

/**
* Internal dependencies
Expand Down Expand Up @@ -63,7 +64,8 @@ const SettingsSidebar = () => {
return {
sidebarName: sidebar,
keyboardShortcut: shortcut,
isTemplateMode: select( editPostStore ).isEditingTemplate(),
isTemplateMode:
select( editorStore ).getRenderingMode() !== 'post-only',
};
},
[]
Expand Down
6 changes: 3 additions & 3 deletions packages/edit-post/src/components/start-page-options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ function StartPageOptionsModal( { onClose } ) {
export default function StartPageOptions() {
const [ isClosed, setIsClosed ] = useState( false );
const shouldEnableModal = useSelect( ( select ) => {
const { isCleanNewPost } = select( editorStore );
const { isEditingTemplate, isFeatureActive } = select( editPostStore );
const { isCleanNewPost, getRenderingMode } = select( editorStore );
const { isFeatureActive } = select( editPostStore );

return (
! isEditingTemplate() &&
getRenderingMode() === 'post-only' &&
! isFeatureActive( 'welcomeGuide' ) &&
isCleanNewPost()
);
Expand Down
25 changes: 6 additions & 19 deletions packages/edit-post/src/components/visual-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import {
__experimentalUseResizeCanvas as useResizeCanvas,
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';
import { useRef, useMemo, useEffect } from '@wordpress/element';
import { useRef, useMemo } from '@wordpress/element';
import { __unstableMotion as motion } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { useSelect } from '@wordpress/data';
import { useMergeRefs } from '@wordpress/compose';
import { store as blocksStore } from '@wordpress/blocks';

Expand All @@ -43,20 +43,16 @@ export default function VisualEditor( { styles } ) {
isBlockBasedTheme,
hasV3BlocksOnly,
} = useSelect( ( select ) => {
const {
isFeatureActive,
isEditingTemplate,
__experimentalGetPreviewDeviceType,
} = select( editPostStore );
const { getEditorSettings } = select( editorStore );
const { isFeatureActive, __experimentalGetPreviewDeviceType } =
select( editPostStore );
const { getEditorSettings, getRenderingMode } = select( editorStore );
const { getBlockTypes } = select( blocksStore );
const _isTemplateMode = isEditingTemplate();
const editorSettings = getEditorSettings();

return {
deviceType: __experimentalGetPreviewDeviceType(),
isWelcomeGuideVisible: isFeatureActive( 'welcomeGuide' ),
isTemplateMode: _isTemplateMode,
isTemplateMode: getRenderingMode() !== 'post-only',
isBlockBasedTheme: editorSettings.__unstableIsBlockBasedTheme,
hasV3BlocksOnly: getBlockTypes().every( ( type ) => {
return type.apiVersion >= 3;
Expand All @@ -67,7 +63,6 @@ export default function VisualEditor( { styles } ) {
( select ) => select( editPostStore ).hasMetaBoxes(),
[]
);
const { setRenderingMode } = useDispatch( editorStore );
const desktopCanvasStyles = {
height: '100%',
width: '100%',
Expand Down Expand Up @@ -126,14 +121,6 @@ export default function VisualEditor( { styles } ) {
deviceType === 'Tablet' ||
deviceType === 'Mobile';

useEffect( () => {
if ( isTemplateMode ) {
setRenderingMode( 'all' );
} else {
setRenderingMode( 'post-only' );
}
}, [ isTemplateMode, setRenderingMode ] );

return (
<BlockTools
__unstableContentRef={ ref }
Expand Down
6 changes: 4 additions & 2 deletions packages/edit-post/src/components/welcome-guide/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';

/**
* Internal dependencies
Expand All @@ -12,8 +13,9 @@ import { store as editPostStore } from '../../store';

export default function WelcomeGuide() {
const { isActive, isTemplateMode } = useSelect( ( select ) => {
const { isFeatureActive, isEditingTemplate } = select( editPostStore );
const _isTemplateMode = isEditingTemplate();
const { isFeatureActive } = select( editPostStore );
const { getRenderingMode } = select( editorStore );
const _isTemplateMode = getRenderingMode() !== 'post-only';
const feature = _isTemplateMode
? 'welcomeGuideTemplate'
: 'welcomeGuide';
Expand Down
8 changes: 7 additions & 1 deletion packages/edit-post/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
store as editorStore,
privateApis as editorPrivateApis,
} from '@wordpress/editor';
import { useMemo } from '@wordpress/element';
import { useEffect, useMemo } from '@wordpress/element';
import { SlotFillProvider } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { store as preferencesStore } from '@wordpress/preferences';
Expand Down Expand Up @@ -142,6 +142,12 @@ function Editor( { postId, postType, settings, initialEdits, ...props } ) {
keepCaretInsideBlock,
] );

// The default mode of the post editor is "post-only" mode.
const { setRenderingMode } = useDispatch( editorStore );
useEffect( () => {
setRenderingMode( 'post-only' );
}, [ setRenderingMode ] );

if ( ! post ) {
return null;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/edit-post/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
registerLegacyWidgetBlock,
registerWidgetGroupBlock,
} from '@wordpress/widgets';
import { store as editorStore } from '@wordpress/editor';

/**
* Internal dependencies
Expand Down Expand Up @@ -93,7 +94,7 @@ export function initializeEditor(
'removeTemplatePartsFromInserter',
( canInsert, blockType ) => {
if (
! select( editPostStore ).isEditingTemplate() &&
select( editorStore ).getRenderingMode() === 'post-only' &&
blockType.name === 'core/template-part'
) {
return false;
Expand All @@ -118,7 +119,7 @@ export function initializeEditor(
{ getBlockParentsByBlockName }
) => {
if (
! select( editPostStore ).isEditingTemplate() &&
select( editorStore ).getRenderingMode() === 'post-only' &&
blockType.name === 'core/post-content'
) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
import { useSelect } from '@wordpress/data';
import { PreferenceToggleMenuItem } from '@wordpress/preferences';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { store as editPostStore } from '../../store';
import { store as editorStore } from '@wordpress/editor';

export default function WelcomeGuideMenuItem() {
const isTemplateMode = useSelect(
( select ) => select( editPostStore ).isEditingTemplate(),
( select ) => select( editorStore ).getRenderingMode() !== 'post-only',
[]
);

Expand Down
18 changes: 9 additions & 9 deletions packages/edit-post/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,14 +513,14 @@ export const setIsListViewOpened =
/**
* Returns an action object used to switch to template editing.
*
* @param {boolean} value Is editing template.
* @return {Object} Action object.
* @deprecated
*/
export function setIsEditingTemplate( value ) {
return {
type: 'SET_IS_EDITING_TEMPLATE',
value,
};
export function setIsEditingTemplate() {
deprecated( "dispatch( 'core/edit-post' ).setIsEditingTemplate", {
since: '6.5',
alternative: "dispatch( 'core/editor').setRenderingMode",
} );
return { type: 'NOTHING' };
}

/**
Expand All @@ -530,8 +530,8 @@ export function setIsEditingTemplate( value ) {
*/
export const __unstableSwitchToTemplateMode =
( newTemplate = false ) =>
( { registry, select, dispatch } ) => {
dispatch( setIsEditingTemplate( true ) );
( { registry, select } ) => {
registry.dispatch( editorStore ).setRenderingMode( 'all' );
const isWelcomeGuideActive = select.isFeatureActive(
'welcomeGuideTemplate'
);
Expand Down
15 changes: 0 additions & 15 deletions packages/edit-post/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,6 @@ export function listViewPanel( state = false, action ) {
return state;
}

/**
* Reducer tracking whether template editing is on or off.
*
* @param {boolean} state
* @param {Object} action
*/
function isEditingTemplate( state = false, action ) {
switch ( action.type ) {
case 'SET_IS_EDITING_TEMPLATE':
return action.value;
}
return state;
}

/**
* Reducer tracking whether meta boxes are initialized.
*
Expand Down Expand Up @@ -196,5 +182,4 @@ export default combineReducers( {
deviceType,
blockInserterPanel,
listViewPanel,
isEditingTemplate,
} );
14 changes: 8 additions & 6 deletions packages/edit-post/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,13 +498,15 @@ export function isListViewOpened( state ) {
/**
* Returns true if the template editing mode is enabled.
*
* @param {Object} state Global application state.
*
* @return {boolean} Whether we're editing the template.
* @deprecated
*/
export function isEditingTemplate( state ) {
return state.isEditingTemplate;
}
export const isEditingTemplate = createRegistrySelector( ( select ) => () => {
deprecated( `select( 'core/edit-post' ).isEditingTemplate`, {
since: '6.5',
alternative: `select( 'core/editor' ).getRenderingMode`,
} );
return select( editorStore ).getRenderingMode() !== 'post-only';
} );

/**
* Returns true if meta boxes are initialized.
Expand Down
Loading

1 comment on commit 6305cf7

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 6305cf7.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7098025111
📝 Reported issues:

Please sign in to comment.