@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.
- package/README.md +7 -7
- package/build/create/debounce-async.cjs +2 -1
- package/build/create/debounce-async.cjs.map +3 -3
- package/build/create/index.cjs +11 -21
- package/build/create/index.cjs.map +4 -4
- package/build/get-newest-data.cjs +53 -0
- package/build/get-newest-data.cjs.map +7 -0
- package/build/index.cjs +8 -16
- package/build/index.cjs.map +4 -4
- package/build/is-preferences-data.cjs +29 -0
- package/build/is-preferences-data.cjs.map +7 -0
- package/build/local-storage.cjs +46 -0
- package/build/local-storage.cjs.map +7 -0
- package/build/migrations/legacy-local-storage-data/convert-edit-post-panels.cjs +2 -1
- package/build/migrations/legacy-local-storage-data/convert-edit-post-panels.cjs.map +3 -3
- package/build/migrations/legacy-local-storage-data/index.cjs +7 -5
- package/build/migrations/legacy-local-storage-data/index.cjs.map +4 -4
- package/build/migrations/legacy-local-storage-data/move-feature-preferences.cjs +2 -1
- package/build/migrations/legacy-local-storage-data/move-feature-preferences.cjs.map +3 -3
- package/build/migrations/legacy-local-storage-data/move-individual-preference.cjs +2 -1
- package/build/migrations/legacy-local-storage-data/move-individual-preference.cjs.map +3 -3
- package/build/migrations/legacy-local-storage-data/move-interface-enable-items.cjs +2 -1
- package/build/migrations/legacy-local-storage-data/move-interface-enable-items.cjs.map +3 -3
- package/build/migrations/legacy-local-storage-data/move-third-party-feature-preferences.cjs +2 -1
- package/build/migrations/legacy-local-storage-data/move-third-party-feature-preferences.cjs.map +3 -3
- package/build/migrations/legacy-local-storage-data/types.cjs +19 -0
- package/build/migrations/legacy-local-storage-data/types.cjs.map +7 -0
- package/build/migrations/preferences-package-data/convert-complementary-areas.cjs +15 -11
- package/build/migrations/preferences-package-data/convert-complementary-areas.cjs.map +3 -3
- package/build/migrations/preferences-package-data/convert-editor-settings.cjs +30 -14
- package/build/migrations/preferences-package-data/convert-editor-settings.cjs.map +4 -4
- package/build/migrations/preferences-package-data/index.cjs +2 -1
- package/build/migrations/preferences-package-data/index.cjs.map +3 -3
- package/build/types.cjs +19 -0
- package/build/types.cjs.map +7 -0
- package/build-module/create/debounce-async.mjs +1 -1
- package/build-module/create/debounce-async.mjs.map +3 -3
- package/build-module/create/index.mjs +10 -21
- package/build-module/create/index.mjs.map +3 -3
- package/build-module/get-newest-data.mjs +22 -0
- package/build-module/get-newest-data.mjs.map +7 -0
- package/build-module/index.mjs +7 -16
- package/build-module/index.mjs.map +3 -3
- package/build-module/is-preferences-data.mjs +8 -0
- package/build-module/is-preferences-data.mjs.map +7 -0
- package/build-module/local-storage.mjs +20 -0
- package/build-module/local-storage.mjs.map +7 -0
- package/build-module/migrations/legacy-local-storage-data/convert-edit-post-panels.mjs +1 -1
- package/build-module/migrations/legacy-local-storage-data/convert-edit-post-panels.mjs.map +3 -3
- package/build-module/migrations/legacy-local-storage-data/index.mjs +6 -5
- package/build-module/migrations/legacy-local-storage-data/index.mjs.map +3 -3
- package/build-module/migrations/legacy-local-storage-data/move-feature-preferences.mjs +1 -1
- package/build-module/migrations/legacy-local-storage-data/move-feature-preferences.mjs.map +3 -3
- package/build-module/migrations/legacy-local-storage-data/move-individual-preference.mjs +1 -1
- package/build-module/migrations/legacy-local-storage-data/move-individual-preference.mjs.map +3 -3
- package/build-module/migrations/legacy-local-storage-data/move-interface-enable-items.mjs +1 -1
- package/build-module/migrations/legacy-local-storage-data/move-interface-enable-items.mjs.map +3 -3
- package/build-module/migrations/legacy-local-storage-data/move-third-party-feature-preferences.mjs +1 -1
- package/build-module/migrations/legacy-local-storage-data/move-third-party-feature-preferences.mjs.map +3 -3
- package/build-module/migrations/legacy-local-storage-data/types.mjs +1 -0
- package/build-module/migrations/legacy-local-storage-data/types.mjs.map +7 -0
- package/build-module/migrations/preferences-package-data/convert-complementary-areas.mjs +14 -11
- package/build-module/migrations/preferences-package-data/convert-complementary-areas.mjs.map +3 -3
- package/build-module/migrations/preferences-package-data/convert-editor-settings.mjs +19 -14
- package/build-module/migrations/preferences-package-data/convert-editor-settings.mjs.map +3 -3
- package/build-module/migrations/preferences-package-data/index.mjs +1 -1
- package/build-module/migrations/preferences-package-data/index.mjs.map +3 -3
- package/build-module/types.mjs +1 -0
- package/build-module/types.mjs.map +7 -0
- package/build-types/create/debounce-async.d.ts +19 -0
- package/build-types/create/debounce-async.d.ts.map +1 -0
- package/build-types/create/index.d.ts +20 -0
- package/build-types/create/index.d.ts.map +1 -0
- package/build-types/get-newest-data.d.ts +11 -0
- package/build-types/get-newest-data.d.ts.map +1 -0
- package/build-types/index.d.ts +20 -0
- package/build-types/index.d.ts.map +1 -0
- package/build-types/is-preferences-data.d.ts +11 -0
- package/build-types/is-preferences-data.d.ts.map +1 -0
- package/build-types/local-storage.d.ts +19 -0
- package/build-types/local-storage.d.ts.map +1 -0
- package/build-types/migrations/legacy-local-storage-data/convert-edit-post-panels.d.ts +35 -0
- package/build-types/migrations/legacy-local-storage-data/convert-edit-post-panels.d.ts.map +1 -0
- package/build-types/migrations/legacy-local-storage-data/index.d.ts +22 -0
- package/build-types/migrations/legacy-local-storage-data/index.d.ts.map +1 -0
- package/build-types/migrations/legacy-local-storage-data/move-feature-preferences.d.ts +60 -0
- package/build-types/migrations/legacy-local-storage-data/move-feature-preferences.d.ts.map +1 -0
- package/build-types/migrations/legacy-local-storage-data/move-individual-preference.d.ts +42 -0
- package/build-types/migrations/legacy-local-storage-data/move-individual-preference.d.ts.map +1 -0
- package/build-types/migrations/legacy-local-storage-data/move-interface-enable-items.d.ts +50 -0
- package/build-types/migrations/legacy-local-storage-data/move-interface-enable-items.d.ts.map +1 -0
- package/build-types/migrations/legacy-local-storage-data/move-third-party-feature-preferences.d.ts +44 -0
- package/build-types/migrations/legacy-local-storage-data/move-third-party-feature-preferences.d.ts.map +1 -0
- package/build-types/migrations/legacy-local-storage-data/types.d.ts +60 -0
- package/build-types/migrations/legacy-local-storage-data/types.d.ts.map +1 -0
- package/build-types/migrations/preferences-package-data/convert-complementary-areas.d.ts +5 -0
- package/build-types/migrations/preferences-package-data/convert-complementary-areas.d.ts.map +1 -0
- package/build-types/migrations/preferences-package-data/convert-editor-settings.d.ts +5 -0
- package/build-types/migrations/preferences-package-data/convert-editor-settings.d.ts.map +1 -0
- package/build-types/migrations/preferences-package-data/index.d.ts +5 -0
- package/build-types/migrations/preferences-package-data/index.d.ts.map +1 -0
- package/build-types/types.d.ts +40 -0
- package/build-types/types.d.ts.map +1 -0
- package/package.json +6 -6
- package/src/create/{debounce-async.js → debounce-async.ts} +13 -10
- package/src/create/index.ts +95 -0
- package/src/create/test/index.jsdom.test.js +45 -0
- package/src/get-newest-data.ts +50 -0
- package/src/index.ts +40 -0
- package/src/is-preferences-data.ts +15 -0
- package/src/local-storage.ts +30 -0
- package/src/migrations/legacy-local-storage-data/{convert-edit-post-panels.js → convert-edit-post-panels.ts} +13 -5
- package/src/migrations/legacy-local-storage-data/{index.js → index.ts} +22 -15
- package/src/migrations/legacy-local-storage-data/{move-feature-preferences.js → move-feature-preferences.ts} +13 -7
- package/src/migrations/legacy-local-storage-data/{move-individual-preference.js → move-individual-preference.ts} +15 -12
- package/src/migrations/legacy-local-storage-data/{move-interface-enable-items.js → move-interface-enable-items.ts} +6 -2
- package/src/migrations/legacy-local-storage-data/{move-third-party-feature-preferences.js → move-third-party-feature-preferences.ts} +8 -4
- package/src/migrations/legacy-local-storage-data/test/index.js +6 -0
- package/src/migrations/legacy-local-storage-data/types.ts +66 -0
- package/src/migrations/preferences-package-data/convert-complementary-areas.ts +21 -0
- package/src/migrations/preferences-package-data/convert-editor-settings.ts +58 -0
- package/src/migrations/preferences-package-data/{index.js → index.ts} +4 -1
- package/src/migrations/preferences-package-data/test/convert-complementary-areas.js +10 -0
- package/src/migrations/preferences-package-data/test/convert-editor-settings.js +35 -0
- package/src/test/index.jsdom.test.js +95 -0
- package/src/test/local-storage.jsdom.test.js +50 -0
- package/src/types.ts +43 -0
- package/CHANGELOG.md +0 -228
- package/src/create/index.js +0 -105
- package/src/index.js +0 -47
- package/src/migrations/preferences-package-data/convert-complementary-areas.js +0 -16
- package/src/migrations/preferences-package-data/convert-editor-settings.js +0 -48
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import isPreferencesData from './is-preferences-data';
|
|
2
|
+
import { readStoredJSON } from './local-storage';
|
|
3
|
+
import convertPreferencesPackageData from './migrations/preferences-package-data';
|
|
4
|
+
import type { PreferencesData, ScopedPreferences } from './types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Picks the most recently modified preferences from the server data and the
|
|
8
|
+
* local storage backup, ignoring either when it is not a preferences object.
|
|
9
|
+
*
|
|
10
|
+
* @param serverData Preferences data from the server.
|
|
11
|
+
* @param localStorageRestoreKey The key of the local storage backup.
|
|
12
|
+
*
|
|
13
|
+
* @return The newest preferences data, or `null` when neither is valid.
|
|
14
|
+
*/
|
|
15
|
+
function pickNewestData(
|
|
16
|
+
serverData: unknown,
|
|
17
|
+
localStorageRestoreKey: string
|
|
18
|
+
): PreferencesData | null {
|
|
19
|
+
const storedData = readStoredJSON( localStorageRestoreKey );
|
|
20
|
+
const localData = isPreferencesData( storedData ) ? storedData : null;
|
|
21
|
+
if ( ! isPreferencesData( serverData ) ) {
|
|
22
|
+
return localData;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Date parse returns NaN for invalid input. Coerce anything invalid
|
|
26
|
+
// into a conveniently comparable zero.
|
|
27
|
+
const serverModified = Date.parse( serverData._modified ?? '' ) || 0;
|
|
28
|
+
const localModified = Date.parse( localData?._modified ?? '' ) || 0;
|
|
29
|
+
|
|
30
|
+
// Prefer server data when it is at least as recent.
|
|
31
|
+
return localData && localModified > serverModified ? localData : serverData;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Gets the newest persisted preferences, migrated to the current format.
|
|
36
|
+
*
|
|
37
|
+
* @param serverData Preferences data from the server.
|
|
38
|
+
* @param localStorageRestoreKey The key of the local storage backup.
|
|
39
|
+
*
|
|
40
|
+
* @return The migrated preferences, or `undefined` when there are none.
|
|
41
|
+
*/
|
|
42
|
+
export default function getNewestData(
|
|
43
|
+
serverData: unknown,
|
|
44
|
+
localStorageRestoreKey: string
|
|
45
|
+
): ScopedPreferences | undefined {
|
|
46
|
+
const newestData = pickNewestData( serverData, localStorageRestoreKey );
|
|
47
|
+
return newestData
|
|
48
|
+
? convertPreferencesPackageData( newestData as ScopedPreferences )
|
|
49
|
+
: undefined;
|
|
50
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import create from './create';
|
|
2
|
+
import convertLegacyLocalStorageData from './migrations/legacy-local-storage-data';
|
|
3
|
+
import getNewestData from './get-newest-data';
|
|
4
|
+
import type { PersistenceLayer } from './types';
|
|
5
|
+
|
|
6
|
+
export { create };
|
|
7
|
+
|
|
8
|
+
export type * from './types';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Creates the persistence layer with preloaded data.
|
|
12
|
+
*
|
|
13
|
+
* It prioritizes any data from the server, but falls back first to localStorage
|
|
14
|
+
* restore data, and then to any legacy data.
|
|
15
|
+
*
|
|
16
|
+
* This function is used internally by WordPress in an inline script, so
|
|
17
|
+
* prefixed with `__unstable`.
|
|
18
|
+
*
|
|
19
|
+
* @param serverData Preferences data preloaded from the server.
|
|
20
|
+
* @param userId The user id.
|
|
21
|
+
*
|
|
22
|
+
* @return The persistence layer initialized with the preloaded data.
|
|
23
|
+
*/
|
|
24
|
+
export function __unstableCreatePersistenceLayer(
|
|
25
|
+
serverData: unknown,
|
|
26
|
+
userId: string | number
|
|
27
|
+
): PersistenceLayer {
|
|
28
|
+
const localStorageRestoreKey = `WP_PREFERENCES_USER_${ userId }`;
|
|
29
|
+
return create( {
|
|
30
|
+
/*
|
|
31
|
+
* Fall back to data in the legacy format from the old persistence system.
|
|
32
|
+
* The server data is already preloaded, so fetching it again is redundant.
|
|
33
|
+
*/
|
|
34
|
+
preloadedData:
|
|
35
|
+
getNewestData( serverData, localStorageRestoreKey ) ??
|
|
36
|
+
convertLegacyLocalStorageData( userId ) ??
|
|
37
|
+
{},
|
|
38
|
+
localStorageRestoreKey,
|
|
39
|
+
} );
|
|
40
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { PreferencesData } from './types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks whether persisted data is a preferences object. Empty user meta
|
|
5
|
+
* reaches the client as `[]` or `''`, which must not be used as preferences.
|
|
6
|
+
*
|
|
7
|
+
* @param data The persisted data.
|
|
8
|
+
*
|
|
9
|
+
* @return Whether the data is a preferences object.
|
|
10
|
+
*/
|
|
11
|
+
export default function isPreferencesData(
|
|
12
|
+
data: unknown
|
|
13
|
+
): data is PreferencesData {
|
|
14
|
+
return !! data && typeof data === 'object' && ! Array.isArray( data );
|
|
15
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads and parses a JSON value from local storage.
|
|
3
|
+
*
|
|
4
|
+
* @param key The local storage key.
|
|
5
|
+
*
|
|
6
|
+
* @return The parsed value, or `null` when the value is missing or invalid,
|
|
7
|
+
* or local storage is unavailable.
|
|
8
|
+
*/
|
|
9
|
+
export function readStoredJSON( key: string ): unknown {
|
|
10
|
+
try {
|
|
11
|
+
const value = window.localStorage.getItem( key );
|
|
12
|
+
return value === null ? null : JSON.parse( value );
|
|
13
|
+
} catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
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
|
+
export function writeStoredJSON( key: string, value: unknown ) {
|
|
27
|
+
try {
|
|
28
|
+
window.localStorage.setItem( key, JSON.stringify( value ) );
|
|
29
|
+
} catch {}
|
|
30
|
+
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import type { ScopePreferences } from '../../types';
|
|
2
|
+
import type { EditPostPanels, LegacyEditPostPanel } from './types';
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* Convert the post editor's panels state from:
|
|
3
6
|
* ```
|
|
@@ -25,13 +28,18 @@
|
|
|
25
28
|
* ],
|
|
26
29
|
* }
|
|
27
30
|
*
|
|
28
|
-
* @param
|
|
31
|
+
* @param preferences A preferences object.
|
|
29
32
|
*
|
|
30
|
-
* @return
|
|
33
|
+
* @return The converted data.
|
|
31
34
|
*/
|
|
32
|
-
export default function convertEditPostPanels(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
+
export default function convertEditPostPanels(
|
|
36
|
+
preferences: ScopePreferences
|
|
37
|
+
): EditPostPanels {
|
|
38
|
+
const panels = ( preferences?.panels ?? {} ) as Record<
|
|
39
|
+
string,
|
|
40
|
+
LegacyEditPostPanel | undefined
|
|
41
|
+
>;
|
|
42
|
+
return Object.keys( panels ).reduce< EditPostPanels >(
|
|
35
43
|
( convertedData, panelName ) => {
|
|
36
44
|
const panel = panels[ panelName ];
|
|
37
45
|
|
|
@@ -3,29 +3,33 @@ import moveThirdPartyFeaturePreferences from './move-third-party-feature-prefere
|
|
|
3
3
|
import moveIndividualPreference from './move-individual-preference';
|
|
4
4
|
import moveInterfaceEnableItems from './move-interface-enable-items';
|
|
5
5
|
import convertEditPostPanels from './convert-edit-post-panels';
|
|
6
|
+
import isPreferencesData from '../../is-preferences-data';
|
|
7
|
+
import { readStoredJSON } from '../../local-storage';
|
|
8
|
+
import type { ScopedPreferences } from '../../types';
|
|
9
|
+
import type { LegacyData } from './types';
|
|
6
10
|
|
|
7
11
|
/**
|
|
8
12
|
* Gets the legacy local storage data for a given user.
|
|
9
13
|
*
|
|
10
|
-
* @param
|
|
14
|
+
* @param userId The user id.
|
|
11
15
|
*
|
|
12
|
-
* @return
|
|
16
|
+
* @return The local storage data.
|
|
13
17
|
*/
|
|
14
|
-
function getLegacyData( userId ) {
|
|
15
|
-
|
|
16
|
-
const unparsedData = window.localStorage.getItem( key );
|
|
17
|
-
return JSON.parse( unparsedData );
|
|
18
|
+
function getLegacyData( userId: string | number ): LegacyData | null {
|
|
19
|
+
return readStoredJSON( `WP_DATA_USER_${ userId }` ) as LegacyData | null;
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
/**
|
|
21
23
|
* Converts data from the old `@wordpress/data` package format.
|
|
22
24
|
*
|
|
23
|
-
* @param
|
|
25
|
+
* @param data The legacy data in its original format.
|
|
24
26
|
*
|
|
25
|
-
* @return
|
|
26
|
-
*
|
|
27
|
+
* @return The converted data or `undefined` if there was
|
|
28
|
+
* nothing to convert.
|
|
27
29
|
*/
|
|
28
|
-
export function convertLegacyData(
|
|
30
|
+
export function convertLegacyData(
|
|
31
|
+
data: LegacyData | null | undefined
|
|
32
|
+
): ScopedPreferences | undefined {
|
|
29
33
|
if ( ! data ) {
|
|
30
34
|
return;
|
|
31
35
|
}
|
|
@@ -81,19 +85,22 @@ export function convertLegacyData( data ) {
|
|
|
81
85
|
|
|
82
86
|
// The new system is only concerned with persisting
|
|
83
87
|
// 'core/preferences' preferences reducer, so only return that.
|
|
84
|
-
|
|
88
|
+
const preferences = data?.[ 'core/preferences' ]?.preferences;
|
|
89
|
+
return isPreferencesData( preferences ) ? preferences : undefined;
|
|
85
90
|
}
|
|
86
91
|
|
|
87
92
|
/**
|
|
88
93
|
* Gets the legacy local storage data for the given user and returns the
|
|
89
94
|
* data converted to the new format.
|
|
90
95
|
*
|
|
91
|
-
* @param
|
|
96
|
+
* @param userId The user id.
|
|
92
97
|
*
|
|
93
|
-
* @return
|
|
94
|
-
*
|
|
98
|
+
* @return The converted data or undefined if no local
|
|
99
|
+
* storage data could be found.
|
|
95
100
|
*/
|
|
96
|
-
export default function convertLegacyLocalStorageData(
|
|
101
|
+
export default function convertLegacyLocalStorageData(
|
|
102
|
+
userId: string | number
|
|
103
|
+
): ScopedPreferences | undefined {
|
|
97
104
|
const data = getLegacyData( userId );
|
|
98
105
|
return convertLegacyData( data );
|
|
99
106
|
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { FeaturePreferences, LegacyData } from './types';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Move the 'features' object in local storage from the sourceStoreName to the
|
|
3
5
|
* preferences store data structure.
|
|
@@ -49,13 +51,16 @@
|
|
|
49
51
|
* This function handles moving from either the source store or the interface
|
|
50
52
|
* store to the preferences data structure.
|
|
51
53
|
*
|
|
52
|
-
* @param
|
|
53
|
-
* @param
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* @return
|
|
54
|
+
* @param state The state before migration.
|
|
55
|
+
* @param sourceStoreName The name of the store that has persisted
|
|
56
|
+
* preferences to migrate to the preferences
|
|
57
|
+
* package.
|
|
58
|
+
* @return The migrated state
|
|
57
59
|
*/
|
|
58
|
-
export default function moveFeaturePreferences(
|
|
60
|
+
export default function moveFeaturePreferences(
|
|
61
|
+
state: LegacyData,
|
|
62
|
+
sourceStoreName: string
|
|
63
|
+
): LegacyData {
|
|
59
64
|
const preferencesStoreName = 'core/preferences';
|
|
60
65
|
const interfaceStoreName = 'core/interface';
|
|
61
66
|
|
|
@@ -67,7 +72,8 @@ export default function moveFeaturePreferences( state, sourceStoreName ) {
|
|
|
67
72
|
state?.[ interfaceStoreName ]?.preferences?.features?.[
|
|
68
73
|
sourceStoreName
|
|
69
74
|
];
|
|
70
|
-
const sourceFeatures = state?.[ sourceStoreName ]?.preferences?.features
|
|
75
|
+
const sourceFeatures = state?.[ sourceStoreName ]?.preferences?.features as
|
|
76
|
+
FeaturePreferences | undefined;
|
|
71
77
|
const featuresToMigrate = interfaceFeatures
|
|
72
78
|
? interfaceFeatures
|
|
73
79
|
: sourceFeatures;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import type { ScopePreferences } from '../../types';
|
|
2
|
+
import type { LegacyData } from './types';
|
|
3
|
+
|
|
4
|
+
const identity = < T >( arg: T ) => arg;
|
|
2
5
|
|
|
3
6
|
/**
|
|
4
7
|
* Migrates an individual item inside the `preferences` object for a package's store.
|
|
@@ -28,19 +31,19 @@ const identity = ( arg ) => arg;
|
|
|
28
31
|
*
|
|
29
32
|
* ```
|
|
30
33
|
*
|
|
31
|
-
* @param
|
|
32
|
-
* @param
|
|
33
|
-
* @param
|
|
34
|
-
* @param
|
|
35
|
-
* @param
|
|
36
|
-
* @param
|
|
34
|
+
* @param state The original state.
|
|
35
|
+
* @param migrate An options object that contains details of the migration.
|
|
36
|
+
* @param migrate.from The name of the store to migrate from.
|
|
37
|
+
* @param migrate.to The scope in the preferences store to migrate to.
|
|
38
|
+
* @param key The key in the preferences object to migrate.
|
|
39
|
+
* @param convert A function that converts preferences from one format to another.
|
|
37
40
|
*/
|
|
38
41
|
export default function moveIndividualPreferenceToPreferences(
|
|
39
|
-
state,
|
|
40
|
-
{ from: sourceStoreName, to: scope },
|
|
41
|
-
key,
|
|
42
|
-
convert = identity
|
|
43
|
-
) {
|
|
42
|
+
state: LegacyData,
|
|
43
|
+
{ from: sourceStoreName, to: scope }: { from: string; to: string },
|
|
44
|
+
key: string,
|
|
45
|
+
convert: ( preferences: ScopePreferences ) => object = identity
|
|
46
|
+
): LegacyData {
|
|
44
47
|
const preferencesStoreName = 'core/preferences';
|
|
45
48
|
const sourcePreference = state?.[ sourceStoreName ]?.preferences?.[ key ];
|
|
46
49
|
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { LegacyData } from './types';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Migrates interface 'enableItems' data to the preferences store.
|
|
3
5
|
*
|
|
@@ -43,9 +45,11 @@
|
|
|
43
45
|
* }
|
|
44
46
|
* ```
|
|
45
47
|
*
|
|
46
|
-
* @param
|
|
48
|
+
* @param state The local storage state.
|
|
47
49
|
*/
|
|
48
|
-
export default function moveInterfaceEnableItems(
|
|
50
|
+
export default function moveInterfaceEnableItems(
|
|
51
|
+
state: LegacyData
|
|
52
|
+
): LegacyData {
|
|
49
53
|
const interfaceStoreName = 'core/interface';
|
|
50
54
|
const preferencesStoreName = 'core/preferences';
|
|
51
55
|
const sourceEnableItems = state?.[ interfaceStoreName ]?.enableItems;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { LegacyData } from './types';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* The interface package previously had a public API that could be used by
|
|
3
5
|
* plugins to set persisted boolean 'feature' preferences.
|
|
@@ -34,12 +36,14 @@
|
|
|
34
36
|
* }
|
|
35
37
|
* ```
|
|
36
38
|
*
|
|
37
|
-
* @param
|
|
39
|
+
* @param state The local storage state
|
|
38
40
|
*
|
|
39
|
-
* @return
|
|
40
|
-
*
|
|
41
|
+
* @return The state with third party preferences moved to the
|
|
42
|
+
* preferences data structure.
|
|
41
43
|
*/
|
|
42
|
-
export default function moveThirdPartyFeaturePreferencesToPreferences(
|
|
44
|
+
export default function moveThirdPartyFeaturePreferencesToPreferences(
|
|
45
|
+
state: LegacyData
|
|
46
|
+
): LegacyData {
|
|
43
47
|
const interfaceStoreName = 'core/interface';
|
|
44
48
|
const preferencesStoreName = 'core/preferences';
|
|
45
49
|
|
|
@@ -214,4 +214,10 @@ describe( 'convertLegacyData', () => {
|
|
|
214
214
|
}
|
|
215
215
|
` );
|
|
216
216
|
} );
|
|
217
|
+
|
|
218
|
+
it( 'ignores legacy preferences that are not an object', () => {
|
|
219
|
+
expect(
|
|
220
|
+
convertLegacyData( { 'core/preferences': { preferences: [] } } )
|
|
221
|
+
).toBeUndefined();
|
|
222
|
+
} );
|
|
217
223
|
} );
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { ScopedPreferences } from '../../types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Boolean 'feature' preferences, keyed by feature name.
|
|
5
|
+
*/
|
|
6
|
+
export type FeaturePreferences = Record< string, boolean | undefined >;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Persisted state of a package's store, such as `core/edit-post`.
|
|
10
|
+
*/
|
|
11
|
+
export interface LegacyStoreState {
|
|
12
|
+
preferences?: Record< string, unknown >;
|
|
13
|
+
[ key: string ]: unknown;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Persisted state of the `core/interface` store.
|
|
18
|
+
*/
|
|
19
|
+
export interface LegacyInterfaceStoreState {
|
|
20
|
+
preferences?: {
|
|
21
|
+
features?: Record< string, FeaturePreferences | undefined >;
|
|
22
|
+
[ key: string ]: unknown;
|
|
23
|
+
};
|
|
24
|
+
enableItems?: {
|
|
25
|
+
singleEnableItems?: {
|
|
26
|
+
complementaryArea?: Record< string, string >;
|
|
27
|
+
};
|
|
28
|
+
multipleEnableItems?: {
|
|
29
|
+
pinnedItems?: Record< string, Record< string, boolean > >;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
[ key: string ]: unknown;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Persisted state of the `core/preferences` store.
|
|
37
|
+
*/
|
|
38
|
+
export interface LegacyPreferencesStoreState {
|
|
39
|
+
preferences?: ScopedPreferences;
|
|
40
|
+
[ key: string ]: unknown;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Local storage data in the legacy `@wordpress/data` format, keyed by store name.
|
|
45
|
+
*/
|
|
46
|
+
export interface LegacyData {
|
|
47
|
+
'core/interface'?: LegacyInterfaceStoreState;
|
|
48
|
+
'core/preferences'?: LegacyPreferencesStoreState;
|
|
49
|
+
[ storeName: string ]: LegacyStoreState | undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The post editor's panels state in the legacy format.
|
|
54
|
+
*/
|
|
55
|
+
export interface LegacyEditPostPanel {
|
|
56
|
+
enabled?: boolean;
|
|
57
|
+
opened?: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The post editor's panels state in the preferences format.
|
|
62
|
+
*/
|
|
63
|
+
export interface EditPostPanels {
|
|
64
|
+
inactivePanels: string[];
|
|
65
|
+
openPanels: string[];
|
|
66
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ScopedPreferences } from '../../types';
|
|
2
|
+
|
|
3
|
+
export default function convertComplementaryAreas( state: ScopedPreferences ) {
|
|
4
|
+
return Object.keys( state ).reduce(
|
|
5
|
+
( stateAccumulator, scope ) => {
|
|
6
|
+
const scopeData = state[ scope ];
|
|
7
|
+
|
|
8
|
+
// If a complementary area is truthy, convert it to the `isComplementaryAreaVisible` boolean.
|
|
9
|
+
if ( scopeData?.complementaryArea ) {
|
|
10
|
+
const updatedScopeData = { ...scopeData };
|
|
11
|
+
delete updatedScopeData.complementaryArea;
|
|
12
|
+
updatedScopeData.isComplementaryAreaVisible = true;
|
|
13
|
+
stateAccumulator[ scope ] = updatedScopeData;
|
|
14
|
+
return stateAccumulator;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return stateAccumulator;
|
|
18
|
+
},
|
|
19
|
+
{ ...state }
|
|
20
|
+
);
|
|
21
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import isPreferencesData from '../../is-preferences-data';
|
|
2
|
+
import type { ScopedPreferences } from '../../types';
|
|
3
|
+
|
|
4
|
+
const copyScope = ( scope: unknown ) =>
|
|
5
|
+
isPreferencesData( scope ) ? { ...scope } : undefined;
|
|
6
|
+
|
|
7
|
+
export default function convertEditorSettings( data: ScopedPreferences ) {
|
|
8
|
+
const newData = { ...data };
|
|
9
|
+
const editPost = copyScope( data[ 'core/edit-post' ] );
|
|
10
|
+
const editSite = copyScope( data[ 'core/edit-site' ] );
|
|
11
|
+
const settingsToMoveToCore = [
|
|
12
|
+
'allowRightClickOverrides',
|
|
13
|
+
'distractionFree',
|
|
14
|
+
'editorMode',
|
|
15
|
+
'fixedToolbar',
|
|
16
|
+
'focusMode',
|
|
17
|
+
'hiddenBlockTypes',
|
|
18
|
+
'inactivePanels',
|
|
19
|
+
'keepCaretInsideBlock',
|
|
20
|
+
'mostUsedBlocks',
|
|
21
|
+
'openPanels',
|
|
22
|
+
'showBlockBreadcrumbs',
|
|
23
|
+
'showIconLabels',
|
|
24
|
+
'showListViewByDefault',
|
|
25
|
+
'isPublishSidebarEnabled',
|
|
26
|
+
'isComplementaryAreaVisible',
|
|
27
|
+
'pinnedItems',
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
settingsToMoveToCore.forEach( ( setting ) => {
|
|
31
|
+
if ( editPost?.[ setting ] !== undefined ) {
|
|
32
|
+
newData.core = {
|
|
33
|
+
...copyScope( newData.core ),
|
|
34
|
+
[ setting ]: editPost[ setting ],
|
|
35
|
+
};
|
|
36
|
+
delete editPost[ setting ];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if ( editSite?.[ setting ] !== undefined ) {
|
|
40
|
+
delete editSite[ setting ];
|
|
41
|
+
}
|
|
42
|
+
} );
|
|
43
|
+
|
|
44
|
+
// Empty and invalid scopes are removed rather than persisted.
|
|
45
|
+
if ( editPost && Object.keys( editPost ).length > 0 ) {
|
|
46
|
+
newData[ 'core/edit-post' ] = editPost;
|
|
47
|
+
} else {
|
|
48
|
+
delete newData[ 'core/edit-post' ];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if ( editSite && Object.keys( editSite ).length > 0 ) {
|
|
52
|
+
newData[ 'core/edit-site' ] = editSite;
|
|
53
|
+
} else {
|
|
54
|
+
delete newData[ 'core/edit-site' ];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return newData;
|
|
58
|
+
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import convertComplementaryAreas from './convert-complementary-areas';
|
|
2
2
|
import convertEditorSettings from './convert-editor-settings';
|
|
3
|
+
import type { ScopedPreferences } from '../../types';
|
|
3
4
|
|
|
4
|
-
export default function convertPreferencesPackageData(
|
|
5
|
+
export default function convertPreferencesPackageData(
|
|
6
|
+
data: ScopedPreferences
|
|
7
|
+
) {
|
|
5
8
|
let newData = convertComplementaryAreas( data );
|
|
6
9
|
newData = convertEditorSettings( newData );
|
|
7
10
|
return newData;
|
|
@@ -31,4 +31,14 @@ describe( 'convertComplementaryAreas', () => {
|
|
|
31
31
|
|
|
32
32
|
expect( convertComplementaryAreas( input ) ).toEqual( expectedOutput );
|
|
33
33
|
} );
|
|
34
|
+
|
|
35
|
+
it( 'does not mutate its input', () => {
|
|
36
|
+
const input = {
|
|
37
|
+
'core/edit-post': { complementaryArea: 'edit-post/block' },
|
|
38
|
+
};
|
|
39
|
+
const snapshot = structuredClone( input );
|
|
40
|
+
|
|
41
|
+
convertComplementaryAreas( input );
|
|
42
|
+
expect( input ).toEqual( snapshot );
|
|
43
|
+
} );
|
|
34
44
|
} );
|
|
@@ -17,4 +17,39 @@ describe( 'convertEditorSettings', () => {
|
|
|
17
17
|
|
|
18
18
|
expect( convertEditorSettings( input ) ).toEqual( expectedOutput );
|
|
19
19
|
} );
|
|
20
|
+
|
|
21
|
+
it( 'does not mutate its input', () => {
|
|
22
|
+
const input = {
|
|
23
|
+
'core/edit-post': { fixedToolbar: true, other: 1 },
|
|
24
|
+
'core/edit-site': { fixedToolbar: true },
|
|
25
|
+
};
|
|
26
|
+
const snapshot = structuredClone( input );
|
|
27
|
+
|
|
28
|
+
expect( convertEditorSettings( input ) ).toEqual( {
|
|
29
|
+
core: { fixedToolbar: true },
|
|
30
|
+
'core/edit-post': { other: 1 },
|
|
31
|
+
} );
|
|
32
|
+
expect( input ).toEqual( snapshot );
|
|
33
|
+
} );
|
|
34
|
+
|
|
35
|
+
it.each( [ [ 'abc' ], [ [] ], [ null ], [ {} ] ] )(
|
|
36
|
+
'removes an editor scope that is %j',
|
|
37
|
+
( scope ) => {
|
|
38
|
+
expect(
|
|
39
|
+
convertEditorSettings( {
|
|
40
|
+
'core/edit-post': scope,
|
|
41
|
+
'core/edit-site': scope,
|
|
42
|
+
} )
|
|
43
|
+
).toEqual( {} );
|
|
44
|
+
}
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
it( 'replaces a `core` scope that is not an object', () => {
|
|
48
|
+
expect(
|
|
49
|
+
convertEditorSettings( {
|
|
50
|
+
core: 'abc',
|
|
51
|
+
'core/edit-post': { fixedToolbar: true },
|
|
52
|
+
} )
|
|
53
|
+
).toEqual( { core: { fixedToolbar: true } } );
|
|
54
|
+
} );
|
|
20
55
|
} );
|