@wordpress/views 1.21.2-next.v.202609031004.0 → 1.22.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 +2 -1
- package/build/resolve-view.cjs +19 -2
- package/build/resolve-view.cjs.map +2 -2
- package/build/use-view.cjs +5 -1
- package/build/use-view.cjs.map +2 -2
- package/build-module/resolve-view.mjs +18 -2
- package/build-module/resolve-view.mjs.map +2 -2
- package/build-module/use-view.mjs +10 -2
- package/build-module/use-view.mjs.map +2 -2
- package/build-types/resolve-view.d.ts +22 -1
- package/build-types/resolve-view.d.ts.map +1 -1
- package/build-types/use-view.d.ts.map +1 -1
- package/package.json +10 -11
- package/src/resolve-view.ts +47 -3
- package/src/test/resolve-view.ts +104 -1
- package/src/test/use-view.jsdom.test.tsx +64 -4
- package/src/use-view.ts +15 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
-
## 1.
|
|
5
|
+
## 1.22.0 (2026-09-10)
|
|
6
6
|
|
|
7
7
|
### Bug Fixes
|
|
8
8
|
|
|
9
9
|
- `useViewConfig`: request the full configuration under the same cache key as `getViewConfig( kind, name )`, so a route loader that preloads it no longer triggers a second request. ([#82141](https://github.com/WordPress/gutenberg/pull/82141))
|
|
10
|
+
- `useView`, `loadView`: ignore a persisted layout `type` that `defaultLayouts` does not offer, resolving the type out of the layers below instead. DataViews renders nothing for a layout it is not given, so a preference saved when a screen still offered that layout used to leave the screen empty, with no way to reset it. Such a type does not count as a modification and is written out of the preference on the next update. ([#82457](https://github.com/WordPress/gutenberg/pull/82457))
|
|
10
11
|
|
|
11
12
|
## 1.21.0 (2026-08-26)
|
|
12
13
|
|
package/build/resolve-view.cjs
CHANGED
|
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// packages/views/src/resolve-view.ts
|
|
21
21
|
var resolve_view_exports = {};
|
|
22
22
|
__export(resolve_view_exports, {
|
|
23
|
+
getApplicablePersistedView: () => getApplicablePersistedView,
|
|
23
24
|
getUserModifications: () => getUserModifications,
|
|
24
25
|
resolveView: () => resolveView
|
|
25
26
|
});
|
|
@@ -84,6 +85,13 @@ function applyLockedFilters(view, overrides) {
|
|
|
84
85
|
);
|
|
85
86
|
return { ...view, filters: [...locked, ...rest] };
|
|
86
87
|
}
|
|
88
|
+
function getApplicablePersistedView(persistedView, defaultLayouts) {
|
|
89
|
+
if (!persistedView || persistedView.type === void 0 || !defaultLayouts || defaultLayouts[persistedView.type]) {
|
|
90
|
+
return persistedView;
|
|
91
|
+
}
|
|
92
|
+
const { type, ...rest } = persistedView;
|
|
93
|
+
return Object.keys(rest).length > 0 ? rest : void 0;
|
|
94
|
+
}
|
|
87
95
|
function resolveBaseView(layers, effectiveType) {
|
|
88
96
|
const { defaultView, defaultLayouts, activeViewOverrides } = layers;
|
|
89
97
|
const layoutDefaults = defaultLayouts?.[effectiveType];
|
|
@@ -96,7 +104,11 @@ function resolveBaseView(layers, effectiveType) {
|
|
|
96
104
|
);
|
|
97
105
|
}
|
|
98
106
|
function resolveView(args) {
|
|
99
|
-
const { defaultView,
|
|
107
|
+
const { defaultView, defaultLayouts, activeViewOverrides, page, search } = args;
|
|
108
|
+
const persistedView = getApplicablePersistedView(
|
|
109
|
+
args.persistedView,
|
|
110
|
+
defaultLayouts
|
|
111
|
+
);
|
|
100
112
|
const effectiveType = persistedView?.type ?? activeViewOverrides?.type ?? defaultView?.type;
|
|
101
113
|
const baseView = resolveBaseView(args, effectiveType);
|
|
102
114
|
const view = {
|
|
@@ -110,7 +122,11 @@ function resolveView(args) {
|
|
|
110
122
|
return view;
|
|
111
123
|
}
|
|
112
124
|
function getUserModifications(newView, layers) {
|
|
113
|
-
const {
|
|
125
|
+
const { defaultLayouts, activeViewOverrides } = layers;
|
|
126
|
+
const persistedView = getApplicablePersistedView(
|
|
127
|
+
layers.persistedView,
|
|
128
|
+
defaultLayouts
|
|
129
|
+
);
|
|
114
130
|
const baseView = resolveBaseView(layers, newView.type);
|
|
115
131
|
const modifications = diffLayer(
|
|
116
132
|
withoutQueryParams(newView),
|
|
@@ -127,6 +143,7 @@ function getUserModifications(newView, layers) {
|
|
|
127
143
|
}
|
|
128
144
|
// Annotate the CommonJS export names for ESM import in node:
|
|
129
145
|
0 && (module.exports = {
|
|
146
|
+
getApplicablePersistedView,
|
|
130
147
|
getUserModifications,
|
|
131
148
|
resolveView
|
|
132
149
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/resolve-view.ts"],
|
|
4
|
-
"sourcesContent": ["import { dequal } from 'dequal';\nimport type { View, Filter, SupportedLayouts } from '@wordpress/dataviews';\nimport type { ViewOverrides } from './types';\n\ntype PlainObject = Record< string, unknown >;\n\n/**\n * `page` and `search` are URL-managed: the URL is their only source. They are\n * never persisted, and no other layer may configure them — an absent URL param\n * is indistinguishable from the user having cleared the value, so any fallback\n * would resurrect a cleared search on the next read.\n */\nconst QUERY_PARAMS = [ 'page', 'search' ] as const;\n\nfunction isPlainObject( value: unknown ): value is PlainObject {\n\treturn (\n\t\ttypeof value === 'object' && value !== null && ! Array.isArray( value )\n\t);\n}\n\n/**\n * Copies a layer without the URL-managed properties.\n *\n * @param layer The layer to copy.\n * @return A copy of the layer with `page` and `search` removed.\n */\nfunction withoutQueryParams( layer: unknown ): PlainObject {\n\tconst result: PlainObject = isPlainObject( layer ) ? { ...layer } : {};\n\tfor ( const key of QUERY_PARAMS ) {\n\t\tdelete result[ key ];\n\t}\n\treturn result;\n}\n\n/**\n * Merges a layer on top of another, recursing into plain objects so the upper\n * layer only takes over the leaves it declares. Arrays and scalars are leaves:\n * they are replaced wholesale.\n *\n * Nested properties (`layout.styles.<field>.width`, `groupBy.direction`,\n * `sort.direction`) are merged leaf by leaf: a shallow merge would let a layer\n * that only declares one leaf wipe every sibling the layers below provide.\n *\n * @param lower The layer to merge into.\n * @param upper The layer to merge on top.\n * @return The merged layer.\n */\nfunction mergeLayer( lower: PlainObject, upper: PlainObject ): PlainObject {\n\tconst result: PlainObject = { ...lower };\n\tfor ( const key of Object.keys( upper ) ) {\n\t\tconst value = upper[ key ];\n\t\tconst current = result[ key ];\n\t\tresult[ key ] =\n\t\t\tisPlainObject( current ) && isPlainObject( value )\n\t\t\t\t? mergeLayer( current, value )\n\t\t\t\t: value;\n\t}\n\treturn result;\n}\n\n/**\n * The leaves of `value` that differ from `base`. Mirrors `mergeLayer`:\n * `mergeLayer( base, diffLayer( value, base ) )` resolves back to `value`.\n *\n * A persisted leaf that equals both `value` and `base` is kept even though it\n * is not a difference: the preference is shared by every view of the same\n * entity, and such a leaf may only differ from the base of *another* view. In\n * this one, where it coincides with the base, a revert is unobservable — so\n * equality cannot mean the user reverted it. Dropping a persisted leaf\n * requires `value` to actually move away from it, back to `base`.\n *\n * @param value The layer to compare.\n * @param base The layer to compare it against.\n * @param persisted The previously persisted leaves.\n * @return The leaves that differ, as a layer of its own.\n */\nfunction diffLayer(\n\tvalue: PlainObject,\n\tbase: PlainObject,\n\tpersisted: PlainObject = {}\n): PlainObject {\n\tconst result: PlainObject = {};\n\tfor ( const key of Object.keys( value ) ) {\n\t\tconst next = value[ key ];\n\t\tconst current = base[ key ];\n\t\tconst prev = persisted[ key ];\n\t\tif ( next === undefined ) {\n\t\t\tcontinue;\n\t\t}\n\t\tif ( isPlainObject( next ) && isPlainObject( current ) ) {\n\t\t\tconst nested = diffLayer(\n\t\t\t\tnext,\n\t\t\t\tcurrent,\n\t\t\t\tisPlainObject( prev ) ? prev : {}\n\t\t\t);\n\t\t\tif ( Object.keys( nested ).length > 0 ) {\n\t\t\t\tresult[ key ] = nested;\n\t\t\t}\n\t\t} else if ( ! dequal( next, current ) ) {\n\t\t\tresult[ key ] = next;\n\t\t} else if ( prev !== undefined && dequal( next, prev ) ) {\n\t\t\tresult[ key ] = prev;\n\t\t}\n\t}\n\treturn result;\n}\n\nfunction getLockedFilters( overrides?: ViewOverrides ): Filter[] {\n\treturn ( overrides?.filters ?? [] ).filter( ( filter ) => filter.isLocked );\n}\n\n/**\n * Hoists the locked filters an override provides to the front of the view's\n * filters, replacing any same-field filter. Locked filters are not the user's\n * to change, so they are pinned in place instead of taking part in the layer\n * resolution.\n *\n * @param view The view to apply the locked filters to.\n * @param overrides The active view overrides.\n * @return The view with the locked filters applied.\n */\nfunction applyLockedFilters(\n\tview: PlainObject,\n\toverrides?: ViewOverrides\n): PlainObject {\n\tconst locked = getLockedFilters( overrides );\n\tif ( locked.length === 0 ) {\n\t\treturn view;\n\t}\n\tconst rest = ( ( view.filters ?? [] ) as Filter[] ).filter(\n\t\t( filter ) => ! locked.some( ( f ) => f.field === filter.field )\n\t);\n\treturn { ...view, filters: [ ...locked, ...rest ] };\n}\n\ninterface ViewLayers {\n\tdefaultView?: View;\n\tdefaultLayouts?: SupportedLayouts;\n\tactiveViewOverrides?: ViewOverrides;\n\t/**\n\t * The user's persisted modifications: a partial view holding only the\n\t * properties they actually changed.\n\t */\n\tpersistedView?: ViewOverrides;\n}\n\ninterface ResolveViewArgs extends ViewLayers {\n\tpage?: number;\n\tsearch?: string;\n}\n\n/**\n * Resolves the layers below the user's modifications — what a modification has\n * to differ from to count as one. The layout type selects which entry of\n * `defaultLayouts` applies, so the base only makes sense relative to a type.\n *\n * @param layers The layers to resolve.\n * @param effectiveType The layout type to resolve the base at.\n * @return The resolved base view.\n */\nfunction resolveBaseView(\n\tlayers: ViewLayers,\n\teffectiveType: View[ 'type' ] | undefined\n): PlainObject {\n\tconst { defaultView, defaultLayouts, activeViewOverrides } = layers;\n\tconst layoutDefaults =\n\t\tdefaultLayouts?.[ effectiveType as keyof SupportedLayouts ];\n\treturn applyLockedFilters(\n\t\t[ layoutDefaults, activeViewOverrides ].reduce< PlainObject >(\n\t\t\t( lower, upper ) =>\n\t\t\t\tmergeLayer( lower, withoutQueryParams( upper ) ),\n\t\t\twithoutQueryParams( defaultView )\n\t\t),\n\t\tactiveViewOverrides\n\t);\n}\n\n/**\n * Resolves a view out of its layers. Later layers override earlier ones:\n *\n * 1. `defaultView`\n * 2. `defaultLayouts` (for the effective type)\n * 3. `activeViewOverrides`\n * 4. the user's persisted modifications\n * 5. the URL query params (`page` and `search` only)\n *\n * @param args See `ResolveViewArgs`.\n * @return The resolved `view`.\n */\nexport function resolveView( args: ResolveViewArgs ): View {\n\tconst { defaultView, activeViewOverrides, persistedView, page, search } =\n\t\targs;\n\n\t// Resolve the effective layout type first: it selects which entry of\n\t// `defaultLayouts` applies, and the layers above the layouts may change it.\n\tconst effectiveType =\n\t\tpersistedView?.type ?? activeViewOverrides?.type ?? defaultView?.type;\n\tconst baseView = resolveBaseView( args, effectiveType );\n\n\tconst view = {\n\t\t...applyLockedFilters(\n\t\t\tmergeLayer( baseView, withoutQueryParams( persistedView ) ),\n\t\t\tactiveViewOverrides\n\t\t),\n\t\tpage: Number( page ?? 1 ),\n\t\tsearch: search ?? '',\n\t};\n\n\t// The layers are resolved as plain objects: `View` requires a `type`, which\n\t// only the layers taken together are guaranteed to provide.\n\treturn view as unknown as View;\n}\n\n/**\n * The user's modifications: the leaves of the view they interacted with that\n * the layers below do not already resolve to. This is what gets persisted, so\n * that a change to any of those layers keeps showing through the properties the\n * user never touched.\n *\n * Previously persisted leaves that coincide with the base of the current view\n * are carried over rather than dropped: they may still make a difference in\n * another view sharing the preference. See `diffLayer`.\n *\n * The diff basis is resolved at `newView.type`, not at the type the previous\n * resolution used: the type selects the `defaultLayouts` entry, and a layout\n * switch reports the new type together with that entry's values. Diffing them\n * against the previous type's base would persist the difference between the\n * two entries as if the user had picked those values — leaves the layouts\n * already provide, which then linger as phantom modifications after switching\n * back.\n *\n * @param newView The view the user produced.\n * @param layers The layers the view resolves from. See `ViewLayers`.\n * @return The modified properties, or `undefined` when the user modified none.\n */\nexport function getUserModifications(\n\tnewView: View,\n\tlayers: ViewLayers\n): ViewOverrides | undefined {\n\tconst { activeViewOverrides, persistedView } = layers;\n\tconst baseView = resolveBaseView( layers, newView.type );\n\n\tconst modifications = diffLayer(\n\t\twithoutQueryParams( newView ),\n\t\twithoutQueryParams( baseView ),\n\t\twithoutQueryParams( persistedView )\n\t);\n\n\t// Locked filters are pinned on every read, so persisting them would only\n\t// leak them into the views that do not lock them.\n\tconst locked = getLockedFilters( activeViewOverrides );\n\tif ( locked.length > 0 && Array.isArray( modifications.filters ) ) {\n\t\tmodifications.filters = ( modifications.filters as Filter[] ).filter(\n\t\t\t( filter ) => ! locked.some( ( f ) => f.field === filter.field )\n\t\t);\n\t}\n\n\treturn Object.keys( modifications ).length > 0\n\t\t? ( modifications as ViewOverrides )\n\t\t: undefined;\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAuB;AAYvB,IAAM,eAAe,CAAE,QAAQ,QAAS;AAExC,SAAS,cAAe,OAAuC;AAC9D,SACC,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAE,MAAM,QAAS,KAAM;AAExE;AAQA,SAAS,mBAAoB,OAA8B;AAC1D,QAAM,SAAsB,cAAe,KAAM,IAAI,EAAE,GAAG,MAAM,IAAI,CAAC;AACrE,aAAY,OAAO,cAAe;AACjC,WAAO,OAAQ,GAAI;AAAA,EACpB;AACA,SAAO;AACR;AAeA,SAAS,WAAY,OAAoB,OAAkC;AAC1E,QAAM,SAAsB,EAAE,GAAG,MAAM;AACvC,aAAY,OAAO,OAAO,KAAM,KAAM,GAAI;AACzC,UAAM,QAAQ,MAAO,GAAI;AACzB,UAAM,UAAU,OAAQ,GAAI;AAC5B,WAAQ,GAAI,IACX,cAAe,OAAQ,KAAK,cAAe,KAAM,IAC9C,WAAY,SAAS,KAAM,IAC3B;AAAA,EACL;AACA,SAAO;AACR;AAkBA,SAAS,UACR,OACA,MACA,YAAyB,CAAC,GACZ;AACd,QAAM,SAAsB,CAAC;AAC7B,aAAY,OAAO,OAAO,KAAM,KAAM,GAAI;AACzC,UAAM,OAAO,MAAO,GAAI;AACxB,UAAM,UAAU,KAAM,GAAI;AAC1B,UAAM,OAAO,UAAW,GAAI;AAC5B,QAAK,SAAS,QAAY;AACzB;AAAA,IACD;AACA,QAAK,cAAe,IAAK,KAAK,cAAe,OAAQ,GAAI;AACxD,YAAM,SAAS;AAAA,QACd;AAAA,QACA;AAAA,QACA,cAAe,IAAK,IAAI,OAAO,CAAC;AAAA,MACjC;AACA,UAAK,OAAO,KAAM,MAAO,EAAE,SAAS,GAAI;AACvC,eAAQ,GAAI,IAAI;AAAA,MACjB;AAAA,IACD,WAAY,KAAE,sBAAQ,MAAM,OAAQ,GAAI;AACvC,aAAQ,GAAI,IAAI;AAAA,IACjB,WAAY,SAAS,cAAa,sBAAQ,MAAM,IAAK,GAAI;AACxD,aAAQ,GAAI,IAAI;AAAA,IACjB;AAAA,EACD;AACA,SAAO;AACR;AAEA,SAAS,iBAAkB,WAAsC;AAChE,UAAS,WAAW,WAAW,CAAC,GAAI,OAAQ,CAAE,WAAY,OAAO,QAAS;AAC3E;AAYA,SAAS,mBACR,MACA,WACc;AACd,QAAM,SAAS,iBAAkB,SAAU;AAC3C,MAAK,OAAO,WAAW,GAAI;AAC1B,WAAO;AAAA,EACR;AACA,QAAM,QAAW,KAAK,WAAW,CAAC,GAAkB;AAAA,IACnD,CAAE,WAAY,CAAE,OAAO,KAAM,CAAE,MAAO,EAAE,UAAU,OAAO,KAAM;AAAA,EAChE;AACA,SAAO,EAAE,GAAG,MAAM,SAAS,CAAE,GAAG,QAAQ,GAAG,IAAK,EAAE;AACnD;
|
|
4
|
+
"sourcesContent": ["import { dequal } from 'dequal';\nimport type { View, Filter, SupportedLayouts } from '@wordpress/dataviews';\nimport type { ViewOverrides } from './types';\n\ntype PlainObject = Record< string, unknown >;\n\n/**\n * `page` and `search` are URL-managed: the URL is their only source. They are\n * never persisted, and no other layer may configure them — an absent URL param\n * is indistinguishable from the user having cleared the value, so any fallback\n * would resurrect a cleared search on the next read.\n */\nconst QUERY_PARAMS = [ 'page', 'search' ] as const;\n\nfunction isPlainObject( value: unknown ): value is PlainObject {\n\treturn (\n\t\ttypeof value === 'object' && value !== null && ! Array.isArray( value )\n\t);\n}\n\n/**\n * Copies a layer without the URL-managed properties.\n *\n * @param layer The layer to copy.\n * @return A copy of the layer with `page` and `search` removed.\n */\nfunction withoutQueryParams( layer: unknown ): PlainObject {\n\tconst result: PlainObject = isPlainObject( layer ) ? { ...layer } : {};\n\tfor ( const key of QUERY_PARAMS ) {\n\t\tdelete result[ key ];\n\t}\n\treturn result;\n}\n\n/**\n * Merges a layer on top of another, recursing into plain objects so the upper\n * layer only takes over the leaves it declares. Arrays and scalars are leaves:\n * they are replaced wholesale.\n *\n * Nested properties (`layout.styles.<field>.width`, `groupBy.direction`,\n * `sort.direction`) are merged leaf by leaf: a shallow merge would let a layer\n * that only declares one leaf wipe every sibling the layers below provide.\n *\n * @param lower The layer to merge into.\n * @param upper The layer to merge on top.\n * @return The merged layer.\n */\nfunction mergeLayer( lower: PlainObject, upper: PlainObject ): PlainObject {\n\tconst result: PlainObject = { ...lower };\n\tfor ( const key of Object.keys( upper ) ) {\n\t\tconst value = upper[ key ];\n\t\tconst current = result[ key ];\n\t\tresult[ key ] =\n\t\t\tisPlainObject( current ) && isPlainObject( value )\n\t\t\t\t? mergeLayer( current, value )\n\t\t\t\t: value;\n\t}\n\treturn result;\n}\n\n/**\n * The leaves of `value` that differ from `base`. Mirrors `mergeLayer`:\n * `mergeLayer( base, diffLayer( value, base ) )` resolves back to `value`.\n *\n * A persisted leaf that equals both `value` and `base` is kept even though it\n * is not a difference: the preference is shared by every view of the same\n * entity, and such a leaf may only differ from the base of *another* view. In\n * this one, where it coincides with the base, a revert is unobservable — so\n * equality cannot mean the user reverted it. Dropping a persisted leaf\n * requires `value` to actually move away from it, back to `base`.\n *\n * @param value The layer to compare.\n * @param base The layer to compare it against.\n * @param persisted The previously persisted leaves.\n * @return The leaves that differ, as a layer of its own.\n */\nfunction diffLayer(\n\tvalue: PlainObject,\n\tbase: PlainObject,\n\tpersisted: PlainObject = {}\n): PlainObject {\n\tconst result: PlainObject = {};\n\tfor ( const key of Object.keys( value ) ) {\n\t\tconst next = value[ key ];\n\t\tconst current = base[ key ];\n\t\tconst prev = persisted[ key ];\n\t\tif ( next === undefined ) {\n\t\t\tcontinue;\n\t\t}\n\t\tif ( isPlainObject( next ) && isPlainObject( current ) ) {\n\t\t\tconst nested = diffLayer(\n\t\t\t\tnext,\n\t\t\t\tcurrent,\n\t\t\t\tisPlainObject( prev ) ? prev : {}\n\t\t\t);\n\t\t\tif ( Object.keys( nested ).length > 0 ) {\n\t\t\t\tresult[ key ] = nested;\n\t\t\t}\n\t\t} else if ( ! dequal( next, current ) ) {\n\t\t\tresult[ key ] = next;\n\t\t} else if ( prev !== undefined && dequal( next, prev ) ) {\n\t\t\tresult[ key ] = prev;\n\t\t}\n\t}\n\treturn result;\n}\n\nfunction getLockedFilters( overrides?: ViewOverrides ): Filter[] {\n\treturn ( overrides?.filters ?? [] ).filter( ( filter ) => filter.isLocked );\n}\n\n/**\n * Hoists the locked filters an override provides to the front of the view's\n * filters, replacing any same-field filter. Locked filters are not the user's\n * to change, so they are pinned in place instead of taking part in the layer\n * resolution.\n *\n * @param view The view to apply the locked filters to.\n * @param overrides The active view overrides.\n * @return The view with the locked filters applied.\n */\nfunction applyLockedFilters(\n\tview: PlainObject,\n\toverrides?: ViewOverrides\n): PlainObject {\n\tconst locked = getLockedFilters( overrides );\n\tif ( locked.length === 0 ) {\n\t\treturn view;\n\t}\n\tconst rest = ( ( view.filters ?? [] ) as Filter[] ).filter(\n\t\t( filter ) => ! locked.some( ( f ) => f.field === filter.field )\n\t);\n\treturn { ...view, filters: [ ...locked, ...rest ] };\n}\n\ninterface ViewLayers {\n\tdefaultView?: View;\n\tdefaultLayouts?: SupportedLayouts;\n\tactiveViewOverrides?: ViewOverrides;\n\t/**\n\t * The user's persisted modifications: a partial view holding only the\n\t * properties they actually changed.\n\t */\n\tpersistedView?: ViewOverrides;\n}\n\ninterface ResolveViewArgs extends ViewLayers {\n\tpage?: number;\n\tsearch?: string;\n}\n\n/**\n * The user's persisted modifications that still apply.\n *\n * A persisted `type` that `defaultLayouts` does not offer is dropped: DataViews\n * renders nothing for a layout it is not given, and the preference may predate\n * the current set of layouts (a screen that used to offer the list layout, say,\n * and no longer does). The type then resolves out of the layers below, and the\n * other modifications are kept. Without `defaultLayouts` every type is offered.\n *\n * `true` is a valid entry, so the check mirrors DataViews': an entry counts as\n * long as it is truthy.\n *\n * @param persistedView The user's persisted modifications.\n * @param defaultLayouts The layouts on offer, keyed by layout type.\n * @return The modifications that apply, or `undefined` when there are none.\n */\nexport function getApplicablePersistedView(\n\tpersistedView: ViewOverrides | undefined,\n\tdefaultLayouts: SupportedLayouts | undefined\n): ViewOverrides | undefined {\n\tif (\n\t\t! persistedView ||\n\t\tpersistedView.type === undefined ||\n\t\t! defaultLayouts ||\n\t\tdefaultLayouts[ persistedView.type as keyof SupportedLayouts ]\n\t) {\n\t\treturn persistedView;\n\t}\n\tconst { type, ...rest } = persistedView;\n\treturn Object.keys( rest ).length > 0 ? rest : undefined;\n}\n\n/**\n * Resolves the layers below the user's modifications — what a modification has\n * to differ from to count as one. The layout type selects which entry of\n * `defaultLayouts` applies, so the base only makes sense relative to a type.\n *\n * @param layers The layers to resolve.\n * @param effectiveType The layout type to resolve the base at.\n * @return The resolved base view.\n */\nfunction resolveBaseView(\n\tlayers: ViewLayers,\n\teffectiveType: View[ 'type' ] | undefined\n): PlainObject {\n\tconst { defaultView, defaultLayouts, activeViewOverrides } = layers;\n\tconst layoutDefaults =\n\t\tdefaultLayouts?.[ effectiveType as keyof SupportedLayouts ];\n\treturn applyLockedFilters(\n\t\t[ layoutDefaults, activeViewOverrides ].reduce< PlainObject >(\n\t\t\t( lower, upper ) =>\n\t\t\t\tmergeLayer( lower, withoutQueryParams( upper ) ),\n\t\t\twithoutQueryParams( defaultView )\n\t\t),\n\t\tactiveViewOverrides\n\t);\n}\n\n/**\n * Resolves a view out of its layers. Later layers override earlier ones:\n *\n * 1. `defaultView`\n * 2. `defaultLayouts` (for the effective type)\n * 3. `activeViewOverrides`\n * 4. the user's persisted modifications (see `getApplicablePersistedView`)\n * 5. the URL query params (`page` and `search` only)\n *\n * @param args See `ResolveViewArgs`.\n * @return The resolved `view`.\n */\nexport function resolveView( args: ResolveViewArgs ): View {\n\tconst { defaultView, defaultLayouts, activeViewOverrides, page, search } =\n\t\targs;\n\tconst persistedView = getApplicablePersistedView(\n\t\targs.persistedView,\n\t\tdefaultLayouts\n\t);\n\n\t// Resolve the effective layout type first: it selects which entry of\n\t// `defaultLayouts` applies, and the layers above the layouts may change it.\n\tconst effectiveType =\n\t\tpersistedView?.type ?? activeViewOverrides?.type ?? defaultView?.type;\n\tconst baseView = resolveBaseView( args, effectiveType );\n\n\tconst view = {\n\t\t...applyLockedFilters(\n\t\t\tmergeLayer( baseView, withoutQueryParams( persistedView ) ),\n\t\t\tactiveViewOverrides\n\t\t),\n\t\tpage: Number( page ?? 1 ),\n\t\tsearch: search ?? '',\n\t};\n\n\t// The layers are resolved as plain objects: `View` requires a `type`, which\n\t// only the layers taken together are guaranteed to provide.\n\treturn view as unknown as View;\n}\n\n/**\n * The user's modifications: the leaves of the view they interacted with that\n * the layers below do not already resolve to. This is what gets persisted, so\n * that a change to any of those layers keeps showing through the properties the\n * user never touched.\n *\n * Previously persisted leaves that coincide with the base of the current view\n * are carried over rather than dropped: they may still make a difference in\n * another view sharing the preference. See `diffLayer`.\n *\n * The diff basis is resolved at `newView.type`, not at the type the previous\n * resolution used: the type selects the `defaultLayouts` entry, and a layout\n * switch reports the new type together with that entry's values. Diffing them\n * against the previous type's base would persist the difference between the\n * two entries as if the user had picked those values — leaves the layouts\n * already provide, which then linger as phantom modifications after switching\n * back.\n *\n * A persisted `type` the current layouts do not offer never applied (see\n * `getApplicablePersistedView`), so it is not carried over either: the\n * modifications returned replace it with whatever the user picked.\n *\n * @param newView The view the user produced.\n * @param layers The layers the view resolves from. See `ViewLayers`.\n * @return The modified properties, or `undefined` when the user modified none.\n */\nexport function getUserModifications(\n\tnewView: View,\n\tlayers: ViewLayers\n): ViewOverrides | undefined {\n\tconst { defaultLayouts, activeViewOverrides } = layers;\n\tconst persistedView = getApplicablePersistedView(\n\t\tlayers.persistedView,\n\t\tdefaultLayouts\n\t);\n\tconst baseView = resolveBaseView( layers, newView.type );\n\n\tconst modifications = diffLayer(\n\t\twithoutQueryParams( newView ),\n\t\twithoutQueryParams( baseView ),\n\t\twithoutQueryParams( persistedView )\n\t);\n\n\t// Locked filters are pinned on every read, so persisting them would only\n\t// leak them into the views that do not lock them.\n\tconst locked = getLockedFilters( activeViewOverrides );\n\tif ( locked.length > 0 && Array.isArray( modifications.filters ) ) {\n\t\tmodifications.filters = ( modifications.filters as Filter[] ).filter(\n\t\t\t( filter ) => ! locked.some( ( f ) => f.field === filter.field )\n\t\t);\n\t}\n\n\treturn Object.keys( modifications ).length > 0\n\t\t? ( modifications as ViewOverrides )\n\t\t: undefined;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAuB;AAYvB,IAAM,eAAe,CAAE,QAAQ,QAAS;AAExC,SAAS,cAAe,OAAuC;AAC9D,SACC,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAE,MAAM,QAAS,KAAM;AAExE;AAQA,SAAS,mBAAoB,OAA8B;AAC1D,QAAM,SAAsB,cAAe,KAAM,IAAI,EAAE,GAAG,MAAM,IAAI,CAAC;AACrE,aAAY,OAAO,cAAe;AACjC,WAAO,OAAQ,GAAI;AAAA,EACpB;AACA,SAAO;AACR;AAeA,SAAS,WAAY,OAAoB,OAAkC;AAC1E,QAAM,SAAsB,EAAE,GAAG,MAAM;AACvC,aA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B;AAAA,IACrB,mBAAoB,OAAQ;AAAA,IAC5B,mBAAoB,QAAS;AAAA,IAC7B,mBAAoB,aAAc;AAAA,EACnC;AAIA,QAAM,SAAS,iBAAkB,mBAAoB;AACrD,MAAK,OAAO,SAAS,KAAK,MAAM,QAAS,cAAc,OAAQ,GAAI;AAClE,kBAAc,UAAY,cAAc,QAAsB;AAAA,MAC7D,CAAE,WAAY,CAAE,OAAO,KAAM,CAAE,MAAO,EAAE,UAAU,OAAO,KAAM;AAAA,IAChE;AAAA,EACD;AAEA,SAAO,OAAO,KAAM,aAAc,EAAE,SAAS,IACxC,gBACF;AACJ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/build/use-view.cjs
CHANGED
|
@@ -71,7 +71,11 @@ function useView(config) {
|
|
|
71
71
|
search
|
|
72
72
|
]
|
|
73
73
|
);
|
|
74
|
-
const
|
|
74
|
+
const applicablePersistedView = (0, import_resolve_view.getApplicablePersistedView)(
|
|
75
|
+
persistedView,
|
|
76
|
+
defaultLayouts
|
|
77
|
+
);
|
|
78
|
+
const isModified = !!applicablePersistedView && Object.keys(applicablePersistedView).length > 0;
|
|
75
79
|
const updateView = (0, import_element.useCallback)(
|
|
76
80
|
(newView) => {
|
|
77
81
|
const newQueryParams = {
|
package/build/use-view.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/use-view.ts"],
|
|
4
|
-
"sourcesContent": ["import { dequal } from 'dequal';\nimport { useCallback, useMemo } from '@wordpress/element';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport type { View } from '@wordpress/dataviews';\nimport { store as preferencesStore } from '@wordpress/preferences';\nimport { generatePreferenceKey } from './preference-keys';\nimport {
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAuB;AACvB,qBAAqC;AACrC,kBAAuC;AAEvC,yBAA0C;AAC1C,6BAAsC;AACtC,
|
|
4
|
+
"sourcesContent": ["import { dequal } from 'dequal';\nimport { useCallback, useMemo } from '@wordpress/element';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport type { View } from '@wordpress/dataviews';\nimport { store as preferencesStore } from '@wordpress/preferences';\nimport { generatePreferenceKey } from './preference-keys';\nimport {\n\tgetApplicablePersistedView,\n\tgetUserModifications,\n\tresolveView,\n} from './resolve-view';\nimport type { ViewConfig, ViewOverrides } from './types';\n\ninterface UseViewReturn {\n\tview: View;\n\tisModified: boolean;\n\tupdateView: ( newView: View ) => void;\n\tresetToDefault: () => void;\n}\n\n/**\n * Hook for managing DataViews view state with local persistence.\n *\n * Only the properties the user actually modified are persisted: every other\n * property keeps resolving out of the layers below, so a change to the default\n * view, to the layout defaults or to the active view overrides keeps showing\n * through.\n *\n * @param config Configuration object for loading the view.\n *\n * @return Object with current view, modification state, and update functions.\n */\nexport function useView( config: ViewConfig ): UseViewReturn {\n\tconst {\n\t\tkind,\n\t\tname,\n\t\tslug,\n\t\tdefaultView,\n\t\tdefaultLayouts,\n\t\tactiveViewOverrides,\n\t\tqueryParams,\n\t\tonChangeQueryParams,\n\t} = config;\n\n\tconst preferenceKey = generatePreferenceKey( kind, name, slug );\n\tconst persistedView: ViewOverrides | undefined = useSelect(\n\t\t( select ) => {\n\t\t\treturn select( preferencesStore ).get(\n\t\t\t\t'core/views',\n\t\t\t\tpreferenceKey\n\t\t\t) as ViewOverrides | undefined;\n\t\t},\n\t\t[ preferenceKey ]\n\t);\n\tconst { set } = useDispatch( preferencesStore );\n\n\tconst page = Number( queryParams?.page ?? 1 );\n\tconst search = queryParams?.search ?? '';\n\n\tconst view = useMemo(\n\t\t() =>\n\t\t\tresolveView( {\n\t\t\t\tdefaultView,\n\t\t\t\tdefaultLayouts,\n\t\t\t\tactiveViewOverrides,\n\t\t\t\tpersistedView,\n\t\t\t\tpage,\n\t\t\t\tsearch,\n\t\t\t} ),\n\t\t[\n\t\t\tdefaultView,\n\t\t\tdefaultLayouts,\n\t\t\tactiveViewOverrides,\n\t\t\tpersistedView,\n\t\t\tpage,\n\t\t\tsearch,\n\t\t]\n\t);\n\n\t// A persisted `type` the layouts do not offer is ignored on read, so it\n\t// does not count as a modification either: there is nothing to reset.\n\tconst applicablePersistedView = getApplicablePersistedView(\n\t\tpersistedView,\n\t\tdefaultLayouts\n\t);\n\tconst isModified =\n\t\t!! applicablePersistedView &&\n\t\tObject.keys( applicablePersistedView ).length > 0;\n\n\tconst updateView = useCallback(\n\t\t( newView: View ) => {\n\t\t\t// `page` and `search` live in the URL, not in the preference: they\n\t\t\t// are reported back to the consumer instead of being persisted.\n\t\t\tconst newQueryParams = {\n\t\t\t\tpage: Number( newView?.page ?? 1 ),\n\t\t\t\tsearch: newView?.search ?? '',\n\t\t\t};\n\t\t\tif (\n\t\t\t\tonChangeQueryParams &&\n\t\t\t\t! dequal( newQueryParams, { page, search } )\n\t\t\t) {\n\t\t\t\tonChangeQueryParams( newQueryParams );\n\t\t\t}\n\n\t\t\tconst modifications = getUserModifications( newView, {\n\t\t\t\tdefaultView,\n\t\t\t\tdefaultLayouts,\n\t\t\t\tactiveViewOverrides,\n\t\t\t\tpersistedView,\n\t\t\t} );\n\t\t\t// Compared against the preference as stored, so a persisted `type`\n\t\t\t// the layouts do not offer is written out on the first update.\n\t\t\tif ( ! dequal( modifications, persistedView ) ) {\n\t\t\t\t// `undefined` clears the preference: the user reverted every\n\t\t\t\t// property they had modified.\n\t\t\t\tset( 'core/views', preferenceKey, modifications );\n\t\t\t}\n\t\t},\n\t\t[\n\t\t\tonChangeQueryParams,\n\t\t\tpage,\n\t\t\tsearch,\n\t\t\tdefaultView,\n\t\t\tdefaultLayouts,\n\t\t\tactiveViewOverrides,\n\t\t\tpersistedView,\n\t\t\tset,\n\t\t\tpreferenceKey,\n\t\t]\n\t);\n\n\tconst resetToDefault = useCallback( () => {\n\t\tset( 'core/views', preferenceKey, undefined );\n\t}, [ preferenceKey, set ] );\n\n\tre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,KAAE,sBAAQ,gBAAgB,EAAE,MAAM,OAAO,CAAE,GAC1C;AACD,4BAAqB,cAAe;AAAA,MACrC;AAEA,YAAM,oBAAgB,0CAAsB,SAAS;AAAA,QACpD;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACD,CAAE;AAGF,UAAK,KAAE,sBAAQ,eAAe,aAAc,GAAI;AAG/C,YAAK,cAAc,eAAe,aAAc;AAAA,MACjD;AAAA,IACD;AAAA,IACA;AAAA,MACC;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AAEA,QAAM,qBAAiB,4BAAa,MAAM;AACzC,QAAK,cAAc,eAAe,MAAU;AAAA,EAC7C,GAAG,CAAE,eAAe,GAAI,CAAE;AAE1B,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;",
|
|
6
6
|
"names": ["preferencesStore"]
|
|
7
7
|
}
|
|
@@ -59,6 +59,13 @@ function applyLockedFilters(view, overrides) {
|
|
|
59
59
|
);
|
|
60
60
|
return { ...view, filters: [...locked, ...rest] };
|
|
61
61
|
}
|
|
62
|
+
function getApplicablePersistedView(persistedView, defaultLayouts) {
|
|
63
|
+
if (!persistedView || persistedView.type === void 0 || !defaultLayouts || defaultLayouts[persistedView.type]) {
|
|
64
|
+
return persistedView;
|
|
65
|
+
}
|
|
66
|
+
const { type, ...rest } = persistedView;
|
|
67
|
+
return Object.keys(rest).length > 0 ? rest : void 0;
|
|
68
|
+
}
|
|
62
69
|
function resolveBaseView(layers, effectiveType) {
|
|
63
70
|
const { defaultView, defaultLayouts, activeViewOverrides } = layers;
|
|
64
71
|
const layoutDefaults = defaultLayouts?.[effectiveType];
|
|
@@ -71,7 +78,11 @@ function resolveBaseView(layers, effectiveType) {
|
|
|
71
78
|
);
|
|
72
79
|
}
|
|
73
80
|
function resolveView(args) {
|
|
74
|
-
const { defaultView,
|
|
81
|
+
const { defaultView, defaultLayouts, activeViewOverrides, page, search } = args;
|
|
82
|
+
const persistedView = getApplicablePersistedView(
|
|
83
|
+
args.persistedView,
|
|
84
|
+
defaultLayouts
|
|
85
|
+
);
|
|
75
86
|
const effectiveType = persistedView?.type ?? activeViewOverrides?.type ?? defaultView?.type;
|
|
76
87
|
const baseView = resolveBaseView(args, effectiveType);
|
|
77
88
|
const view = {
|
|
@@ -85,7 +96,11 @@ function resolveView(args) {
|
|
|
85
96
|
return view;
|
|
86
97
|
}
|
|
87
98
|
function getUserModifications(newView, layers) {
|
|
88
|
-
const {
|
|
99
|
+
const { defaultLayouts, activeViewOverrides } = layers;
|
|
100
|
+
const persistedView = getApplicablePersistedView(
|
|
101
|
+
layers.persistedView,
|
|
102
|
+
defaultLayouts
|
|
103
|
+
);
|
|
89
104
|
const baseView = resolveBaseView(layers, newView.type);
|
|
90
105
|
const modifications = diffLayer(
|
|
91
106
|
withoutQueryParams(newView),
|
|
@@ -101,6 +116,7 @@ function getUserModifications(newView, layers) {
|
|
|
101
116
|
return Object.keys(modifications).length > 0 ? modifications : void 0;
|
|
102
117
|
}
|
|
103
118
|
export {
|
|
119
|
+
getApplicablePersistedView,
|
|
104
120
|
getUserModifications,
|
|
105
121
|
resolveView
|
|
106
122
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/resolve-view.ts"],
|
|
4
|
-
"sourcesContent": ["import { dequal } from 'dequal';\nimport type { View, Filter, SupportedLayouts } from '@wordpress/dataviews';\nimport type { ViewOverrides } from './types';\n\ntype PlainObject = Record< string, unknown >;\n\n/**\n * `page` and `search` are URL-managed: the URL is their only source. They are\n * never persisted, and no other layer may configure them — an absent URL param\n * is indistinguishable from the user having cleared the value, so any fallback\n * would resurrect a cleared search on the next read.\n */\nconst QUERY_PARAMS = [ 'page', 'search' ] as const;\n\nfunction isPlainObject( value: unknown ): value is PlainObject {\n\treturn (\n\t\ttypeof value === 'object' && value !== null && ! Array.isArray( value )\n\t);\n}\n\n/**\n * Copies a layer without the URL-managed properties.\n *\n * @param layer The layer to copy.\n * @return A copy of the layer with `page` and `search` removed.\n */\nfunction withoutQueryParams( layer: unknown ): PlainObject {\n\tconst result: PlainObject = isPlainObject( layer ) ? { ...layer } : {};\n\tfor ( const key of QUERY_PARAMS ) {\n\t\tdelete result[ key ];\n\t}\n\treturn result;\n}\n\n/**\n * Merges a layer on top of another, recursing into plain objects so the upper\n * layer only takes over the leaves it declares. Arrays and scalars are leaves:\n * they are replaced wholesale.\n *\n * Nested properties (`layout.styles.<field>.width`, `groupBy.direction`,\n * `sort.direction`) are merged leaf by leaf: a shallow merge would let a layer\n * that only declares one leaf wipe every sibling the layers below provide.\n *\n * @param lower The layer to merge into.\n * @param upper The layer to merge on top.\n * @return The merged layer.\n */\nfunction mergeLayer( lower: PlainObject, upper: PlainObject ): PlainObject {\n\tconst result: PlainObject = { ...lower };\n\tfor ( const key of Object.keys( upper ) ) {\n\t\tconst value = upper[ key ];\n\t\tconst current = result[ key ];\n\t\tresult[ key ] =\n\t\t\tisPlainObject( current ) && isPlainObject( value )\n\t\t\t\t? mergeLayer( current, value )\n\t\t\t\t: value;\n\t}\n\treturn result;\n}\n\n/**\n * The leaves of `value` that differ from `base`. Mirrors `mergeLayer`:\n * `mergeLayer( base, diffLayer( value, base ) )` resolves back to `value`.\n *\n * A persisted leaf that equals both `value` and `base` is kept even though it\n * is not a difference: the preference is shared by every view of the same\n * entity, and such a leaf may only differ from the base of *another* view. In\n * this one, where it coincides with the base, a revert is unobservable — so\n * equality cannot mean the user reverted it. Dropping a persisted leaf\n * requires `value` to actually move away from it, back to `base`.\n *\n * @param value The layer to compare.\n * @param base The layer to compare it against.\n * @param persisted The previously persisted leaves.\n * @return The leaves that differ, as a layer of its own.\n */\nfunction diffLayer(\n\tvalue: PlainObject,\n\tbase: PlainObject,\n\tpersisted: PlainObject = {}\n): PlainObject {\n\tconst result: PlainObject = {};\n\tfor ( const key of Object.keys( value ) ) {\n\t\tconst next = value[ key ];\n\t\tconst current = base[ key ];\n\t\tconst prev = persisted[ key ];\n\t\tif ( next === undefined ) {\n\t\t\tcontinue;\n\t\t}\n\t\tif ( isPlainObject( next ) && isPlainObject( current ) ) {\n\t\t\tconst nested = diffLayer(\n\t\t\t\tnext,\n\t\t\t\tcurrent,\n\t\t\t\tisPlainObject( prev ) ? prev : {}\n\t\t\t);\n\t\t\tif ( Object.keys( nested ).length > 0 ) {\n\t\t\t\tresult[ key ] = nested;\n\t\t\t}\n\t\t} else if ( ! dequal( next, current ) ) {\n\t\t\tresult[ key ] = next;\n\t\t} else if ( prev !== undefined && dequal( next, prev ) ) {\n\t\t\tresult[ key ] = prev;\n\t\t}\n\t}\n\treturn result;\n}\n\nfunction getLockedFilters( overrides?: ViewOverrides ): Filter[] {\n\treturn ( overrides?.filters ?? [] ).filter( ( filter ) => filter.isLocked );\n}\n\n/**\n * Hoists the locked filters an override provides to the front of the view's\n * filters, replacing any same-field filter. Locked filters are not the user's\n * to change, so they are pinned in place instead of taking part in the layer\n * resolution.\n *\n * @param view The view to apply the locked filters to.\n * @param overrides The active view overrides.\n * @return The view with the locked filters applied.\n */\nfunction applyLockedFilters(\n\tview: PlainObject,\n\toverrides?: ViewOverrides\n): PlainObject {\n\tconst locked = getLockedFilters( overrides );\n\tif ( locked.length === 0 ) {\n\t\treturn view;\n\t}\n\tconst rest = ( ( view.filters ?? [] ) as Filter[] ).filter(\n\t\t( filter ) => ! locked.some( ( f ) => f.field === filter.field )\n\t);\n\treturn { ...view, filters: [ ...locked, ...rest ] };\n}\n\ninterface ViewLayers {\n\tdefaultView?: View;\n\tdefaultLayouts?: SupportedLayouts;\n\tactiveViewOverrides?: ViewOverrides;\n\t/**\n\t * The user's persisted modifications: a partial view holding only the\n\t * properties they actually changed.\n\t */\n\tpersistedView?: ViewOverrides;\n}\n\ninterface ResolveViewArgs extends ViewLayers {\n\tpage?: number;\n\tsearch?: string;\n}\n\n/**\n * Resolves the layers below the user's modifications — what a modification has\n * to differ from to count as one. The layout type selects which entry of\n * `defaultLayouts` applies, so the base only makes sense relative to a type.\n *\n * @param layers The layers to resolve.\n * @param effectiveType The layout type to resolve the base at.\n * @return The resolved base view.\n */\nfunction resolveBaseView(\n\tlayers: ViewLayers,\n\teffectiveType: View[ 'type' ] | undefined\n): PlainObject {\n\tconst { defaultView, defaultLayouts, activeViewOverrides } = layers;\n\tconst layoutDefaults =\n\t\tdefaultLayouts?.[ effectiveType as keyof SupportedLayouts ];\n\treturn applyLockedFilters(\n\t\t[ layoutDefaults, activeViewOverrides ].reduce< PlainObject >(\n\t\t\t( lower, upper ) =>\n\t\t\t\tmergeLayer( lower, withoutQueryParams( upper ) ),\n\t\t\twithoutQueryParams( defaultView )\n\t\t),\n\t\tactiveViewOverrides\n\t);\n}\n\n/**\n * Resolves a view out of its layers. Later layers override earlier ones:\n *\n * 1. `defaultView`\n * 2. `defaultLayouts` (for the effective type)\n * 3. `activeViewOverrides`\n * 4. the user's persisted modifications\n * 5. the URL query params (`page` and `search` only)\n *\n * @param args See `ResolveViewArgs`.\n * @return The resolved `view`.\n */\nexport function resolveView( args: ResolveViewArgs ): View {\n\tconst { defaultView, activeViewOverrides, persistedView, page, search } =\n\t\targs;\n\n\t// Resolve the effective layout type first: it selects which entry of\n\t// `defaultLayouts` applies, and the layers above the layouts may change it.\n\tconst effectiveType =\n\t\tpersistedView?.type ?? activeViewOverrides?.type ?? defaultView?.type;\n\tconst baseView = resolveBaseView( args, effectiveType );\n\n\tconst view = {\n\t\t...applyLockedFilters(\n\t\t\tmergeLayer( baseView, withoutQueryParams( persistedView ) ),\n\t\t\tactiveViewOverrides\n\t\t),\n\t\tpage: Number( page ?? 1 ),\n\t\tsearch: search ?? '',\n\t};\n\n\t// The layers are resolved as plain objects: `View` requires a `type`, which\n\t// only the layers taken together are guaranteed to provide.\n\treturn view as unknown as View;\n}\n\n/**\n * The user's modifications: the leaves of the view they interacted with that\n * the layers below do not already resolve to. This is what gets persisted, so\n * that a change to any of those layers keeps showing through the properties the\n * user never touched.\n *\n * Previously persisted leaves that coincide with the base of the current view\n * are carried over rather than dropped: they may still make a difference in\n * another view sharing the preference. See `diffLayer`.\n *\n * The diff basis is resolved at `newView.type`, not at the type the previous\n * resolution used: the type selects the `defaultLayouts` entry, and a layout\n * switch reports the new type together with that entry's values. Diffing them\n * against the previous type's base would persist the difference between the\n * two entries as if the user had picked those values — leaves the layouts\n * already provide, which then linger as phantom modifications after switching\n * back.\n *\n * @param newView The view the user produced.\n * @param layers The layers the view resolves from. See `ViewLayers`.\n * @return The modified properties, or `undefined` when the user modified none.\n */\nexport function getUserModifications(\n\tnewView: View,\n\tlayers: ViewLayers\n): ViewOverrides | undefined {\n\tconst { activeViewOverrides, persistedView } = layers;\n\tconst baseView = resolveBaseView( layers, newView.type );\n\n\tconst modifications = diffLayer(\n\t\twithoutQueryParams( newView ),\n\t\twithoutQueryParams( baseView ),\n\t\twithoutQueryParams( persistedView )\n\t);\n\n\t// Locked filters are pinned on every read, so persisting them would only\n\t// leak them into the views that do not lock them.\n\tconst locked = getLockedFilters( activeViewOverrides );\n\tif ( locked.length > 0 && Array.isArray( modifications.filters ) ) {\n\t\tmodifications.filters = ( modifications.filters as Filter[] ).filter(\n\t\t\t( filter ) => ! locked.some( ( f ) => f.field === filter.field )\n\t\t);\n\t}\n\n\treturn Object.keys( modifications ).length > 0\n\t\t? ( modifications as ViewOverrides )\n\t\t: undefined;\n}\n"],
|
|
5
|
-
"mappings": ";AAAA,SAAS,cAAc;AAYvB,IAAM,eAAe,CAAE,QAAQ,QAAS;AAExC,SAAS,cAAe,OAAuC;AAC9D,SACC,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAE,MAAM,QAAS,KAAM;AAExE;AAQA,SAAS,mBAAoB,OAA8B;AAC1D,QAAM,SAAsB,cAAe,KAAM,IAAI,EAAE,GAAG,MAAM,IAAI,CAAC;AACrE,aAAY,OAA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ACR;AACA,QAAM,QAAW,KAAK,WAAW,CAAC,GAAkB;AAAA,IACnD,CAAE,WAAY,CAAE,OAAO,KAAM,CAAE,MAAO,EAAE,UAAU,OAAO,KAAM;AAAA,EAChE;AACA,SAAO,EAAE,GAAG,MAAM,SAAS,CAAE,GAAG,QAAQ,GAAG,IAAK,EAAE;AACnD;
|
|
4
|
+
"sourcesContent": ["import { dequal } from 'dequal';\nimport type { View, Filter, SupportedLayouts } from '@wordpress/dataviews';\nimport type { ViewOverrides } from './types';\n\ntype PlainObject = Record< string, unknown >;\n\n/**\n * `page` and `search` are URL-managed: the URL is their only source. They are\n * never persisted, and no other layer may configure them — an absent URL param\n * is indistinguishable from the user having cleared the value, so any fallback\n * would resurrect a cleared search on the next read.\n */\nconst QUERY_PARAMS = [ 'page', 'search' ] as const;\n\nfunction isPlainObject( value: unknown ): value is PlainObject {\n\treturn (\n\t\ttypeof value === 'object' && value !== null && ! Array.isArray( value )\n\t);\n}\n\n/**\n * Copies a layer without the URL-managed properties.\n *\n * @param layer The layer to copy.\n * @return A copy of the layer with `page` and `search` removed.\n */\nfunction withoutQueryParams( layer: unknown ): PlainObject {\n\tconst result: PlainObject = isPlainObject( layer ) ? { ...layer } : {};\n\tfor ( const key of QUERY_PARAMS ) {\n\t\tdelete result[ key ];\n\t}\n\treturn result;\n}\n\n/**\n * Merges a layer on top of another, recursing into plain objects so the upper\n * layer only takes over the leaves it declares. Arrays and scalars are leaves:\n * they are replaced wholesale.\n *\n * Nested properties (`layout.styles.<field>.width`, `groupBy.direction`,\n * `sort.direction`) are merged leaf by leaf: a shallow merge would let a layer\n * that only declares one leaf wipe every sibling the layers below provide.\n *\n * @param lower The layer to merge into.\n * @param upper The layer to merge on top.\n * @return The merged layer.\n */\nfunction mergeLayer( lower: PlainObject, upper: PlainObject ): PlainObject {\n\tconst result: PlainObject = { ...lower };\n\tfor ( const key of Object.keys( upper ) ) {\n\t\tconst value = upper[ key ];\n\t\tconst current = result[ key ];\n\t\tresult[ key ] =\n\t\t\tisPlainObject( current ) && isPlainObject( value )\n\t\t\t\t? mergeLayer( current, value )\n\t\t\t\t: value;\n\t}\n\treturn result;\n}\n\n/**\n * The leaves of `value` that differ from `base`. Mirrors `mergeLayer`:\n * `mergeLayer( base, diffLayer( value, base ) )` resolves back to `value`.\n *\n * A persisted leaf that equals both `value` and `base` is kept even though it\n * is not a difference: the preference is shared by every view of the same\n * entity, and such a leaf may only differ from the base of *another* view. In\n * this one, where it coincides with the base, a revert is unobservable — so\n * equality cannot mean the user reverted it. Dropping a persisted leaf\n * requires `value` to actually move away from it, back to `base`.\n *\n * @param value The layer to compare.\n * @param base The layer to compare it against.\n * @param persisted The previously persisted leaves.\n * @return The leaves that differ, as a layer of its own.\n */\nfunction diffLayer(\n\tvalue: PlainObject,\n\tbase: PlainObject,\n\tpersisted: PlainObject = {}\n): PlainObject {\n\tconst result: PlainObject = {};\n\tfor ( const key of Object.keys( value ) ) {\n\t\tconst next = value[ key ];\n\t\tconst current = base[ key ];\n\t\tconst prev = persisted[ key ];\n\t\tif ( next === undefined ) {\n\t\t\tcontinue;\n\t\t}\n\t\tif ( isPlainObject( next ) && isPlainObject( current ) ) {\n\t\t\tconst nested = diffLayer(\n\t\t\t\tnext,\n\t\t\t\tcurrent,\n\t\t\t\tisPlainObject( prev ) ? prev : {}\n\t\t\t);\n\t\t\tif ( Object.keys( nested ).length > 0 ) {\n\t\t\t\tresult[ key ] = nested;\n\t\t\t}\n\t\t} else if ( ! dequal( next, current ) ) {\n\t\t\tresult[ key ] = next;\n\t\t} else if ( prev !== undefined && dequal( next, prev ) ) {\n\t\t\tresult[ key ] = prev;\n\t\t}\n\t}\n\treturn result;\n}\n\nfunction getLockedFilters( overrides?: ViewOverrides ): Filter[] {\n\treturn ( overrides?.filters ?? [] ).filter( ( filter ) => filter.isLocked );\n}\n\n/**\n * Hoists the locked filters an override provides to the front of the view's\n * filters, replacing any same-field filter. Locked filters are not the user's\n * to change, so they are pinned in place instead of taking part in the layer\n * resolution.\n *\n * @param view The view to apply the locked filters to.\n * @param overrides The active view overrides.\n * @return The view with the locked filters applied.\n */\nfunction applyLockedFilters(\n\tview: PlainObject,\n\toverrides?: ViewOverrides\n): PlainObject {\n\tconst locked = getLockedFilters( overrides );\n\tif ( locked.length === 0 ) {\n\t\treturn view;\n\t}\n\tconst rest = ( ( view.filters ?? [] ) as Filter[] ).filter(\n\t\t( filter ) => ! locked.some( ( f ) => f.field === filter.field )\n\t);\n\treturn { ...view, filters: [ ...locked, ...rest ] };\n}\n\ninterface ViewLayers {\n\tdefaultView?: View;\n\tdefaultLayouts?: SupportedLayouts;\n\tactiveViewOverrides?: ViewOverrides;\n\t/**\n\t * The user's persisted modifications: a partial view holding only the\n\t * properties they actually changed.\n\t */\n\tpersistedView?: ViewOverrides;\n}\n\ninterface ResolveViewArgs extends ViewLayers {\n\tpage?: number;\n\tsearch?: string;\n}\n\n/**\n * The user's persisted modifications that still apply.\n *\n * A persisted `type` that `defaultLayouts` does not offer is dropped: DataViews\n * renders nothing for a layout it is not given, and the preference may predate\n * the current set of layouts (a screen that used to offer the list layout, say,\n * and no longer does). The type then resolves out of the layers below, and the\n * other modifications are kept. Without `defaultLayouts` every type is offered.\n *\n * `true` is a valid entry, so the check mirrors DataViews': an entry counts as\n * long as it is truthy.\n *\n * @param persistedView The user's persisted modifications.\n * @param defaultLayouts The layouts on offer, keyed by layout type.\n * @return The modifications that apply, or `undefined` when there are none.\n */\nexport function getApplicablePersistedView(\n\tpersistedView: ViewOverrides | undefined,\n\tdefaultLayouts: SupportedLayouts | undefined\n): ViewOverrides | undefined {\n\tif (\n\t\t! persistedView ||\n\t\tpersistedView.type === undefined ||\n\t\t! defaultLayouts ||\n\t\tdefaultLayouts[ persistedView.type as keyof SupportedLayouts ]\n\t) {\n\t\treturn persistedView;\n\t}\n\tconst { type, ...rest } = persistedView;\n\treturn Object.keys( rest ).length > 0 ? rest : undefined;\n}\n\n/**\n * Resolves the layers below the user's modifications — what a modification has\n * to differ from to count as one. The layout type selects which entry of\n * `defaultLayouts` applies, so the base only makes sense relative to a type.\n *\n * @param layers The layers to resolve.\n * @param effectiveType The layout type to resolve the base at.\n * @return The resolved base view.\n */\nfunction resolveBaseView(\n\tlayers: ViewLayers,\n\teffectiveType: View[ 'type' ] | undefined\n): PlainObject {\n\tconst { defaultView, defaultLayouts, activeViewOverrides } = layers;\n\tconst layoutDefaults =\n\t\tdefaultLayouts?.[ effectiveType as keyof SupportedLayouts ];\n\treturn applyLockedFilters(\n\t\t[ layoutDefaults, activeViewOverrides ].reduce< PlainObject >(\n\t\t\t( lower, upper ) =>\n\t\t\t\tmergeLayer( lower, withoutQueryParams( upper ) ),\n\t\t\twithoutQueryParams( defaultView )\n\t\t),\n\t\tactiveViewOverrides\n\t);\n}\n\n/**\n * Resolves a view out of its layers. Later layers override earlier ones:\n *\n * 1. `defaultView`\n * 2. `defaultLayouts` (for the effective type)\n * 3. `activeViewOverrides`\n * 4. the user's persisted modifications (see `getApplicablePersistedView`)\n * 5. the URL query params (`page` and `search` only)\n *\n * @param args See `ResolveViewArgs`.\n * @return The resolved `view`.\n */\nexport function resolveView( args: ResolveViewArgs ): View {\n\tconst { defaultView, defaultLayouts, activeViewOverrides, page, search } =\n\t\targs;\n\tconst persistedView = getApplicablePersistedView(\n\t\targs.persistedView,\n\t\tdefaultLayouts\n\t);\n\n\t// Resolve the effective layout type first: it selects which entry of\n\t// `defaultLayouts` applies, and the layers above the layouts may change it.\n\tconst effectiveType =\n\t\tpersistedView?.type ?? activeViewOverrides?.type ?? defaultView?.type;\n\tconst baseView = resolveBaseView( args, effectiveType );\n\n\tconst view = {\n\t\t...applyLockedFilters(\n\t\t\tmergeLayer( baseView, withoutQueryParams( persistedView ) ),\n\t\t\tactiveViewOverrides\n\t\t),\n\t\tpage: Number( page ?? 1 ),\n\t\tsearch: search ?? '',\n\t};\n\n\t// The layers are resolved as plain objects: `View` requires a `type`, which\n\t// only the layers taken together are guaranteed to provide.\n\treturn view as unknown as View;\n}\n\n/**\n * The user's modifications: the leaves of the view they interacted with that\n * the layers below do not already resolve to. This is what gets persisted, so\n * that a change to any of those layers keeps showing through the properties the\n * user never touched.\n *\n * Previously persisted leaves that coincide with the base of the current view\n * are carried over rather than dropped: they may still make a difference in\n * another view sharing the preference. See `diffLayer`.\n *\n * The diff basis is resolved at `newView.type`, not at the type the previous\n * resolution used: the type selects the `defaultLayouts` entry, and a layout\n * switch reports the new type together with that entry's values. Diffing them\n * against the previous type's base would persist the difference between the\n * two entries as if the user had picked those values — leaves the layouts\n * already provide, which then linger as phantom modifications after switching\n * back.\n *\n * A persisted `type` the current layouts do not offer never applied (see\n * `getApplicablePersistedView`), so it is not carried over either: the\n * modifications returned replace it with whatever the user picked.\n *\n * @param newView The view the user produced.\n * @param layers The layers the view resolves from. See `ViewLayers`.\n * @return The modified properties, or `undefined` when the user modified none.\n */\nexport function getUserModifications(\n\tnewView: View,\n\tlayers: ViewLayers\n): ViewOverrides | undefined {\n\tconst { defaultLayouts, activeViewOverrides } = layers;\n\tconst persistedView = getApplicablePersistedView(\n\t\tlayers.persistedView,\n\t\tdefaultLayouts\n\t);\n\tconst baseView = resolveBaseView( layers, newView.type );\n\n\tconst modifications = diffLayer(\n\t\twithoutQueryParams( newView ),\n\t\twithoutQueryParams( baseView ),\n\t\twithoutQueryParams( persistedView )\n\t);\n\n\t// Locked filters are pinned on every read, so persisting them would only\n\t// leak them into the views that do not lock them.\n\tconst locked = getLockedFilters( activeViewOverrides );\n\tif ( locked.length > 0 && Array.isArray( modifications.filters ) ) {\n\t\tmodifications.filters = ( modifications.filters as Filter[] ).filter(\n\t\t\t( filter ) => ! locked.some( ( f ) => f.field === filter.field )\n\t\t);\n\t}\n\n\treturn Object.keys( modifications ).length > 0\n\t\t? ( modifications as ViewOverrides )\n\t\t: undefined;\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,SAAS,cAAc;AAYvB,IAAM,eAAe,CAAE,QAAQ,QAAS;AAExC,SAAS,cAAe,OA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B,aAAc,CAAE;AAAA,MAC1D;AAAA,IACD;AAAA,IACA,MAAM,OAAQ,QAAQ,CAAE;AAAA,IACxB,QAAQ,UAAU;AAAA,EACnB;AAIA,SAAO;AACR;AA4BO,SAAS,qBACf,SACA,QAC4B;AAC5B,QAAM,EAAE,gBAAgB,oBAAoB,IAAI;AAChD,QAAM,gBAAgB;AAAA,IACrB,OAAO;AAAA,IACP;AAAA,EACD;AACA,QAAM,WAAW,gBAAiB,QAAQ,QAAQ,IAAK;AAEvD,QAAM,gBAAgB;AAAA,IACrB,mBAAoB,OAAQ;AAAA,IAC5B,mBAAoB,QAAS;AAAA,IAC7B,mBAAoB,aAAc;AAAA,EACnC;AAIA,QAAM,SAAS,iBAAkB,mBAAoB;AACrD,MAAK,OAAO,SAAS,KAAK,MAAM,QAAS,cAAc,OAAQ,GAAI;AAClE,kBAAc,UAAY,cAAc,QAAsB;AAAA,MAC7D,CAAE,WAAY,CAAE,OAAO,KAAM,CAAE,MAAO,EAAE,UAAU,OAAO,KAAM;AAAA,IAChE;AAAA,EACD;AAEA,SAAO,OAAO,KAAM,aAAc,EAAE,SAAS,IACxC,gBACF;AACJ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -4,7 +4,11 @@ import { useCallback, useMemo } from "@wordpress/element";
|
|
|
4
4
|
import { useDispatch, useSelect } from "@wordpress/data";
|
|
5
5
|
import { store as preferencesStore } from "@wordpress/preferences";
|
|
6
6
|
import { generatePreferenceKey } from "./preference-keys.mjs";
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
getApplicablePersistedView,
|
|
9
|
+
getUserModifications,
|
|
10
|
+
resolveView
|
|
11
|
+
} from "./resolve-view.mjs";
|
|
8
12
|
function useView(config) {
|
|
9
13
|
const {
|
|
10
14
|
kind,
|
|
@@ -47,7 +51,11 @@ function useView(config) {
|
|
|
47
51
|
search
|
|
48
52
|
]
|
|
49
53
|
);
|
|
50
|
-
const
|
|
54
|
+
const applicablePersistedView = getApplicablePersistedView(
|
|
55
|
+
persistedView,
|
|
56
|
+
defaultLayouts
|
|
57
|
+
);
|
|
58
|
+
const isModified = !!applicablePersistedView && Object.keys(applicablePersistedView).length > 0;
|
|
51
59
|
const updateView = useCallback(
|
|
52
60
|
(newView) => {
|
|
53
61
|
const newQueryParams = {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/use-view.ts"],
|
|
4
|
-
"sourcesContent": ["import { dequal } from 'dequal';\nimport { useCallback, useMemo } from '@wordpress/element';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport type { View } from '@wordpress/dataviews';\nimport { store as preferencesStore } from '@wordpress/preferences';\nimport { generatePreferenceKey } from './preference-keys';\nimport {
|
|
5
|
-
"mappings": ";AAAA,SAAS,cAAc;AACvB,SAAS,aAAa,eAAe;AACrC,SAAS,aAAa,iBAAiB;AAEvC,SAAS,SAAS,wBAAwB;AAC1C,SAAS,6BAA6B;AACtC,
|
|
4
|
+
"sourcesContent": ["import { dequal } from 'dequal';\nimport { useCallback, useMemo } from '@wordpress/element';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport type { View } from '@wordpress/dataviews';\nimport { store as preferencesStore } from '@wordpress/preferences';\nimport { generatePreferenceKey } from './preference-keys';\nimport {\n\tgetApplicablePersistedView,\n\tgetUserModifications,\n\tresolveView,\n} from './resolve-view';\nimport type { ViewConfig, ViewOverrides } from './types';\n\ninterface UseViewReturn {\n\tview: View;\n\tisModified: boolean;\n\tupdateView: ( newView: View ) => void;\n\tresetToDefault: () => void;\n}\n\n/**\n * Hook for managing DataViews view state with local persistence.\n *\n * Only the properties the user actually modified are persisted: every other\n * property keeps resolving out of the layers below, so a change to the default\n * view, to the layout defaults or to the active view overrides keeps showing\n * through.\n *\n * @param config Configuration object for loading the view.\n *\n * @return Object with current view, modification state, and update functions.\n */\nexport function useView( config: ViewConfig ): UseViewReturn {\n\tconst {\n\t\tkind,\n\t\tname,\n\t\tslug,\n\t\tdefaultView,\n\t\tdefaultLayouts,\n\t\tactiveViewOverrides,\n\t\tqueryParams,\n\t\tonChangeQueryParams,\n\t} = config;\n\n\tconst preferenceKey = generatePreferenceKey( kind, name, slug );\n\tconst persistedView: ViewOverrides | undefined = useSelect(\n\t\t( select ) => {\n\t\t\treturn select( preferencesStore ).get(\n\t\t\t\t'core/views',\n\t\t\t\tpreferenceKey\n\t\t\t) as ViewOverrides | undefined;\n\t\t},\n\t\t[ preferenceKey ]\n\t);\n\tconst { set } = useDispatch( preferencesStore );\n\n\tconst page = Number( queryParams?.page ?? 1 );\n\tconst search = queryParams?.search ?? '';\n\n\tconst view = useMemo(\n\t\t() =>\n\t\t\tresolveView( {\n\t\t\t\tdefaultView,\n\t\t\t\tdefaultLayouts,\n\t\t\t\tactiveViewOverrides,\n\t\t\t\tpersistedView,\n\t\t\t\tpage,\n\t\t\t\tsearch,\n\t\t\t} ),\n\t\t[\n\t\t\tdefaultView,\n\t\t\tdefaultLayouts,\n\t\t\tactiveViewOverrides,\n\t\t\tpersistedView,\n\t\t\tpage,\n\t\t\tsearch,\n\t\t]\n\t);\n\n\t// A persisted `type` the layouts do not offer is ignored on read, so it\n\t// does not count as a modification either: there is nothing to reset.\n\tconst applicablePersistedView = getApplicablePersistedView(\n\t\tpersistedView,\n\t\tdefaultLayouts\n\t);\n\tconst isModified =\n\t\t!! applicablePersistedView &&\n\t\tObject.keys( applicablePersistedView ).length > 0;\n\n\tconst updateView = useCallback(\n\t\t( newView: View ) => {\n\t\t\t// `page` and `search` live in the URL, not in the preference: they\n\t\t\t// are reported back to the consumer instead of being persisted.\n\t\t\tconst newQueryParams = {\n\t\t\t\tpage: Number( newView?.page ?? 1 ),\n\t\t\t\tsearch: newView?.search ?? '',\n\t\t\t};\n\t\t\tif (\n\t\t\t\tonChangeQueryParams &&\n\t\t\t\t! dequal( newQueryParams, { page, search } )\n\t\t\t) {\n\t\t\t\tonChangeQueryParams( newQueryParams );\n\t\t\t}\n\n\t\t\tconst modifications = getUserModifications( newView, {\n\t\t\t\tdefaultView,\n\t\t\t\tdefaultLayouts,\n\t\t\t\tactiveViewOverrides,\n\t\t\t\tpersistedView,\n\t\t\t} );\n\t\t\t// Compared against the preference as stored, so a persisted `type`\n\t\t\t// the layouts do not offer is written out on the first update.\n\t\t\tif ( ! dequal( modifications, persistedView ) ) {\n\t\t\t\t// `undefined` clears the preference: the user reverted every\n\t\t\t\t// property they had modified.\n\t\t\t\tset( 'core/views', preferenceKey, modifications );\n\t\t\t}\n\t\t},\n\t\t[\n\t\t\tonChangeQueryParams,\n\t\t\tpage,\n\t\t\tsearch,\n\t\t\tdefaultView,\n\t\t\tdefaultLayouts,\n\t\t\tactiveViewOverrides,\n\t\t\tpersistedView,\n\t\t\tset,\n\t\t\tpreferenceKey,\n\t\t]\n\t);\n\n\tconst resetToDefault = useCallback( () => {\n\t\tset( 'core/views', preferenceKey, undefined );\n\t}, [ preferenceKey, set ] );\n\n\treturn {\n\t\tview,\n\t\tisModified,\n\t\tupdateView,\n\t\tresetToDefault,\n\t};\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,SAAS,cAAc;AACvB,SAAS,aAAa,eAAe;AACrC,SAAS,aAAa,iBAAiB;AAEvC,SAAS,SAAS,wBAAwB;AAC1C,SAAS,6BAA6B;AACtC;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAsBA,SAAS,QAAS,QAAoC;AAC5D,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD,IAAI;AAEJ,QAAM,gBAAgB,sBAAuB,MAAM,MAAM,IAAK;AAC9D,QAAM,gBAA2C;AAAA,IAChD,CAAE,WAAY;AACb,aAAO,OAAQ,gBAAiB,EAAE;AAAA,QACjC;AAAA,QACA;AAAA,MACD;AAAA,IACD;AAAA,IACA,CAAE,aAAc;AAAA,EACjB;AACA,QAAM,EAAE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
7
|
}
|
|
@@ -14,13 +14,30 @@ interface ResolveViewArgs extends ViewLayers {
|
|
|
14
14
|
page?: number;
|
|
15
15
|
search?: string;
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* The user's persisted modifications that still apply.
|
|
19
|
+
*
|
|
20
|
+
* A persisted `type` that `defaultLayouts` does not offer is dropped: DataViews
|
|
21
|
+
* renders nothing for a layout it is not given, and the preference may predate
|
|
22
|
+
* the current set of layouts (a screen that used to offer the list layout, say,
|
|
23
|
+
* and no longer does). The type then resolves out of the layers below, and the
|
|
24
|
+
* other modifications are kept. Without `defaultLayouts` every type is offered.
|
|
25
|
+
*
|
|
26
|
+
* `true` is a valid entry, so the check mirrors DataViews': an entry counts as
|
|
27
|
+
* long as it is truthy.
|
|
28
|
+
*
|
|
29
|
+
* @param persistedView The user's persisted modifications.
|
|
30
|
+
* @param defaultLayouts The layouts on offer, keyed by layout type.
|
|
31
|
+
* @return The modifications that apply, or `undefined` when there are none.
|
|
32
|
+
*/
|
|
33
|
+
export declare function getApplicablePersistedView(persistedView: ViewOverrides | undefined, defaultLayouts: SupportedLayouts | undefined): ViewOverrides | undefined;
|
|
17
34
|
/**
|
|
18
35
|
* Resolves a view out of its layers. Later layers override earlier ones:
|
|
19
36
|
*
|
|
20
37
|
* 1. `defaultView`
|
|
21
38
|
* 2. `defaultLayouts` (for the effective type)
|
|
22
39
|
* 3. `activeViewOverrides`
|
|
23
|
-
* 4. the user's persisted modifications
|
|
40
|
+
* 4. the user's persisted modifications (see `getApplicablePersistedView`)
|
|
24
41
|
* 5. the URL query params (`page` and `search` only)
|
|
25
42
|
*
|
|
26
43
|
* @param args See `ResolveViewArgs`.
|
|
@@ -45,6 +62,10 @@ export declare function resolveView(args: ResolveViewArgs): View;
|
|
|
45
62
|
* already provide, which then linger as phantom modifications after switching
|
|
46
63
|
* back.
|
|
47
64
|
*
|
|
65
|
+
* A persisted `type` the current layouts do not offer never applied (see
|
|
66
|
+
* `getApplicablePersistedView`), so it is not carried over either: the
|
|
67
|
+
* modifications returned replace it with whatever the user picked.
|
|
68
|
+
*
|
|
48
69
|
* @param newView The view the user produced.
|
|
49
70
|
* @param layers The layers the view resolves from. See `ViewLayers`.
|
|
50
71
|
* @return The modified properties, or `undefined` when the user modified none.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolve-view.d.ts","sourceRoot":"","sources":["../src/resolve-view.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAU,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAC3E,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAqI7C,UAAU,UAAU;IACnB,WAAW,CAAC,EAAE,IAAI,CAAC;IACnB,cAAc,CAAC,EAAE,gBAAgB,CAAC;IAClC,mBAAmB,CAAC,EAAE,aAAa,CAAC;IACpC;;;OAGG;IACH,aAAa,CAAC,EAAE,aAAa,CAAC;CAC9B;AAED,UAAU,eAAgB,SAAQ,UAAU;IAC3C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CAChB;AA4BD;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAE,IAAI,EAAE,eAAe,GAAI,IAAI,
|
|
1
|
+
{"version":3,"file":"resolve-view.d.ts","sourceRoot":"","sources":["../src/resolve-view.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAU,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAC3E,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAqI7C,UAAU,UAAU;IACnB,WAAW,CAAC,EAAE,IAAI,CAAC;IACnB,cAAc,CAAC,EAAE,gBAAgB,CAAC;IAClC,mBAAmB,CAAC,EAAE,aAAa,CAAC;IACpC;;;OAGG;IACH,aAAa,CAAC,EAAE,aAAa,CAAC;CAC9B;AAED,UAAU,eAAgB,SAAQ,UAAU;IAC3C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,0BAA0B,CACzC,aAAa,EAAE,aAAa,GAAG,SAAS,EACxC,cAAc,EAAE,gBAAgB,GAAG,SAAS,GAC1C,aAAa,GAAG,SAAS,CAW3B;AA4BD;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAE,IAAI,EAAE,eAAe,GAAI,IAAI,CA0BzD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,oBAAoB,CACnC,OAAO,EAAE,IAAI,EACb,MAAM,EAAE,UAAU,GAChB,aAAa,GAAG,SAAS,CA0B3B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-view.d.ts","sourceRoot":"","sources":["../src/use-view.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"use-view.d.ts","sourceRoot":"","sources":["../src/use-view.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAQjD,OAAO,KAAK,EAAE,UAAU,EAAiB,MAAM,SAAS,CAAC;AAEzD,UAAU,aAAa;IACtB,IAAI,EAAE,IAAI,CAAC;IACX,UAAU,EAAE,OAAO,CAAC;IACpB,UAAU,EAAE,CAAE,OAAO,EAAE,IAAI,KAAM,IAAI,CAAC;IACtC,cAAc,EAAE,MAAM,IAAI,CAAC;CAC3B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,OAAO,CAAE,MAAM,EAAE,UAAU,GAAI,aAAa,CA6G3D"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/views",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.22.0",
|
|
4
4
|
"description": "View persistence and management for WordPress DataViews.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -41,23 +41,22 @@
|
|
|
41
41
|
},
|
|
42
42
|
"types": "build-types",
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@wordpress/core-data": "^8.0.
|
|
45
|
-
"@wordpress/data": "^10.
|
|
46
|
-
"@wordpress/dataviews": "^19.0.
|
|
47
|
-
"@wordpress/element": "^8.
|
|
48
|
-
"@wordpress/preferences": "^4.
|
|
49
|
-
"@wordpress/private-apis": "^1.
|
|
44
|
+
"@wordpress/core-data": "^8.0.0",
|
|
45
|
+
"@wordpress/data": "^10.55.0",
|
|
46
|
+
"@wordpress/dataviews": "^19.0.0",
|
|
47
|
+
"@wordpress/element": "^8.7.0",
|
|
48
|
+
"@wordpress/preferences": "^4.55.0",
|
|
49
|
+
"@wordpress/private-apis": "^1.55.0",
|
|
50
50
|
"dequal": "^2.0.3"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@testing-library/react": "^16.3.
|
|
54
|
-
"@types/jest": "^30.0.0",
|
|
53
|
+
"@testing-library/react": "^16.3.3",
|
|
55
54
|
"react": "^18.3.1",
|
|
56
55
|
"react-dom": "^18.3.1",
|
|
57
|
-
"vitest": "^
|
|
56
|
+
"vitest": "^5.0.0"
|
|
58
57
|
},
|
|
59
58
|
"publishConfig": {
|
|
60
59
|
"access": "public"
|
|
61
60
|
},
|
|
62
|
-
"gitHead": "
|
|
61
|
+
"gitHead": "485f42ae8a1c58ceea18371a507fd4acfa86fbd8"
|
|
63
62
|
}
|
package/src/resolve-view.ts
CHANGED
|
@@ -149,6 +149,38 @@ interface ResolveViewArgs extends ViewLayers {
|
|
|
149
149
|
search?: string;
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
+
/**
|
|
153
|
+
* The user's persisted modifications that still apply.
|
|
154
|
+
*
|
|
155
|
+
* A persisted `type` that `defaultLayouts` does not offer is dropped: DataViews
|
|
156
|
+
* renders nothing for a layout it is not given, and the preference may predate
|
|
157
|
+
* the current set of layouts (a screen that used to offer the list layout, say,
|
|
158
|
+
* and no longer does). The type then resolves out of the layers below, and the
|
|
159
|
+
* other modifications are kept. Without `defaultLayouts` every type is offered.
|
|
160
|
+
*
|
|
161
|
+
* `true` is a valid entry, so the check mirrors DataViews': an entry counts as
|
|
162
|
+
* long as it is truthy.
|
|
163
|
+
*
|
|
164
|
+
* @param persistedView The user's persisted modifications.
|
|
165
|
+
* @param defaultLayouts The layouts on offer, keyed by layout type.
|
|
166
|
+
* @return The modifications that apply, or `undefined` when there are none.
|
|
167
|
+
*/
|
|
168
|
+
export function getApplicablePersistedView(
|
|
169
|
+
persistedView: ViewOverrides | undefined,
|
|
170
|
+
defaultLayouts: SupportedLayouts | undefined
|
|
171
|
+
): ViewOverrides | undefined {
|
|
172
|
+
if (
|
|
173
|
+
! persistedView ||
|
|
174
|
+
persistedView.type === undefined ||
|
|
175
|
+
! defaultLayouts ||
|
|
176
|
+
defaultLayouts[ persistedView.type as keyof SupportedLayouts ]
|
|
177
|
+
) {
|
|
178
|
+
return persistedView;
|
|
179
|
+
}
|
|
180
|
+
const { type, ...rest } = persistedView;
|
|
181
|
+
return Object.keys( rest ).length > 0 ? rest : undefined;
|
|
182
|
+
}
|
|
183
|
+
|
|
152
184
|
/**
|
|
153
185
|
* Resolves the layers below the user's modifications — what a modification has
|
|
154
186
|
* to differ from to count as one. The layout type selects which entry of
|
|
@@ -181,15 +213,19 @@ function resolveBaseView(
|
|
|
181
213
|
* 1. `defaultView`
|
|
182
214
|
* 2. `defaultLayouts` (for the effective type)
|
|
183
215
|
* 3. `activeViewOverrides`
|
|
184
|
-
* 4. the user's persisted modifications
|
|
216
|
+
* 4. the user's persisted modifications (see `getApplicablePersistedView`)
|
|
185
217
|
* 5. the URL query params (`page` and `search` only)
|
|
186
218
|
*
|
|
187
219
|
* @param args See `ResolveViewArgs`.
|
|
188
220
|
* @return The resolved `view`.
|
|
189
221
|
*/
|
|
190
222
|
export function resolveView( args: ResolveViewArgs ): View {
|
|
191
|
-
const { defaultView,
|
|
223
|
+
const { defaultView, defaultLayouts, activeViewOverrides, page, search } =
|
|
192
224
|
args;
|
|
225
|
+
const persistedView = getApplicablePersistedView(
|
|
226
|
+
args.persistedView,
|
|
227
|
+
defaultLayouts
|
|
228
|
+
);
|
|
193
229
|
|
|
194
230
|
// Resolve the effective layout type first: it selects which entry of
|
|
195
231
|
// `defaultLayouts` applies, and the layers above the layouts may change it.
|
|
@@ -229,6 +265,10 @@ export function resolveView( args: ResolveViewArgs ): View {
|
|
|
229
265
|
* already provide, which then linger as phantom modifications after switching
|
|
230
266
|
* back.
|
|
231
267
|
*
|
|
268
|
+
* A persisted `type` the current layouts do not offer never applied (see
|
|
269
|
+
* `getApplicablePersistedView`), so it is not carried over either: the
|
|
270
|
+
* modifications returned replace it with whatever the user picked.
|
|
271
|
+
*
|
|
232
272
|
* @param newView The view the user produced.
|
|
233
273
|
* @param layers The layers the view resolves from. See `ViewLayers`.
|
|
234
274
|
* @return The modified properties, or `undefined` when the user modified none.
|
|
@@ -237,7 +277,11 @@ export function getUserModifications(
|
|
|
237
277
|
newView: View,
|
|
238
278
|
layers: ViewLayers
|
|
239
279
|
): ViewOverrides | undefined {
|
|
240
|
-
const {
|
|
280
|
+
const { defaultLayouts, activeViewOverrides } = layers;
|
|
281
|
+
const persistedView = getApplicablePersistedView(
|
|
282
|
+
layers.persistedView,
|
|
283
|
+
defaultLayouts
|
|
284
|
+
);
|
|
241
285
|
const baseView = resolveBaseView( layers, newView.type );
|
|
242
286
|
|
|
243
287
|
const modifications = diffLayer(
|
package/src/test/resolve-view.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest';
|
|
2
2
|
import type { View } from '@wordpress/dataviews';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
getApplicablePersistedView,
|
|
5
|
+
getUserModifications,
|
|
6
|
+
resolveView,
|
|
7
|
+
} from '../resolve-view';
|
|
4
8
|
|
|
5
9
|
const LOCKED = {
|
|
6
10
|
field: 'author',
|
|
@@ -62,3 +66,102 @@ describe( 'locked filters', () => {
|
|
|
62
66
|
).toBeUndefined();
|
|
63
67
|
} );
|
|
64
68
|
} );
|
|
69
|
+
|
|
70
|
+
// A preference may carry a `type` the screen no longer offers, because it
|
|
71
|
+
// predates the current `defaultLayouts` (a screen that used to offer the list
|
|
72
|
+
// layout, say). DataViews renders nothing for a type it is not given, so such a
|
|
73
|
+
// type is ignored and the layers below decide.
|
|
74
|
+
describe( 'persisted type the layouts do not offer', () => {
|
|
75
|
+
const defaultView = { type: 'table', perPage: 20 } as unknown as View;
|
|
76
|
+
const defaultLayouts = { table: { perPage: 50 }, grid: true as const };
|
|
77
|
+
|
|
78
|
+
it( 'should resolve the type out of the layers below', () => {
|
|
79
|
+
const view = resolveView( {
|
|
80
|
+
defaultView,
|
|
81
|
+
defaultLayouts,
|
|
82
|
+
persistedView: { type: 'list' },
|
|
83
|
+
} );
|
|
84
|
+
expect( view.type ).toBe( 'table' );
|
|
85
|
+
expect( view.perPage ).toBe( 50 );
|
|
86
|
+
} );
|
|
87
|
+
|
|
88
|
+
it( 'should keep the other persisted modifications', () => {
|
|
89
|
+
const view = resolveView( {
|
|
90
|
+
defaultView,
|
|
91
|
+
defaultLayouts,
|
|
92
|
+
persistedView: { type: 'list', perPage: 10 },
|
|
93
|
+
} );
|
|
94
|
+
expect( view ).toMatchObject( { type: 'table', perPage: 10 } );
|
|
95
|
+
} );
|
|
96
|
+
|
|
97
|
+
it( 'should prefer the type an override sets over the default view', () => {
|
|
98
|
+
const view = resolveView( {
|
|
99
|
+
defaultView,
|
|
100
|
+
defaultLayouts,
|
|
101
|
+
activeViewOverrides: { type: 'grid' },
|
|
102
|
+
persistedView: { type: 'list' },
|
|
103
|
+
} );
|
|
104
|
+
expect( view.type ).toBe( 'grid' );
|
|
105
|
+
} );
|
|
106
|
+
|
|
107
|
+
it( 'should honor a persisted type with a `true` layout entry', () => {
|
|
108
|
+
const view = resolveView( {
|
|
109
|
+
defaultView,
|
|
110
|
+
defaultLayouts,
|
|
111
|
+
persistedView: { type: 'grid' },
|
|
112
|
+
} );
|
|
113
|
+
expect( view.type ).toBe( 'grid' );
|
|
114
|
+
} );
|
|
115
|
+
|
|
116
|
+
it( 'should honor any persisted type without default layouts', () => {
|
|
117
|
+
const view = resolveView( {
|
|
118
|
+
defaultView,
|
|
119
|
+
persistedView: { type: 'list' },
|
|
120
|
+
} );
|
|
121
|
+
expect( view.type ).toBe( 'list' );
|
|
122
|
+
} );
|
|
123
|
+
|
|
124
|
+
it( 'should not carry the type over to the next modifications', () => {
|
|
125
|
+
const layers = {
|
|
126
|
+
defaultView,
|
|
127
|
+
defaultLayouts,
|
|
128
|
+
persistedView: { type: 'list' as const },
|
|
129
|
+
};
|
|
130
|
+
const view = resolveView( layers );
|
|
131
|
+
expect( getUserModifications( view, layers ) ).toBeUndefined();
|
|
132
|
+
expect(
|
|
133
|
+
getUserModifications( { ...view, perPage: 10 } as View, layers )
|
|
134
|
+
).toEqual( { perPage: 10 } );
|
|
135
|
+
} );
|
|
136
|
+
|
|
137
|
+
describe( 'getApplicablePersistedView', () => {
|
|
138
|
+
it( 'should drop the type and keep the rest', () => {
|
|
139
|
+
expect(
|
|
140
|
+
getApplicablePersistedView(
|
|
141
|
+
{ type: 'list', perPage: 10 },
|
|
142
|
+
defaultLayouts
|
|
143
|
+
)
|
|
144
|
+
).toEqual( { perPage: 10 } );
|
|
145
|
+
} );
|
|
146
|
+
|
|
147
|
+
it( 'should return `undefined` when the type was the only modification', () => {
|
|
148
|
+
expect(
|
|
149
|
+
getApplicablePersistedView( { type: 'list' }, defaultLayouts )
|
|
150
|
+
).toBeUndefined();
|
|
151
|
+
} );
|
|
152
|
+
|
|
153
|
+
it( 'should return the preference as is when its type is offered', () => {
|
|
154
|
+
const persistedView = { type: 'grid' as const, perPage: 10 };
|
|
155
|
+
expect(
|
|
156
|
+
getApplicablePersistedView( persistedView, defaultLayouts )
|
|
157
|
+
).toBe( persistedView );
|
|
158
|
+
} );
|
|
159
|
+
|
|
160
|
+
it( 'should return the preference as is without default layouts', () => {
|
|
161
|
+
const persistedView = { type: 'list' as const };
|
|
162
|
+
expect(
|
|
163
|
+
getApplicablePersistedView( persistedView, undefined )
|
|
164
|
+
).toBe( persistedView );
|
|
165
|
+
} );
|
|
166
|
+
} );
|
|
167
|
+
} );
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
1
2
|
import { act, renderHook } from '@testing-library/react';
|
|
2
3
|
import { createRegistry, RegistryProvider } from '@wordpress/data';
|
|
3
4
|
import { store as preferencesStore } from '@wordpress/preferences';
|
|
@@ -152,7 +153,10 @@ describe( 'useView', () => {
|
|
|
152
153
|
const { result } = renderUseView( registry, {
|
|
153
154
|
...BASE_PROPS,
|
|
154
155
|
defaultView: { type: 'list', perPage: 20 },
|
|
155
|
-
defaultLayouts: {
|
|
156
|
+
defaultLayouts: {
|
|
157
|
+
table: { perPage: 50 },
|
|
158
|
+
grid: { perPage: 40 },
|
|
159
|
+
},
|
|
156
160
|
activeViewOverrides: {
|
|
157
161
|
type: 'table',
|
|
158
162
|
perPage: 10,
|
|
@@ -258,6 +262,62 @@ describe( 'useView', () => {
|
|
|
258
262
|
expect( result.current.view.perPage ).toBe( 20 );
|
|
259
263
|
} );
|
|
260
264
|
|
|
265
|
+
// DataViews renders nothing for a type it is not given, and the
|
|
266
|
+
// preference may predate the current layouts.
|
|
267
|
+
describe( 'a persisted type the default layouts do not offer', () => {
|
|
268
|
+
const props = {
|
|
269
|
+
...BASE_PROPS,
|
|
270
|
+
defaultView: { type: 'table', perPage: 20 },
|
|
271
|
+
defaultLayouts: { table: { perPage: 50 }, grid: true },
|
|
272
|
+
} as unknown as Parameters< typeof useView >[ 0 ];
|
|
273
|
+
|
|
274
|
+
it( 'should be ignored in favor of the layers below', () => {
|
|
275
|
+
const registry = createTestRegistry();
|
|
276
|
+
setPreference( registry, { type: 'list', perPage: 10 } );
|
|
277
|
+
const { result } = renderUseView( registry, props );
|
|
278
|
+
expect( result.current.view ).toMatchObject( {
|
|
279
|
+
type: 'table',
|
|
280
|
+
perPage: 10,
|
|
281
|
+
} );
|
|
282
|
+
} );
|
|
283
|
+
|
|
284
|
+
it( 'should not count as a modification', () => {
|
|
285
|
+
const registry = createTestRegistry();
|
|
286
|
+
setPreference( registry, { type: 'list' } );
|
|
287
|
+
const { result } = renderUseView( registry, props );
|
|
288
|
+
expect( result.current.isModified ).toBe( false );
|
|
289
|
+
} );
|
|
290
|
+
|
|
291
|
+
it( 'should be written out of the preference on the next update', () => {
|
|
292
|
+
const registry = createTestRegistry();
|
|
293
|
+
setPreference( registry, { type: 'list' } );
|
|
294
|
+
const { result, rerender } = renderUseView( registry, props );
|
|
295
|
+
act( () => {
|
|
296
|
+
result.current.updateView( {
|
|
297
|
+
...result.current.view,
|
|
298
|
+
perPage: 10,
|
|
299
|
+
} );
|
|
300
|
+
} );
|
|
301
|
+
rerender( props );
|
|
302
|
+
|
|
303
|
+
expect( getPreference( registry ) ).toEqual( { perPage: 10 } );
|
|
304
|
+
expect( result.current.isModified ).toBe( true );
|
|
305
|
+
} );
|
|
306
|
+
|
|
307
|
+
it( 'should be cleared from the preference by an update that changes nothing', () => {
|
|
308
|
+
const registry = createTestRegistry();
|
|
309
|
+
setPreference( registry, { type: 'list' } );
|
|
310
|
+
const { result, rerender } = renderUseView( registry, props );
|
|
311
|
+
act( () => {
|
|
312
|
+
result.current.updateView( result.current.view );
|
|
313
|
+
} );
|
|
314
|
+
rerender( props );
|
|
315
|
+
|
|
316
|
+
expect( getPreference( registry ) ).toBeUndefined();
|
|
317
|
+
expect( result.current.isModified ).toBe( false );
|
|
318
|
+
} );
|
|
319
|
+
} );
|
|
320
|
+
|
|
261
321
|
it( 'should ignore a missing layout entry for the effective type', () => {
|
|
262
322
|
const registry = createTestRegistry();
|
|
263
323
|
const { result } = renderUseView( registry, {
|
|
@@ -448,7 +508,7 @@ describe( 'useView', () => {
|
|
|
448
508
|
|
|
449
509
|
it( 'should be reported through `onChangeQueryParams` instead of being persisted', () => {
|
|
450
510
|
const registry = createTestRegistry();
|
|
451
|
-
const onChangeQueryParams =
|
|
511
|
+
const onChangeQueryParams = vi.fn();
|
|
452
512
|
const { result } = renderUseView( registry, {
|
|
453
513
|
...BASE_PROPS,
|
|
454
514
|
defaultView: { type: 'table' },
|
|
@@ -471,7 +531,7 @@ describe( 'useView', () => {
|
|
|
471
531
|
|
|
472
532
|
it( 'should not report unchanged query params', () => {
|
|
473
533
|
const registry = createTestRegistry();
|
|
474
|
-
const onChangeQueryParams =
|
|
534
|
+
const onChangeQueryParams = vi.fn();
|
|
475
535
|
const { result } = renderUseView( registry, {
|
|
476
536
|
...BASE_PROPS,
|
|
477
537
|
defaultView: { type: 'table' },
|
|
@@ -492,7 +552,7 @@ describe( 'useView', () => {
|
|
|
492
552
|
let queryParams: { page?: number; search?: string } = {
|
|
493
553
|
search: 'level',
|
|
494
554
|
};
|
|
495
|
-
const onChangeQueryParams =
|
|
555
|
+
const onChangeQueryParams = vi.fn(
|
|
496
556
|
( newParams: { page?: number; search?: string } ) => {
|
|
497
557
|
// Mirrors real consumers, which drop an empty search from
|
|
498
558
|
// the URL instead of serializing `?search=`.
|
package/src/use-view.ts
CHANGED
|
@@ -4,7 +4,11 @@ import { useDispatch, useSelect } from '@wordpress/data';
|
|
|
4
4
|
import type { View } from '@wordpress/dataviews';
|
|
5
5
|
import { store as preferencesStore } from '@wordpress/preferences';
|
|
6
6
|
import { generatePreferenceKey } from './preference-keys';
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
getApplicablePersistedView,
|
|
9
|
+
getUserModifications,
|
|
10
|
+
resolveView,
|
|
11
|
+
} from './resolve-view';
|
|
8
12
|
import type { ViewConfig, ViewOverrides } from './types';
|
|
9
13
|
|
|
10
14
|
interface UseViewReturn {
|
|
@@ -73,8 +77,15 @@ export function useView( config: ViewConfig ): UseViewReturn {
|
|
|
73
77
|
]
|
|
74
78
|
);
|
|
75
79
|
|
|
80
|
+
// A persisted `type` the layouts do not offer is ignored on read, so it
|
|
81
|
+
// does not count as a modification either: there is nothing to reset.
|
|
82
|
+
const applicablePersistedView = getApplicablePersistedView(
|
|
83
|
+
persistedView,
|
|
84
|
+
defaultLayouts
|
|
85
|
+
);
|
|
76
86
|
const isModified =
|
|
77
|
-
!!
|
|
87
|
+
!! applicablePersistedView &&
|
|
88
|
+
Object.keys( applicablePersistedView ).length > 0;
|
|
78
89
|
|
|
79
90
|
const updateView = useCallback(
|
|
80
91
|
( newView: View ) => {
|
|
@@ -97,6 +108,8 @@ export function useView( config: ViewConfig ): UseViewReturn {
|
|
|
97
108
|
activeViewOverrides,
|
|
98
109
|
persistedView,
|
|
99
110
|
} );
|
|
111
|
+
// Compared against the preference as stored, so a persisted `type`
|
|
112
|
+
// the layouts do not offer is written out on the first update.
|
|
100
113
|
if ( ! dequal( modifications, persistedView ) ) {
|
|
101
114
|
// `undefined` clears the preference: the user reverted every
|
|
102
115
|
// property they had modified.
|