diff --git a/package-lock.json b/package-lock.json index 5e03169140f398..df6643a9982ac7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -64349,6 +64349,7 @@ "@wordpress/admin-ui": "file:../../packages/admin-ui", "@wordpress/api-fetch": "file:../../packages/api-fetch", "@wordpress/base-styles": "file:../../packages/base-styles", + "@wordpress/commands": "file:../../packages/commands", "@wordpress/components": "file:../../packages/components", "@wordpress/compose": "file:../../packages/compose", "@wordpress/core-data": "file:../../packages/core-data", diff --git a/routes/dashboard/package.json b/routes/dashboard/package.json index 2b9c4132cfbcf1..22de7aaf85c6fe 100644 --- a/routes/dashboard/package.json +++ b/routes/dashboard/package.json @@ -13,6 +13,7 @@ "@wordpress/api-fetch": "file:../../packages/api-fetch", "@wordpress/base-styles": "file:../../packages/base-styles", "@wordpress/core-data": "file:../../packages/core-data", + "@wordpress/commands": "file:../../packages/commands", "@wordpress/components": "file:../../packages/components", "@wordpress/compose": "file:../../packages/compose", "@wordpress/data": "file:../../packages/data", diff --git a/routes/dashboard/widget-dashboard/components/actions/actions.tsx b/routes/dashboard/widget-dashboard/components/actions/actions.tsx index 816394df6489a0..db65f6ca5d2d08 100644 --- a/routes/dashboard/widget-dashboard/components/actions/actions.tsx +++ b/routes/dashboard/widget-dashboard/components/actions/actions.tsx @@ -72,10 +72,13 @@ export function Actions(): React.ReactNode { return () => clearTimeout( exitTimeout ); }, [ editMode, isEditActionsMounted ] ); - const { setInserterOpen } = useDashboardUIContext(); - - const [ isResetDialogOpen, setIsResetDialogOpen ] = useState( false ); - const [ isLayoutSettingsOpen, setIsLayoutSettingsOpen ] = useState( false ); + const { + setInserterOpen, + layoutSettingsOpen, + setLayoutSettingsOpen, + resetDialogOpen, + setResetDialogOpen, + } = useDashboardUIContext(); const handleEditMode = useCallback( () => { onEditChange?.( ! editMode ); @@ -94,13 +97,13 @@ export function Actions(): React.ReactNode { }, [ commit ] ); const openLayoutSettings = useCallback( () => { - setIsLayoutSettingsOpen( true ); - }, [] ); + setLayoutSettingsOpen( true ); + }, [ setLayoutSettingsOpen ] ); const moreActionsItems: MoreActionsDropdownItem[] = [ { label: __( 'Reset to default' ), - onClick: () => setIsResetDialogOpen( true ), + onClick: () => setResetDialogOpen( true ), disabled: ! onLayoutReset, }, ]; @@ -178,12 +181,12 @@ export function Actions(): React.ReactNode { { await onLayoutReset?.(); onEditChange?.( false ); - setIsResetDialogOpen( false ); + setResetDialogOpen( false ); } } > ) } diff --git a/routes/dashboard/widget-dashboard/components/dashboard-commands/dashboard-commands.tsx b/routes/dashboard/widget-dashboard/components/dashboard-commands/dashboard-commands.tsx new file mode 100644 index 00000000000000..9ee5f0d5c40809 --- /dev/null +++ b/routes/dashboard/widget-dashboard/components/dashboard-commands/dashboard-commands.tsx @@ -0,0 +1,182 @@ +/** + * WordPress dependencies + */ +import { useCallback, useMemo } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; +import { + useCommands, + privateApis as commandsPrivateApis, +} from '@wordpress/commands'; +import { + columns, + grid, + layout as layoutIcon, + plus, + trash, +} from '@wordpress/icons'; + +/** + * Internal dependencies + */ +import { unlock } from '../../../lock-unlock'; +import { useDashboardInternalContext } from '../../context/dashboard-context'; +import { useDashboardUIContext } from '../../context/ui-context'; +import { getGridModel } from '../../utils/grid-model-change'; +import { usePendingWhenEditMode } from './use-pending-when-edit-mode'; + +const { useCommandContext } = unlock( commandsPrivateApis ); + +export const DASHBOARD_COMMAND_CONTEXT = 'dashboard'; + +type CommandCallback = ( options: { close: () => void } ) => void; + +/** + * Registers dashboard-specific command palette commands and sets the + * active command context so they surface under Suggestions by default. + */ +export function DashboardCommands() { + const { + editMode, + onEditChange, + onLayoutReset, + gridSettings, + canEditGridSettings, + commitGridModelChange, + } = useDashboardInternalContext(); + + const { setInserterOpen, setResetDialogOpen } = useDashboardUIContext(); + + useCommandContext( DASHBOARD_COMMAND_CONTEXT ); + + const runWhenInEditMode = usePendingWhenEditMode( { + editMode, + onEditChange, + } ); + + const gridModel = getGridModel( gridSettings ); + const isGridLayout = gridModel === 'grid'; + const isMasonryLayout = gridModel === 'masonry'; + + const switchToMasonry = useCallback< CommandCallback >( + ( { close } ) => { + close(); + commitGridModelChange( 'masonry' ); + }, + [ commitGridModelChange ] + ); + + const switchToGrid = useCallback< CommandCallback >( + ( { close } ) => { + close(); + commitGridModelChange( 'grid' ); + }, + [ commitGridModelChange ] + ); + + const resetToDefault = useCallback< CommandCallback >( + ( { close } ) => { + close(); + setResetDialogOpen( true ); + }, + [ setResetDialogOpen ] + ); + + const addWidgets = useCallback< CommandCallback >( + ( { close } ) => { + close(); + runWhenInEditMode( () => { + setInserterOpen( true ); + } ); + }, + [ runWhenInEditMode, setInserterOpen ] + ); + + const customize = useCallback< CommandCallback >( + ( { close } ) => { + close(); + onEditChange?.( true ); + }, + [ onEditChange ] + ); + + const commands = useMemo( + () => [ + { + name: 'core/dashboard/customize', + label: __( 'Customize dashboard' ), + icon: layoutIcon, + category: 'command', + context: DASHBOARD_COMMAND_CONTEXT, + keywords: [ __( 'edit' ), __( 'widgets' ), __( 'layout' ) ], + disabled: ! onEditChange || editMode, + callback: customize, + }, + { + name: 'core/dashboard/add-widgets', + label: __( 'Add dashboard widgets' ), + icon: plus, + category: 'command', + context: DASHBOARD_COMMAND_CONTEXT, + keywords: [ __( 'widgets' ), __( 'inserter' ) ], + disabled: ! onEditChange, + callback: addWidgets, + }, + { + name: 'core/dashboard/switch-to-masonry-layout', + label: __( 'Switch dashboard to masonry layout' ), + icon: columns, + category: 'command', + context: DASHBOARD_COMMAND_CONTEXT, + keywords: [ + __( 'layout' ), + __( 'layout model' ), + __( 'masonry' ), + ], + disabled: ! canEditGridSettings || isMasonryLayout || editMode, + callback: switchToMasonry, + }, + { + name: 'core/dashboard/switch-to-grid-layout', + label: __( 'Switch dashboard to grid layout' ), + icon: grid, + category: 'command', + context: DASHBOARD_COMMAND_CONTEXT, + keywords: [ + __( 'layout' ), + __( 'layout model' ), + __( 'grid' ), + __( 'standard grid' ), + ], + disabled: ! canEditGridSettings || isGridLayout || editMode, + callback: switchToGrid, + }, + { + name: 'core/dashboard/reset-to-default', + label: __( 'Reset dashboard widgets to default' ), + icon: trash, + category: 'command', + context: DASHBOARD_COMMAND_CONTEXT, + keywords: [ __( 'reset' ), __( 'default' ) ], + disabled: ! onLayoutReset, + callback: resetToDefault, + }, + ], + [ + onEditChange, + editMode, + customize, + addWidgets, + canEditGridSettings, + isMasonryLayout, + isGridLayout, + switchToMasonry, + switchToGrid, + onLayoutReset, + resetToDefault, + ] + ); + + useCommands( commands ); + + return null; +} diff --git a/routes/dashboard/widget-dashboard/components/dashboard-commands/index.ts b/routes/dashboard/widget-dashboard/components/dashboard-commands/index.ts new file mode 100644 index 00000000000000..b2cd081992acb5 --- /dev/null +++ b/routes/dashboard/widget-dashboard/components/dashboard-commands/index.ts @@ -0,0 +1,4 @@ +export { + DashboardCommands, + DASHBOARD_COMMAND_CONTEXT, +} from './dashboard-commands'; diff --git a/routes/dashboard/widget-dashboard/components/dashboard-commands/use-pending-when-edit-mode.ts b/routes/dashboard/widget-dashboard/components/dashboard-commands/use-pending-when-edit-mode.ts new file mode 100644 index 00000000000000..58e9aa0d73dfcd --- /dev/null +++ b/routes/dashboard/widget-dashboard/components/dashboard-commands/use-pending-when-edit-mode.ts @@ -0,0 +1,48 @@ +/** + * WordPress dependencies + */ +import { useCallback, useEffect, useRef } from '@wordpress/element'; + +type usePendingWhenEditModeProps = { + editMode: boolean; + onEditChange?: ( next: boolean ) => void; +}; + +type usePendingWhenEditModeResult = ( action: () => void ) => void; + +/** + * Runs an action immediately when already in edit mode. Otherwise enables + * edit mode and runs the action after edit mode becomes active. + * + * @param {usePendingWhenEditModeProps} params - The parameters for the function. + * @return {usePendingWhenEditModeResult} The function result. + */ +export function usePendingWhenEditMode( { + editMode, + onEditChange, +}: usePendingWhenEditModeProps ): usePendingWhenEditModeResult { + const pendingRef = useRef< ( () => void ) | null >( null ); + const wasEditingRef = useRef( editMode ); + + useEffect( () => { + if ( ! wasEditingRef.current && editMode && pendingRef.current ) { + const action = pendingRef.current; + pendingRef.current = null; + action(); + } + wasEditingRef.current = editMode; + }, [ editMode ] ); + + return useCallback( + ( action: () => void ) => { + if ( editMode ) { + action(); + return; + } + + pendingRef.current = action; + onEditChange?.( true ); + }, + [ editMode, onEditChange ] + ); +} diff --git a/routes/dashboard/widget-dashboard/context/dashboard-context.tsx b/routes/dashboard/widget-dashboard/context/dashboard-context.tsx index 5adc425dad269f..80e2066ce9cb21 100644 --- a/routes/dashboard/widget-dashboard/context/dashboard-context.tsx +++ b/routes/dashboard/widget-dashboard/context/dashboard-context.tsx @@ -19,8 +19,10 @@ import { /** * Internal dependencies */ +import { computeGridModelChange } from '../utils/grid-model-change'; import type { ResolveWidgetModule, + WidgetGridModel, WidgetGridSettings, DashboardWidget, WidgetType, @@ -123,6 +125,13 @@ interface InternalDashboardContextValue { */ commit: ( options?: { exitEditMode?: boolean } ) => void; + /** + * Switches the layout model, updates staging, and publishes + * immediately — equivalent to changing the model in layout + * settings and clicking Save. + */ + commitGridModelChange: ( targetModel: WidgetGridModel ) => void; + /** * Reverts both staging slices. By default also exits edit mode; pass * `{ exitEditMode: false }` when dismissing the layout settings drawer. @@ -309,6 +318,33 @@ export function WidgetDashboardProvider( { [ committedLayout, committedGridSettings, onEditChange ] ); + const commitGridModelChange = useCallback( + ( targetModel: WidgetGridModel ) => { + const next = computeGridModelChange( { + layout: stagingLayout, + gridSettings: stagingGridSettings, + targetModel, + } ); + + if ( ! next ) { + return; + } + + setStagingLayout( next.layout ); + setStagingGridSettings( next.gridSettings ); + onLayoutChange( canonicalize( next.layout ) ); + onGridSettingsChange?.( next.gridSettings ); + onEditChange?.( false ); + }, + [ + stagingLayout, + stagingGridSettings, + onLayoutChange, + onGridSettingsChange, + onEditChange, + ] + ); + const resetGridSettings = useCallback( () => { setStagingGridSettings( DEFAULT_GRID ); }, [] ); @@ -337,6 +373,7 @@ export function WidgetDashboardProvider( { canEditGridSettings, resetGridSettings, commit, + commitGridModelChange, cancel, hasUncommittedChanges, editMode, @@ -351,6 +388,7 @@ export function WidgetDashboardProvider( { canEditGridSettings, resetGridSettings, commit, + commitGridModelChange, cancel, hasUncommittedChanges, editMode, diff --git a/routes/dashboard/widget-dashboard/context/ui-context.tsx b/routes/dashboard/widget-dashboard/context/ui-context.tsx index 04962a9036bf9f..bf764882d6989b 100644 --- a/routes/dashboard/widget-dashboard/context/ui-context.tsx +++ b/routes/dashboard/widget-dashboard/context/ui-context.tsx @@ -16,6 +16,10 @@ import { interface DashboardUIContextValue { inserterOpen: boolean; setInserterOpen: ( next: boolean ) => void; + layoutSettingsOpen: boolean; + setLayoutSettingsOpen: ( next: boolean ) => void; + resetDialogOpen: boolean; + setResetDialogOpen: ( next: boolean ) => void; /** * `uuid` of the instance whose settings drawer is open, or `null` @@ -76,6 +80,8 @@ interface ProviderProps { */ export function WidgetDashboardUIProvider( { children }: ProviderProps ) { const [ inserterOpen, setInserterOpen ] = useState( false ); + const [ layoutSettingsOpen, setLayoutSettingsOpen ] = useState( false ); + const [ resetDialogOpen, setResetDialogOpen ] = useState( false ); const [ settingsWidgetUuid, setSettingsWidgetUuid ] = useState< string | null >( null ); @@ -88,6 +94,10 @@ export function WidgetDashboardUIProvider( { children }: ProviderProps ) { () => ( { inserterOpen, setInserterOpen, + layoutSettingsOpen, + setLayoutSettingsOpen, + resetDialogOpen, + setResetDialogOpen, settingsWidgetUuid, setSettingsWidgetUuid, settingsDrawerSide, @@ -97,6 +107,8 @@ export function WidgetDashboardUIProvider( { children }: ProviderProps ) { } ), [ inserterOpen, + layoutSettingsOpen, + resetDialogOpen, settingsWidgetUuid, settingsDrawerSide, settingsDrawerInset, diff --git a/routes/dashboard/widget-dashboard/test/dashboard-commands.test.tsx b/routes/dashboard/widget-dashboard/test/dashboard-commands.test.tsx new file mode 100644 index 00000000000000..4c347d7b0ae4b4 --- /dev/null +++ b/routes/dashboard/widget-dashboard/test/dashboard-commands.test.tsx @@ -0,0 +1,176 @@ +/** + * External dependencies + */ +import '@testing-library/jest-dom'; +import { render, screen } from '@testing-library/react'; + +/** + * WordPress dependencies + */ +import { useMemo, useState } from '@wordpress/element'; +import { useSelect } from '@wordpress/data'; +import { store as commandsStore } from '@wordpress/commands'; + +/** + * Internal dependencies + */ +import { WidgetDashboard } from '../widget-dashboard'; +import { DASHBOARD_COMMAND_CONTEXT } from '../components/dashboard-commands'; +import type { DashboardWidget, WidgetType } from '../types'; + +const widgetTypes: WidgetType[] = []; + +const layout: DashboardWidget[] = [ + { uuid: 'a', type: 'core/test', placement: { width: 1, height: 1 } }, +]; + +function CommandsProbe( { names }: { names: string[] } ) { + const context = useSelect( + ( select ) => select( commandsStore ).getContext(), + [] + ); + const contextualCommands = useSelect( + ( select ) => select( commandsStore ).getCommands( true ), + [] + ); + + const registered = useMemo( + () => + Object.fromEntries( + names.map( ( name ) => [ + name, + contextualCommands.some( + ( command ) => command.name === name + ), + ] ) + ), + [ names, contextualCommands ] + ); + + return ( +
+ ); +} + +const COMMAND_NAMES = [ + 'core/dashboard/customize', + 'core/dashboard/add-widgets', + 'core/dashboard/switch-to-masonry-layout', + 'core/dashboard/switch-to-grid-layout', + 'core/dashboard/reset-to-default', +]; + +interface HarnessProps { + initialEditMode?: boolean; + withGridSettings?: boolean; + withLayoutReset?: boolean; + gridModel?: 'grid' | 'masonry'; +} + +function Harness( { + initialEditMode = false, + withGridSettings = false, + withLayoutReset = false, + gridModel = 'grid', +}: HarnessProps ) { + const [ editMode, setEditMode ] = useState( initialEditMode ); + + return ( + {} } + widgetTypes={ widgetTypes } + editMode={ editMode } + onEditChange={ setEditMode } + onLayoutReset={ withLayoutReset ? async () => {} : undefined } + gridSettings={ + withGridSettings + ? { model: gridModel, columns: 6, minColumnWidth: 350 } + : undefined + } + onGridSettingsChange={ withGridSettings ? () => {} : undefined } + > + + + ); +} + +function getRegistered( probe: HTMLElement ): Record< string, boolean > { + return JSON.parse( probe.getAttribute( 'data-registered' ) ?? '{}' ); +} + +describe( 'WidgetDashboard.DashboardCommands', () => { + it( 'sets the dashboard command context and registers core commands', () => { + render( ); + const probe = screen.getByTestId( 'commands-probe' ); + const registered = getRegistered( probe ); + + expect( probe ).toHaveAttribute( + 'data-context', + DASHBOARD_COMMAND_CONTEXT + ); + expect( registered[ 'core/dashboard/customize' ] ).toBe( true ); + expect( registered[ 'core/dashboard/add-widgets' ] ).toBe( true ); + expect( registered[ 'core/dashboard/reset-to-default' ] ).toBe( true ); + } ); + + it( 'unregisters Customize while edit mode is active', () => { + render( ); + const registered = getRegistered( + screen.getByTestId( 'commands-probe' ) + ); + + expect( registered[ 'core/dashboard/customize' ] ).toBe( false ); + expect( registered[ 'core/dashboard/add-widgets' ] ).toBe( true ); + } ); + + it( 'disables layout commands while edit mode is active', () => { + render( + + ); + const registered = getRegistered( + screen.getByTestId( 'commands-probe' ) + ); + + expect( registered[ 'core/dashboard/switch-to-masonry-layout' ] ).toBe( + false + ); + expect( registered[ 'core/dashboard/switch-to-grid-layout' ] ).toBe( + false + ); + } ); + + it( 'only registers the active layout-model switch command', () => { + render( ); + const registered = getRegistered( + screen.getByTestId( 'commands-probe' ) + ); + + expect( registered[ 'core/dashboard/switch-to-masonry-layout' ] ).toBe( + true + ); + expect( registered[ 'core/dashboard/switch-to-grid-layout' ] ).toBe( + false + ); + } ); + + it( 'omits grid-settings commands when grid settings are not editable', () => { + render( ); + const registered = getRegistered( + screen.getByTestId( 'commands-probe' ) + ); + + expect( registered[ 'core/dashboard/switch-to-masonry-layout' ] ).toBe( + false + ); + } ); +} ); diff --git a/routes/dashboard/widget-dashboard/utils/grid-model-change/grid-model-change.ts b/routes/dashboard/widget-dashboard/utils/grid-model-change/grid-model-change.ts new file mode 100644 index 00000000000000..3ba7a59512083f --- /dev/null +++ b/routes/dashboard/widget-dashboard/utils/grid-model-change/grid-model-change.ts @@ -0,0 +1,53 @@ +/** + * Internal dependencies + */ +import { migrateLayout } from '../migrate-layout'; +import type { + DashboardWidget, + WidgetGridModel, + WidgetGridSettings, +} from '../../types'; + +const DEFAULT_FIXED_COLUMNS = 6; + +export function getGridModel( settings: WidgetGridSettings ): WidgetGridModel { + return settings.model ?? 'grid'; +} + +type computeGridModelChangeProps = { + layout: DashboardWidget[]; + gridSettings: WidgetGridSettings; + targetModel: WidgetGridModel; +}; + +type computeGridModelChangeResult = { + layout: DashboardWidget[]; + gridSettings: WidgetGridSettings; +} | null; + +/** + * Computes the staged layout and grid settings after a layout-model change. + * @param {computeGridModelChangeProps} params - The parameters for the function. + * @return {computeGridModelChangeResult} The function result. + */ +export function computeGridModelChange( { + layout, + gridSettings, + targetModel, +}: computeGridModelChangeProps ): computeGridModelChangeResult { + const currentModel = getGridModel( gridSettings ); + + if ( currentModel === targetModel ) { + return null; + } + + return { + layout: migrateLayout( layout, currentModel, targetModel, { + columns: gridSettings.columns ?? DEFAULT_FIXED_COLUMNS, + } ), + gridSettings: { + ...gridSettings, + model: targetModel, + } as WidgetGridSettings, + }; +} diff --git a/routes/dashboard/widget-dashboard/utils/grid-model-change/index.ts b/routes/dashboard/widget-dashboard/utils/grid-model-change/index.ts new file mode 100644 index 00000000000000..efb90fab583b64 --- /dev/null +++ b/routes/dashboard/widget-dashboard/utils/grid-model-change/index.ts @@ -0,0 +1 @@ +export { computeGridModelChange, getGridModel } from './grid-model-change'; diff --git a/routes/dashboard/widget-dashboard/widget-dashboard.tsx b/routes/dashboard/widget-dashboard/widget-dashboard.tsx index cfd9913ee06396..12a8b71fab4bb3 100644 --- a/routes/dashboard/widget-dashboard/widget-dashboard.tsx +++ b/routes/dashboard/widget-dashboard/widget-dashboard.tsx @@ -4,6 +4,7 @@ import { WidgetDashboardProvider } from './context/dashboard-context'; import { WidgetDashboardUIProvider } from './context/ui-context'; import { Actions } from './components/actions'; +import { DashboardCommands } from './components/dashboard-commands'; import { Inserter } from './components/inserter'; import { WidgetChrome } from './components/widget-chrome'; import { WidgetSettings } from './components/widget-settings'; @@ -23,20 +24,27 @@ import { NoWidgetsState } from './components/no-widgets-state'; * * function MyDashboard() { * const [ layout, setLayout ] = useState( defaultLayout ); + * const [ editMode, setEditMode ] = useState( false ); * return ( * * *

No widgets yet.

*
+ * * *
* ); * } * ``` + * + * `Actions` hosts the reset dialog, so include it (or rely on the default + * children) for the command-palette "Reset to default" action to work. */ export const WidgetDashboard = Object.assign( function WidgetDashboard( { @@ -72,6 +80,7 @@ export const WidgetDashboard = Object.assign( ) } +