2023-08-28 01:28:05 +00:00
|
|
|
// Reference: https://github.com/WordPress/gutenberg/tree/v16.4.0/packages/edit-site/src/index.js
|
|
|
|
/* eslint-disable @woocommerce/dependency-group */
|
|
|
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-12-01 02:23:00 +00:00
|
|
|
import { createContext, useEffect, useRef, useState } from '@wordpress/element';
|
2023-11-17 11:35:52 +00:00
|
|
|
import { dispatch } from '@wordpress/data';
|
2023-08-28 01:28:05 +00:00
|
|
|
import {
|
|
|
|
__experimentalFetchLinkSuggestions as fetchLinkSuggestions,
|
|
|
|
__experimentalFetchUrlData as fetchUrlData,
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
} from '@wordpress/core-data';
|
|
|
|
// eslint-disable-next-line @woocommerce/dependency-group
|
|
|
|
import {
|
|
|
|
registerCoreBlocks,
|
|
|
|
__experimentalGetCoreBlocks,
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
} from '@wordpress/block-library';
|
2023-09-05 06:21:19 +00:00
|
|
|
import {
|
|
|
|
getBlockType,
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
store as blocksStore,
|
|
|
|
} from '@wordpress/blocks';
|
2023-08-28 01:28:05 +00:00
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
import { privateApis as routerPrivateApis } from '@wordpress/router';
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
import { unlock } from '@wordpress/edit-site/build-module/lock-unlock';
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
import { ShortcutProvider } from '@wordpress/keyboard-shortcuts';
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
import { store as preferencesStore } from '@wordpress/preferences';
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
import { store as editorStore } from '@wordpress/editor';
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
import { store as editSiteStore } from '@wordpress/edit-site/build-module/store';
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
import { GlobalStylesProvider } from '@wordpress/edit-site/build-module/components/global-styles/global-styles-provider';
|
2023-09-05 06:21:19 +00:00
|
|
|
import { MediaUpload } from '@wordpress/media-utils';
|
|
|
|
import { addFilter } from '@wordpress/hooks';
|
2023-08-28 01:28:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { CustomizeStoreComponent } from '../types';
|
|
|
|
import { Layout } from './layout';
|
|
|
|
import './style.scss';
|
2023-09-29 05:20:29 +00:00
|
|
|
import { PreloadFonts } from './preload-fonts';
|
2023-12-01 02:23:00 +00:00
|
|
|
import { GoBackWarningModal } from './go-back-warning-modal';
|
|
|
|
import { onBackButtonClicked } from '../utils';
|
|
|
|
import { getNewPath } from '@woocommerce/navigation';
|
2024-05-28 06:55:14 +00:00
|
|
|
import useBodyClass from '../hooks/use-body-class';
|
2023-08-28 01:28:05 +00:00
|
|
|
|
|
|
|
const { RouterProvider } = unlock( routerPrivateApis );
|
|
|
|
|
2023-09-05 06:21:19 +00:00
|
|
|
addFilter(
|
|
|
|
'editor.MediaUpload',
|
|
|
|
'woo/customize-store/assembler-hub',
|
|
|
|
() => MediaUpload
|
|
|
|
);
|
2023-08-28 01:28:05 +00:00
|
|
|
type CustomizeStoreComponentProps = Parameters< CustomizeStoreComponent >[ 0 ];
|
|
|
|
|
|
|
|
export const CustomizeStoreContext = createContext< {
|
|
|
|
sendEvent: CustomizeStoreComponentProps[ 'sendEvent' ];
|
2024-01-11 15:12:41 +00:00
|
|
|
context: CustomizeStoreComponentProps[ 'context' ];
|
2023-10-05 12:36:08 +00:00
|
|
|
currentState: CustomizeStoreComponentProps[ 'currentState' ];
|
2023-08-28 01:28:05 +00:00
|
|
|
} >( {
|
|
|
|
sendEvent: () => {},
|
2024-01-11 15:12:41 +00:00
|
|
|
context: {} as CustomizeStoreComponentProps[ 'context' ],
|
2023-10-05 12:36:08 +00:00
|
|
|
currentState: 'assemblerHub',
|
2023-08-28 01:28:05 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
export type events =
|
|
|
|
| { type: 'FINISH_CUSTOMIZATION' }
|
2024-01-11 15:12:41 +00:00
|
|
|
| { type: 'GO_BACK_TO_DESIGN_WITH_AI' }
|
|
|
|
| { type: 'GO_BACK_TO_DESIGN_WITHOUT_AI' };
|
2023-08-28 01:28:05 +00:00
|
|
|
|
2023-11-17 11:35:52 +00:00
|
|
|
const initializeAssembleHub = () => {
|
|
|
|
if ( ! window.wcBlockSettings ) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.warn(
|
|
|
|
'window.blockSettings not found. Skipping initialization.'
|
2023-08-28 01:28:05 +00:00
|
|
|
);
|
2023-11-17 11:35:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-08-28 01:28:05 +00:00
|
|
|
|
2023-11-17 11:35:52 +00:00
|
|
|
// Set up the block editor settings.
|
|
|
|
const settings = window.wcBlockSettings;
|
|
|
|
settings.__experimentalFetchLinkSuggestions = (
|
|
|
|
search: string,
|
|
|
|
searchOptions: {
|
|
|
|
isInitialSuggestions: boolean;
|
|
|
|
type: 'attachment' | 'post' | 'term' | 'post-format';
|
|
|
|
subtype: string;
|
|
|
|
page: number;
|
|
|
|
perPage: number;
|
|
|
|
}
|
|
|
|
) => fetchLinkSuggestions( search, searchOptions, settings );
|
|
|
|
settings.__experimentalFetchRichUrlData = fetchUrlData;
|
2023-08-28 01:28:05 +00:00
|
|
|
|
2023-11-17 11:35:52 +00:00
|
|
|
const reapplyBlockTypeFilters =
|
2023-08-28 01:28:05 +00:00
|
|
|
// @ts-ignore No types for this exist yet.
|
2023-11-17 11:35:52 +00:00
|
|
|
dispatch( blocksStore ).__experimentalReapplyBlockTypeFilters || // GB < 16.6
|
2023-08-28 01:28:05 +00:00
|
|
|
// @ts-ignore No types for this exist yet.
|
2023-11-17 11:35:52 +00:00
|
|
|
dispatch( blocksStore ).reapplyBlockTypeFilters; // GB >= 16.6
|
|
|
|
reapplyBlockTypeFilters();
|
2023-08-28 01:28:05 +00:00
|
|
|
|
2023-11-17 11:35:52 +00:00
|
|
|
const coreBlocks = __experimentalGetCoreBlocks().filter(
|
|
|
|
( { name }: { name: string } ) =>
|
|
|
|
name !== 'core/freeform' && ! getBlockType( name )
|
|
|
|
);
|
|
|
|
registerCoreBlocks( coreBlocks );
|
|
|
|
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
dispatch( blocksStore ).setFreeformFallbackBlockName( 'core/html' );
|
|
|
|
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
dispatch( preferencesStore ).setDefaults( 'core/edit-site', {
|
|
|
|
editorMode: 'visual',
|
|
|
|
fixedToolbar: false,
|
|
|
|
focusMode: false,
|
|
|
|
distractionFree: false,
|
|
|
|
keepCaretInsideBlock: false,
|
|
|
|
welcomeGuide: false,
|
|
|
|
welcomeGuideStyles: false,
|
|
|
|
welcomeGuidePage: false,
|
|
|
|
welcomeGuideTemplate: false,
|
|
|
|
showListViewByDefault: false,
|
|
|
|
showBlockBreadcrumbs: true,
|
|
|
|
} );
|
2024-06-05 12:27:35 +00:00
|
|
|
|
2023-11-17 11:35:52 +00:00
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
dispatch( editSiteStore ).updateSettings( settings );
|
|
|
|
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
dispatch( editorStore ).updateEditorSettings( {
|
|
|
|
defaultTemplateTypes: settings.defaultTemplateTypes,
|
|
|
|
defaultTemplatePartAreas: settings.defaultTemplatePartAreas,
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Prevent the default browser action for files dropped outside of dropzones.
|
|
|
|
window.addEventListener( 'dragover', ( e ) => e.preventDefault(), false );
|
|
|
|
window.addEventListener( 'drop', ( e ) => e.preventDefault(), false );
|
|
|
|
|
|
|
|
unlock( dispatch( editSiteStore ) ).setCanvasMode( 'view' );
|
|
|
|
};
|
|
|
|
|
|
|
|
export const AssemblerHub: CustomizeStoreComponent = ( props ) => {
|
|
|
|
const isInitializedRef = useRef( false );
|
2023-08-28 01:28:05 +00:00
|
|
|
|
2024-05-28 06:55:14 +00:00
|
|
|
useBodyClass( 'woocommerce-assembler' );
|
|
|
|
|
2023-11-17 11:35:52 +00:00
|
|
|
if ( ! isInitializedRef.current ) {
|
|
|
|
initializeAssembleHub();
|
|
|
|
isInitializedRef.current = true;
|
|
|
|
}
|
2023-08-28 01:28:05 +00:00
|
|
|
|
2023-12-01 02:23:00 +00:00
|
|
|
const [ showExitModal, setShowExitModal ] = useState( false );
|
|
|
|
useEffect( () => {
|
|
|
|
onBackButtonClicked( () => setShowExitModal( true ) );
|
|
|
|
}, [] );
|
2023-12-06 12:49:28 +00:00
|
|
|
// @ts-expect-error temp fix
|
|
|
|
// Since we load the assember hub in an iframe, we don't have access to
|
|
|
|
// xstate's context values.
|
|
|
|
// This is the best workaround I can think of for now.
|
|
|
|
// Set the aiOnline value from the parent window so that any child components
|
|
|
|
// can access it.
|
|
|
|
props.context.aiOnline = window.parent?.window.cys_aiOnline;
|
2023-12-01 02:23:00 +00:00
|
|
|
|
2023-08-28 01:28:05 +00:00
|
|
|
return (
|
2023-12-01 02:23:00 +00:00
|
|
|
<>
|
|
|
|
{ showExitModal && (
|
|
|
|
<GoBackWarningModal
|
|
|
|
setOpenWarningModal={ setShowExitModal }
|
|
|
|
onExitClicked={ () => {
|
|
|
|
window.location.href = getNewPath( {}, '/', {} );
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
<CustomizeStoreContext.Provider value={ props }>
|
|
|
|
<ShortcutProvider style={ { height: '100%' } }>
|
|
|
|
<GlobalStylesProvider>
|
|
|
|
<RouterProvider>
|
|
|
|
<Layout />
|
|
|
|
</RouterProvider>
|
|
|
|
<PreloadFonts />
|
|
|
|
</GlobalStylesProvider>
|
|
|
|
</ShortcutProvider>
|
|
|
|
</CustomizeStoreContext.Provider>
|
|
|
|
</>
|
2023-08-28 01:28:05 +00:00
|
|
|
);
|
|
|
|
};
|