@wordpress/interface 4.3.0 → 4.5.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.
@@ -1,87 +1,87 @@
1
1
  /**
2
- * Returns an action object used in signalling that an active area should be changed.
3
- *
4
- * @param {string} itemType Type of item.
5
- * @param {string} scope Item scope.
6
- * @param {string} item Item identifier.
7
- *
8
- * @return {Object} Action object.
2
+ * WordPress dependencies
9
3
  */
10
- function setSingleEnableItem( itemType, scope, item ) {
11
- return {
12
- type: 'SET_SINGLE_ENABLE_ITEM',
13
- itemType,
14
- scope,
15
- item,
16
- };
17
- }
4
+ import deprecated from '@wordpress/deprecated';
5
+ import { store as preferencesStore } from '@wordpress/preferences';
18
6
 
19
7
  /**
20
- * Returns an action object used in signalling that a complementary item should be enabled.
8
+ * Enable the complementary area.
21
9
  *
22
10
  * @param {string} scope Complementary area scope.
23
11
  * @param {string} area Area identifier.
24
- *
25
- * @return {Object} Action object.
26
12
  */
27
- export function enableComplementaryArea( scope, area ) {
28
- return setSingleEnableItem( 'complementaryArea', scope, area );
29
- }
13
+ export const enableComplementaryArea = ( scope, area ) => ( { registry } ) => {
14
+ // Return early if there's no area.
15
+ if ( ! area ) {
16
+ return;
17
+ }
18
+
19
+ registry
20
+ .dispatch( preferencesStore )
21
+ .set( scope, 'complementaryArea', area );
22
+ };
30
23
 
31
24
  /**
32
- * Returns an action object used in signalling that the complementary area of a given scope should be disabled.
25
+ * Disable the complementary area.
33
26
  *
34
27
  * @param {string} scope Complementary area scope.
35
- *
36
- * @return {Object} Action object.
37
28
  */
38
- export function disableComplementaryArea( scope ) {
39
- return setSingleEnableItem( 'complementaryArea', scope, undefined );
40
- }
29
+ export const disableComplementaryArea = ( scope ) => ( { registry } ) => {
30
+ registry
31
+ .dispatch( preferencesStore )
32
+ .set( scope, 'complementaryArea', null );
33
+ };
41
34
 
42
35
  /**
43
- * Returns an action object to make an area enabled/disabled.
36
+ * Pins an item.
44
37
  *
45
- * @param {string} itemType Type of item.
46
- * @param {string} scope Item scope.
47
- * @param {string} item Item identifier.
48
- * @param {boolean} isEnable Boolean indicating if an area should be pinned or not.
38
+ * @param {string} scope Item scope.
39
+ * @param {string} item Item identifier.
49
40
  *
50
41
  * @return {Object} Action object.
51
42
  */
52
- function setMultipleEnableItem( itemType, scope, item, isEnable ) {
53
- return {
54
- type: 'SET_MULTIPLE_ENABLE_ITEM',
55
- itemType,
56
- scope,
57
- item,
58
- isEnable,
59
- };
60
- }
43
+ export const pinItem = ( scope, item ) => ( { registry } ) => {
44
+ // Return early if there's no item.
45
+ if ( ! item ) {
46
+ return;
47
+ }
61
48
 
62
- /**
63
- * Returns an action object used in signalling that an item should be pinned.
64
- *
65
- * @param {string} scope Item scope.
66
- * @param {string} itemId Item identifier.
67
- *
68
- * @return {Object} Action object.
69
- */
70
- export function pinItem( scope, itemId ) {
71
- return setMultipleEnableItem( 'pinnedItems', scope, itemId, true );
72
- }
49
+ const pinnedItems = registry
50
+ .select( preferencesStore )
51
+ .get( scope, 'pinnedItems' );
52
+
53
+ // The item is already pinned, there's nothing to do.
54
+ if ( pinnedItems?.[ item ] === true ) {
55
+ return;
56
+ }
57
+
58
+ registry.dispatch( preferencesStore ).set( scope, 'pinnedItems', {
59
+ ...pinnedItems,
60
+ [ item ]: true,
61
+ } );
62
+ };
73
63
 
74
64
  /**
75
- * Returns an action object used in signalling that an item should be unpinned.
76
- *
77
- * @param {string} scope Item scope.
78
- * @param {string} itemId Item identifier.
65
+ * Unpins an item.
79
66
  *
80
- * @return {Object} Action object.
67
+ * @param {string} scope Item scope.
68
+ * @param {string} item Item identifier.
81
69
  */
82
- export function unpinItem( scope, itemId ) {
83
- return setMultipleEnableItem( 'pinnedItems', scope, itemId, false );
84
- }
70
+ export const unpinItem = ( scope, item ) => ( { registry } ) => {
71
+ // Return early if there's no item.
72
+ if ( ! item ) {
73
+ return;
74
+ }
75
+
76
+ const pinnedItems = registry
77
+ .select( preferencesStore )
78
+ .get( scope, 'pinnedItems' );
79
+
80
+ registry.dispatch( preferencesStore ).set( scope, 'pinnedItems', {
81
+ ...pinnedItems,
82
+ [ item ]: false,
83
+ } );
84
+ };
85
85
 
86
86
  /**
87
87
  * Returns an action object used in signalling that a feature should be toggled.
@@ -90,9 +90,13 @@ export function unpinItem( scope, itemId ) {
90
90
  * @param {string} featureName The feature name.
91
91
  */
92
92
  export function toggleFeature( scope, featureName ) {
93
- return function ( { select, dispatch } ) {
94
- const currentValue = select.isFeatureActive( scope, featureName );
95
- dispatch.setFeatureValue( scope, featureName, ! currentValue );
93
+ return function ( { registry } ) {
94
+ deprecated( `wp.dispatch( 'core/interface' ).toggleFeature`, {
95
+ since: '6.0',
96
+ alternative: `wp.dispatch( 'core/preferences' ).toggle`,
97
+ } );
98
+
99
+ registry.dispatch( preferencesStore ).toggle( scope, featureName );
96
100
  };
97
101
  }
98
102
 
@@ -107,11 +111,15 @@ export function toggleFeature( scope, featureName ) {
107
111
  * @return {Object} Action object.
108
112
  */
109
113
  export function setFeatureValue( scope, featureName, value ) {
110
- return {
111
- type: 'SET_FEATURE_VALUE',
112
- scope,
113
- featureName,
114
- value: !! value,
114
+ return function ( { registry } ) {
115
+ deprecated( `wp.dispatch( 'core/interface' ).setFeatureValue`, {
116
+ since: '6.0',
117
+ alternative: `wp.dispatch( 'core/preferences' ).set`,
118
+ } );
119
+
120
+ registry
121
+ .dispatch( preferencesStore )
122
+ .set( scope, featureName, !! value );
115
123
  };
116
124
  }
117
125
 
@@ -124,9 +132,12 @@ export function setFeatureValue( scope, featureName, value ) {
124
132
  * @return {Object} Action object.
125
133
  */
126
134
  export function setFeatureDefaults( scope, defaults ) {
127
- return {
128
- type: 'SET_FEATURE_DEFAULTS',
129
- scope,
130
- defaults,
135
+ return function ( { registry } ) {
136
+ deprecated( `wp.dispatch( 'core/interface' ).setFeatureDefaults`, {
137
+ since: '6.0',
138
+ alternative: `wp.dispatch( 'core/preferences' ).setDefaults`,
139
+ } );
140
+
141
+ registry.dispatch( preferencesStore ).setDefaults( scope, defaults );
131
142
  };
132
143
  }
@@ -1,23 +1,15 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import { createReduxStore, registerStore } from '@wordpress/data';
4
+ import { createReduxStore, register } from '@wordpress/data';
5
5
 
6
6
  /**
7
7
  * Internal dependencies
8
8
  */
9
- import reducer from './reducer';
10
9
  import * as actions from './actions';
11
10
  import * as selectors from './selectors';
12
11
  import { STORE_NAME } from './constants';
13
12
 
14
- const storeConfig = {
15
- reducer,
16
- actions,
17
- selectors,
18
- persist: [ 'enableItems', 'preferences' ],
19
- };
20
-
21
13
  /**
22
14
  * Store definition for the interface namespace.
23
15
  *
@@ -25,8 +17,12 @@ const storeConfig = {
25
17
  *
26
18
  * @type {Object}
27
19
  */
28
- export const store = createReduxStore( STORE_NAME, storeConfig );
20
+ export const store = createReduxStore( STORE_NAME, {
21
+ reducer: () => {},
22
+ actions,
23
+ selectors,
24
+ } );
29
25
 
30
26
  // Once we build a more generic persistence plugin that works across types of stores
31
27
  // we'd be able to replace this with a register call.
32
- registerStore( STORE_NAME, storeConfig );
28
+ register( store );
@@ -1,20 +1,9 @@
1
1
  /**
2
- * External dependencies
2
+ * WordPress dependencies
3
3
  */
4
- import { get } from 'lodash';
5
-
6
- /**
7
- * Returns the item that is enabled in a given scope.
8
- *
9
- * @param {Object} state Global application state.
10
- * @param {string} itemType Type of item.
11
- * @param {string} scope Item scope.
12
- *
13
- * @return {?string|null} The item that is enabled in the passed scope and type.
14
- */
15
- function getSingleEnableItem( state, itemType, scope ) {
16
- return get( state.enableItems.singleEnableItems, [ itemType, scope ] );
17
- }
4
+ import { createRegistrySelector } from '@wordpress/data';
5
+ import deprecated from '@wordpress/deprecated';
6
+ import { store as preferencesStore } from '@wordpress/preferences';
18
7
 
19
8
  /**
20
9
  * Returns the complementary area that is active in a given scope.
@@ -24,27 +13,11 @@ function getSingleEnableItem( state, itemType, scope ) {
24
13
  *
25
14
  * @return {string} The complementary area that is active in the given scope.
26
15
  */
27
- export function getActiveComplementaryArea( state, scope ) {
28
- return getSingleEnableItem( state, 'complementaryArea', scope );
29
- }
30
-
31
- /**
32
- * Returns a boolean indicating if an item is enabled or not in a given scope.
33
- *
34
- * @param {Object} state Global application state.
35
- * @param {string} itemType Type of item.
36
- * @param {string} scope Scope.
37
- * @param {string} item Item to check.
38
- *
39
- * @return {boolean|undefined} True if the item is enabled, false otherwise if the item is explicitly disabled, and undefined if there is no information for that item.
40
- */
41
- function isMultipleEnabledItemEnabled( state, itemType, scope, item ) {
42
- return get( state.enableItems.multipleEnableItems, [
43
- itemType,
44
- scope,
45
- item,
46
- ] );
47
- }
16
+ export const getActiveComplementaryArea = createRegistrySelector(
17
+ ( select ) => ( state, scope ) => {
18
+ return select( preferencesStore ).get( scope, 'complementaryArea' );
19
+ }
20
+ );
48
21
 
49
22
  /**
50
23
  * Returns a boolean indicating if an item is pinned or not.
@@ -55,12 +28,15 @@ function isMultipleEnabledItemEnabled( state, itemType, scope, item ) {
55
28
  *
56
29
  * @return {boolean} True if the item is pinned and false otherwise.
57
30
  */
58
- export function isItemPinned( state, scope, item ) {
59
- return (
60
- isMultipleEnabledItemEnabled( state, 'pinnedItems', scope, item ) !==
61
- false
62
- );
63
- }
31
+ export const isItemPinned = createRegistrySelector(
32
+ ( select ) => ( state, scope, item ) => {
33
+ const pinnedItems = select( preferencesStore ).get(
34
+ scope,
35
+ 'pinnedItems'
36
+ );
37
+ return pinnedItems?.[ item ] ?? true;
38
+ }
39
+ );
64
40
 
65
41
  /**
66
42
  * Returns a boolean indicating whether a feature is active for a particular
@@ -72,12 +48,16 @@ export function isItemPinned( state, scope, item ) {
72
48
  *
73
49
  * @return {boolean} Is the feature enabled?
74
50
  */
75
- export function isFeatureActive( state, scope, featureName ) {
76
- const featureValue = state.preferences.features[ scope ]?.[ featureName ];
77
- const defaultedFeatureValue =
78
- featureValue !== undefined
79
- ? featureValue
80
- : state.preferenceDefaults.features[ scope ]?.[ featureName ];
51
+ export const isFeatureActive = createRegistrySelector(
52
+ ( select ) => ( state, scope, featureName ) => {
53
+ deprecated(
54
+ `wp.select( 'core/interface' ).isFeatureActive( scope, featureName )`,
55
+ {
56
+ since: '6.0',
57
+ alternative: `!! wp.select( 'core/preferences' ).isFeatureActive( scope, featureName )`,
58
+ }
59
+ );
81
60
 
82
- return !! defaultedFeatureValue;
83
- }
61
+ return !! select( preferencesStore ).get( scope, featureName );
62
+ }
63
+ );
@@ -0,0 +1,259 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { createRegistry } from '@wordpress/data';
5
+ import { store as preferencesStore } from '@wordpress/preferences';
6
+
7
+ /**
8
+ * Internal dependencies
9
+ */
10
+ import { store as interfaceStore } from '../';
11
+
12
+ function createRegistryWithStores() {
13
+ // Create a registry and register used stores.
14
+ const registry = createRegistry();
15
+ [ interfaceStore, preferencesStore ].forEach( registry.register );
16
+ return registry;
17
+ }
18
+
19
+ describe( 'actions', () => {
20
+ let registry;
21
+ beforeEach( () => {
22
+ registry = createRegistryWithStores();
23
+ } );
24
+
25
+ describe( 'enableComplementaryArea', () => {
26
+ it( 'sets a single area as active in the complementary area', () => {
27
+ // Starts off as `undefined`.
28
+ expect(
29
+ registry
30
+ .select( interfaceStore )
31
+ .getActiveComplementaryArea( 'my-plugin' )
32
+ ).toBeUndefined();
33
+
34
+ registry
35
+ .dispatch( interfaceStore )
36
+ .enableComplementaryArea( 'my-plugin', 'custom-sidebar-1' );
37
+
38
+ expect(
39
+ registry
40
+ .select( interfaceStore )
41
+ .getActiveComplementaryArea( 'my-plugin' )
42
+ ).toBe( 'custom-sidebar-1' );
43
+
44
+ registry
45
+ .dispatch( interfaceStore )
46
+ .enableComplementaryArea( 'my-plugin', 'custom-sidebar-2' );
47
+
48
+ expect(
49
+ registry
50
+ .select( interfaceStore )
51
+ .getActiveComplementaryArea( 'my-plugin' )
52
+ ).toBe( 'custom-sidebar-2' );
53
+ } );
54
+ } );
55
+
56
+ describe( 'disableComplementaryArea', () => {
57
+ it( 'removes any assignment to a complementary area', () => {
58
+ registry
59
+ .dispatch( interfaceStore )
60
+ .enableComplementaryArea( 'my-plugin', 'custom-sidebar' );
61
+
62
+ expect(
63
+ registry
64
+ .select( interfaceStore )
65
+ .getActiveComplementaryArea( 'my-plugin' )
66
+ ).toBe( 'custom-sidebar' );
67
+
68
+ registry
69
+ .dispatch( interfaceStore )
70
+ .disableComplementaryArea( 'my-plugin' );
71
+
72
+ expect(
73
+ registry
74
+ .select( interfaceStore )
75
+ .getActiveComplementaryArea( 'my-plugin' )
76
+ ).toBeNull();
77
+ } );
78
+ } );
79
+
80
+ describe( 'pinItem / unpinItem', () => {
81
+ it( 'can be used to pin and unpin multiple items using successive calls', () => {
82
+ // Items are pinned by default.
83
+ expect(
84
+ registry
85
+ .select( interfaceStore )
86
+ .isItemPinned( 'my-plugin', 'ui-item-1' )
87
+ ).toBe( true );
88
+
89
+ expect(
90
+ registry
91
+ .select( interfaceStore )
92
+ .isItemPinned( 'my-plugin', 'ui-item-2' )
93
+ ).toBe( true );
94
+
95
+ // Unpinning the default value works.
96
+ registry
97
+ .dispatch( interfaceStore )
98
+ .unpinItem( 'my-plugin', 'ui-item-1' );
99
+
100
+ registry
101
+ .dispatch( interfaceStore )
102
+ .unpinItem( 'my-plugin', 'ui-item-2' );
103
+
104
+ expect(
105
+ registry
106
+ .select( interfaceStore )
107
+ .isItemPinned( 'my-plugin', 'ui-item-1' )
108
+ ).toBe( false );
109
+
110
+ expect(
111
+ registry
112
+ .select( interfaceStore )
113
+ .isItemPinned( 'my-plugin', 'ui-item-2' )
114
+ ).toBe( false );
115
+
116
+ // Now explicitly set the items to be pinned.
117
+ registry
118
+ .dispatch( interfaceStore )
119
+ .pinItem( 'my-plugin', 'ui-item-1' );
120
+
121
+ registry
122
+ .dispatch( interfaceStore )
123
+ .pinItem( 'my-plugin', 'ui-item-2' );
124
+
125
+ expect(
126
+ registry
127
+ .select( interfaceStore )
128
+ .isItemPinned( 'my-plugin', 'ui-item-1' )
129
+ ).toBe( true );
130
+
131
+ expect(
132
+ registry
133
+ .select( interfaceStore )
134
+ .isItemPinned( 'my-plugin', 'ui-item-2' )
135
+ ).toBe( true );
136
+
137
+ // Unpinning should still work.
138
+ registry
139
+ .dispatch( interfaceStore )
140
+ .unpinItem( 'my-plugin', 'ui-item-1' );
141
+
142
+ registry
143
+ .dispatch( interfaceStore )
144
+ .unpinItem( 'my-plugin', 'ui-item-2' );
145
+
146
+ expect(
147
+ registry
148
+ .select( interfaceStore )
149
+ .isItemPinned( 'my-plugin', 'ui-item-1' )
150
+ ).toBe( false );
151
+
152
+ expect(
153
+ registry
154
+ .select( interfaceStore )
155
+ .isItemPinned( 'my-plugin', 'ui-item-2' )
156
+ ).toBe( false );
157
+ } );
158
+ } );
159
+
160
+ describe( 'setFeatureDefaults', () => {
161
+ it( 'results in default values being present', () => {
162
+ registry.dispatch( interfaceStore ).setFeatureDefaults( 'test', {
163
+ feature1: true,
164
+ feature2: false,
165
+ } );
166
+
167
+ expect(
168
+ registry
169
+ .select( interfaceStore )
170
+ .isFeatureActive( 'test', 'feature1' )
171
+ ).toBe( true );
172
+ expect(
173
+ registry
174
+ .select( interfaceStore )
175
+ .isFeatureActive( 'test', 'feature2' )
176
+ ).toBe( false );
177
+
178
+ // Expect a deprecation message.
179
+ expect( console ).toHaveWarned();
180
+ } );
181
+ } );
182
+
183
+ describe( 'setFeatureValue', () => {
184
+ it( 'sets a feature to a boolean value', () => {
185
+ registry
186
+ .dispatch( interfaceStore )
187
+ .setFeatureValue( 'test', 'feature1', false );
188
+ registry
189
+ .dispatch( interfaceStore )
190
+ .setFeatureValue( 'test', 'feature2', true );
191
+ expect(
192
+ registry
193
+ .select( interfaceStore )
194
+ .isFeatureActive( 'test', 'feature1' )
195
+ ).toBe( false );
196
+ expect(
197
+ registry
198
+ .select( interfaceStore )
199
+ .isFeatureActive( 'test', 'feature2' )
200
+ ).toBe( true );
201
+
202
+ // Expect a deprecation message.
203
+ expect( console ).toHaveWarned();
204
+ } );
205
+
206
+ it( 'coerces non-boolean values into booleans', () => {
207
+ registry
208
+ .dispatch( interfaceStore )
209
+ .setFeatureValue( 'test', 'feature1', 'avocado' );
210
+ registry
211
+ .dispatch( interfaceStore )
212
+ .setFeatureValue( 'test', 'feature2', 0 );
213
+
214
+ expect(
215
+ registry
216
+ .select( interfaceStore )
217
+ .isFeatureActive( 'test', 'feature1' )
218
+ ).toBe( true );
219
+ expect(
220
+ registry
221
+ .select( interfaceStore )
222
+ .isFeatureActive( 'test', 'feature2' )
223
+ ).toBe( false );
224
+ } );
225
+ } );
226
+
227
+ describe( 'toggleFeature', () => {
228
+ it( 'changes a value that was true to false', () => {
229
+ registry
230
+ .dispatch( interfaceStore )
231
+ .setFeatureValue( 'test', 'feature1', true );
232
+ registry
233
+ .dispatch( interfaceStore )
234
+ .toggleFeature( 'test', 'feature1' );
235
+ expect(
236
+ registry
237
+ .select( interfaceStore )
238
+ .isFeatureActive( 'test', 'feature1' )
239
+ ).toBe( false );
240
+
241
+ // Expect a deprecation message.
242
+ expect( console ).toHaveWarned();
243
+ } );
244
+
245
+ it( 'changes a value that was false to true', () => {
246
+ registry
247
+ .dispatch( interfaceStore )
248
+ .setFeatureValue( 'test', 'feature1', false );
249
+ registry
250
+ .dispatch( interfaceStore )
251
+ .toggleFeature( 'test', 'feature1' );
252
+ expect(
253
+ registry
254
+ .select( interfaceStore )
255
+ .isFeatureActive( 'test', 'feature1' )
256
+ ).toBe( true );
257
+ } );
258
+ } );
259
+ } );