@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
141 lines (140 loc) • 5.64 kB
JavaScript
;
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 __canonicalize_layout_ts = require("./canonicalize-layout.cjs");
// packages/widget-dashboard/src/utils/enforce-layout-policy.ts
const SIZE_FIELDS = ["width", "height"];
const POSITION_FIELDS = ["lane"];
function placementOf(widget) {
return widget.placement ?? {};
}
function placementFieldsEqual(a, b, fields) {
const pa = placementOf(a);
const pb = placementOf(b);
return fields.every((field) => pa[field] === pb[field]);
}
function restorePlacementFields(widget, prior, fields) {
const restored = { ...placementOf(widget) };
const priorPlacement = placementOf(prior);
for (const field of fields) if (field in priorPlacement) restored[field] = priorPlacement[field];
else delete restored[field];
if (!Object.keys(restored).length && !prior.placement) {
const { placement: _dropped, ...rest } = widget;
return rest;
}
return {
...widget,
placement: restored
};
}
/**
* Re-asserts every change the policy denies before a layout enters
* staging, so no composed trigger can slip a denied operation past the
* interface: what the UI hides, the staging layer rejects.
*
* Per instance: a new instance whose type the policy rejects for
* `insert` is dropped. An unregistered type has no type to ask about,
* so a new instance of one passes, and instance operations on a
* surviving tile still ask with `widgetType` absent: a lock keyed on
* the type does not fire. Denied `edit` keeps the staged `attributes`;
* denied `resize` keeps the placement spans; denied `move` keeps the
* masonry `lane` and holds the instance's canonical position, adjusted
* for allowed membership changes: the same hold `remove` uses, and the
* position the grid's pinned items keep.
*
* Both layouts are canonicalized before the diff (sorted by
* `order ?? index` with `order` stripped, as the renderer and the
* commit read them), so a move is a move whichever encoding expressed
* it. Returns `next` itself when it was already canonical and nothing
* was re-asserted.
*
* @param {EnforceLayoutPolicyArgs} args The layouts to diff and the policy.
* @return {DashboardWidget[]} The incoming layout with denied changes re-asserted.
*/
function enforceLayoutPolicy({ previous: rawPrevious, next: rawNext, canPerform, widgetTypes }) {
const previous = (0, __canonicalize_layout_ts.canonicalizeLayout)(rawPrevious);
const next = (0, __canonicalize_layout_ts.canonicalizeLayout)(rawNext);
const typeOf = (widget) => widgetTypes.find((type) => type.name === widget.type);
const priorByUuid = new Map(previous.map((widget) => [widget.uuid, widget]));
const free = [];
const heldByUuid = /* @__PURE__ */ new Map();
for (const incoming of next) {
const prior = priorByUuid.get(incoming.uuid);
if (!prior) {
const widgetType = typeOf(incoming);
if (widgetType && !canPerform({
operation: "insert",
widgetType
})) continue;
free.push(incoming);
continue;
}
const widgetType = typeOf(prior);
const allows = (operation) => canPerform({
operation,
widget: prior,
widgetType
});
let staged = incoming;
if (!(0, fast_deep_equal_es6_index_js.default)(prior.attributes, incoming.attributes) && !allows("edit")) {
staged = { ...staged };
if ("attributes" in prior) staged.attributes = prior.attributes;
else delete staged.attributes;
}
if (!placementFieldsEqual(prior, staged, SIZE_FIELDS) && !allows("resize")) staged = restorePlacementFields(staged, prior, SIZE_FIELDS);
if (allows("move")) {
free.push(staged);
continue;
}
if (!placementFieldsEqual(prior, staged, POSITION_FIELDS)) staged = restorePlacementFields(staged, prior, POSITION_FIELDS);
heldByUuid.set(staged.uuid, staged);
}
const nextUuids = new Set(next.map((widget) => widget.uuid));
const result = [...free];
let absent = 0;
const place = (widget, index) => result.splice(Math.min(index - absent, result.length), 0, widget);
previous.forEach((widget, index) => {
if (!nextUuids.has(widget.uuid)) {
if (canPerform({
operation: "remove",
widget,
widgetType: typeOf(widget)
})) {
absent += 1;
return;
}
place(widget, index);
return;
}
const held = heldByUuid.get(widget.uuid);
if (held) place(held, index);
});
return result.length === next.length && result.every((widget, index) => widget === next[index]) ? next : result;
}
exports.enforceLayoutPolicy = enforceLayoutPolicy;
//# sourceMappingURL=enforce-layout-policy.cjs.map