UNPKG

@wordpress/widget-dashboard

Version:

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

198 lines (196 loc) • 7.27 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/widget-dashboard/src/context/dashboard-context.tsx var dashboard_context_exports = {}; __export(dashboard_context_exports, { WidgetDashboardProvider: () => WidgetDashboardProvider, useDashboardInternalContext: () => useDashboardInternalContext }); module.exports = __toCommonJS(dashboard_context_exports); var import_es6 = __toESM(require("fast-deep-equal/es6/index.js")); var import_compose = require("@wordpress/compose"); var import_element = require("@wordpress/element"); var import_canonicalize_layout = require("../utils/canonicalize-layout.cjs"); var import_default_grid = require("../utils/default-grid/index.cjs"); var import_enforce_layout_policy = require("../utils/enforce-layout-policy.cjs"); var import_normalize_grid_settings = require("../utils/normalize-grid-settings/index.cjs"); var import_resolve_dashboard_column_count = require("../utils/resolve-dashboard-column-count/resolve-dashboard-column-count.cjs"); var import_row_height_presets = require("../utils/row-height-presets/index.cjs"); var import_dashboard_policy = require("../components/dashboard-policy/index.cjs"); var import_jsx_runtime = require("react/jsx-runtime"); function resolveGridSettings(settings) { const normalized = (0, import_normalize_grid_settings.normalizeGridSettings)(settings, import_row_height_presets.DEFAULT_ROW_HEIGHT); return { ...normalized, columns: (0, import_resolve_dashboard_column_count.resolveDashboardColumnCap)(normalized.columns) }; } var DEFAULT_RESOLVE_WIDGET_MODULE = (moduleId) => import( /* webpackIgnore: true */ /* @vite-ignore */ moduleId ); var AUTO_SAVE_DELAY_MS = 5e3; var ALLOW_EVERY_OPERATION = () => true; var Context = (0, import_element.createContext)(null); function useDashboardInternalContext() { const ctx = (0, import_element.useContext)(Context); if (!ctx) { throw new Error( "Dashboard compound used outside a WidgetDashboard subtree." ); } return ctx; } function WidgetDashboardProvider({ widgetTypes, isResolvingWidgetTypes = false, layout: committedLayout, onLayoutChange, onLayoutReset, editMode = false, onEditChange, resolveWidgetModule = DEFAULT_RESOLVE_WIDGET_MODULE, gridSettings: committedGridSettings = import_default_grid.DEFAULT_GRID, children }) { const [stagingLayout, setStagingLayout] = (0, import_element.useState)(committedLayout); const policy = (0, import_dashboard_policy.useDashboardPolicy)(); const canPerform = (0, import_element.useMemo)( () => policy ? (request) => !!policy(request) : ALLOW_EVERY_OPERATION, [policy] ); const stageLayout = (0, import_element.useCallback)( (next) => { setStagingLayout( (previous) => canPerform === ALLOW_EVERY_OPERATION ? next : (0, import_enforce_layout_policy.enforceLayoutPolicy)({ previous, next, canPerform, widgetTypes }) ); }, [canPerform, widgetTypes] ); (0, import_element.useEffect)(() => { setStagingLayout(committedLayout); }, [committedLayout]); const gridSettings = (0, import_element.useMemo)( () => resolveGridSettings(committedGridSettings), [committedGridSettings] ); const hasLayoutChanges = (0, import_element.useMemo)( () => !(0, import_es6.default)( (0, import_canonicalize_layout.canonicalizeLayout)(committedLayout), (0, import_canonicalize_layout.canonicalizeLayout)(stagingLayout) ), [committedLayout, stagingLayout] ); const hasUncommittedChanges = hasLayoutChanges; const commit = (0, import_element.useCallback)( (options) => { if (hasLayoutChanges) { onLayoutChange((0, import_canonicalize_layout.canonicalizeLayout)(stagingLayout)); } if (options?.exitEditMode !== false) { onEditChange?.(false); } }, [hasLayoutChanges, onLayoutChange, stagingLayout, onEditChange] ); const publishAutoSave = (0, import_compose.useEvent)(() => commit({ exitEditMode: false })); const scheduleAutoSave = (0, import_element.useMemo)( () => (0, import_compose.debounce)(publishAutoSave, AUTO_SAVE_DELAY_MS), [publishAutoSave] ); const flushAutoSave = (0, import_element.useCallback)( () => scheduleAutoSave.flush(), [scheduleAutoSave] ); (0, import_element.useEffect)(() => { if (!editMode) { return; } scheduleAutoSave.flush(); }, [editMode, scheduleAutoSave]); (0, import_element.useEffect)(() => () => scheduleAutoSave.flush(), [scheduleAutoSave]); const cancel = (0, import_element.useCallback)(() => { setStagingLayout(committedLayout); onEditChange?.(false); }, [committedLayout, onEditChange]); (0, import_element.useEffect)(() => { if (stagingLayout.length === 0 && canPerform({ operation: "customize" })) { onEditChange?.(true); } }, [stagingLayout.length === 0]); const value = (0, import_element.useMemo)( () => ({ widgetTypes, isResolvingWidgetTypes, layout: stagingLayout, onLayoutChange: stageLayout, onLayoutReset, gridSettings, commit, cancel, scheduleAutoSave, flushAutoSave, hasUncommittedChanges, editMode, onEditChange, resolveWidgetModule, canPerform }), [ widgetTypes, isResolvingWidgetTypes, stagingLayout, stageLayout, onLayoutReset, gridSettings, commit, cancel, scheduleAutoSave, flushAutoSave, hasUncommittedChanges, editMode, onEditChange, resolveWidgetModule, canPerform ] ); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Context.Provider, { value, children }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { WidgetDashboardProvider, useDashboardInternalContext }); //# sourceMappingURL=dashboard-context.cjs.map