import type { ReactNode } from 'react'; import { privateApis as editorPrivateApis } from '@wordpress/editor'; import { store as coreDataStore } from '@wordpress/core-data'; import { useSelect } from '@wordpress/data'; import { Spinner as WCSpinner } from '@wordpress/components'; import { useMemo } from '@wordpress/element'; import { useStylesId } from '../../hooks/use-styles-id'; import { useEditorSettings } from '../../hooks/use-editor-settings'; import { useEditorAssets } from '../../hooks/use-editor-assets'; import { unlock } from '../../lock-unlock'; const { Editor: PrivateEditor, BackButton, PreferencesModal, ToolsMoreMenuGroup, SiteExport, UploadProgressSnackbar, } = unlock( editorPrivateApis ); interface EditorProps { postType?: string; postId?: string; settings?: Record< string, any >; backButton?: ReactNode; onActionPerformed?: ( actionId: string, items: any[] ) => void; initialViewport?: string; renderingMode?: string; } /** * Lazy-loading editor component that handles asset loading and settings initialization. * * @param {Object} props Component props * @param {string} props.postType Optional post type to edit. If not provided, resolves to homepage. * @param {string} props.postId Optional post ID to edit. If not provided, resolves to homepage. * @param {Object} props.settings Optional extra settings to merge with editor settings * @param {ReactNode} props.backButton Optional back button to render in editor header * @param {Function} props.onActionPerformed Optional callback run after a post action * @param {string} props.initialViewport Optional device type the entity opens at * @param {string} props.renderingMode Optional rendering mode the editor stays in, for a route that shows the site rather than one piece of content * @return The editor component with loading states */ export function Editor( { postType, postId, settings, backButton, onActionPerformed, initialViewport, renderingMode, }: EditorProps ) { // Resolve homepage when no postType/postId provided const homePage = useSelect( ( select ) => { // Only resolve homepage when both postType and postId are missing if ( postType || postId ) { return null; } const { getHomePage } = unlock( select( coreDataStore ) ); return getHomePage(); }, [ postType, postId ] ); // Use provided postType/postId, or fall back to homepage resolution const resolvedPostType = postType || homePage?.postType; const resolvedPostId = postId || homePage?.postId; // Resolve template ID from post type/ID const templateId = useSelect( ( select ) => { if ( ! resolvedPostType || ! resolvedPostId ) { return undefined; } if ( resolvedPostType === 'wp_template' ) { return resolvedPostId; } // Use private API to get template ID for this post return unlock( select( coreDataStore ) ).getTemplateId( resolvedPostType, resolvedPostId ); }, [ resolvedPostType, resolvedPostId ] ); // Resolve styles ID from template const stylesId = useStylesId( { templateId } ); // Load editor settings and assets const { isReady: settingsReady, editorSettings } = useEditorSettings( { stylesId, } ); const { isReady: assetsReady } = useEditorAssets(); const finalSettings = useMemo( () => ( { ...editorSettings, ...settings, /* * The theme's styles and the user's global styles, then whatever the * host adds for the surface it is rendering into. Spelled out after * the spread because `styles` is a list each source adds to: a host * contributing its own must not drop everyone else's. */ styles: [ ...( editorSettings.styles ?? [] ), ...( settings?.styles ?? [] ), ], } ), [ editorSettings, settings ] ); // Show loading spinner while assets or settings are loading if ( ! settingsReady || ! assetsReady ) { return (