@wordpress/views 1.19.0 → 1.21.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 +18 -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 +14 -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/src/types.ts CHANGED
@@ -1,22 +1,17 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
1
  import type { View, SupportedLayouts } from '@wordpress/dataviews';
5
2
 
6
- export type ActiveViewOverrides = {
7
- // scalar values
8
- titleField?: View[ 'titleField' ];
9
- showTitle?: View[ 'showTitle' ];
10
- mediaField?: View[ 'mediaField' ];
11
- showMedia?: View[ 'showMedia' ];
12
- descriptionField?: View[ 'descriptionField' ];
13
- showDescription?: View[ 'showDescription' ];
14
- showLevels?: View[ 'showLevels' ];
15
- infiniteScrollEnabled?: View[ 'infiniteScrollEnabled' ];
16
- // array & object values
17
- filters?: View[ 'filters' ];
18
- sort?: View[ 'sort' ];
19
- groupBy?: View[ 'groupBy' ];
3
+ /**
4
+ * A layer merged on top of a view: the active view overrides, and the user's
5
+ * persisted modifications.
6
+ *
7
+ * It is derived from `View` so it stays in sync with it, but every property is
8
+ * optional — including `type`, which `View` requires as the discriminant of its
9
+ * layout union. A layer only ever carries the subset of properties it means to
10
+ * override, and `layout` is loosened to a plain record because a `type`
11
+ * override may change which layout shape applies.
12
+ */
13
+ export type ViewOverrides = Partial< Omit< View, 'type' | 'layout' > > & {
14
+ type?: View[ 'type' ];
20
15
  layout?: Record< string, unknown >;
21
16
  };
22
17
 
@@ -39,17 +34,18 @@ export interface ViewConfig {
39
34
  slug: string;
40
35
 
41
36
  /**
42
- * Default view configuration
37
+ * Default view configuration.
43
38
  */
44
39
  defaultView: View;
45
40
 
46
41
  /**
47
- * View overrides applied on top of the persisted view but never persisted.
48
- * These represent tab-specific configuration (filters, sort) and
49
- * developer-defined view defaults that should override the persisted
50
- * view settings.
42
+ * View overrides applied on top of the default view and the default
43
+ * layouts, but never persisted. These represent tab-specific configuration
44
+ * (filters, sort) and developer-defined view defaults. The properties the
45
+ * user modified take precedence over them; the ones they never touched
46
+ * resolve out of them.
51
47
  */
52
- activeViewOverrides?: ActiveViewOverrides;
48
+ activeViewOverrides?: ViewOverrides;
53
49
 
54
50
  /**
55
51
  * Default layout configurations keyed by layout type.
@@ -59,7 +55,7 @@ export interface ViewConfig {
59
55
  defaultLayouts?: SupportedLayouts;
60
56
 
61
57
  /**
62
- * Optional query parameters from URL (page, search)
58
+ * Optional query parameters from URL (page, search).
63
59
  */
64
60
  queryParams?: {
65
61
  page?: number;
@@ -1,13 +1,6 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
1
  import { useSelect } from '@wordpress/data';
5
2
  import { store as coreStore } from '@wordpress/core-data';
6
3
  import type { View, SupportedLayouts, Form } from '@wordpress/dataviews';
7
-
8
- /**
9
- * Internal dependencies
10
- */
11
4
  import { unlock } from './lock-unlock';
12
5
 
13
6
  /**
package/src/use-view.ts CHANGED
@@ -1,26 +1,11 @@
1
- /**
2
- * External dependencies
3
- */
4
1
  import { dequal } from 'dequal';
5
-
6
- /**
7
- * WordPress dependencies
8
- */
9
2
  import { useCallback, useMemo } from '@wordpress/element';
10
3
  import { useDispatch, useSelect } from '@wordpress/data';
11
4
  import type { View } from '@wordpress/dataviews';
12
- // @ts-ignore - Preferences package is not typed
13
5
  import { store as preferencesStore } from '@wordpress/preferences';
14
-
15
- /**
16
- * Internal dependencies
17
- */
18
6
  import { generatePreferenceKey } from './preference-keys';
19
- import {
20
- mergeActiveViewOverrides,
21
- stripActiveViewOverrides,
22
- } from './filter-utils';
23
- import type { ViewConfig } from './types';
7
+ import { getUserModifications, resolveView } from './resolve-view';
8
+ import type { ViewConfig, ViewOverrides } from './types';
24
9
 
25
10
  interface UseViewReturn {
26
11
  view: View;
@@ -29,20 +14,14 @@ interface UseViewReturn {
29
14
  resetToDefault: () => void;
30
15
  }
31
16
 
32
- function omit< T extends object, K extends keyof T >(
33
- obj: T,
34
- keys: K[]
35
- ): Omit< T, K > {
36
- const result = { ...obj };
37
- for ( const key of keys ) {
38
- delete result[ key ];
39
- }
40
- return result;
41
- }
42
-
43
17
  /**
44
18
  * Hook for managing DataViews view state with local persistence.
45
19
  *
20
+ * Only the properties the user actually modified are persisted: every other
21
+ * property keeps resolving out of the layers below, so a change to the default
22
+ * view, to the layout defaults or to the active view overrides keeps showing
23
+ * through.
24
+ *
46
25
  * @param config Configuration object for loading the view.
47
26
  *
48
27
  * @return Object with current view, modification state, and update functions.
@@ -53,106 +32,85 @@ export function useView( config: ViewConfig ): UseViewReturn {
53
32
  name,
54
33
  slug,
55
34
  defaultView,
35
+ defaultLayouts,
56
36
  activeViewOverrides,
57
37
  queryParams,
58
38
  onChangeQueryParams,
59
39
  } = config;
60
40
 
61
41
  const preferenceKey = generatePreferenceKey( kind, name, slug );
62
- const persistedView: View | undefined = useSelect(
42
+ const persistedView: ViewOverrides | undefined = useSelect(
63
43
  ( select ) => {
64
44
  return select( preferencesStore ).get(
65
45
  'core/views',
66
46
  preferenceKey
67
- ) as View | undefined;
47
+ ) as ViewOverrides | undefined;
68
48
  },
69
49
  [ preferenceKey ]
70
50
  );
71
51
  const { set } = useDispatch( preferencesStore );
72
52
 
73
- const baseView: View = useMemo(
74
- () => persistedView ?? defaultView ?? {},
75
- [ persistedView, defaultView ]
76
- );
77
- const page = Number( queryParams?.page ?? baseView.page ?? 1 );
78
- const search = queryParams?.search ?? baseView.search ?? '';
79
-
80
- const combinedOverrides = useMemo( () => {
81
- const rawDefaults =
82
- config.defaultLayouts?.[
83
- baseView.type as keyof typeof config.defaultLayouts
84
- ];
85
- const layoutTypeDefaults =
86
- ! rawDefaults || rawDefaults === true ? {} : rawDefaults;
87
- return { ...layoutTypeDefaults, ...activeViewOverrides };
88
- }, [ config.defaultLayouts, baseView.type, activeViewOverrides ] );
53
+ const page = Number( queryParams?.page ?? 1 );
54
+ const search = queryParams?.search ?? '';
89
55
 
90
- // Merge URL query parameters (page, search) and activeViewOverrides into the view
91
- const view: View = useMemo( () => {
92
- return mergeActiveViewOverrides(
93
- {
94
- ...baseView,
56
+ const view = useMemo(
57
+ () =>
58
+ resolveView( {
59
+ defaultView,
60
+ defaultLayouts,
61
+ activeViewOverrides,
62
+ persistedView,
95
63
  page,
96
64
  search,
97
- },
98
- combinedOverrides,
99
- defaultView
100
- );
101
- }, [ baseView, page, search, combinedOverrides, defaultView ] );
65
+ } ),
66
+ [
67
+ defaultView,
68
+ defaultLayouts,
69
+ activeViewOverrides,
70
+ persistedView,
71
+ page,
72
+ search,
73
+ ]
74
+ );
102
75
 
103
- const isModified = !! persistedView;
76
+ const isModified =
77
+ !! persistedView && Object.keys( persistedView ).length > 0;
104
78
 
105
79
  const updateView = useCallback(
106
80
  ( newView: View ) => {
107
- // Extract URL params (page, search) from the new view
108
- const urlParams: { page?: number; search?: string } = {
109
- page: newView?.page,
110
- search: newView?.search,
81
+ // `page` and `search` live in the URL, not in the preference: they
82
+ // are reported back to the consumer instead of being persisted.
83
+ const newQueryParams = {
84
+ page: Number( newView?.page ?? 1 ),
85
+ search: newView?.search ?? '',
111
86
  };
112
- // Strip activeViewOverrides and URL params before persisting
113
- // Cast is safe: omitting page/search doesn't change the discriminant (type field)
114
- const preferenceView = stripActiveViewOverrides(
115
- omit( newView, [ 'page', 'search' ] ) as View,
116
- combinedOverrides,
117
- defaultView
118
- );
119
-
120
- // If we have URL handling enabled, separate URL state from preference state
121
87
  if (
122
88
  onChangeQueryParams &&
123
- ! dequal( urlParams, { page, search } )
89
+ ! dequal( newQueryParams, { page, search } )
124
90
  ) {
125
- onChangeQueryParams( urlParams );
91
+ onChangeQueryParams( newQueryParams );
126
92
  }
127
93
 
128
- // Compare with baseView and defaultView after stripping activeViewOverrides
129
- const comparableBaseView = stripActiveViewOverrides(
130
- baseView,
131
- combinedOverrides,
132
- defaultView
133
- );
134
- const comparableDefaultView = stripActiveViewOverrides(
94
+ const modifications = getUserModifications( newView, {
135
95
  defaultView,
136
- combinedOverrides,
137
- defaultView
138
- );
139
-
140
- // Only persist non-URL preferences if different from baseView
141
- if ( ! dequal( comparableBaseView, preferenceView ) ) {
142
- if ( dequal( preferenceView, comparableDefaultView ) ) {
143
- set( 'core/views', preferenceKey, undefined );
144
- } else {
145
- set( 'core/views', preferenceKey, preferenceView );
146
- }
96
+ defaultLayouts,
97
+ activeViewOverrides,
98
+ persistedView,
99
+ } );
100
+ if ( ! dequal( modifications, persistedView ) ) {
101
+ // `undefined` clears the preference: the user reverted every
102
+ // property they had modified.
103
+ set( 'core/views', preferenceKey, modifications );
147
104
  }
148
105
  },
149
106
  [
150
107
  onChangeQueryParams,
151
108
  page,
152
109
  search,
153
- baseView,
154
110
  defaultView,
155
- combinedOverrides,
111
+ defaultLayouts,
112
+ activeViewOverrides,
113
+ persistedView,
156
114
  set,
157
115
  preferenceKey,
158
116
  ]
@@ -1,134 +0,0 @@
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/filter-utils.ts
21
- var filter_utils_exports = {};
22
- __export(filter_utils_exports, {
23
- mergeActiveViewOverrides: () => mergeActiveViewOverrides,
24
- stripActiveViewOverrides: () => stripActiveViewOverrides
25
- });
26
- module.exports = __toCommonJS(filter_utils_exports);
27
- var SCALAR_VALUES = [
28
- "titleField",
29
- "mediaField",
30
- "descriptionField",
31
- "showTitle",
32
- "showMedia",
33
- "showDescription",
34
- "showLevels",
35
- "infiniteScrollEnabled"
36
- ];
37
- function mergeActiveViewOverrides(view, activeViewOverrides, defaultView) {
38
- if (!activeViewOverrides) {
39
- return view;
40
- }
41
- let result = view;
42
- for (const key of SCALAR_VALUES) {
43
- if (key in activeViewOverrides) {
44
- result = { ...result, [key]: activeViewOverrides[key] };
45
- }
46
- }
47
- if (activeViewOverrides.filters && activeViewOverrides.filters.length > 0) {
48
- const activeFields = new Set(
49
- activeViewOverrides.filters.map((f) => f.field)
50
- );
51
- const preserved = (view.filters ?? []).filter(
52
- (f) => !activeFields.has(f.field)
53
- );
54
- result = {
55
- ...result,
56
- filters: [...preserved, ...activeViewOverrides.filters]
57
- };
58
- }
59
- if (activeViewOverrides.sort) {
60
- const isDefaultSort = defaultView && view.sort?.field === defaultView.sort?.field && view.sort?.direction === defaultView.sort?.direction;
61
- if (isDefaultSort) {
62
- result = {
63
- ...result,
64
- sort: activeViewOverrides.sort
65
- };
66
- }
67
- }
68
- if (activeViewOverrides.layout) {
69
- result = {
70
- ...result,
71
- layout: {
72
- ...result.layout,
73
- ...activeViewOverrides.layout
74
- }
75
- };
76
- }
77
- if (activeViewOverrides.groupBy) {
78
- result = {
79
- ...result,
80
- groupBy: activeViewOverrides.groupBy
81
- };
82
- }
83
- return result;
84
- }
85
- function stripActiveViewOverrides(view, activeViewOverrides, defaultView) {
86
- if (!activeViewOverrides) {
87
- return view;
88
- }
89
- let result = view;
90
- for (const key of SCALAR_VALUES) {
91
- if (key in activeViewOverrides) {
92
- const { [key]: _, ...rest } = result;
93
- result = rest;
94
- }
95
- }
96
- if (activeViewOverrides.filters && activeViewOverrides.filters.length > 0) {
97
- const activeFields = new Set(
98
- activeViewOverrides.filters.map((f) => f.field)
99
- );
100
- result = {
101
- ...result,
102
- filters: (view.filters ?? []).filter(
103
- (f) => !activeFields.has(f.field)
104
- )
105
- };
106
- }
107
- if (activeViewOverrides.sort && view.sort?.field === activeViewOverrides.sort.field && view.sort?.direction === activeViewOverrides.sort.direction) {
108
- result = {
109
- ...result,
110
- sort: defaultView?.sort
111
- };
112
- }
113
- if (activeViewOverrides.layout && "layout" in result && result.layout) {
114
- const layout = { ...result.layout };
115
- for (const key of Object.keys(activeViewOverrides.layout)) {
116
- delete layout[key];
117
- }
118
- result = {
119
- ...result,
120
- layout: Object.keys(layout).length > 0 ? layout : void 0
121
- };
122
- }
123
- if (activeViewOverrides.groupBy && "groupBy" in result) {
124
- const { groupBy: _, ...rest } = result;
125
- result = rest;
126
- }
127
- return result;
128
- }
129
- // Annotate the CommonJS export names for ESM import in node:
130
- 0 && (module.exports = {
131
- mergeActiveViewOverrides,
132
- stripActiveViewOverrides
133
- });
134
- //# sourceMappingURL=filter-utils.cjs.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/filter-utils.ts"],
4
- "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport type { View, Filter } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport type { ActiveViewOverrides } from './types';\n\nconst SCALAR_VALUES = [\n\t'titleField',\n\t'mediaField',\n\t'descriptionField',\n\t'showTitle',\n\t'showMedia',\n\t'showDescription',\n\t'showLevels',\n\t'infiniteScrollEnabled',\n] as const;\n\n/**\n * Merges activeViewOverrides into a view.\n * Filters: Active filters take precedence; same-field filters are replaced.\n * Sort: Active sort is applied only if current sort matches the default.\n *\n * @param view The view to merge overrides into.\n * @param activeViewOverrides The tab-specific overrides to apply.\n * @param defaultView The default view configuration.\n * @return A new view with merged overrides, or the original view if no overrides.\n */\nexport function mergeActiveViewOverrides(\n\tview: View,\n\tactiveViewOverrides?: ActiveViewOverrides,\n\tdefaultView?: View\n): View {\n\tif ( ! activeViewOverrides ) {\n\t\treturn view;\n\t}\n\n\tlet result = view;\n\n\t// Merge scalar overrides — always win over persisted values\n\tfor ( const key of SCALAR_VALUES ) {\n\t\tif ( key in activeViewOverrides ) {\n\t\t\tresult = { ...result, [ key ]: activeViewOverrides[ key ] };\n\t\t}\n\t}\n\n\t// Merge filters\n\tif (\n\t\tactiveViewOverrides.filters &&\n\t\tactiveViewOverrides.filters.length > 0\n\t) {\n\t\tconst activeFields = new Set(\n\t\t\tactiveViewOverrides.filters.map( ( f ) => f.field )\n\t\t);\n\t\tconst preserved = ( view.filters ?? [] ).filter(\n\t\t\t( f: Filter ) => ! activeFields.has( f.field )\n\t\t);\n\t\tresult = {\n\t\t\t...result,\n\t\t\tfilters: [ ...preserved, ...activeViewOverrides.filters ],\n\t\t};\n\t}\n\n\t// Merge sort - only apply if the current sort matches the default\n\tif ( activeViewOverrides.sort ) {\n\t\tconst isDefaultSort =\n\t\t\tdefaultView &&\n\t\t\tview.sort?.field === defaultView.sort?.field &&\n\t\t\tview.sort?.direction === defaultView.sort?.direction;\n\n\t\tif ( isDefaultSort ) {\n\t\t\tresult = {\n\t\t\t\t...result,\n\t\t\t\tsort: activeViewOverrides.sort,\n\t\t\t};\n\t\t}\n\t}\n\n\t// Merge layout — shallow merge, override keys always win\n\tif ( activeViewOverrides.layout ) {\n\t\tresult = {\n\t\t\t...result,\n\t\t\tlayout: {\n\t\t\t\t...( result as any ).layout,\n\t\t\t\t...activeViewOverrides.layout,\n\t\t\t},\n\t\t} as View;\n\t}\n\n\t// Merge groupBy — full replacement, override always wins\n\tif ( activeViewOverrides.groupBy ) {\n\t\tresult = {\n\t\t\t...result,\n\t\t\tgroupBy: activeViewOverrides.groupBy,\n\t\t};\n\t}\n\n\treturn result;\n}\n\n/**\n * Strips overrides before persisting.\n * Filters: Removes filters on fields managed by activeViewOverrides.\n * Sort: If sort matches the override, restores the default sort.\n *\n * @param view The view to strip overrides from.\n * @param activeViewOverrides The tab-specific override definitions.\n * @param defaultView The default view configuration.\n * @return A new view with overrides stripped, or the original view if no overrides.\n */\nexport function stripActiveViewOverrides(\n\tview: View,\n\tactiveViewOverrides?: ActiveViewOverrides,\n\tdefaultView?: View\n): View {\n\tif ( ! activeViewOverrides ) {\n\t\treturn view;\n\t}\n\n\tlet result = view;\n\n\t// Strip scalar keys managed by overrides\n\tfor ( const key of SCALAR_VALUES ) {\n\t\tif ( key in activeViewOverrides ) {\n\t\t\tconst { [ key ]: _, ...rest } = result;\n\t\t\tresult = rest as View;\n\t\t}\n\t}\n\n\t// Strip managed filters\n\tif (\n\t\tactiveViewOverrides.filters &&\n\t\tactiveViewOverrides.filters.length > 0\n\t) {\n\t\tconst activeFields = new Set(\n\t\t\tactiveViewOverrides.filters.map( ( f ) => f.field )\n\t\t);\n\t\tresult = {\n\t\t\t...result,\n\t\t\tfilters: ( view.filters ?? [] ).filter(\n\t\t\t\t( f: Filter ) => ! activeFields.has( f.field )\n\t\t\t),\n\t\t};\n\t}\n\n\t// Strip sort if it matches the override (restore to default)\n\tif (\n\t\tactiveViewOverrides.sort &&\n\t\tview.sort?.field === activeViewOverrides.sort.field &&\n\t\tview.sort?.direction === activeViewOverrides.sort.direction\n\t) {\n\t\tresult = {\n\t\t\t...result,\n\t\t\tsort: defaultView?.sort,\n\t\t};\n\t}\n\n\t// Strip layout keys managed by overrides\n\tif ( activeViewOverrides.layout && 'layout' in result && result.layout ) {\n\t\tconst layout = { ...result.layout } as Record< string, unknown >;\n\t\tfor ( const key of Object.keys( activeViewOverrides.layout ) ) {\n\t\t\tdelete layout[ key ];\n\t\t}\n\t\tresult = {\n\t\t\t...result,\n\t\t\tlayout: Object.keys( layout ).length > 0 ? layout : undefined,\n\t\t} as View;\n\t}\n\n\t// Strip groupBy managed by overrides\n\tif ( activeViewOverrides.groupBy && 'groupBy' in result ) {\n\t\tconst { groupBy: _, ...rest } = result;\n\t\tresult = rest as View;\n\t}\n\n\treturn result;\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAUA,IAAM,gBAAgB;AAAA,EACrB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,E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;AAGb,aAAY,OAAO,eAAgB;AAClC,QAAK,OAAO,qBAAsB;AACjC,YAAM,EAAE,CAAE,GAAI,GAAG,GAAG,GAAG,KAAK,IAAI;AAChC,eAAS;AAAA,IACV;AAAA,EACD;AAGA,MACC,oBAAoB,WACpB,oBAAoB,QAAQ,SAAS,GACpC;AACD,UAAM,eAAe,IAAI;AAAA,MACxB,oBAAoB,QAAQ,IAAK,CAAE,MAAO,EAAE,KAAM;AAAA,IACnD;AACA,aAAS;AAAA,MACR,GAAG;AAAA,MACH,UAAW,KAAK,WAAW,CAAC,GAAI;AAAA,QAC/B,CAAE,MAAe,CAAE,aAAa,IAAK,EAAE,KAAM;AAAA,MAC9C;AAAA,IACD;AAAA,EACD;AAGA,MACC,oBAAoB,QACpB,KAAK,MAAM,UAAU,oBAAoB,KAAK,SAC9C,KAAK,MAAM,cAAc,oBAAoB,KAAK,WACjD;AACD,aAAS;AAAA,MACR,GAAG;AAAA,MACH,MAAM,aAAa;AAAA,IACpB;AAAA,EACD;AAGA,MAAK,oBAAoB,UAAU,YAAY,UAAU,OAAO,QAAS;AACxE,UAAM,SAAS,EAAE,GAAG,OAAO,OAAO;AAClC,eAAY,OAAO,OAAO,KAAM,oBAAoB,MAAO,GAAI;AAC9D,aAAO,OAAQ,GAAI;AAAA,IACpB;AACA,aAAS;AAAA,MACR,GAAG;AAAA,MACH,QAAQ,OAAO,KAAM,MAAO,EAAE,SAAS,IAAI,SAAS;AAAA,IACrD;AAAA,EACD;AAGA,MAAK,oBAAoB,WAAW,aAAa,QAAS;AACzD,UAAM,EAAE,SAAS,GAAG,GAAG,KAAK,IAAI;AAChC,aAAS;AAAA,EACV;AAEA,SAAO;AACR;",
6
- "names": []
7
- }
@@ -1,108 +0,0 @@
1
- // packages/views/src/filter-utils.ts
2
- var SCALAR_VALUES = [
3
- "titleField",
4
- "mediaField",
5
- "descriptionField",
6
- "showTitle",
7
- "showMedia",
8
- "showDescription",
9
- "showLevels",
10
- "infiniteScrollEnabled"
11
- ];
12
- function mergeActiveViewOverrides(view, activeViewOverrides, defaultView) {
13
- if (!activeViewOverrides) {
14
- return view;
15
- }
16
- let result = view;
17
- for (const key of SCALAR_VALUES) {
18
- if (key in activeViewOverrides) {
19
- result = { ...result, [key]: activeViewOverrides[key] };
20
- }
21
- }
22
- if (activeViewOverrides.filters && activeViewOverrides.filters.length > 0) {
23
- const activeFields = new Set(
24
- activeViewOverrides.filters.map((f) => f.field)
25
- );
26
- const preserved = (view.filters ?? []).filter(
27
- (f) => !activeFields.has(f.field)
28
- );
29
- result = {
30
- ...result,
31
- filters: [...preserved, ...activeViewOverrides.filters]
32
- };
33
- }
34
- if (activeViewOverrides.sort) {
35
- const isDefaultSort = defaultView && view.sort?.field === defaultView.sort?.field && view.sort?.direction === defaultView.sort?.direction;
36
- if (isDefaultSort) {
37
- result = {
38
- ...result,
39
- sort: activeViewOverrides.sort
40
- };
41
- }
42
- }
43
- if (activeViewOverrides.layout) {
44
- result = {
45
- ...result,
46
- layout: {
47
- ...result.layout,
48
- ...activeViewOverrides.layout
49
- }
50
- };
51
- }
52
- if (activeViewOverrides.groupBy) {
53
- result = {
54
- ...result,
55
- groupBy: activeViewOverrides.groupBy
56
- };
57
- }
58
- return result;
59
- }
60
- function stripActiveViewOverrides(view, activeViewOverrides, defaultView) {
61
- if (!activeViewOverrides) {
62
- return view;
63
- }
64
- let result = view;
65
- for (const key of SCALAR_VALUES) {
66
- if (key in activeViewOverrides) {
67
- const { [key]: _, ...rest } = result;
68
- result = rest;
69
- }
70
- }
71
- if (activeViewOverrides.filters && activeViewOverrides.filters.length > 0) {
72
- const activeFields = new Set(
73
- activeViewOverrides.filters.map((f) => f.field)
74
- );
75
- result = {
76
- ...result,
77
- filters: (view.filters ?? []).filter(
78
- (f) => !activeFields.has(f.field)
79
- )
80
- };
81
- }
82
- if (activeViewOverrides.sort && view.sort?.field === activeViewOverrides.sort.field && view.sort?.direction === activeViewOverrides.sort.direction) {
83
- result = {
84
- ...result,
85
- sort: defaultView?.sort
86
- };
87
- }
88
- if (activeViewOverrides.layout && "layout" in result && result.layout) {
89
- const layout = { ...result.layout };
90
- for (const key of Object.keys(activeViewOverrides.layout)) {
91
- delete layout[key];
92
- }
93
- result = {
94
- ...result,
95
- layout: Object.keys(layout).length > 0 ? layout : void 0
96
- };
97
- }
98
- if (activeViewOverrides.groupBy && "groupBy" in result) {
99
- const { groupBy: _, ...rest } = result;
100
- result = rest;
101
- }
102
- return result;
103
- }
104
- export {
105
- mergeActiveViewOverrides,
106
- stripActiveViewOverrides
107
- };
108
- //# sourceMappingURL=filter-utils.mjs.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/filter-utils.ts"],
4
- "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport type { View, Filter } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport type { ActiveViewOverrides } from './types';\n\nconst SCALAR_VALUES = [\n\t'titleField',\n\t'mediaField',\n\t'descriptionField',\n\t'showTitle',\n\t'showMedia',\n\t'showDescription',\n\t'showLevels',\n\t'infiniteScrollEnabled',\n] as const;\n\n/**\n * Merges activeViewOverrides into a view.\n * Filters: Active filters take precedence; same-field filters are replaced.\n * Sort: Active sort is applied only if current sort matches the default.\n *\n * @param view The view to merge overrides into.\n * @param activeViewOverrides The tab-specific overrides to apply.\n * @param defaultView The default view configuration.\n * @return A new view with merged overrides, or the original view if no overrides.\n */\nexport function mergeActiveViewOverrides(\n\tview: View,\n\tactiveViewOverrides?: ActiveViewOverrides,\n\tdefaultView?: View\n): View {\n\tif ( ! activeViewOverrides ) {\n\t\treturn view;\n\t}\n\n\tlet result = view;\n\n\t// Merge scalar overrides — always win over persisted values\n\tfor ( const key of SCALAR_VALUES ) {\n\t\tif ( key in activeViewOverrides ) {\n\t\t\tresult = { ...result, [ key ]: activeViewOverrides[ key ] };\n\t\t}\n\t}\n\n\t// Merge filters\n\tif (\n\t\tactiveViewOverrides.filters &&\n\t\tactiveViewOverrides.filters.length > 0\n\t) {\n\t\tconst activeFields = new Set(\n\t\t\tactiveViewOverrides.filters.map( ( f ) => f.field )\n\t\t);\n\t\tconst preserved = ( view.filters ?? [] ).filter(\n\t\t\t( f: Filter ) => ! activeFields.has( f.field )\n\t\t);\n\t\tresult = {\n\t\t\t...result,\n\t\t\tfilters: [ ...preserved, ...activeViewOverrides.filters ],\n\t\t};\n\t}\n\n\t// Merge sort - only apply if the current sort matches the default\n\tif ( activeViewOverrides.sort ) {\n\t\tconst isDefaultSort =\n\t\t\tdefaultView &&\n\t\t\tview.sort?.field === defaultView.sort?.field &&\n\t\t\tview.sort?.direction === defaultView.sort?.direction;\n\n\t\tif ( isDefaultSort ) {\n\t\t\tresult = {\n\t\t\t\t...result,\n\t\t\t\tsort: activeViewOverrides.sort,\n\t\t\t};\n\t\t}\n\t}\n\n\t// Merge layout — shallow merge, override keys always win\n\tif ( activeViewOverrides.layout ) {\n\t\tresult = {\n\t\t\t...result,\n\t\t\tlayout: {\n\t\t\t\t...( result as any ).layout,\n\t\t\t\t...activeViewOverrides.layout,\n\t\t\t},\n\t\t} as View;\n\t}\n\n\t// Merge groupBy — full replacement, override always wins\n\tif ( activeViewOverrides.groupBy ) {\n\t\tresult = {\n\t\t\t...result,\n\t\t\tgroupBy: activeViewOverrides.groupBy,\n\t\t};\n\t}\n\n\treturn result;\n}\n\n/**\n * Strips overrides before persisting.\n * Filters: Removes filters on fields managed by activeViewOverrides.\n * Sort: If sort matches the override, restores the default sort.\n *\n * @param view The view to strip overrides from.\n * @param activeViewOverrides The tab-specific override definitions.\n * @param defaultView The default view configuration.\n * @return A new view with overrides stripped, or the original view if no overrides.\n */\nexport function stripActiveViewOverrides(\n\tview: View,\n\tactiveViewOverrides?: ActiveViewOverrides,\n\tdefaultView?: View\n): View {\n\tif ( ! activeViewOverrides ) {\n\t\treturn view;\n\t}\n\n\tlet result = view;\n\n\t// Strip scalar keys managed by overrides\n\tfor ( const key of SCALAR_VALUES ) {\n\t\tif ( key in activeViewOverrides ) {\n\t\t\tconst { [ key ]: _, ...rest } = result;\n\t\t\tresult = rest as View;\n\t\t}\n\t}\n\n\t// Strip managed filters\n\tif (\n\t\tactiveViewOverrides.filters &&\n\t\tactiveViewOverrides.filters.length > 0\n\t) {\n\t\tconst activeFields = new Set(\n\t\t\tactiveViewOverrides.filters.map( ( f ) => f.field )\n\t\t);\n\t\tresult = {\n\t\t\t...result,\n\t\t\tfilters: ( view.filters ?? [] ).filter(\n\t\t\t\t( f: Filter ) => ! activeFields.has( f.field )\n\t\t\t),\n\t\t};\n\t}\n\n\t// Strip sort if it matches the override (restore to default)\n\tif (\n\t\tactiveViewOverrides.sort &&\n\t\tview.sort?.field === activeViewOverrides.sort.field &&\n\t\tview.sort?.direction === activeViewOverrides.sort.direction\n\t) {\n\t\tresult = {\n\t\t\t...result,\n\t\t\tsort: defaultView?.sort,\n\t\t};\n\t}\n\n\t// Strip layout keys managed by overrides\n\tif ( activeViewOverrides.layout && 'layout' in result && result.layout ) {\n\t\tconst layout = { ...result.layout } as Record< string, unknown >;\n\t\tfor ( const key of Object.keys( activeViewOverrides.layout ) ) {\n\t\t\tdelete layout[ key ];\n\t\t}\n\t\tresult = {\n\t\t\t...result,\n\t\t\tlayout: Object.keys( layout ).length > 0 ? layout : undefined,\n\t\t} as View;\n\t}\n\n\t// Strip groupBy managed by overrides\n\tif ( activeViewOverrides.groupBy && 'groupBy' in result ) {\n\t\tconst { groupBy: _, ...rest } = result;\n\t\tresult = rest as View;\n\t}\n\n\treturn result;\n}\n"],
5
- "mappings": ";AAUA,IAAM,gBAAgB;AAAA,EACrB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD;AAYO,SAAS,yBACf,MACA,qBACA,aACO;AACP,MAAK,CAAE,qBAAsB;AAC5B,WAAO;AAAA,EACR;AAEA,MAAI,SAAS;AAGb,aAAY,OAAO,eAAgB;AAClC,QAAK,OAAO,qBAAsB;AACj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AoB,QAAQ,SAAS,GACpC;AACD,UAAM,eAAe,IAAI;AAAA,MACxB,oBAAoB,QAAQ,IAAK,CAAE,MAAO,EAAE,KAAM;AAAA,IACnD;AACA,aAAS;AAAA,MACR,GAAG;AAAA,MACH,UAAW,KAAK,WAAW,CAAC,GAAI;AAAA,QAC/B,CAAE,MAAe,CAAE,aAAa,IAAK,EAAE,KAAM;AAAA,MAC9C;AAAA,IACD;AAAA,EACD;AAGA,MACC,oBAAoB,QACpB,KAAK,MAAM,UAAU,oBAAoB,KAAK,SAC9C,KAAK,MAAM,cAAc,oBAAoB,KAAK,WACjD;AACD,aAAS;AAAA,MACR,GAAG;AAAA,MACH,MAAM,aAAa;AAAA,IACpB;AAAA,EACD;AAGA,MAAK,oBAAoB,UAAU,YAAY,UAAU,OAAO,QAAS;AACxE,UAAM,SAAS,EAAE,GAAG,OAAO,OAAO;AAClC,eAAY,OAAO,OAAO,KAAM,oBAAoB,MAAO,GAAI;AAC9D,aAAO,OAAQ,GAAI;AAAA,IACpB;AACA,aAAS;AAAA,MACR,GAAG;AAAA,MACH,QAAQ,OAAO,KAAM,MAAO,EAAE,SAAS,IAAI,SAAS;AAAA,IACrD;AAAA,EACD;AAGA,MAAK,oBAAoB,WAAW,aAAa,QAAS;AACzD,UAAM,EAAE,SAAS,GAAG,GAAG,KAAK,IAAI;AAChC,aAAS;AAAA,EACV;AAEA,SAAO;AACR;",
6
- "names": []
7
- }
@@ -1,31 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import type { View } from '@wordpress/dataviews';
5
- /**
6
- * Internal dependencies
7
- */
8
- import type { ActiveViewOverrides } from './types';
9
- /**
10
- * Merges activeViewOverrides into a view.
11
- * Filters: Active filters take precedence; same-field filters are replaced.
12
- * Sort: Active sort is applied only if current sort matches the default.
13
- *
14
- * @param view The view to merge overrides into.
15
- * @param activeViewOverrides The tab-specific overrides to apply.
16
- * @param defaultView The default view configuration.
17
- * @return A new view with merged overrides, or the original view if no overrides.
18
- */
19
- export declare function mergeActiveViewOverrides(view: View, activeViewOverrides?: ActiveViewOverrides, defaultView?: View): View;
20
- /**
21
- * Strips overrides before persisting.
22
- * Filters: Removes filters on fields managed by activeViewOverrides.
23
- * Sort: If sort matches the override, restores the default sort.
24
- *
25
- * @param view The view to strip overrides from.
26
- * @param activeViewOverrides The tab-specific override definitions.
27
- * @param defaultView The default view configuration.
28
- * @return A new view with overrides stripped, or the original view if no overrides.
29
- */
30
- export declare function stripActiveViewOverrides(view: View, activeViewOverrides?: ActiveViewOverrides, defaultView?: View): View;
31
- //# sourceMappingURL=filter-utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"filter-utils.d.ts","sourceRoot":"","sources":["../src/filter-utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,IAAI,EAAU,MAAM,sBAAsB,CAAC;AAEzD;;GAEG;AACH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAanD;;;;;;;;;GASG;AACH,wBAAgB,wBAAwB,CACvC,IAAI,EAAE,IAAI,EACV,mBAAmB,CAAC,EAAE,mBAAmB,EACzC,WAAW,CAAC,EAAE,IAAI,GAChB,IAAI,CAkEN;AAED;;;;;;;;;GASG;AACH,wBAAgB,wBAAwB,CACvC,IAAI,EAAE,IAAI,EACV,mBAAmB,CAAC,EAAE,mBAAmB,EACzC,WAAW,CAAC,EAAE,IAAI,GAChB,IAAI,CA8DN"}