Skip to content

Commit 4968513

Browse files
committed
Ignore undefined/internal keys in draft diff
1 parent 5dc6fb9 commit 4968513

2 files changed

Lines changed: 105 additions & 2 deletions

File tree

src/lib/services/contents/draft/index.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ export const revokeDraftFileURLs = (nextFiles = {}) => {
114114
export const filterRealValues = (valueMap) =>
115115
Object.fromEntries(Object.entries(valueMap).filter(([key]) => !INTERNAL_PROP_REGEX.test(key)));
116116

117+
/**
118+
* Check whether a value map key holds content that the comparison below has to look at. Internal
119+
* properties are bookkeeping rather than content. An `undefined` value is left out as well: it’s
120+
* empty when the entry is written, so it makes no difference to the saved file whether the key is
121+
* there, and a key holding one can’t always be reproduced — reverting a field assigns the original
122+
* `undefined` back to a property that was just deleted, which leaves a state proxy without the key.
123+
* @param {FlattenedEntryContent} valueMap Value map to look in.
124+
* @param {string} key Key to check.
125+
* @returns {boolean} Whether the key counts.
126+
*/
127+
const isRealKey = (valueMap, key) => !INTERNAL_PROP_REGEX.test(key) && valueMap[key] !== undefined;
128+
117129
/**
118130
* Compare a locale’s original and current value maps, ignoring internal properties in the current
119131
* one. Equivalent to deep-comparing {@link filterRealValues} of the current map against the
@@ -126,7 +138,7 @@ const isValueMapModified = (originalValueMap, currentValueMap) => {
126138
let realKeyCount = 0;
127139

128140
const anyValueChanged = Object.keys(currentValueMap).some((key) => {
129-
if (INTERNAL_PROP_REGEX.test(key)) {
141+
if (!isRealKey(currentValueMap, key)) {
130142
return false;
131143
}
132144

@@ -138,7 +150,11 @@ const isValueMapModified = (originalValueMap, currentValueMap) => {
138150
});
139151

140152
// Also catch keys that only exist in the original map, which the loop above cannot see
141-
return anyValueChanged || Object.keys(originalValueMap).length !== realKeyCount;
153+
return (
154+
anyValueChanged ||
155+
Object.keys(originalValueMap).filter((key) => isRealKey(originalValueMap, key)).length !==
156+
realKeyCount
157+
);
142158
};
143159

144160
/**

src/lib/services/contents/draft/index.test.js

Lines changed: 87 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)