@wordpress/preferences-persistence 1.0.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.
Files changed (58) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE.md +788 -0
  3. package/README.md +59 -0
  4. package/build/create/debounce-async.js +80 -0
  5. package/build/create/debounce-async.js.map +1 -0
  6. package/build/create/index.js +115 -0
  7. package/build/create/index.js.map +1 -0
  8. package/build/index.js +61 -0
  9. package/build/index.js.map +1 -0
  10. package/build/migrations/legacy-local-storage-data/convert-edit-post-panels.js +60 -0
  11. package/build/migrations/legacy-local-storage-data/convert-edit-post-panels.js.map +1 -0
  12. package/build/migrations/legacy-local-storage-data/index.js +111 -0
  13. package/build/migrations/legacy-local-storage-data/index.js.map +1 -0
  14. package/build/migrations/legacy-local-storage-data/move-feature-preferences.js +135 -0
  15. package/build/migrations/legacy-local-storage-data/move-feature-preferences.js.map +1 -0
  16. package/build/migrations/legacy-local-storage-data/move-individual-preference.js +91 -0
  17. package/build/migrations/legacy-local-storage-data/move-individual-preference.js.map +1 -0
  18. package/build/migrations/legacy-local-storage-data/move-interface-enable-items.js +114 -0
  19. package/build/migrations/legacy-local-storage-data/move-interface-enable-items.js.map +1 -0
  20. package/build/migrations/legacy-local-storage-data/move-third-party-feature-preferences.js +99 -0
  21. package/build/migrations/legacy-local-storage-data/move-third-party-feature-preferences.js.map +1 -0
  22. package/build-module/create/debounce-async.js +73 -0
  23. package/build-module/create/debounce-async.js.map +1 -0
  24. package/build-module/create/index.js +104 -0
  25. package/build-module/create/index.js.map +1 -0
  26. package/build-module/index.js +45 -0
  27. package/build-module/index.js.map +1 -0
  28. package/build-module/migrations/legacy-local-storage-data/convert-edit-post-panels.js +53 -0
  29. package/build-module/migrations/legacy-local-storage-data/convert-edit-post-panels.js.map +1 -0
  30. package/build-module/migrations/legacy-local-storage-data/index.js +95 -0
  31. package/build-module/migrations/legacy-local-storage-data/index.js.map +1 -0
  32. package/build-module/migrations/legacy-local-storage-data/move-feature-preferences.js +128 -0
  33. package/build-module/migrations/legacy-local-storage-data/move-feature-preferences.js.map +1 -0
  34. package/build-module/migrations/legacy-local-storage-data/move-individual-preference.js +84 -0
  35. package/build-module/migrations/legacy-local-storage-data/move-individual-preference.js.map +1 -0
  36. package/build-module/migrations/legacy-local-storage-data/move-interface-enable-items.js +107 -0
  37. package/build-module/migrations/legacy-local-storage-data/move-interface-enable-items.js.map +1 -0
  38. package/build-module/migrations/legacy-local-storage-data/move-third-party-feature-preferences.js +92 -0
  39. package/build-module/migrations/legacy-local-storage-data/move-third-party-feature-preferences.js.map +1 -0
  40. package/package.json +37 -0
  41. package/src/create/debounce-async.js +75 -0
  42. package/src/create/index.js +112 -0
  43. package/src/create/test/debounce-async.js +129 -0
  44. package/src/create/test/index.js +178 -0
  45. package/src/index.js +49 -0
  46. package/src/migrations/legacy-local-storage-data/README.md +42 -0
  47. package/src/migrations/legacy-local-storage-data/convert-edit-post-panels.js +50 -0
  48. package/src/migrations/legacy-local-storage-data/index.js +102 -0
  49. package/src/migrations/legacy-local-storage-data/move-feature-preferences.js +135 -0
  50. package/src/migrations/legacy-local-storage-data/move-individual-preference.js +90 -0
  51. package/src/migrations/legacy-local-storage-data/move-interface-enable-items.js +120 -0
  52. package/src/migrations/legacy-local-storage-data/move-third-party-feature-preferences.js +98 -0
  53. package/src/migrations/legacy-local-storage-data/test/convert-edit-post-panels.js +47 -0
  54. package/src/migrations/legacy-local-storage-data/test/index.js +229 -0
  55. package/src/migrations/legacy-local-storage-data/test/move-feature-preferences.js +260 -0
  56. package/src/migrations/legacy-local-storage-data/test/move-individual-preference.js +188 -0
  57. package/src/migrations/legacy-local-storage-data/test/move-interface-enable-items.js +118 -0
  58. package/src/migrations/legacy-local-storage-data/test/move-third-party-feature-preferences.js +107 -0
@@ -0,0 +1,104 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import apiFetch from '@wordpress/api-fetch';
5
+ /**
6
+ * Internal dependencies
7
+ */
8
+
9
+ import debounceAsync from './debounce-async';
10
+ const EMPTY_OBJECT = {};
11
+ const localStorage = window.localStorage;
12
+ /**
13
+ * Creates a persistence layer that stores data in WordPress user meta via the
14
+ * REST API.
15
+ *
16
+ * @param {Object} options
17
+ * @param {?Object} options.preloadedData Any persisted preferences data that should be preloaded.
18
+ * When set, the persistence layer will avoid fetching data
19
+ * from the REST API.
20
+ * @param {?string} options.localStorageRestoreKey The key to use for restoring the localStorage backup, used
21
+ * when the persistence layer calls `localStorage.getItem` or
22
+ * `localStorage.setItem`.
23
+ * @param {?number} options.requestDebounceMS Debounce requests to the API so that they only occur at
24
+ * minimum every `requestDebounceMS` milliseconds, and don't
25
+ * swamp the server. Defaults to 2500ms.
26
+ *
27
+ * @return {Object} A persistence layer for WordPress user meta.
28
+ */
29
+
30
+ export default function create() {
31
+ let {
32
+ preloadedData,
33
+ localStorageRestoreKey = 'WP_PREFERENCES_RESTORE_DATA',
34
+ requestDebounceMS = 2500
35
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
36
+ let cache = preloadedData;
37
+ const debouncedApiFetch = debounceAsync(apiFetch, requestDebounceMS);
38
+
39
+ async function get() {
40
+ var _user$meta;
41
+
42
+ if (cache) {
43
+ return cache;
44
+ }
45
+
46
+ const user = await apiFetch({
47
+ path: '/wp/v2/users/me?context=edit'
48
+ });
49
+ const serverData = user === null || user === void 0 ? void 0 : (_user$meta = user.meta) === null || _user$meta === void 0 ? void 0 : _user$meta.persisted_preferences;
50
+ const localData = JSON.parse(localStorage.getItem(localStorageRestoreKey)); // Date parse returns NaN for invalid input. Coerce anything invalid
51
+ // into a conveniently comparable zero.
52
+
53
+ const serverTimestamp = Date.parse(serverData === null || serverData === void 0 ? void 0 : serverData._modified) || 0;
54
+ const localTimestamp = Date.parse(localData === null || localData === void 0 ? void 0 : localData._modified) || 0; // Prefer server data if it exists and is more recent.
55
+ // Otherwise fallback to localStorage data.
56
+
57
+ if (serverData && serverTimestamp >= localTimestamp) {
58
+ cache = serverData;
59
+ } else if (localData) {
60
+ cache = localData;
61
+ } else {
62
+ cache = EMPTY_OBJECT;
63
+ }
64
+
65
+ return cache;
66
+ }
67
+
68
+ function set(newData) {
69
+ const dataWithTimestamp = { ...newData,
70
+ _modified: new Date().toISOString()
71
+ };
72
+ cache = dataWithTimestamp; // Store data in local storage as a fallback. If for some reason the
73
+ // api request does not complete or becomes unavailable, this data
74
+ // can be used to restore preferences.
75
+
76
+ localStorage.setItem(localStorageRestoreKey, JSON.stringify(dataWithTimestamp)); // The user meta endpoint seems susceptible to errors when consecutive
77
+ // requests are made in quick succession. Ensure there's a gap between
78
+ // any consecutive requests.
79
+ //
80
+ // Catch and do nothing with errors from the REST API.
81
+
82
+ debouncedApiFetch({
83
+ path: '/wp/v2/users/me',
84
+ method: 'PUT',
85
+ // `keepalive` will still send the request in the background,
86
+ // even when a browser unload event might interrupt it.
87
+ // This should hopefully make things more resilient.
88
+ // This does have a size limit of 64kb, but the data is usually
89
+ // much less.
90
+ keepalive: true,
91
+ data: {
92
+ meta: {
93
+ persisted_preferences: dataWithTimestamp
94
+ }
95
+ }
96
+ }).catch(() => {});
97
+ }
98
+
99
+ return {
100
+ get,
101
+ set
102
+ };
103
+ }
104
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences-persistence/src/create/index.js"],"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"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,QAAP,MAAqB,sBAArB;AAEA;AACA;AACA;;AACA,OAAOC,aAAP,MAA0B,kBAA1B;AAEA,MAAMC,YAAY,GAAG,EAArB;AACA,MAAMC,YAAY,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ACA;;AAED,WAAOO,KAAP;AACA;;AAED,WAASgB,GAAT,CAAcC,OAAd,EAAwB;AACvB,UAAMC,iBAAiB,GAAG,EACzB,GAAGD,OADsB;AAEzBH,MAAAA,SAAS,EAAE,IAAID,IAAJ,GAAWM,WAAX;AAFc,KAA1B;AAIAnB,IAAAA,KAAK,GAAGkB,iBAAR,CALuB,CAOvB;AACA;AACA;;AACAxB,IAAAA,YAAY,CAAC0B,OAAb,CACCtB,sBADD,EAECW,IAAI,CAACY,SAAL,CAAgBH,iBAAhB,CAFD,EAVuB,CAevB;AACA;AACA;AACA;AACA;;AACAjB,IAAAA,iBAAiB,CAAE;AAClBG,MAAAA,IAAI,EAAE,iBADY;AAElBkB,MAAAA,MAAM,EAAE,KAFU;AAGlB;AACA;AACA;AACA;AACA;AACAC,MAAAA,SAAS,EAAE,IARO;AASlBC,MAAAA,IAAI,EAAE;AACLlB,QAAAA,IAAI,EAAE;AACLC,UAAAA,qBAAqB,EAAEW;AADlB;AADD;AATY,KAAF,CAAjB,CAcIO,KAdJ,CAcW,MAAM,CAAE,CAdnB;AAeA;;AAED,SAAO;AACNvB,IAAAA,GADM;AAENc,IAAAA;AAFM,GAAP;AAIA","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}\n\n\treturn {\n\t\tget,\n\t\tset,\n\t};\n}\n"]}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import create from './create';
5
+ import convertLegacyLocalStorageData from './migrations/legacy-local-storage-data';
6
+ export { create };
7
+ /**
8
+ * Creates the persistence layer with preloaded data.
9
+ *
10
+ * It prioritizes any data from the server, but falls back first to localStorage
11
+ * restore data, and then to any legacy data.
12
+ *
13
+ * This function is used internally by WordPress in an inline script, so
14
+ * prefixed with `__unstable`.
15
+ *
16
+ * @param {Object} serverData Preferences data preloaded from the server.
17
+ * @param {string} userId The user id.
18
+ *
19
+ * @return {Object} The persistence layer initialized with the preloaded data.
20
+ */
21
+
22
+ export function __unstableCreatePersistenceLayer(serverData, userId) {
23
+ const localStorageRestoreKey = `WP_PREFERENCES_USER_${userId}`;
24
+ const localData = JSON.parse(window.localStorage.getItem(localStorageRestoreKey)); // Date parse returns NaN for invalid input. Coerce anything invalid
25
+ // into a conveniently comparable zero.
26
+
27
+ const serverModified = Date.parse(serverData && serverData._modified) || 0;
28
+ const localModified = Date.parse(localData && localData._modified) || 0;
29
+ let preloadedData;
30
+
31
+ if (serverData && serverModified >= localModified) {
32
+ preloadedData = serverData;
33
+ } else if (localData) {
34
+ preloadedData = localData;
35
+ } else {
36
+ // Check if there is data in the legacy format from the old persistence system.
37
+ preloadedData = convertLegacyLocalStorageData(userId);
38
+ }
39
+
40
+ return create({
41
+ preloadedData,
42
+ localStorageRestoreKey
43
+ });
44
+ }
45
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences-persistence/src/index.js"],"names":["create","convertLegacyLocalStorageData","__unstableCreatePersistenceLayer","serverData","userId","localStorageRestoreKey","localData","JSON","parse","window","localStorage","getItem","serverModified","Date","_modified","localModified","preloadedData"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,MAAP,MAAmB,UAAnB;AACA,OAAOC,6BAAP,MAA0C,wCAA1C;AAEA,SAASD,MAAT;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASE,gCAAT,CAA2CC,UAA3C,EAAuDC,MAAvD,EAAgE;AACtE,QAAMC,sBAAsB,GAAI,uBAAuBD,MAAQ,EAA/D;AACA,QAAME,SAAS,GAAGC,IAAI,CAACC,KAAL,CACjBC,MAAM,CAACC,YAAP,CAAoBC,OAApB,CAA6BN,sBAA7B,CADiB,CAAlB,CAFsE,CAMtE;AACA;;AACA,QAAMO,cAAc,GACnBC,IAAI,CAACL,KAAL,CAAYL,UAAU,IAAIA,UAAU,CAACW,SAArC,KAAoD,CADrD;AAEA,QAAMC,aAAa,GAAGF,IAAI,CAACL,KAAL,CAAYF,SAAS,IAAIA,SAAS,CAACQ,SAAnC,KAAkD,CAAxE;AAEA,MAAIE,aAAJ;;AACA,MAAKb,UAAU,IAAIS,cAAc,IAAIG,aAArC,EAAqD;AACpDC,IAAAA,aAAa,GAAGb,UAAhB;AACA,GAFD,MAEO,IAAKG,SAAL,EAAiB;AACvBU,IAAAA,aAAa,GAAGV,SAAhB;AACA,GAFM,MAEA;AACN;AACAU,IAAAA,aAAa,GAAGf,6BAA6B,CAAEG,MAAF,CAA7C;AACA;;AAED,SAAOJ,MAAM,CAAE;AACdgB,IAAAA,aADc;AAEdX,IAAAA;AAFc,GAAF,CAAb;AAIA","sourcesContent":["/**\n * Internal dependencies\n */\nimport create from './create';\nimport convertLegacyLocalStorageData from './migrations/legacy-local-storage-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 = serverData;\n\t} else if ( localData ) {\n\t\tpreloadedData = 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"]}
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Convert the post editor's panels state from:
3
+ * ```
4
+ * {
5
+ * panels: {
6
+ * tags: {
7
+ * enabled: true,
8
+ * opened: true,
9
+ * },
10
+ * permalinks: {
11
+ * enabled: false,
12
+ * opened: false,
13
+ * },
14
+ * },
15
+ * }
16
+ * ```
17
+ *
18
+ * to a new, more concise data structure:
19
+ * {
20
+ * inactivePanels: [
21
+ * 'permalinks',
22
+ * ],
23
+ * openPanels: [
24
+ * 'tags',
25
+ * ],
26
+ * }
27
+ *
28
+ * @param {Object} preferences A preferences object.
29
+ *
30
+ * @return {Object} The converted data.
31
+ */
32
+ export default function convertEditPostPanels(preferences) {
33
+ var _preferences$panels;
34
+
35
+ const panels = (_preferences$panels = preferences === null || preferences === void 0 ? void 0 : preferences.panels) !== null && _preferences$panels !== void 0 ? _preferences$panels : {};
36
+ return Object.keys(panels).reduce((convertedData, panelName) => {
37
+ const panel = panels[panelName];
38
+
39
+ if ((panel === null || panel === void 0 ? void 0 : panel.enabled) === false) {
40
+ convertedData.inactivePanels.push(panelName);
41
+ }
42
+
43
+ if ((panel === null || panel === void 0 ? void 0 : panel.opened) === true) {
44
+ convertedData.openPanels.push(panelName);
45
+ }
46
+
47
+ return convertedData;
48
+ }, {
49
+ inactivePanels: [],
50
+ openPanels: []
51
+ });
52
+ }
53
+ //# sourceMappingURL=convert-edit-post-panels.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences-persistence/src/migrations/legacy-local-storage-data/convert-edit-post-panels.js"],"names":["convertEditPostPanels","preferences","panels","Object","keys","reduce","convertedData","panelName","panel","enabled","inactivePanels","push","opened","openPanels"],"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,qBAAT,CAAgCC,WAAhC,EAA8C;AAAA;;AAC5D,QAAMC,MAAM,0BAAGD,WAAH,aAAGA,WAAH,uBAAGA,WAAW,CAAEC,MAAhB,qEAA0B,EAAtC;AACA,SAAOC,MAAM,CAACC,IAAP,CAAaF,MAAb,EAAsBG,MAAtB,CACN,CAAEC,aAAF,EAAiBC,SAAjB,KAAgC;AAC/B,UAAMC,KAAK,GAAGN,MAAM,CAAEK,SAAF,CAApB;;AAEA,QAAK,CAAAC,KAAK,SAAL,IAAAA,KAAK,WAAL,YAAAA,KAAK,CAAEC,OAAP,MAAmB,KAAxB,EAAgC;AAC/BH,MAAAA,aAAa,CAACI,cAAd,CAA6BC,IAA7B,CAAmCJ,SAAnC;AACA;;AAED,QAAK,CAAAC,KAAK,SAAL,IAAAA,KAAK,WAAL,YAAAA,KAAK,CAAEI,MAAP,MAAkB,IAAvB,EAA8B;AAC7BN,MAAAA,aAAa,CAACO,UAAd,CAAyBF,IAAzB,CAA+BJ,SAA/B;AACA;;AAED,WAAOD,aAAP;AACA,GAbK,EAcN;AAAEI,IAAAA,cAAc,EAAE,EAAlB;AAAsBG,IAAAA,UAAU,EAAE;AAAlC,GAdM,CAAP;AAgBA","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"]}
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import moveFeaturePreferences from './move-feature-preferences';
5
+ import moveThirdPartyFeaturePreferences from './move-third-party-feature-preferences';
6
+ import moveIndividualPreference from './move-individual-preference';
7
+ import moveInterfaceEnableItems from './move-interface-enable-items';
8
+ import convertEditPostPanels from './convert-edit-post-panels';
9
+ /**
10
+ * Gets the legacy local storage data for a given user.
11
+ *
12
+ * @param {string | number} userId The user id.
13
+ *
14
+ * @return {Object | null} The local storage data.
15
+ */
16
+
17
+ function getLegacyData(userId) {
18
+ const key = `WP_DATA_USER_${userId}`;
19
+ const unparsedData = window.localStorage.getItem(key);
20
+ return JSON.parse(unparsedData);
21
+ }
22
+ /**
23
+ * Converts data from the old `@wordpress/data` package format.
24
+ *
25
+ * @param {Object | null | undefined} data The legacy data in its original format.
26
+ *
27
+ * @return {Object | undefined} The converted data or `undefined` if there was
28
+ * nothing to convert.
29
+ */
30
+
31
+
32
+ export function convertLegacyData(data) {
33
+ var _data, _data$corePreference;
34
+
35
+ if (!data) {
36
+ return;
37
+ } // Move boolean feature preferences from each editor into the
38
+ // preferences store data structure.
39
+
40
+
41
+ data = moveFeaturePreferences(data, 'core/edit-widgets');
42
+ data = moveFeaturePreferences(data, 'core/customize-widgets');
43
+ data = moveFeaturePreferences(data, 'core/edit-post');
44
+ data = moveFeaturePreferences(data, 'core/edit-site'); // Move third party boolean feature preferences from the interface package
45
+ // to the preferences store data structure.
46
+
47
+ data = moveThirdPartyFeaturePreferences(data); // Move and convert the interface store's `enableItems` data into the
48
+ // preferences data structure.
49
+
50
+ data = moveInterfaceEnableItems(data); // Move individual ad-hoc preferences from various packages into the
51
+ // preferences store data structure.
52
+
53
+ data = moveIndividualPreference(data, {
54
+ from: 'core/edit-post',
55
+ to: 'core/edit-post'
56
+ }, 'hiddenBlockTypes');
57
+ data = moveIndividualPreference(data, {
58
+ from: 'core/edit-post',
59
+ to: 'core/edit-post'
60
+ }, 'editorMode');
61
+ data = moveIndividualPreference(data, {
62
+ from: 'core/edit-post',
63
+ to: 'core/edit-post'
64
+ }, 'preferredStyleVariations');
65
+ data = moveIndividualPreference(data, {
66
+ from: 'core/edit-post',
67
+ to: 'core/edit-post'
68
+ }, 'panels', convertEditPostPanels);
69
+ data = moveIndividualPreference(data, {
70
+ from: 'core/editor',
71
+ to: 'core/edit-post'
72
+ }, 'isPublishSidebarEnabled');
73
+ data = moveIndividualPreference(data, {
74
+ from: 'core/edit-site',
75
+ to: 'core/edit-site'
76
+ }, 'editorMode'); // The new system is only concerned with persisting
77
+ // 'core/preferences' preferences reducer, so only return that.
78
+
79
+ return (_data = data) === null || _data === void 0 ? void 0 : (_data$corePreference = _data['core/preferences']) === null || _data$corePreference === void 0 ? void 0 : _data$corePreference.preferences;
80
+ }
81
+ /**
82
+ * Gets the legacy local storage data for the given user and returns the
83
+ * data converted to the new format.
84
+ *
85
+ * @param {string | number} userId The user id.
86
+ *
87
+ * @return {Object | undefined} The converted data or undefined if no local
88
+ * storage data could be found.
89
+ */
90
+
91
+ export default function convertLegacyLocalStorageData(userId) {
92
+ const data = getLegacyData(userId);
93
+ return convertLegacyData(data);
94
+ }
95
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences-persistence/src/migrations/legacy-local-storage-data/index.js"],"names":["moveFeaturePreferences","moveThirdPartyFeaturePreferences","moveIndividualPreference","moveInterfaceEnableItems","convertEditPostPanels","getLegacyData","userId","key","unparsedData","window","localStorage","getItem","JSON","parse","convertLegacyData","data","from","to","preferences","convertLegacyLocalStorageData"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,sBAAP,MAAmC,4BAAnC;AACA,OAAOC,gCAAP,MAA6C,wCAA7C;AACA,OAAOC,wBAAP,MAAqC,8BAArC;AACA,OAAOC,wBAAP,MAAqC,+BAArC;AACA,OAAOC,qBAAP,MAAkC,4BAAlC;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,aAAT,CAAwBC,MAAxB,EAAiC;AAChC,QAAMC,GAAG,GAAI,gBAAgBD,MAAQ,EAArC;AACA,QAAME,YAAY,GAAGC,MAAM,CAACC,YAAP,CAAoBC,OAApB,CAA6BJ,GAA7B,CAArB;AACA,SAAOK,IAAI,CAACC,KAAL,CAAYL,YAAZ,CAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,OAAO,SAASM,iBAAT,CAA4BC,IAA5B,EAAmC;AAAA;;AACzC,MAAK,CAAEA,IAAP,EAAc;A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uBC,IAAAA,EAAE,EAAE;AAA3B,GAF8B,EAG9B,yBAH8B,CAA/B;AAKAF,EAAAA,IAAI,GAAGb,wBAAwB,CAC9Ba,IAD8B,EAE9B;AAAEC,IAAAA,IAAI,EAAE,gBAAR;AAA0BC,IAAAA,EAAE,EAAE;AAA9B,GAF8B,EAG9B,YAH8B,CAA/B,CAhDyC,CAsDzC;AACA;;AACA,kBAAOF,IAAP,kEAAO,MAAQ,kBAAR,CAAP,yDAAO,qBAA8BG,WAArC;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAe,SAASC,6BAAT,CAAwCb,MAAxC,EAAiD;AAC/D,QAAMS,IAAI,GAAGV,aAAa,CAAEC,MAAF,CAA1B;AACA,SAAOQ,iBAAiB,CAAEC,IAAF,CAAxB;AACA","sourcesContent":["/**\n * Internal dependencies\n */\nimport moveFeaturePreferences from './move-feature-preferences';\nimport moveThirdPartyFeaturePreferences from './move-third-party-feature-preferences';\nimport moveIndividualPreference from './move-individual-preference';\nimport moveInterfaceEnableItems from './move-interface-enable-items';\nimport convertEditPostPanels from './convert-edit-post-panels';\n\n/**\n * Gets the legacy local storage data for a given user.\n *\n * @param {string | number} userId The user id.\n *\n * @return {Object | null} The local storage data.\n */\nfunction getLegacyData( userId ) {\n\tconst key = `WP_DATA_USER_${ userId }`;\n\tconst unparsedData = window.localStorage.getItem( key );\n\treturn JSON.parse( unparsedData );\n}\n\n/**\n * Converts data from the old `@wordpress/data` package format.\n *\n * @param {Object | null | undefined} data The legacy data in its original format.\n *\n * @return {Object | undefined} The converted data or `undefined` if there was\n * nothing to convert.\n */\nexport function convertLegacyData( data ) {\n\tif ( ! data ) {\n\t\treturn;\n\t}\n\n\t// Move boolean feature preferences from each editor into the\n\t// preferences store data structure.\n\tdata = moveFeaturePreferences( data, 'core/edit-widgets' );\n\tdata = moveFeaturePreferences( data, 'core/customize-widgets' );\n\tdata = moveFeaturePreferences( data, 'core/edit-post' );\n\tdata = moveFeaturePreferences( data, 'core/edit-site' );\n\n\t// Move third party boolean feature preferences from the interface package\n\t// to the preferences store data structure.\n\tdata = moveThirdPartyFeaturePreferences( data );\n\n\t// Move and convert the interface store's `enableItems` data into the\n\t// preferences data structure.\n\tdata = moveInterfaceEnableItems( data );\n\n\t// Move individual ad-hoc preferences from various packages into the\n\t// preferences store data structure.\n\tdata = moveIndividualPreference(\n\t\tdata,\n\t\t{ from: 'core/edit-post', to: 'core/edit-post' },\n\t\t'hiddenBlockTypes'\n\t);\n\tdata = moveIndividualPreference(\n\t\tdata,\n\t\t{ from: 'core/edit-post', to: 'core/edit-post' },\n\t\t'editorMode'\n\t);\n\tdata = moveIndividualPreference(\n\t\tdata,\n\t\t{ from: 'core/edit-post', to: 'core/edit-post' },\n\t\t'preferredStyleVariations'\n\t);\n\tdata = moveIndividualPreference(\n\t\tdata,\n\t\t{ from: 'core/edit-post', to: 'core/edit-post' },\n\t\t'panels',\n\t\tconvertEditPostPanels\n\t);\n\tdata = moveIndividualPreference(\n\t\tdata,\n\t\t{ from: 'core/editor', to: 'core/edit-post' },\n\t\t'isPublishSidebarEnabled'\n\t);\n\tdata = moveIndividualPreference(\n\t\tdata,\n\t\t{ from: 'core/edit-site', to: 'core/edit-site' },\n\t\t'editorMode'\n\t);\n\n\t// The new system is only concerned with persisting\n\t// 'core/preferences' preferences reducer, so only return that.\n\treturn data?.[ 'core/preferences' ]?.preferences;\n}\n\n/**\n * Gets the legacy local storage data for the given user and returns the\n * data converted to the new format.\n *\n * @param {string | number} userId The user id.\n *\n * @return {Object | undefined} The converted data or undefined if no local\n * storage data could be found.\n */\nexport default function convertLegacyLocalStorageData( userId ) {\n\tconst data = getLegacyData( userId );\n\treturn convertLegacyData( data );\n}\n"]}
@@ -0,0 +1,128 @@
1
+ /**
2
+ * Move the 'features' object in local storage from the sourceStoreName to the
3
+ * preferences store data structure.
4
+ *
5
+ * Previously, editors used a data structure like this for feature preferences:
6
+ * ```js
7
+ * {
8
+ * 'core/edit-post': {
9
+ * preferences: {
10
+ * features; {
11
+ * topToolbar: true,
12
+ * // ... other boolean 'feature' preferences
13
+ * },
14
+ * },
15
+ * },
16
+ * }
17
+ * ```
18
+ *
19
+ * And for a while these feature preferences lived in the interface package:
20
+ * ```js
21
+ * {
22
+ * 'core/interface': {
23
+ * preferences: {
24
+ * features: {
25
+ * 'core/edit-post': {
26
+ * topToolbar: true
27
+ * }
28
+ * }
29
+ * }
30
+ * }
31
+ * }
32
+ * ```
33
+ *
34
+ * In the preferences store, 'features' aren't considered special, they're
35
+ * merged to the root level of the scope along with other preferences:
36
+ * ```js
37
+ * {
38
+ * 'core/preferences': {
39
+ * preferences: {
40
+ * 'core/edit-post': {
41
+ * topToolbar: true,
42
+ * // ... any other preferences.
43
+ * }
44
+ * }
45
+ * }
46
+ * }
47
+ * ```
48
+ *
49
+ * This function handles moving from either the source store or the interface
50
+ * store to the preferences data structure.
51
+ *
52
+ * @param {Object} state The state before migration.
53
+ * @param {string} sourceStoreName The name of the store that has persisted
54
+ * preferences to migrate to the preferences
55
+ * package.
56
+ * @return {Object} The migrated state
57
+ */
58
+ export default function moveFeaturePreferences(state, sourceStoreName) {
59
+ var _state$interfaceStore, _state$interfaceStore2, _state$interfaceStore3, _state$sourceStoreNam, _state$sourceStoreNam2, _state$preferencesSto;
60
+
61
+ const preferencesStoreName = 'core/preferences';
62
+ const interfaceStoreName = 'core/interface'; // Features most recently (and briefly) lived in the interface package.
63
+ // If data exists there, prioritize using that for the migration. If not
64
+ // also check the original package as the user may have updated from an
65
+ // older block editor version.
66
+
67
+ const interfaceFeatures = state === null || state === void 0 ? void 0 : (_state$interfaceStore = state[interfaceStoreName]) === null || _state$interfaceStore === void 0 ? void 0 : (_state$interfaceStore2 = _state$interfaceStore.preferences) === null || _state$interfaceStore2 === void 0 ? void 0 : (_state$interfaceStore3 = _state$interfaceStore2.features) === null || _state$interfaceStore3 === void 0 ? void 0 : _state$interfaceStore3[sourceStoreName];
68
+ const sourceFeatures = state === null || state === void 0 ? void 0 : (_state$sourceStoreNam = state[sourceStoreName]) === null || _state$sourceStoreNam === void 0 ? void 0 : (_state$sourceStoreNam2 = _state$sourceStoreNam.preferences) === null || _state$sourceStoreNam2 === void 0 ? void 0 : _state$sourceStoreNam2.features;
69
+ const featuresToMigrate = interfaceFeatures ? interfaceFeatures : sourceFeatures;
70
+
71
+ if (!featuresToMigrate) {
72
+ return state;
73
+ }
74
+
75
+ const existingPreferences = state === null || state === void 0 ? void 0 : (_state$preferencesSto = state[preferencesStoreName]) === null || _state$preferencesSto === void 0 ? void 0 : _state$preferencesSto.preferences; // Avoid migrating features again if they've previously been migrated.
76
+
77
+ if (existingPreferences !== null && existingPreferences !== void 0 && existingPreferences[sourceStoreName]) {
78
+ return state;
79
+ }
80
+
81
+ let updatedInterfaceState;
82
+
83
+ if (interfaceFeatures) {
84
+ var _state$interfaceStore4, _state$interfaceStore5;
85
+
86
+ const otherInterfaceState = state === null || state === void 0 ? void 0 : state[interfaceStoreName];
87
+ const otherInterfaceScopes = state === null || state === void 0 ? void 0 : (_state$interfaceStore4 = state[interfaceStoreName]) === null || _state$interfaceStore4 === void 0 ? void 0 : (_state$interfaceStore5 = _state$interfaceStore4.preferences) === null || _state$interfaceStore5 === void 0 ? void 0 : _state$interfaceStore5.features;
88
+ updatedInterfaceState = {
89
+ [interfaceStoreName]: { ...otherInterfaceState,
90
+ preferences: {
91
+ features: { ...otherInterfaceScopes,
92
+ [sourceStoreName]: undefined
93
+ }
94
+ }
95
+ }
96
+ };
97
+ }
98
+
99
+ let updatedSourceState;
100
+
101
+ if (sourceFeatures) {
102
+ var _state$sourceStoreNam3;
103
+
104
+ const otherSourceState = state === null || state === void 0 ? void 0 : state[sourceStoreName];
105
+ const sourcePreferences = state === null || state === void 0 ? void 0 : (_state$sourceStoreNam3 = state[sourceStoreName]) === null || _state$sourceStoreNam3 === void 0 ? void 0 : _state$sourceStoreNam3.preferences;
106
+ updatedSourceState = {
107
+ [sourceStoreName]: { ...otherSourceState,
108
+ preferences: { ...sourcePreferences,
109
+ features: undefined
110
+ }
111
+ }
112
+ };
113
+ } // Set the feature values in the interface store, the features
114
+ // object is keyed by 'scope', which matches the store name for
115
+ // the source.
116
+
117
+
118
+ return { ...state,
119
+ [preferencesStoreName]: {
120
+ preferences: { ...existingPreferences,
121
+ [sourceStoreName]: featuresToMigrate
122
+ }
123
+ },
124
+ ...updatedInterfaceState,
125
+ ...updatedSourceState
126
+ };
127
+ }
128
+ //# sourceMappingURL=move-feature-preferences.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences-persistence/src/migrations/legacy-local-storage-data/move-feature-preferences.js"],"names":["moveFeaturePreferences","state","sourceStoreName","preferencesStoreName","interfaceStoreName","interfaceFeatures","preferences","features","sourceFeatures","featuresToMigrate","existingPreferences","updatedInterfaceState","otherInterfaceState","otherInterfaceScopes","undefined","updatedSourceState","otherSourceState","sourcePreferences"],"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;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASA,sBAAT,CAAiCC,KAAjC,EAAwCC,eAAxC,EAA0D;AAAA;;AACxE,QAAMC,oBAAoB,GAAG,kBAA7B;AACA,QAAMC,kBAAkB,GAAG,gBAA3B,CAFwE,CAIxE;AACA;AACA;AACA;;AACA,QAAMC,iBAAiB,GACtBJ,KADsB,aACtBA,KADsB,gDACtBA,KAAK,CAAIG,kBAAJ,CADiB,oFACtB,sBAA+BE,WADT,qFACtB,uBAA4CC,QADtB,2DACtB,uBACCL,eADD,CADD;AAIA,QAAMM,cAAc,GAAGP,KAAH,aAAGA,KAAH,gD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c,gBADiB;AAEpBV,QAAAA,WAAW,EAAE,EACZ,GAAGW,iBADS;AAEZV,UAAAA,QAAQ,EAAEO;AAFE;AAFO;AADD,KAArB;AASA,GA7DuE,CA+DxE;AACA;AACA;;;AACA,SAAO,EACN,GAAGb,KADG;AAEN,KAAEE,oBAAF,GAA0B;AACzBG,MAAAA,WAAW,EAAE,EACZ,GAAGI,mBADS;AAEZ,SAAER,eAAF,GAAqBO;AAFT;AADY,KAFpB;AAQN,OAAGE,qBARG;AASN,OAAGI;AATG,GAAP;AAWA","sourcesContent":["/**\n * Move the 'features' object in local storage from the sourceStoreName to the\n * preferences store data structure.\n *\n * Previously, editors used a data structure like this for feature preferences:\n * ```js\n * {\n * 'core/edit-post': {\n * preferences: {\n * features; {\n * topToolbar: true,\n * // ... other boolean 'feature' preferences\n * },\n * },\n * },\n * }\n * ```\n *\n * And for a while these feature preferences lived in the interface package:\n * ```js\n * {\n * 'core/interface': {\n * preferences: {\n * features: {\n * 'core/edit-post': {\n * topToolbar: true\n * }\n * }\n * }\n * }\n * }\n * ```\n *\n * In the preferences store, 'features' aren't considered special, they're\n * merged to the root level of the scope along with other preferences:\n * ```js\n * {\n * 'core/preferences': {\n * preferences: {\n * 'core/edit-post': {\n * topToolbar: true,\n * // ... any other preferences.\n * }\n * }\n * }\n * }\n * ```\n *\n * This function handles moving from either the source store or the interface\n * store to the preferences data structure.\n *\n * @param {Object} state The state before migration.\n * @param {string} sourceStoreName The name of the store that has persisted\n * preferences to migrate to the preferences\n * package.\n * @return {Object} The migrated state\n */\nexport default function moveFeaturePreferences( state, sourceStoreName ) {\n\tconst preferencesStoreName = 'core/preferences';\n\tconst interfaceStoreName = 'core/interface';\n\n\t// Features most recently (and briefly) lived in the interface package.\n\t// If data exists there, prioritize using that for the migration. If not\n\t// also check the original package as the user may have updated from an\n\t// older block editor version.\n\tconst interfaceFeatures =\n\t\tstate?.[ interfaceStoreName ]?.preferences?.features?.[\n\t\t\tsourceStoreName\n\t\t];\n\tconst sourceFeatures = state?.[ sourceStoreName ]?.preferences?.features;\n\tconst featuresToMigrate = interfaceFeatures\n\t\t? interfaceFeatures\n\t\t: sourceFeatures;\n\n\tif ( ! featuresToMigrate ) {\n\t\treturn state;\n\t}\n\n\tconst existingPreferences = state?.[ preferencesStoreName ]?.preferences;\n\n\t// Avoid migrating features again if they've previously been migrated.\n\tif ( existingPreferences?.[ sourceStoreName ] ) {\n\t\treturn state;\n\t}\n\n\tlet updatedInterfaceState;\n\tif ( interfaceFeatures ) {\n\t\tconst otherInterfaceState = state?.[ interfaceStoreName ];\n\t\tconst otherInterfaceScopes =\n\t\t\tstate?.[ interfaceStoreName ]?.preferences?.features;\n\n\t\tupdatedInterfaceState = {\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[ sourceStoreName ]: undefined,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t};\n\t}\n\n\tlet updatedSourceState;\n\tif ( sourceFeatures ) {\n\t\tconst otherSourceState = state?.[ sourceStoreName ];\n\t\tconst sourcePreferences = state?.[ sourceStoreName ]?.preferences;\n\n\t\tupdatedSourceState = {\n\t\t\t[ sourceStoreName ]: {\n\t\t\t\t...otherSourceState,\n\t\t\t\tpreferences: {\n\t\t\t\t\t...sourcePreferences,\n\t\t\t\t\tfeatures: undefined,\n\t\t\t\t},\n\t\t\t},\n\t\t};\n\t}\n\n\t// Set the feature values in the interface store, the features\n\t// object is keyed by 'scope', which matches the store name for\n\t// the source.\n\treturn {\n\t\t...state,\n\t\t[ preferencesStoreName ]: {\n\t\t\tpreferences: {\n\t\t\t\t...existingPreferences,\n\t\t\t\t[ sourceStoreName ]: featuresToMigrate,\n\t\t\t},\n\t\t},\n\t\t...updatedInterfaceState,\n\t\t...updatedSourceState,\n\t};\n}\n"]}
@@ -0,0 +1,84 @@
1
+ const identity = arg => arg;
2
+ /**
3
+ * Migrates an individual item inside the `preferences` object for a package's store.
4
+ *
5
+ * Previously, some packages had individual 'preferences' of any data type, and many used
6
+ * complex nested data structures. For example:
7
+ * ```js
8
+ * {
9
+ * 'core/edit-post': {
10
+ * preferences: {
11
+ * panels: {
12
+ * publish: {
13
+ * opened: true,
14
+ * enabled: true,
15
+ * }
16
+ * },
17
+ * // ...other preferences.
18
+ * },
19
+ * },
20
+ * }
21
+ *
22
+ * This function supports moving an individual preference like 'panels' above into the
23
+ * preferences package data structure.
24
+ *
25
+ * It supports moving a preference to a particular scope in the preferences store and
26
+ * optionally converting the data using a `convert` function.
27
+ *
28
+ * ```
29
+ *
30
+ * @param {Object} state The original state.
31
+ * @param {Object} migrate An options object that contains details of the migration.
32
+ * @param {string} migrate.from The name of the store to migrate from.
33
+ * @param {string} migrate.to The scope in the preferences store to migrate to.
34
+ * @param {string} key The key in the preferences object to migrate.
35
+ * @param {?Function} convert A function that converts preferences from one format to another.
36
+ */
37
+
38
+
39
+ export default function moveIndividualPreferenceToPreferences(state, _ref, key) {
40
+ var _state$sourceStoreNam, _state$sourceStoreNam2, _state$preferencesSto, _state$preferencesSto2, _state$preferencesSto3, _state$preferencesSto4, _state$preferencesSto5, _state$preferencesSto6, _state$sourceStoreNam3;
41
+
42
+ let {
43
+ from: sourceStoreName,
44
+ to: scope
45
+ } = _ref;
46
+ let convert = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : identity;
47
+ const preferencesStoreName = 'core/preferences';
48
+ const sourcePreference = state === null || state === void 0 ? void 0 : (_state$sourceStoreNam = state[sourceStoreName]) === null || _state$sourceStoreNam === void 0 ? void 0 : (_state$sourceStoreNam2 = _state$sourceStoreNam.preferences) === null || _state$sourceStoreNam2 === void 0 ? void 0 : _state$sourceStoreNam2[key]; // There's nothing to migrate, exit early.
49
+
50
+ if (sourcePreference === undefined) {
51
+ return state;
52
+ }
53
+
54
+ const targetPreference = state === null || state === void 0 ? void 0 : (_state$preferencesSto = state[preferencesStoreName]) === null || _state$preferencesSto === void 0 ? void 0 : (_state$preferencesSto2 = _state$preferencesSto.preferences) === null || _state$preferencesSto2 === void 0 ? void 0 : (_state$preferencesSto3 = _state$preferencesSto2[scope]) === null || _state$preferencesSto3 === void 0 ? void 0 : _state$preferencesSto3[key]; // There's existing data at the target, so don't overwrite it, exit early.
55
+
56
+ if (targetPreference) {
57
+ return state;
58
+ }
59
+
60
+ const otherScopes = state === null || state === void 0 ? void 0 : (_state$preferencesSto4 = state[preferencesStoreName]) === null || _state$preferencesSto4 === void 0 ? void 0 : _state$preferencesSto4.preferences;
61
+ const otherPreferences = state === null || state === void 0 ? void 0 : (_state$preferencesSto5 = state[preferencesStoreName]) === null || _state$preferencesSto5 === void 0 ? void 0 : (_state$preferencesSto6 = _state$preferencesSto5.preferences) === null || _state$preferencesSto6 === void 0 ? void 0 : _state$preferencesSto6[scope];
62
+ const otherSourceState = state === null || state === void 0 ? void 0 : state[sourceStoreName];
63
+ const allSourcePreferences = state === null || state === void 0 ? void 0 : (_state$sourceStoreNam3 = state[sourceStoreName]) === null || _state$sourceStoreNam3 === void 0 ? void 0 : _state$sourceStoreNam3.preferences; // Pass an object with the key and value as this allows the convert
64
+ // function to convert to a data structure that has different keys.
65
+
66
+ const convertedPreferences = convert({
67
+ [key]: sourcePreference
68
+ });
69
+ return { ...state,
70
+ [preferencesStoreName]: {
71
+ preferences: { ...otherScopes,
72
+ [scope]: { ...otherPreferences,
73
+ ...convertedPreferences
74
+ }
75
+ }
76
+ },
77
+ [sourceStoreName]: { ...otherSourceState,
78
+ preferences: { ...allSourcePreferences,
79
+ [key]: undefined
80
+ }
81
+ }
82
+ };
83
+ }
84
+ //# sourceMappingURL=move-individual-preference.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences-persistence/src/migrations/legacy-local-storage-data/move-individual-preference.js"],"names":["identity","arg","moveIndividualPreferenceToPreferences","state","key","from","sourceStoreName","to","scope","convert","preferencesStoreName","sourcePreference","preferences","undefined","targetPreference","otherScopes","otherPreferences","otherSourceState","allSourcePreferences","convertedPreferences"],"mappings":"AAAA,MAAMA,QAAQ,GAAKC,GAAF,IAAWA,GAA5B;AAEA;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,eAAe,SAASC,qCAAT,CACdC,KADc,QAGdC,GAHc,EAKb;AAAA;;AAAA,MAHD;AAAEC,IAAAA,IAAI,EAAEC,eAAR;AAAyBC,IAAAA,EAAE,EAAEC;AAA7B,GAGC;AAAA,MADDC,OACC,uEADST,QACT;AACD,QAAMU,oBAAoB,GAAG,kBAA7B;AACA,QAAMC,gBAAgB,GAAGR,KAAH,aAAGA,KAAH,gDAAGA,KAAK,CAAIG,eAAJ,CAAR,oFAAG,sBAA4BM,WAA/B,2DAAG,uBAA2CR,GAA3C,CAAzB,CAFC,CAID;;AACA,MAAKO,gBAAgB,KAAKE,SAA1B,EAAsC;AACrC,WAAOV,KAAP;AACA;;AAED,QAAMW,gBAAgB,GACrBX,KADqB,aACrBA,KADqB,gDACrBA,KAAK,CAAIO,oBAAJ,CADgB,oFACrB,sBAAiCE,WADZ,qFACrB,uBAAgDJ,KAAhD,CADqB,2DACrB,uBAA2DJ,GAA3D,CADD,CATC,CAYD;;AACA,MAAKU,gBAAL,EAAwB;AACvB,WAAOX,KAAP;AACA;;AAED,QAAMY,WAAW,GAAGZ,KAAH,aAAGA,KAAH,iDAAGA,KAAK,CAAIO,oBAAJ,CAAR,2DAAG,uBAAiCE,WAArD;AACA,QAAMI,gBAAgB,GACrBb,KADqB,aACrBA,KADqB,iDACrBA,KAAK,CAAIO,oBAAJ,CADgB,qFACrB,uBAAiCE,WADZ,2DACrB,uBAAgDJ,KAAhD,CADD;AAGA,QAAMS,gBAAgB,GAAGd,KAAH,aAAGA,KAAH,uBAAGA,KAAK,CAAIG,eAAJ,CAA9B;AACA,QAAMY,oBAAoB,GAAGf,KAAH,aAAGA,KAAH,iDAAGA,KAAK,CAAIG,eAAJ,CAAR,2DAAG,uBAA4BM,WAAzD,CAtBC,CAwBD;AACA;;AACA,QAAMO,oBAAoB,GAAGV,OAAO,CAAE;AAAE,KAAEL,GAAF,GAASO;AAAX,GAAF,CAApC;AAEA,SAAO,EACN,GAAGR,KADG;AAEN,KAAEO,oBAAF,GAA0B;AACzBE,MAAAA,WAAW,EAAE,EACZ,GAAGG,WADS;AAEZ,SAAEP,KAAF,GAAW,EACV,GAAGQ,gBADO;AAEV,aAAGG;AAFO;AAFC;AADY,KAFpB;AAWN,KAAEb,eAAF,GAAqB,EACpB,GAAGW,gBADiB;AAEpBL,MAAAA,WAAW,EAAE,EACZ,GAAGM,oBADS;AAEZ,SAAEd,GAAF,GAASS;AAFG;AAFO;AAXf,GAAP;AAmBA","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"]}