@paroicms/server
Version:
The ParoiCMS server
84 lines • 3.51 kB
JavaScript
import { isJsonFieldValue, isMediaHandleValue, isReadLabelingValue, unwrapReadFieldValues, } from "@paroicms/public-anywhere-lib";
export function computeChangeSummary(options) {
const { currentFieldValues, previousFieldValues, currentDocumentValues, previousDocumentValues } = options;
const affectedFields = computeAffectedFields(currentFieldValues, previousFieldValues);
const affectedDocumentMetadata = computeAffectedDocumentMetadata(currentDocumentValues, previousDocumentValues);
if (affectedFields.length === 0 && affectedDocumentMetadata.length === 0) {
return;
}
const result = {};
if (affectedFields.length > 0) {
result.affectedFields = affectedFields;
}
if (affectedDocumentMetadata.length > 0) {
result.affectedDocumentMetadata = affectedDocumentMetadata;
}
return result;
}
function computeAffectedFields(currentValues, previousValues) {
const currentUnwrapped = unwrapReadFieldValues(currentValues);
const previous = previousValues ?? {};
const allFieldNames = new Set([...Object.keys(currentUnwrapped), ...Object.keys(previous)]);
const affectedFields = [];
for (const fieldName of allFieldNames) {
const currentValue = currentUnwrapped[fieldName];
const previousValue = previous[fieldName];
if (!areReadFieldValuesEqual(currentValue, previousValue)) {
affectedFields.push(fieldName);
}
}
return affectedFields;
}
function computeAffectedDocumentMetadata(current, previous) {
if (!current && !previous)
return [];
const affectedMetadata = [];
if (!areNullableStringsEqual(current?.title, previous?.title)) {
affectedMetadata.push("title");
}
if (!areNullableStringsEqual(current?.slug, previous?.slug)) {
affectedMetadata.push("slug");
}
if (!areNullableStringsEqual(current?.metaDescription, previous?.metaDescription)) {
affectedMetadata.push("metaDescription");
}
if (!areNullableStringsEqual(current?.metaKeywords, previous?.metaKeywords)) {
affectedMetadata.push("metaKeywords");
}
if (current?.ready !== previous?.ready) {
affectedMetadata.push("ready");
}
return affectedMetadata;
}
function areNullableStringsEqual(a, b) {
return a === b;
}
function areReadFieldValuesEqual(current, previous) {
if (current === undefined && previous === undefined)
return true;
if (current === undefined || previous === undefined)
return false;
if (typeof current === "string" || typeof current === "number" || typeof current === "boolean") {
return current === previous;
}
if (isReadLabelingValue(current)) {
if (!isReadLabelingValue(previous))
return false;
const currentNodeIds = current.t.map((term) => term.nodeId);
const previousNodeIds = previous.t.map((term) => term.nodeId);
return (currentNodeIds.length === previousNodeIds.length &&
currentNodeIds.every((id, i) => id === previousNodeIds[i]));
}
if (isJsonFieldValue(current)) {
if (!isJsonFieldValue(previous))
return false;
return JSON.stringify(current.j) === JSON.stringify(previous.j);
}
if (isMediaHandleValue(current)) {
if (!isMediaHandleValue(previous))
return false;
return current.h === previous.h;
}
return JSON.stringify(current) === JSON.stringify(previous);
}
//# sourceMappingURL=change-summary.js.map