UNPKG

@wordpress/widget-dashboard

Version:

Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.

108 lines (107 loc) • 3.28 kB
// packages/widget-dashboard/src/components/commands/commands.tsx import { useCallback, useMemo } from "@wordpress/element"; import { __ } from "@wordpress/i18n"; import { useCommands, privateApis as commandsPrivateApis } from "@wordpress/commands"; import { layout as layoutIcon, plus, trash } from "@wordpress/icons"; import { unlock } from "../../lock-unlock.mjs"; import { useDashboardInternalContext } from "../../context/dashboard-context.mjs"; import { useDashboardUIContext } from "../../context/ui-context.mjs"; import { usePendingWhenEditMode } from "./use-pending-when-edit-mode.mjs"; var { useCommandContext } = unlock(commandsPrivateApis); var DASHBOARD_COMMAND_CONTEXT = "dashboard"; function Commands() { const { editMode, onEditChange, onLayoutReset, canPerform, widgetTypes } = useDashboardInternalContext(); const canCustomize = canPerform({ operation: "customize" }); const canReset = canPerform({ operation: "reset" }); const canInsertAny = useMemo( () => widgetTypes.some( (widgetType) => canPerform({ operation: "insert", widgetType }) ), [widgetTypes, canPerform] ); const { setInserterOpen, setResetDialogOpen } = useDashboardUIContext(); useCommandContext(DASHBOARD_COMMAND_CONTEXT); const runWhenInEditMode = usePendingWhenEditMode({ editMode, onEditChange }); const resetToDefault = useCallback( ({ close }) => { close(); setResetDialogOpen(true); }, [setResetDialogOpen] ); const addWidgets = useCallback( ({ close }) => { close(); runWhenInEditMode(() => { setInserterOpen(true); }); }, [runWhenInEditMode, setInserterOpen] ); const customize = useCallback( ({ 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 || !canCustomize, callback: customize }, { name: "core/dashboard/add-widgets", label: __("Add dashboard widgets"), icon: plus, category: "command", context: DASHBOARD_COMMAND_CONTEXT, keywords: [__("widgets"), __("inserter")], // Outside edit mode the command enters it first. disabled: !onEditChange || !canInsertAny || !editMode && !canCustomize, callback: addWidgets }, { 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 || !canReset, callback: resetToDefault } ], [ onEditChange, editMode, canCustomize, canReset, canInsertAny, customize, addWidgets, onLayoutReset, resetToDefault ] ); useCommands(commands); return null; } export { Commands, DASHBOARD_COMMAND_CONTEXT }; //# sourceMappingURL=commands.mjs.map