UNPKG

@wordpress/widget-dashboard

Version:

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

176 lines (175 loc) • 8.29 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); // \0rolldown/runtime.js 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 __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); let fast_deep_equal_es6_index_js = require("fast-deep-equal/es6/index.js"); fast_deep_equal_es6_index_js = __toESM(fast_deep_equal_es6_index_js); let _wordpress_compose = require("@wordpress/compose"); let _wordpress_element = require("@wordpress/element"); let ___utils_canonicalize_layout_ts = require("../utils/canonicalize-layout.cjs"); let ___utils_default_grid_index_ts = require("../utils/default-grid/index.cjs"); let ___utils_enforce_layout_policy_ts = require("../utils/enforce-layout-policy.cjs"); let ___utils_normalize_grid_settings_index_ts = require("../utils/normalize-grid-settings/index.cjs"); let ___utils_resolve_dashboard_column_count_resolve_dashboard_column_count_ts = require("../utils/resolve-dashboard-column-count/resolve-dashboard-column-count.cjs"); let ___utils_row_height_presets_index_ts = require("../utils/row-height-presets/index.cjs"); let ___utils_pending_actions_map_ts = require("../utils/pending-actions-map.cjs"); let ___utils_runtime_actions_map_ts = require("../utils/runtime-actions-map.cjs"); let ___components_dashboard_policy_index_ts = require("../components/dashboard-policy/index.cjs"); let ___types_ts = require("../types.cjs"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/widget-dashboard/src/context/dashboard-context.tsx function resolveGridSettings(settings) { const normalized = (0, ___utils_normalize_grid_settings_index_ts.normalizeGridSettings)(settings, ___utils_row_height_presets_index_ts.DEFAULT_ROW_HEIGHT); const minColumnWidth = (0, ___utils_resolve_dashboard_column_count_resolve_dashboard_column_count_ts.resolveDashboardMinColumnWidth)(normalized.minColumnWidth); const columns = (0, ___utils_resolve_dashboard_column_count_resolve_dashboard_column_count_ts.resolveDashboardColumns)(normalized.columns, minColumnWidth); return { ...normalized, columns: columns === void 0 && minColumnWidth === void 0 ? ___types_ts.WIDGET_DASHBOARD_COLUMN_STEPS : columns, minColumnWidth }; } const DEFAULT_RESOLVE_WIDGET_MODULE = (moduleId) => import( /* webpackIgnore: true */ /* @vite-ignore */ moduleId ); /** * Inline widget-instance edits stage live, then publish once the user pauses. * A single global timer, so editing several widgets settles into one save. */ const AUTO_SAVE_DELAY_MS = 5e3; const ALLOW_EVERY_OPERATION = () => true; const Context = (0, _wordpress_element.createContext)(null); /** * Compound-internal hook — exposes the full provider state. * Not part of the public API; lives in the same module * so compound components can reach the state directly. */ function useDashboardInternalContext() { const ctx = (0, _wordpress_element.useContext)(Context); if (!ctx) throw new Error("Dashboard compound used outside a WidgetDashboard subtree."); return ctx; } /** * Provider for the dashboard's staging layer. Owns the staging copy of * `layout`; `commit` publishes it when it differs from the committed * prop, `cancel` reverts it. * * Staging re-syncs from the committed prop on prop change. In-flight * edits are dropped silently when an external update (cross-tab commit, * reset, websocket push) lands. Consumers that cannot tolerate this * loss should mediate the prop updates before forwarding them here. * * @param {ProviderProps} props Component props. */ function WidgetDashboardProvider({ widgetTypes, isResolvingWidgetTypes = false, layout: committedLayout, onLayoutChange, onLayoutReset, editMode = false, onEditChange, resolveWidgetModule = DEFAULT_RESOLVE_WIDGET_MODULE, gridSettings: committedGridSettings = ___utils_default_grid_index_ts.DEFAULT_GRID, children }) { const [stagingLayout, setStagingLayout] = (0, _wordpress_element.useState)(committedLayout); const [runtimeActions] = (0, _wordpress_element.useState)(___utils_runtime_actions_map_ts.createRuntimeActionsMap); const [pendingActions] = (0, _wordpress_element.useState)(___utils_pending_actions_map_ts.createPendingActionsMap); const policy = (0, ___components_dashboard_policy_index_ts.useDashboardPolicy)(); const canPerform = (0, _wordpress_element.useMemo)(() => policy ? (request) => !!policy(request) : ALLOW_EVERY_OPERATION, [policy]); const stageLayout = (0, _wordpress_element.useCallback)((next) => { setStagingLayout((previous) => canPerform === ALLOW_EVERY_OPERATION ? next : (0, ___utils_enforce_layout_policy_ts.enforceLayoutPolicy)({ previous, next, canPerform, widgetTypes })); }, [canPerform, widgetTypes]); (0, _wordpress_element.useEffect)(() => { setStagingLayout(committedLayout); }, [committedLayout]); const gridSettings = (0, _wordpress_element.useMemo)(() => resolveGridSettings(committedGridSettings), [committedGridSettings]); const hasLayoutChanges = (0, _wordpress_element.useMemo)(() => !(0, fast_deep_equal_es6_index_js.default)((0, ___utils_canonicalize_layout_ts.canonicalizeLayout)(committedLayout), (0, ___utils_canonicalize_layout_ts.canonicalizeLayout)(stagingLayout)), [committedLayout, stagingLayout]); const hasUncommittedChanges = hasLayoutChanges; const commit = (0, _wordpress_element.useCallback)((options) => { if (hasLayoutChanges) onLayoutChange((0, ___utils_canonicalize_layout_ts.canonicalizeLayout)(stagingLayout)); if (options?.exitEditMode !== false) onEditChange?.(false); }, [ hasLayoutChanges, onLayoutChange, stagingLayout, onEditChange ]); const publishAutoSave = (0, _wordpress_compose.useEvent)(() => commit({ exitEditMode: false })); const scheduleAutoSave = (0, _wordpress_element.useMemo)(() => (0, _wordpress_compose.debounce)(publishAutoSave, AUTO_SAVE_DELAY_MS), [publishAutoSave]); const flushAutoSave = (0, _wordpress_element.useCallback)(() => scheduleAutoSave.flush(), [scheduleAutoSave]); (0, _wordpress_element.useEffect)(() => { if (!editMode) return; scheduleAutoSave.flush(); }, [editMode, scheduleAutoSave]); (0, _wordpress_element.useEffect)(() => () => scheduleAutoSave.flush(), [scheduleAutoSave]); const cancel = (0, _wordpress_element.useCallback)(() => { setStagingLayout(committedLayout); onEditChange?.(false); }, [committedLayout, onEditChange]); (0, _wordpress_element.useEffect)(() => { if (stagingLayout.length === 0 && canPerform({ operation: "customize" })) onEditChange?.(true); }, [stagingLayout.length === 0]); const value = (0, _wordpress_element.useMemo)(() => ({ widgetTypes, isResolvingWidgetTypes, layout: stagingLayout, onLayoutChange: stageLayout, onLayoutReset, gridSettings, commit, cancel, scheduleAutoSave, flushAutoSave, hasUncommittedChanges, editMode, onEditChange, resolveWidgetModule, canPerform, runtimeActions, pendingActions }), [ widgetTypes, isResolvingWidgetTypes, stagingLayout, stageLayout, onLayoutReset, gridSettings, commit, cancel, scheduleAutoSave, flushAutoSave, hasUncommittedChanges, editMode, onEditChange, resolveWidgetModule, canPerform, runtimeActions, pendingActions ]); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Context.Provider, { value, children }); } exports.WidgetDashboardProvider = WidgetDashboardProvider; exports.useDashboardInternalContext = useDashboardInternalContext; //# sourceMappingURL=dashboard-context.cjs.map