Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/widget-dashboard/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
nothing, and the wp-admin dashboard pins it where it reads its stored
settings, so preferences persisted by the removed Columns control stay
inert ([#82204](https://github.com/WordPress/gutenberg/pull/82204)).
- The staging layer enforces the policy on every instance operation,
whichever trigger staged the mutation: a denied `move` holds the
instance's index, a denied `resize` keeps its spans, a denied `edit`
keeps its attributes, and a new instance of a rejected type is
dropped ([#82256](https://github.com/WordPress/gutenberg/pull/82256)).

### Documentation

- Policy contract: instance operations carry `widgetType` only while
the type is registered; a gone plugin or a failed load leaves it
absent, so locks keyed on the type do not fire
([#82256](https://github.com/WordPress/gutenberg/pull/82256)).

### Internal

Expand Down
6 changes: 5 additions & 1 deletion packages/widget-dashboard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,17 @@ Each request names the operation and carries its subject, so a branch on `reques
| `customize` | none | The Customize button, the `core/dashboard/customize` command, the `core/dashboard/add-widgets` command outside edit mode, and the automatic entry into customize mode on an empty layout. |
| `reset` | none | The Reset to default entry in the overflow menu, the `core/dashboard/reset-to-default` command, and the confirmation prompt they open. A denied reset is hidden, not disabled. |
| `insert` | `widgetType` | Whether the inserter offers the type; a rejected type keeps rendering where already placed. The Add widget button and command show only while some registered type is insertable. |
| `remove` | `widget`, `widgetType?` | The Remove control in customize mode. The staging layer re-asserts, in place, a locked instance dropped by any trigger. |
| `remove` | `widget`, `widgetType?` | The Remove control in customize mode. |
| `move` | `widget`, `widgetType?` | Dragging the tile in customize mode. A denied tile is pinned: it holds its index while the other tiles reorder around it; a change ahead of it can still reflow the cell it lands in. |
| `resize` | `widget`, `widgetType?` | The resize handle and the width menu. |
| `edit` | `widget`, `widgetType?` | Attribute editing: the inline fields and the settings trigger in the header, the settings surface, and the widget's `setAttributes`, which is absent when denied so the widget renders read-only. |

The engine resolves the policy once, in its provider, and every surface asks that resolved answer, so further sources join at the same point without touching the surfaces.

The staging layer enforces the same answers. Every mutation is diffed per instance on arrival, whichever trigger staged it, and each change the policy denies is re-asserted before it lands: a removed locked instance returns at its index, a denied move holds its index, a denied resize keeps its spans, a denied edit keeps its attributes, and a new instance of a rejected type is dropped. What the interface hides, the staging layer rejects.

Instance operations carry `widgetType` only while that type is still in `widgetTypes`. A plugin that is gone, or a render module that failed to load, leaves the field absent: the engine still asks, because the instance is still on the board, but a policy that keys its locks on the type has nothing to match. Locks that must survive that case belong on the instance itself. The same absence is why a new instance of an unregistered type is not dropped: there is no type to ask about.

Rules of the contract:

- **Return `true` for operations you do not govern.** Policies compose restrictively, so a default `false` would deny every operation added later.
Expand Down
79 changes: 20 additions & 59 deletions packages/widget-dashboard/src/context/dashboard-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import type {
ResolveWidgetModule,
WidgetType,
} from '@wordpress/widget-primitives';
import { canonicalizeLayout } from '../utils/canonicalize-layout';
import { DEFAULT_GRID } from '../utils/default-grid';
import { enforceLayoutPolicy } from '../utils/enforce-layout-policy';
import { normalizeGridSettings } from '../utils/normalize-grid-settings';
import { resolveDashboardColumnCap } from '../utils/resolve-dashboard-column-count/resolve-dashboard-column-count';
import { DEFAULT_ROW_HEIGHT } from '../utils/row-height-presets';
Expand Down Expand Up @@ -45,33 +47,6 @@ const DEFAULT_RESOLVE_WIDGET_MODULE: ResolveWidgetModule = ( moduleId ) =>
*/
const AUTO_SAVE_DELAY_MS = 5000;

/**
* Canonical form of `layout`: widgets sorted by `placement.order` (falling
* back to array index), then `order` stripped since position now implies it.
* Used both as the comparison form for `hasUncommittedChanges` (so a change
* and its undo compare equal) and as the publish form, keeping persisted
* payloads free of redundant `order` fields.
*
* @param {DashboardWidget[]} layout Layout to canonicalize.
* @return {DashboardWidget[]} Canonicalized layout.
*/
function canonicalize( layout: DashboardWidget[] ): DashboardWidget[] {
const indexed = layout.map( ( widget, index ) => ( {
widget,
order: widget.placement?.order ?? index,
} ) );

indexed.sort( ( a, b ) => a.order - b.order );

return indexed.map( ( { widget } ) => {
if ( ! widget.placement ) {
return widget;
}
const { order: _stripped, ...placement } = widget.placement;
return { ...widget, placement };
} );
}

/**
* Rich state distributed to every compound component inside `WidgetDashboard`.
* Internal — compounds reach the full state via `useDashboardInternalContext()`.
Expand Down Expand Up @@ -218,37 +193,23 @@ export function WidgetDashboardProvider( {
[ policy ]
);

// Every mutation stages through here. Instances the policy locks against
// removal are re-asserted right after the nearest preceding instance that
// survived, so no composed trigger can drop or displace them.
// Every mutation stages through here, diffed against the current
// staging so every change the policy denies is re-asserted before it
// lands: what the interface hides, the staging layer rejects. Without
// a policy there is nothing to enforce, and staging stays byte-equal
// to what the trigger wrote.
const stageLayout = useCallback(
( next: DashboardWidget[] ) => {
setStagingLayout( ( previous ) => {
const staged = [ ...next ];
let insertAt = 0;
previous.forEach( ( widget ) => {
const position = staged.findIndex(
( { uuid } ) => uuid === widget.uuid
);
if ( position !== -1 ) {
insertAt = position + 1;
return;
}
const removable = canPerform( {
operation: 'remove',
widget,
widgetType: widgetTypes.find(
( type ) => type.name === widget.type
),
} );
if ( removable ) {
return;
}
staged.splice( insertAt, 0, widget );
insertAt += 1;
} );
return staged.length === next.length ? next : staged;
} );
setStagingLayout( ( previous ) =>
canPerform === ALLOW_EVERY_OPERATION
? next
: enforceLayoutPolicy( {
previous,
next,
canPerform,
widgetTypes,
} )
);
},
[ canPerform, widgetTypes ]
);
Expand All @@ -268,8 +229,8 @@ export function WidgetDashboardProvider( {
const hasLayoutChanges = useMemo(
() =>
! fastDeepEqual(
canonicalize( committedLayout ),
canonicalize( stagingLayout )
canonicalizeLayout( committedLayout ),
canonicalizeLayout( stagingLayout )
),
[ committedLayout, stagingLayout ]
);
Expand All @@ -279,7 +240,7 @@ export function WidgetDashboardProvider( {
const commit = useCallback(
( options?: CommitOptions ) => {
if ( hasLayoutChanges ) {
onLayoutChange( canonicalize( stagingLayout ) );
onLayoutChange( canonicalizeLayout( stagingLayout ) );
}

if ( options?.exitEditMode !== false ) {
Expand Down
Loading
Loading