2024-06-18 12:16:16 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-07-01 09:21:06 +00:00
|
|
|
import {
|
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
useMemo,
|
|
|
|
useRef,
|
|
|
|
useState,
|
|
|
|
} from '@wordpress/element';
|
2024-07-03 07:25:12 +00:00
|
|
|
import { useAsyncList } from '@wordpress/compose';
|
2024-06-18 12:16:16 +00:00
|
|
|
import { useSelect, useDispatch, select } from '@wordpress/data';
|
2024-06-21 13:22:18 +00:00
|
|
|
import { BlockInstance, cloneBlock } from '@wordpress/blocks';
|
2024-06-18 12:16:16 +00:00
|
|
|
import { close } from '@wordpress/icons';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { getNewPath, navigateTo } from '@woocommerce/navigation';
|
|
|
|
import { capitalize } from 'lodash';
|
|
|
|
import { Button, Spinner } from '@wordpress/components';
|
|
|
|
import {
|
|
|
|
unlock,
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
} from '@wordpress/edit-site/build-module/lock-unlock';
|
2024-07-01 09:21:06 +00:00
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
// eslint-disable-next-line @woocommerce/dependency-group
|
|
|
|
import { useIsSiteEditorLoading } from '@wordpress/edit-site/build-module/components/layout/hooks';
|
2024-06-18 12:16:16 +00:00
|
|
|
// eslint-disable-next-line @woocommerce/dependency-group
|
|
|
|
import {
|
|
|
|
store as coreStore,
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
} from '@wordpress/core-data';
|
|
|
|
// eslint-disable-next-line @woocommerce/dependency-group
|
|
|
|
import {
|
|
|
|
__experimentalBlockPatternsList as BlockPatternList,
|
|
|
|
store as blockEditorStore,
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
} from '@wordpress/block-editor';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { usePatternsByCategory } from '../../hooks/use-patterns';
|
|
|
|
import './style.scss';
|
|
|
|
import { useEditorBlocks } from '../../hooks/use-editor-blocks';
|
|
|
|
import { PATTERN_CATEGORIES } from './categories';
|
2024-07-02 14:39:49 +00:00
|
|
|
import { THEME_SLUG } from '~/customize-store/data/constants';
|
2024-07-05 12:41:27 +00:00
|
|
|
import {
|
2024-07-19 10:15:10 +00:00
|
|
|
findButtonBlockInsideCoverBlockWithBlackBackgroundPatternAndUpdate,
|
2024-07-05 12:41:27 +00:00
|
|
|
PRODUCT_HERO_PATTERN_BUTTON_STYLE,
|
2024-07-19 10:15:10 +00:00
|
|
|
} from '../../utils/black-background-pattern-update-button';
|
2024-07-05 12:41:27 +00:00
|
|
|
import { useIsActiveNewNeutralVariation } from '../../hooks/use-is-active-new-neutral-variation';
|
2024-07-16 09:52:58 +00:00
|
|
|
import {
|
|
|
|
sortPatternsByCategory,
|
|
|
|
addIsAddedClassToPatternPreview,
|
|
|
|
} from './utils';
|
2024-07-16 11:24:55 +00:00
|
|
|
import { trackEvent } from '~/customize-store/tracking';
|
2024-06-18 12:16:16 +00:00
|
|
|
|
|
|
|
export const SidebarPatternScreen = ( { category }: { category: string } ) => {
|
|
|
|
const { patterns, isLoading } = usePatternsByCategory( category );
|
|
|
|
|
2024-07-05 12:41:27 +00:00
|
|
|
const isActiveNewNeutralVariation = useIsActiveNewNeutralVariation();
|
2024-07-02 14:39:49 +00:00
|
|
|
const sortedPatterns = useMemo( () => {
|
|
|
|
const patternsWithoutThemePatterns = patterns.filter(
|
|
|
|
( pattern ) =>
|
|
|
|
! pattern.name.includes( THEME_SLUG ) &&
|
|
|
|
pattern.source !== 'pattern-directory/theme' &&
|
|
|
|
pattern.source !== 'pattern-directory/core'
|
|
|
|
);
|
|
|
|
|
2024-07-05 12:41:27 +00:00
|
|
|
const patternWithPatchedProductHeroPattern =
|
|
|
|
patternsWithoutThemePatterns.map( ( pattern ) => {
|
|
|
|
if (
|
2024-07-19 10:15:10 +00:00
|
|
|
pattern.name !==
|
|
|
|
'woocommerce-blocks/just-arrived-full-hero' &&
|
|
|
|
pattern.name !==
|
|
|
|
'woocommerce-blocks/featured-category-cover-image'
|
2024-07-05 12:41:27 +00:00
|
|
|
) {
|
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isActiveNewNeutralVariation ) {
|
|
|
|
const blocks =
|
2024-07-19 10:15:10 +00:00
|
|
|
findButtonBlockInsideCoverBlockWithBlackBackgroundPatternAndUpdate(
|
2024-07-05 12:41:27 +00:00
|
|
|
pattern.blocks,
|
2024-07-19 10:15:10 +00:00
|
|
|
( patternBlocks: BlockInstance[] ) => {
|
|
|
|
patternBlocks.forEach(
|
|
|
|
( block: BlockInstance ) =>
|
|
|
|
( block.attributes.style = {} )
|
|
|
|
);
|
2024-07-05 12:41:27 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
return { ...pattern, blocks };
|
|
|
|
}
|
|
|
|
|
|
|
|
const blocks =
|
2024-07-19 10:15:10 +00:00
|
|
|
findButtonBlockInsideCoverBlockWithBlackBackgroundPatternAndUpdate(
|
2024-07-05 12:41:27 +00:00
|
|
|
pattern.blocks,
|
2024-07-19 10:15:10 +00:00
|
|
|
( patternBlocks: BlockInstance[] ) => {
|
|
|
|
patternBlocks.forEach(
|
|
|
|
( block ) =>
|
|
|
|
( block.attributes.style =
|
|
|
|
PRODUCT_HERO_PATTERN_BUTTON_STYLE )
|
|
|
|
);
|
2024-07-05 12:41:27 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return { ...pattern, blocks };
|
|
|
|
} );
|
|
|
|
|
2024-07-02 14:39:49 +00:00
|
|
|
return sortPatternsByCategory(
|
2024-07-05 12:41:27 +00:00
|
|
|
patternWithPatchedProductHeroPattern,
|
2024-07-02 14:39:49 +00:00
|
|
|
category as keyof typeof PATTERN_CATEGORIES
|
|
|
|
);
|
2024-07-05 12:41:27 +00:00
|
|
|
}, [ category, isActiveNewNeutralVariation, patterns ] );
|
2024-07-02 14:39:49 +00:00
|
|
|
|
2024-07-03 07:25:12 +00:00
|
|
|
const asyncSortedPatterns = useAsyncList( sortedPatterns );
|
|
|
|
|
2024-06-18 12:16:16 +00:00
|
|
|
const [ patternPagination, setPatternPagination ] = useState( 10 );
|
|
|
|
|
|
|
|
const refElement = useRef< HTMLDivElement >( null );
|
|
|
|
|
|
|
|
const currentTemplate = useSelect(
|
|
|
|
( sel ) =>
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
sel( coreStore ).__experimentalGetTemplateForLink( '/' ),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const [ blocks ] = useEditorBlocks(
|
|
|
|
'wp_template',
|
|
|
|
currentTemplate?.id ?? ''
|
|
|
|
);
|
|
|
|
|
2024-07-01 09:21:06 +00:00
|
|
|
const blockToScroll = useRef< string | null >( null );
|
|
|
|
|
|
|
|
const isEditorLoading = useIsSiteEditorLoading();
|
|
|
|
|
|
|
|
const isSpinnerVisible = isLoading || isEditorLoading;
|
|
|
|
|
2024-07-09 07:12:33 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( isSpinnerVisible || refElement.current === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want to add the is-added class to the pattern preview when the pattern is loaded in the editor and for each mutation.
|
|
|
|
addIsAddedClassToPatternPreview( refElement.current, blocks );
|
|
|
|
|
|
|
|
const observer = new MutationObserver( () => {
|
|
|
|
addIsAddedClassToPatternPreview(
|
|
|
|
refElement.current as HTMLElement,
|
|
|
|
blocks
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
const previewPatternList = document.querySelector(
|
|
|
|
'.woocommerce-customize-store-edit-site-layout__sidebar-extra__pattern .block-editor-block-patterns-list'
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( previewPatternList ) {
|
|
|
|
observer.observe( previewPatternList, {
|
|
|
|
childList: true,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
observer.disconnect();
|
|
|
|
};
|
|
|
|
}, [ isLoading, blocks, isSpinnerVisible ] );
|
|
|
|
|
2024-07-01 09:21:06 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( isEditorLoading ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const iframe = window.document.querySelector(
|
|
|
|
'.woocommerce-customize-store-assembler > iframe[name="editor-canvas"]'
|
|
|
|
) as HTMLIFrameElement;
|
|
|
|
|
|
|
|
const blockList = iframe?.contentWindow?.document.body.querySelector(
|
|
|
|
'.block-editor-block-list__layout'
|
|
|
|
);
|
|
|
|
|
|
|
|
const observer = new MutationObserver( () => {
|
|
|
|
if ( blockToScroll.current ) {
|
|
|
|
const block = blockList?.querySelector(
|
|
|
|
`[id="block-${ blockToScroll.current }"]`
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( block ) {
|
|
|
|
block.scrollIntoView();
|
|
|
|
blockToScroll.current = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( blockList ) {
|
|
|
|
observer.observe( blockList, { childList: true } );
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
observer.disconnect();
|
|
|
|
};
|
|
|
|
}, [ isEditorLoading ] );
|
|
|
|
|
2024-06-18 12:16:16 +00:00
|
|
|
// @ts-expect-error No types for this exist yet.
|
2024-07-01 09:21:06 +00:00
|
|
|
const { insertBlocks } = useDispatch( blockEditorStore );
|
2024-06-18 12:16:16 +00:00
|
|
|
|
|
|
|
const insertableIndex = useMemo( () => {
|
|
|
|
return blocks.findLastIndex(
|
|
|
|
( block ) => block.name === 'core/template-part'
|
|
|
|
);
|
|
|
|
}, [ blocks ] );
|
|
|
|
|
|
|
|
const onClickPattern = useCallback(
|
|
|
|
( pattern ) => {
|
|
|
|
const parsedPattern = unlock(
|
|
|
|
select( blockEditorStore )
|
|
|
|
).__experimentalGetParsedPattern( pattern.name );
|
|
|
|
|
2024-06-21 13:22:18 +00:00
|
|
|
const cloneBlocks = parsedPattern.blocks.map(
|
|
|
|
( blockInstance: BlockInstance ) => cloneBlock( blockInstance )
|
|
|
|
);
|
2024-06-18 12:16:16 +00:00
|
|
|
|
2024-07-19 10:15:10 +00:00
|
|
|
if ( ! isActiveNewNeutralVariation ) {
|
|
|
|
const updatedBlocks =
|
|
|
|
findButtonBlockInsideCoverBlockWithBlackBackgroundPatternAndUpdate(
|
|
|
|
cloneBlocks,
|
|
|
|
( patternBlocks: BlockInstance[] ) => {
|
|
|
|
patternBlocks.forEach(
|
|
|
|
( block: BlockInstance ) =>
|
|
|
|
( block.attributes.style = {} )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2024-06-18 12:16:16 +00:00
|
|
|
|
2024-07-19 10:15:10 +00:00
|
|
|
insertBlocks(
|
|
|
|
updatedBlocks,
|
|
|
|
insertableIndex,
|
|
|
|
undefined,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
blockToScroll.current = updatedBlocks[ 0 ].clientId;
|
|
|
|
} else {
|
|
|
|
const updatedBlocks =
|
|
|
|
findButtonBlockInsideCoverBlockWithBlackBackgroundPatternAndUpdate(
|
|
|
|
cloneBlocks,
|
|
|
|
( patternBlocks: BlockInstance[] ) => {
|
|
|
|
patternBlocks.forEach(
|
|
|
|
( block ) =>
|
|
|
|
( block.attributes.style =
|
|
|
|
PRODUCT_HERO_PATTERN_BUTTON_STYLE )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
insertBlocks(
|
|
|
|
updatedBlocks,
|
|
|
|
insertableIndex,
|
|
|
|
undefined,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
blockToScroll.current = updatedBlocks[ 0 ].clientId;
|
|
|
|
}
|
2024-07-16 11:24:55 +00:00
|
|
|
|
|
|
|
trackEvent(
|
|
|
|
'customize_your_store_assembler_pattern_sidebar_click',
|
|
|
|
{ pattern: pattern.name }
|
|
|
|
);
|
2024-06-18 12:16:16 +00:00
|
|
|
},
|
2024-07-19 10:15:10 +00:00
|
|
|
[ insertBlocks, insertableIndex, isActiveNewNeutralVariation ]
|
2024-06-18 12:16:16 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="woocommerce-customize-store-edit-site-layout__sidebar-extra__pattern"
|
|
|
|
onScroll={ ( event ) => {
|
|
|
|
const element = event.target as HTMLElement;
|
|
|
|
const scrollTop = element.scrollTop;
|
|
|
|
const percentage =
|
|
|
|
scrollTop / ( element.scrollHeight - element.clientHeight );
|
|
|
|
|
|
|
|
if ( percentage > 0.5 ) {
|
|
|
|
setPatternPagination( ( prev ) => prev + 10 );
|
|
|
|
}
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
<div className="woocommerce-customize-store-edit-site-layout__sidebar-extra__pattern__header">
|
|
|
|
<h1>
|
|
|
|
{ capitalize(
|
|
|
|
PATTERN_CATEGORIES[
|
|
|
|
category as keyof typeof PATTERN_CATEGORIES
|
|
|
|
].label
|
|
|
|
) }
|
|
|
|
</h1>
|
|
|
|
<Button
|
|
|
|
onClick={ () => {
|
|
|
|
const homepageUrl = getNewPath(
|
|
|
|
{ customizing: true },
|
|
|
|
`/customize-store/assembler-hub/homepage`,
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
navigateTo( { url: homepageUrl } );
|
2024-07-16 11:24:55 +00:00
|
|
|
trackEvent(
|
|
|
|
'customize_your_store_assembler_pattern_sidebar_close'
|
|
|
|
);
|
2024-06-18 12:16:16 +00:00
|
|
|
} }
|
2024-07-02 08:03:56 +00:00
|
|
|
iconSize={ 18 }
|
2024-06-18 12:16:16 +00:00
|
|
|
icon={ close }
|
|
|
|
label={ __( 'Close', 'woocommerce' ) }
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="woocommerce-customize-store-edit-site-layout__sidebar-extra__pattern__description">
|
|
|
|
<span>
|
|
|
|
{
|
|
|
|
PATTERN_CATEGORIES[
|
|
|
|
category as keyof typeof PATTERN_CATEGORIES
|
|
|
|
].description
|
|
|
|
}
|
|
|
|
</span>
|
|
|
|
</div>
|
2024-07-01 09:21:06 +00:00
|
|
|
{ isSpinnerVisible && (
|
2024-06-18 12:16:16 +00:00
|
|
|
<span className="components-placeholder__preview">
|
|
|
|
<Spinner />
|
|
|
|
</span>
|
|
|
|
) }
|
2024-07-01 09:21:06 +00:00
|
|
|
{ ! isSpinnerVisible && (
|
2024-06-18 12:16:16 +00:00
|
|
|
<BlockPatternList
|
2024-07-03 07:25:12 +00:00
|
|
|
shownPatterns={ asyncSortedPatterns.slice(
|
2024-07-02 14:39:49 +00:00
|
|
|
0,
|
|
|
|
patternPagination
|
|
|
|
) }
|
2024-07-03 07:25:12 +00:00
|
|
|
blockPatterns={ asyncSortedPatterns.slice(
|
2024-07-02 14:39:49 +00:00
|
|
|
0,
|
|
|
|
patternPagination
|
|
|
|
) }
|
2024-06-18 12:16:16 +00:00
|
|
|
onClickPattern={ onClickPattern }
|
|
|
|
label={ 'Homepage' }
|
|
|
|
orientation="vertical"
|
|
|
|
category={ category }
|
|
|
|
isDraggable={ false }
|
|
|
|
showTitlesAsTooltip={ true }
|
|
|
|
ref={ refElement }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|