preferences-persistence.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /******/ (function() { // webpackBootstrap
  2. /******/ "use strict";
  3. /******/ // The require scope
  4. /******/ var __webpack_require__ = {};
  5. /******/
  6. /************************************************************************/
  7. /******/ /* webpack/runtime/compat get default export */
  8. /******/ !function() {
  9. /******/ // getDefaultExport function for compatibility with non-harmony modules
  10. /******/ __webpack_require__.n = function(module) {
  11. /******/ var getter = module && module.__esModule ?
  12. /******/ function() { return module['default']; } :
  13. /******/ function() { return module; };
  14. /******/ __webpack_require__.d(getter, { a: getter });
  15. /******/ return getter;
  16. /******/ };
  17. /******/ }();
  18. /******/
  19. /******/ /* webpack/runtime/define property getters */
  20. /******/ !function() {
  21. /******/ // define getter functions for harmony exports
  22. /******/ __webpack_require__.d = function(exports, definition) {
  23. /******/ for(var key in definition) {
  24. /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  25. /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
  26. /******/ }
  27. /******/ }
  28. /******/ };
  29. /******/ }();
  30. /******/
  31. /******/ /* webpack/runtime/hasOwnProperty shorthand */
  32. /******/ !function() {
  33. /******/ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
  34. /******/ }();
  35. /******/
  36. /******/ /* webpack/runtime/make namespace object */
  37. /******/ !function() {
  38. /******/ // define __esModule on exports
  39. /******/ __webpack_require__.r = function(exports) {
  40. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  41. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  42. /******/ }
  43. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  44. /******/ };
  45. /******/ }();
  46. /******/
  47. /************************************************************************/
  48. var __webpack_exports__ = {};
  49. // ESM COMPAT FLAG
  50. __webpack_require__.r(__webpack_exports__);
  51. // EXPORTS
  52. __webpack_require__.d(__webpack_exports__, {
  53. "__unstableCreatePersistenceLayer": function() { return /* binding */ __unstableCreatePersistenceLayer; },
  54. "create": function() { return /* reexport */ create; }
  55. });
  56. ;// CONCATENATED MODULE: external ["wp","apiFetch"]
  57. var external_wp_apiFetch_namespaceObject = window["wp"]["apiFetch"];
  58. var external_wp_apiFetch_default = /*#__PURE__*/__webpack_require__.n(external_wp_apiFetch_namespaceObject);
  59. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences-persistence/build-module/create/debounce-async.js
  60. /**
  61. * Performs a leading edge debounce of async functions.
  62. *
  63. * If three functions are throttled at the same time:
  64. * - The first happens immediately.
  65. * - The second is never called.
  66. * - The third happens `delayMS` milliseconds after the first has resolved.
  67. *
  68. * This is distinct from `lodash.debounce` in that it waits for promise
  69. * resolution.
  70. *
  71. * @param {Function} func A function that returns a promise.
  72. * @param {number} delayMS A delay in milliseconds.
  73. *
  74. * @return {Function} A function that debounce whatever function is passed
  75. * to it.
  76. */
  77. function debounceAsync(func, delayMS) {
  78. let timeoutId;
  79. let activePromise;
  80. return async function debounced() {
  81. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  82. args[_key] = arguments[_key];
  83. }
  84. // This is a leading edge debounce. If there's no promise or timeout
  85. // in progress, call the debounced function immediately.
  86. if (!activePromise && !timeoutId) {
  87. return new Promise((resolve, reject) => {
  88. // Keep a reference to the promise.
  89. activePromise = func(...args).then(function () {
  90. resolve(...arguments);
  91. }).catch(error => {
  92. reject(error);
  93. }).finally(() => {
  94. // As soon this promise is complete, clear the way for the
  95. // next one to happen immediately.
  96. activePromise = null;
  97. });
  98. });
  99. }
  100. if (activePromise) {
  101. // Let any active promises finish before queuing the next request.
  102. await activePromise;
  103. } // Clear any active timeouts, abandoning any requests that have
  104. // been queued but not been made.
  105. if (timeoutId) {
  106. clearTimeout(timeoutId);
  107. timeoutId = null;
  108. } // Trigger any trailing edge calls to the function.
  109. return new Promise((resolve, reject) => {
  110. // Schedule the next request but with a delay.
  111. timeoutId = setTimeout(() => {
  112. activePromise = func(...args).then(function () {
  113. resolve(...arguments);
  114. }).catch(error => {
  115. reject(error);
  116. }).finally(() => {
  117. // As soon this promise is complete, clear the way for the
  118. // next one to happen immediately.
  119. activePromise = null;
  120. timeoutId = null;
  121. });
  122. }, delayMS);
  123. });
  124. };
  125. }
  126. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences-persistence/build-module/create/index.js
  127. /**
  128. * WordPress dependencies
  129. */
  130. /**
  131. * Internal dependencies
  132. */
  133. const EMPTY_OBJECT = {};
  134. const localStorage = window.localStorage;
  135. /**
  136. * Creates a persistence layer that stores data in WordPress user meta via the
  137. * REST API.
  138. *
  139. * @param {Object} options
  140. * @param {?Object} options.preloadedData Any persisted preferences data that should be preloaded.
  141. * When set, the persistence layer will avoid fetching data
  142. * from the REST API.
  143. * @param {?string} options.localStorageRestoreKey The key to use for restoring the localStorage backup, used
  144. * when the persistence layer calls `localStorage.getItem` or
  145. * `localStorage.setItem`.
  146. * @param {?number} options.requestDebounceMS Debounce requests to the API so that they only occur at
  147. * minimum every `requestDebounceMS` milliseconds, and don't
  148. * swamp the server. Defaults to 2500ms.
  149. *
  150. * @return {Object} A persistence layer for WordPress user meta.
  151. */
  152. function create() {
  153. let {
  154. preloadedData,
  155. localStorageRestoreKey = 'WP_PREFERENCES_RESTORE_DATA',
  156. requestDebounceMS = 2500
  157. } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  158. let cache = preloadedData;
  159. const debouncedApiFetch = debounceAsync((external_wp_apiFetch_default()), requestDebounceMS);
  160. async function get() {
  161. var _user$meta;
  162. if (cache) {
  163. return cache;
  164. }
  165. const user = await external_wp_apiFetch_default()({
  166. path: '/wp/v2/users/me?context=edit'
  167. });
  168. const serverData = user === null || user === void 0 ? void 0 : (_user$meta = user.meta) === null || _user$meta === void 0 ? void 0 : _user$meta.persisted_preferences;
  169. const localData = JSON.parse(localStorage.getItem(localStorageRestoreKey)); // Date parse returns NaN for invalid input. Coerce anything invalid
  170. // into a conveniently comparable zero.
  171. const serverTimestamp = Date.parse(serverData === null || serverData === void 0 ? void 0 : serverData._modified) || 0;
  172. const localTimestamp = Date.parse(localData === null || localData === void 0 ? void 0 : localData._modified) || 0; // Prefer server data if it exists and is more recent.
  173. // Otherwise fallback to localStorage data.
  174. if (serverData && serverTimestamp >= localTimestamp) {
  175. cache = serverData;
  176. } else if (localData) {
  177. cache = localData;
  178. } else {
  179. cache = EMPTY_OBJECT;
  180. }
  181. return cache;
  182. }
  183. function set(newData) {
  184. const dataWithTimestamp = { ...newData,
  185. _modified: new Date().toISOString()
  186. };
  187. cache = dataWithTimestamp; // Store data in local storage as a fallback. If for some reason the
  188. // api request does not complete or becomes unavailable, this data
  189. // can be used to restore preferences.
  190. localStorage.setItem(localStorageRestoreKey, JSON.stringify(dataWithTimestamp)); // The user meta endpoint seems susceptible to errors when consecutive
  191. // requests are made in quick succession. Ensure there's a gap between
  192. // any consecutive requests.
  193. //
  194. // Catch and do nothing with errors from the REST API.
  195. debouncedApiFetch({
  196. path: '/wp/v2/users/me',
  197. method: 'PUT',
  198. // `keepalive` will still send the request in the background,
  199. // even when a browser unload event might interrupt it.
  200. // This should hopefully make things more resilient.
  201. // This does have a size limit of 64kb, but the data is usually
  202. // much less.
  203. keepalive: true,
  204. data: {
  205. meta: {
  206. persisted_preferences: dataWithTimestamp
  207. }
  208. }
  209. }).catch(() => {});
  210. }
  211. return {
  212. get,
  213. set
  214. };
  215. }
  216. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences-persistence/build-module/migrations/legacy-local-storage-data/move-feature-preferences.js
  217. /**
  218. * Move the 'features' object in local storage from the sourceStoreName to the
  219. * preferences store data structure.
  220. *
  221. * Previously, editors used a data structure like this for feature preferences:
  222. * ```js
  223. * {
  224. * 'core/edit-post': {
  225. * preferences: {
  226. * features; {
  227. * topToolbar: true,
  228. * // ... other boolean 'feature' preferences
  229. * },
  230. * },
  231. * },
  232. * }
  233. * ```
  234. *
  235. * And for a while these feature preferences lived in the interface package:
  236. * ```js
  237. * {
  238. * 'core/interface': {
  239. * preferences: {
  240. * features: {
  241. * 'core/edit-post': {
  242. * topToolbar: true
  243. * }
  244. * }
  245. * }
  246. * }
  247. * }
  248. * ```
  249. *
  250. * In the preferences store, 'features' aren't considered special, they're
  251. * merged to the root level of the scope along with other preferences:
  252. * ```js
  253. * {
  254. * 'core/preferences': {
  255. * preferences: {
  256. * 'core/edit-post': {
  257. * topToolbar: true,
  258. * // ... any other preferences.
  259. * }
  260. * }
  261. * }
  262. * }
  263. * ```
  264. *
  265. * This function handles moving from either the source store or the interface
  266. * store to the preferences data structure.
  267. *
  268. * @param {Object} state The state before migration.
  269. * @param {string} sourceStoreName The name of the store that has persisted
  270. * preferences to migrate to the preferences
  271. * package.
  272. * @return {Object} The migrated state
  273. */
  274. function moveFeaturePreferences(state, sourceStoreName) {
  275. var _state$interfaceStore, _state$interfaceStore2, _state$interfaceStore3, _state$sourceStoreNam, _state$sourceStoreNam2, _state$preferencesSto;
  276. const preferencesStoreName = 'core/preferences';
  277. const interfaceStoreName = 'core/interface'; // Features most recently (and briefly) lived in the interface package.
  278. // If data exists there, prioritize using that for the migration. If not
  279. // also check the original package as the user may have updated from an
  280. // older block editor version.
  281. 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];
  282. 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;
  283. const featuresToMigrate = interfaceFeatures ? interfaceFeatures : sourceFeatures;
  284. if (!featuresToMigrate) {
  285. return state;
  286. }
  287. 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.
  288. if (existingPreferences !== null && existingPreferences !== void 0 && existingPreferences[sourceStoreName]) {
  289. return state;
  290. }
  291. let updatedInterfaceState;
  292. if (interfaceFeatures) {
  293. var _state$interfaceStore4, _state$interfaceStore5;
  294. const otherInterfaceState = state === null || state === void 0 ? void 0 : state[interfaceStoreName];
  295. 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;
  296. updatedInterfaceState = {
  297. [interfaceStoreName]: { ...otherInterfaceState,
  298. preferences: {
  299. features: { ...otherInterfaceScopes,
  300. [sourceStoreName]: undefined
  301. }
  302. }
  303. }
  304. };
  305. }
  306. let updatedSourceState;
  307. if (sourceFeatures) {
  308. var _state$sourceStoreNam3;
  309. const otherSourceState = state === null || state === void 0 ? void 0 : state[sourceStoreName];
  310. const sourcePreferences = state === null || state === void 0 ? void 0 : (_state$sourceStoreNam3 = state[sourceStoreName]) === null || _state$sourceStoreNam3 === void 0 ? void 0 : _state$sourceStoreNam3.preferences;
  311. updatedSourceState = {
  312. [sourceStoreName]: { ...otherSourceState,
  313. preferences: { ...sourcePreferences,
  314. features: undefined
  315. }
  316. }
  317. };
  318. } // Set the feature values in the interface store, the features
  319. // object is keyed by 'scope', which matches the store name for
  320. // the source.
  321. return { ...state,
  322. [preferencesStoreName]: {
  323. preferences: { ...existingPreferences,
  324. [sourceStoreName]: featuresToMigrate
  325. }
  326. },
  327. ...updatedInterfaceState,
  328. ...updatedSourceState
  329. };
  330. }
  331. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences-persistence/build-module/migrations/legacy-local-storage-data/move-third-party-feature-preferences.js
  332. /**
  333. * The interface package previously had a public API that could be used by
  334. * plugins to set persisted boolean 'feature' preferences.
  335. *
  336. * While usage was likely non-existent or very small, this function ensures
  337. * those are migrated to the preferences data structure. The interface
  338. * package's APIs have now been deprecated and use the preferences store.
  339. *
  340. * This will convert data that looks like this:
  341. * ```js
  342. * {
  343. * 'core/interface': {
  344. * preferences: {
  345. * features: {
  346. * 'my-plugin': {
  347. * myPluginFeature: true
  348. * }
  349. * }
  350. * }
  351. * }
  352. * }
  353. * ```
  354. *
  355. * To this:
  356. * ```js
  357. * * {
  358. * 'core/preferences': {
  359. * preferences: {
  360. * 'my-plugin': {
  361. * myPluginFeature: true
  362. * }
  363. * }
  364. * }
  365. * }
  366. * ```
  367. *
  368. * @param {Object} state The local storage state
  369. *
  370. * @return {Object} The state with third party preferences moved to the
  371. * preferences data structure.
  372. */
  373. function moveThirdPartyFeaturePreferencesToPreferences(state) {
  374. var _state$interfaceStore, _state$interfaceStore2;
  375. const interfaceStoreName = 'core/interface';
  376. const preferencesStoreName = 'core/preferences';
  377. const interfaceScopes = 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$interfaceStore2.features;
  378. const interfaceScopeKeys = interfaceScopes ? Object.keys(interfaceScopes) : [];
  379. if (!(interfaceScopeKeys !== null && interfaceScopeKeys !== void 0 && interfaceScopeKeys.length)) {
  380. return state;
  381. }
  382. return interfaceScopeKeys.reduce(function (convertedState, scope) {
  383. var _convertedState$prefe, _convertedState$prefe2, _convertedState$prefe3, _convertedState$inter, _convertedState$inter2;
  384. if (scope.startsWith('core')) {
  385. return convertedState;
  386. }
  387. const featuresToMigrate = interfaceScopes === null || interfaceScopes === void 0 ? void 0 : interfaceScopes[scope];
  388. if (!featuresToMigrate) {
  389. return convertedState;
  390. }
  391. const existingMigratedData = convertedState === null || convertedState === void 0 ? void 0 : (_convertedState$prefe = convertedState[preferencesStoreName]) === null || _convertedState$prefe === void 0 ? void 0 : (_convertedState$prefe2 = _convertedState$prefe.preferences) === null || _convertedState$prefe2 === void 0 ? void 0 : _convertedState$prefe2[scope];
  392. if (existingMigratedData) {
  393. return convertedState;
  394. }
  395. const otherPreferencesScopes = convertedState === null || convertedState === void 0 ? void 0 : (_convertedState$prefe3 = convertedState[preferencesStoreName]) === null || _convertedState$prefe3 === void 0 ? void 0 : _convertedState$prefe3.preferences;
  396. const otherInterfaceState = convertedState === null || convertedState === void 0 ? void 0 : convertedState[interfaceStoreName];
  397. const otherInterfaceScopes = convertedState === null || convertedState === void 0 ? void 0 : (_convertedState$inter = convertedState[interfaceStoreName]) === null || _convertedState$inter === void 0 ? void 0 : (_convertedState$inter2 = _convertedState$inter.preferences) === null || _convertedState$inter2 === void 0 ? void 0 : _convertedState$inter2.features;
  398. return { ...convertedState,
  399. [preferencesStoreName]: {
  400. preferences: { ...otherPreferencesScopes,
  401. [scope]: featuresToMigrate
  402. }
  403. },
  404. [interfaceStoreName]: { ...otherInterfaceState,
  405. preferences: {
  406. features: { ...otherInterfaceScopes,
  407. [scope]: undefined
  408. }
  409. }
  410. }
  411. };
  412. }, state);
  413. }
  414. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences-persistence/build-module/migrations/legacy-local-storage-data/move-individual-preference.js
  415. const identity = arg => arg;
  416. /**
  417. * Migrates an individual item inside the `preferences` object for a package's store.
  418. *
  419. * Previously, some packages had individual 'preferences' of any data type, and many used
  420. * complex nested data structures. For example:
  421. * ```js
  422. * {
  423. * 'core/edit-post': {
  424. * preferences: {
  425. * panels: {
  426. * publish: {
  427. * opened: true,
  428. * enabled: true,
  429. * }
  430. * },
  431. * // ...other preferences.
  432. * },
  433. * },
  434. * }
  435. *
  436. * This function supports moving an individual preference like 'panels' above into the
  437. * preferences package data structure.
  438. *
  439. * It supports moving a preference to a particular scope in the preferences store and
  440. * optionally converting the data using a `convert` function.
  441. *
  442. * ```
  443. *
  444. * @param {Object} state The original state.
  445. * @param {Object} migrate An options object that contains details of the migration.
  446. * @param {string} migrate.from The name of the store to migrate from.
  447. * @param {string} migrate.to The scope in the preferences store to migrate to.
  448. * @param {string} key The key in the preferences object to migrate.
  449. * @param {?Function} convert A function that converts preferences from one format to another.
  450. */
  451. function moveIndividualPreferenceToPreferences(state, _ref, key) {
  452. var _state$sourceStoreNam, _state$sourceStoreNam2, _state$preferencesSto, _state$preferencesSto2, _state$preferencesSto3, _state$preferencesSto4, _state$preferencesSto5, _state$preferencesSto6, _state$sourceStoreNam3;
  453. let {
  454. from: sourceStoreName,
  455. to: scope
  456. } = _ref;
  457. let convert = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : identity;
  458. const preferencesStoreName = 'core/preferences';
  459. 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.
  460. if (sourcePreference === undefined) {
  461. return state;
  462. }
  463. 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.
  464. if (targetPreference) {
  465. return state;
  466. }
  467. const otherScopes = state === null || state === void 0 ? void 0 : (_state$preferencesSto4 = state[preferencesStoreName]) === null || _state$preferencesSto4 === void 0 ? void 0 : _state$preferencesSto4.preferences;
  468. 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];
  469. const otherSourceState = state === null || state === void 0 ? void 0 : state[sourceStoreName];
  470. 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
  471. // function to convert to a data structure that has different keys.
  472. const convertedPreferences = convert({
  473. [key]: sourcePreference
  474. });
  475. return { ...state,
  476. [preferencesStoreName]: {
  477. preferences: { ...otherScopes,
  478. [scope]: { ...otherPreferences,
  479. ...convertedPreferences
  480. }
  481. }
  482. },
  483. [sourceStoreName]: { ...otherSourceState,
  484. preferences: { ...allSourcePreferences,
  485. [key]: undefined
  486. }
  487. }
  488. };
  489. }
  490. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences-persistence/build-module/migrations/legacy-local-storage-data/move-interface-enable-items.js
  491. /**
  492. * Migrates interface 'enableItems' data to the preferences store.
  493. *
  494. * The interface package stores this data in this format:
  495. * ```js
  496. * {
  497. * enableItems: {
  498. * singleEnableItems: {
  499. * complementaryArea: {
  500. * 'core/edit-post': 'edit-post/document',
  501. * 'core/edit-site': 'edit-site/global-styles',
  502. * }
  503. * },
  504. * multipleEnableItems: {
  505. * pinnedItems: {
  506. * 'core/edit-post': {
  507. * 'plugin-1': true,
  508. * },
  509. * 'core/edit-site': {
  510. * 'plugin-2': true,
  511. * },
  512. * },
  513. * }
  514. * }
  515. * }
  516. * ```
  517. *
  518. * and it should be converted it to:
  519. * ```js
  520. * {
  521. * 'core/edit-post': {
  522. * complementaryArea: 'edit-post/document',
  523. * pinnedItems: {
  524. * 'plugin-1': true,
  525. * },
  526. * },
  527. * 'core/edit-site': {
  528. * complementaryArea: 'edit-site/global-styles',
  529. * pinnedItems: {
  530. * 'plugin-2': true,
  531. * },
  532. * },
  533. * }
  534. * ```
  535. *
  536. * @param {Object} state The local storage state.
  537. */
  538. function moveInterfaceEnableItems(state) {
  539. var _state$interfaceStore, _state$preferencesSto, _state$preferencesSto2, _sourceEnableItems$si, _sourceEnableItems$si2, _sourceEnableItems$mu, _sourceEnableItems$mu2;
  540. const interfaceStoreName = 'core/interface';
  541. const preferencesStoreName = 'core/preferences';
  542. const sourceEnableItems = state === null || state === void 0 ? void 0 : (_state$interfaceStore = state[interfaceStoreName]) === null || _state$interfaceStore === void 0 ? void 0 : _state$interfaceStore.enableItems; // There's nothing to migrate, exit early.
  543. if (!sourceEnableItems) {
  544. return state;
  545. }
  546. const allPreferences = (_state$preferencesSto = state === null || state === void 0 ? void 0 : (_state$preferencesSto2 = state[preferencesStoreName]) === null || _state$preferencesSto2 === void 0 ? void 0 : _state$preferencesSto2.preferences) !== null && _state$preferencesSto !== void 0 ? _state$preferencesSto : {}; // First convert complementaryAreas into the right format.
  547. // Use the existing preferences as the accumulator so that the data is
  548. // merged.
  549. const sourceComplementaryAreas = (_sourceEnableItems$si = sourceEnableItems === null || sourceEnableItems === void 0 ? void 0 : (_sourceEnableItems$si2 = sourceEnableItems.singleEnableItems) === null || _sourceEnableItems$si2 === void 0 ? void 0 : _sourceEnableItems$si2.complementaryArea) !== null && _sourceEnableItems$si !== void 0 ? _sourceEnableItems$si : {};
  550. const preferencesWithConvertedComplementaryAreas = Object.keys(sourceComplementaryAreas).reduce((accumulator, scope) => {
  551. var _accumulator$scope;
  552. const data = sourceComplementaryAreas[scope]; // Don't overwrite any existing data in the preferences store.
  553. if (accumulator !== null && accumulator !== void 0 && (_accumulator$scope = accumulator[scope]) !== null && _accumulator$scope !== void 0 && _accumulator$scope.complementaryArea) {
  554. return accumulator;
  555. }
  556. return { ...accumulator,
  557. [scope]: { ...accumulator[scope],
  558. complementaryArea: data
  559. }
  560. };
  561. }, allPreferences); // Next feed the converted complementary areas back into a reducer that
  562. // converts the pinned items, resulting in the fully migrated data.
  563. const sourcePinnedItems = (_sourceEnableItems$mu = sourceEnableItems === null || sourceEnableItems === void 0 ? void 0 : (_sourceEnableItems$mu2 = sourceEnableItems.multipleEnableItems) === null || _sourceEnableItems$mu2 === void 0 ? void 0 : _sourceEnableItems$mu2.pinnedItems) !== null && _sourceEnableItems$mu !== void 0 ? _sourceEnableItems$mu : {};
  564. const allConvertedData = Object.keys(sourcePinnedItems).reduce((accumulator, scope) => {
  565. var _accumulator$scope2;
  566. const data = sourcePinnedItems[scope]; // Don't overwrite any existing data in the preferences store.
  567. if (accumulator !== null && accumulator !== void 0 && (_accumulator$scope2 = accumulator[scope]) !== null && _accumulator$scope2 !== void 0 && _accumulator$scope2.pinnedItems) {
  568. return accumulator;
  569. }
  570. return { ...accumulator,
  571. [scope]: { ...accumulator[scope],
  572. pinnedItems: data
  573. }
  574. };
  575. }, preferencesWithConvertedComplementaryAreas);
  576. const otherInterfaceItems = state[interfaceStoreName];
  577. return { ...state,
  578. [preferencesStoreName]: {
  579. preferences: allConvertedData
  580. },
  581. [interfaceStoreName]: { ...otherInterfaceItems,
  582. enableItems: undefined
  583. }
  584. };
  585. }
  586. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences-persistence/build-module/migrations/legacy-local-storage-data/convert-edit-post-panels.js
  587. /**
  588. * Convert the post editor's panels state from:
  589. * ```
  590. * {
  591. * panels: {
  592. * tags: {
  593. * enabled: true,
  594. * opened: true,
  595. * },
  596. * permalinks: {
  597. * enabled: false,
  598. * opened: false,
  599. * },
  600. * },
  601. * }
  602. * ```
  603. *
  604. * to a new, more concise data structure:
  605. * {
  606. * inactivePanels: [
  607. * 'permalinks',
  608. * ],
  609. * openPanels: [
  610. * 'tags',
  611. * ],
  612. * }
  613. *
  614. * @param {Object} preferences A preferences object.
  615. *
  616. * @return {Object} The converted data.
  617. */
  618. function convertEditPostPanels(preferences) {
  619. var _preferences$panels;
  620. const panels = (_preferences$panels = preferences === null || preferences === void 0 ? void 0 : preferences.panels) !== null && _preferences$panels !== void 0 ? _preferences$panels : {};
  621. return Object.keys(panels).reduce((convertedData, panelName) => {
  622. const panel = panels[panelName];
  623. if ((panel === null || panel === void 0 ? void 0 : panel.enabled) === false) {
  624. convertedData.inactivePanels.push(panelName);
  625. }
  626. if ((panel === null || panel === void 0 ? void 0 : panel.opened) === true) {
  627. convertedData.openPanels.push(panelName);
  628. }
  629. return convertedData;
  630. }, {
  631. inactivePanels: [],
  632. openPanels: []
  633. });
  634. }
  635. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences-persistence/build-module/migrations/legacy-local-storage-data/index.js
  636. /**
  637. * Internal dependencies
  638. */
  639. /**
  640. * Gets the legacy local storage data for a given user.
  641. *
  642. * @param {string | number} userId The user id.
  643. *
  644. * @return {Object | null} The local storage data.
  645. */
  646. function getLegacyData(userId) {
  647. const key = `WP_DATA_USER_${userId}`;
  648. const unparsedData = window.localStorage.getItem(key);
  649. return JSON.parse(unparsedData);
  650. }
  651. /**
  652. * Converts data from the old `@wordpress/data` package format.
  653. *
  654. * @param {Object | null | undefined} data The legacy data in its original format.
  655. *
  656. * @return {Object | undefined} The converted data or `undefined` if there was
  657. * nothing to convert.
  658. */
  659. function convertLegacyData(data) {
  660. var _data, _data$corePreference;
  661. if (!data) {
  662. return;
  663. } // Move boolean feature preferences from each editor into the
  664. // preferences store data structure.
  665. data = moveFeaturePreferences(data, 'core/edit-widgets');
  666. data = moveFeaturePreferences(data, 'core/customize-widgets');
  667. data = moveFeaturePreferences(data, 'core/edit-post');
  668. data = moveFeaturePreferences(data, 'core/edit-site'); // Move third party boolean feature preferences from the interface package
  669. // to the preferences store data structure.
  670. data = moveThirdPartyFeaturePreferencesToPreferences(data); // Move and convert the interface store's `enableItems` data into the
  671. // preferences data structure.
  672. data = moveInterfaceEnableItems(data); // Move individual ad-hoc preferences from various packages into the
  673. // preferences store data structure.
  674. data = moveIndividualPreferenceToPreferences(data, {
  675. from: 'core/edit-post',
  676. to: 'core/edit-post'
  677. }, 'hiddenBlockTypes');
  678. data = moveIndividualPreferenceToPreferences(data, {
  679. from: 'core/edit-post',
  680. to: 'core/edit-post'
  681. }, 'editorMode');
  682. data = moveIndividualPreferenceToPreferences(data, {
  683. from: 'core/edit-post',
  684. to: 'core/edit-post'
  685. }, 'preferredStyleVariations');
  686. data = moveIndividualPreferenceToPreferences(data, {
  687. from: 'core/edit-post',
  688. to: 'core/edit-post'
  689. }, 'panels', convertEditPostPanels);
  690. data = moveIndividualPreferenceToPreferences(data, {
  691. from: 'core/editor',
  692. to: 'core/edit-post'
  693. }, 'isPublishSidebarEnabled');
  694. data = moveIndividualPreferenceToPreferences(data, {
  695. from: 'core/edit-site',
  696. to: 'core/edit-site'
  697. }, 'editorMode'); // The new system is only concerned with persisting
  698. // 'core/preferences' preferences reducer, so only return that.
  699. return (_data = data) === null || _data === void 0 ? void 0 : (_data$corePreference = _data['core/preferences']) === null || _data$corePreference === void 0 ? void 0 : _data$corePreference.preferences;
  700. }
  701. /**
  702. * Gets the legacy local storage data for the given user and returns the
  703. * data converted to the new format.
  704. *
  705. * @param {string | number} userId The user id.
  706. *
  707. * @return {Object | undefined} The converted data or undefined if no local
  708. * storage data could be found.
  709. */
  710. function convertLegacyLocalStorageData(userId) {
  711. const data = getLegacyData(userId);
  712. return convertLegacyData(data);
  713. }
  714. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences-persistence/build-module/migrations/preferences-package-data/convert-complementary-areas.js
  715. function convertComplementaryAreas(state) {
  716. return Object.keys(state).reduce((stateAccumulator, scope) => {
  717. const scopeData = state[scope]; // If a complementary area is truthy, convert it to the `isComplementaryAreaVisible` boolean.
  718. if (scopeData !== null && scopeData !== void 0 && scopeData.complementaryArea) {
  719. const updatedScopeData = { ...scopeData
  720. };
  721. delete updatedScopeData.complementaryArea;
  722. updatedScopeData.isComplementaryAreaVisible = true;
  723. stateAccumulator[scope] = updatedScopeData;
  724. return stateAccumulator;
  725. }
  726. return stateAccumulator;
  727. }, state);
  728. }
  729. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences-persistence/build-module/migrations/preferences-package-data/index.js
  730. /**
  731. * Internal dependencies
  732. */
  733. function convertPreferencesPackageData(data) {
  734. return convertComplementaryAreas(data);
  735. }
  736. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences-persistence/build-module/index.js
  737. /**
  738. * Internal dependencies
  739. */
  740. /**
  741. * Creates the persistence layer with preloaded data.
  742. *
  743. * It prioritizes any data from the server, but falls back first to localStorage
  744. * restore data, and then to any legacy data.
  745. *
  746. * This function is used internally by WordPress in an inline script, so
  747. * prefixed with `__unstable`.
  748. *
  749. * @param {Object} serverData Preferences data preloaded from the server.
  750. * @param {string} userId The user id.
  751. *
  752. * @return {Object} The persistence layer initialized with the preloaded data.
  753. */
  754. function __unstableCreatePersistenceLayer(serverData, userId) {
  755. const localStorageRestoreKey = `WP_PREFERENCES_USER_${userId}`;
  756. const localData = JSON.parse(window.localStorage.getItem(localStorageRestoreKey)); // Date parse returns NaN for invalid input. Coerce anything invalid
  757. // into a conveniently comparable zero.
  758. const serverModified = Date.parse(serverData && serverData._modified) || 0;
  759. const localModified = Date.parse(localData && localData._modified) || 0;
  760. let preloadedData;
  761. if (serverData && serverModified >= localModified) {
  762. preloadedData = convertPreferencesPackageData(serverData);
  763. } else if (localData) {
  764. preloadedData = convertPreferencesPackageData(localData);
  765. } else {
  766. // Check if there is data in the legacy format from the old persistence system.
  767. preloadedData = convertLegacyLocalStorageData(userId);
  768. }
  769. return create({
  770. preloadedData,
  771. localStorageRestoreKey
  772. });
  773. }
  774. (window.wp = window.wp || {}).preferencesPersistence = __webpack_exports__;
  775. /******/ })()
  776. ;