2024-03-12 14:05:12 +00:00
|
|
|
/* eslint-disable @woocommerce/dependency-group */
|
|
|
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-05-08 07:36:45 +00:00
|
|
|
import {
|
2024-06-06 07:25:07 +00:00
|
|
|
store as blockEditorStore,
|
2024-05-08 07:36:45 +00:00
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
} from '@wordpress/block-editor';
|
2024-03-12 14:05:12 +00:00
|
|
|
// @ts-expect-error No types for this exist yet.
|
2024-06-05 12:27:35 +00:00
|
|
|
import { store as coreStore } from '@wordpress/core-data';
|
2024-03-20 13:28:56 +00:00
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2024-06-06 07:25:07 +00:00
|
|
|
import { useQuery } from '@woocommerce/navigation';
|
2024-03-12 14:05:12 +00:00
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
import { unlock } from '@wordpress/edit-site/build-module/lock-unlock';
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
import useSiteEditorSettings from '@wordpress/edit-site/build-module/components/block-editor/use-site-editor-settings';
|
2024-06-05 12:27:35 +00:00
|
|
|
import { useContext, useEffect, useMemo } from '@wordpress/element';
|
2024-06-06 07:25:07 +00:00
|
|
|
import { BlockInstance, createBlock } from '@wordpress/blocks';
|
2024-03-15 17:01:56 +00:00
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
import { store as editSiteStore } from '@wordpress/edit-site/build-module/store';
|
2024-03-12 14:05:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { CustomizeStoreContext } from './';
|
|
|
|
import { BlockEditor } from './block-editor';
|
|
|
|
import { HighlightedBlockContext } from './context/highlighted-block-context';
|
2024-06-06 07:25:07 +00:00
|
|
|
import { useAddNoBlocksPlaceholder } from './hooks/block-placeholder/use-add-no-blocks-placeholder';
|
2024-03-12 14:05:12 +00:00
|
|
|
import { useEditorBlocks } from './hooks/use-editor-blocks';
|
|
|
|
import { useScrollOpacity } from './hooks/use-scroll-opacity';
|
2024-05-08 07:36:45 +00:00
|
|
|
import {
|
|
|
|
PRODUCT_HERO_PATTERN_BUTTON_STYLE,
|
2024-07-19 10:15:10 +00:00
|
|
|
findButtonBlockInsideCoverBlockWithBlackBackgroundPatternAndUpdate,
|
|
|
|
} 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-03-12 14:05:12 +00:00
|
|
|
|
|
|
|
export const BlockEditorContainer = () => {
|
|
|
|
const settings = useSiteEditorSettings();
|
|
|
|
|
|
|
|
const currentTemplate:
|
|
|
|
| {
|
|
|
|
id: string;
|
|
|
|
}
|
|
|
|
| undefined = useSelect(
|
|
|
|
( select ) =>
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
select( coreStore ).__experimentalGetTemplateForLink( '/' ),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2024-03-15 17:01:56 +00:00
|
|
|
// This is necessary to avoid this issue: https://github.com/woocommerce/woocommerce/issues/45593
|
|
|
|
// Related PR: https://github.com/woocommerce/woocommerce/pull/45600
|
|
|
|
const { templateType } = useSelect( ( select ) => {
|
|
|
|
const { getEditedPostType } = unlock( select( editSiteStore ) );
|
|
|
|
|
|
|
|
return {
|
|
|
|
templateType: getEditedPostType(),
|
|
|
|
};
|
|
|
|
}, [] );
|
|
|
|
|
2024-03-12 14:05:12 +00:00
|
|
|
const [ blocks, , onChange ] = useEditorBlocks(
|
2024-03-15 17:01:56 +00:00
|
|
|
templateType,
|
2024-03-12 14:05:12 +00:00
|
|
|
currentTemplate?.id ?? ''
|
|
|
|
);
|
|
|
|
|
|
|
|
const urlParams = useQuery();
|
|
|
|
const { currentState } = useContext( CustomizeStoreContext );
|
|
|
|
|
|
|
|
const scrollDirection =
|
|
|
|
urlParams.path === '/customize-store/assembler-hub/footer'
|
|
|
|
? 'bottomUp'
|
|
|
|
: 'topDown';
|
|
|
|
|
|
|
|
const previewOpacity = useScrollOpacity(
|
|
|
|
'.woocommerce-customize-store__block-editor iframe',
|
|
|
|
scrollDirection
|
|
|
|
);
|
|
|
|
|
|
|
|
const { highlightedBlockClientId } = useContext( HighlightedBlockContext );
|
|
|
|
const isHighlighting = highlightedBlockClientId !== null;
|
|
|
|
const additionalStyles = isHighlighting
|
|
|
|
? `
|
|
|
|
.wp-block.preview-opacity {
|
|
|
|
opacity: ${ previewOpacity };
|
|
|
|
}
|
|
|
|
`
|
|
|
|
: '';
|
|
|
|
|
|
|
|
const opacityClass = 'preview-opacity';
|
|
|
|
|
2024-03-20 13:28:56 +00:00
|
|
|
const clientIds = blocks.map( ( block ) => block.clientId );
|
|
|
|
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
const { updateBlockAttributes } = useDispatch( blockEditorStore );
|
|
|
|
|
2024-07-05 12:41:27 +00:00
|
|
|
const isActiveNewNeutralVariation = useIsActiveNewNeutralVariation();
|
2024-05-08 07:36:45 +00:00
|
|
|
|
2024-07-05 12:41:27 +00:00
|
|
|
useEffect( () => {
|
2024-05-08 07:36:45 +00:00
|
|
|
if ( ! isActiveNewNeutralVariation ) {
|
2024-07-19 10:15:10 +00:00
|
|
|
findButtonBlockInsideCoverBlockWithBlackBackgroundPatternAndUpdate(
|
2024-05-08 07:36:45 +00:00
|
|
|
blocks,
|
2024-07-19 10:15:10 +00:00
|
|
|
( buttonBlocks: BlockInstance[] ) => {
|
|
|
|
const buttonBlockClientIds = buttonBlocks.map(
|
|
|
|
( { clientId } ) => clientId
|
|
|
|
);
|
|
|
|
|
|
|
|
updateBlockAttributes( buttonBlockClientIds, {
|
2024-05-08 07:36:45 +00:00
|
|
|
style: {},
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
);
|
2024-07-19 10:15:10 +00:00
|
|
|
|
2024-05-08 07:36:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-07-19 10:15:10 +00:00
|
|
|
|
|
|
|
findButtonBlockInsideCoverBlockWithBlackBackgroundPatternAndUpdate(
|
2024-05-08 07:36:45 +00:00
|
|
|
blocks,
|
2024-07-19 10:15:10 +00:00
|
|
|
( buttonBlocks: BlockInstance[] ) => {
|
|
|
|
const buttonBlockClientIds = buttonBlocks.map(
|
|
|
|
( { clientId } ) => clientId
|
|
|
|
);
|
|
|
|
updateBlockAttributes( buttonBlockClientIds, {
|
2024-05-08 07:36:45 +00:00
|
|
|
style: PRODUCT_HERO_PATTERN_BUTTON_STYLE,
|
|
|
|
// This is necessary; otherwise, the style won't be applied on the frontend during the style variation change.
|
|
|
|
className: '',
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
);
|
2024-07-19 10:15:10 +00:00
|
|
|
// Blocks are not part of the dependencies because we don't want to trigger this effect when the blocks change. This would cause an infinite loop.
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [ isActiveNewNeutralVariation, updateBlockAttributes ] );
|
2024-05-08 07:36:45 +00:00
|
|
|
|
2024-06-06 07:25:07 +00:00
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
const { insertBlock, removeBlock } = useDispatch( blockEditorStore );
|
|
|
|
|
|
|
|
useAddNoBlocksPlaceholder( {
|
|
|
|
blocks,
|
|
|
|
createBlock,
|
|
|
|
insertBlock,
|
|
|
|
removeBlock,
|
|
|
|
} );
|
|
|
|
|
2024-03-20 13:28:56 +00:00
|
|
|
useEffect( () => {
|
|
|
|
const { blockIdToHighlight, restOfBlockIds } = clientIds.reduce(
|
|
|
|
( acc, clientId ) => {
|
2024-03-12 14:05:12 +00:00
|
|
|
if (
|
|
|
|
! isHighlighting ||
|
2024-03-20 13:28:56 +00:00
|
|
|
clientId === highlightedBlockClientId
|
2024-03-12 14:05:12 +00:00
|
|
|
) {
|
|
|
|
return {
|
2024-03-20 13:28:56 +00:00
|
|
|
blockIdToHighlight: clientId,
|
|
|
|
restOfBlockIds: acc.restOfBlockIds,
|
2024-03-12 14:05:12 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2024-03-20 13:28:56 +00:00
|
|
|
blockIdToHighlight: acc.blockIdToHighlight,
|
|
|
|
restOfBlockIds: [ ...acc.restOfBlockIds, clientId ],
|
2024-03-12 14:05:12 +00:00
|
|
|
};
|
2024-03-20 13:28:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
blockIdToHighlight: null,
|
|
|
|
restOfBlockIds: [],
|
|
|
|
} as {
|
|
|
|
blockIdToHighlight: string | null;
|
|
|
|
restOfBlockIds: string[];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
updateBlockAttributes( blockIdToHighlight, {
|
|
|
|
className: '',
|
|
|
|
} );
|
|
|
|
|
|
|
|
updateBlockAttributes( restOfBlockIds, {
|
|
|
|
className: ` ${ opacityClass }`,
|
|
|
|
} );
|
|
|
|
}, [
|
|
|
|
clientIds,
|
|
|
|
highlightedBlockClientId,
|
|
|
|
isHighlighting,
|
|
|
|
updateBlockAttributes,
|
|
|
|
] );
|
2024-03-12 14:05:12 +00:00
|
|
|
|
|
|
|
const isScrollable = useMemo(
|
|
|
|
() =>
|
|
|
|
// Disable scrollable for transitional screen
|
|
|
|
! (
|
|
|
|
typeof currentState === 'object' &&
|
|
|
|
currentState.transitionalScreen === 'transitional'
|
|
|
|
),
|
|
|
|
[ currentState ]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<BlockEditor
|
2024-03-20 13:28:56 +00:00
|
|
|
renderedBlocks={ blocks }
|
2024-03-12 14:05:12 +00:00
|
|
|
isScrollable={ isScrollable }
|
|
|
|
onChange={ onChange }
|
|
|
|
settings={ settings }
|
|
|
|
additionalStyles={ additionalStyles }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|