@wordpress/preferences-persistence 1.30.0 → 1.32.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/build/create/debounce-async.js +5 -7
- package/build/create/debounce-async.js.map +1 -1
- package/build/create/index.js +15 -18
- package/build/create/index.js.map +1 -1
- package/build/index.js +3 -9
- package/build/index.js.map +1 -1
- package/build/migrations/legacy-local-storage-data/convert-edit-post-panels.js +0 -5
- package/build/migrations/legacy-local-storage-data/convert-edit-post-panels.js.map +1 -1
- package/build/migrations/legacy-local-storage-data/index.js +15 -20
- package/build/migrations/legacy-local-storage-data/index.js.map +1 -1
- package/build/migrations/legacy-local-storage-data/move-feature-preferences.js +20 -19
- package/build/migrations/legacy-local-storage-data/move-feature-preferences.js.map +1 -1
- package/build/migrations/legacy-local-storage-data/move-individual-preference.js +18 -14
- package/build/migrations/legacy-local-storage-data/move-individual-preference.js.map +1 -1
- package/build/migrations/legacy-local-storage-data/move-interface-enable-items.js +23 -18
- package/build/migrations/legacy-local-storage-data/move-interface-enable-items.js.map +1 -1
- package/build/migrations/legacy-local-storage-data/move-third-party-feature-preferences.js +8 -12
- package/build/migrations/legacy-local-storage-data/move-third-party-feature-preferences.js.map +1 -1
- package/build/migrations/preferences-package-data/convert-complementary-areas.js +4 -4
- package/build/migrations/preferences-package-data/convert-complementary-areas.js.map +1 -1
- package/build/migrations/preferences-package-data/index.js +1 -3
- package/build/migrations/preferences-package-data/index.js.map +1 -1
- package/build-module/create/debounce-async.js +5 -6
- package/build-module/create/debounce-async.js.map +1 -1
- package/build-module/create/index.js +15 -15
- package/build-module/create/index.js.map +1 -1
- package/build-module/index.js +4 -5
- package/build-module/index.js.map +1 -1
- package/build-module/migrations/legacy-local-storage-data/convert-edit-post-panels.js +0 -4
- package/build-module/migrations/legacy-local-storage-data/convert-edit-post-panels.js.map +1 -1
- package/build-module/migrations/legacy-local-storage-data/index.js +16 -13
- package/build-module/migrations/legacy-local-storage-data/index.js.map +1 -1
- package/build-module/migrations/legacy-local-storage-data/move-feature-preferences.js +20 -18
- package/build-module/migrations/legacy-local-storage-data/move-feature-preferences.js.map +1 -1
- package/build-module/migrations/legacy-local-storage-data/move-individual-preference.js +18 -13
- package/build-module/migrations/legacy-local-storage-data/move-individual-preference.js.map +1 -1
- package/build-module/migrations/legacy-local-storage-data/move-interface-enable-items.js +23 -17
- package/build-module/migrations/legacy-local-storage-data/move-interface-enable-items.js.map +1 -1
- package/build-module/migrations/legacy-local-storage-data/move-third-party-feature-preferences.js +8 -11
- package/build-module/migrations/legacy-local-storage-data/move-third-party-feature-preferences.js.map +1 -1
- package/build-module/migrations/preferences-package-data/convert-complementary-areas.js +4 -3
- package/build-module/migrations/preferences-package-data/convert-complementary-areas.js.map +1 -1
- package/build-module/migrations/preferences-package-data/index.js.map +1 -1
- package/package.json +3 -3
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"names":["identity","arg","moveIndividualPreferenceToPreferences","state","from","sourceStoreName","to","scope","key","convert","preferencesStoreName","sourcePreference","preferences","undefined","targetPreference","otherScopes","otherPreferences","otherSourceState","allSourcePreferences","convertedPreferences"],"sources":["@wordpress/preferences-persistence/src/migrations/legacy-local-storage-data/move-individual-preference.js"],"sourcesContent":["const identity = ( arg ) => arg;\n\n/**\n * Migrates an individual item inside the `preferences` object for a package's store.\n *\n * Previously, some packages had individual 'preferences' of any data type, and many used\n * complex nested data structures. For example:\n * ```js\n * {\n * 'core/edit-post': {\n * preferences: {\n * panels: {\n * publish: {\n * opened: true,\n * enabled: true,\n * }\n * },\n * // ...other preferences.\n * },\n * },\n * }\n *\n * This function supports moving an individual preference like 'panels' above into the\n * preferences package data structure.\n *\n * It supports moving a preference to a particular scope in the preferences store and\n * optionally converting the data using a `convert` function.\n *\n * ```\n *\n * @param {Object} state The original state.\n * @param {Object} migrate An options object that contains details of the migration.\n * @param {string} migrate.from The name of the store to migrate from.\n * @param {string} migrate.to The scope in the preferences store to migrate to.\n * @param {string} key The key in the preferences object to migrate.\n * @param {?Function} convert A function that converts preferences from one format to another.\n */\nexport default function moveIndividualPreferenceToPreferences(\n\tstate,\n\t{ from: sourceStoreName, to: scope },\n\tkey,\n\tconvert = identity\n) {\n\tconst preferencesStoreName = 'core/preferences';\n\tconst sourcePreference = state?.[ sourceStoreName ]?.preferences?.[ key ];\n\n\t// There's nothing to migrate, exit early.\n\tif ( sourcePreference === undefined ) {\n\t\treturn state;\n\t}\n\n\tconst targetPreference =\n\t\tstate?.[ preferencesStoreName ]?.preferences?.[ scope ]?.[ key ];\n\n\t// There's existing data at the target, so don't overwrite it, exit early.\n\tif ( targetPreference ) {\n\t\treturn state;\n\t}\n\n\tconst otherScopes = state?.[ preferencesStoreName ]?.preferences;\n\tconst otherPreferences =\n\t\tstate?.[ preferencesStoreName ]?.preferences?.[ scope ];\n\n\tconst otherSourceState = state?.[ sourceStoreName ];\n\tconst allSourcePreferences = state?.[ sourceStoreName ]?.preferences;\n\n\t// Pass an object with the key and value as this allows the convert\n\t// function to convert to a data structure that has different keys.\n\tconst convertedPreferences = convert( { [ key ]: sourcePreference } );\n\n\treturn {\n\t\t...state,\n\t\t[ preferencesStoreName ]: {\n\t\t\tpreferences: {\n\t\t\t\t...otherScopes,\n\t\t\t\t[ scope ]: {\n\t\t\t\t\t...otherPreferences,\n\t\t\t\t\t...convertedPreferences,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\t[ sourceStoreName ]: {\n\t\t\t...otherSourceState,\n\t\t\tpreferences: {\n\t\t\t\t...allSourcePreferences,\n\t\t\t\t[ key ]: undefined,\n\t\t\t},\n\t\t},\n\t};\n}\n"],"mappings":";;;;;;AAAA,MAAMA,QAAQ,GAAKC,GAAG,IAAMA,GAAG;;AAE/B;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAC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ACD,CAAC;AACF"}
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = moveInterfaceEnableItems;
|
7
|
-
|
8
7
|
/**
|
9
8
|
* Migrates interface 'enableItems' data to the preferences store.
|
10
9
|
*
|
@@ -54,55 +53,61 @@ exports.default = moveInterfaceEnableItems;
|
|
54
53
|
*/
|
55
54
|
function moveInterfaceEnableItems(state) {
|
56
55
|
var _state$preferencesSto, _sourceEnableItems$si, _sourceEnableItems$mu;
|
57
|
-
|
58
56
|
const interfaceStoreName = 'core/interface';
|
59
57
|
const preferencesStoreName = 'core/preferences';
|
60
|
-
const sourceEnableItems = state?.[interfaceStoreName]?.enableItems;
|
58
|
+
const sourceEnableItems = state?.[interfaceStoreName]?.enableItems;
|
61
59
|
|
60
|
+
// There's nothing to migrate, exit early.
|
62
61
|
if (!sourceEnableItems) {
|
63
62
|
return state;
|
64
63
|
}
|
64
|
+
const allPreferences = (_state$preferencesSto = state?.[preferencesStoreName]?.preferences) !== null && _state$preferencesSto !== void 0 ? _state$preferencesSto : {};
|
65
65
|
|
66
|
-
|
66
|
+
// First convert complementaryAreas into the right format.
|
67
67
|
// Use the existing preferences as the accumulator so that the data is
|
68
68
|
// merged.
|
69
|
-
|
70
69
|
const sourceComplementaryAreas = (_sourceEnableItems$si = sourceEnableItems?.singleEnableItems?.complementaryArea) !== null && _sourceEnableItems$si !== void 0 ? _sourceEnableItems$si : {};
|
71
70
|
const preferencesWithConvertedComplementaryAreas = Object.keys(sourceComplementaryAreas).reduce((accumulator, scope) => {
|
72
|
-
const data = sourceComplementaryAreas[scope];
|
71
|
+
const data = sourceComplementaryAreas[scope];
|
73
72
|
|
73
|
+
// Don't overwrite any existing data in the preferences store.
|
74
74
|
if (accumulator?.[scope]?.complementaryArea) {
|
75
75
|
return accumulator;
|
76
76
|
}
|
77
|
-
|
78
|
-
|
79
|
-
[scope]: {
|
77
|
+
return {
|
78
|
+
...accumulator,
|
79
|
+
[scope]: {
|
80
|
+
...accumulator[scope],
|
80
81
|
complementaryArea: data
|
81
82
|
}
|
82
83
|
};
|
83
|
-
}, allPreferences);
|
84
|
-
// converts the pinned items, resulting in the fully migrated data.
|
84
|
+
}, allPreferences);
|
85
85
|
|
86
|
+
// Next feed the converted complementary areas back into a reducer that
|
87
|
+
// converts the pinned items, resulting in the fully migrated data.
|
86
88
|
const sourcePinnedItems = (_sourceEnableItems$mu = sourceEnableItems?.multipleEnableItems?.pinnedItems) !== null && _sourceEnableItems$mu !== void 0 ? _sourceEnableItems$mu : {};
|
87
89
|
const allConvertedData = Object.keys(sourcePinnedItems).reduce((accumulator, scope) => {
|
88
|
-
const data = sourcePinnedItems[scope];
|
89
|
-
|
90
|
+
const data = sourcePinnedItems[scope];
|
91
|
+
// Don't overwrite any existing data in the preferences store.
|
90
92
|
if (accumulator?.[scope]?.pinnedItems) {
|
91
93
|
return accumulator;
|
92
94
|
}
|
93
|
-
|
94
|
-
|
95
|
-
[scope]: {
|
95
|
+
return {
|
96
|
+
...accumulator,
|
97
|
+
[scope]: {
|
98
|
+
...accumulator[scope],
|
96
99
|
pinnedItems: data
|
97
100
|
}
|
98
101
|
};
|
99
102
|
}, preferencesWithConvertedComplementaryAreas);
|
100
103
|
const otherInterfaceItems = state[interfaceStoreName];
|
101
|
-
return {
|
104
|
+
return {
|
105
|
+
...state,
|
102
106
|
[preferencesStoreName]: {
|
103
107
|
preferences: allConvertedData
|
104
108
|
},
|
105
|
-
[interfaceStoreName]: {
|
109
|
+
[interfaceStoreName]: {
|
110
|
+
...otherInterfaceItems,
|
106
111
|
enableItems: undefined
|
107
112
|
}
|
108
113
|
};
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"names":["moveInterfaceEnableItems","state","_state$preferencesSto","_sourceEnableItems$si","_sourceEnableItems$mu","interfaceStoreName","preferencesStoreName","sourceEnableItems","enableItems","allPreferences","preferences","sourceComplementaryAreas","singleEnableItems","complementaryArea","preferencesWithConvertedComplementaryAreas","Object","keys","reduce","accumulator","scope","data","sourcePinnedItems","multipleEnableItems","pinnedItems","allConvertedData","otherInterfaceItems","undefined"],"sources":["@wordpress/preferences-persistence/src/migrations/legacy-local-storage-data/move-interface-enable-items.js"],"sourcesContent":["/**\n * Migrates interface 'enableItems' data to the preferences store.\n *\n * The interface package stores this data in this format:\n * ```js\n * {\n * enableItems: {\n * singleEnableItems: {\n * \t complementaryArea: {\n * 'core/edit-post': 'edit-post/document',\n * 'core/edit-site': 'edit-site/global-styles',\n * }\n * },\n * multipleEnableItems: {\n * pinnedItems: {\n * 'core/edit-post': {\n * 'plugin-1': true,\n * },\n * 'core/edit-site': {\n * 'plugin-2': true,\n * },\n * },\n * }\n * }\n * }\n * ```\n *\n * and it should be converted it to:\n * ```js\n * {\n * 'core/edit-post': {\n * complementaryArea: 'edit-post/document',\n * pinnedItems: {\n * 'plugin-1': true,\n * },\n * },\n * 'core/edit-site': {\n * complementaryArea: 'edit-site/global-styles',\n * pinnedItems: {\n * 'plugin-2': true,\n * },\n * },\n * }\n * ```\n *\n * @param {Object} state The local storage state.\n */\nexport default function moveInterfaceEnableItems( state ) {\n\tconst interfaceStoreName = 'core/interface';\n\tconst preferencesStoreName = 'core/preferences';\n\tconst sourceEnableItems = state?.[ interfaceStoreName ]?.enableItems;\n\n\t// There's nothing to migrate, exit early.\n\tif ( ! sourceEnableItems ) {\n\t\treturn state;\n\t}\n\n\tconst allPreferences = state?.[ preferencesStoreName ]?.preferences ?? {};\n\n\t// First convert complementaryAreas into the right format.\n\t// Use the existing preferences as the accumulator so that the data is\n\t// merged.\n\tconst sourceComplementaryAreas =\n\t\tsourceEnableItems?.singleEnableItems?.complementaryArea ?? {};\n\n\tconst preferencesWithConvertedComplementaryAreas = Object.keys(\n\t\tsourceComplementaryAreas\n\t).reduce( ( accumulator, scope ) => {\n\t\tconst data = sourceComplementaryAreas[ scope ];\n\n\t\t// Don't overwrite any existing data in the preferences store.\n\t\tif ( accumulator?.[ scope ]?.complementaryArea ) {\n\t\t\treturn accumulator;\n\t\t}\n\n\t\treturn {\n\t\t\t...accumulator,\n\t\t\t[ scope ]: {\n\t\t\t\t...accumulator[ scope ],\n\t\t\t\tcomplementaryArea: data,\n\t\t\t},\n\t\t};\n\t}, allPreferences );\n\n\t// Next feed the converted complementary areas back into a reducer that\n\t// converts the pinned items, resulting in the fully migrated data.\n\tconst sourcePinnedItems =\n\t\tsourceEnableItems?.multipleEnableItems?.pinnedItems ?? {};\n\tconst allConvertedData = Object.keys( sourcePinnedItems ).reduce(\n\t\t( accumulator, scope ) => {\n\t\t\tconst data = sourcePinnedItems[ scope ];\n\t\t\t// Don't overwrite any existing data in the preferences store.\n\t\t\tif ( accumulator?.[ scope ]?.pinnedItems ) {\n\t\t\t\treturn accumulator;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\t...accumulator,\n\t\t\t\t[ scope ]: {\n\t\t\t\t\t...accumulator[ scope ],\n\t\t\t\t\tpinnedItems: data,\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t\tpreferencesWithConvertedComplementaryAreas\n\t);\n\n\tconst otherInterfaceItems = state[ interfaceStoreName ];\n\n\treturn {\n\t\t...state,\n\t\t[ preferencesStoreName ]: {\n\t\t\tpreferences: allConvertedData,\n\t\t},\n\t\t[ interfaceStoreName ]: {\n\t\t\t...otherInterfaceItems,\n\t\t\tenableItems: undefined,\n\t\t},\n\t};\n}\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,wBAAwBA,CAAEC,KAAK,EAAG;EAAA,IAAAC,qBAAA,EAAAC,qBAAA,EAAAC,q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,CAAE;IACvC;IACA,IAAKD,WAAW,GAAIC,KAAK,CAAE,EAAEI,WAAW,EAAG;MAC1C,OAAOL,WAAW;IACnB;IAEA,OAAO;MACN,GAAGA,WAAW;MACd,CAAEC,KAAK,GAAI;QACV,GAAGD,WAAW,CAAEC,KAAK,CAAE;QACvBI,WAAW,EAAEH;MACd;IACD,CAAC;EACF,CAAC,EACDN,0CACD,CAAC;EAED,MAAMW,mBAAmB,GAAGxB,KAAK,CAAEI,kBAAkB,CAAE;EAEvD,OAAO;IACN,GAAGJ,KAAK;IACR,CAAEK,oBAAoB,GAAI;MACzBI,WAAW,EAAEc;IACd,CAAC;IACD,CAAEnB,kBAAkB,GAAI;MACvB,GAAGoB,mBAAmB;MACtBjB,WAAW,EAAEkB;IACd;EACD,CAAC;AACF"}
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = moveThirdPartyFeaturePreferencesToPreferences;
|
7
|
-
|
8
7
|
/**
|
9
8
|
* The interface package previously had a public API that could be used by
|
10
9
|
* plugins to set persisted boolean 'feature' preferences.
|
@@ -51,40 +50,37 @@ function moveThirdPartyFeaturePreferencesToPreferences(state) {
|
|
51
50
|
const preferencesStoreName = 'core/preferences';
|
52
51
|
const interfaceScopes = state?.[interfaceStoreName]?.preferences?.features;
|
53
52
|
const interfaceScopeKeys = interfaceScopes ? Object.keys(interfaceScopes) : [];
|
54
|
-
|
55
53
|
if (!interfaceScopeKeys?.length) {
|
56
54
|
return state;
|
57
55
|
}
|
58
|
-
|
59
56
|
return interfaceScopeKeys.reduce(function (convertedState, scope) {
|
60
57
|
if (scope.startsWith('core')) {
|
61
58
|
return convertedState;
|
62
59
|
}
|
63
|
-
|
64
60
|
const featuresToMigrate = interfaceScopes?.[scope];
|
65
|
-
|
66
61
|
if (!featuresToMigrate) {
|
67
62
|
return convertedState;
|
68
63
|
}
|
69
|
-
|
70
64
|
const existingMigratedData = convertedState?.[preferencesStoreName]?.preferences?.[scope];
|
71
|
-
|
72
65
|
if (existingMigratedData) {
|
73
66
|
return convertedState;
|
74
67
|
}
|
75
|
-
|
76
68
|
const otherPreferencesScopes = convertedState?.[preferencesStoreName]?.preferences;
|
77
69
|
const otherInterfaceState = convertedState?.[interfaceStoreName];
|
78
70
|
const otherInterfaceScopes = convertedState?.[interfaceStoreName]?.preferences?.features;
|
79
|
-
return {
|
71
|
+
return {
|
72
|
+
...convertedState,
|
80
73
|
[preferencesStoreName]: {
|
81
|
-
preferences: {
|
74
|
+
preferences: {
|
75
|
+
...otherPreferencesScopes,
|
82
76
|
[scope]: featuresToMigrate
|
83
77
|
}
|
84
78
|
},
|
85
|
-
[interfaceStoreName]: {
|
79
|
+
[interfaceStoreName]: {
|
80
|
+
...otherInterfaceState,
|
86
81
|
preferences: {
|
87
|
-
features: {
|
82
|
+
features: {
|
83
|
+
...otherInterfaceScopes,
|
88
84
|
[scope]: undefined
|
89
85
|
}
|
90
86
|
}
|
package/build/migrations/legacy-local-storage-data/move-third-party-feature-preferences.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"names":["moveThirdPartyFeaturePreferencesToPreferences","state","interfaceStoreName","preferencesStoreName","interfaceScopes","preferences","features","interfaceScopeKeys","Object","keys","length","reduce","convertedState","scope","startsWith","featuresToMigrate","existingMigratedData","otherPreferencesScopes","otherInterfaceState","otherInterfaceScopes","undefined"],"sources":["@wordpress/preferences-persistence/src/migrations/legacy-local-storage-data/move-third-party-feature-preferences.js"],"sourcesContent":["/**\n * The interface package previously had a public API that could be used by\n * plugins to set persisted boolean 'feature' preferences.\n *\n * While usage was likely non-existent or very small, this function ensures\n * those are migrated to the preferences data structure. The interface\n * package's APIs have now been deprecated and use the preferences store.\n *\n * This will convert data that looks like this:\n * ```js\n * {\n * 'core/interface': {\n * preferences: {\n * features: {\n * 'my-plugin': {\n * myPluginFeature: true\n * }\n * }\n * }\n * }\n * }\n * ```\n *\n * To this:\n * ```js\n * * {\n * 'core/preferences': {\n * preferences: {\n * 'my-plugin': {\n * myPluginFeature: true\n * }\n * }\n * }\n * }\n * ```\n *\n * @param {Object} state The local storage state\n *\n * @return {Object} The state with third party preferences moved to the\n * preferences data structure.\n */\nexport default function moveThirdPartyFeaturePreferencesToPreferences( state ) {\n\tconst interfaceStoreName = 'core/interface';\n\tconst preferencesStoreName = 'core/preferences';\n\n\tconst interfaceScopes =\n\t\tstate?.[ interfaceStoreName ]?.preferences?.features;\n\tconst interfaceScopeKeys = interfaceScopes\n\t\t? Object.keys( interfaceScopes )\n\t\t: [];\n\n\tif ( ! interfaceScopeKeys?.length ) {\n\t\treturn state;\n\t}\n\n\treturn interfaceScopeKeys.reduce( function ( convertedState, scope ) {\n\t\tif ( scope.startsWith( 'core' ) ) {\n\t\t\treturn convertedState;\n\t\t}\n\n\t\tconst featuresToMigrate = interfaceScopes?.[ scope ];\n\t\tif ( ! featuresToMigrate ) {\n\t\t\treturn convertedState;\n\t\t}\n\n\t\tconst existingMigratedData =\n\t\t\tconvertedState?.[ preferencesStoreName ]?.preferences?.[ scope ];\n\n\t\tif ( existingMigratedData ) {\n\t\t\treturn convertedState;\n\t\t}\n\n\t\tconst otherPreferencesScopes =\n\t\t\tconvertedState?.[ preferencesStoreName ]?.preferences;\n\t\tconst otherInterfaceState = convertedState?.[ interfaceStoreName ];\n\t\tconst otherInterfaceScopes =\n\t\t\tconvertedState?.[ interfaceStoreName ]?.preferences?.features;\n\n\t\treturn {\n\t\t\t...convertedState,\n\t\t\t[ preferencesStoreName ]: {\n\t\t\t\tpreferences: {\n\t\t\t\t\t...otherPreferencesScopes,\n\t\t\t\t\t[ scope ]: featuresToMigrate,\n\t\t\t\t},\n\t\t\t},\n\t\t\t[ interfaceStoreName ]: {\n\t\t\t\t...otherInterfaceState,\n\t\t\t\tpreferences: {\n\t\t\t\t\tfeatures: {\n\t\t\t\t\t\t...otherInterfaceScopes,\n\t\t\t\t\t\t[ scope ]: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BAAkB,CAAE,EAAEG,WAAW,EAAEC,QAAQ;IAE9D,OAAO;MACN,GAAGM,cAAc;MACjB,CAAET,oBAAoB,GAAI;QACzBE,WAAW,EAAE;UACZ,GAAGY,sBAAsB;UACzB,CAAEJ,KAAK,GAAIE;QACZ;MACD,CAAC;MACD,CAAEb,kBAAkB,GAAI;QACvB,GAAGgB,mBAAmB;QACtBb,WAAW,EAAE;UACZC,QAAQ,EAAE;YACT,GAAGa,oBAAoB;YACvB,CAAEN,KAAK,GAAIO;UACZ;QACD;MACD;IACD,CAAC;EACF,CAAC,EAAEnB,KAAM,CAAC;AACX"}
|
@@ -4,20 +4,20 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = convertComplementaryAreas;
|
7
|
-
|
8
7
|
function convertComplementaryAreas(state) {
|
9
8
|
return Object.keys(state).reduce((stateAccumulator, scope) => {
|
10
|
-
const scopeData = state[scope];
|
9
|
+
const scopeData = state[scope];
|
11
10
|
|
11
|
+
// If a complementary area is truthy, convert it to the `isComplementaryAreaVisible` boolean.
|
12
12
|
if (scopeData?.complementaryArea) {
|
13
|
-
const updatedScopeData = {
|
13
|
+
const updatedScopeData = {
|
14
|
+
...scopeData
|
14
15
|
};
|
15
16
|
delete updatedScopeData.complementaryArea;
|
16
17
|
updatedScopeData.isComplementaryAreaVisible = true;
|
17
18
|
stateAccumulator[scope] = updatedScopeData;
|
18
19
|
return stateAccumulator;
|
19
20
|
}
|
20
|
-
|
21
21
|
return stateAccumulator;
|
22
22
|
}, state);
|
23
23
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"names":["convertComplementaryAreas","state","Object","keys","reduce","stateAccumulator","scope","scopeData","complementaryArea","updatedScopeData","isComplementaryAreaVisible"],"sources":["@wordpress/preferences-persistence/src/migrations/preferences-package-data/convert-complementary-areas.js"],"sourcesContent":["export default function convertComplementaryAreas( state ) {\n\treturn Object.keys( state ).reduce( ( stateAccumulator, scope ) => {\n\t\tconst scopeData = state[ scope ];\n\n\t\t// If a complementary area is truthy, convert it to the `isComplementaryAreaVisible` boolean.\n\t\tif ( scopeData?.complementaryArea ) {\n\t\t\tconst updatedScopeData = { ...scopeData };\n\t\t\tdelete updatedScopeData.complementaryArea;\n\t\t\tupdatedScopeData.isComplementaryAreaVisible = true;\n\t\t\tstateAccumulator[ scope ] = updatedScopeData;\n\t\t\treturn stateAccumulator;\n\t\t}\n\n\t\treturn stateAccumulator;\n\t}, state );\n}\n"],"mappings":";;;;;;AAAe,SAASA,yBAAyBA,CAAEC,KAAK,EAAG;EAC1D,OAAOC,MAAM,CAACC,IAAI,CAAEF,KAAM,CAAC,CAACG,MAAM,CAAE,CAAEC,gBAAgB,EAAEC,KAAK,KAAM;IAClE,MAAMC,SAAS,GAAGN,KAAK,CAAEK,KAAK,CAAE;;IAEhC;IACA,IAAKC,SAAS,EAAEC,iBAAiB,EAAG;MACnC,MAAMC,gBAAgB,GAAG;QAAE,GAAGF;MAAU,CAAC;MACzC,OAAOE,gBAAgB,CAACD,iBAAiB;MACzCC,gBAAgB,CAACC,0BAA0B,GAAG,IAAI;MAClDL,gBAAgB,CAAEC,KAAK,CAAE,GAAGG,gBAAgB;MAC5C,OAAOJ,gBAAgB;IACxB;IAEA,OAAOA,gBAAgB;EACxB,CAAC,EAAEJ,KAAM,CAAC;AACX"}
|
@@ -1,17 +1,15 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4
|
-
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
6
5
|
value: true
|
7
6
|
});
|
8
7
|
exports.default = convertPreferencesPackageData;
|
9
|
-
|
10
8
|
var _convertComplementaryAreas = _interopRequireDefault(require("./convert-complementary-areas"));
|
11
|
-
|
12
9
|
/**
|
13
10
|
* Internal dependencies
|
14
11
|
*/
|
12
|
+
|
15
13
|
function convertPreferencesPackageData(data) {
|
16
14
|
return (0, _convertComplementaryAreas.default)(data);
|
17
15
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["@wordpress/preferences-persistence/src/migrations/preferences-package-data/index.js"],"
|
1
|
+
{"version":3,"names":["_convertComplementaryAreas","_interopRequireDefault","require","convertPreferencesPackageData","data","convertComplementaryAreas"],"sources":["@wordpress/preferences-persistence/src/migrations/preferences-package-data/index.js"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport convertComplementaryAreas from './convert-complementary-areas';\n\nexport default function convertPreferencesPackageData( data ) {\n\treturn convertComplementaryAreas( data );\n}\n"],"mappings":";;;;;;;AAGA,IAAAA,0BAAA,GAAAC,sBAAA,CAAAC,OAAA;AAHA;AACA;AACA;;AAGe,SAASC,6BAA6BA,CAAEC,IAAI,EAAG;EAC7D,OAAO,IAAAC,kCAAyB,EAAED,IAAK,CAAC;AACzC"}
|
@@ -35,20 +35,19 @@ export default function debounceAsync(func, delayMS) {
|
|
35
35
|
});
|
36
36
|
});
|
37
37
|
}
|
38
|
-
|
39
38
|
if (activePromise) {
|
40
39
|
// Let any active promises finish before queuing the next request.
|
41
40
|
await activePromise;
|
42
|
-
}
|
43
|
-
// been queued but not been made.
|
44
|
-
|
41
|
+
}
|
45
42
|
|
43
|
+
// Clear any active timeouts, abandoning any requests that have
|
44
|
+
// been queued but not been made.
|
46
45
|
if (timeoutId) {
|
47
46
|
clearTimeout(timeoutId);
|
48
47
|
timeoutId = null;
|
49
|
-
}
|
50
|
-
|
48
|
+
}
|
51
49
|
|
50
|
+
// Trigger any trailing edge calls to the function.
|
52
51
|
return new Promise((resolve, reject) => {
|
53
52
|
// Schedule the next request but with a delay.
|
54
53
|
timeoutId = setTimeout(() => {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"names":["debounceAsync","func","delayMS","timeoutId","activePromise","debounced","args","Promise","resolve","reject","then","thenArgs","catch","error","finally","clearTimeout","setTimeout"],"sources":["@wordpress/preferences-persistence/src/create/debounce-async.js"],"sourcesContent":["/**\n * Performs a leading edge debounce of async functions.\n *\n * If three functions are throttled at the same time:\n * - The first happens immediately.\n * - The second is never called.\n * - The third happens `delayMS` milliseconds after the first has resolved.\n *\n * This is distinct from `{ debounce } from @wordpress/compose` in that it\n * waits for promise resolution.\n *\n * @param {Function} func A function that returns a promise.\n * @param {number} delayMS A delay in milliseconds.\n *\n * @return {Function} A function that debounce whatever function is passed\n * to it.\n */\nexport default function debounceAsync( func, delayMS ) {\n\tlet timeoutId;\n\tlet activePromise;\n\n\treturn async function debounced( ...args ) {\n\t\t// This is a leading edge debounce. If there's no promise or timeout\n\t\t// in progress, call the debounced function immediately.\n\t\tif ( ! activePromise && ! timeoutId ) {\n\t\t\treturn new Promise( ( resolve, reject ) => {\n\t\t\t\t// Keep a reference to the promise.\n\t\t\t\tactivePromise = func( ...args )\n\t\t\t\t\t.then( ( ...thenArgs ) => {\n\t\t\t\t\t\tresolve( ...thenArgs );\n\t\t\t\t\t} )\n\t\t\t\t\t.catch( ( error ) => {\n\t\t\t\t\t\treject( error );\n\t\t\t\t\t} )\n\t\t\t\t\t.finally( () => {\n\t\t\t\t\t\t// As soon this promise is complete, clear the way for the\n\t\t\t\t\t\t// next one to happen immediately.\n\t\t\t\t\t\tactivePromise = null;\n\t\t\t\t\t} );\n\t\t\t} );\n\t\t}\n\n\t\tif ( activePromise ) {\n\t\t\t// Let any active promises finish before queuing the next request.\n\t\t\tawait activePromise;\n\t\t}\n\n\t\t// Clear any active timeouts, abandoning any requests that have\n\t\t// been queued but not been made.\n\t\tif ( timeoutId ) {\n\t\t\tclearTimeout( timeoutId );\n\t\t\ttimeoutId = null;\n\t\t}\n\n\t\t// Trigger any trailing edge calls to the function.\n\t\treturn new Promise( ( resolve, reject ) => {\n\t\t\t// Schedule the next request but with a delay.\n\t\t\ttimeoutId = setTimeout( () => {\n\t\t\t\tactivePromise = func( ...args )\n\t\t\t\t\t.then( ( ...thenArgs ) => {\n\t\t\t\t\t\tresolve( ...thenArgs );\n\t\t\t\t\t} )\n\t\t\t\t\t.catch( ( error ) => {\n\t\t\t\t\t\treject( error );\n\t\t\t\t\t} )\n\t\t\t\t\t.finally( () => {\n\t\t\t\t\t\t// As soon this promise is complete, clear the way for the\n\t\t\t\t\t\t// next one to happen immediately.\n\t\t\t\t\t\tactivePromise = null;\n\t\t\t\t\t\ttimeoutId = null;\n\t\t\t\t\t} );\n\t\t\t}, delayMS );\n\t\t} );\n\t};\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASA,aAAaA,CAAEC,IAAI,EAAEC,OAAO,EAAG;EACtD,IAAIC,SAAS;EACb,IAAIC,aAAa;EAEjB,OAAO,eAAeC,SAASA,CAAE,GAAGC,IAAI,EAAG;IAC1C;IACA;IACA,IAAK,CAAEF,aAAa,IAAI,CAAED,SAAS,EAAG;MACrC,OAAO,IAAII,OAAO,CAAE,CAAEC,OAAO,EAAEC,MAAM,KAAM;QAC1C;QACAL,aAAa,GAAGH,IAAI,CAAE,GAAGK,IAAK,CAAC,CAC7BI,IAAI,CAAE,CAAE,GAAGC,QAAQ,KAAM;UACzBH,OAAO,CAAE,GAAGG,QAAS,CAAC;QACvB,CAAE,CAAC,CACFC,KAAK,CAAIC,KAAK,IAAM;UACpBJ,MAAM,CAAEI,KAAM,CAAC;QAChB,CAAE,CAAC,CACFC,OAAO,CAAE,MAAM;UACf;UACA;UACAV,aAAa,GAAG,IAAI;QACrB,CAAE,CAAC;MACL,CAAE,CAAC;IACJ;IAEA,IAAKA,aAAa,EAAG;MACpB;MACA,MAAMA,aAAa;IACpB;;IAEA;IACA;IACA,IAAKD,SAAS,EAAG;MAChBY,YAAY,CAAEZ,SAAU,CAAC;MACzBA,SAAS,GAAG,IAAI;IACjB;;IAEA;IACA,OAAO,IAAII,OAAO,CAAE,CAAEC,OAAO,EAAEC,MAAM,KAAM;MAC1C;MACAN,SAAS,GAAGa,UAAU,CAAE,MAAM;QAC7BZ,aAAa,GAAGH,IAAI,CAAE,GAAGK,IAAK,CAAC,CAC7BI,IAAI,CAAE,CAAE,GAAGC,QAAQ,KAAM;UACzBH,OAAO,CAAE,GAAGG,QAAS,CAAC;QACvB,CAAE,CAAC,CACFC,KAAK,CAAIC,KAAK,IAAM;UACpBJ,MAAM,CAAEI,KAAM,CAAC;QAChB,CAAE,CAAC,CACFC,OAAO,CAAE,MAAM;UACf;UACA;UACAV,aAAa,GAAG,IAAI;UACpBD,SAAS,GAAG,IAAI;QACjB,CAAE,CAAC;MACL,CAAC,EAAED,OAAQ,CAAC;IACb,CAAE,CAAC;EACJ,CAAC;AACF"}
|
@@ -2,13 +2,14 @@
|
|
2
2
|
* WordPress dependencies
|
3
3
|
*/
|
4
4
|
import apiFetch from '@wordpress/api-fetch';
|
5
|
+
|
5
6
|
/**
|
6
7
|
* Internal dependencies
|
7
8
|
*/
|
8
|
-
|
9
9
|
import debounceAsync from './debounce-async';
|
10
10
|
const EMPTY_OBJECT = {};
|
11
11
|
const localStorage = window.localStorage;
|
12
|
+
|
12
13
|
/**
|
13
14
|
* Creates a persistence layer that stores data in WordPress user meta via the
|
14
15
|
* REST API.
|
@@ -26,7 +27,6 @@ const localStorage = window.localStorage;
|
|
26
27
|
*
|
27
28
|
* @return {Object} A persistence layer for WordPress user meta.
|
28
29
|
*/
|
29
|
-
|
30
30
|
export default function create({
|
31
31
|
preloadedData,
|
32
32
|
localStorageRestoreKey = 'WP_PREFERENCES_RESTORE_DATA',
|
@@ -34,23 +34,23 @@ export default function create({
|
|
34
34
|
} = {}) {
|
35
35
|
let cache = preloadedData;
|
36
36
|
const debouncedApiFetch = debounceAsync(apiFetch, requestDebounceMS);
|
37
|
-
|
38
37
|
async function get() {
|
39
38
|
if (cache) {
|
40
39
|
return cache;
|
41
40
|
}
|
42
|
-
|
43
41
|
const user = await apiFetch({
|
44
42
|
path: '/wp/v2/users/me?context=edit'
|
45
43
|
});
|
46
44
|
const serverData = user?.meta?.persisted_preferences;
|
47
|
-
const localData = JSON.parse(localStorage.getItem(localStorageRestoreKey));
|
48
|
-
// into a conveniently comparable zero.
|
45
|
+
const localData = JSON.parse(localStorage.getItem(localStorageRestoreKey));
|
49
46
|
|
47
|
+
// Date parse returns NaN for invalid input. Coerce anything invalid
|
48
|
+
// into a conveniently comparable zero.
|
50
49
|
const serverTimestamp = Date.parse(serverData?._modified) || 0;
|
51
|
-
const localTimestamp = Date.parse(localData?._modified) || 0;
|
52
|
-
// Otherwise fallback to localStorage data.
|
50
|
+
const localTimestamp = Date.parse(localData?._modified) || 0;
|
53
51
|
|
52
|
+
// Prefer server data if it exists and is more recent.
|
53
|
+
// Otherwise fallback to localStorage data.
|
54
54
|
if (serverData && serverTimestamp >= localTimestamp) {
|
55
55
|
cache = serverData;
|
56
56
|
} else if (localData) {
|
@@ -58,24 +58,25 @@ export default function create({
|
|
58
58
|
} else {
|
59
59
|
cache = EMPTY_OBJECT;
|
60
60
|
}
|
61
|
-
|
62
61
|
return cache;
|
63
62
|
}
|
64
|
-
|
65
63
|
function set(newData) {
|
66
|
-
const dataWithTimestamp = {
|
64
|
+
const dataWithTimestamp = {
|
65
|
+
...newData,
|
67
66
|
_modified: new Date().toISOString()
|
68
67
|
};
|
69
|
-
cache = dataWithTimestamp;
|
68
|
+
cache = dataWithTimestamp;
|
69
|
+
|
70
|
+
// Store data in local storage as a fallback. If for some reason the
|
70
71
|
// api request does not complete or becomes unavailable, this data
|
71
72
|
// can be used to restore preferences.
|
73
|
+
localStorage.setItem(localStorageRestoreKey, JSON.stringify(dataWithTimestamp));
|
72
74
|
|
73
|
-
|
75
|
+
// The user meta endpoint seems susceptible to errors when consecutive
|
74
76
|
// requests are made in quick succession. Ensure there's a gap between
|
75
77
|
// any consecutive requests.
|
76
78
|
//
|
77
79
|
// Catch and do nothing with errors from the REST API.
|
78
|
-
|
79
80
|
debouncedApiFetch({
|
80
81
|
path: '/wp/v2/users/me',
|
81
82
|
method: 'PUT',
|
@@ -92,7 +93,6 @@ export default function create({
|
|
92
93
|
}
|
93
94
|
}).catch(() => {});
|
94
95
|
}
|
95
|
-
|
96
96
|
return {
|
97
97
|
get,
|
98
98
|
set
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"names":["apiFetch","debounceAsync","EMPTY_OBJECT","localStorage","window","create","preloadedData","localStorageRestoreKey","requestDebounceMS","cache","debouncedApiFetch","get","user","path","serverData","meta","persisted_preferences","localData","JSON","parse","getItem","serverTimestamp","Date","_modified","localTimestamp","set","newData","dataWithTimestamp","toISOString","setItem","stringify","method","keepalive","data","catch"],"sources":["@wordpress/preferences-persistence/src/create/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Internal dependencies\n */\nimport debounceAsync from './debounce-async';\n\nconst EMPTY_OBJECT = {};\nconst localStorage = window.localStorage;\n\n/**\n * Creates a persistence layer that stores data in WordPress user meta via the\n * REST API.\n *\n * @param {Object} options\n * @param {?Object} options.preloadedData Any persisted preferences data that should be preloaded.\n * When set, the persistence layer will avoid fetching data\n * from the REST API.\n * @param {?string} options.localStorageRestoreKey The key to use for restoring the localStorage backup, used\n * when the persistence layer calls `localStorage.getItem` or\n * `localStorage.setItem`.\n * @param {?number} options.requestDebounceMS Debounce requests to the API so that they only occur at\n * minimum every `requestDebounceMS` milliseconds, and don't\n * swamp the server. Defaults to 2500ms.\n *\n * @return {Object} A persistence layer for WordPress user meta.\n */\nexport default function create( {\n\tpreloadedData,\n\tlocalStorageRestoreKey = 'WP_PREFERENCES_RESTORE_DATA',\n\trequestDebounceMS = 2500,\n} = {} ) {\n\tlet cache = preloadedData;\n\tconst debouncedApiFetch = debounceAsync( apiFetch, requestDebounceMS );\n\n\tasync function get() {\n\t\tif ( cache ) {\n\t\t\treturn cache;\n\t\t}\n\n\t\tconst user = await apiFetch( {\n\t\t\tpath: '/wp/v2/users/me?context=edit',\n\t\t} );\n\n\t\tconst serverData = user?.meta?.persisted_preferences;\n\t\tconst localData = JSON.parse(\n\t\t\tlocalStorage.getItem( localStorageRestoreKey )\n\t\t);\n\n\t\t// Date parse returns NaN for invalid input. Coerce anything invalid\n\t\t// into a conveniently comparable zero.\n\t\tconst serverTimestamp = Date.parse( serverData?._modified ) || 0;\n\t\tconst localTimestamp = Date.parse( localData?._modified ) || 0;\n\n\t\t// Prefer server data if it exists and is more recent.\n\t\t// Otherwise fallback to localStorage data.\n\t\tif ( serverData && serverTimestamp >= localTimestamp ) {\n\t\t\tcache = serverData;\n\t\t} else if ( localData ) {\n\t\t\tcache = localData;\n\t\t} else {\n\t\t\tcache = EMPTY_OBJECT;\n\t\t}\n\n\t\treturn cache;\n\t}\n\n\tfunction set( newData ) {\n\t\tconst dataWithTimestamp = {\n\t\t\t...newData,\n\t\t\t_modified: new Date().toISOString(),\n\t\t};\n\t\tcache = dataWithTimestamp;\n\n\t\t// Store data in local storage as a fallback. If for some reason the\n\t\t// api request does not complete or becomes unavailable, this data\n\t\t// can be used to restore preferences.\n\t\tlocalStorage.setItem(\n\t\t\tlocalStorageRestoreKey,\n\t\t\tJSON.stringify( dataWithTimestamp )\n\t\t);\n\n\t\t// The user meta endpoint seems susceptible to errors when consecutive\n\t\t// requests are made in quick succession. Ensure there's a gap between\n\t\t// any consecutive requests.\n\t\t//\n\t\t// Catch and do nothing with errors from the REST API.\n\t\tdebouncedApiFetch( {\n\t\t\tpath: '/wp/v2/users/me',\n\t\t\tmethod: 'PUT',\n\t\t\t// `keepalive` will still send the request in the background,\n\t\t\t// even when a browser unload event might interrupt it.\n\t\t\t// This should hopefully make things more resilient.\n\t\t\t// This does have a size limit of 64kb, but the data is usually\n\t\t\t// much less.\n\t\t\tkeepalive: true,\n\t\t\tdata: {\n\t\t\t\tmeta: {\n\t\t\t\t\tpersisted_preferences: dataWithTimestamp,\n\t\t\t\t},\n\t\t\t},\n\t\t} ).catch( () => {} );\n\t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,CAAC,MAAM;MACNR,KAAK,GAAGP,YAAY;IACrB;IAEA,OAAOO,KAAK;EACb;EAEA,SAASgB,GAAGA,CAAEC,OAAO,EAAG;IACvB,MAAMC,iBAAiB,GAAG;MACzB,GAAGD,OAAO;MACVH,SAAS,EAAE,IAAID,IAAI,CAAC,CAAC,CAACM,WAAW,CAAC;IACnC,CAAC;IACDnB,KAAK,GAAGkB,iBAAiB;;IAEzB;IACA;IACA;IACAxB,YAAY,CAAC0B,OAAO,CACnBtB,sBAAsB,EACtBW,IAAI,CAACY,SAAS,CAAEH,iBAAkB,CACnC,CAAC;;IAED;IACA;IACA;IACA;IACA;IACAjB,iBAAiB,CAAE;MAClBG,IAAI,EAAE,iBAAiB;MACvBkB,MAAM,EAAE,KAAK;MACb;MACA;MACA;MACA;MACA;MACAC,SAAS,EAAE,IAAI;MACfC,IAAI,EAAE;QACLlB,IAAI,EAAE;UACLC,qBAAqB,EAAEW;QACxB;MACD;IACD,CAAE,CAAC,CAACO,KAAK,CAAE,MAAM,CAAC,CAAE,CAAC;EACtB;EAEA,OAAO;IACNvB,GAAG;IACHc;EACD,CAAC;AACF"}
|
package/build-module/index.js
CHANGED
@@ -5,6 +5,7 @@ import create from './create';
|
|
5
5
|
import convertLegacyLocalStorageData from './migrations/legacy-local-storage-data';
|
6
6
|
import convertPreferencesPackageData from './migrations/preferences-package-data';
|
7
7
|
export { create };
|
8
|
+
|
8
9
|
/**
|
9
10
|
* Creates the persistence layer with preloaded data.
|
10
11
|
*
|
@@ -19,16 +20,15 @@ export { create };
|
|
19
20
|
*
|
20
21
|
* @return {Object} The persistence layer initialized with the preloaded data.
|
21
22
|
*/
|
22
|
-
|
23
23
|
export function __unstableCreatePersistenceLayer(serverData, userId) {
|
24
24
|
const localStorageRestoreKey = `WP_PREFERENCES_USER_${userId}`;
|
25
|
-
const localData = JSON.parse(window.localStorage.getItem(localStorageRestoreKey));
|
26
|
-
// into a conveniently comparable zero.
|
25
|
+
const localData = JSON.parse(window.localStorage.getItem(localStorageRestoreKey));
|
27
26
|
|
27
|
+
// Date parse returns NaN for invalid input. Coerce anything invalid
|
28
|
+
// into a conveniently comparable zero.
|
28
29
|
const serverModified = Date.parse(serverData && serverData._modified) || 0;
|
29
30
|
const localModified = Date.parse(localData && localData._modified) || 0;
|
30
31
|
let preloadedData;
|
31
|
-
|
32
32
|
if (serverData && serverModified >= localModified) {
|
33
33
|
preloadedData = convertPreferencesPackageData(serverData);
|
34
34
|
} else if (localData) {
|
@@ -37,7 +37,6 @@ export function __unstableCreatePersistenceLayer(serverData, userId) {
|
|
37
37
|
// Check if there is data in the legacy format from the old persistence system.
|
38
38
|
preloadedData = convertLegacyLocalStorageData(userId);
|
39
39
|
}
|
40
|
-
|
41
40
|
return create({
|
42
41
|
preloadedData,
|
43
42
|
localStorageRestoreKey
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"names":["create","convertLegacyLocalStorageData","convertPreferencesPackageData","__unstableCreatePersistenceLayer","serverData","userId","localStorageRestoreKey","localData","JSON","parse","window","localStorage","getItem","serverModified","Date","_modified","localModified","preloadedData"],"sources":["@wordpress/preferences-persistence/src/index.js"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport create from './create';\nimport convertLegacyLocalStorageData from './migrations/legacy-local-storage-data';\nimport convertPreferencesPackageData from './migrations/preferences-package-data';\n\nexport { create };\n\n/**\n * Creates the persistence layer with preloaded data.\n *\n * It prioritizes any data from the server, but falls back first to localStorage\n * restore data, and then to any legacy data.\n *\n * This function is used internally by WordPress in an inline script, so\n * prefixed with `__unstable`.\n *\n * @param {Object} serverData Preferences data preloaded from the server.\n * @param {string} userId The user id.\n *\n * @return {Object} The persistence layer initialized with the preloaded data.\n */\nexport function __unstableCreatePersistenceLayer( serverData, userId ) {\n\tconst localStorageRestoreKey = `WP_PREFERENCES_USER_${ userId }`;\n\tconst localData = JSON.parse(\n\t\twindow.localStorage.getItem( localStorageRestoreKey )\n\t);\n\n\t// Date parse returns NaN for invalid input. Coerce anything invalid\n\t// into a conveniently comparable zero.\n\tconst serverModified =\n\t\tDate.parse( serverData && serverData._modified ) || 0;\n\tconst localModified = Date.parse( localData && localData._modified ) || 0;\n\n\tlet preloadedData;\n\tif ( serverData && serverModified >= localModified ) {\n\t\tpreloadedData = convertPreferencesPackageData( serverData );\n\t} else if ( localData ) {\n\t\tpreloadedData = convertPreferencesPackageData( localData );\n\t} else {\n\t\t// Check if there is data in the legacy format from the old persistence system.\n\t\tpreloadedData = convertLegacyLocalStorageData( userId );\n\t}\n\n\treturn create( {\n\t\tpreloadedData,\n\t\tlocalStorageRestoreKey,\n\t} );\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,MAAM,MAAM,UAAU;AAC7B,OAAOC,6BAA6B,MAAM,wCAAwC;AAClF,OAAOC,6BAA6B,MAAM,uCAAuC;AAEjF,SAASF,MAAM;;AAEf;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASG,gCAAgCA,CAAEC,UAAU,EAAEC,MAAM,EAAG;EACtE,MAAMC,sBAAsB,GAAI,uBAAuBD,MAAQ,EAAC;EAChE,MAAME,SAAS,GAAGC,IAAI,CAACC,KAAK,CAC3BC,MAAM,CAACC,YAAY,CAACC,OAAO,CAAEN,sBAAuB,CACrD,CAAC;;EAED;EACA;EACA,MAAMO,cAAc,GACnBC,IAAI,CAACL,KAAK,CAAEL,UAAU,IAAIA,UAAU,CAACW,SAAU,CAAC,IAAI,CAAC;EACtD,MAAMC,aAAa,GAAGF,IAAI,CAACL,KAAK,CAAEF,SAAS,IAAIA,SAAS,CAACQ,SAAU,CAAC,IAAI,CAAC;EAEzE,IAAIE,aAAa;EACjB,IAAKb,UAAU,IAAIS,cAAc,IAAIG,aAAa,EAAG;IACpDC,aAAa,GAAGf,6BAA6B,CAAEE,UAAW,CAAC;EAC5D,CAAC,MAAM,IAAKG,SAAS,EAAG;IACvBU,aAAa,GAAGf,6BAA6B,CAAEK,SAAU,CAAC;EAC3D,CAAC,MAAM;IACN;IACAU,aAAa,GAAGhB,6BAA6B,CAAEI,MAAO,CAAC;EACxD;EAEA,OAAOL,MAAM,CAAE;IACdiB,aAAa;IACbX;EACD,CAAE,CAAC;AACJ"}
|
@@ -31,19 +31,15 @@
|
|
31
31
|
*/
|
32
32
|
export default function convertEditPostPanels(preferences) {
|
33
33
|
var _preferences$panels;
|
34
|
-
|
35
34
|
const panels = (_preferences$panels = preferences?.panels) !== null && _preferences$panels !== void 0 ? _preferences$panels : {};
|
36
35
|
return Object.keys(panels).reduce((convertedData, panelName) => {
|
37
36
|
const panel = panels[panelName];
|
38
|
-
|
39
37
|
if (panel?.enabled === false) {
|
40
38
|
convertedData.inactivePanels.push(panelName);
|
41
39
|
}
|
42
|
-
|
43
40
|
if (panel?.opened === true) {
|
44
41
|
convertedData.openPanels.push(panelName);
|
45
42
|
}
|
46
|
-
|
47
43
|
return convertedData;
|
48
44
|
}, {
|
49
45
|
inactivePanels: [],
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"names":["convertEditPostPanels","preferences","_preferences$panels","panels","Object","keys","reduce","convertedData","panelName","panel","enabled","inactivePanels","push","opened","openPanels"],"sources":["@wordpress/preferences-persistence/src/migrations/legacy-local-storage-data/convert-edit-post-panels.js"],"sourcesContent":["/**\n * Convert the post editor's panels state from:\n * ```\n * {\n * panels: {\n * tags: {\n * enabled: true,\n * opened: true,\n * },\n * permalinks: {\n * enabled: false,\n * opened: false,\n * },\n * },\n * }\n * ```\n *\n * to a new, more concise data structure:\n * {\n * inactivePanels: [\n * 'permalinks',\n * ],\n * openPanels: [\n * 'tags',\n * ],\n * }\n *\n * @param {Object} preferences A preferences object.\n *\n * @return {Object} The converted data.\n */\nexport default function convertEditPostPanels( preferences ) {\n\tconst panels = preferences?.panels ?? {};\n\treturn Object.keys( panels ).reduce(\n\t\t( convertedData, panelName ) => {\n\t\t\tconst panel = panels[ panelName ];\n\n\t\t\tif ( panel?.enabled === false ) {\n\t\t\t\tconvertedData.inactivePanels.push( panelName );\n\t\t\t}\n\n\t\t\tif ( panel?.opened === true ) {\n\t\t\t\tconvertedData.openPanels.push( panelName );\n\t\t\t}\n\n\t\t\treturn convertedData;\n\t\t},\n\t\t{ inactivePanels: [], openPanels: [] }\n\t);\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASA,qBAAqBA,CAAEC,WAAW,EAAG;EAAA,IAAAC,mBAAA;EAC5D,MAAMC,MAAM,IAAAD,mBAAA,GAAGD,WAAW,EAAEE,MAAM,cAAAD,mBAAA,cAAAA,mBAAA,GAAI,CAAC,CAAC;EACxC,OAAOE,MAAM,CAACC,IAAI,CAAEF,MAAO,CAAC,CAACG,MAAM,CAClC,CAAEC,aAAa,EAAEC,SAAS,KAAM;IAC/B,MAAMC,KAAK,GAAGN,MAAM,CAAEK,SAAS,CAAE;IAEjC,IAAKC,KAAK,EAAEC,OAAO,KAAK,KAAK,EAAG;MAC/BH,aAAa,CAACI,cAAc,CAACC,IAAI,CAAEJ,SAAU,CAAC;IAC/C;IAEA,IAAKC,KAAK,EAAEI,MAAM,KAAK,IAAI,EAAG;MAC7BN,aAAa,CAACO,UAAU,CAACF,IAAI,CAAEJ,SAAU,CAAC;IAC3C;IAEA,OAAOD,aAAa;EACrB,CAAC,EACD;IAAEI,cAAc,EAAE,EAAE;IAAEG,UAAU,EAAE;EAAG,CACtC,CAAC;AACF"}
|