Skip to content

Commit

Permalink
Moving getPageContentFocusType and setPageContentFocusType selector/a…
Browse files Browse the repository at this point in the history
…ction to locked, private methods

In the use-page-content-blocks.js test, add innerblocks to the query test block
  • Loading branch information
ramonjd committed Sep 20, 2023
1 parent 30af53c commit 0724b6d
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 137 deletions.
36 changes: 2 additions & 34 deletions docs/reference-guides/data/data-core-edit-site.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,6 @@ _Returns_

- `Object`: Page.

### getPageContentFocusType

Returns the type of the current page content focus, or null if there is no page content focus.

Possible values are:

- `'disableTemplate'`: Disable the blocks belonging to the page's template.
- `'hideTemplate'`: Hide the blocks belonging to the page's template.

_Parameters_

- _state_ `Object`: Global application state.

_Returns_

- `'disableTemplate'|'hideTemplate'|null`: Type of the current page content focus.

### getReusableBlocks

Returns any available Reusable blocks.
Expand Down Expand Up @@ -150,7 +133,7 @@ _Returns_

### hasPageContentFocus

Whether the site editor allows only page content blocks to be edited, as opposed to any surrounding template entities or template parts.
Whether or not the editor allows only page content to be edited.

_Parameters_

Expand Down Expand Up @@ -312,7 +295,7 @@ _Returns_

### setHasPageContentFocus

Sets whether the editor allows only page content to be edited.
Sets whether or not the editor allows only page content to be edited.

_Parameters_

Expand Down Expand Up @@ -396,21 +379,6 @@ _Returns_

- `number`: The resolved template ID for the page route.

### setPageContentFocusType

Sets the type of page content focus. Can be one of:

- `'disableTemplate'`: Disable the blocks belonging to the page's template.
- `'hideTemplate'`: Hide the blocks belonging to the page's template.

_Parameters_

- _pageContentFocusType_ `'disableTemplate'|'hideTemplate'`: The type of page content focus.

_Returns_

- `Object`: Action object.

### setTemplate

Action that sets a template, optionally fetching it from REST API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ export default function DefaultBlockEditorProvider( { children } ) {
const settings = useSiteEditorSettings();

const { templateType, isTemplateHidden } = useSelect( ( select ) => {
const { getEditedPostType, getPageContentFocusType } =
select( editSiteStore );
const canvasMode = unlock( select( editSiteStore ) ).getCanvasMode();
const { getEditedPostType } = select( editSiteStore );
const { getPageContentFocusType, getCanvasMode } = unlock(
select( editSiteStore )
);
return {
templateType: getEditedPostType(),
isTemplateHidden:
canvasMode === 'edit' &&
getCanvasMode() === 'edit' &&
getPageContentFocusType() === 'hideTemplate',
canvasMode: unlock( select( editSiteStore ) ).getCanvasMode(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ describe( 'usePageContentBlocks', () => {
createBlock( 'core/group', {}, [
createBlock( 'core/post-title' ),
createBlock( 'core/post-featured-image' ),
createBlock( 'core/query' ),
createBlock( 'core/query', {}, [
createBlock( 'core/post-title' ),
createBlock( 'core/post-featured-image' ),
createBlock( 'core/post-content' ),
] ),
createBlock( 'core/post-content' ),
] ),
createBlock( 'core/query' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ const POPOVER_PROPS = {
export default function EditTemplate() {
const { hasResolved, template, isTemplateHidden } = useSelect(
( select ) => {
const {
getEditedPostContext,
getEditedPostType,
getEditedPostId,
getPageContentFocusType,
} = select( editSiteStore );
const canvasMode = unlock(
const { getEditedPostContext, getEditedPostType, getEditedPostId } =
select( editSiteStore );
const { getCanvasMode, getPageContentFocusType } = unlock(
select( editSiteStore )
).getCanvasMode();
);
const { getEditedEntityRecord, hasFinishedResolution } =
select( coreStore );
const _context = getEditedPostContext();
Expand All @@ -54,17 +50,18 @@ export default function EditTemplate() {
queryArgs
),
template: getEditedEntityRecord( ...queryArgs ),
pageContentFocusType: getPageContentFocusType(),
isTemplateHidden:
canvasMode === 'edit' &&
getCanvasMode() === 'edit' &&
getPageContentFocusType() === 'hideTemplate',
};
},
[]
);

const { setHasPageContentFocus, setPageContentFocusType } =
useDispatch( editSiteStore );
const { setHasPageContentFocus } = useDispatch( editSiteStore );
// Disable reason: `useDispatch` can't be called conditionally.
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
const { setPageContentFocusType } = unlock( useDispatch( editSiteStore ) );

if ( ! hasResolved ) {
return null;
Expand Down
21 changes: 2 additions & 19 deletions packages/edit-site/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export const setPage =
.getEntityRecords( 'postType', 'wp_template', {
per_page: -1,
} )
)?.find( ( { slug } ) => slug === currentTemplateSlug );
)?.find( ( { slug } ) => slug === currentTemplateSlug );
if ( currentTemplate ) {
template = currentTemplate;
} else {
Expand Down Expand Up @@ -616,7 +616,7 @@ export const switchEditorMode =
};

/**
* Sets whether the editor allows only page content to be edited.
* Sets whether or not the editor allows only page content to be edited.
*
* @param {boolean} hasPageContentFocus True to allow only page content to be
* edited, false to allow template to be
Expand Down Expand Up @@ -676,20 +676,3 @@ export const toggleDistractionFree =
);
} );
};

/**
* Sets the type of page content focus. Can be one of:
*
* - `'disableTemplate'`: Disable the blocks belonging to the page's template.
* - `'hideTemplate'`: Hide the blocks belonging to the page's template.
*
* @param {'disableTemplate'|'hideTemplate'} pageContentFocusType The type of page content focus.
*
* @return {Object} Action object.
*/
export function setPageContentFocusType( pageContentFocusType ) {
return {
type: 'SET_PAGE_CONTENT_FOCUS_TYPE',
pageContentFocusType,
};
}
19 changes: 19 additions & 0 deletions packages/edit-site/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,22 @@ export const setEditorCanvasContainerView =
view,
} );
};

/**
* Sets the type of page content focus. Can be one of:
*
* - `'disableTemplate'`: Disable the blocks belonging to the page's template.
* - `'hideTemplate'`: Hide the blocks belonging to the page's template.
*
* @param {'disableTemplate'|'hideTemplate'} pageContentFocusType The type of page content focus.
*
* @return {Object} Action object.
*/
export const setPageContentFocusType =
( pageContentFocusType ) =>
( { dispatch } ) => {
dispatch( {
type: 'SET_PAGE_CONTENT_FOCUS_TYPE',
pageContentFocusType,
} );
};
22 changes: 22 additions & 0 deletions packages/edit-site/src/store/private-selectors.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Internal dependencies
*/
import { hasPageContentFocus } from './selectors';

/**
* Returns the current canvas mode.
*
Expand All @@ -19,3 +24,20 @@ export function getCanvasMode( state ) {
export function getEditorCanvasContainerView( state ) {
return state.editorCanvasContainerView;
}

/**
* Returns the type of the current page content focus, or null if there is no
* page content focus.
*
* Possible values are:
*
* - `'disableTemplate'`: Disable the blocks belonging to the page's template.
* - `'hideTemplate'`: Hide the blocks belonging to the page's template.
*
* @param {Object} state Global application state.
*
* @return {'disableTemplate'|'hideTemplate'|null} Type of the current page content focus.
*/
export function getPageContentFocusType( state ) {
return hasPageContentFocus( state ) ? state.pageContentFocusType : null;
}
20 changes: 1 addition & 19 deletions packages/edit-site/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,7 @@ export function isPage( state ) {
}

/**
* Whether the site editor allows only page content blocks to be edited,
* as opposed to any surrounding template entities or template parts.
* Whether or not the editor allows only page content to be edited.
*
* @param {Object} state Global application state.
*
Expand All @@ -376,20 +375,3 @@ export function isPage( state ) {
export function hasPageContentFocus( state ) {
return isPage( state ) ? state.hasPageContentFocus : false;
}

/**
* Returns the type of the current page content focus, or null if there is no
* page content focus.
*
* Possible values are:
*
* - `'disableTemplate'`: Disable the blocks belonging to the page's template.
* - `'hideTemplate'`: Hide the blocks belonging to the page's template.
*
* @param {Object} state Global application state.
*
* @return {'disableTemplate'|'hideTemplate'|null} Type of the current page content focus.
*/
export function getPageContentFocusType( state ) {
return hasPageContentFocus( state ) ? state.pageContentFocusType : null;
}
11 changes: 1 addition & 10 deletions packages/edit-site/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { store as preferencesStore } from '@wordpress/preferences';
* Internal dependencies
*/
import { store as editSiteStore } from '..';
import { setHasPageContentFocus, setPageContentFocusType } from '../actions';
import { setHasPageContentFocus } from '../actions';

const ENTITY_TYPES = {
wp_template: {
Expand Down Expand Up @@ -333,13 +333,4 @@ describe( 'actions', () => {
} );
} );
} );

describe( 'setPageContentFocusType', () => {
it( 'sets the page content focus type', () => {
expect( setPageContentFocusType( 'disableTemplate' ) ).toEqual( {
type: 'SET_PAGE_CONTENT_FOCUS_TYPE',
pageContentFocusType: 'disableTemplate',
} );
} );
} );
} );
38 changes: 0 additions & 38 deletions packages/edit-site/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
__unstableGetPreference,
isPage,
hasPageContentFocus,
getPageContentFocusType,
} from '../selectors';

describe( 'selectors', () => {
Expand Down Expand Up @@ -205,41 +204,4 @@ describe( 'selectors', () => {
expect( hasPageContentFocus( state ) ).toBe( false );
} );
} );

describe( 'getPageContentFocusType', () => {
it( 'returns the current content focus type', () => {
const state = {
editedPost: {
postType: 'wp_template',
context: { postType: 'page', postId: 123 },
},
hasPageContentFocus: true,
pageContentFocusType: 'disableTemplate',
};
expect( getPageContentFocusType( state ) ).toBe(
'disableTemplate'
);
} );

it( 'returns null if the page does not have content focus', () => {
const state = {
editedPost: {
postType: 'wp_template',
context: { postType: 'page', postId: 123 },
},
hasPageContentFocus: false,
};
expect( getPageContentFocusType( state ) ).toBe( null );
} );

it( 'returns null if the edited post type is a template', () => {
const state = {
editedPost: {
postType: 'wp_template',
},
hasPageContentFocus: true,
};
expect( getPageContentFocusType( state ) ).toBe( null );
} );
} );
} );

0 comments on commit 0724b6d

Please sign in to comment.