@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
131 lines (129 loc) • 4.76 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/widget-dashboard/src/components/commands/commands.tsx
var commands_exports = {};
__export(commands_exports, {
Commands: () => Commands,
DASHBOARD_COMMAND_CONTEXT: () => DASHBOARD_COMMAND_CONTEXT
});
module.exports = __toCommonJS(commands_exports);
var import_element = require("@wordpress/element");
var import_i18n = require("@wordpress/i18n");
var import_commands = require("@wordpress/commands");
var import_icons = require("@wordpress/icons");
var import_lock_unlock = require("../../lock-unlock.cjs");
var import_dashboard_context = require("../../context/dashboard-context.cjs");
var import_ui_context = require("../../context/ui-context.cjs");
var import_use_pending_when_edit_mode = require("./use-pending-when-edit-mode.cjs");
var { useCommandContext } = (0, import_lock_unlock.unlock)(import_commands.privateApis);
var DASHBOARD_COMMAND_CONTEXT = "dashboard";
function Commands() {
const { editMode, onEditChange, onLayoutReset, canPerform, widgetTypes } = (0, import_dashboard_context.useDashboardInternalContext)();
const canCustomize = canPerform({ operation: "customize" });
const canReset = canPerform({ operation: "reset" });
const canInsertAny = (0, import_element.useMemo)(
() => widgetTypes.some(
(widgetType) => canPerform({ operation: "insert", widgetType })
),
[widgetTypes, canPerform]
);
const { setInserterOpen, setResetDialogOpen } = (0, import_ui_context.useDashboardUIContext)();
useCommandContext(DASHBOARD_COMMAND_CONTEXT);
const runWhenInEditMode = (0, import_use_pending_when_edit_mode.usePendingWhenEditMode)({
editMode,
onEditChange
});
const resetToDefault = (0, import_element.useCallback)(
({ close }) => {
close();
setResetDialogOpen(true);
},
[setResetDialogOpen]
);
const addWidgets = (0, import_element.useCallback)(
({ close }) => {
close();
runWhenInEditMode(() => {
setInserterOpen(true);
});
},
[runWhenInEditMode, setInserterOpen]
);
const customize = (0, import_element.useCallback)(
({ close }) => {
close();
onEditChange?.(true);
},
[onEditChange]
);
const commands = (0, import_element.useMemo)(
() => [
{
name: "core/dashboard/customize",
label: (0, import_i18n.__)("Customize dashboard"),
icon: import_icons.layout,
category: "command",
context: DASHBOARD_COMMAND_CONTEXT,
keywords: [(0, import_i18n.__)("edit"), (0, import_i18n.__)("widgets"), (0, import_i18n.__)("layout")],
disabled: !onEditChange || editMode || !canCustomize,
callback: customize
},
{
name: "core/dashboard/add-widgets",
label: (0, import_i18n.__)("Add dashboard widgets"),
icon: import_icons.plus,
category: "command",
context: DASHBOARD_COMMAND_CONTEXT,
keywords: [(0, import_i18n.__)("widgets"), (0, import_i18n.__)("inserter")],
// Outside edit mode the command enters it first.
disabled: !onEditChange || !canInsertAny || !editMode && !canCustomize,
callback: addWidgets
},
{
name: "core/dashboard/reset-to-default",
label: (0, import_i18n.__)("Reset dashboard widgets to default"),
icon: import_icons.trash,
category: "command",
context: DASHBOARD_COMMAND_CONTEXT,
keywords: [(0, import_i18n.__)("reset"), (0, import_i18n.__)("default")],
disabled: !onLayoutReset || !canReset,
callback: resetToDefault
}
],
[
onEditChange,
editMode,
canCustomize,
canReset,
canInsertAny,
customize,
addWidgets,
onLayoutReset,
resetToDefault
]
);
(0, import_commands.useCommands)(commands);
return null;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Commands,
DASHBOARD_COMMAND_CONTEXT
});
//# sourceMappingURL=commands.cjs.map