@wordpress/preferences 4.33.1 → 4.34.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/CHANGELOG.md +6 -0
- package/README.md +10 -10
- package/build/components/index.js +1 -0
- package/build/components/index.js.map +2 -2
- package/build/components/preference-base-option/index.js +8 -1
- package/build/components/preference-base-option/index.js.map +3 -3
- package/build/components/preference-base-option/types.js +17 -0
- package/build/components/preference-base-option/types.js.map +7 -0
- package/build/components/preference-toggle-control/index.js +3 -2
- package/build/components/preference-toggle-control/index.js.map +3 -3
- package/build/components/preference-toggle-menu-item/index.js +2 -1
- package/build/components/preference-toggle-menu-item/index.js.map +3 -3
- package/build/components/preferences-modal/index.js +5 -1
- package/build/components/preferences-modal/index.js.map +3 -3
- package/build/components/preferences-modal/types.js +17 -0
- package/build/components/preferences-modal/types.js.map +7 -0
- package/build/components/preferences-modal-section/index.js +1 -0
- package/build/components/preferences-modal-section/index.js.map +3 -3
- package/build/components/preferences-modal-tabs/index.js +25 -18
- package/build/components/preferences-modal-tabs/index.js.map +3 -3
- package/build/index.js +1 -0
- package/build/index.js.map +2 -2
- package/build/lock-unlock.js +1 -0
- package/build/lock-unlock.js.map +2 -2
- package/build/private-apis.js +1 -0
- package/build/private-apis.js.map +2 -2
- package/build/store/actions.js +1 -0
- package/build/store/actions.js.map +3 -3
- package/build/store/constants.js +1 -0
- package/build/store/constants.js.map +3 -3
- package/build/store/index.js +1 -0
- package/build/store/index.js.map +3 -3
- package/build/store/reducer.js +1 -0
- package/build/store/reducer.js.map +3 -3
- package/build/store/selectors.js +7 -4
- package/build/store/selectors.js.map +3 -3
- package/build/store/types.js +17 -0
- package/build/store/types.js.map +7 -0
- package/build-module/components/index.js.map +1 -1
- package/build-module/components/preference-base-option/index.js +7 -1
- package/build-module/components/preference-base-option/index.js.map +3 -3
- package/build-module/components/preference-base-option/types.js +1 -0
- package/build-module/components/preference-base-option/types.js.map +7 -0
- package/build-module/components/preference-toggle-control/index.js +2 -2
- package/build-module/components/preference-toggle-control/index.js.map +3 -3
- package/build-module/components/preference-toggle-menu-item/index.js +1 -1
- package/build-module/components/preference-toggle-menu-item/index.js.map +3 -3
- package/build-module/components/preferences-modal/index.js +4 -1
- package/build-module/components/preferences-modal/index.js.map +3 -3
- package/build-module/components/preferences-modal/types.js +1 -0
- package/build-module/components/preferences-modal/types.js.map +7 -0
- package/build-module/components/preferences-modal-section/index.js.map +3 -3
- package/build-module/components/preferences-modal-tabs/index.js +24 -18
- package/build-module/components/preferences-modal-tabs/index.js.map +3 -3
- package/build-module/index.js.map +1 -1
- package/build-module/lock-unlock.js.map +1 -1
- package/build-module/private-apis.js.map +1 -1
- package/build-module/store/actions.js.map +3 -3
- package/build-module/store/constants.js.map +3 -3
- package/build-module/store/index.js.map +3 -3
- package/build-module/store/reducer.js.map +3 -3
- package/build-module/store/selectors.js +6 -4
- package/build-module/store/selectors.js.map +3 -3
- package/build-module/store/types.js +1 -0
- package/build-module/store/types.js.map +7 -0
- package/build-types/components/index.d.ts +2 -0
- package/build-types/components/index.d.ts.map +1 -0
- package/build-types/components/preference-base-option/index.d.ts +7 -0
- package/build-types/components/preference-base-option/index.d.ts.map +1 -0
- package/build-types/components/preference-base-option/types.d.ts +11 -0
- package/build-types/components/preference-base-option/types.d.ts.map +1 -0
- package/build-types/components/preference-toggle-control/index.d.ts +9 -0
- package/build-types/components/preference-toggle-control/index.d.ts.map +1 -0
- package/build-types/components/preference-toggle-menu-item/index.d.ts +18 -0
- package/build-types/components/preference-toggle-menu-item/index.d.ts.map +1 -0
- package/build-types/components/preferences-modal/index.d.ts +6 -0
- package/build-types/components/preferences-modal/index.d.ts.map +1 -0
- package/build-types/components/preferences-modal/types.d.ts +11 -0
- package/build-types/components/preferences-modal/types.d.ts.map +1 -0
- package/build-types/components/preferences-modal-section/index.d.ts +8 -0
- package/build-types/components/preferences-modal-section/index.d.ts.map +1 -0
- package/build-types/components/preferences-modal-tabs/index.d.ts +9 -0
- package/build-types/components/preferences-modal-tabs/index.d.ts.map +1 -0
- package/build-types/index.d.ts +4 -0
- package/build-types/index.d.ts.map +1 -0
- package/build-types/lock-unlock.d.ts +2 -0
- package/build-types/lock-unlock.d.ts.map +1 -0
- package/build-types/private-apis.d.ts +2 -0
- package/build-types/private-apis.d.ts.map +1 -0
- package/build-types/store/actions.d.ts +69 -0
- package/build-types/store/actions.d.ts.map +1 -0
- package/build-types/store/constants.d.ts +5 -0
- package/build-types/store/constants.d.ts.map +1 -0
- package/build-types/store/index.d.ts +10 -0
- package/build-types/store/index.d.ts.map +1 -0
- package/build-types/store/reducer.d.ts +39 -0
- package/build-types/store/reducer.d.ts.map +1 -0
- package/build-types/store/selectors.d.ts +16 -0
- package/build-types/store/selectors.d.ts.map +1 -0
- package/build-types/store/types.d.ts +27 -0
- package/build-types/store/types.d.ts.map +1 -0
- package/package.json +16 -12
- package/src/components/preference-base-option/{index.js → index.tsx} +11 -1
- package/src/components/preference-base-option/types.ts +10 -0
- package/src/components/preference-toggle-control/{index.js → index.tsx} +9 -2
- package/src/components/preference-toggle-menu-item/{index.js → index.tsx} +14 -2
- package/src/components/preferences-modal/{index.js → index.tsx} +8 -1
- package/src/components/preferences-modal/types.ts +11 -0
- package/src/components/preferences-modal-section/{index.js → index.tsx} +7 -1
- package/src/components/preferences-modal-tabs/{index.js → index.tsx} +13 -3
- package/src/store/{actions.js → actions.ts} +48 -18
- package/src/store/{constants.js → constants.ts} +0 -2
- package/src/store/{index.js → index.ts} +6 -3
- package/src/store/{reducer.js → reducer.ts} +23 -10
- package/src/store/selectors.ts +67 -0
- package/src/store/types.ts +29 -0
- package/tsconfig.json +16 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/src/store/selectors.js +0 -57
- /package/src/components/{index.js → index.ts} +0 -0
- /package/src/{index.js → index.ts} +0 -0
- /package/src/{lock-unlock.js → lock-unlock.ts} +0 -0
- /package/src/{private-apis.js → private-apis.ts} +0 -0
- /package/src/store/test/{actions.js → actions.ts} +0 -0
- /package/src/store/test/{reducer.js → reducer.ts} +0 -0
- /package/src/store/test/{selectors.js → selectors.ts} +0 -0
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type {
|
|
5
|
+
ActionObject,
|
|
6
|
+
StoreState,
|
|
7
|
+
WPPreferencesPersistenceLayer,
|
|
8
|
+
} from './types';
|
|
9
|
+
|
|
1
10
|
/**
|
|
2
11
|
* Returns an action object used in signalling that a preference should be
|
|
3
12
|
* toggled.
|
|
@@ -5,13 +14,19 @@
|
|
|
5
14
|
* @param {string} scope The preference scope (e.g. core/edit-post).
|
|
6
15
|
* @param {string} name The preference name.
|
|
7
16
|
*/
|
|
8
|
-
export function toggle( scope, name ) {
|
|
17
|
+
export function toggle( scope: string, name: string ) {
|
|
18
|
+
// @ts-expect-error We cannot properly type this until we have a fully-typed datastore
|
|
9
19
|
return function ( { select, dispatch } ) {
|
|
10
20
|
const currentValue = select.get( scope, name );
|
|
11
21
|
dispatch.set( scope, name, ! currentValue );
|
|
12
22
|
};
|
|
13
23
|
}
|
|
14
24
|
|
|
25
|
+
type SetAction = ActionObject<
|
|
26
|
+
'SET_PREFERENCE_VALUE',
|
|
27
|
+
{ scope: string; name: string; value: any }
|
|
28
|
+
>;
|
|
29
|
+
|
|
15
30
|
/**
|
|
16
31
|
* Returns an action object used in signalling that a preference should be set
|
|
17
32
|
* to a value
|
|
@@ -20,9 +35,9 @@ export function toggle( scope, name ) {
|
|
|
20
35
|
* @param {string} name The preference name.
|
|
21
36
|
* @param {*} value The value to set.
|
|
22
37
|
*
|
|
23
|
-
* @return {
|
|
38
|
+
* @return {SetAction} Action object.
|
|
24
39
|
*/
|
|
25
|
-
export function set( scope, name, value ) {
|
|
40
|
+
export function set( scope: string, name: string, value: any ): SetAction {
|
|
26
41
|
return {
|
|
27
42
|
type: 'SET_PREFERENCE_VALUE',
|
|
28
43
|
scope,
|
|
@@ -31,16 +46,25 @@ export function set( scope, name, value ) {
|
|
|
31
46
|
};
|
|
32
47
|
}
|
|
33
48
|
|
|
49
|
+
type ScopedDefaults = StoreState[ 'defaults' ][ string ];
|
|
50
|
+
type SetDefaultsAction = ActionObject<
|
|
51
|
+
'SET_PREFERENCE_DEFAULTS',
|
|
52
|
+
{ scope: string; defaults: ScopedDefaults }
|
|
53
|
+
>;
|
|
54
|
+
|
|
34
55
|
/**
|
|
35
56
|
* Returns an action object used in signalling that preference defaults should
|
|
36
57
|
* be set.
|
|
37
58
|
*
|
|
38
|
-
* @param
|
|
39
|
-
* @param
|
|
59
|
+
* @param scope The preference scope (e.g. core/edit-post).
|
|
60
|
+
* @param defaults A key/value map of preference names to values.
|
|
40
61
|
*
|
|
41
|
-
* @return
|
|
62
|
+
* @return Action object.
|
|
42
63
|
*/
|
|
43
|
-
export function setDefaults(
|
|
64
|
+
export function setDefaults(
|
|
65
|
+
scope: string,
|
|
66
|
+
defaults: ScopedDefaults
|
|
67
|
+
): SetDefaultsAction {
|
|
44
68
|
return {
|
|
45
69
|
type: 'SET_PREFERENCE_DEFAULTS',
|
|
46
70
|
scope,
|
|
@@ -48,14 +72,13 @@ export function setDefaults( scope, defaults ) {
|
|
|
48
72
|
};
|
|
49
73
|
}
|
|
50
74
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
*/
|
|
75
|
+
type SetPersistenceLayerAction< D extends Object > = ActionObject<
|
|
76
|
+
'SET_PERSISTENCE_LAYER',
|
|
77
|
+
{
|
|
78
|
+
persistenceLayer: WPPreferencesPersistenceLayer< D >;
|
|
79
|
+
persistedData: D;
|
|
80
|
+
}
|
|
81
|
+
>;
|
|
59
82
|
|
|
60
83
|
/**
|
|
61
84
|
* Sets the persistence layer.
|
|
@@ -68,11 +91,13 @@ export function setDefaults( scope, defaults ) {
|
|
|
68
91
|
* application's lifecycle, before any other actions have been dispatched to
|
|
69
92
|
* the preferences store.
|
|
70
93
|
*
|
|
71
|
-
* @param
|
|
94
|
+
* @param persistenceLayer The persistence layer.
|
|
72
95
|
*
|
|
73
|
-
* @return
|
|
96
|
+
* @return Action object.
|
|
74
97
|
*/
|
|
75
|
-
export async function setPersistenceLayer
|
|
98
|
+
export async function setPersistenceLayer< D extends Object >(
|
|
99
|
+
persistenceLayer: WPPreferencesPersistenceLayer< D >
|
|
100
|
+
): Promise< SetPersistenceLayerAction< D > > {
|
|
76
101
|
const persistedData = await persistenceLayer.get();
|
|
77
102
|
return {
|
|
78
103
|
type: 'SET_PERSISTENCE_LAYER',
|
|
@@ -80,3 +105,8 @@ export async function setPersistenceLayer( persistenceLayer ) {
|
|
|
80
105
|
persistedData,
|
|
81
106
|
};
|
|
82
107
|
}
|
|
108
|
+
|
|
109
|
+
export type AvailableActions =
|
|
110
|
+
| SetAction
|
|
111
|
+
| SetDefaultsAction
|
|
112
|
+
| SetPersistenceLayerAction< any >;
|
|
@@ -10,15 +10,18 @@ import reducer from './reducer';
|
|
|
10
10
|
import * as actions from './actions';
|
|
11
11
|
import * as selectors from './selectors';
|
|
12
12
|
import { STORE_NAME } from './constants';
|
|
13
|
+
import type { StoreState } from './types';
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
16
|
* Store definition for the preferences namespace.
|
|
16
17
|
*
|
|
17
18
|
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
|
|
18
|
-
*
|
|
19
|
-
* @type {Object}
|
|
20
19
|
*/
|
|
21
|
-
export const store = createReduxStore
|
|
20
|
+
export const store = createReduxStore<
|
|
21
|
+
StoreState,
|
|
22
|
+
typeof actions,
|
|
23
|
+
typeof selectors
|
|
24
|
+
>( STORE_NAME, {
|
|
22
25
|
reducer,
|
|
23
26
|
actions,
|
|
24
27
|
selectors,
|
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
4
|
import { combineReducers } from '@wordpress/data';
|
|
5
|
+
/**
|
|
6
|
+
* Internal dependencies
|
|
7
|
+
*/
|
|
8
|
+
import type { StoreState, WPPreferencesPersistenceLayer } from './types';
|
|
9
|
+
import type { AvailableActions } from './actions';
|
|
5
10
|
|
|
6
11
|
/**
|
|
7
12
|
* Reducer returning the defaults for user preferences.
|
|
@@ -9,12 +14,15 @@ import { combineReducers } from '@wordpress/data';
|
|
|
9
14
|
* This is kept intentionally separate from the preferences
|
|
10
15
|
* themselves so that defaults are not persisted.
|
|
11
16
|
*
|
|
12
|
-
* @param
|
|
13
|
-
* @param
|
|
17
|
+
* @param state Current state.
|
|
18
|
+
* @param action Dispatched action.
|
|
14
19
|
*
|
|
15
|
-
* @return
|
|
20
|
+
* @return Updated state.
|
|
16
21
|
*/
|
|
17
|
-
export function defaults(
|
|
22
|
+
export function defaults(
|
|
23
|
+
state: StoreState[ 'defaults' ] = {},
|
|
24
|
+
action: AvailableActions
|
|
25
|
+
): StoreState[ 'defaults' ] {
|
|
18
26
|
if ( action.type === 'SET_PREFERENCE_DEFAULTS' ) {
|
|
19
27
|
const { scope, defaults: values } = action;
|
|
20
28
|
return {
|
|
@@ -29,21 +37,26 @@ export function defaults( state = {}, action ) {
|
|
|
29
37
|
return state;
|
|
30
38
|
}
|
|
31
39
|
|
|
40
|
+
type PreferencesReducer = (
|
|
41
|
+
state: StoreState[ 'preferences' ],
|
|
42
|
+
action: AvailableActions
|
|
43
|
+
) => StoreState[ 'preferences' ];
|
|
44
|
+
|
|
32
45
|
/**
|
|
33
46
|
* Higher order reducer that does the following:
|
|
34
47
|
* - Merges any data from the persistence layer into the state when the
|
|
35
48
|
* `SET_PERSISTENCE_LAYER` action is received.
|
|
36
49
|
* - Passes any preferences changes to the persistence layer.
|
|
37
50
|
*
|
|
38
|
-
* @param
|
|
51
|
+
* @param reducer The preferences reducer.
|
|
39
52
|
*
|
|
40
|
-
* @return
|
|
53
|
+
* @return The enhanced reducer.
|
|
41
54
|
*/
|
|
42
|
-
function withPersistenceLayer( reducer ) {
|
|
43
|
-
let persistenceLayer
|
|
55
|
+
function withPersistenceLayer( reducer: PreferencesReducer ) {
|
|
56
|
+
let persistenceLayer: WPPreferencesPersistenceLayer< any >;
|
|
44
57
|
|
|
45
|
-
return ( state, action ) => {
|
|
46
|
-
//
|
|
58
|
+
return ( state: StoreState[ 'preferences' ], action: AvailableActions ) => {
|
|
59
|
+
// Set up the persistence layer, and return the persisted data
|
|
47
60
|
// as the state.
|
|
48
61
|
if ( action.type === 'SET_PERSISTENCE_LAYER' ) {
|
|
49
62
|
const { persistenceLayer: persistence, persistedData } = action;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import deprecated from '@wordpress/deprecated';
|
|
5
|
+
/**
|
|
6
|
+
* Internal dependencies
|
|
7
|
+
*/
|
|
8
|
+
import type { StoreState } from './types';
|
|
9
|
+
|
|
10
|
+
const withDeprecatedKeys =
|
|
11
|
+
(
|
|
12
|
+
originalGet: ( state: StoreState, scope: string, name: string ) => any
|
|
13
|
+
) =>
|
|
14
|
+
( state: StoreState, scope: string, name: string ) => {
|
|
15
|
+
const settingsToMoveToCore = [
|
|
16
|
+
'allowRightClickOverrides',
|
|
17
|
+
'distractionFree',
|
|
18
|
+
'editorMode',
|
|
19
|
+
'fixedToolbar',
|
|
20
|
+
'focusMode',
|
|
21
|
+
'hiddenBlockTypes',
|
|
22
|
+
'inactivePanels',
|
|
23
|
+
'keepCaretInsideBlock',
|
|
24
|
+
'mostUsedBlocks',
|
|
25
|
+
'openPanels',
|
|
26
|
+
'showBlockBreadcrumbs',
|
|
27
|
+
'showIconLabels',
|
|
28
|
+
'showListViewByDefault',
|
|
29
|
+
'isPublishSidebarEnabled',
|
|
30
|
+
'isComplementaryAreaVisible',
|
|
31
|
+
'pinnedItems',
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
if (
|
|
35
|
+
settingsToMoveToCore.includes( name ) &&
|
|
36
|
+
[ 'core/edit-post', 'core/edit-site' ].includes( scope )
|
|
37
|
+
) {
|
|
38
|
+
deprecated(
|
|
39
|
+
`wp.data.select( 'core/preferences' ).get( '${ scope }', '${ name }' )`,
|
|
40
|
+
{
|
|
41
|
+
since: '6.5',
|
|
42
|
+
alternative: `wp.data.select( 'core/preferences' ).get( 'core', '${ name }' )`,
|
|
43
|
+
}
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
return originalGet( state, 'core', name );
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return originalGet( state, scope, name );
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Returns a boolean indicating whether a prefer is active for a particular
|
|
54
|
+
* scope.
|
|
55
|
+
*
|
|
56
|
+
* @param {StoreState} state The store state.
|
|
57
|
+
* @param {string} scope The scope of the feature (e.g. core/edit-post).
|
|
58
|
+
* @param {string} name The name of the feature.
|
|
59
|
+
*
|
|
60
|
+
* @return {*} Is the feature enabled?
|
|
61
|
+
*/
|
|
62
|
+
export const get = withDeprecatedKeys(
|
|
63
|
+
( state: StoreState, scope: string, name: string ) => {
|
|
64
|
+
const value = state.preferences[ scope ]?.[ name ];
|
|
65
|
+
return value !== undefined ? value : state.defaults[ scope ]?.[ name ];
|
|
66
|
+
}
|
|
67
|
+
);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type StoreState = {
|
|
2
|
+
preferences: { [ k in string ]: { [ p in string ]: any } };
|
|
3
|
+
defaults: { [ k in string ]: { [ p in string ]: any } };
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export type OmitFirstArg< F > = F extends (
|
|
7
|
+
x: any,
|
|
8
|
+
...args: infer P
|
|
9
|
+
) => infer R
|
|
10
|
+
? ( ...args: P ) => R
|
|
11
|
+
: never;
|
|
12
|
+
|
|
13
|
+
export interface WPPreferencesPersistenceLayer< D extends Object > {
|
|
14
|
+
/**
|
|
15
|
+
* An async function that gets data from the persistence layer.
|
|
16
|
+
*/
|
|
17
|
+
get: () => Promise< D >;
|
|
18
|
+
/**
|
|
19
|
+
* A function that sets data in the persistence layer.
|
|
20
|
+
*/
|
|
21
|
+
set: ( value: D ) => void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type ActionObject<
|
|
25
|
+
T extends string,
|
|
26
|
+
D extends Record< Exclude< string, 'type' >, any > = {},
|
|
27
|
+
> = {
|
|
28
|
+
type: T;
|
|
29
|
+
} & D;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/tsconfig.json",
|
|
3
|
+
"extends": "../../tsconfig.base.json",
|
|
4
|
+
"references": [
|
|
5
|
+
{ "path": "../a11y" },
|
|
6
|
+
{ "path": "../components" },
|
|
7
|
+
{ "path": "../compose" },
|
|
8
|
+
{ "path": "../data" },
|
|
9
|
+
{ "path": "../deprecated" },
|
|
10
|
+
{ "path": "../element" },
|
|
11
|
+
{ "path": "../i18n" },
|
|
12
|
+
{ "path": "../icons" },
|
|
13
|
+
{ "path": "../private-apis" }
|
|
14
|
+
],
|
|
15
|
+
"include": [ "src/**/*" ]
|
|
16
|
+
}
|