Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Add isSiteEditorPage util (#9468)
Browse files Browse the repository at this point in the history
* Add isSiteEditorPage util

* fix logic
  • Loading branch information
gigitux authored May 16, 2023
1 parent fdc9c11 commit cabbaa2
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 22 deletions.
5 changes: 3 additions & 2 deletions assets/js/blocks/product-query/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { useSelect } from '@wordpress/data';
import { store as WP_BLOCKS_STORE } from '@wordpress/blocks';
import { isSiteEditorPage } from '@woocommerce/utils';

/**
* Internal dependencies
Expand Down Expand Up @@ -79,7 +80,7 @@ export function isWooInheritQueryEnabled(
export function useAllowedControls(
attributes: ProductQueryBlock[ 'attributes' ]
) {
const isSiteEditor = useSelect( 'core/edit-site' ) !== undefined;
const editSiteStore = useSelect( 'core/edit-site' );

const controls = useSelect(
( select ) =>
Expand All @@ -90,7 +91,7 @@ export function useAllowedControls(
[ attributes ]
);

if ( ! isSiteEditor ) {
if ( ! isSiteEditorPage( editSiteStore ) ) {
return controls.filter( ( control ) => control !== 'wooInherit' );
}

Expand Down
37 changes: 20 additions & 17 deletions assets/js/blocks/product-query/variations/product-query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { stacks } from '@woocommerce/icons';
import { isWpVersion } from '@woocommerce/settings';
import { select, subscribe } from '@wordpress/data';
import { QueryBlockAttributes } from '@woocommerce/blocks/product-query/types';
import { isSiteEditorPage } from '@woocommerce/utils';

/**
* Internal dependencies
Expand Down Expand Up @@ -64,20 +65,16 @@ const registerProductsBlock = ( attributes: QueryBlockAttributes ) => {
};

if ( isWpVersion( '6.1', '>=' ) ) {
const store = select( 'core/edit-site' );

if ( store ) {
let currentTemplateId: string | undefined;

subscribe( () => {
const previousTemplateId = currentTemplateId;

currentTemplateId = store?.getEditedPostId();

if ( previousTemplateId === currentTemplateId ) {
return;
}
let currentTemplateId: string | undefined;
subscribe( () => {
const previousTemplateId = currentTemplateId;
const store = select( 'core/edit-site' );
currentTemplateId = store?.getEditedPostId();
if ( previousTemplateId === currentTemplateId ) {
return;
}

if ( isSiteEditorPage( store ) ) {
const queryAttributes = {
...QUERY_DEFAULT_ATTRIBUTES,
query: {
Expand All @@ -90,8 +87,14 @@ if ( isWpVersion( '6.1', '>=' ) ) {
unregisterBlockVariation( QUERY_LOOP_ID, VARIATION_NAME );

registerProductsBlock( queryAttributes );
} );
} else {
registerProductsBlock( QUERY_DEFAULT_ATTRIBUTES );
}
}
}, 'core/edit-site' );

let isBlockRegistered = false;
subscribe( () => {
if ( ! isBlockRegistered ) {
isBlockRegistered = true;
registerProductsBlock( QUERY_DEFAULT_ATTRIBUTES );
}
}, 'core/edit-post' );
}
9 changes: 7 additions & 2 deletions assets/js/templates/revert-button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { __ } from '@wordpress/i18n';
import { createInterpolateElement, useMemo } from '@wordpress/element';
import { useEntityRecord } from '@wordpress/core-data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { isSiteEditorPage } from '@woocommerce/utils';

// @ts-expect-error: @wordpress/plugin is typed in the newer versions
// eslint-disable-next-line @woocommerce/dependency-group
import {
registerPlugin,
Expand Down Expand Up @@ -140,7 +140,12 @@ let currentTemplateId: string | undefined;
subscribe( () => {
const previousTemplateId = currentTemplateId;
const store = select( 'core/edit-site' );
currentTemplateId = store.getEditedPostId();

if ( ! isSiteEditorPage( store ) ) {
return;
}

currentTemplateId = store?.getEditedPostId();

if ( previousTemplateId === currentTemplateId ) {
return;
Expand Down
1 change: 1 addition & 0 deletions assets/js/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './object-operations';
export * from './products';
export * from './shared-attributes';
export * from './sanitize-html';
export * from './is-site-editor-page';
21 changes: 21 additions & 0 deletions assets/js/utils/is-site-editor-page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Internal dependencies
*/
import { isObject } from '../types/type-guards';

export const isSiteEditorPage = ( store: unknown ): boolean => {
if ( isObject( store ) ) {
const editedPostType = (
store as {
getEditedPostType: () => string;
}
).getEditedPostType();

return (
editedPostType === 'wp_template' ||
editedPostType === 'wp_template_part'
);
}

return false;
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"./assets/js/blocks/filter-wrapper/register-components.ts",
"./assets/js/blocks/product-query/variations/**.tsx",
"./assets/js/blocks/product-query/index.tsx",
"./assets/js/blocks/product-query/inspector-controls.tsx"
"./assets/js/blocks/product-query/inspector-controls.tsx",
"./assets/js/templates/revert-button/index.tsx"
],
"repository": {
"type": "git",
Expand Down

0 comments on commit cabbaa2

Please sign in to comment.