@wordpress/views 1.19.0 → 1.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/CHANGELOG.md +11 -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
package/CHANGELOG.md CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 1.20.0 (2026-08-12)
6
+
7
+ ### Bug Fixes
8
+
9
+ - `useView`, `loadView`. Resolve the view as a stack of layers — `defaultView`, `defaultLayouts` (for the effective type), `activeViewOverrides`, the user's preference, and the URL query params — and persist only the properties the user actually modified. Previously the whole view was persisted, so a later change to any of the lower layers stopped showing through, an override could bounce back a value the user had picked, and a partial `sort` or `layout` override marked the view as modified forever. `page` and `search` are now sourced from the URL alone, and `layout`, `groupBy` and `sort` merge leaf by leaf, so an override for one field's styles no longer wipes the other fields'. Persisted modifications that happen to coincide with the current view's base are retained on update, so a change made in one view no longer drops a pick made in another view sharing the preference. [#80832](https://github.com/WordPress/gutenberg/pull/80832)
10
+
11
+ ### Internal
12
+
13
+ - Remove obsolete dependency grouping comments as part of the repository-wide separator-free import migration. ([#81248](https://github.com/WordPress/gutenberg/pull/81248))
14
+ - Add `react` and `react-dom` to the package's dev dependencies, so the tests resolve them from the package rather than relying on a hoisted install. [#81139](https://github.com/WordPress/gutenberg/pull/81139)
15
+
5
16
  ## 1.19.0 (2026-07-29)
6
17
 
7
18
  ## 1.18.0 (2026-07-14)
package/README.md CHANGED
@@ -25,6 +25,8 @@ npm install @wordpress/views --save
25
25
 
26
26
  Async function for loading view state in route loaders.
27
27
 
28
+ Resolves the same layers `useView` does, so a route loader and the hook that takes over from it agree on the view.
29
+
28
30
  _Parameters_
29
31
 
30
32
  - _config_ `ViewConfig`: Configuration object for loading the view.
@@ -32,6 +34,7 @@ _Parameters_
32
34
  - _config.name_ `ViewConfig`: Specific entity name.
33
35
  - _config.slug_ `ViewConfig`: View identifier.
34
36
  - _config.defaultView_ `ViewConfig`: Default view configuration.
37
+ - _config.defaultLayouts_ `ViewConfig`: Default layout configurations keyed by layout type.
35
38
  - _config.activeViewOverrides_ `ViewConfig`: View overrides applied on top but never persisted.
36
39
  - _config.queryParams_ `ViewConfig`: Object with `page` and/or `search` from URL.
37
40
 
@@ -43,6 +46,8 @@ _Returns_
43
46
 
44
47
  Hook for managing DataViews view state with local persistence.
45
48
 
49
+ Only the properties the user actually modified are persisted: every other property keeps resolving out of the layers below, so a change to the default view, to the layout defaults or to the active view overrides keeps showing through.
50
+
46
51
  _Parameters_
47
52
 
48
53
  - _config_ `ViewConfig`: Configuration object for loading the view.
@@ -26,29 +26,29 @@ module.exports = __toCommonJS(load_view_exports);
26
26
  var import_data = require("@wordpress/data");
27
27
  var import_preferences = require("@wordpress/preferences");
28
28
  var import_preference_keys = require("./preference-keys.cjs");
29
- var import_filter_utils = require("./filter-utils.cjs");
29
+ var import_resolve_view = require("./resolve-view.cjs");
30
30
  async function loadView(config) {
31
- const { kind, name, slug, defaultView, activeViewOverrides, queryParams } = config;
31
+ const {
32
+ kind,
33
+ name,
34
+ slug,
35
+ defaultView,
36
+ defaultLayouts,
37
+ activeViewOverrides,
38
+ queryParams
39
+ } = config;
32
40
  const preferenceKey = (0, import_preference_keys.generatePreferenceKey)(kind, name, slug);
33
- const persistedView = (0, import_data.select)(import_preferences.store).get(
34
- "core/views",
35
- preferenceKey
36
- );
37
- const baseView = persistedView ?? defaultView;
38
- const page = queryParams?.page ?? 1;
39
- const search = queryParams?.search ?? "";
40
- const rawDefaults = config.defaultLayouts?.[baseView?.type];
41
- const layoutTypeDefaults = !rawDefaults || rawDefaults === true ? {} : rawDefaults;
42
- const combinedOverrides = { ...layoutTypeDefaults, ...activeViewOverrides };
43
- return (0, import_filter_utils.mergeActiveViewOverrides)(
44
- {
45
- ...baseView,
46
- page,
47
- search
48
- },
49
- combinedOverrides,
50
- defaultView
51
- );
41
+ const persistedView = (0, import_data.select)(
42
+ import_preferences.store
43
+ ).get("core/views", preferenceKey);
44
+ return (0, import_resolve_view.resolveView)({
45
+ defaultView,
46
+ defaultLayouts,
47
+ activeViewOverrides,
48
+ persistedView,
49
+ page: queryParams?.page,
50
+ search: queryParams?.search
51
+ });
52
52
  }
53
53
  // Annotate the CommonJS export names for ESM import in node:
54
54
  0 && (module.exports = {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/load-view.ts"],
4
- "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { select } from '@wordpress/data';\n// @ts-ignore - Preferences package is not typed\nimport { store as preferencesStore } from '@wordpress/preferences';\nimport type { View } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport { generatePreferenceKey } from './preference-keys';\nimport { mergeActiveViewOverrides } from './filter-utils';\nimport type { ViewConfig } from './types';\n\n/**\n * Async function for loading view state in route loaders.\n *\n * @param config Configuration object for loading the view.\n * @param config.kind Entity kind (e.g., 'postType', 'taxonomy', 'root').\n * @param config.name Specific entity name.\n * @param config.slug View identifier.\n * @param config.defaultView Default view configuration.\n * @param config.activeViewOverrides View overrides applied on top but never persisted.\n * @param config.queryParams Object with `page` and/or `search` from URL.\n * @return Promise resolving to the loaded view object.\n */\nexport async function loadView( config: ViewConfig ) {\n\tconst { kind, name, slug, defaultView, activeViewOverrides, queryParams } =\n\t\tconfig;\n\tconst preferenceKey = generatePreferenceKey( kind, name, slug );\n\tconst persistedView: View | undefined = select( preferencesStore ).get(\n\t\t'core/views',\n\t\tpreferenceKey\n\t) as View | undefined;\n\n\tconst baseView = persistedView ?? defaultView;\n\tconst page = queryParams?.page ?? 1;\n\tconst search = queryParams?.search ?? '';\n\n\tconst rawDefaults =\n\t\tconfig.defaultLayouts?.[\n\t\t\tbaseView?.type as keyof typeof config.defaultLayouts\n\t\t];\n\tconst layoutTypeDefaults =\n\t\t! rawDefaults || rawDefaults === true ? {} : rawDefaults;\n\tconst combinedOverrides = { ...layoutTypeDefaults, ...activeViewOverrides };\n\n\treturn mergeActiveViewOverrides(\n\t\t{\n\t\t\t...baseView,\n\t\t\tpage,\n\t\t\tsearch,\n\t\t},\n\t\tcombinedOverrides,\n\t\tdefaultView\n\t);\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAAuB;AAEvB,yBAA0C;AAM1C,6BAAsC;AACtC,0BAAyC;AAezC,eAAsB,SAAU,QAAqB;AACpD,QAAM,EAAE,MAAM,MAAM,MAAM,aAAa,qBAAqB,YAAY,IACvE;AACD,QAAM,oBAAgB,8CAAuB,MAAM,MAAM,IAAK;AAC9D,QAAM,oBAAkC,oBAAQ,mBAAAA,KAAiB,EAAE;AAAA,IAClE;AAAA,IACA;AAAA,EACD;AAEA,QAAM,WAAW,iBAAiB;AAClC,QAAM,OAAO,aAAa,QAAQ;AAClC,QAAM,SAAS,aAAa,UAAU;AAEtC,QAAM,cACL,OAAO,iBACN,UAAU,IACX;AACD,QAAM,qBACL,CAAE,eAAe,gBAAgB,OAAO,CAAC,IAAI;AAC9C,QAAM,oBAAoB,EAAE,GAAG,oBAAoB,GAAG,oBAAoB;AAE1E,aAAO;AAAA,IACN;AAAA,MACC,GAAG;AAAA,MACH;AAAA,MACA;AAAA,IACD;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;",
4
+ "sourcesContent": ["import { select } from '@wordpress/data';\nimport { store as preferencesStore } from '@wordpress/preferences';\nimport { generatePreferenceKey } from './preference-keys';\nimport { resolveView } from './resolve-view';\nimport type { ViewConfig, ViewOverrides } from './types';\n\n/**\n * Async function for loading view state in route loaders.\n *\n * Resolves the same layers `useView` does, so a route loader and the hook that\n * takes over from it agree on the view.\n *\n * @param config Configuration object for loading the view.\n * @param config.kind Entity kind (e.g., 'postType', 'taxonomy', 'root').\n * @param config.name Specific entity name.\n * @param config.slug View identifier.\n * @param config.defaultView Default view configuration.\n * @param config.defaultLayouts Default layout configurations keyed by layout type.\n * @param config.activeViewOverrides View overrides applied on top but never persisted.\n * @param config.queryParams Object with `page` and/or `search` from URL.\n * @return Promise resolving to the loaded view object.\n */\nexport async function loadView( config: ViewConfig ) {\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} = config;\n\tconst preferenceKey = generatePreferenceKey( kind, name, slug );\n\tconst persistedView: ViewOverrides | undefined = select(\n\t\tpreferencesStore\n\t).get( 'core/views', preferenceKey ) as ViewOverrides | undefined;\n\n\treturn resolveView( {\n\t\tdefaultView,\n\t\tdefaultLayouts,\n\t\tactiveViewOverrides,\n\t\tpersistedView,\n\t\tpage: queryParams?.page,\n\t\tsearch: queryParams?.search,\n\t} );\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAuB;AACvB,yBAA0C;AAC1C,6BAAsC;AACtC,0BAA4B;AAmB5B,eAAsB,SAAU,QAAqB;AACpD,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD,IAAI;AACJ,QAAM,oBAAgB,8CAAuB,MAAM,MAAM,IAAK;AAC9D,QAAM,oBAA2C;AAAA,IAChD,mBAAAA;AAAA,EACD,EAAE,IAAK,cAAc,aAAc;AAEnC,aAAO,iCAAa;AAAA,IACnB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,MAAM,aAAa;AAAA,IACnB,QAAQ,aAAa;AAAA,EACtB,CAAE;AACH;",
6
6
  "names": ["preferencesStore"]
7
7
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/lock-unlock.ts"],
4
- "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';\n\nexport const { lock, unlock } =\n\t__dangerousOptInToUnstableAPIsOnlyForCoreModules(\n\t\t'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.',\n\t\t'@wordpress/views'\n\t);\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,0BAAiE;AAE1D,IAAM,EAAE,MAAM,OAAO,QAC3B;AAAA,EACC;AAAA,EACA;AACD;",
4
+ "sourcesContent": ["import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';\n\nexport const { lock, unlock } =\n\t__dangerousOptInToUnstableAPIsOnlyForCoreModules(\n\t\t'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.',\n\t\t'@wordpress/views'\n\t);\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,0BAAiE;AAE1D,IAAM,EAAE,MAAM,OAAO,QAC3B;AAAA,EACC;AAAA,EACA;AACD;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // packages/views/src/resolve-view.ts
21
+ var resolve_view_exports = {};
22
+ __export(resolve_view_exports, {
23
+ getUserModifications: () => getUserModifications,
24
+ resolveView: () => resolveView
25
+ });
26
+ module.exports = __toCommonJS(resolve_view_exports);
27
+ var import_dequal = require("dequal");
28
+ var QUERY_PARAMS = ["page", "search"];
29
+ function isPlainObject(value) {
30
+ return typeof value === "object" && value !== null && !Array.isArray(value);
31
+ }
32
+ function withoutQueryParams(layer) {
33
+ const result = isPlainObject(layer) ? { ...layer } : {};
34
+ for (const key of QUERY_PARAMS) {
35
+ delete result[key];
36
+ }
37
+ return result;
38
+ }
39
+ function mergeLayer(lower, upper) {
40
+ const result = { ...lower };
41
+ for (const key of Object.keys(upper)) {
42
+ const value = upper[key];
43
+ const current = result[key];
44
+ result[key] = isPlainObject(current) && isPlainObject(value) ? mergeLayer(current, value) : value;
45
+ }
46
+ return result;
47
+ }
48
+ function diffLayer(value, base, persisted = {}) {
49
+ const result = {};
50
+ for (const key of Object.keys(value)) {
51
+ const next = value[key];
52
+ const current = base[key];
53
+ const prev = persisted[key];
54
+ if (next === void 0) {
55
+ continue;
56
+ }
57
+ if (isPlainObject(next) && isPlainObject(current)) {
58
+ const nested = diffLayer(
59
+ next,
60
+ current,
61
+ isPlainObject(prev) ? prev : {}
62
+ );
63
+ if (Object.keys(nested).length > 0) {
64
+ result[key] = nested;
65
+ }
66
+ } else if (!(0, import_dequal.dequal)(next, current)) {
67
+ result[key] = next;
68
+ } else if (prev !== void 0 && (0, import_dequal.dequal)(next, prev)) {
69
+ result[key] = prev;
70
+ }
71
+ }
72
+ return result;
73
+ }
74
+ function getLockedFilters(overrides) {
75
+ return (overrides?.filters ?? []).filter((filter) => filter.isLocked);
76
+ }
77
+ function applyLockedFilters(view, overrides) {
78
+ const locked = getLockedFilters(overrides);
79
+ if (locked.length === 0) {
80
+ return view;
81
+ }
82
+ const rest = (view.filters ?? []).filter(
83
+ (filter) => !locked.some((f) => f.field === filter.field)
84
+ );
85
+ return { ...view, filters: [...locked, ...rest] };
86
+ }
87
+ function resolveBaseView(layers, effectiveType) {
88
+ const { defaultView, defaultLayouts, activeViewOverrides } = layers;
89
+ const layoutDefaults = defaultLayouts?.[effectiveType];
90
+ return applyLockedFilters(
91
+ [layoutDefaults, activeViewOverrides].reduce(
92
+ (lower, upper) => mergeLayer(lower, withoutQueryParams(upper)),
93
+ withoutQueryParams(defaultView)
94
+ ),
95
+ activeViewOverrides
96
+ );
97
+ }
98
+ function resolveView(args) {
99
+ const { defaultView, activeViewOverrides, persistedView, page, search } = args;
100
+ const effectiveType = persistedView?.type ?? activeViewOverrides?.type ?? defaultView?.type;
101
+ const baseView = resolveBaseView(args, effectiveType);
102
+ const view = {
103
+ ...applyLockedFilters(
104
+ mergeLayer(baseView, withoutQueryParams(persistedView)),
105
+ activeViewOverrides
106
+ ),
107
+ page: Number(page ?? 1),
108
+ search: search ?? ""
109
+ };
110
+ return view;
111
+ }
112
+ function getUserModifications(newView, layers) {
113
+ const { activeViewOverrides, persistedView } = layers;
114
+ const baseView = resolveBaseView(layers, newView.type);
115
+ const modifications = diffLayer(
116
+ withoutQueryParams(newView),
117
+ withoutQueryParams(baseView),
118
+ withoutQueryParams(persistedView)
119
+ );
120
+ const locked = getLockedFilters(activeViewOverrides);
121
+ if (locked.length > 0 && Array.isArray(modifications.filters)) {
122
+ modifications.filters = modifications.filters.filter(
123
+ (filter) => !locked.some((f) => f.field === filter.field)
124
+ );
125
+ }
126
+ return Object.keys(modifications).length > 0 ? modifications : void 0;
127
+ }
128
+ // Annotate the CommonJS export names for ESM import in node:
129
+ 0 && (module.exports = {
130
+ getUserModifications,
131
+ resolveView
132
+ });
133
+ //# sourceMappingURL=resolve-view.cjs.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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAuB;AAYvB,IAAM,eAAe,CAAE,QAAQ,QAAS;AAExC,SAAS,cAAe,OAAu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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/types.ts"],
4
- "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport type { View, SupportedLayouts } from '@wordpress/dataviews';\n\nexport type ActiveViewOverrides = {\n\t// scalar values\n\ttitleField?: View[ 'titleField' ];\n\tshowTitle?: View[ 'showTitle' ];\n\tmediaField?: View[ 'mediaField' ];\n\tshowMedia?: View[ 'showMedia' ];\n\tdescriptionField?: View[ 'descriptionField' ];\n\tshowDescription?: View[ 'showDescription' ];\n\tshowLevels?: View[ 'showLevels' ];\n\tinfiniteScrollEnabled?: View[ 'infiniteScrollEnabled' ];\n\t// array & object values\n\tfilters?: View[ 'filters' ];\n\tsort?: View[ 'sort' ];\n\tgroupBy?: View[ 'groupBy' ];\n\tlayout?: Record< string, unknown >;\n};\n\nexport interface ViewConfig {\n\t/**\n\t * Entity kind (e.g. postType, root).\n\t * Similar to core-data entity kinds.\n\t */\n\tkind: string;\n\n\t/**\n\t * Entity name (e.g. post, page, user, site).\n\t * Similar to core-data entity names.\n\t */\n\tname: string;\n\n\t/**\n\t * Identifier for the current view (all, published, mine, etc.)\n\t */\n\tslug: string;\n\n\t/**\n\t * Default view configuration\n\t */\n\tdefaultView: View;\n\n\t/**\n\t * View overrides applied on top of the persisted view but never persisted.\n\t * These represent tab-specific configuration (filters, sort) and\n\t * developer-defined view defaults that should override the persisted\n\t * view settings.\n\t */\n\tactiveViewOverrides?: ActiveViewOverrides;\n\n\t/**\n\t * Default layout configurations keyed by layout type.\n\t * Used to apply layout-type-specific defaults (e.g., table column styles)\n\t * that are merged as overrides based on the resolved view type.\n\t */\n\tdefaultLayouts?: SupportedLayouts;\n\n\t/**\n\t * Optional query parameters from URL (page, search)\n\t */\n\tqueryParams?: {\n\t\tpage?: number;\n\t\tsearch?: string;\n\t};\n\n\t/**\n\t * Callback for when URL query parameters should change\n\t */\n\tonChangeQueryParams?: ( params: {\n\t\tpage?: number;\n\t\tsearch?: string;\n\t} ) => void;\n}\n"],
4
+ "sourcesContent": ["import type { View, SupportedLayouts } from '@wordpress/dataviews';\n\n/**\n * A layer merged on top of a view: the active view overrides, and the user's\n * persisted modifications.\n *\n * It is derived from `View` so it stays in sync with it, but every property is\n * optional — including `type`, which `View` requires as the discriminant of its\n * layout union. A layer only ever carries the subset of properties it means to\n * override, and `layout` is loosened to a plain record because a `type`\n * override may change which layout shape applies.\n */\nexport type ViewOverrides = Partial< Omit< View, 'type' | 'layout' > > & {\n\ttype?: View[ 'type' ];\n\tlayout?: Record< string, unknown >;\n};\n\nexport interface ViewConfig {\n\t/**\n\t * Entity kind (e.g. postType, root).\n\t * Similar to core-data entity kinds.\n\t */\n\tkind: string;\n\n\t/**\n\t * Entity name (e.g. post, page, user, site).\n\t * Similar to core-data entity names.\n\t */\n\tname: string;\n\n\t/**\n\t * Identifier for the current view (all, published, mine, etc.)\n\t */\n\tslug: string;\n\n\t/**\n\t * Default view configuration.\n\t */\n\tdefaultView: View;\n\n\t/**\n\t * View overrides applied on top of the default view and the default\n\t * layouts, but never persisted. These represent tab-specific configuration\n\t * (filters, sort) and developer-defined view defaults. The properties the\n\t * user modified take precedence over them; the ones they never touched\n\t * resolve out of them.\n\t */\n\tactiveViewOverrides?: ViewOverrides;\n\n\t/**\n\t * Default layout configurations keyed by layout type.\n\t * Used to apply layout-type-specific defaults (e.g., table column styles)\n\t * that are merged as overrides based on the resolved view type.\n\t */\n\tdefaultLayouts?: SupportedLayouts;\n\n\t/**\n\t * Optional query parameters from URL (page, search).\n\t */\n\tqueryParams?: {\n\t\tpage?: number;\n\t\tsearch?: string;\n\t};\n\n\t/**\n\t * Callback for when URL query parameters should change\n\t */\n\tonChangeQueryParams?: ( params: {\n\t\tpage?: number;\n\t\tsearch?: string;\n\t} ) => void;\n}\n"],
5
5
  "mappings": ";;;;;;;;;;;;;;;;AAAA;AAAA;",
6
6
  "names": []
7
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": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAA0B;AAC1B,uBAAmC;AAMnC,yBAAuB;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,aAAO;AAAA,IACN,CAAE,WAAY;AACb,iBAAO,2BAAQ,OAAQ,iBAAAA,KAAU,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;AAAA;AAAA;AAAA;AAAA;AAAA,kBAA0B;AAC1B,uBAAmC;AAEnC,yBAAuB;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,aAAO;AAAA,IACN,CAAE,WAAY;AACb,iBAAO,2BAAQ,OAAQ,iBAAAA,KAAU,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": ["coreStore"]
7
7
  }
@@ -28,20 +28,14 @@ var import_element = require("@wordpress/element");
28
28
  var import_data = require("@wordpress/data");
29
29
  var import_preferences = require("@wordpress/preferences");
30
30
  var import_preference_keys = require("./preference-keys.cjs");
31
- var import_filter_utils = require("./filter-utils.cjs");
32
- function omit(obj, keys) {
33
- const result = { ...obj };
34
- for (const key of keys) {
35
- delete result[key];
36
- }
37
- return result;
38
- }
31
+ var import_resolve_view = require("./resolve-view.cjs");
39
32
  function useView(config) {
40
33
  const {
41
34
  kind,
42
35
  name,
43
36
  slug,
44
37
  defaultView,
38
+ defaultLayouts,
45
39
  activeViewOverrides,
46
40
  queryParams,
47
41
  onChangeQueryParams
@@ -57,68 +51,54 @@ function useView(config) {
57
51
  [preferenceKey]
58
52
  );
59
53
  const { set } = (0, import_data.useDispatch)(import_preferences.store);
60
- const baseView = (0, import_element.useMemo)(
61
- () => persistedView ?? defaultView ?? {},
62
- [persistedView, defaultView]
54
+ const page = Number(queryParams?.page ?? 1);
55
+ const search = queryParams?.search ?? "";
56
+ const view = (0, import_element.useMemo)(
57
+ () => (0, import_resolve_view.resolveView)({
58
+ defaultView,
59
+ defaultLayouts,
60
+ activeViewOverrides,
61
+ persistedView,
62
+ page,
63
+ search
64
+ }),
65
+ [
66
+ defaultView,
67
+ defaultLayouts,
68
+ activeViewOverrides,
69
+ persistedView,
70
+ page,
71
+ search
72
+ ]
63
73
  );
64
- const page = Number(queryParams?.page ?? baseView.page ?? 1);
65
- const search = queryParams?.search ?? baseView.search ?? "";
66
- const combinedOverrides = (0, import_element.useMemo)(() => {
67
- const rawDefaults = config.defaultLayouts?.[baseView.type];
68
- const layoutTypeDefaults = !rawDefaults || rawDefaults === true ? {} : rawDefaults;
69
- return { ...layoutTypeDefaults, ...activeViewOverrides };
70
- }, [config.defaultLayouts, baseView.type, activeViewOverrides]);
71
- const view = (0, import_element.useMemo)(() => {
72
- return (0, import_filter_utils.mergeActiveViewOverrides)(
73
- {
74
- ...baseView,
75
- page,
76
- search
77
- },
78
- combinedOverrides,
79
- defaultView
80
- );
81
- }, [baseView, page, search, combinedOverrides, defaultView]);
82
- const isModified = !!persistedView;
74
+ const isModified = !!persistedView && Object.keys(persistedView).length > 0;
83
75
  const updateView = (0, import_element.useCallback)(
84
76
  (newView) => {
85
- const urlParams = {
86
- page: newView?.page,
87
- search: newView?.search
77
+ const newQueryParams = {
78
+ page: Number(newView?.page ?? 1),
79
+ search: newView?.search ?? ""
88
80
  };
89
- const preferenceView = (0, import_filter_utils.stripActiveViewOverrides)(
90
- omit(newView, ["page", "search"]),
91
- combinedOverrides,
92
- defaultView
93
- );
94
- if (onChangeQueryParams && !(0, import_dequal.dequal)(urlParams, { page, search })) {
95
- onChangeQueryParams(urlParams);
81
+ if (onChangeQueryParams && !(0, import_dequal.dequal)(newQueryParams, { page, search })) {
82
+ onChangeQueryParams(newQueryParams);
96
83
  }
97
- const comparableBaseView = (0, import_filter_utils.stripActiveViewOverrides)(
98
- baseView,
99
- combinedOverrides,
100
- defaultView
101
- );
102
- const comparableDefaultView = (0, import_filter_utils.stripActiveViewOverrides)(
84
+ const modifications = (0, import_resolve_view.getUserModifications)(newView, {
103
85
  defaultView,
104
- combinedOverrides,
105
- defaultView
106
- );
107
- if (!(0, import_dequal.dequal)(comparableBaseView, preferenceView)) {
108
- if ((0, import_dequal.dequal)(preferenceView, comparableDefaultView)) {
109
- set("core/views", preferenceKey, void 0);
110
- } else {
111
- set("core/views", preferenceKey, preferenceView);
112
- }
86
+ defaultLayouts,
87
+ activeViewOverrides,
88
+ persistedView
89
+ });
90
+ if (!(0, import_dequal.dequal)(modifications, persistedView)) {
91
+ set("core/views", preferenceKey, modifications);
113
92
  }
114
93
  },
115
94
  [
116
95
  onChangeQueryParams,
117
96
  page,
118
97
  search,
119
- baseView,
120
98
  defaultView,
121
- combinedOverrides,
99
+ defaultLayouts,
100
+ activeViewOverrides,
101
+ persistedView,
122
102
  set,
123
103
  preferenceKey
124
104
  ]
@@ -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, 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;AAAA;AAAA;AAAA;AAAA;AAGA,oBAAuB;AAKvB,qBAAqC;AACrC,kBAAuC;AAGvC,yBAA0C;AAK1C,6BAAsC;AACtC,0BAGO;AAUP,SAAS,KACR,KACA,MACe;AACf,QAAM,SAAS,EAAE,GAAG,IAAI;AACxB,aAAY,OAAO,MAAO;AACzB,WAAO,OAAQ,GAAI;AAAA,EACpB;AACA,SAAO;AACR;AASO,SAAS,QAAS,QAAoC;AAC5D,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD,IAAI;AAEJ,QAAM,oBAAgB,8CAAuB,MAAM,MAAM,IAAK;AAC9D,QAAM,oBAAkC;AAAA,IACvC,CAAE,WAAY;AACb,aAAO,OAAQ,mBAAAA,KAAiB,EAAE;AAAA,QACjC;AAAA,QACA;AAAA,MACD;AAAA,IACD;AAAA,IACA,CAAE,aAAc;AAAA,EACjB;AACA,QAAM,EAAE,IAAI,QAAI,yBAAa,mBAAAA,KAAiB;AAE9C,QAAM,eAAiB;AAAA,IACtB,MAAM,iBAAiB,eAAe,CAAC;AAAA,IACvC,CAAE,eAAe,WAAY;AAAA,EAC9B;AACA,QAAM,OAAO,OAAQ,aAAa,QAAQ,SAAS,QAAQ,CAAE;AAC7D,QAAM,SAAS,aAAa,UAAU,SAAS,UAAU;AAEzD,QAAM,wBAAoB,wBAAS,MAAM;AACxC,UAAM,cACL,OAAO,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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;",
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 { 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;AAAA;AAAA;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AACF,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
  }
@@ -2,29 +2,29 @@
2
2
  import { select } from "@wordpress/data";
3
3
  import { store as preferencesStore } from "@wordpress/preferences";
4
4
  import { generatePreferenceKey } from "./preference-keys.mjs";
5
- import { mergeActiveViewOverrides } from "./filter-utils.mjs";
5
+ import { resolveView } from "./resolve-view.mjs";
6
6
  async function loadView(config) {
7
- const { kind, name, slug, defaultView, activeViewOverrides, queryParams } = config;
7
+ const {
8
+ kind,
9
+ name,
10
+ slug,
11
+ defaultView,
12
+ defaultLayouts,
13
+ activeViewOverrides,
14
+ queryParams
15
+ } = config;
8
16
  const preferenceKey = generatePreferenceKey(kind, name, slug);
9
- const persistedView = select(preferencesStore).get(
10
- "core/views",
11
- preferenceKey
12
- );
13
- const baseView = persistedView ?? defaultView;
14
- const page = queryParams?.page ?? 1;
15
- const search = queryParams?.search ?? "";
16
- const rawDefaults = config.defaultLayouts?.[baseView?.type];
17
- const layoutTypeDefaults = !rawDefaults || rawDefaults === true ? {} : rawDefaults;
18
- const combinedOverrides = { ...layoutTypeDefaults, ...activeViewOverrides };
19
- return mergeActiveViewOverrides(
20
- {
21
- ...baseView,
22
- page,
23
- search
24
- },
25
- combinedOverrides,
26
- defaultView
27
- );
17
+ const persistedView = select(
18
+ preferencesStore
19
+ ).get("core/views", preferenceKey);
20
+ return resolveView({
21
+ defaultView,
22
+ defaultLayouts,
23
+ activeViewOverrides,
24
+ persistedView,
25
+ page: queryParams?.page,
26
+ search: queryParams?.search
27
+ });
28
28
  }
29
29
  export {
30
30
  loadView
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/load-view.ts"],
4
- "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { select } from '@wordpress/data';\n// @ts-ignore - Preferences package is not typed\nimport { store as preferencesStore } from '@wordpress/preferences';\nimport type { View } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport { generatePreferenceKey } from './preference-keys';\nimport { mergeActiveViewOverrides } from './filter-utils';\nimport type { ViewConfig } from './types';\n\n/**\n * Async function for loading view state in route loaders.\n *\n * @param config Configuration object for loading the view.\n * @param config.kind Entity kind (e.g., 'postType', 'taxonomy', 'root').\n * @param config.name Specific entity name.\n * @param config.slug View identifier.\n * @param config.defaultView Default view configuration.\n * @param config.activeViewOverrides View overrides applied on top but never persisted.\n * @param config.queryParams Object with `page` and/or `search` from URL.\n * @return Promise resolving to the loaded view object.\n */\nexport async function loadView( config: ViewConfig ) {\n\tconst { kind, name, slug, defaultView, activeViewOverrides, queryParams } =\n\t\tconfig;\n\tconst preferenceKey = generatePreferenceKey( kind, name, slug );\n\tconst persistedView: View | undefined = select( preferencesStore ).get(\n\t\t'core/views',\n\t\tpreferenceKey\n\t) as View | undefined;\n\n\tconst baseView = persistedView ?? defaultView;\n\tconst page = queryParams?.page ?? 1;\n\tconst search = queryParams?.search ?? '';\n\n\tconst rawDefaults =\n\t\tconfig.defaultLayouts?.[\n\t\t\tbaseView?.type as keyof typeof config.defaultLayouts\n\t\t];\n\tconst layoutTypeDefaults =\n\t\t! rawDefaults || rawDefaults === true ? {} : rawDefaults;\n\tconst combinedOverrides = { ...layoutTypeDefaults, ...activeViewOverrides };\n\n\treturn mergeActiveViewOverrides(\n\t\t{\n\t\t\t...baseView,\n\t\t\tpage,\n\t\t\tsearch,\n\t\t},\n\t\tcombinedOverrides,\n\t\tdefaultView\n\t);\n}\n"],
5
- "mappings": ";AAGA,SAAS,cAAc;AAEvB,SAAS,SAAS,wBAAwB;AAM1C,SAAS,6BAA6B;AACtC,SAAS,gCAAgC;AAezC,eAAsB,SAAU,QAAqB;AACpD,QAAM,EAAE,MAAM,MAAM,MAAM,aAAa,qBAAqB,YAAY,IACvE;AACD,QAAM,gBAAgB,sBAAuB,MAAM,MAAM,IAAK;AAC9D,QAAM,gBAAkC,OAAQ,gBAAiB,EAAE;AAAA,IAClE;AAAA,IACA;AAAA,EACD;AAEA,QAAM,WAAW,iBAAiB;AAClC,QAAM,OAAO,aAAa,QAAQ;AAClC,QAAM,SAAS,aAAa,UAAU;AAEtC,QAAM,cACL,OAAO,iBACN,UAAU,IACX;AACD,QAAM,qBACL,CAAE,eAAe,gBAAgB,OAAO,CAAC,IAAI;AAC9C,QAAM,oBAAoB,EAAE,GAAG,oBAAoB,GAAG,oBAAoB;AAE1E,SAAO;AAAA,IACN;AAAA,MACC,GAAG;AAAA,MACH;AAAA,MACA;AAAA,IACD;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;",
4
+ "sourcesContent": ["import { select } from '@wordpress/data';\nimport { store as preferencesStore } from '@wordpress/preferences';\nimport { generatePreferenceKey } from './preference-keys';\nimport { resolveView } from './resolve-view';\nimport type { ViewConfig, ViewOverrides } from './types';\n\n/**\n * Async function for loading view state in route loaders.\n *\n * Resolves the same layers `useView` does, so a route loader and the hook that\n * takes over from it agree on the view.\n *\n * @param config Configuration object for loading the view.\n * @param config.kind Entity kind (e.g., 'postType', 'taxonomy', 'root').\n * @param config.name Specific entity name.\n * @param config.slug View identifier.\n * @param config.defaultView Default view configuration.\n * @param config.defaultLayouts Default layout configurations keyed by layout type.\n * @param config.activeViewOverrides View overrides applied on top but never persisted.\n * @param config.queryParams Object with `page` and/or `search` from URL.\n * @return Promise resolving to the loaded view object.\n */\nexport async function loadView( config: ViewConfig ) {\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} = config;\n\tconst preferenceKey = generatePreferenceKey( kind, name, slug );\n\tconst persistedView: ViewOverrides | undefined = select(\n\t\tpreferencesStore\n\t).get( 'core/views', preferenceKey ) as ViewOverrides | undefined;\n\n\treturn resolveView( {\n\t\tdefaultView,\n\t\tdefaultLayouts,\n\t\tactiveViewOverrides,\n\t\tpersistedView,\n\t\tpage: queryParams?.page,\n\t\tsearch: queryParams?.search,\n\t} );\n}\n"],
5
+ "mappings": ";AAAA,SAAS,cAAc;AACvB,SAAS,SAAS,wBAAwB;AAC1C,SAAS,6BAA6B;AACtC,SAAS,mBAAmB;AAmB5B,eAAsB,SAAU,QAAqB;AACpD,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD,IAAI;AACJ,QAAM,gBAAgB,sBAAuB,MAAM,MAAM,IAAK;AAC9D,QAAM,gBAA2C;AAAA,IAChD;AAAA,EACD,EAAE,IAAK,cAAc,aAAc;AAEnC,SAAO,YAAa;AAAA,IACnB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,MAAM,aAAa;AAAA,IACnB,QAAQ,aAAa;AAAA,EACtB,CAAE;AACH;",
6
6
  "names": []
7
7
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/lock-unlock.ts"],
4
- "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';\n\nexport const { lock, unlock } =\n\t__dangerousOptInToUnstableAPIsOnlyForCoreModules(\n\t\t'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.',\n\t\t'@wordpress/views'\n\t);\n"],
5
- "mappings": ";AAGA,SAAS,wDAAwD;AAE1D,IAAM,EAAE,MAAM,OAAO,IAC3B;AAAA,EACC;AAAA,EACA;AACD;",
4
+ "sourcesContent": ["import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';\n\nexport const { lock, unlock } =\n\t__dangerousOptInToUnstableAPIsOnlyForCoreModules(\n\t\t'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.',\n\t\t'@wordpress/views'\n\t);\n"],
5
+ "mappings": ";AAAA,SAAS,wDAAwD;AAE1D,IAAM,EAAE,MAAM,OAAO,IAC3B;AAAA,EACC;AAAA,EACA;AACD;",
6
6
  "names": []
7
7
  }