@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
142 lines (140 loc) • 5.19 kB
JavaScript
;
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/utils/enforce-layout-policy.ts
var enforce_layout_policy_exports = {};
__export(enforce_layout_policy_exports, {
enforceLayoutPolicy: () => enforceLayoutPolicy
});
module.exports = __toCommonJS(enforce_layout_policy_exports);
var import_es6 = __toESM(require("fast-deep-equal/es6/index.js"));
var import_canonicalize_layout = require("./canonicalize-layout.cjs");
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 = (0, import_canonicalize_layout.canonicalizeLayout)(rawPrevious);
const next = (0, import_canonicalize_layout.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 (!(0, import_es6.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)) {
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;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
enforceLayoutPolicy
});
//# sourceMappingURL=enforce-layout-policy.cjs.map