@wordpress/preferences-persistence 2.57.0 → 3.1.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 (136) hide show
  1. package/README.md +7 -7
  2. package/build/create/debounce-async.cjs +55 -58
  3. package/build/create/debounce-async.cjs.map +1 -7
  4. package/build/create/index.cjs +75 -91
  5. package/build/create/index.cjs.map +1 -7
  6. package/build/get-newest-data.cjs +64 -0
  7. package/build/get-newest-data.cjs.map +1 -0
  8. package/build/index.cjs +52 -53
  9. package/build/index.cjs.map +1 -7
  10. package/build/is-preferences-data.cjs +20 -0
  11. package/build/is-preferences-data.cjs.map +1 -0
  12. package/build/local-storage.cjs +39 -0
  13. package/build/local-storage.cjs.map +1 -0
  14. package/build/migrations/legacy-local-storage-data/convert-edit-post-panels.cjs +49 -38
  15. package/build/migrations/legacy-local-storage-data/convert-edit-post-panels.cjs.map +1 -7
  16. package/build/migrations/legacy-local-storage-data/index.cjs +95 -83
  17. package/build/migrations/legacy-local-storage-data/index.cjs.map +1 -7
  18. package/build/migrations/legacy-local-storage-data/move-feature-preferences.cjs +106 -77
  19. package/build/migrations/legacy-local-storage-data/move-feature-preferences.cjs.map +1 -7
  20. package/build/migrations/legacy-local-storage-data/move-individual-preference.cjs +70 -58
  21. package/build/migrations/legacy-local-storage-data/move-individual-preference.cjs.map +1 -7
  22. package/build/migrations/legacy-local-storage-data/move-interface-enable-items.cjs +93 -75
  23. package/build/migrations/legacy-local-storage-data/move-interface-enable-items.cjs.map +1 -7
  24. package/build/migrations/legacy-local-storage-data/move-third-party-feature-preferences.cjs +77 -65
  25. package/build/migrations/legacy-local-storage-data/move-third-party-feature-preferences.cjs.map +1 -7
  26. package/build/migrations/legacy-local-storage-data/types.cjs +5 -0
  27. package/build/migrations/preferences-package-data/convert-complementary-areas.cjs +19 -35
  28. package/build/migrations/preferences-package-data/convert-complementary-areas.cjs.map +1 -7
  29. package/build/migrations/preferences-package-data/convert-editor-settings.cjs +62 -60
  30. package/build/migrations/preferences-package-data/convert-editor-settings.cjs.map +1 -7
  31. package/build/migrations/preferences-package-data/index.cjs +29 -32
  32. package/build/migrations/preferences-package-data/index.cjs.map +1 -7
  33. package/build/types.cjs +5 -0
  34. package/build-module/create/debounce-async.mjs +51 -39
  35. package/build-module/create/debounce-async.mjs.map +1 -7
  36. package/build-module/create/index.mjs +51 -67
  37. package/build-module/create/index.mjs.map +1 -7
  38. package/build-module/get-newest-data.mjs +36 -0
  39. package/build-module/get-newest-data.mjs.map +1 -0
  40. package/build-module/index.mjs +24 -25
  41. package/build-module/index.mjs.map +1 -7
  42. package/build-module/is-preferences-data.mjs +15 -0
  43. package/build-module/is-preferences-data.mjs.map +1 -0
  44. package/build-module/local-storage.mjs +33 -0
  45. package/build-module/local-storage.mjs.map +1 -0
  46. package/build-module/migrations/legacy-local-storage-data/convert-edit-post-panels.mjs +45 -19
  47. package/build-module/migrations/legacy-local-storage-data/convert-edit-post-panels.mjs.map +1 -7
  48. package/build-module/migrations/legacy-local-storage-data/index.mjs +65 -52
  49. package/build-module/migrations/legacy-local-storage-data/index.mjs.map +1 -7
  50. package/build-module/migrations/legacy-local-storage-data/move-feature-preferences.mjs +102 -58
  51. package/build-module/migrations/legacy-local-storage-data/move-feature-preferences.mjs.map +1 -7
  52. package/build-module/migrations/legacy-local-storage-data/move-individual-preference.mjs +66 -39
  53. package/build-module/migrations/legacy-local-storage-data/move-individual-preference.mjs.map +1 -7
  54. package/build-module/migrations/legacy-local-storage-data/move-interface-enable-items.mjs +89 -56
  55. package/build-module/migrations/legacy-local-storage-data/move-interface-enable-items.mjs.map +1 -7
  56. package/build-module/migrations/legacy-local-storage-data/move-third-party-feature-preferences.mjs +73 -46
  57. package/build-module/migrations/legacy-local-storage-data/move-third-party-feature-preferences.mjs.map +1 -7
  58. package/build-module/migrations/legacy-local-storage-data/types.mjs +0 -0
  59. package/build-module/migrations/preferences-package-data/convert-complementary-areas.mjs +15 -16
  60. package/build-module/migrations/preferences-package-data/convert-complementary-areas.mjs.map +1 -7
  61. package/build-module/migrations/preferences-package-data/convert-editor-settings.mjs +41 -46
  62. package/build-module/migrations/preferences-package-data/convert-editor-settings.mjs.map +1 -7
  63. package/build-module/migrations/preferences-package-data/index.mjs +7 -8
  64. package/build-module/migrations/preferences-package-data/index.mjs.map +1 -7
  65. package/build-module/types.mjs +0 -0
  66. package/build-types/create/debounce-async.d.ts +19 -0
  67. package/build-types/create/debounce-async.d.ts.map +1 -0
  68. package/build-types/create/index.d.ts +20 -0
  69. package/build-types/create/index.d.ts.map +1 -0
  70. package/build-types/get-newest-data.d.ts +11 -0
  71. package/build-types/get-newest-data.d.ts.map +1 -0
  72. package/build-types/index.d.ts +20 -0
  73. package/build-types/index.d.ts.map +1 -0
  74. package/build-types/is-preferences-data.d.ts +11 -0
  75. package/build-types/is-preferences-data.d.ts.map +1 -0
  76. package/build-types/local-storage.d.ts +19 -0
  77. package/build-types/local-storage.d.ts.map +1 -0
  78. package/build-types/migrations/legacy-local-storage-data/convert-edit-post-panels.d.ts +35 -0
  79. package/build-types/migrations/legacy-local-storage-data/convert-edit-post-panels.d.ts.map +1 -0
  80. package/build-types/migrations/legacy-local-storage-data/index.d.ts +22 -0
  81. package/build-types/migrations/legacy-local-storage-data/index.d.ts.map +1 -0
  82. package/build-types/migrations/legacy-local-storage-data/move-feature-preferences.d.ts +60 -0
  83. package/build-types/migrations/legacy-local-storage-data/move-feature-preferences.d.ts.map +1 -0
  84. package/build-types/migrations/legacy-local-storage-data/move-individual-preference.d.ts +42 -0
  85. package/build-types/migrations/legacy-local-storage-data/move-individual-preference.d.ts.map +1 -0
  86. package/build-types/migrations/legacy-local-storage-data/move-interface-enable-items.d.ts +50 -0
  87. package/build-types/migrations/legacy-local-storage-data/move-interface-enable-items.d.ts.map +1 -0
  88. package/build-types/migrations/legacy-local-storage-data/move-third-party-feature-preferences.d.ts +44 -0
  89. package/build-types/migrations/legacy-local-storage-data/move-third-party-feature-preferences.d.ts.map +1 -0
  90. package/build-types/migrations/legacy-local-storage-data/types.d.ts +60 -0
  91. package/build-types/migrations/legacy-local-storage-data/types.d.ts.map +1 -0
  92. package/build-types/migrations/preferences-package-data/convert-complementary-areas.d.ts +5 -0
  93. package/build-types/migrations/preferences-package-data/convert-complementary-areas.d.ts.map +1 -0
  94. package/build-types/migrations/preferences-package-data/convert-editor-settings.d.ts +5 -0
  95. package/build-types/migrations/preferences-package-data/convert-editor-settings.d.ts.map +1 -0
  96. package/build-types/migrations/preferences-package-data/index.d.ts +5 -0
  97. package/build-types/migrations/preferences-package-data/index.d.ts.map +1 -0
  98. package/build-types/types.d.ts +40 -0
  99. package/build-types/types.d.ts.map +1 -0
  100. package/package.json +8 -7
  101. package/src/create/{debounce-async.js → debounce-async.ts} +13 -10
  102. package/src/create/index.ts +95 -0
  103. package/src/create/test/{debounce-async.js → debounce-async.ts} +7 -3
  104. package/src/create/test/{index.jsdom.test.js → index.jsdom.test.ts} +52 -4
  105. package/src/get-newest-data.ts +50 -0
  106. package/src/index.ts +40 -0
  107. package/src/is-preferences-data.ts +15 -0
  108. package/src/local-storage.ts +30 -0
  109. package/src/migrations/legacy-local-storage-data/{convert-edit-post-panels.js → convert-edit-post-panels.ts} +13 -5
  110. package/src/migrations/legacy-local-storage-data/{index.js → index.ts} +22 -15
  111. package/src/migrations/legacy-local-storage-data/{move-feature-preferences.js → move-feature-preferences.ts} +13 -7
  112. package/src/migrations/legacy-local-storage-data/{move-individual-preference.js → move-individual-preference.ts} +15 -12
  113. package/src/migrations/legacy-local-storage-data/{move-interface-enable-items.js → move-interface-enable-items.ts} +6 -2
  114. package/src/migrations/legacy-local-storage-data/{move-third-party-feature-preferences.js → move-third-party-feature-preferences.ts} +8 -4
  115. package/src/migrations/legacy-local-storage-data/test/{index.js → index.ts} +11 -0
  116. package/src/migrations/legacy-local-storage-data/types.ts +66 -0
  117. package/src/migrations/preferences-package-data/convert-complementary-areas.ts +21 -0
  118. package/src/migrations/preferences-package-data/convert-editor-settings.ts +58 -0
  119. package/src/migrations/preferences-package-data/{index.js → index.ts} +4 -1
  120. package/src/migrations/preferences-package-data/test/{convert-complementary-areas.js → convert-complementary-areas.ts} +10 -0
  121. package/src/migrations/preferences-package-data/test/convert-editor-settings.ts +58 -0
  122. package/src/test/index.jsdom.test.ts +97 -0
  123. package/src/test/local-storage.jsdom.test.ts +50 -0
  124. package/src/types.ts +43 -0
  125. package/CHANGELOG.md +0 -228
  126. package/src/create/index.js +0 -105
  127. package/src/index.js +0 -47
  128. package/src/migrations/preferences-package-data/convert-complementary-areas.js +0 -16
  129. package/src/migrations/preferences-package-data/convert-editor-settings.js +0 -48
  130. package/src/migrations/preferences-package-data/test/convert-editor-settings.js +0 -20
  131. /package/src/migrations/legacy-local-storage-data/test/{convert-edit-post-panels.js → convert-edit-post-panels.ts} +0 -0
  132. /package/src/migrations/legacy-local-storage-data/test/{move-feature-preferences.js → move-feature-preferences.ts} +0 -0
  133. /package/src/migrations/legacy-local-storage-data/test/{move-individual-preference.js → move-individual-preference.ts} +0 -0
  134. /package/src/migrations/legacy-local-storage-data/test/{move-interface-enable-items.js → move-interface-enable-items.ts} +0 -0
  135. /package/src/migrations/legacy-local-storage-data/test/{move-third-party-feature-preferences.js → move-third-party-feature-preferences.ts} +0 -0
  136. /package/src/migrations/preferences-package-data/test/{index.js → index.ts} +0 -0
@@ -1,7 +1 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/index.js"],
4
- "sourcesContent": ["import create from './create';\nimport convertLegacyLocalStorageData from './migrations/legacy-local-storage-data';\nimport convertPreferencesPackageData from './migrations/preferences-package-data';\n\nexport { create };\n\n/**\n * Creates the persistence layer with preloaded data.\n *\n * It prioritizes any data from the server, but falls back first to localStorage\n * restore data, and then to any legacy data.\n *\n * This function is used internally by WordPress in an inline script, so\n * prefixed with `__unstable`.\n *\n * @param {Object} serverData Preferences data preloaded from the server.\n * @param {string} userId The user id.\n *\n * @return {Object} The persistence layer initialized with the preloaded data.\n */\nexport function __unstableCreatePersistenceLayer( serverData, userId ) {\n\tconst localStorageRestoreKey = `WP_PREFERENCES_USER_${ userId }`;\n\tconst localData = JSON.parse(\n\t\twindow.localStorage.getItem( localStorageRestoreKey )\n\t);\n\n\t// Date parse returns NaN for invalid input. Coerce anything invalid\n\t// into a conveniently comparable zero.\n\tconst serverModified =\n\t\tDate.parse( serverData && serverData._modified ) || 0;\n\tconst localModified = Date.parse( localData && localData._modified ) || 0;\n\n\tlet preloadedData;\n\tif ( serverData && serverModified >= localModified ) {\n\t\tpreloadedData = convertPreferencesPackageData( serverData );\n\t} else if ( localData ) {\n\t\tpreloadedData = convertPreferencesPackageData( localData );\n\t} else {\n\t\t// Check if there is data in the legacy format from the old persistence system.\n\t\tpreloadedData = convertLegacyLocalStorageData( userId );\n\t}\n\n\treturn create( {\n\t\tpreloadedData,\n\t\tlocalStorageRestoreKey,\n\t} );\n}\n"],
5
- "mappings": ";AAAA,OAAO,YAAY;AACnB,OAAO,mCAAmC;AAC1C,OAAO,mCAAmC;AAkBnC,SAAS,iCAAkC,YAAY,QAAS;AACtE,QAAM,yBAAyB,uBAAwB,MAAO;AAC9D,QAAM,YAAY,KAAK;AAAA,IACtB,OAAO,aAAa,QAAS,sBAAuB;AAAA,EACrD;AAIA,QAAM,iBACL,KAAK,MAAO,cAAc,WAAW,SAAU,KAAK;AACrD,QAAM,gBAAgB,KAAK,MAAO,aAAa,UAAU,SAAU,KAAK;AAExE,MAAI;AACJ,MAAK,cAAc,kBAAkB,eAAgB;AACpD,oBAAgB,8BAA+B,UAAW;AAAA,EAC3D,WAAY,WAAY;AACvB,oBAAgB,8BAA+B,SAAU;AAAA,EAC1D,OAAO;AAEN,oBAAgB,8BAA+B,MAAO;AAAA,EACvD;AAEA,SAAO,OAAQ;AAAA,IACd;AAAA,IACA;AAAA,EACD,CAAE;AACH;",
6
- "names": []
7
- }
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import create from './create';\nimport convertLegacyLocalStorageData from './migrations/legacy-local-storage-data';\nimport getNewestData from './get-newest-data';\nimport type { PersistenceLayer } from './types';\n\nexport { create };\n\nexport type * from './types';\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 serverData Preferences data preloaded from the server.\n * @param userId The user id.\n *\n * @return The persistence layer initialized with the preloaded data.\n */\nexport function __unstableCreatePersistenceLayer(\n\tserverData: unknown,\n\tuserId: string | number\n): PersistenceLayer {\n\tconst localStorageRestoreKey = `WP_PREFERENCES_USER_${ userId }`;\n\treturn create( {\n\t\t/*\n\t\t * Fall back to data in the legacy format from the old persistence system.\n\t\t * The server data is already preloaded, so fetching it again is redundant.\n\t\t */\n\t\tpreloadedData:\n\t\t\tgetNewestData( serverData, localStorageRestoreKey ) ??\n\t\t\tconvertLegacyLocalStorageData( userId ) ??\n\t\t\t{},\n\t\tlocalStorageRestoreKey,\n\t} );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAuBA,SAAgB,iCACf,YACA,QACmB;CACnB,MAAM,yBAAyB,uBAAwB;CACvD,OAAO,OAAQ;EAKd,eACC,cAAe,YAAY,sBAAuB,KAClD,8BAA+B,MAAO,KACtC,CAAC;EACF;CACD,CAAE;AACH"}
@@ -0,0 +1,15 @@
1
+ // packages/preferences-persistence/src/is-preferences-data.ts
2
+ /**
3
+ * Checks whether persisted data is a preferences object. Empty user meta
4
+ * reaches the client as `[]` or `''`, which must not be used as preferences.
5
+ *
6
+ * @param data The persisted data.
7
+ *
8
+ * @return Whether the data is a preferences object.
9
+ */
10
+ function isPreferencesData(data) {
11
+ return !!data && typeof data === "object" && !Array.isArray(data);
12
+ }
13
+ export { isPreferencesData as default };
14
+
15
+ //# sourceMappingURL=is-preferences-data.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-preferences-data.mjs","names":[],"sources":["../src/is-preferences-data.ts"],"sourcesContent":["import type { PreferencesData } from './types';\n\n/**\n * Checks whether persisted data is a preferences object. Empty user meta\n * reaches the client as `[]` or `''`, which must not be used as preferences.\n *\n * @param data The persisted data.\n *\n * @return Whether the data is a preferences object.\n */\nexport default function isPreferencesData(\n\tdata: unknown\n): data is PreferencesData {\n\treturn !! data && typeof data === 'object' && ! Array.isArray( data );\n}\n"],"mappings":";;;;;;;;;AAUA,SAAwB,kBACvB,MAC0B;CAC1B,OAAO,CAAC,CAAE,QAAQ,OAAO,SAAS,YAAY,CAAE,MAAM,QAAS,IAAK;AACrE"}
@@ -0,0 +1,33 @@
1
+ // packages/preferences-persistence/src/local-storage.ts
2
+ /**
3
+ * Reads and parses a JSON value from local storage.
4
+ *
5
+ * @param key The local storage key.
6
+ *
7
+ * @return The parsed value, or `null` when the value is missing or invalid,
8
+ * or local storage is unavailable.
9
+ */
10
+ function readStoredJSON(key) {
11
+ try {
12
+ const value = window.localStorage.getItem(key);
13
+ return value === null ? null : JSON.parse(value);
14
+ } catch {
15
+ return null;
16
+ }
17
+ }
18
+ /**
19
+ * Stores a value in local storage as JSON.
20
+ *
21
+ * Failures, such as blocked storage or an exceeded quota, are ignored.
22
+ *
23
+ * @param key The local storage key.
24
+ * @param value The value to store.
25
+ */
26
+ function writeStoredJSON(key, value) {
27
+ try {
28
+ window.localStorage.setItem(key, JSON.stringify(value));
29
+ } catch {}
30
+ }
31
+ export { readStoredJSON, writeStoredJSON };
32
+
33
+ //# sourceMappingURL=local-storage.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"local-storage.mjs","names":[],"sources":["../src/local-storage.ts"],"sourcesContent":["/**\n * Reads and parses a JSON value from local storage.\n *\n * @param key The local storage key.\n *\n * @return The parsed value, or `null` when the value is missing or invalid,\n * or local storage is unavailable.\n */\nexport function readStoredJSON( key: string ): unknown {\n\ttry {\n\t\tconst value = window.localStorage.getItem( key );\n\t\treturn value === null ? null : JSON.parse( value );\n\t} catch {\n\t\treturn null;\n\t}\n}\n\n/**\n * Stores a value in local storage as JSON.\n *\n * Failures, such as blocked storage or an exceeded quota, are ignored.\n *\n * @param key The local storage key.\n * @param value The value to store.\n */\nexport function writeStoredJSON( key: string, value: unknown ) {\n\ttry {\n\t\twindow.localStorage.setItem( key, JSON.stringify( value ) );\n\t} catch {}\n}\n"],"mappings":";;;;;;;;;AAQA,SAAgB,eAAgB,KAAuB;CACtD,IAAI;EACH,MAAM,QAAQ,OAAO,aAAa,QAAS,GAAI;EAC/C,OAAO,UAAU,OAAO,OAAO,KAAK,MAAO,KAAM;CAClD,QAAQ;EACP,OAAO;CACR;AACD;;;;;;;;;AAUA,SAAgB,gBAAiB,KAAa,OAAiB;CAC9D,IAAI;EACH,OAAO,aAAa,QAAS,KAAK,KAAK,UAAW,KAAM,CAAE;CAC3D,QAAQ,CAAC;AACV"}
@@ -1,21 +1,47 @@
1
- // packages/preferences-persistence/src/migrations/legacy-local-storage-data/convert-edit-post-panels.js
1
+ // packages/preferences-persistence/src/migrations/legacy-local-storage-data/convert-edit-post-panels.ts
2
+ /**
3
+ * Convert the post editor's panels state from:
4
+ * ```
5
+ * {
6
+ * panels: {
7
+ * tags: {
8
+ * enabled: true,
9
+ * opened: true,
10
+ * },
11
+ * permalinks: {
12
+ * enabled: false,
13
+ * opened: false,
14
+ * },
15
+ * },
16
+ * }
17
+ * ```
18
+ *
19
+ * to a new, more concise data structure:
20
+ * {
21
+ * inactivePanels: [
22
+ * 'permalinks',
23
+ * ],
24
+ * openPanels: [
25
+ * 'tags',
26
+ * ],
27
+ * }
28
+ *
29
+ * @param preferences A preferences object.
30
+ *
31
+ * @return The converted data.
32
+ */
2
33
  function convertEditPostPanels(preferences) {
3
- const panels = preferences?.panels ?? {};
4
- return Object.keys(panels).reduce(
5
- (convertedData, panelName) => {
6
- const panel = panels[panelName];
7
- if (panel?.enabled === false) {
8
- convertedData.inactivePanels.push(panelName);
9
- }
10
- if (panel?.opened === true) {
11
- convertedData.openPanels.push(panelName);
12
- }
13
- return convertedData;
14
- },
15
- { inactivePanels: [], openPanels: [] }
16
- );
34
+ const panels = preferences?.panels ?? {};
35
+ return Object.keys(panels).reduce((convertedData, panelName) => {
36
+ const panel = panels[panelName];
37
+ if (panel?.enabled === false) convertedData.inactivePanels.push(panelName);
38
+ if (panel?.opened === true) convertedData.openPanels.push(panelName);
39
+ return convertedData;
40
+ }, {
41
+ inactivePanels: [],
42
+ openPanels: []
43
+ });
17
44
  }
18
- export {
19
- convertEditPostPanels as default
20
- };
21
- //# sourceMappingURL=convert-edit-post-panels.mjs.map
45
+ export { convertEditPostPanels as default };
46
+
47
+ //# sourceMappingURL=convert-edit-post-panels.mjs.map
@@ -1,7 +1 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../src/migrations/legacy-local-storage-data/convert-edit-post-panels.js"],
4
- "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"],
5
- "mappings": ";AA+Be,SAAR,sBAAwC,aAAc;AAC5D,QAAM,SAAS,aAAa,UAAU,CAAC;AACvC,SAAO,OAAO,KAAM,MAAO,EAAE;AAAA,IAC5B,CAAE,eAAe,cAAe;AAC/B,YAAM,QAAQ,OAAQ,SAAU;AAEhC,UAAK,OAAO,YAAY,OAAQ;AAC/B,sBAAc,eAAe,KAAM,SAAU;AAAA,MAC9C;AAEA,UAAK,OAAO,WAAW,MAAO;AAC7B,sBAAc,WAAW,KAAM,SAAU;AAAA,MAC1C;AAEA,aAAO;AAAA,IACR;AAAA,IACA,EAAE,gBAAgB,CAAC,GAAG,YAAY,CAAC,EAAE;AAAA,EACtC;AACD;",
6
- "names": []
7
- }
1
+ {"version":3,"file":"convert-edit-post-panels.mjs","names":[],"sources":["../../../src/migrations/legacy-local-storage-data/convert-edit-post-panels.ts"],"sourcesContent":["import type { ScopePreferences } from '../../types';\nimport type { EditPostPanels, LegacyEditPostPanel } from './types';\n\n/**\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 preferences A preferences object.\n *\n * @return The converted data.\n */\nexport default function convertEditPostPanels(\n\tpreferences: ScopePreferences\n): EditPostPanels {\n\tconst panels = ( preferences?.panels ?? {} ) as Record<\n\t\tstring,\n\t\tLegacyEditPostPanel | undefined\n\t>;\n\treturn Object.keys( panels ).reduce< EditPostPanels >(\n\t\t( convertedData, panelName ) => {\n\t\t\tconst panel = panels[ panelName ];\n\n\t\t\tif ( panel?.enabled === false ) {\n\t\t\t\tconvertedData.inactivePanels.push( panelName );\n\t\t\t}\n\n\t\t\tif ( panel?.opened === true ) {\n\t\t\t\tconvertedData.openPanels.push( panelName );\n\t\t\t}\n\n\t\t\treturn convertedData;\n\t\t},\n\t\t{ inactivePanels: [], openPanels: [] }\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,SAAwB,sBACvB,aACiB;CACjB,MAAM,SAAW,aAAa,UAAU,CAAC;CAIzC,OAAO,OAAO,KAAM,MAAO,CAAC,CAAC,QAC1B,eAAe,cAAe;EAC/B,MAAM,QAAQ,OAAQ;EAEtB,IAAK,OAAO,YAAY,OACvB,cAAc,eAAe,KAAM,SAAU;EAG9C,IAAK,OAAO,WAAW,MACtB,cAAc,WAAW,KAAM,SAAU;EAG1C,OAAO;CACR,GACA;EAAE,gBAAgB,CAAC;EAAG,YAAY,CAAC;CAAE,CACtC;AACD"}
@@ -1,63 +1,76 @@
1
- // packages/preferences-persistence/src/migrations/legacy-local-storage-data/index.js
2
1
  import moveFeaturePreferences from "./move-feature-preferences.mjs";
3
2
  import moveThirdPartyFeaturePreferences from "./move-third-party-feature-preferences.mjs";
4
3
  import moveIndividualPreference from "./move-individual-preference.mjs";
5
4
  import moveInterfaceEnableItems from "./move-interface-enable-items.mjs";
6
5
  import convertEditPostPanels from "./convert-edit-post-panels.mjs";
6
+ import isPreferencesData from "../../is-preferences-data.mjs";
7
+ import { readStoredJSON } from "../../local-storage.mjs";
8
+ // packages/preferences-persistence/src/migrations/legacy-local-storage-data/index.ts
9
+ /**
10
+ * Gets the legacy local storage data for a given user.
11
+ *
12
+ * @param userId The user id.
13
+ *
14
+ * @return The local storage data.
15
+ */
7
16
  function getLegacyData(userId) {
8
- const key = `WP_DATA_USER_${userId}`;
9
- const unparsedData = window.localStorage.getItem(key);
10
- return JSON.parse(unparsedData);
17
+ return readStoredJSON(`WP_DATA_USER_${userId}`);
11
18
  }
19
+ /**
20
+ * Converts data from the old `@wordpress/data` package format.
21
+ *
22
+ * @param data The legacy data in its original format.
23
+ *
24
+ * @return The converted data or `undefined` if there was
25
+ * nothing to convert.
26
+ */
12
27
  function convertLegacyData(data) {
13
- if (!data) {
14
- return;
15
- }
16
- data = moveFeaturePreferences(data, "core/edit-widgets");
17
- data = moveFeaturePreferences(data, "core/customize-widgets");
18
- data = moveFeaturePreferences(data, "core/edit-post");
19
- data = moveFeaturePreferences(data, "core/edit-site");
20
- data = moveThirdPartyFeaturePreferences(data);
21
- data = moveInterfaceEnableItems(data);
22
- data = moveIndividualPreference(
23
- data,
24
- { from: "core/edit-post", to: "core/edit-post" },
25
- "hiddenBlockTypes"
26
- );
27
- data = moveIndividualPreference(
28
- data,
29
- { from: "core/edit-post", to: "core/edit-post" },
30
- "editorMode"
31
- );
32
- data = moveIndividualPreference(
33
- data,
34
- { from: "core/edit-post", to: "core/edit-post" },
35
- "panels",
36
- convertEditPostPanels
37
- );
38
- data = moveIndividualPreference(
39
- data,
40
- { from: "core/editor", to: "core" },
41
- "isPublishSidebarEnabled"
42
- );
43
- data = moveIndividualPreference(
44
- data,
45
- { from: "core/edit-post", to: "core" },
46
- "isPublishSidebarEnabled"
47
- );
48
- data = moveIndividualPreference(
49
- data,
50
- { from: "core/edit-site", to: "core/edit-site" },
51
- "editorMode"
52
- );
53
- return data?.["core/preferences"]?.preferences;
28
+ if (!data) return;
29
+ data = moveFeaturePreferences(data, "core/edit-widgets");
30
+ data = moveFeaturePreferences(data, "core/customize-widgets");
31
+ data = moveFeaturePreferences(data, "core/edit-post");
32
+ data = moveFeaturePreferences(data, "core/edit-site");
33
+ data = moveThirdPartyFeaturePreferences(data);
34
+ data = moveInterfaceEnableItems(data);
35
+ data = moveIndividualPreference(data, {
36
+ from: "core/edit-post",
37
+ to: "core/edit-post"
38
+ }, "hiddenBlockTypes");
39
+ data = moveIndividualPreference(data, {
40
+ from: "core/edit-post",
41
+ to: "core/edit-post"
42
+ }, "editorMode");
43
+ data = moveIndividualPreference(data, {
44
+ from: "core/edit-post",
45
+ to: "core/edit-post"
46
+ }, "panels", convertEditPostPanels);
47
+ data = moveIndividualPreference(data, {
48
+ from: "core/editor",
49
+ to: "core"
50
+ }, "isPublishSidebarEnabled");
51
+ data = moveIndividualPreference(data, {
52
+ from: "core/edit-post",
53
+ to: "core"
54
+ }, "isPublishSidebarEnabled");
55
+ data = moveIndividualPreference(data, {
56
+ from: "core/edit-site",
57
+ to: "core/edit-site"
58
+ }, "editorMode");
59
+ const preferences = data?.["core/preferences"]?.preferences;
60
+ return isPreferencesData(preferences) ? preferences : void 0;
54
61
  }
62
+ /**
63
+ * Gets the legacy local storage data for the given user and returns the
64
+ * data converted to the new format.
65
+ *
66
+ * @param userId The user id.
67
+ *
68
+ * @return The converted data or undefined if no local
69
+ * storage data could be found.
70
+ */
55
71
  function convertLegacyLocalStorageData(userId) {
56
- const data = getLegacyData(userId);
57
- return convertLegacyData(data);
72
+ return convertLegacyData(getLegacyData(userId));
58
73
  }
59
- export {
60
- convertLegacyData,
61
- convertLegacyLocalStorageData as default
62
- };
63
- //# sourceMappingURL=index.mjs.map
74
+ export { convertLegacyData, convertLegacyLocalStorageData as default };
75
+
76
+ //# sourceMappingURL=index.mjs.map
@@ -1,7 +1 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../src/migrations/legacy-local-storage-data/index.js"],
4
- "sourcesContent": ["import 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'panels',\n\t\tconvertEditPostPanels\n\t);\n\tdata = moveIndividualPreference(\n\t\tdata,\n\t\t{ from: 'core/editor', to: 'core' },\n\t\t'isPublishSidebarEnabled'\n\t);\n\tdata = moveIndividualPreference(\n\t\tdata,\n\t\t{ from: 'core/edit-post', to: 'core' },\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"],
5
- "mappings": ";AAAA,OAAO,4BAA4B;AACnC,OAAO,sCAAsC;AAC7C,OAAO,8BAA8B;AACrC,OAAO,8BAA8B;AACrC,OAAO,2BAA2B;AASlC,SAAS,cAAe,QAAS;AAChC,QAAM,MAAM,gBAAiB,MAAO;AACpC,QAAM,eAAe,OAAO,aAAa,QAAS,GAAI;AACtD,SAAO,KAAK,MAAO,YAAa;AACjC;AAUO,SAAS,kBAAmB,MAAO;AACzC,MAAK,CAAE,MAAO;AACb;AAAA,EACD;AAIA,SAAO,uBAAwB,MAAM,mBAAoB;AACzD,SAAO,uBAAwB,MAAM,wBAAyB;AAC9D,SAAO,uBAAwB,MAAM,gBAAiB;AACtD,SAAO,uBAAwB,MAAM,gBAAiB;AAItD,SAAO,iCAAkC,IAAK;AAI9C,SAAO,yBAA0B,IAAK;AAItC,SAAO;AAAA,IACN;AAAA,IACA,EAAE,MAAM,kBAAkB,IAAI,iBAAiB;AAAA,IAC/C;AAAA,EACD;AACA,SAAO;AAAA,IACN;AAAA,IACA,EAAE,MAAM,kBAAkB,IAAI,iBAAiB;AAAA,IAC/C;AAAA,EACD;AACA,SAAO;AAAA,IACN;AAAA,IACA,EAAE,MAAM,kBAAkB,IAAI,iBAAiB;AAAA,IAC/C;AAAA,IACA;AAAA,EACD;AACA,SAAO;AAAA,IACN;AAAA,IACA,EAAE,MAAM,eAAe,IAAI,OAAO;AAAA,IAClC;AAAA,EACD;AACA,SAAO;AAAA,IACN;AAAA,IACA,EAAE,MAAM,kBAAkB,IAAI,OAAO;AAAA,IACrC;AAAA,EACD;AACA,SAAO;AAAA,IACN;AAAA,IACA,EAAE,MAAM,kBAAkB,IAAI,iBAAiB;AAAA,IAC/C;AAAA,EACD;AAIA,SAAO,OAAQ,kBAAmB,GAAG;AACtC;AAWe,SAAR,8BAAgD,QAAS;AAC/D,QAAM,OAAO,cAAe,MAAO;AACnC,SAAO,kBAAmB,IAAK;AAChC;",
6
- "names": []
7
- }
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../../src/migrations/legacy-local-storage-data/index.ts"],"sourcesContent":["import 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';\nimport isPreferencesData from '../../is-preferences-data';\nimport { readStoredJSON } from '../../local-storage';\nimport type { ScopedPreferences } from '../../types';\nimport type { LegacyData } from './types';\n\n/**\n * Gets the legacy local storage data for a given user.\n *\n * @param userId The user id.\n *\n * @return The local storage data.\n */\nfunction getLegacyData( userId: string | number ): LegacyData | null {\n\treturn readStoredJSON( `WP_DATA_USER_${ userId }` ) as LegacyData | null;\n}\n\n/**\n * Converts data from the old `@wordpress/data` package format.\n *\n * @param data The legacy data in its original format.\n *\n * @return The converted data or `undefined` if there was\n * nothing to convert.\n */\nexport function convertLegacyData(\n\tdata: LegacyData | null | undefined\n): ScopedPreferences | undefined {\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'panels',\n\t\tconvertEditPostPanels\n\t);\n\tdata = moveIndividualPreference(\n\t\tdata,\n\t\t{ from: 'core/editor', to: 'core' },\n\t\t'isPublishSidebarEnabled'\n\t);\n\tdata = moveIndividualPreference(\n\t\tdata,\n\t\t{ from: 'core/edit-post', to: 'core' },\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\tconst preferences = data?.[ 'core/preferences' ]?.preferences;\n\treturn isPreferencesData( preferences ) ? preferences : undefined;\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 userId The user id.\n *\n * @return The converted data or undefined if no local\n * storage data could be found.\n */\nexport default function convertLegacyLocalStorageData(\n\tuserId: string | number\n): ScopedPreferences | undefined {\n\tconst data = getLegacyData( userId );\n\treturn convertLegacyData( data );\n}\n"],"mappings":";;;;;;;;;;;;;;;AAiBA,SAAS,cAAe,QAA6C;CACpE,OAAO,eAAgB,gBAAiB,QAAU;AACnD;;;;;;;;;AAUA,SAAgB,kBACf,MACgC;CAChC,IAAK,CAAE,MACN;CAKD,OAAO,uBAAwB,MAAM,mBAAoB;CACzD,OAAO,uBAAwB,MAAM,wBAAyB;CAC9D,OAAO,uBAAwB,MAAM,gBAAiB;CACtD,OAAO,uBAAwB,MAAM,gBAAiB;CAItD,OAAO,iCAAkC,IAAK;CAI9C,OAAO,yBAA0B,IAAK;CAItC,OAAO,yBACN,MACA;EAAE,MAAM;EAAkB,IAAI;CAAiB,GAC/C,kBACD;CACA,OAAO,yBACN,MACA;EAAE,MAAM;EAAkB,IAAI;CAAiB,GAC/C,YACD;CACA,OAAO,yBACN,MACA;EAAE,MAAM;EAAkB,IAAI;CAAiB,GAC/C,UACA,qBACD;CACA,OAAO,yBACN,MACA;EAAE,MAAM;EAAe,IAAI;CAAO,GAClC,yBACD;CACA,OAAO,yBACN,MACA;EAAE,MAAM;EAAkB,IAAI;CAAO,GACrC,yBACD;CACA,OAAO,yBACN,MACA;EAAE,MAAM;EAAkB,IAAI;CAAiB,GAC/C,YACD;CAIA,MAAM,cAAc,OAAQ,mBAAoB,EAAE;CAClD,OAAO,kBAAmB,WAAY,IAAI,cAAc,KAAA;AACzD;;;;;;;;;;AAWA,SAAwB,8BACvB,QACgC;CAEhC,OAAO,kBADM,cAAe,MACC,CAAE;AAChC"}
@@ -1,60 +1,104 @@
1
- // packages/preferences-persistence/src/migrations/legacy-local-storage-data/move-feature-preferences.js
1
+ // packages/preferences-persistence/src/migrations/legacy-local-storage-data/move-feature-preferences.ts
2
+ /**
3
+ * Move the 'features' object in local storage from the sourceStoreName to the
4
+ * preferences store data structure.
5
+ *
6
+ * Previously, editors used a data structure like this for feature preferences:
7
+ * ```js
8
+ * {
9
+ * 'core/edit-post': {
10
+ * preferences: {
11
+ * features; {
12
+ * topToolbar: true,
13
+ * // ... other boolean 'feature' preferences
14
+ * },
15
+ * },
16
+ * },
17
+ * }
18
+ * ```
19
+ *
20
+ * And for a while these feature preferences lived in the interface package:
21
+ * ```js
22
+ * {
23
+ * 'core/interface': {
24
+ * preferences: {
25
+ * features: {
26
+ * 'core/edit-post': {
27
+ * topToolbar: true
28
+ * }
29
+ * }
30
+ * }
31
+ * }
32
+ * }
33
+ * ```
34
+ *
35
+ * In the preferences store, 'features' aren't considered special, they're
36
+ * merged to the root level of the scope along with other preferences:
37
+ * ```js
38
+ * {
39
+ * 'core/preferences': {
40
+ * preferences: {
41
+ * 'core/edit-post': {
42
+ * topToolbar: true,
43
+ * // ... any other preferences.
44
+ * }
45
+ * }
46
+ * }
47
+ * }
48
+ * ```
49
+ *
50
+ * This function handles moving from either the source store or the interface
51
+ * store to the preferences data structure.
52
+ *
53
+ * @param state The state before migration.
54
+ * @param sourceStoreName The name of the store that has persisted
55
+ * preferences to migrate to the preferences
56
+ * package.
57
+ * @return The migrated state
58
+ */
2
59
  function moveFeaturePreferences(state, sourceStoreName) {
3
- const preferencesStoreName = "core/preferences";
4
- const interfaceStoreName = "core/interface";
5
- const interfaceFeatures = state?.[interfaceStoreName]?.preferences?.features?.[sourceStoreName];
6
- const sourceFeatures = state?.[sourceStoreName]?.preferences?.features;
7
- const featuresToMigrate = interfaceFeatures ? interfaceFeatures : sourceFeatures;
8
- if (!featuresToMigrate) {
9
- return state;
10
- }
11
- const existingPreferences = state?.[preferencesStoreName]?.preferences;
12
- if (existingPreferences?.[sourceStoreName]) {
13
- return state;
14
- }
15
- let updatedInterfaceState;
16
- if (interfaceFeatures) {
17
- const otherInterfaceState = state?.[interfaceStoreName];
18
- const otherInterfaceScopes = state?.[interfaceStoreName]?.preferences?.features;
19
- updatedInterfaceState = {
20
- [interfaceStoreName]: {
21
- ...otherInterfaceState,
22
- preferences: {
23
- features: {
24
- ...otherInterfaceScopes,
25
- [sourceStoreName]: void 0
26
- }
27
- }
28
- }
29
- };
30
- }
31
- let updatedSourceState;
32
- if (sourceFeatures) {
33
- const otherSourceState = state?.[sourceStoreName];
34
- const sourcePreferences = state?.[sourceStoreName]?.preferences;
35
- updatedSourceState = {
36
- [sourceStoreName]: {
37
- ...otherSourceState,
38
- preferences: {
39
- ...sourcePreferences,
40
- features: void 0
41
- }
42
- }
43
- };
44
- }
45
- return {
46
- ...state,
47
- [preferencesStoreName]: {
48
- preferences: {
49
- ...existingPreferences,
50
- [sourceStoreName]: featuresToMigrate
51
- }
52
- },
53
- ...updatedInterfaceState,
54
- ...updatedSourceState
55
- };
60
+ const preferencesStoreName = "core/preferences";
61
+ const interfaceStoreName = "core/interface";
62
+ const interfaceFeatures = state?.[interfaceStoreName]?.preferences?.features?.[sourceStoreName];
63
+ const sourceFeatures = state?.[sourceStoreName]?.preferences?.features;
64
+ const featuresToMigrate = interfaceFeatures ? interfaceFeatures : sourceFeatures;
65
+ if (!featuresToMigrate) return state;
66
+ const existingPreferences = state?.[preferencesStoreName]?.preferences;
67
+ if (existingPreferences?.[sourceStoreName]) return state;
68
+ let updatedInterfaceState;
69
+ if (interfaceFeatures) {
70
+ const otherInterfaceState = state?.[interfaceStoreName];
71
+ const otherInterfaceScopes = state?.[interfaceStoreName]?.preferences?.features;
72
+ updatedInterfaceState = { [interfaceStoreName]: {
73
+ ...otherInterfaceState,
74
+ preferences: { features: {
75
+ ...otherInterfaceScopes,
76
+ [sourceStoreName]: void 0
77
+ } }
78
+ } };
79
+ }
80
+ let updatedSourceState;
81
+ if (sourceFeatures) {
82
+ const otherSourceState = state?.[sourceStoreName];
83
+ const sourcePreferences = state?.[sourceStoreName]?.preferences;
84
+ updatedSourceState = { [sourceStoreName]: {
85
+ ...otherSourceState,
86
+ preferences: {
87
+ ...sourcePreferences,
88
+ features: void 0
89
+ }
90
+ } };
91
+ }
92
+ return {
93
+ ...state,
94
+ [preferencesStoreName]: { preferences: {
95
+ ...existingPreferences,
96
+ [sourceStoreName]: featuresToMigrate
97
+ } },
98
+ ...updatedInterfaceState,
99
+ ...updatedSourceState
100
+ };
56
101
  }
57
- export {
58
- moveFeaturePreferences as default
59
- };
60
- //# sourceMappingURL=move-feature-preferences.mjs.map
102
+ export { moveFeaturePreferences as default };
103
+
104
+ //# sourceMappingURL=move-feature-preferences.mjs.map
@@ -1,7 +1 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../src/migrations/legacy-local-storage-data/move-feature-preferences.js"],
4
- "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"],
5
- "mappings": ";AAyDe,SAAR,uBAAyC,OAAO,iBAAkB;AACxE,QAAM,uBAAuB;AAC7B,QAAM,qBAAqB;AAM3B,QAAM,oBACL,QAAS,kBAAmB,GAAG,aAAa,WAC3C,eACD;AACD,QAAM,iBAAiB,QAAS,eAAgB,GAAG,aAAa;AAChE,QAAM,oBAAoB,oBACvB,oBACA;AAEH,MAAK,CAAE,mBAAoB;AAC1B,WAAO;AAAA,EACR;AAEA,QAAM,sBAAsB,QAAS,oBAAqB,GAAG;AAG7D,MAAK,sBAAuB,eAAgB,GAAI;AAC/C,WAAO;AAAA,EACR;AAEA,MAAI;AACJ,MAAK,mBAAoB;AACxB,UAAM,sBAAsB,QAAS,kBAAmB;AACxD,UAAM,uBACL,QAAS,kBAAmB,GAAG,aAAa;AAE7C,4BAAwB;AAAA,MACvB,CAAE,kBAAmB,GAAG;AAAA,QACvB,GAAG;AAAA,QACH,aAAa;AAAA,UACZ,UAAU;AAAA,YACT,GAAG;AAAA,YACH,CAAE,eAAgB,GAAG;AAAA,UACtB;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD;AAEA,MAAI;AACJ,MAAK,gBAAiB;AACrB,UAAM,mBAAmB,QAAS,eAAgB;AAClD,UAAM,oBAAoB,QAAS,eAAgB,GAAG;AAEtD,yBAAqB;AAAA,MACpB,CAAE,eAAgB,GAAG;AAAA,QACpB,GAAG;AAAA,QACH,aAAa;AAAA,UACZ,GAAG;AAAA,UACH,UAAU;AAAA,QACX;AAAA,MACD;AAAA,IACD;AAAA,EACD;AAKA,SAAO;AAAA,IACN,GAAG;AAAA,IACH,CAAE,oBAAqB,GAAG;AAAA,MACzB,aAAa;AAAA,QACZ,GAAG;AAAA,QACH,CAAE,eAAgB,GAAG;AAAA,MACtB;AAAA,IACD;AAAA,IACA,GAAG;AAAA,IACH,GAAG;AAAA,EACJ;AACD;",
6
- "names": []
7
- }
1
+ {"version":3,"file":"move-feature-preferences.mjs","names":[],"sources":["../../../src/migrations/legacy-local-storage-data/move-feature-preferences.ts"],"sourcesContent":["import type { FeaturePreferences, LegacyData } from './types';\n\n/**\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 state The state before migration.\n * @param sourceStoreName The name of the store that has persisted\n * preferences to migrate to the preferences\n * package.\n * @return The migrated state\n */\nexport default function moveFeaturePreferences(\n\tstate: LegacyData,\n\tsourceStoreName: string\n): LegacyData {\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 as\n\t\tFeaturePreferences | undefined;\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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2DA,SAAwB,uBACvB,OACA,iBACa;CACb,MAAM,uBAAuB;CAC7B,MAAM,qBAAqB;CAM3B,MAAM,oBACL,QAAS,mBAAoB,EAAE,aAAa,WAC3C;CAEF,MAAM,iBAAiB,QAAS,gBAAiB,EAAE,aAAa;CAEhE,MAAM,oBAAoB,oBACvB,oBACA;CAEH,IAAK,CAAE,mBACN,OAAO;CAGR,MAAM,sBAAsB,QAAS,qBAAsB,EAAE;CAG7D,IAAK,sBAAuB,kBAC3B,OAAO;CAGR,IAAI;CACJ,IAAK,mBAAoB;EACxB,MAAM,sBAAsB,QAAS;EACrC,MAAM,uBACL,QAAS,mBAAoB,EAAE,aAAa;EAE7C,wBAAwB,GACrB,qBAAsB;GACvB,GAAG;GACH,aAAa,EACZ,UAAU;IACT,GAAG;KACD,kBAAmB,KAAA;GACtB,EACD;EACD,EACD;CACD;CAEA,IAAI;CACJ,IAAK,gBAAiB;EACrB,MAAM,mBAAmB,QAAS;EAClC,MAAM,oBAAoB,QAAS,gBAAiB,EAAE;EAEtD,qBAAqB,GAClB,kBAAmB;GACpB,GAAG;GACH,aAAa;IACZ,GAAG;IACH,UAAU,KAAA;GACX;EACD,EACD;CACD;CAKA,OAAO;EACN,GAAG;GACD,uBAAwB,EACzB,aAAa;GACZ,GAAG;IACD,kBAAmB;EACtB,EACD;EACA,GAAG;EACH,GAAG;CACJ;AACD"}
@@ -1,41 +1,68 @@
1
- // packages/preferences-persistence/src/migrations/legacy-local-storage-data/move-individual-preference.js
2
- var identity = (arg) => arg;
1
+ // packages/preferences-persistence/src/migrations/legacy-local-storage-data/move-individual-preference.ts
2
+ const identity = (arg) => arg;
3
+ /**
4
+ * Migrates an individual item inside the `preferences` object for a package's store.
5
+ *
6
+ * Previously, some packages had individual 'preferences' of any data type, and many used
7
+ * complex nested data structures. For example:
8
+ * ```js
9
+ * {
10
+ * 'core/edit-post': {
11
+ * preferences: {
12
+ * panels: {
13
+ * publish: {
14
+ * opened: true,
15
+ * enabled: true,
16
+ * }
17
+ * },
18
+ * // ...other preferences.
19
+ * },
20
+ * },
21
+ * }
22
+ *
23
+ * This function supports moving an individual preference like 'panels' above into the
24
+ * preferences package data structure.
25
+ *
26
+ * It supports moving a preference to a particular scope in the preferences store and
27
+ * optionally converting the data using a `convert` function.
28
+ *
29
+ * ```
30
+ *
31
+ * @param state The original state.
32
+ * @param migrate An options object that contains details of the migration.
33
+ * @param migrate.from The name of the store to migrate from.
34
+ * @param migrate.to The scope in the preferences store to migrate to.
35
+ * @param key The key in the preferences object to migrate.
36
+ * @param convert A function that converts preferences from one format to another.
37
+ */
3
38
  function moveIndividualPreferenceToPreferences(state, { from: sourceStoreName, to: scope }, key, convert = identity) {
4
- const preferencesStoreName = "core/preferences";
5
- const sourcePreference = state?.[sourceStoreName]?.preferences?.[key];
6
- if (sourcePreference === void 0) {
7
- return state;
8
- }
9
- const targetPreference = state?.[preferencesStoreName]?.preferences?.[scope]?.[key];
10
- if (targetPreference) {
11
- return state;
12
- }
13
- const otherScopes = state?.[preferencesStoreName]?.preferences;
14
- const otherPreferences = state?.[preferencesStoreName]?.preferences?.[scope];
15
- const otherSourceState = state?.[sourceStoreName];
16
- const allSourcePreferences = state?.[sourceStoreName]?.preferences;
17
- const convertedPreferences = convert({ [key]: sourcePreference });
18
- return {
19
- ...state,
20
- [preferencesStoreName]: {
21
- preferences: {
22
- ...otherScopes,
23
- [scope]: {
24
- ...otherPreferences,
25
- ...convertedPreferences
26
- }
27
- }
28
- },
29
- [sourceStoreName]: {
30
- ...otherSourceState,
31
- preferences: {
32
- ...allSourcePreferences,
33
- [key]: void 0
34
- }
35
- }
36
- };
39
+ const preferencesStoreName = "core/preferences";
40
+ const sourcePreference = state?.[sourceStoreName]?.preferences?.[key];
41
+ if (sourcePreference === void 0) return state;
42
+ if (state?.[preferencesStoreName]?.preferences?.[scope]?.[key]) return state;
43
+ const otherScopes = state?.[preferencesStoreName]?.preferences;
44
+ const otherPreferences = state?.[preferencesStoreName]?.preferences?.[scope];
45
+ const otherSourceState = state?.[sourceStoreName];
46
+ const allSourcePreferences = state?.[sourceStoreName]?.preferences;
47
+ const convertedPreferences = convert({ [key]: sourcePreference });
48
+ return {
49
+ ...state,
50
+ [preferencesStoreName]: { preferences: {
51
+ ...otherScopes,
52
+ [scope]: {
53
+ ...otherPreferences,
54
+ ...convertedPreferences
55
+ }
56
+ } },
57
+ [sourceStoreName]: {
58
+ ...otherSourceState,
59
+ preferences: {
60
+ ...allSourcePreferences,
61
+ [key]: void 0
62
+ }
63
+ }
64
+ };
37
65
  }
38
- export {
39
- moveIndividualPreferenceToPreferences as default
40
- };
41
- //# sourceMappingURL=move-individual-preference.mjs.map
66
+ export { moveIndividualPreferenceToPreferences as default };
67
+
68
+ //# sourceMappingURL=move-individual-preference.mjs.map