@wordpress/preferences-persistence 2.57.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/README.md +7 -7
  2. package/build/create/debounce-async.cjs +2 -1
  3. package/build/create/debounce-async.cjs.map +3 -3
  4. package/build/create/index.cjs +11 -21
  5. package/build/create/index.cjs.map +4 -4
  6. package/build/get-newest-data.cjs +53 -0
  7. package/build/get-newest-data.cjs.map +7 -0
  8. package/build/index.cjs +8 -16
  9. package/build/index.cjs.map +4 -4
  10. package/build/is-preferences-data.cjs +29 -0
  11. package/build/is-preferences-data.cjs.map +7 -0
  12. package/build/local-storage.cjs +46 -0
  13. package/build/local-storage.cjs.map +7 -0
  14. package/build/migrations/legacy-local-storage-data/convert-edit-post-panels.cjs +2 -1
  15. package/build/migrations/legacy-local-storage-data/convert-edit-post-panels.cjs.map +3 -3
  16. package/build/migrations/legacy-local-storage-data/index.cjs +7 -5
  17. package/build/migrations/legacy-local-storage-data/index.cjs.map +4 -4
  18. package/build/migrations/legacy-local-storage-data/move-feature-preferences.cjs +2 -1
  19. package/build/migrations/legacy-local-storage-data/move-feature-preferences.cjs.map +3 -3
  20. package/build/migrations/legacy-local-storage-data/move-individual-preference.cjs +2 -1
  21. package/build/migrations/legacy-local-storage-data/move-individual-preference.cjs.map +3 -3
  22. package/build/migrations/legacy-local-storage-data/move-interface-enable-items.cjs +2 -1
  23. package/build/migrations/legacy-local-storage-data/move-interface-enable-items.cjs.map +3 -3
  24. package/build/migrations/legacy-local-storage-data/move-third-party-feature-preferences.cjs +2 -1
  25. package/build/migrations/legacy-local-storage-data/move-third-party-feature-preferences.cjs.map +3 -3
  26. package/build/migrations/legacy-local-storage-data/types.cjs +19 -0
  27. package/build/migrations/legacy-local-storage-data/types.cjs.map +7 -0
  28. package/build/migrations/preferences-package-data/convert-complementary-areas.cjs +15 -11
  29. package/build/migrations/preferences-package-data/convert-complementary-areas.cjs.map +3 -3
  30. package/build/migrations/preferences-package-data/convert-editor-settings.cjs +30 -14
  31. package/build/migrations/preferences-package-data/convert-editor-settings.cjs.map +4 -4
  32. package/build/migrations/preferences-package-data/index.cjs +2 -1
  33. package/build/migrations/preferences-package-data/index.cjs.map +3 -3
  34. package/build/types.cjs +19 -0
  35. package/build/types.cjs.map +7 -0
  36. package/build-module/create/debounce-async.mjs +1 -1
  37. package/build-module/create/debounce-async.mjs.map +3 -3
  38. package/build-module/create/index.mjs +10 -21
  39. package/build-module/create/index.mjs.map +3 -3
  40. package/build-module/get-newest-data.mjs +22 -0
  41. package/build-module/get-newest-data.mjs.map +7 -0
  42. package/build-module/index.mjs +7 -16
  43. package/build-module/index.mjs.map +3 -3
  44. package/build-module/is-preferences-data.mjs +8 -0
  45. package/build-module/is-preferences-data.mjs.map +7 -0
  46. package/build-module/local-storage.mjs +20 -0
  47. package/build-module/local-storage.mjs.map +7 -0
  48. package/build-module/migrations/legacy-local-storage-data/convert-edit-post-panels.mjs +1 -1
  49. package/build-module/migrations/legacy-local-storage-data/convert-edit-post-panels.mjs.map +3 -3
  50. package/build-module/migrations/legacy-local-storage-data/index.mjs +6 -5
  51. package/build-module/migrations/legacy-local-storage-data/index.mjs.map +3 -3
  52. package/build-module/migrations/legacy-local-storage-data/move-feature-preferences.mjs +1 -1
  53. package/build-module/migrations/legacy-local-storage-data/move-feature-preferences.mjs.map +3 -3
  54. package/build-module/migrations/legacy-local-storage-data/move-individual-preference.mjs +1 -1
  55. package/build-module/migrations/legacy-local-storage-data/move-individual-preference.mjs.map +3 -3
  56. package/build-module/migrations/legacy-local-storage-data/move-interface-enable-items.mjs +1 -1
  57. package/build-module/migrations/legacy-local-storage-data/move-interface-enable-items.mjs.map +3 -3
  58. package/build-module/migrations/legacy-local-storage-data/move-third-party-feature-preferences.mjs +1 -1
  59. package/build-module/migrations/legacy-local-storage-data/move-third-party-feature-preferences.mjs.map +3 -3
  60. package/build-module/migrations/legacy-local-storage-data/types.mjs +1 -0
  61. package/build-module/migrations/legacy-local-storage-data/types.mjs.map +7 -0
  62. package/build-module/migrations/preferences-package-data/convert-complementary-areas.mjs +14 -11
  63. package/build-module/migrations/preferences-package-data/convert-complementary-areas.mjs.map +3 -3
  64. package/build-module/migrations/preferences-package-data/convert-editor-settings.mjs +19 -14
  65. package/build-module/migrations/preferences-package-data/convert-editor-settings.mjs.map +3 -3
  66. package/build-module/migrations/preferences-package-data/index.mjs +1 -1
  67. package/build-module/migrations/preferences-package-data/index.mjs.map +3 -3
  68. package/build-module/types.mjs +1 -0
  69. package/build-module/types.mjs.map +7 -0
  70. package/build-types/create/debounce-async.d.ts +19 -0
  71. package/build-types/create/debounce-async.d.ts.map +1 -0
  72. package/build-types/create/index.d.ts +20 -0
  73. package/build-types/create/index.d.ts.map +1 -0
  74. package/build-types/get-newest-data.d.ts +11 -0
  75. package/build-types/get-newest-data.d.ts.map +1 -0
  76. package/build-types/index.d.ts +20 -0
  77. package/build-types/index.d.ts.map +1 -0
  78. package/build-types/is-preferences-data.d.ts +11 -0
  79. package/build-types/is-preferences-data.d.ts.map +1 -0
  80. package/build-types/local-storage.d.ts +19 -0
  81. package/build-types/local-storage.d.ts.map +1 -0
  82. package/build-types/migrations/legacy-local-storage-data/convert-edit-post-panels.d.ts +35 -0
  83. package/build-types/migrations/legacy-local-storage-data/convert-edit-post-panels.d.ts.map +1 -0
  84. package/build-types/migrations/legacy-local-storage-data/index.d.ts +22 -0
  85. package/build-types/migrations/legacy-local-storage-data/index.d.ts.map +1 -0
  86. package/build-types/migrations/legacy-local-storage-data/move-feature-preferences.d.ts +60 -0
  87. package/build-types/migrations/legacy-local-storage-data/move-feature-preferences.d.ts.map +1 -0
  88. package/build-types/migrations/legacy-local-storage-data/move-individual-preference.d.ts +42 -0
  89. package/build-types/migrations/legacy-local-storage-data/move-individual-preference.d.ts.map +1 -0
  90. package/build-types/migrations/legacy-local-storage-data/move-interface-enable-items.d.ts +50 -0
  91. package/build-types/migrations/legacy-local-storage-data/move-interface-enable-items.d.ts.map +1 -0
  92. package/build-types/migrations/legacy-local-storage-data/move-third-party-feature-preferences.d.ts +44 -0
  93. package/build-types/migrations/legacy-local-storage-data/move-third-party-feature-preferences.d.ts.map +1 -0
  94. package/build-types/migrations/legacy-local-storage-data/types.d.ts +60 -0
  95. package/build-types/migrations/legacy-local-storage-data/types.d.ts.map +1 -0
  96. package/build-types/migrations/preferences-package-data/convert-complementary-areas.d.ts +5 -0
  97. package/build-types/migrations/preferences-package-data/convert-complementary-areas.d.ts.map +1 -0
  98. package/build-types/migrations/preferences-package-data/convert-editor-settings.d.ts +5 -0
  99. package/build-types/migrations/preferences-package-data/convert-editor-settings.d.ts.map +1 -0
  100. package/build-types/migrations/preferences-package-data/index.d.ts +5 -0
  101. package/build-types/migrations/preferences-package-data/index.d.ts.map +1 -0
  102. package/build-types/types.d.ts +40 -0
  103. package/build-types/types.d.ts.map +1 -0
  104. package/package.json +6 -6
  105. package/src/create/{debounce-async.js → debounce-async.ts} +13 -10
  106. package/src/create/index.ts +95 -0
  107. package/src/create/test/index.jsdom.test.js +45 -0
  108. package/src/get-newest-data.ts +50 -0
  109. package/src/index.ts +40 -0
  110. package/src/is-preferences-data.ts +15 -0
  111. package/src/local-storage.ts +30 -0
  112. package/src/migrations/legacy-local-storage-data/{convert-edit-post-panels.js → convert-edit-post-panels.ts} +13 -5
  113. package/src/migrations/legacy-local-storage-data/{index.js → index.ts} +22 -15
  114. package/src/migrations/legacy-local-storage-data/{move-feature-preferences.js → move-feature-preferences.ts} +13 -7
  115. package/src/migrations/legacy-local-storage-data/{move-individual-preference.js → move-individual-preference.ts} +15 -12
  116. package/src/migrations/legacy-local-storage-data/{move-interface-enable-items.js → move-interface-enable-items.ts} +6 -2
  117. package/src/migrations/legacy-local-storage-data/{move-third-party-feature-preferences.js → move-third-party-feature-preferences.ts} +8 -4
  118. package/src/migrations/legacy-local-storage-data/test/index.js +6 -0
  119. package/src/migrations/legacy-local-storage-data/types.ts +66 -0
  120. package/src/migrations/preferences-package-data/convert-complementary-areas.ts +21 -0
  121. package/src/migrations/preferences-package-data/convert-editor-settings.ts +58 -0
  122. package/src/migrations/preferences-package-data/{index.js → index.ts} +4 -1
  123. package/src/migrations/preferences-package-data/test/convert-complementary-areas.js +10 -0
  124. package/src/migrations/preferences-package-data/test/convert-editor-settings.js +35 -0
  125. package/src/test/index.jsdom.test.js +95 -0
  126. package/src/test/local-storage.jsdom.test.js +50 -0
  127. package/src/types.ts +43 -0
  128. package/CHANGELOG.md +0 -228
  129. package/src/create/index.js +0 -105
  130. package/src/index.js +0 -47
  131. package/src/migrations/preferences-package-data/convert-complementary-areas.js +0 -16
  132. package/src/migrations/preferences-package-data/convert-editor-settings.js +0 -48
@@ -0,0 +1,95 @@
1
+ import { afterEach, describe, expect, it, vi } from 'vitest';
2
+ import apiFetch from '@wordpress/api-fetch';
3
+ import { __unstableCreatePersistenceLayer } from '..';
4
+
5
+ vi.mock( import( '@wordpress/api-fetch' ), () => ( {
6
+ default: vi.fn( () => Promise.resolve() ),
7
+ } ) );
8
+
9
+ describe( '__unstableCreatePersistenceLayer', () => {
10
+ afterEach( () => {
11
+ vi.mocked( apiFetch ).mockClear();
12
+ vi.restoreAllMocks();
13
+ window.localStorage.clear();
14
+ } );
15
+
16
+ it( 'works when local storage is unavailable', async () => {
17
+ // Import afresh so that reading storage at module load would throw.
18
+ vi.resetModules();
19
+ vi.spyOn( window, 'localStorage', 'get' ).mockImplementation( () => {
20
+ throw new Error( 'SecurityError' );
21
+ } );
22
+ const module = await import( '..' );
23
+ const { get, set } = module.__unstableCreatePersistenceLayer(
24
+ false,
25
+ 1
26
+ );
27
+ expect( await get() ).toEqual( {} );
28
+ expect( () => set( { core: { fixedToolbar: true } } ) ).not.toThrow();
29
+ expect( await get() ).toEqual(
30
+ expect.objectContaining( { core: { fixedToolbar: true } } )
31
+ );
32
+ } );
33
+
34
+ it( 'prefers server data', async () => {
35
+ const { get } = __unstableCreatePersistenceLayer(
36
+ { core: { fixedToolbar: true } },
37
+ 1
38
+ );
39
+ expect( await get() ).toEqual( { core: { fixedToolbar: true } } );
40
+ } );
41
+
42
+ it.each( [ [ [] ], [ '' ], [ false ] ] )(
43
+ 'falls back to local storage when server data is %j',
44
+ async ( serverData ) => {
45
+ window.localStorage.setItem(
46
+ 'WP_PREFERENCES_USER_1',
47
+ JSON.stringify( { core: { fixedToolbar: true } } )
48
+ );
49
+ const { get } = __unstableCreatePersistenceLayer( serverData, 1 );
50
+ expect( await get() ).toEqual( { core: { fixedToolbar: true } } );
51
+ }
52
+ );
53
+
54
+ it.each( [ [ [] ], [ '' ], [ false ] ] )(
55
+ 'returns an empty object without fetching when server data is %j',
56
+ async ( serverData ) => {
57
+ const { get } = __unstableCreatePersistenceLayer( serverData, 1 );
58
+ expect( await get() ).toEqual( {} );
59
+ expect( apiFetch ).not.toHaveBeenCalled();
60
+ }
61
+ );
62
+
63
+ it( 'falls back to legacy data when server data is an empty array', async () => {
64
+ window.localStorage.setItem(
65
+ 'WP_DATA_USER_1',
66
+ JSON.stringify( {
67
+ 'core/preferences': {
68
+ preferences: { core: { fixedToolbar: true } },
69
+ },
70
+ } )
71
+ );
72
+ const { get } = __unstableCreatePersistenceLayer( [], 1 );
73
+ expect( await get() ).toEqual( { core: { fixedToolbar: true } } );
74
+ } );
75
+
76
+ it.each( [ [ [] ], [ '' ], [ false ] ] )(
77
+ 'falls back to legacy data when local data is %j',
78
+ async ( localData ) => {
79
+ window.localStorage.setItem(
80
+ 'WP_PREFERENCES_USER_1',
81
+ JSON.stringify( localData )
82
+ );
83
+ window.localStorage.setItem(
84
+ 'WP_DATA_USER_1',
85
+ JSON.stringify( {
86
+ 'core/preferences': {
87
+ preferences: { core: { fixedToolbar: true } },
88
+ },
89
+ } )
90
+ );
91
+ const { get } = __unstableCreatePersistenceLayer( false, 1 );
92
+ expect( await get() ).toEqual( { core: { fixedToolbar: true } } );
93
+ }
94
+ );
95
+ } );
@@ -0,0 +1,50 @@
1
+ import { afterEach, describe, expect, it, vi } from 'vitest';
2
+ import { readStoredJSON, writeStoredJSON } from '../local-storage';
3
+
4
+ describe( 'local storage', () => {
5
+ afterEach( () => {
6
+ vi.restoreAllMocks();
7
+ window.localStorage.clear();
8
+ } );
9
+
10
+ describe( 'readStoredJSON', () => {
11
+ it( 'returns the parsed value', () => {
12
+ window.localStorage.setItem( 'test', '{"a":1}' );
13
+ expect( readStoredJSON( 'test' ) ).toEqual( { a: 1 } );
14
+ } );
15
+
16
+ it( 'returns null when the value is missing', () => {
17
+ expect( readStoredJSON( 'test' ) ).toBeNull();
18
+ } );
19
+
20
+ it( 'returns null when the value is invalid JSON', () => {
21
+ window.localStorage.setItem( 'test', '{' );
22
+ expect( readStoredJSON( 'test' ) ).toBeNull();
23
+ } );
24
+
25
+ it( 'returns null when local storage is unavailable', () => {
26
+ vi.spyOn( window, 'localStorage', 'get' ).mockImplementation(
27
+ () => {
28
+ throw new Error( 'SecurityError' );
29
+ }
30
+ );
31
+ expect( readStoredJSON( 'test' ) ).toBeNull();
32
+ } );
33
+ } );
34
+
35
+ describe( 'writeStoredJSON', () => {
36
+ it( 'stores the value as JSON', () => {
37
+ writeStoredJSON( 'test', { a: 1 } );
38
+ expect( window.localStorage.getItem( 'test' ) ).toBe( '{"a":1}' );
39
+ } );
40
+
41
+ it( 'ignores errors from local storage', () => {
42
+ vi.spyOn( global.Storage.prototype, 'setItem' ).mockImplementation(
43
+ () => {
44
+ throw new Error( 'QuotaExceededError' );
45
+ }
46
+ );
47
+ expect( () => writeStoredJSON( 'test', { a: 1 } ) ).not.toThrow();
48
+ } );
49
+ } );
50
+ } );
package/src/types.ts ADDED
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Preferences of a single scope, such as `core/edit-post`.
3
+ */
4
+ export type ScopePreferences = Record< string, unknown >;
5
+
6
+ /**
7
+ * Preferences store data, keyed by scope.
8
+ */
9
+ export type ScopedPreferences = Record< string, ScopePreferences | undefined >;
10
+
11
+ /**
12
+ * Persisted preferences data, keyed by scope.
13
+ */
14
+ export interface PreferencesData {
15
+ /**
16
+ * When the data was last modified, as an ISO 8601 date string.
17
+ */
18
+ _modified?: string;
19
+ [ scope: string ]: unknown;
20
+ }
21
+
22
+ export interface CreateOptions {
23
+ /**
24
+ * Any persisted preferences data that should be preloaded.
25
+ * When set, the persistence layer will avoid fetching data from the REST API.
26
+ */
27
+ preloadedData?: PreferencesData | null;
28
+ /**
29
+ * The key to use for restoring the localStorage backup, used when the
30
+ * persistence layer calls `localStorage.getItem` or `localStorage.setItem`.
31
+ */
32
+ localStorageRestoreKey?: string;
33
+ /**
34
+ * Debounce requests to the API so that they only occur at minimum every
35
+ * `requestDebounceMS` milliseconds, and don't swamp the server. Defaults to 2500ms.
36
+ */
37
+ requestDebounceMS?: number;
38
+ }
39
+
40
+ export interface PersistenceLayer {
41
+ get: () => Promise< PreferencesData >;
42
+ set: ( newData: PreferencesData ) => void;
43
+ }
package/CHANGELOG.md DELETED
@@ -1,228 +0,0 @@
1
- <!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. -->
2
-
3
- ## Unreleased
4
-
5
- ## 2.57.0 (2026-10-07)
6
-
7
- ## 2.56.0 (2026-09-23)
8
-
9
- ## 2.55.0 (2026-09-10)
10
-
11
- ## 2.54.0 (2026-08-26)
12
-
13
- ## 2.53.0 (2026-08-12)
14
-
15
-
16
- ## 2.52.0 (2026-07-29)
17
-
18
- ## 2.51.0 (2026-07-14)
19
-
20
- ## 2.50.0 (2026-07-01)
21
-
22
- ## 2.49.0 (2026-06-24)
23
-
24
- ## 2.48.1 (2026-06-16)
25
-
26
- ## 2.48.0 (2026-06-10)
27
-
28
- ## 2.47.0 (2026-05-27)
29
-
30
- ## 2.46.0 (2026-05-14)
31
-
32
- ## 2.45.0 (2026-04-29)
33
-
34
- ## 2.44.0 (2026-04-15)
35
-
36
- ## 2.43.0 (2026-04-01)
37
-
38
- ## 2.42.0 (2026-03-18)
39
-
40
- ## 2.41.0 (2026-03-04)
41
-
42
- ## 2.40.0 (2026-02-18)
43
-
44
- ## 2.39.0 (2026-01-29)
45
-
46
- ## 2.38.0 (2026-01-16)
47
-
48
- ## 2.36.0 (2025-11-26)
49
-
50
- ## 2.35.0 (2025-11-12)
51
-
52
- ## 2.34.0 (2025-10-29)
53
-
54
- ## 2.33.0 (2025-10-17)
55
-
56
- ## 2.32.0 (2025-10-01)
57
-
58
- ## 2.31.0 (2025-09-17)
59
-
60
- ## 2.30.0 (2025-09-03)
61
-
62
- ## 2.29.0 (2025-08-20)
63
-
64
- ## 2.28.0 (2025-08-07)
65
-
66
- ## 2.27.0 (2025-07-23)
67
-
68
- ## 2.26.0 (2025-06-25)
69
-
70
- ## 2.25.0 (2025-06-04)
71
-
72
- ## 2.24.0 (2025-05-22)
73
-
74
- ## 2.23.0 (2025-05-07)
75
-
76
- ## 2.22.0 (2025-04-11)
77
-
78
- ## 2.21.0 (2025-03-27)
79
-
80
- ## 2.20.0 (2025-03-13)
81
-
82
- ## 2.19.0 (2025-02-28)
83
-
84
- ## 2.18.0 (2025-02-12)
85
-
86
- ## 2.17.0 (2025-01-29)
87
-
88
- ## 2.16.0 (2025-01-15)
89
-
90
- ## 2.15.0 (2025-01-02)
91
-
92
- ## 2.14.0 (2024-12-11)
93
-
94
- ## 2.13.0 (2024-11-27)
95
-
96
- ## 2.12.0 (2024-11-16)
97
-
98
- ## 2.11.0 (2024-10-30)
99
-
100
- ## 2.10.0 (2024-10-16)
101
-
102
- ## 2.9.0 (2024-10-03)
103
-
104
- ## 2.8.0 (2024-09-19)
105
-
106
- ## 2.7.0 (2024-09-05)
107
-
108
- ## 2.6.0 (2024-08-21)
109
-
110
- ## 2.5.0 (2024-08-07)
111
-
112
- ## 2.4.0 (2024-07-24)
113
-
114
- ## 2.3.0 (2024-07-10)
115
-
116
- ## 2.2.0 (2024-06-26)
117
-
118
- ## 2.1.0 (2024-06-15)
119
-
120
- ## 2.0.0 (2024-05-31)
121
-
122
- ### Breaking Changes
123
-
124
- - Increase the minimum required Node.js version to v18.12.0 matching long-term support releases ([#31270](https://github.com/WordPress/gutenberg/pull/61930)). Learn more about [Node.js releases](https://nodejs.org/en/about/previous-releases).
125
-
126
- ## 1.50.0 (2024-05-16)
127
-
128
- ## 1.49.0 (2024-05-02)
129
-
130
- ## 1.48.0 (2024-04-19)
131
-
132
- ## 1.47.0 (2024-04-03)
133
-
134
- ## 1.46.0 (2024-03-21)
135
-
136
- ## 1.45.0 (2024-03-06)
137
-
138
- ## 1.44.0 (2024-02-21)
139
-
140
- ## 1.43.0 (2024-02-09)
141
-
142
- ## 1.42.0 (2024-01-24)
143
-
144
- ## 1.41.0 (2024-01-10)
145
-
146
- ## 1.40.0 (2023-12-13)
147
-
148
- ## 1.39.0 (2023-11-29)
149
-
150
- ## 1.38.0 (2023-11-16)
151
-
152
- ## 1.37.0 (2023-11-02)
153
-
154
- ## 1.36.0 (2023-10-18)
155
-
156
- ## 1.35.0 (2023-10-05)
157
-
158
- ## 1.34.0 (2023-09-20)
159
-
160
- ## 1.33.0 (2023-08-31)
161
-
162
- ## 1.32.0 (2023-08-16)
163
-
164
- ## 1.31.0 (2023-08-10)
165
-
166
- ## 1.30.0 (2023-07-20)
167
-
168
- ## 1.29.0 (2023-07-05)
169
-
170
- ## 1.28.0 (2023-06-23)
171
-
172
- ## 1.27.0 (2023-06-07)
173
-
174
- ## 1.26.0 (2023-05-24)
175
-
176
- ## 1.25.0 (2023-05-10)
177
-
178
- ## 1.24.0 (2023-04-26)
179
-
180
- ## 1.23.0 (2023-04-12)
181
-
182
- ## 1.22.0 (2023-03-29)
183
-
184
- ## 1.21.0 (2023-03-15)
185
-
186
- ## 1.20.0 (2023-03-01)
187
-
188
- ## 1.19.0 (2023-02-15)
189
-
190
- ## 1.18.0 (2023-02-01)
191
-
192
- ## 1.17.0 (2023-01-11)
193
-
194
- ## 1.16.0 (2023-01-02)
195
-
196
- ## 1.15.0 (2022-12-14)
197
-
198
- ## 1.14.0 (2022-11-16)
199
-
200
- ## 1.13.0 (2022-11-02)
201
-
202
- ## 1.12.0 (2022-10-19)
203
-
204
- ## 1.11.0 (2022-10-05)
205
-
206
- ## 1.10.0 (2022-09-21)
207
-
208
- ## 1.9.0 (2022-09-13)
209
-
210
- ## 1.8.0 (2022-08-24)
211
-
212
- ## 1.7.0 (2022-08-10)
213
-
214
- ## 1.6.0 (2022-07-27)
215
-
216
- ## 1.5.0 (2022-07-13)
217
-
218
- ## 1.4.0 (2022-06-29)
219
-
220
- ## 1.3.0 (2022-06-15)
221
-
222
- ## 1.2.0 (2022-06-01)
223
-
224
- ## 1.1.0 (2022-05-18)
225
-
226
- ## 1.0.0 (2022-05-04)
227
-
228
- - Initial version of the package.
@@ -1,105 +0,0 @@
1
- import apiFetch from '@wordpress/api-fetch';
2
- import debounceAsync from './debounce-async';
3
-
4
- const EMPTY_OBJECT = {};
5
- const localStorage = window.localStorage;
6
-
7
- /**
8
- * Creates a persistence layer that stores data in WordPress user meta via the
9
- * REST API.
10
- *
11
- * @param {Object} options
12
- * @param {?Object} options.preloadedData Any persisted preferences data that should be preloaded.
13
- * When set, the persistence layer will avoid fetching data
14
- * from the REST API.
15
- * @param {?string} options.localStorageRestoreKey The key to use for restoring the localStorage backup, used
16
- * when the persistence layer calls `localStorage.getItem` or
17
- * `localStorage.setItem`.
18
- * @param {?number} options.requestDebounceMS Debounce requests to the API so that they only occur at
19
- * minimum every `requestDebounceMS` milliseconds, and don't
20
- * swamp the server. Defaults to 2500ms.
21
- *
22
- * @return {Object} A persistence layer for WordPress user meta.
23
- */
24
- export default function create( {
25
- preloadedData,
26
- localStorageRestoreKey = 'WP_PREFERENCES_RESTORE_DATA',
27
- requestDebounceMS = 2500,
28
- } = {} ) {
29
- let cache = preloadedData;
30
- const debouncedApiFetch = debounceAsync( apiFetch, requestDebounceMS );
31
-
32
- async function get() {
33
- if ( cache ) {
34
- return cache;
35
- }
36
-
37
- const user = await apiFetch( {
38
- path: '/wp/v2/users/me?context=edit',
39
- } );
40
-
41
- const serverData = user?.meta?.persisted_preferences;
42
- const localData = JSON.parse(
43
- localStorage.getItem( localStorageRestoreKey )
44
- );
45
-
46
- // Date parse returns NaN for invalid input. Coerce anything invalid
47
- // into a conveniently comparable zero.
48
- const serverTimestamp = Date.parse( serverData?._modified ) || 0;
49
- const localTimestamp = Date.parse( localData?._modified ) || 0;
50
-
51
- // Prefer server data if it exists and is more recent.
52
- // Otherwise fallback to localStorage data.
53
- if ( serverData && serverTimestamp >= localTimestamp ) {
54
- cache = serverData;
55
- } else if ( localData ) {
56
- cache = localData;
57
- } else {
58
- cache = EMPTY_OBJECT;
59
- }
60
-
61
- return cache;
62
- }
63
-
64
- function set( newData ) {
65
- const dataWithTimestamp = {
66
- ...newData,
67
- _modified: new Date().toISOString(),
68
- };
69
- cache = dataWithTimestamp;
70
-
71
- // Store data in local storage as a fallback. If for some reason the
72
- // api request does not complete or becomes unavailable, this data
73
- // can be used to restore preferences.
74
- localStorage.setItem(
75
- localStorageRestoreKey,
76
- JSON.stringify( dataWithTimestamp )
77
- );
78
-
79
- // The user meta endpoint seems susceptible to errors when consecutive
80
- // requests are made in quick succession. Ensure there's a gap between
81
- // any consecutive requests.
82
- //
83
- // Catch and do nothing with errors from the REST API.
84
- debouncedApiFetch( {
85
- path: '/wp/v2/users/me',
86
- method: 'PUT',
87
- // `keepalive` will still send the request in the background,
88
- // even when a browser unload event might interrupt it.
89
- // This should hopefully make things more resilient.
90
- // This does have a size limit of 64kb, but the data is usually
91
- // much less.
92
- keepalive: true,
93
- data: {
94
- meta: {
95
- persisted_preferences: dataWithTimestamp,
96
- },
97
- },
98
- } ).catch( () => {} );
99
- }
100
-
101
- return {
102
- get,
103
- set,
104
- };
105
- }
package/src/index.js DELETED
@@ -1,47 +0,0 @@
1
- import create from './create';
2
- import convertLegacyLocalStorageData from './migrations/legacy-local-storage-data';
3
- import convertPreferencesPackageData from './migrations/preferences-package-data';
4
-
5
- export { create };
6
-
7
- /**
8
- * Creates the persistence layer with preloaded data.
9
- *
10
- * It prioritizes any data from the server, but falls back first to localStorage
11
- * restore data, and then to any legacy data.
12
- *
13
- * This function is used internally by WordPress in an inline script, so
14
- * prefixed with `__unstable`.
15
- *
16
- * @param {Object} serverData Preferences data preloaded from the server.
17
- * @param {string} userId The user id.
18
- *
19
- * @return {Object} The persistence layer initialized with the preloaded data.
20
- */
21
- export function __unstableCreatePersistenceLayer( serverData, userId ) {
22
- const localStorageRestoreKey = `WP_PREFERENCES_USER_${ userId }`;
23
- const localData = JSON.parse(
24
- window.localStorage.getItem( localStorageRestoreKey )
25
- );
26
-
27
- // Date parse returns NaN for invalid input. Coerce anything invalid
28
- // into a conveniently comparable zero.
29
- const serverModified =
30
- Date.parse( serverData && serverData._modified ) || 0;
31
- const localModified = Date.parse( localData && localData._modified ) || 0;
32
-
33
- let preloadedData;
34
- if ( serverData && serverModified >= localModified ) {
35
- preloadedData = convertPreferencesPackageData( serverData );
36
- } else if ( localData ) {
37
- preloadedData = convertPreferencesPackageData( localData );
38
- } else {
39
- // Check if there is data in the legacy format from the old persistence system.
40
- preloadedData = convertLegacyLocalStorageData( userId );
41
- }
42
-
43
- return create( {
44
- preloadedData,
45
- localStorageRestoreKey,
46
- } );
47
- }
@@ -1,16 +0,0 @@
1
- export default function convertComplementaryAreas( state ) {
2
- return Object.keys( state ).reduce( ( stateAccumulator, scope ) => {
3
- const scopeData = state[ scope ];
4
-
5
- // If a complementary area is truthy, convert it to the `isComplementaryAreaVisible` boolean.
6
- if ( scopeData?.complementaryArea ) {
7
- const updatedScopeData = { ...scopeData };
8
- delete updatedScopeData.complementaryArea;
9
- updatedScopeData.isComplementaryAreaVisible = true;
10
- stateAccumulator[ scope ] = updatedScopeData;
11
- return stateAccumulator;
12
- }
13
-
14
- return stateAccumulator;
15
- }, state );
16
- }
@@ -1,48 +0,0 @@
1
- export default function convertEditorSettings( data ) {
2
- let newData = data;
3
- const settingsToMoveToCore = [
4
- 'allowRightClickOverrides',
5
- 'distractionFree',
6
- 'editorMode',
7
- 'fixedToolbar',
8
- 'focusMode',
9
- 'hiddenBlockTypes',
10
- 'inactivePanels',
11
- 'keepCaretInsideBlock',
12
- 'mostUsedBlocks',
13
- 'openPanels',
14
- 'showBlockBreadcrumbs',
15
- 'showIconLabels',
16
- 'showListViewByDefault',
17
- 'isPublishSidebarEnabled',
18
- 'isComplementaryAreaVisible',
19
- 'pinnedItems',
20
- ];
21
-
22
- settingsToMoveToCore.forEach( ( setting ) => {
23
- if ( data?.[ 'core/edit-post' ]?.[ setting ] !== undefined ) {
24
- newData = {
25
- ...newData,
26
- core: {
27
- ...newData?.core,
28
- [ setting ]: data[ 'core/edit-post' ][ setting ],
29
- },
30
- };
31
- delete newData[ 'core/edit-post' ][ setting ];
32
- }
33
-
34
- if ( data?.[ 'core/edit-site' ]?.[ setting ] !== undefined ) {
35
- delete newData[ 'core/edit-site' ][ setting ];
36
- }
37
- } );
38
-
39
- if ( Object.keys( newData?.[ 'core/edit-post' ] ?? {} )?.length === 0 ) {
40
- delete newData[ 'core/edit-post' ];
41
- }
42
-
43
- if ( Object.keys( newData?.[ 'core/edit-site' ] ?? {} )?.length === 0 ) {
44
- delete newData[ 'core/edit-site' ];
45
- }
46
-
47
- return newData;
48
- }