|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +import { useCallback, useMemo } from '@wordpress/element'; |
| 5 | +import { __ } from '@wordpress/i18n'; |
| 6 | +import { |
| 7 | + useCommands, |
| 8 | + privateApis as commandsPrivateApis, |
| 9 | +} from '@wordpress/commands'; |
| 10 | +import { |
| 11 | + columns, |
| 12 | + grid, |
| 13 | + layout as layoutIcon, |
| 14 | + plus, |
| 15 | + trash, |
| 16 | +} from '@wordpress/icons'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Internal dependencies |
| 20 | + */ |
| 21 | +import { unlock } from '../../../lock-unlock'; |
| 22 | +import { useDashboardInternalContext } from '../../context/dashboard-context'; |
| 23 | +import { useDashboardUIContext } from '../../context/ui-context'; |
| 24 | +import { getGridModel } from '../../utils/grid-model-change'; |
| 25 | +import { usePendingWhenEditMode } from './use-pending-when-edit-mode'; |
| 26 | + |
| 27 | +const { useCommandContext } = unlock( commandsPrivateApis ); |
| 28 | + |
| 29 | +export const DASHBOARD_COMMAND_CONTEXT = 'dashboard'; |
| 30 | + |
| 31 | +type CommandCallback = ( options: { close: () => void } ) => void; |
| 32 | + |
| 33 | +/** |
| 34 | + * Registers dashboard-specific command palette commands and sets the |
| 35 | + * active command context so they surface under Suggestions by default. |
| 36 | + */ |
| 37 | +export function DashboardCommands() { |
| 38 | + const { |
| 39 | + editMode, |
| 40 | + onEditChange, |
| 41 | + onLayoutReset, |
| 42 | + gridSettings, |
| 43 | + canEditGridSettings, |
| 44 | + commitGridModelChange, |
| 45 | + } = useDashboardInternalContext(); |
| 46 | + |
| 47 | + const { setInserterOpen, setResetDialogOpen } = useDashboardUIContext(); |
| 48 | + |
| 49 | + useCommandContext( DASHBOARD_COMMAND_CONTEXT ); |
| 50 | + |
| 51 | + const runWhenInEditMode = usePendingWhenEditMode( { |
| 52 | + editMode, |
| 53 | + onEditChange, |
| 54 | + } ); |
| 55 | + |
| 56 | + const gridModel = getGridModel( gridSettings ); |
| 57 | + const isGridLayout = gridModel === 'grid'; |
| 58 | + const isMasonryLayout = gridModel === 'masonry'; |
| 59 | + |
| 60 | + const switchToMasonry = useCallback< CommandCallback >( |
| 61 | + ( { close } ) => { |
| 62 | + close(); |
| 63 | + commitGridModelChange( 'masonry' ); |
| 64 | + }, |
| 65 | + [ commitGridModelChange ] |
| 66 | + ); |
| 67 | + |
| 68 | + const switchToGrid = useCallback< CommandCallback >( |
| 69 | + ( { close } ) => { |
| 70 | + close(); |
| 71 | + commitGridModelChange( 'grid' ); |
| 72 | + }, |
| 73 | + [ commitGridModelChange ] |
| 74 | + ); |
| 75 | + |
| 76 | + const resetToDefault = useCallback< CommandCallback >( |
| 77 | + ( { close } ) => { |
| 78 | + close(); |
| 79 | + setResetDialogOpen( true ); |
| 80 | + }, |
| 81 | + [ setResetDialogOpen ] |
| 82 | + ); |
| 83 | + |
| 84 | + const addWidgets = useCallback< CommandCallback >( |
| 85 | + ( { close } ) => { |
| 86 | + close(); |
| 87 | + runWhenInEditMode( () => { |
| 88 | + setInserterOpen( true ); |
| 89 | + } ); |
| 90 | + }, |
| 91 | + [ runWhenInEditMode, setInserterOpen ] |
| 92 | + ); |
| 93 | + |
| 94 | + const customize = useCallback< CommandCallback >( |
| 95 | + ( { close } ) => { |
| 96 | + close(); |
| 97 | + onEditChange?.( true ); |
| 98 | + }, |
| 99 | + [ onEditChange ] |
| 100 | + ); |
| 101 | + |
| 102 | + const commands = useMemo( |
| 103 | + () => [ |
| 104 | + { |
| 105 | + name: 'core/dashboard/customize', |
| 106 | + label: __( 'Customize dashboard' ), |
| 107 | + icon: layoutIcon, |
| 108 | + category: 'command', |
| 109 | + context: DASHBOARD_COMMAND_CONTEXT, |
| 110 | + keywords: [ __( 'edit' ), __( 'widgets' ), __( 'layout' ) ], |
| 111 | + disabled: ! onEditChange || editMode, |
| 112 | + callback: customize, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: 'core/dashboard/add-widgets', |
| 116 | + label: __( 'Add dashboard widgets' ), |
| 117 | + icon: plus, |
| 118 | + category: 'command', |
| 119 | + context: DASHBOARD_COMMAND_CONTEXT, |
| 120 | + keywords: [ __( 'widgets' ), __( 'inserter' ) ], |
| 121 | + disabled: ! onEditChange, |
| 122 | + callback: addWidgets, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: 'core/dashboard/switch-to-masonry-layout', |
| 126 | + label: __( 'Switch dashboard to masonry layout' ), |
| 127 | + icon: columns, |
| 128 | + category: 'command', |
| 129 | + context: DASHBOARD_COMMAND_CONTEXT, |
| 130 | + keywords: [ |
| 131 | + __( 'layout' ), |
| 132 | + __( 'layout model' ), |
| 133 | + __( 'masonry' ), |
| 134 | + ], |
| 135 | + disabled: ! canEditGridSettings || isMasonryLayout || editMode, |
| 136 | + callback: switchToMasonry, |
| 137 | + }, |
| 138 | + { |
| 139 | + name: 'core/dashboard/switch-to-grid-layout', |
| 140 | + label: __( 'Switch dashboard to grid layout' ), |
| 141 | + icon: grid, |
| 142 | + category: 'command', |
| 143 | + context: DASHBOARD_COMMAND_CONTEXT, |
| 144 | + keywords: [ |
| 145 | + __( 'layout' ), |
| 146 | + __( 'layout model' ), |
| 147 | + __( 'grid' ), |
| 148 | + __( 'standard grid' ), |
| 149 | + ], |
| 150 | + disabled: ! canEditGridSettings || isGridLayout || editMode, |
| 151 | + callback: switchToGrid, |
| 152 | + }, |
| 153 | + { |
| 154 | + name: 'core/dashboard/reset-to-default', |
| 155 | + label: __( 'Reset dashboard widgets to default' ), |
| 156 | + icon: trash, |
| 157 | + category: 'command', |
| 158 | + context: DASHBOARD_COMMAND_CONTEXT, |
| 159 | + keywords: [ __( 'reset' ), __( 'default' ) ], |
| 160 | + disabled: ! onLayoutReset, |
| 161 | + callback: resetToDefault, |
| 162 | + }, |
| 163 | + ], |
| 164 | + [ |
| 165 | + onEditChange, |
| 166 | + editMode, |
| 167 | + customize, |
| 168 | + addWidgets, |
| 169 | + canEditGridSettings, |
| 170 | + isMasonryLayout, |
| 171 | + isGridLayout, |
| 172 | + switchToMasonry, |
| 173 | + switchToGrid, |
| 174 | + onLayoutReset, |
| 175 | + resetToDefault, |
| 176 | + ] |
| 177 | + ); |
| 178 | + |
| 179 | + useCommands( commands ); |
| 180 | + |
| 181 | + return null; |
| 182 | +} |
0 commit comments