@wordpress/views 1.19.0 → 1.20.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 +11 -0
- package/README.md +5 -0
- package/build/load-view.cjs +21 -21
- package/build/load-view.cjs.map +2 -2
- package/build/lock-unlock.cjs.map +2 -2
- package/build/resolve-view.cjs +133 -0
- package/build/resolve-view.cjs.map +7 -0
- package/build/types.cjs.map +1 -1
- package/build/use-view-config.cjs.map +2 -2
- package/build/use-view.cjs +37 -57
- package/build/use-view.cjs.map +2 -2
- package/build-module/load-view.mjs +21 -21
- package/build-module/load-view.mjs.map +2 -2
- package/build-module/lock-unlock.mjs.map +2 -2
- package/build-module/resolve-view.mjs +107 -0
- package/build-module/resolve-view.mjs.map +7 -0
- package/build-module/use-view-config.mjs.map +2 -2
- package/build-module/use-view.mjs +37 -60
- package/build-module/use-view.mjs.map +2 -2
- package/build-types/load-view.d.ts +5 -2
- package/build-types/load-view.d.ts.map +1 -1
- package/build-types/lock-unlock.d.ts.map +1 -1
- package/build-types/resolve-view.d.ts +54 -0
- package/build-types/resolve-view.d.ts.map +1 -0
- package/build-types/types.d.ts +19 -21
- package/build-types/types.d.ts.map +1 -1
- package/build-types/use-view-config.d.ts.map +1 -1
- package/build-types/use-view.d.ts +5 -0
- package/build-types/use-view.d.ts.map +1 -1
- package/package.json +13 -8
- package/src/load-view.ts +26 -38
- package/src/lock-unlock.ts +0 -3
- package/src/resolve-view.ts +261 -0
- package/src/test/resolve-view.ts +63 -0
- package/src/test/use-view.tsx +966 -0
- package/src/types.ts +20 -24
- package/src/use-view-config.ts +0 -7
- package/src/use-view.ts +50 -92
- package/build/filter-utils.cjs +0 -134
- package/build/filter-utils.cjs.map +0 -7
- package/build-module/filter-utils.mjs +0 -108
- package/build-module/filter-utils.mjs.map +0 -7
- package/build-types/filter-utils.d.ts +0 -31
- package/build-types/filter-utils.d.ts.map +0 -1
- package/src/filter-utils.ts +0 -180
- package/src/test/filter-utils.ts +0 -522
package/src/load-view.ts
CHANGED
|
@@ -1,58 +1,46 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WordPress dependencies
|
|
3
|
-
*/
|
|
4
1
|
import { select } from '@wordpress/data';
|
|
5
|
-
// @ts-ignore - Preferences package is not typed
|
|
6
2
|
import { store as preferencesStore } from '@wordpress/preferences';
|
|
7
|
-
import type { View } from '@wordpress/dataviews';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Internal dependencies
|
|
11
|
-
*/
|
|
12
3
|
import { generatePreferenceKey } from './preference-keys';
|
|
13
|
-
import {
|
|
14
|
-
import type { ViewConfig } from './types';
|
|
4
|
+
import { resolveView } from './resolve-view';
|
|
5
|
+
import type { ViewConfig, ViewOverrides } from './types';
|
|
15
6
|
|
|
16
7
|
/**
|
|
17
8
|
* Async function for loading view state in route loaders.
|
|
18
9
|
*
|
|
10
|
+
* Resolves the same layers `useView` does, so a route loader and the hook that
|
|
11
|
+
* takes over from it agree on the view.
|
|
12
|
+
*
|
|
19
13
|
* @param config Configuration object for loading the view.
|
|
20
14
|
* @param config.kind Entity kind (e.g., 'postType', 'taxonomy', 'root').
|
|
21
15
|
* @param config.name Specific entity name.
|
|
22
16
|
* @param config.slug View identifier.
|
|
23
17
|
* @param config.defaultView Default view configuration.
|
|
18
|
+
* @param config.defaultLayouts Default layout configurations keyed by layout type.
|
|
24
19
|
* @param config.activeViewOverrides View overrides applied on top but never persisted.
|
|
25
20
|
* @param config.queryParams Object with `page` and/or `search` from URL.
|
|
26
21
|
* @return Promise resolving to the loaded view object.
|
|
27
22
|
*/
|
|
28
23
|
export async function loadView( config: ViewConfig ) {
|
|
29
|
-
const {
|
|
30
|
-
|
|
24
|
+
const {
|
|
25
|
+
kind,
|
|
26
|
+
name,
|
|
27
|
+
slug,
|
|
28
|
+
defaultView,
|
|
29
|
+
defaultLayouts,
|
|
30
|
+
activeViewOverrides,
|
|
31
|
+
queryParams,
|
|
32
|
+
} = config;
|
|
31
33
|
const preferenceKey = generatePreferenceKey( kind, name, slug );
|
|
32
|
-
const persistedView:
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
) as View | undefined;
|
|
36
|
-
|
|
37
|
-
const baseView = persistedView ?? defaultView;
|
|
38
|
-
const page = queryParams?.page ?? 1;
|
|
39
|
-
const search = queryParams?.search ?? '';
|
|
40
|
-
|
|
41
|
-
const rawDefaults =
|
|
42
|
-
config.defaultLayouts?.[
|
|
43
|
-
baseView?.type as keyof typeof config.defaultLayouts
|
|
44
|
-
];
|
|
45
|
-
const layoutTypeDefaults =
|
|
46
|
-
! rawDefaults || rawDefaults === true ? {} : rawDefaults;
|
|
47
|
-
const combinedOverrides = { ...layoutTypeDefaults, ...activeViewOverrides };
|
|
34
|
+
const persistedView: ViewOverrides | undefined = select(
|
|
35
|
+
preferencesStore
|
|
36
|
+
).get( 'core/views', preferenceKey ) as ViewOverrides | undefined;
|
|
48
37
|
|
|
49
|
-
return
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
);
|
|
38
|
+
return resolveView( {
|
|
39
|
+
defaultView,
|
|
40
|
+
defaultLayouts,
|
|
41
|
+
activeViewOverrides,
|
|
42
|
+
persistedView,
|
|
43
|
+
page: queryParams?.page,
|
|
44
|
+
search: queryParams?.search,
|
|
45
|
+
} );
|
|
58
46
|
}
|
package/src/lock-unlock.ts
CHANGED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { dequal } from 'dequal';
|
|
2
|
+
import type { View, Filter, SupportedLayouts } from '@wordpress/dataviews';
|
|
3
|
+
import type { ViewOverrides } from './types';
|
|
4
|
+
|
|
5
|
+
type PlainObject = Record< string, unknown >;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* `page` and `search` are URL-managed: the URL is their only source. They are
|
|
9
|
+
* never persisted, and no other layer may configure them — an absent URL param
|
|
10
|
+
* is indistinguishable from the user having cleared the value, so any fallback
|
|
11
|
+
* would resurrect a cleared search on the next read.
|
|
12
|
+
*/
|
|
13
|
+
const QUERY_PARAMS = [ 'page', 'search' ] as const;
|
|
14
|
+
|
|
15
|
+
function isPlainObject( value: unknown ): value is PlainObject {
|
|
16
|
+
return (
|
|
17
|
+
typeof value === 'object' && value !== null && ! Array.isArray( value )
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Copies a layer without the URL-managed properties.
|
|
23
|
+
*
|
|
24
|
+
* @param layer The layer to copy.
|
|
25
|
+
* @return A copy of the layer with `page` and `search` removed.
|
|
26
|
+
*/
|
|
27
|
+
function withoutQueryParams( layer: unknown ): PlainObject {
|
|
28
|
+
const result: PlainObject = isPlainObject( layer ) ? { ...layer } : {};
|
|
29
|
+
for ( const key of QUERY_PARAMS ) {
|
|
30
|
+
delete result[ key ];
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Merges a layer on top of another, recursing into plain objects so the upper
|
|
37
|
+
* layer only takes over the leaves it declares. Arrays and scalars are leaves:
|
|
38
|
+
* they are replaced wholesale.
|
|
39
|
+
*
|
|
40
|
+
* Nested properties (`layout.styles.<field>.width`, `groupBy.direction`,
|
|
41
|
+
* `sort.direction`) are merged leaf by leaf: a shallow merge would let a layer
|
|
42
|
+
* that only declares one leaf wipe every sibling the layers below provide.
|
|
43
|
+
*
|
|
44
|
+
* @param lower The layer to merge into.
|
|
45
|
+
* @param upper The layer to merge on top.
|
|
46
|
+
* @return The merged layer.
|
|
47
|
+
*/
|
|
48
|
+
function mergeLayer( lower: PlainObject, upper: PlainObject ): PlainObject {
|
|
49
|
+
const result: PlainObject = { ...lower };
|
|
50
|
+
for ( const key of Object.keys( upper ) ) {
|
|
51
|
+
const value = upper[ key ];
|
|
52
|
+
const current = result[ key ];
|
|
53
|
+
result[ key ] =
|
|
54
|
+
isPlainObject( current ) && isPlainObject( value )
|
|
55
|
+
? mergeLayer( current, value )
|
|
56
|
+
: value;
|
|
57
|
+
}
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The leaves of `value` that differ from `base`. Mirrors `mergeLayer`:
|
|
63
|
+
* `mergeLayer( base, diffLayer( value, base ) )` resolves back to `value`.
|
|
64
|
+
*
|
|
65
|
+
* A persisted leaf that equals both `value` and `base` is kept even though it
|
|
66
|
+
* is not a difference: the preference is shared by every view of the same
|
|
67
|
+
* entity, and such a leaf may only differ from the base of *another* view. In
|
|
68
|
+
* this one, where it coincides with the base, a revert is unobservable — so
|
|
69
|
+
* equality cannot mean the user reverted it. Dropping a persisted leaf
|
|
70
|
+
* requires `value` to actually move away from it, back to `base`.
|
|
71
|
+
*
|
|
72
|
+
* @param value The layer to compare.
|
|
73
|
+
* @param base The layer to compare it against.
|
|
74
|
+
* @param persisted The previously persisted leaves.
|
|
75
|
+
* @return The leaves that differ, as a layer of its own.
|
|
76
|
+
*/
|
|
77
|
+
function diffLayer(
|
|
78
|
+
value: PlainObject,
|
|
79
|
+
base: PlainObject,
|
|
80
|
+
persisted: PlainObject = {}
|
|
81
|
+
): PlainObject {
|
|
82
|
+
const result: PlainObject = {};
|
|
83
|
+
for ( const key of Object.keys( value ) ) {
|
|
84
|
+
const next = value[ key ];
|
|
85
|
+
const current = base[ key ];
|
|
86
|
+
const prev = persisted[ key ];
|
|
87
|
+
if ( next === undefined ) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if ( isPlainObject( next ) && isPlainObject( current ) ) {
|
|
91
|
+
const nested = diffLayer(
|
|
92
|
+
next,
|
|
93
|
+
current,
|
|
94
|
+
isPlainObject( prev ) ? prev : {}
|
|
95
|
+
);
|
|
96
|
+
if ( Object.keys( nested ).length > 0 ) {
|
|
97
|
+
result[ key ] = nested;
|
|
98
|
+
}
|
|
99
|
+
} else if ( ! dequal( next, current ) ) {
|
|
100
|
+
result[ key ] = next;
|
|
101
|
+
} else if ( prev !== undefined && dequal( next, prev ) ) {
|
|
102
|
+
result[ key ] = prev;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function getLockedFilters( overrides?: ViewOverrides ): Filter[] {
|
|
109
|
+
return ( overrides?.filters ?? [] ).filter( ( filter ) => filter.isLocked );
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Hoists the locked filters an override provides to the front of the view's
|
|
114
|
+
* filters, replacing any same-field filter. Locked filters are not the user's
|
|
115
|
+
* to change, so they are pinned in place instead of taking part in the layer
|
|
116
|
+
* resolution.
|
|
117
|
+
*
|
|
118
|
+
* @param view The view to apply the locked filters to.
|
|
119
|
+
* @param overrides The active view overrides.
|
|
120
|
+
* @return The view with the locked filters applied.
|
|
121
|
+
*/
|
|
122
|
+
function applyLockedFilters(
|
|
123
|
+
view: PlainObject,
|
|
124
|
+
overrides?: ViewOverrides
|
|
125
|
+
): PlainObject {
|
|
126
|
+
const locked = getLockedFilters( overrides );
|
|
127
|
+
if ( locked.length === 0 ) {
|
|
128
|
+
return view;
|
|
129
|
+
}
|
|
130
|
+
const rest = ( ( view.filters ?? [] ) as Filter[] ).filter(
|
|
131
|
+
( filter ) => ! locked.some( ( f ) => f.field === filter.field )
|
|
132
|
+
);
|
|
133
|
+
return { ...view, filters: [ ...locked, ...rest ] };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
interface ViewLayers {
|
|
137
|
+
defaultView?: View;
|
|
138
|
+
defaultLayouts?: SupportedLayouts;
|
|
139
|
+
activeViewOverrides?: ViewOverrides;
|
|
140
|
+
/**
|
|
141
|
+
* The user's persisted modifications: a partial view holding only the
|
|
142
|
+
* properties they actually changed.
|
|
143
|
+
*/
|
|
144
|
+
persistedView?: ViewOverrides;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
interface ResolveViewArgs extends ViewLayers {
|
|
148
|
+
page?: number;
|
|
149
|
+
search?: string;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Resolves the layers below the user's modifications — what a modification has
|
|
154
|
+
* to differ from to count as one. The layout type selects which entry of
|
|
155
|
+
* `defaultLayouts` applies, so the base only makes sense relative to a type.
|
|
156
|
+
*
|
|
157
|
+
* @param layers The layers to resolve.
|
|
158
|
+
* @param effectiveType The layout type to resolve the base at.
|
|
159
|
+
* @return The resolved base view.
|
|
160
|
+
*/
|
|
161
|
+
function resolveBaseView(
|
|
162
|
+
layers: ViewLayers,
|
|
163
|
+
effectiveType: View[ 'type' ] | undefined
|
|
164
|
+
): PlainObject {
|
|
165
|
+
const { defaultView, defaultLayouts, activeViewOverrides } = layers;
|
|
166
|
+
const layoutDefaults =
|
|
167
|
+
defaultLayouts?.[ effectiveType as keyof SupportedLayouts ];
|
|
168
|
+
return applyLockedFilters(
|
|
169
|
+
[ layoutDefaults, activeViewOverrides ].reduce< PlainObject >(
|
|
170
|
+
( lower, upper ) =>
|
|
171
|
+
mergeLayer( lower, withoutQueryParams( upper ) ),
|
|
172
|
+
withoutQueryParams( defaultView )
|
|
173
|
+
),
|
|
174
|
+
activeViewOverrides
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Resolves a view out of its layers. Later layers override earlier ones:
|
|
180
|
+
*
|
|
181
|
+
* 1. `defaultView`
|
|
182
|
+
* 2. `defaultLayouts` (for the effective type)
|
|
183
|
+
* 3. `activeViewOverrides`
|
|
184
|
+
* 4. the user's persisted modifications
|
|
185
|
+
* 5. the URL query params (`page` and `search` only)
|
|
186
|
+
*
|
|
187
|
+
* @param args See `ResolveViewArgs`.
|
|
188
|
+
* @return The resolved `view`.
|
|
189
|
+
*/
|
|
190
|
+
export function resolveView( args: ResolveViewArgs ): View {
|
|
191
|
+
const { defaultView, activeViewOverrides, persistedView, page, search } =
|
|
192
|
+
args;
|
|
193
|
+
|
|
194
|
+
// Resolve the effective layout type first: it selects which entry of
|
|
195
|
+
// `defaultLayouts` applies, and the layers above the layouts may change it.
|
|
196
|
+
const effectiveType =
|
|
197
|
+
persistedView?.type ?? activeViewOverrides?.type ?? defaultView?.type;
|
|
198
|
+
const baseView = resolveBaseView( args, effectiveType );
|
|
199
|
+
|
|
200
|
+
const view = {
|
|
201
|
+
...applyLockedFilters(
|
|
202
|
+
mergeLayer( baseView, withoutQueryParams( persistedView ) ),
|
|
203
|
+
activeViewOverrides
|
|
204
|
+
),
|
|
205
|
+
page: Number( page ?? 1 ),
|
|
206
|
+
search: search ?? '',
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
// The layers are resolved as plain objects: `View` requires a `type`, which
|
|
210
|
+
// only the layers taken together are guaranteed to provide.
|
|
211
|
+
return view as unknown as View;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* The user's modifications: the leaves of the view they interacted with that
|
|
216
|
+
* the layers below do not already resolve to. This is what gets persisted, so
|
|
217
|
+
* that a change to any of those layers keeps showing through the properties the
|
|
218
|
+
* user never touched.
|
|
219
|
+
*
|
|
220
|
+
* Previously persisted leaves that coincide with the base of the current view
|
|
221
|
+
* are carried over rather than dropped: they may still make a difference in
|
|
222
|
+
* another view sharing the preference. See `diffLayer`.
|
|
223
|
+
*
|
|
224
|
+
* The diff basis is resolved at `newView.type`, not at the type the previous
|
|
225
|
+
* resolution used: the type selects the `defaultLayouts` entry, and a layout
|
|
226
|
+
* switch reports the new type together with that entry's values. Diffing them
|
|
227
|
+
* against the previous type's base would persist the difference between the
|
|
228
|
+
* two entries as if the user had picked those values — leaves the layouts
|
|
229
|
+
* already provide, which then linger as phantom modifications after switching
|
|
230
|
+
* back.
|
|
231
|
+
*
|
|
232
|
+
* @param newView The view the user produced.
|
|
233
|
+
* @param layers The layers the view resolves from. See `ViewLayers`.
|
|
234
|
+
* @return The modified properties, or `undefined` when the user modified none.
|
|
235
|
+
*/
|
|
236
|
+
export function getUserModifications(
|
|
237
|
+
newView: View,
|
|
238
|
+
layers: ViewLayers
|
|
239
|
+
): ViewOverrides | undefined {
|
|
240
|
+
const { activeViewOverrides, persistedView } = layers;
|
|
241
|
+
const baseView = resolveBaseView( layers, newView.type );
|
|
242
|
+
|
|
243
|
+
const modifications = diffLayer(
|
|
244
|
+
withoutQueryParams( newView ),
|
|
245
|
+
withoutQueryParams( baseView ),
|
|
246
|
+
withoutQueryParams( persistedView )
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
// Locked filters are pinned on every read, so persisting them would only
|
|
250
|
+
// leak them into the views that do not lock them.
|
|
251
|
+
const locked = getLockedFilters( activeViewOverrides );
|
|
252
|
+
if ( locked.length > 0 && Array.isArray( modifications.filters ) ) {
|
|
253
|
+
modifications.filters = ( modifications.filters as Filter[] ).filter(
|
|
254
|
+
( filter ) => ! locked.some( ( f ) => f.field === filter.field )
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return Object.keys( modifications ).length > 0
|
|
259
|
+
? ( modifications as ViewOverrides )
|
|
260
|
+
: undefined;
|
|
261
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { View } from '@wordpress/dataviews';
|
|
2
|
+
import { getUserModifications, resolveView } from '../resolve-view';
|
|
3
|
+
|
|
4
|
+
const LOCKED = {
|
|
5
|
+
field: 'author',
|
|
6
|
+
operator: 'is',
|
|
7
|
+
value: 'admin',
|
|
8
|
+
isLocked: true,
|
|
9
|
+
} as const;
|
|
10
|
+
|
|
11
|
+
const STATUS = {
|
|
12
|
+
field: 'status',
|
|
13
|
+
operator: 'is',
|
|
14
|
+
value: 'draft',
|
|
15
|
+
} as const;
|
|
16
|
+
|
|
17
|
+
// The layer resolution itself is covered by the `useView` tests. What is left
|
|
18
|
+
// here is the handling of locked filters: they are not the user's to change, so
|
|
19
|
+
// they are pinned on every read and never persisted — a preference is shared by
|
|
20
|
+
// every view of the same entity, and only some of them lock the filter.
|
|
21
|
+
describe( 'locked filters', () => {
|
|
22
|
+
const defaultView = { type: 'table', filters: [] } as unknown as View;
|
|
23
|
+
const activeViewOverrides = { filters: [ LOCKED ] };
|
|
24
|
+
|
|
25
|
+
it( 'should pin the locked filters an override provides', () => {
|
|
26
|
+
const view = resolveView( {
|
|
27
|
+
defaultView,
|
|
28
|
+
activeViewOverrides,
|
|
29
|
+
} );
|
|
30
|
+
expect( view.filters ).toEqual( [ LOCKED ] );
|
|
31
|
+
} );
|
|
32
|
+
|
|
33
|
+
it( 'should keep them pinned over the persisted filters', () => {
|
|
34
|
+
const view = resolveView( {
|
|
35
|
+
defaultView,
|
|
36
|
+
activeViewOverrides,
|
|
37
|
+
persistedView: { filters: [ STATUS ] },
|
|
38
|
+
} );
|
|
39
|
+
expect( view.filters ).toEqual( [ LOCKED, STATUS ] );
|
|
40
|
+
} );
|
|
41
|
+
|
|
42
|
+
it( 'should not persist them alongside the filters the user added', () => {
|
|
43
|
+
const view = resolveView( {
|
|
44
|
+
defaultView,
|
|
45
|
+
activeViewOverrides,
|
|
46
|
+
} );
|
|
47
|
+
const modifications = getUserModifications(
|
|
48
|
+
{ ...view, filters: [ LOCKED, STATUS ] } as View,
|
|
49
|
+
{ defaultView, activeViewOverrides }
|
|
50
|
+
);
|
|
51
|
+
expect( modifications ).toEqual( { filters: [ STATUS ] } );
|
|
52
|
+
} );
|
|
53
|
+
|
|
54
|
+
it( 'should not count them as a modification on their own', () => {
|
|
55
|
+
const view = resolveView( {
|
|
56
|
+
defaultView,
|
|
57
|
+
activeViewOverrides,
|
|
58
|
+
} );
|
|
59
|
+
expect(
|
|
60
|
+
getUserModifications( view, { defaultView, activeViewOverrides } )
|
|
61
|
+
).toBeUndefined();
|
|
62
|
+
} );
|
|
63
|
+
} );
|