@wordpress/views 1.18.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.
Files changed (46) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +5 -0
  3. package/build/load-view.cjs +21 -21
  4. package/build/load-view.cjs.map +2 -2
  5. package/build/lock-unlock.cjs.map +2 -2
  6. package/build/resolve-view.cjs +133 -0
  7. package/build/resolve-view.cjs.map +7 -0
  8. package/build/types.cjs.map +1 -1
  9. package/build/use-view-config.cjs.map +2 -2
  10. package/build/use-view.cjs +37 -57
  11. package/build/use-view.cjs.map +2 -2
  12. package/build-module/load-view.mjs +21 -21
  13. package/build-module/load-view.mjs.map +2 -2
  14. package/build-module/lock-unlock.mjs.map +2 -2
  15. package/build-module/resolve-view.mjs +107 -0
  16. package/build-module/resolve-view.mjs.map +7 -0
  17. package/build-module/use-view-config.mjs.map +2 -2
  18. package/build-module/use-view.mjs +37 -60
  19. package/build-module/use-view.mjs.map +2 -2
  20. package/build-types/load-view.d.ts +5 -2
  21. package/build-types/load-view.d.ts.map +1 -1
  22. package/build-types/lock-unlock.d.ts.map +1 -1
  23. package/build-types/resolve-view.d.ts +54 -0
  24. package/build-types/resolve-view.d.ts.map +1 -0
  25. package/build-types/types.d.ts +19 -21
  26. package/build-types/types.d.ts.map +1 -1
  27. package/build-types/use-view-config.d.ts.map +1 -1
  28. package/build-types/use-view.d.ts +5 -0
  29. package/build-types/use-view.d.ts.map +1 -1
  30. package/package.json +13 -8
  31. package/src/load-view.ts +26 -38
  32. package/src/lock-unlock.ts +0 -3
  33. package/src/resolve-view.ts +261 -0
  34. package/src/test/resolve-view.ts +63 -0
  35. package/src/test/use-view.tsx +966 -0
  36. package/src/types.ts +20 -24
  37. package/src/use-view-config.ts +0 -7
  38. package/src/use-view.ts +50 -92
  39. package/build/filter-utils.cjs +0 -134
  40. package/build/filter-utils.cjs.map +0 -7
  41. package/build-module/filter-utils.mjs +0 -108
  42. package/build-module/filter-utils.mjs.map +0 -7
  43. package/build-types/filter-utils.d.ts +0 -31
  44. package/build-types/filter-utils.d.ts.map +0 -1
  45. package/src/filter-utils.ts +0 -180
  46. package/src/test/filter-utils.ts +0 -522
@@ -0,0 +1,107 @@
1
+ // packages/views/src/resolve-view.ts
2
+ import { dequal } from "dequal";
3
+ var QUERY_PARAMS = ["page", "search"];
4
+ function isPlainObject(value) {
5
+ return typeof value === "object" && value !== null && !Array.isArray(value);
6
+ }
7
+ function withoutQueryParams(layer) {
8
+ const result = isPlainObject(layer) ? { ...layer } : {};
9
+ for (const key of QUERY_PARAMS) {
10
+ delete result[key];
11
+ }
12
+ return result;
13
+ }
14
+ function mergeLayer(lower, upper) {
15
+ const result = { ...lower };
16
+ for (const key of Object.keys(upper)) {
17
+ const value = upper[key];
18
+ const current = result[key];
19
+ result[key] = isPlainObject(current) && isPlainObject(value) ? mergeLayer(current, value) : value;
20
+ }
21
+ return result;
22
+ }
23
+ function diffLayer(value, base, persisted = {}) {
24
+ const result = {};
25
+ for (const key of Object.keys(value)) {
26
+ const next = value[key];
27
+ const current = base[key];
28
+ const prev = persisted[key];
29
+ if (next === void 0) {
30
+ continue;
31
+ }
32
+ if (isPlainObject(next) && isPlainObject(current)) {
33
+ const nested = diffLayer(
34
+ next,
35
+ current,
36
+ isPlainObject(prev) ? prev : {}
37
+ );
38
+ if (Object.keys(nested).length > 0) {
39
+ result[key] = nested;
40
+ }
41
+ } else if (!dequal(next, current)) {
42
+ result[key] = next;
43
+ } else if (prev !== void 0 && dequal(next, prev)) {
44
+ result[key] = prev;
45
+ }
46
+ }
47
+ return result;
48
+ }
49
+ function getLockedFilters(overrides) {
50
+ return (overrides?.filters ?? []).filter((filter) => filter.isLocked);
51
+ }
52
+ function applyLockedFilters(view, overrides) {
53
+ const locked = getLockedFilters(overrides);
54
+ if (locked.length === 0) {
55
+ return view;
56
+ }
57
+ const rest = (view.filters ?? []).filter(
58
+ (filter) => !locked.some((f) => f.field === filter.field)
59
+ );
60
+ return { ...view, filters: [...locked, ...rest] };
61
+ }
62
+ function resolveBaseView(layers, effectiveType) {
63
+ const { defaultView, defaultLayouts, activeViewOverrides } = layers;
64
+ const layoutDefaults = defaultLayouts?.[effectiveType];
65
+ return applyLockedFilters(
66
+ [layoutDefaults, activeViewOverrides].reduce(
67
+ (lower, upper) => mergeLayer(lower, withoutQueryParams(upper)),
68
+ withoutQueryParams(defaultView)
69
+ ),
70
+ activeViewOverrides
71
+ );
72
+ }
73
+ function resolveView(args) {
74
+ const { defaultView, activeViewOverrides, persistedView, page, search } = args;
75
+ const effectiveType = persistedView?.type ?? activeViewOverrides?.type ?? defaultView?.type;
76
+ const baseView = resolveBaseView(args, effectiveType);
77
+ const view = {
78
+ ...applyLockedFilters(
79
+ mergeLayer(baseView, withoutQueryParams(persistedView)),
80
+ activeViewOverrides
81
+ ),
82
+ page: Number(page ?? 1),
83
+ search: search ?? ""
84
+ };
85
+ return view;
86
+ }
87
+ function getUserModifications(newView, layers) {
88
+ const { activeViewOverrides, persistedView } = layers;
89
+ const baseView = resolveBaseView(layers, newView.type);
90
+ const modifications = diffLayer(
91
+ withoutQueryParams(newView),
92
+ withoutQueryParams(baseView),
93
+ withoutQueryParams(persistedView)
94
+ );
95
+ const locked = getLockedFilters(activeViewOverrides);
96
+ if (locked.length > 0 && Array.isArray(modifications.filters)) {
97
+ modifications.filters = modifications.filters.filter(
98
+ (filter) => !locked.some((f) => f.field === filter.field)
99
+ );
100
+ }
101
+ return Object.keys(modifications).length > 0 ? modifications : void 0;
102
+ }
103
+ export {
104
+ getUserModifications,
105
+ resolveView
106
+ };
107
+ //# sourceMappingURL=resolve-view.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 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,OA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AAA,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
+ "names": []
7
+ }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/use-view-config.ts"],
4
- "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { View, SupportedLayouts, Form } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport { unlock } from './lock-unlock';\n\n/**\n * A hook that retrieves the view configuration for a given entity\n * from the core data store.\n *\n * @param {Object} params\n * @param {string} params.kind The kind of the entity.\n * @param {string} params.name The name of the entity.\n * @param {?(string|string[])} [params.fields] Subset of top-level config\n * properties to request, as an array\n * or a comma-separated string\n * (mapped to the REST API `_fields`\n * parameter). When omitted, the full\n * config is requested.\n * @return {Object} An object containing the `default_view`, `default_layouts`, `view_list`, and `form` configuration for the entity.\n */\nexport function useViewConfig( {\n\tkind,\n\tname,\n\tfields,\n}: {\n\tkind: string;\n\tname: string;\n\tfields?: string | string[];\n} ): {\n\tdefault_view: View | undefined;\n\tdefault_layouts: SupportedLayouts | undefined;\n\tview_list: Array< any > | undefined;\n\tform: Form | undefined;\n} {\n\t// Sort fields so the cache key is independent of the order callers list\n\t// them; `['title','author']` and `['author','title']` request the same data.\n\tconst fieldList = Array.isArray( fields ) ? fields : fields?.split( ',' );\n\tconst fieldsKey = fieldList\n\t\t? [ ...fieldList ].sort().join( ',' )\n\t\t: undefined;\n\treturn useSelect(\n\t\t( select ) => {\n\t\t\treturn unlock( select( coreStore ) ).getViewConfig( kind, name, {\n\t\t\t\tfields: fieldsKey,\n\t\t\t} );\n\t\t},\n\t\t[ kind, name, fieldsKey ]\n\t);\n}\n"],
5
- "mappings": ";AAGA,SAAS,iBAAiB;AAC1B,SAAS,SAAS,iBAAiB;AAMnC,SAAS,cAAc;AAiBhB,SAAS,cAAe;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AACD,GASE;AAGD,QAAM,YAAY,MAAM,QAAS,MAAO,IAAI,SAAS,QAAQ,MAAO,GAAI;AACxE,QAAM,YAAY,YACf,CAAE,GAAG,SAAU,EAAE,KAAK,EAAE,KAAM,GAAI,IAClC;AACH,SAAO;AAAA,IACN,CAAE,WAAY;AACb,aAAO,OAAQ,OAAQ,SAAU,CAAE,EAAE,cAAe,MAAM,MAAM;AAAA,QAC/D,QAAQ;AAAA,MACT,CAAE;AAAA,IACH;AAAA,IACA,CAAE,MAAM,MAAM,SAAU;AAAA,EACzB;AACD;",
4
+ "sourcesContent": ["import { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { View, SupportedLayouts, Form } from '@wordpress/dataviews';\nimport { unlock } from './lock-unlock';\n\n/**\n * A hook that retrieves the view configuration for a given entity\n * from the core data store.\n *\n * @param {Object} params\n * @param {string} params.kind The kind of the entity.\n * @param {string} params.name The name of the entity.\n * @param {?(string|string[])} [params.fields] Subset of top-level config\n * properties to request, as an array\n * or a comma-separated string\n * (mapped to the REST API `_fields`\n * parameter). When omitted, the full\n * config is requested.\n * @return {Object} An object containing the `default_view`, `default_layouts`, `view_list`, and `form` configuration for the entity.\n */\nexport function useViewConfig( {\n\tkind,\n\tname,\n\tfields,\n}: {\n\tkind: string;\n\tname: string;\n\tfields?: string | string[];\n} ): {\n\tdefault_view: View | undefined;\n\tdefault_layouts: SupportedLayouts | undefined;\n\tview_list: Array< any > | undefined;\n\tform: Form | undefined;\n} {\n\t// Sort fields so the cache key is independent of the order callers list\n\t// them; `['title','author']` and `['author','title']` request the same data.\n\tconst fieldList = Array.isArray( fields ) ? fields : fields?.split( ',' );\n\tconst fieldsKey = fieldList\n\t\t? [ ...fieldList ].sort().join( ',' )\n\t\t: undefined;\n\treturn useSelect(\n\t\t( select ) => {\n\t\t\treturn unlock( select( coreStore ) ).getViewConfig( kind, name, {\n\t\t\t\tfields: fieldsKey,\n\t\t\t} );\n\t\t},\n\t\t[ kind, name, fieldsKey ]\n\t);\n}\n"],
5
+ "mappings": ";AAAA,SAAS,iBAAiB;AAC1B,SAAS,SAAS,iBAAiB;AAEnC,SAAS,cAAc;AAiBhB,SAAS,cAAe;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AACD,GASE;AAGD,QAAM,YAAY,MAAM,QAAS,MAAO,IAAI,SAAS,QAAQ,MAAO,GAAI;AACxE,QAAM,YAAY,YACf,CAAE,GAAG,SAAU,EAAE,KAAK,EAAE,KAAM,GAAI,IAClC;AACH,SAAO;AAAA,IACN,CAAE,WAAY;AACb,aAAO,OAAQ,OAAQ,SAAU,CAAE,EAAE,cAAe,MAAM,MAAM;AAAA,QAC/D,QAAQ;AAAA,MACT,CAAE;AAAA,IACH;AAAA,IACA,CAAE,MAAM,MAAM,SAAU;AAAA,EACzB;AACD;",
6
6
  "names": []
7
7
  }
@@ -4,23 +4,14 @@ 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 {
8
- mergeActiveViewOverrides,
9
- stripActiveViewOverrides
10
- } from "./filter-utils.mjs";
11
- function omit(obj, keys) {
12
- const result = { ...obj };
13
- for (const key of keys) {
14
- delete result[key];
15
- }
16
- return result;
17
- }
7
+ import { getUserModifications, resolveView } from "./resolve-view.mjs";
18
8
  function useView(config) {
19
9
  const {
20
10
  kind,
21
11
  name,
22
12
  slug,
23
13
  defaultView,
14
+ defaultLayouts,
24
15
  activeViewOverrides,
25
16
  queryParams,
26
17
  onChangeQueryParams
@@ -36,68 +27,54 @@ function useView(config) {
36
27
  [preferenceKey]
37
28
  );
38
29
  const { set } = useDispatch(preferencesStore);
39
- const baseView = useMemo(
40
- () => persistedView ?? defaultView ?? {},
41
- [persistedView, defaultView]
30
+ const page = Number(queryParams?.page ?? 1);
31
+ const search = queryParams?.search ?? "";
32
+ const view = useMemo(
33
+ () => resolveView({
34
+ defaultView,
35
+ defaultLayouts,
36
+ activeViewOverrides,
37
+ persistedView,
38
+ page,
39
+ search
40
+ }),
41
+ [
42
+ defaultView,
43
+ defaultLayouts,
44
+ activeViewOverrides,
45
+ persistedView,
46
+ page,
47
+ search
48
+ ]
42
49
  );
43
- const page = Number(queryParams?.page ?? baseView.page ?? 1);
44
- const search = queryParams?.search ?? baseView.search ?? "";
45
- const combinedOverrides = useMemo(() => {
46
- const rawDefaults = config.defaultLayouts?.[baseView.type];
47
- const layoutTypeDefaults = !rawDefaults || rawDefaults === true ? {} : rawDefaults;
48
- return { ...layoutTypeDefaults, ...activeViewOverrides };
49
- }, [config.defaultLayouts, baseView.type, activeViewOverrides]);
50
- const view = useMemo(() => {
51
- return mergeActiveViewOverrides(
52
- {
53
- ...baseView,
54
- page,
55
- search
56
- },
57
- combinedOverrides,
58
- defaultView
59
- );
60
- }, [baseView, page, search, combinedOverrides, defaultView]);
61
- const isModified = !!persistedView;
50
+ const isModified = !!persistedView && Object.keys(persistedView).length > 0;
62
51
  const updateView = useCallback(
63
52
  (newView) => {
64
- const urlParams = {
65
- page: newView?.page,
66
- search: newView?.search
53
+ const newQueryParams = {
54
+ page: Number(newView?.page ?? 1),
55
+ search: newView?.search ?? ""
67
56
  };
68
- const preferenceView = stripActiveViewOverrides(
69
- omit(newView, ["page", "search"]),
70
- combinedOverrides,
71
- defaultView
72
- );
73
- if (onChangeQueryParams && !dequal(urlParams, { page, search })) {
74
- onChangeQueryParams(urlParams);
57
+ if (onChangeQueryParams && !dequal(newQueryParams, { page, search })) {
58
+ onChangeQueryParams(newQueryParams);
75
59
  }
76
- const comparableBaseView = stripActiveViewOverrides(
77
- baseView,
78
- combinedOverrides,
79
- defaultView
80
- );
81
- const comparableDefaultView = stripActiveViewOverrides(
60
+ const modifications = getUserModifications(newView, {
82
61
  defaultView,
83
- combinedOverrides,
84
- defaultView
85
- );
86
- if (!dequal(comparableBaseView, preferenceView)) {
87
- if (dequal(preferenceView, comparableDefaultView)) {
88
- set("core/views", preferenceKey, void 0);
89
- } else {
90
- set("core/views", preferenceKey, preferenceView);
91
- }
62
+ defaultLayouts,
63
+ activeViewOverrides,
64
+ persistedView
65
+ });
66
+ if (!dequal(modifications, persistedView)) {
67
+ set("core/views", preferenceKey, modifications);
92
68
  }
93
69
  },
94
70
  [
95
71
  onChangeQueryParams,
96
72
  page,
97
73
  search,
98
- baseView,
99
74
  defaultView,
100
- combinedOverrides,
75
+ defaultLayouts,
76
+ activeViewOverrides,
77
+ persistedView,
101
78
  set,
102
79
  preferenceKey
103
80
  ]
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/use-view.ts"],
4
- "sourcesContent": ["/**\n * External dependencies\n */\nimport { dequal } from 'dequal';\n\n/**\n * WordPress dependencies\n */\nimport { useCallback, useMemo } from '@wordpress/element';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport type { View } from '@wordpress/dataviews';\n// @ts-ignore - Preferences package is not typed\nimport { store as preferencesStore } from '@wordpress/preferences';\n\n/**\n * Internal dependencies\n */\nimport { generatePreferenceKey } from './preference-keys';\nimport {\n\tmergeActiveViewOverrides,\n\tstripActiveViewOverrides,\n} from './filter-utils';\nimport type { ViewConfig } from './types';\n\ninterface UseViewReturn {\n\tview: View;\n\tisModified: boolean;\n\tupdateView: ( newView: View ) => void;\n\tresetToDefault: () => void;\n}\n\nfunction omit< T extends object, K extends keyof T >(\n\tobj: T,\n\tkeys: K[]\n): Omit< T, K > {\n\tconst result = { ...obj };\n\tfor ( const key of keys ) {\n\t\tdelete result[ key ];\n\t}\n\treturn result;\n}\n\n/**\n * Hook for managing DataViews view state with local persistence.\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\tactiveViewOverrides,\n\t\tqueryParams,\n\t\tonChangeQueryParams,\n\t} = config;\n\n\tconst preferenceKey = generatePreferenceKey( kind, name, slug );\n\tconst persistedView: View | 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 View | undefined;\n\t\t},\n\t\t[ preferenceKey ]\n\t);\n\tconst { set } = useDispatch( preferencesStore );\n\n\tconst baseView: View = useMemo(\n\t\t() => persistedView ?? defaultView ?? {},\n\t\t[ persistedView, defaultView ]\n\t);\n\tconst page = Number( queryParams?.page ?? baseView.page ?? 1 );\n\tconst search = queryParams?.search ?? baseView.search ?? '';\n\n\tconst combinedOverrides = useMemo( () => {\n\t\tconst rawDefaults =\n\t\t\tconfig.defaultLayouts?.[\n\t\t\t\tbaseView.type as keyof typeof config.defaultLayouts\n\t\t\t];\n\t\tconst layoutTypeDefaults =\n\t\t\t! rawDefaults || rawDefaults === true ? {} : rawDefaults;\n\t\treturn { ...layoutTypeDefaults, ...activeViewOverrides };\n\t}, [ config.defaultLayouts, baseView.type, activeViewOverrides ] );\n\n\t// Merge URL query parameters (page, search) and activeViewOverrides into the view\n\tconst view: View = useMemo( () => {\n\t\treturn mergeActiveViewOverrides(\n\t\t\t{\n\t\t\t\t...baseView,\n\t\t\t\tpage,\n\t\t\t\tsearch,\n\t\t\t},\n\t\t\tcombinedOverrides,\n\t\t\tdefaultView\n\t\t);\n\t}, [ baseView, page, search, combinedOverrides, defaultView ] );\n\n\tconst isModified = !! persistedView;\n\n\tconst updateView = useCallback(\n\t\t( newView: View ) => {\n\t\t\t// Extract URL params (page, search) from the new view\n\t\t\tconst urlParams: { page?: number; search?: string } = {\n\t\t\t\tpage: newView?.page,\n\t\t\t\tsearch: newView?.search,\n\t\t\t};\n\t\t\t// Strip activeViewOverrides and URL params before persisting\n\t\t\t// Cast is safe: omitting page/search doesn't change the discriminant (type field)\n\t\t\tconst preferenceView = stripActiveViewOverrides(\n\t\t\t\tomit( newView, [ 'page', 'search' ] ) as View,\n\t\t\t\tcombinedOverrides,\n\t\t\t\tdefaultView\n\t\t\t);\n\n\t\t\t// If we have URL handling enabled, separate URL state from preference state\n\t\t\tif (\n\t\t\t\tonChangeQueryParams &&\n\t\t\t\t! dequal( urlParams, { page, search } )\n\t\t\t) {\n\t\t\t\tonChangeQueryParams( urlParams );\n\t\t\t}\n\n\t\t\t// Compare with baseView and defaultView after stripping activeViewOverrides\n\t\t\tconst comparableBaseView = stripActiveViewOverrides(\n\t\t\t\tbaseView,\n\t\t\t\tcombinedOverrides,\n\t\t\t\tdefaultView\n\t\t\t);\n\t\t\tconst comparableDefaultView = stripActiveViewOverrides(\n\t\t\t\tdefaultView,\n\t\t\t\tcombinedOverrides,\n\t\t\t\tdefaultView\n\t\t\t);\n\n\t\t\t// Only persist non-URL preferences if different from baseView\n\t\t\tif ( ! dequal( comparableBaseView, preferenceView ) ) {\n\t\t\t\tif ( dequal( preferenceView, comparableDefaultView ) ) {\n\t\t\t\t\tset( 'core/views', preferenceKey, undefined );\n\t\t\t\t} else {\n\t\t\t\t\tset( 'core/views', preferenceKey, preferenceView );\n\t\t\t\t}\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\tbaseView,\n\t\t\tdefaultView,\n\t\t\tcombinedOverrides,\n\t\t\tset,\n\t\t\tpreferenceKey,\n\t\t]\n\t);\n\n\tconst resetToDefault = useCallback( () => {\n\t\tset( 'core/views', preferenceKey, undef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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 { getUserModifications, resolveView } 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\tconst isModified =\n\t\t!! persistedView && Object.keys( persistedView ).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\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,SAAS,sBAAsB,mBAAmB;AAsB3C,SAAS,QAAS,QAAoC;AAC5D,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AA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AA,IACD;AAAA,EACD;AAEA,QAAM,iBAAiB,YAAa,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": []
7
7
  }
@@ -1,16 +1,19 @@
1
- import type { View } from '@wordpress/dataviews';
2
1
  import type { ViewConfig } from './types';
3
2
  /**
4
3
  * Async function for loading view state in route loaders.
5
4
  *
5
+ * Resolves the same layers `useView` does, so a route loader and the hook that
6
+ * takes over from it agree on the view.
7
+ *
6
8
  * @param config Configuration object for loading the view.
7
9
  * @param config.kind Entity kind (e.g., 'postType', 'taxonomy', 'root').
8
10
  * @param config.name Specific entity name.
9
11
  * @param config.slug View identifier.
10
12
  * @param config.defaultView Default view configuration.
13
+ * @param config.defaultLayouts Default layout configurations keyed by layout type.
11
14
  * @param config.activeViewOverrides View overrides applied on top but never persisted.
12
15
  * @param config.queryParams Object with `page` and/or `search` from URL.
13
16
  * @return Promise resolving to the loaded view object.
14
17
  */
15
- export declare function loadView(config: ViewConfig): Promise<View>;
18
+ export declare function loadView(config: ViewConfig): Promise<import("@wordpress/dataviews").View>;
16
19
  //# sourceMappingURL=load-view.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"load-view.d.ts","sourceRoot":"","sources":["../src/load-view.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAOjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1C;;;;;;;;;;;GAWG;AACH,wBAAsB,QAAQ,CAAE,MAAM,EAAE,UAAU,iBA8BjD"}
1
+ {"version":3,"file":"load-view.d.ts","sourceRoot":"","sources":["../src/load-view.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,UAAU,EAAiB,MAAM,SAAS,CAAC;AAEzD;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,QAAQ,CAAE,MAAM,EAAE,UAAU,gDAuBjD"}
@@ -1 +1 @@
1
- {"version":3,"file":"lock-unlock.d.ts","sourceRoot":"","sources":["../src/lock-unlock.ts"],"names":[],"mappings":"AAKA,eAAO,MAAQ,IAAI,mDAAE,MAAM,iCAIzB,CAAC"}
1
+ {"version":3,"file":"lock-unlock.d.ts","sourceRoot":"","sources":["../src/lock-unlock.ts"],"names":[],"mappings":"AAEA,eAAO,MAAQ,IAAI,mDAAE,MAAM,iCAIzB,CAAC"}
@@ -0,0 +1,54 @@
1
+ import type { View, SupportedLayouts } from '@wordpress/dataviews';
2
+ import type { ViewOverrides } from './types';
3
+ interface ViewLayers {
4
+ defaultView?: View;
5
+ defaultLayouts?: SupportedLayouts;
6
+ activeViewOverrides?: ViewOverrides;
7
+ /**
8
+ * The user's persisted modifications: a partial view holding only the
9
+ * properties they actually changed.
10
+ */
11
+ persistedView?: ViewOverrides;
12
+ }
13
+ interface ResolveViewArgs extends ViewLayers {
14
+ page?: number;
15
+ search?: string;
16
+ }
17
+ /**
18
+ * Resolves a view out of its layers. Later layers override earlier ones:
19
+ *
20
+ * 1. `defaultView`
21
+ * 2. `defaultLayouts` (for the effective type)
22
+ * 3. `activeViewOverrides`
23
+ * 4. the user's persisted modifications
24
+ * 5. the URL query params (`page` and `search` only)
25
+ *
26
+ * @param args See `ResolveViewArgs`.
27
+ * @return The resolved `view`.
28
+ */
29
+ export declare function resolveView(args: ResolveViewArgs): View;
30
+ /**
31
+ * The user's modifications: the leaves of the view they interacted with that
32
+ * the layers below do not already resolve to. This is what gets persisted, so
33
+ * that a change to any of those layers keeps showing through the properties the
34
+ * user never touched.
35
+ *
36
+ * Previously persisted leaves that coincide with the base of the current view
37
+ * are carried over rather than dropped: they may still make a difference in
38
+ * another view sharing the preference. See `diffLayer`.
39
+ *
40
+ * The diff basis is resolved at `newView.type`, not at the type the previous
41
+ * resolution used: the type selects the `defaultLayouts` entry, and a layout
42
+ * switch reports the new type together with that entry's values. Diffing them
43
+ * against the previous type's base would persist the difference between the
44
+ * two entries as if the user had picked those values — leaves the layouts
45
+ * already provide, which then linger as phantom modifications after switching
46
+ * back.
47
+ *
48
+ * @param newView The view the user produced.
49
+ * @param layers The layers the view resolves from. See `ViewLayers`.
50
+ * @return The modified properties, or `undefined` when the user modified none.
51
+ */
52
+ export declare function getUserModifications(newView: View, layers: ViewLayers): ViewOverrides | undefined;
53
+ export {};
54
+ //# sourceMappingURL=resolve-view.d.ts.map
@@ -0,0 +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,CAsBzD;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,oBAAoB,CACnC,OAAO,EAAE,IAAI,EACb,MAAM,EAAE,UAAU,GAChB,aAAa,GAAG,SAAS,CAsB3B"}
@@ -1,19 +1,16 @@
1
+ import type { View, SupportedLayouts } from '@wordpress/dataviews';
1
2
  /**
2
- * WordPress dependencies
3
+ * A layer merged on top of a view: the active view overrides, and the user's
4
+ * persisted modifications.
5
+ *
6
+ * It is derived from `View` so it stays in sync with it, but every property is
7
+ * optional — including `type`, which `View` requires as the discriminant of its
8
+ * layout union. A layer only ever carries the subset of properties it means to
9
+ * override, and `layout` is loosened to a plain record because a `type`
10
+ * override may change which layout shape applies.
3
11
  */
4
- import type { View, SupportedLayouts } from '@wordpress/dataviews';
5
- export type ActiveViewOverrides = {
6
- titleField?: View['titleField'];
7
- showTitle?: View['showTitle'];
8
- mediaField?: View['mediaField'];
9
- showMedia?: View['showMedia'];
10
- descriptionField?: View['descriptionField'];
11
- showDescription?: View['showDescription'];
12
- showLevels?: View['showLevels'];
13
- infiniteScrollEnabled?: View['infiniteScrollEnabled'];
14
- filters?: View['filters'];
15
- sort?: View['sort'];
16
- groupBy?: View['groupBy'];
12
+ export type ViewOverrides = Partial<Omit<View, 'type' | 'layout'>> & {
13
+ type?: View['type'];
17
14
  layout?: Record<string, unknown>;
18
15
  };
19
16
  export interface ViewConfig {
@@ -32,16 +29,17 @@ export interface ViewConfig {
32
29
  */
33
30
  slug: string;
34
31
  /**
35
- * Default view configuration
32
+ * Default view configuration.
36
33
  */
37
34
  defaultView: View;
38
35
  /**
39
- * View overrides applied on top of the persisted view but never persisted.
40
- * These represent tab-specific configuration (filters, sort) and
41
- * developer-defined view defaults that should override the persisted
42
- * view settings.
36
+ * View overrides applied on top of the default view and the default
37
+ * layouts, but never persisted. These represent tab-specific configuration
38
+ * (filters, sort) and developer-defined view defaults. The properties the
39
+ * user modified take precedence over them; the ones they never touched
40
+ * resolve out of them.
43
41
  */
44
- activeViewOverrides?: ActiveViewOverrides;
42
+ activeViewOverrides?: ViewOverrides;
45
43
  /**
46
44
  * Default layout configurations keyed by layout type.
47
45
  * Used to apply layout-type-specific defaults (e.g., table column styles)
@@ -49,7 +47,7 @@ export interface ViewConfig {
49
47
  */
50
48
  defaultLayouts?: SupportedLayouts;
51
49
  /**
52
- * Optional query parameters from URL (page, search)
50
+ * Optional query parameters from URL (page, search).
53
51
  */
54
52
  queryParams?: {
55
53
  page?: number;
@@ -1 +1 @@
1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use-view-config.d.ts","sourceRoot":"","sources":["../src/use-view-config.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,IAAI,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAOzE;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,aAAa,CAAE,EAC9B,IAAI,EACJ,IAAI,EACJ,MAAM,GACN,EAAE;IACF,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CAC3B,GAAI;IACJ,YAAY,EAAE,IAAI,GAAG,SAAS,CAAC;IAC/B,eAAe,EAAE,gBAAgB,GAAG,SAAS,CAAC;IAC9C,SAAS,EAAE,KAAK,CAAE,GAAG,CAAE,GAAG,SAAS,CAAC;IACpC,IAAI,EAAE,IAAI,GAAG,SAAS,CAAC;CACvB,CAeA"}
1
+ {"version":3,"file":"use-view-config.d.ts","sourceRoot":"","sources":["../src/use-view-config.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAGzE;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,aAAa,CAAE,EAC9B,IAAI,EACJ,IAAI,EACJ,MAAM,GACN,EAAE;IACF,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CAC3B,GAAI;IACJ,YAAY,EAAE,IAAI,GAAG,SAAS,CAAC;IAC/B,eAAe,EAAE,gBAAgB,GAAG,SAAS,CAAC;IAC9C,SAAS,EAAE,KAAK,CAAE,GAAG,CAAE,GAAG,SAAS,CAAC;IACpC,IAAI,EAAE,IAAI,GAAG,SAAS,CAAC;CACvB,CAeA"}
@@ -9,6 +9,11 @@ interface UseViewReturn {
9
9
  /**
10
10
  * Hook for managing DataViews view state with local persistence.
11
11
  *
12
+ * Only the properties the user actually modified are persisted: every other
13
+ * property keeps resolving out of the layers below, so a change to the default
14
+ * view, to the layout defaults or to the active view overrides keeps showing
15
+ * through.
16
+ *
12
17
  * @param config Configuration object for loading the view.
13
18
  *
14
19
  * @return Object with current view, modification state, and update functions.
@@ -1 +1 @@
1
- {"version":3,"file":"use-view.d.ts","sourceRoot":"","sources":["../src/use-view.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAYjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1C,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;AAaD;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAE,MAAM,EAAE,UAAU,GAAI,aAAa,CAyH3D"}
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;AAIjD,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,CAoG3D"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/views",
3
- "version": "1.18.0",
3
+ "version": "1.20.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,16 +41,21 @@
41
41
  },
42
42
  "types": "build-types",
43
43
  "dependencies": {
44
- "@wordpress/core-data": "^7.51.0",
45
- "@wordpress/data": "^10.51.0",
46
- "@wordpress/dataviews": "^17.2.0",
47
- "@wordpress/element": "^8.3.0",
48
- "@wordpress/preferences": "^4.51.0",
49
- "@wordpress/private-apis": "^1.51.0",
44
+ "@wordpress/core-data": "^7.53.0",
45
+ "@wordpress/data": "^10.53.0",
46
+ "@wordpress/dataviews": "^18.0.0",
47
+ "@wordpress/element": "^8.5.0",
48
+ "@wordpress/preferences": "^4.53.0",
49
+ "@wordpress/private-apis": "^1.53.0",
50
50
  "dequal": "^2.0.3"
51
51
  },
52
+ "devDependencies": {
53
+ "@testing-library/react": "^16.3.2",
54
+ "react": "^18.3.1",
55
+ "react-dom": "^18.3.1"
56
+ },
52
57
  "publishConfig": {
53
58
  "access": "public"
54
59
  },
55
- "gitHead": "e9a74f9c14095a34398ecd4d1f7a908e55051205"
60
+ "gitHead": "989764d42ff93e33684aaf0b96a5b7f3d9bbff52"
56
61
  }