Repository navigation
Dashboard: add command palette commands #78429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
eae2fa9
Dashboard: add "customize" command palette command
simison 7eb79a0
Add more commands
simison 4687d6e
Update package-lock.json
simison c597f11
Update routes/dashboard/widget-dashboard/components/dashboard-command…
retrofox 7b29b7f
Update routes/dashboard/widget-dashboard/utils/grid-model-change/grid…
retrofox b0265e0
Merge branch 'trunk' into update/dashboard-commands
retrofox 2ff7cda
Merge remote-tracking branch 'origin/trunk' into update/dashboard-com…
retrofox 2fd4eb0
remove unused dashboard-commands utils file
retrofox b7491bf
remove layout settings command from palette
retrofox 3354ca0
add Actions to WidgetDashboard JSDoc example
retrofox e9f35e4
Merge branch 'trunk' into update/dashboard-commands
retrofox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
routes/dashboard/widget-dashboard/components/dashboard-commands/dashboard-commands.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
4 changes: 4 additions & 0 deletions
4
routes/dashboard/widget-dashboard/components/dashboard-commands/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export { | ||
| DashboardCommands, | ||
| DASHBOARD_COMMAND_CONTEXT, | ||
| } from './dashboard-commands'; |
48 changes: 48 additions & 0 deletions
48
...es/dashboard/widget-dashboard/components/dashboard-commands/use-pending-when-edit-mode.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 ] | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super mint one: every other callback in this file calls
close()first and runs the action after; this one inverts it. Same outcome, but worth unifying so the pattern is consistent across the six commands.