@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
package/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Preferences persistence
2
+
3
+ Persistence utilities for `wordpress/preferences`.
4
+
5
+ Includes a persistence layer that saves data to WordPress user meta via the REST API. If for any reason data cannot be saved to the database, this persistence layer also uses local storage as a fallback.
6
+
7
+ ## Installation
8
+
9
+ Install the module
10
+
11
+ ```bash
12
+ npm install @wordpress/preferences-persistence --save
13
+ ```
14
+
15
+ _This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for such language features and APIs, you should include [the polyfill shipped in `@wordpress/babel-preset-default`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/babel-preset-default#polyfill) in your code._
16
+
17
+ ## Usage
18
+
19
+ Call the `create` function to create a persistence layer.
20
+
21
+ ```js
22
+ const persistenceLayer = create();
23
+ ```
24
+
25
+ Next, configure the preferences package to use this persistence layer:
26
+
27
+ ```js
28
+ wp.data( 'core/preferences' ).setPersistenceLayer( persistenceLayer );
29
+ ```
30
+
31
+ ## Reference
32
+
33
+ <!-- START TOKEN(Autogenerated Docs|src/index.js) -->
34
+
35
+ ### create
36
+
37
+ Creates a persistence layer that stores data in WordPress user meta via the
38
+ REST API.
39
+
40
+ _Parameters_
41
+
42
+ - _options_ `Object`:
43
+ - _options.preloadedData_ `?Object`: Any persisted preferences data that should be preloaded. When set, the persistence layer will avoid fetching data from the REST API.
44
+ - _options.localStorageRestoreKey_ `?string`: The key to use for restoring the localStorage backup, used when the persistence layer calls `localStorage.getItem` or `localStorage.setItem`.
45
+ - _options.requestDebounceMS_ `?number`: Debounce requests to the API so that they only occur at minimum every `requestDebounceMS` milliseconds, and don't swamp the server. Defaults to 2500ms.
46
+
47
+ _Returns_
48
+
49
+ - `Object`: A persistence layer for WordPress user meta.
50
+
51
+ <!-- END TOKEN(Autogenerated Docs|src/index.js) -->
52
+
53
+ ## Contributing to this package
54
+
55
+ This is an individual package that's part of the Gutenberg project. The project is organized as a monorepo. It's made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to [npm](https://www.npmjs.com/) and used by [WordPress](https://make.wordpress.org/core/) as well as other software projects.
56
+
57
+ To find out more about contributing to this package or Gutenberg as a whole, please read the project's main [contributor guide](https://github.com/WordPress/gutenberg/tree/HEAD/CONTRIBUTING.md).
58
+
59
+ <br /><br /><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = debounceAsync;
7
+
8
+ /**
9
+ * Performs a leading edge debounce of async functions.
10
+ *
11
+ * If three functions are throttled at the same time:
12
+ * - The first happens immediately.
13
+ * - The second is never called.
14
+ * - The third happens `delayMS` milliseconds after the first has resolved.
15
+ *
16
+ * This is distinct from `lodash.debounce` in that it waits for promise
17
+ * resolution.
18
+ *
19
+ * @param {Function} func A function that returns a promise.
20
+ * @param {number} delayMS A delay in milliseconds.
21
+ *
22
+ * @return {Function} A function that debounce whatever function is passed
23
+ * to it.
24
+ */
25
+ function debounceAsync(func, delayMS) {
26
+ let timeoutId;
27
+ let activePromise;
28
+ return async function debounced() {
29
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
30
+ args[_key] = arguments[_key];
31
+ }
32
+
33
+ // This is a leading edge debounce. If there's no promise or timeout
34
+ // in progress, call the debounced function immediately.
35
+ if (!activePromise && !timeoutId) {
36
+ return new Promise((resolve, reject) => {
37
+ // Keep a reference to the promise.
38
+ activePromise = func(...args).then(function () {
39
+ resolve(...arguments);
40
+ }).catch(error => {
41
+ reject(error);
42
+ }).finally(() => {
43
+ // As soon this promise is complete, clear the way for the
44
+ // next one to happen immediately.
45
+ activePromise = null;
46
+ });
47
+ });
48
+ }
49
+
50
+ if (activePromise) {
51
+ // Let any active promises finish before queuing the next request.
52
+ await activePromise;
53
+ } // Clear any active timeouts, abandoning any requests that have
54
+ // been queued but not been made.
55
+
56
+
57
+ if (timeoutId) {
58
+ clearTimeout(timeoutId);
59
+ timeoutId = null;
60
+ } // Trigger any trailing edge calls to the function.
61
+
62
+
63
+ return new Promise((resolve, reject) => {
64
+ // Schedule the next request but with a delay.
65
+ timeoutId = setTimeout(() => {
66
+ activePromise = func(...args).then(function () {
67
+ resolve(...arguments);
68
+ }).catch(error => {
69
+ reject(error);
70
+ }).finally(() => {
71
+ // As soon this promise is complete, clear the way for the
72
+ // next one to happen immediately.
73
+ activePromise = null;
74
+ timeoutId = null;
75
+ });
76
+ }, delayMS);
77
+ });
78
+ };
79
+ }
80
+ //# sourceMappingURL=debounce-async.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences-persistence/src/create/debounce-async.js"],"names":["debounceAsync","func","delayMS","timeoutId","activePromise","debounced","args","Promise","resolve","reject","then","catch","error","finally","clearTimeout","setTimeout"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,aAAT,CAAwBC,IAAxB,EAA8BC,OAA9B,EAAwC;AACtD,MAAIC,SAAJ;AACA,MAAIC,aAAJ;AAEA,SAAO,eAAeC,SAAf,GAAoC;AAAA,sCAAPC,IAAO;AAAPA,MAAAA,IAAO;AAAA;;AAC1C;AACA;AACA,QAAK,CAAEF,aAAF,IAAmB,CAAED,SAA1B,EAAsC;AACrC,aAAO,IAAII,OAAJ,CAAa,CAAEC,OAAF,EAAWC,MAAX,KAAuB;AAC1C;AACAL,QAAAA,aAAa,GAAGH,IAAI,CAAE,GAAGK,IAAL,CAAJ,CACdI,IADc,CACR,YAAmB;AACzBF,UAAAA,OAAO,CAAE,YAAF,CAAP;AACA,SAHc,EAIdG,KAJc,CAILC,KAAF,IAAa;AACpBH,UAAAA,MAAM,CAAEG,KAAF,CAAN;AACA,SANc,EAOdC,OAPc,CAOL,MAAM;AACf;AACA;AACAT,UAAAA,aAAa,GAAG,IAAhB;AACA,SAXc,CAAhB;AAYA,OAdM,CAAP;AAeA;;AAED,QAAKA,aAAL,EAAqB;AACpB;AACA,YAAMA,aAAN;AACA,KAxByC,CA0B1C;AACA;;;AACA,QAAKD,SAAL,EAAiB;AAChBW,MAAAA,YAAY,CAAEX,SAAF,CAAZ;AACAA,MAAAA,SAAS,GAAG,IAAZ;AACA,KA/ByC,CAiC1C;;;AACA,WAAO,IAAII,OAAJ,CAAa,CAAEC,OAAF,EAAWC,MAAX,KAAuB;AAC1C;AACAN,MAAAA,SAAS,GAAGY,UAAU,CAAE,MAAM;AAC7BX,QAAAA,aAAa,GAAGH,IAAI,CAAE,GAAGK,IAAL,CAAJ,CACdI,IADc,CACR,YAAmB;AACzBF,UAAAA,OAAO,CAAE,YAAF,CAAP;AACA,SAHc,EAIdG,KAJc,CAILC,KAAF,IAAa;AACpBH,UAAAA,MAAM,CAAEG,KAAF,CAAN;AACA,SANc,EAOdC,OAPc,CAOL,MAAM;AACf;AACA;AACAT,UAAAA,aAAa,GAAG,IAAhB;AACAD,UAAAA,SAAS,GAAG,IAAZ;AACA,SAZc,CAAhB;AAaA,OAdqB,EAcnBD,OAdmB,CAAtB;AAeA,KAjBM,CAAP;AAkBA,GApDD;AAqDA","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 `lodash.debounce` in that it waits for promise\n * 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"]}
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = create;
9
+
10
+ var _apiFetch = _interopRequireDefault(require("@wordpress/api-fetch"));
11
+
12
+ var _debounceAsync = _interopRequireDefault(require("./debounce-async"));
13
+
14
+ /**
15
+ * WordPress dependencies
16
+ */
17
+
18
+ /**
19
+ * Internal dependencies
20
+ */
21
+ const EMPTY_OBJECT = {};
22
+ const localStorage = window.localStorage;
23
+ /**
24
+ * Creates a persistence layer that stores data in WordPress user meta via the
25
+ * REST API.
26
+ *
27
+ * @param {Object} options
28
+ * @param {?Object} options.preloadedData Any persisted preferences data that should be preloaded.
29
+ * When set, the persistence layer will avoid fetching data
30
+ * from the REST API.
31
+ * @param {?string} options.localStorageRestoreKey The key to use for restoring the localStorage backup, used
32
+ * when the persistence layer calls `localStorage.getItem` or
33
+ * `localStorage.setItem`.
34
+ * @param {?number} options.requestDebounceMS Debounce requests to the API so that they only occur at
35
+ * minimum every `requestDebounceMS` milliseconds, and don't
36
+ * swamp the server. Defaults to 2500ms.
37
+ *
38
+ * @return {Object} A persistence layer for WordPress user meta.
39
+ */
40
+
41
+ function create() {
42
+ let {
43
+ preloadedData,
44
+ localStorageRestoreKey = 'WP_PREFERENCES_RESTORE_DATA',
45
+ requestDebounceMS = 2500
46
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
47
+ let cache = preloadedData;
48
+ const debouncedApiFetch = (0, _debounceAsync.default)(_apiFetch.default, requestDebounceMS);
49
+
50
+ async function get() {
51
+ var _user$meta;
52
+
53
+ if (cache) {
54
+ return cache;
55
+ }
56
+
57
+ const user = await (0, _apiFetch.default)({
58
+ path: '/wp/v2/users/me?context=edit'
59
+ });
60
+ const serverData = user === null || user === void 0 ? void 0 : (_user$meta = user.meta) === null || _user$meta === void 0 ? void 0 : _user$meta.persisted_preferences;
61
+ const localData = JSON.parse(localStorage.getItem(localStorageRestoreKey)); // Date parse returns NaN for invalid input. Coerce anything invalid
62
+ // into a conveniently comparable zero.
63
+
64
+ const serverTimestamp = Date.parse(serverData === null || serverData === void 0 ? void 0 : serverData._modified) || 0;
65
+ const localTimestamp = Date.parse(localData === null || localData === void 0 ? void 0 : localData._modified) || 0; // Prefer server data if it exists and is more recent.
66
+ // Otherwise fallback to localStorage data.
67
+
68
+ if (serverData && serverTimestamp >= localTimestamp) {
69
+ cache = serverData;
70
+ } else if (localData) {
71
+ cache = localData;
72
+ } else {
73
+ cache = EMPTY_OBJECT;
74
+ }
75
+
76
+ return cache;
77
+ }
78
+
79
+ function set(newData) {
80
+ const dataWithTimestamp = { ...newData,
81
+ _modified: new Date().toISOString()
82
+ };
83
+ cache = dataWithTimestamp; // Store data in local storage as a fallback. If for some reason the
84
+ // api request does not complete or becomes unavailable, this data
85
+ // can be used to restore preferences.
86
+
87
+ localStorage.setItem(localStorageRestoreKey, JSON.stringify(dataWithTimestamp)); // The user meta endpoint seems susceptible to errors when consecutive
88
+ // requests are made in quick succession. Ensure there's a gap between
89
+ // any consecutive requests.
90
+ //
91
+ // Catch and do nothing with errors from the REST API.
92
+
93
+ debouncedApiFetch({
94
+ path: '/wp/v2/users/me',
95
+ method: 'PUT',
96
+ // `keepalive` will still send the request in the background,
97
+ // even when a browser unload event might interrupt it.
98
+ // This should hopefully make things more resilient.
99
+ // This does have a size limit of 64kb, but the data is usually
100
+ // much less.
101
+ keepalive: true,
102
+ data: {
103
+ meta: {
104
+ persisted_preferences: dataWithTimestamp
105
+ }
106
+ }
107
+ }).catch(() => {});
108
+ }
109
+
110
+ return {
111
+ get,
112
+ set
113
+ };
114
+ }
115
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences-persistence/src/create/index.js"],"names":["EMPTY_OBJECT","localStorage","window","create","preloadedData","localStorageRestoreKey","requestDebounceMS","cache","debouncedApiFetch","apiFetch","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":";;;;;;;;;AAGA;;AAKA;;AARA;AACA;AACA;;AAGA;AACA;AACA;AAGA,MAAMA,YAAY,GAAG,EAArB;AACA,MAAMC,YAAY,GAAGC,MAAM,CAACD,YAA5B;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACe,SAASE,MAAT,GAIN;AAAA,MAJuB;AAC/BC,IAAAA,aAD+B;AAE/BC,IAAAA,sBAAsB,GAAG,6BAFM;AAG/BC,IAAAA,iBAAiB,GAAG;AAHW,GAIvB,uEAAL,EAAK;AACR,MAAIC,KAAK,GAAGH,aAAZ;AACA,QAAMI,iBAAiB,GAAG,4BAAeC,iBAAf,EAAyBH,iBAAzB,CAA1B;;AAEA,iBAAeI,GAAf,GAAqB;AAAA;;AACpB,QAAKH,KAAL,EAAa;AACZ,aAAOA,KAAP;AACA;;AAED,UAAMI,IAAI,GAAG,MAAM,uBAAU;AAC5BC,MAAAA,IAAI,EAAE;AADsB,KAAV,CAAnB;AAIA,UAAMC,UAAU,GAAGF,IAAH,aAAGA,IAAH,qCAAGA,IAAI,CAAEG,IAAT,+CAAG,WAAYC,qBAA/B;AACA,UAAMC,SAAS,GAAGC,IAAI,CAACC,KAAL,CACjBjB,YAAY,CAACkB,OAAb,CAAsBd,sBAAtB,CADiB,CAAlB,CAVoB,CAcpB;AACA;;AACA,UAAMe,eAAe,GAAGC,IAAI,CAACH,KAAL,CAAYL,UAAZ,aAAYA,UAAZ,uBAAYA,UAAU,CAAES,SAAxB,KAAuC,CAA/D;AACA,UAAMC,cAAc,GAAGF,IAAI,CAACH,KAAL,CAAYF,SAAZ,aAAYA,SAAZ,uBAAYA,SAAS,CAAEM,SAAvB,KAAsC,CAA7D,CAjBoB,CAmBpB;AACA;;AACA,QAAKT,UAAU,IAAIO,eAAe,IAAIG,cAAtC,EAAuD;AACtDhB,MAAAA,KAAK,GAAGM,UAAR;AACA,KAFD,MAEO,IAAKG,SAAL,EAAiB;AACvBT,MAAAA,KAAK,GAAGS,SAAR;AACA,KAFM,MAEA;AACNT,MAAAA,KAAK,GAAGP,YAAR;AACA;;AAED,WAAOO,KAAP;AACA;;AAED,WAASiB,GAAT,CAAcC,OAAd,EAAwB;AACvB,UAAMC,iBAAiB,GAAG,EACzB,GAAGD,OADsB;AAEzBH,MAAAA,SAAS,EAAE,IAAID,IAAJ,GAAWM,WAAX;AAFc,KAA1B;AAIApB,IAAAA,KAAK,GAAGmB,iBAAR,CALuB,CAOvB;AACA;AACA;;AACAzB,IAAAA,YAAY,CAAC2B,OAAb,CACCvB,sBADD,EAECY,IAAI,CAACY,SAAL,CAAgBH,iBAAhB,CAFD,EAVuB,CAevB;AACA;AACA;AACA;AACA;;AACAlB,IAAAA,iBAAiB,CAAE;AAClBI,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"]}
package/build/index.js ADDED
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.__unstableCreatePersistenceLayer = __unstableCreatePersistenceLayer;
9
+ Object.defineProperty(exports, "create", {
10
+ enumerable: true,
11
+ get: function () {
12
+ return _create.default;
13
+ }
14
+ });
15
+
16
+ var _create = _interopRequireDefault(require("./create"));
17
+
18
+ var _legacyLocalStorageData = _interopRequireDefault(require("./migrations/legacy-local-storage-data"));
19
+
20
+ /**
21
+ * Internal dependencies
22
+ */
23
+
24
+ /**
25
+ * Creates the persistence layer with preloaded data.
26
+ *
27
+ * It prioritizes any data from the server, but falls back first to localStorage
28
+ * restore data, and then to any legacy data.
29
+ *
30
+ * This function is used internally by WordPress in an inline script, so
31
+ * prefixed with `__unstable`.
32
+ *
33
+ * @param {Object} serverData Preferences data preloaded from the server.
34
+ * @param {string} userId The user id.
35
+ *
36
+ * @return {Object} The persistence layer initialized with the preloaded data.
37
+ */
38
+ function __unstableCreatePersistenceLayer(serverData, userId) {
39
+ const localStorageRestoreKey = `WP_PREFERENCES_USER_${userId}`;
40
+ const localData = JSON.parse(window.localStorage.getItem(localStorageRestoreKey)); // Date parse returns NaN for invalid input. Coerce anything invalid
41
+ // into a conveniently comparable zero.
42
+
43
+ const serverModified = Date.parse(serverData && serverData._modified) || 0;
44
+ const localModified = Date.parse(localData && localData._modified) || 0;
45
+ let preloadedData;
46
+
47
+ if (serverData && serverModified >= localModified) {
48
+ preloadedData = serverData;
49
+ } else if (localData) {
50
+ preloadedData = localData;
51
+ } else {
52
+ // Check if there is data in the legacy format from the old persistence system.
53
+ preloadedData = (0, _legacyLocalStorageData.default)(userId);
54
+ }
55
+
56
+ return (0, _create.default)({
57
+ preloadedData,
58
+ localStorageRestoreKey
59
+ });
60
+ }
61
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences-persistence/src/index.js"],"names":["__unstableCreatePersistenceLayer","serverData","userId","localStorageRestoreKey","localData","JSON","parse","window","localStorage","getItem","serverModified","Date","_modified","localModified","preloadedData"],"mappings":";;;;;;;;;;;;;;;AAGA;;AACA;;AAJA;AACA;AACA;;AAMA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,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,GAAG,qCAA+BZ,MAA/B,CAAhB;AACA;;AAED,SAAO,qBAAQ;AACdY,IAAAA,aADc;AAEdX,IAAAA;AAFc,GAAR,CAAP;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,60 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = convertEditPostPanels;
7
+
8
+ /**
9
+ * Convert the post editor's panels state from:
10
+ * ```
11
+ * {
12
+ * panels: {
13
+ * tags: {
14
+ * enabled: true,
15
+ * opened: true,
16
+ * },
17
+ * permalinks: {
18
+ * enabled: false,
19
+ * opened: false,
20
+ * },
21
+ * },
22
+ * }
23
+ * ```
24
+ *
25
+ * to a new, more concise data structure:
26
+ * {
27
+ * inactivePanels: [
28
+ * 'permalinks',
29
+ * ],
30
+ * openPanels: [
31
+ * 'tags',
32
+ * ],
33
+ * }
34
+ *
35
+ * @param {Object} preferences A preferences object.
36
+ *
37
+ * @return {Object} The converted data.
38
+ */
39
+ function convertEditPostPanels(preferences) {
40
+ var _preferences$panels;
41
+
42
+ const panels = (_preferences$panels = preferences === null || preferences === void 0 ? void 0 : preferences.panels) !== null && _preferences$panels !== void 0 ? _preferences$panels : {};
43
+ return Object.keys(panels).reduce((convertedData, panelName) => {
44
+ const panel = panels[panelName];
45
+
46
+ if ((panel === null || panel === void 0 ? void 0 : panel.enabled) === false) {
47
+ convertedData.inactivePanels.push(panelName);
48
+ }
49
+
50
+ if ((panel === null || panel === void 0 ? void 0 : panel.opened) === true) {
51
+ convertedData.openPanels.push(panelName);
52
+ }
53
+
54
+ return convertedData;
55
+ }, {
56
+ inactivePanels: [],
57
+ openPanels: []
58
+ });
59
+ }
60
+ //# 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;AACe,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,111 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.convertLegacyData = convertLegacyData;
9
+ exports.default = convertLegacyLocalStorageData;
10
+
11
+ var _moveFeaturePreferences = _interopRequireDefault(require("./move-feature-preferences"));
12
+
13
+ var _moveThirdPartyFeaturePreferences = _interopRequireDefault(require("./move-third-party-feature-preferences"));
14
+
15
+ var _moveIndividualPreference = _interopRequireDefault(require("./move-individual-preference"));
16
+
17
+ var _moveInterfaceEnableItems = _interopRequireDefault(require("./move-interface-enable-items"));
18
+
19
+ var _convertEditPostPanels = _interopRequireDefault(require("./convert-edit-post-panels"));
20
+
21
+ /**
22
+ * Internal dependencies
23
+ */
24
+
25
+ /**
26
+ * Gets the legacy local storage data for a given user.
27
+ *
28
+ * @param {string | number} userId The user id.
29
+ *
30
+ * @return {Object | null} The local storage data.
31
+ */
32
+ function getLegacyData(userId) {
33
+ const key = `WP_DATA_USER_${userId}`;
34
+ const unparsedData = window.localStorage.getItem(key);
35
+ return JSON.parse(unparsedData);
36
+ }
37
+ /**
38
+ * Converts data from the old `@wordpress/data` package format.
39
+ *
40
+ * @param {Object | null | undefined} data The legacy data in its original format.
41
+ *
42
+ * @return {Object | undefined} The converted data or `undefined` if there was
43
+ * nothing to convert.
44
+ */
45
+
46
+
47
+ function convertLegacyData(data) {
48
+ var _data, _data$corePreference;
49
+
50
+ if (!data) {
51
+ return;
52
+ } // Move boolean feature preferences from each editor into the
53
+ // preferences store data structure.
54
+
55
+
56
+ data = (0, _moveFeaturePreferences.default)(data, 'core/edit-widgets');
57
+ data = (0, _moveFeaturePreferences.default)(data, 'core/customize-widgets');
58
+ data = (0, _moveFeaturePreferences.default)(data, 'core/edit-post');
59
+ data = (0, _moveFeaturePreferences.default)(data, 'core/edit-site'); // Move third party boolean feature preferences from the interface package
60
+ // to the preferences store data structure.
61
+
62
+ data = (0, _moveThirdPartyFeaturePreferences.default)(data); // Move and convert the interface store's `enableItems` data into the
63
+ // preferences data structure.
64
+
65
+ data = (0, _moveInterfaceEnableItems.default)(data); // Move individual ad-hoc preferences from various packages into the
66
+ // preferences store data structure.
67
+
68
+ data = (0, _moveIndividualPreference.default)(data, {
69
+ from: 'core/edit-post',
70
+ to: 'core/edit-post'
71
+ }, 'hiddenBlockTypes');
72
+ data = (0, _moveIndividualPreference.default)(data, {
73
+ from: 'core/edit-post',
74
+ to: 'core/edit-post'
75
+ }, 'editorMode');
76
+ data = (0, _moveIndividualPreference.default)(data, {
77
+ from: 'core/edit-post',
78
+ to: 'core/edit-post'
79
+ }, 'preferredStyleVariations');
80
+ data = (0, _moveIndividualPreference.default)(data, {
81
+ from: 'core/edit-post',
82
+ to: 'core/edit-post'
83
+ }, 'panels', _convertEditPostPanels.default);
84
+ data = (0, _moveIndividualPreference.default)(data, {
85
+ from: 'core/editor',
86
+ to: 'core/edit-post'
87
+ }, 'isPublishSidebarEnabled');
88
+ data = (0, _moveIndividualPreference.default)(data, {
89
+ from: 'core/edit-site',
90
+ to: 'core/edit-site'
91
+ }, 'editorMode'); // The new system is only concerned with persisting
92
+ // 'core/preferences' preferences reducer, so only return that.
93
+
94
+ return (_data = data) === null || _data === void 0 ? void 0 : (_data$corePreference = _data['core/preferences']) === null || _data$corePreference === void 0 ? void 0 : _data$corePreference.preferences;
95
+ }
96
+ /**
97
+ * Gets the legacy local storage data for the given user and returns the
98
+ * data converted to the new format.
99
+ *
100
+ * @param {string | number} userId The user id.
101
+ *
102
+ * @return {Object | undefined} The converted data or undefined if no local
103
+ * storage data could be found.
104
+ */
105
+
106
+
107
+ function convertLegacyLocalStorageData(userId) {
108
+ const data = getLegacyData(userId);
109
+ return convertLegacyData(data);
110
+ }
111
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences-persistence/src/migrations/legacy-local-storage-data/index.js"],"names":["getLegacyData","userId","key","unparsedData","window","localStorage","getItem","JSON","parse","convertLegacyData","data","from","to","convertEditPostPanels","preferences","convertLegacyLocalStorageData"],"mappings":";;;;;;;;;;AAGA;;AACA;;AACA;;AACA;;AACA;;AAPA;AACA;AACA;;AAOA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,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;;;AACO,SAASM,iBAAT,CAA4BC,IAA5B,EAAmC;AAAA;;AACzC,MAAK,CAAEA,IAAP,EAAc;AACb;AACA,GAHwC,CAKzC;AACA;;;AACAA,EAAAA,IAAI,GAAG,qCAAwBA,IAAxB,EAA8B,mBAA9B,CAAP;AACAA,EAAAA,IAAI,GAAG,qCAAwBA,IAAxB,EAA8B,wBAA9B,CAAP;AACAA,EAAAA,IAAI,GAAG,qCAAwBA,IAAxB,EAA8B,gBAA9B,CAAP;AACAA,EAAAA,IAAI,GAAG,qCAAwBA,IAAxB,EAA8B,gBAA9B,CAAP,CAVyC,CAYzC;AACA;;AACAA,EAAAA,IAAI,GAAG,+CAAkCA,IAAlC,CAAP,CAdyC,CAgBzC;AACA;;AACAA,EAAAA,IAAI,GAAG,uCAA0BA,IAA1B,CAAP,CAlByC,CAoBzC;AACA;;AACAA,EAAAA,IAAI,GAAG,uCACNA,IADM,EAEN;AAAEC,IAAAA,IAAI,EAAE,gBAAR;AAA0BC,IAAAA,EAAE,EAAE;AAA9B,GAFM,EAGN,kBAHM,CAAP;AAKAF,EAAAA,IAAI,GAAG,uCACNA,IADM,EAEN;AAAEC,IAAAA,IAAI,EAAE,gBAAR;AAA0BC,IAAAA,EAAE,EAAE;AAA9B,GAFM,EAGN,YAHM,CAAP;AAKAF,EAAAA,IAAI,GAAG,uCACNA,IADM,EAEN;AAAEC,IAAAA,IAAI,EAAE,gBAAR;AAA0BC,IAAAA,EAAE,EAAE;AAA9B,GAFM,EAGN,0BAHM,CAAP;AAKAF,EAAAA,IAAI,GAAG,uCACNA,IADM,EAEN;AAAEC,IAAAA,IAAI,EAAE,gBAAR;AAA0BC,IAAAA,EAAE,EAAE;AAA9B,GAFM,EAGN,QAHM,EAINC,8BAJM,CAAP;AAMAH,EAAAA,IAAI,GAAG,uCACNA,IADM,EAEN;AAAEC,IAAAA,IAAI,EAAE,aAAR;AAAuBC,IAAAA,EAAE,EAAE;AAA3B,GAFM,EAGN,yBAHM,CAAP;AAKAF,EAAAA,IAAI,GAAG,uCACNA,IADM,EAEN;AAAEC,IAAAA,IAAI,EAAE,gBAAR;AAA0BC,IAAAA,EAAE,EAAE;AAA9B,GAFM,EAGN,YAHM,CAAP,CAhDyC,CAsDzC;AACA;;AACA,kBAAOF,IAAP,kEAAO,MAAQ,kBAAR,CAAP,yDAAO,qBAA8BI,WAArC;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACe,SAASC,6BAAT,CAAwCd,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"]}