@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
107 lines (106 loc) • 3.43 kB
JavaScript
// packages/widget-dashboard/src/utils/enforce-layout-policy.ts
import fastDeepEqual from "fast-deep-equal/es6/index.js";
import { canonicalizeLayout } from "./canonicalize-layout.mjs";
var SIZE_FIELDS = ["width", "height"];
var 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 };
}
function enforceLayoutPolicy({
previous: rawPrevious,
next: rawNext,
canPerform,
widgetTypes
}) {
const previous = canonicalizeLayout(rawPrevious);
const next = 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 widgetType2 = typeOf(incoming);
if (widgetType2 && !canPerform({ operation: "insert", widgetType: widgetType2 })) {
continue;
}
free.push(incoming);
continue;
}
const widgetType = typeOf(prior);
const allows = (operation) => canPerform({ operation, widget: prior, widgetType });
let staged = incoming;
if (!fastDeepEqual(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)) {
const removable = canPerform({
operation: "remove",
widget,
widgetType: typeOf(widget)
});
if (removable) {
absent += 1;
return;
}
place(widget, index);
return;
}
const held = heldByUuid.get(widget.uuid);
if (held) {
place(held, index);
}
});
const untouched = result.length === next.length && result.every((widget, index) => widget === next[index]);
return untouched ? next : result;
}
export {
enforceLayoutPolicy
};
//# sourceMappingURL=enforce-layout-policy.mjs.map