@wordpress/views 3.0.0 → 3.2.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.
@@ -1,7 +1 @@
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 * The user's persisted modifications that still apply.\n *\n * A persisted `type` that `defaultLayouts` does not offer is dropped: DataViews\n * renders nothing for a layout it is not given, and the preference may predate\n * the current set of layouts (a screen that used to offer the list layout, say,\n * and no longer does). The type then resolves out of the layers below, and the\n * other modifications are kept. Without `defaultLayouts` every type is offered.\n *\n * `true` is a valid entry, so the check mirrors DataViews': an entry counts as\n * long as it is truthy.\n *\n * @param persistedView The user's persisted modifications.\n * @param defaultLayouts The layouts on offer, keyed by layout type.\n * @return The modifications that apply, or `undefined` when there are none.\n */\nexport function getApplicablePersistedView(\n\tpersistedView: ViewOverrides | undefined,\n\tdefaultLayouts: SupportedLayouts | undefined\n): ViewOverrides | undefined {\n\tif (\n\t\t! persistedView ||\n\t\tpersistedView.type === undefined ||\n\t\t! defaultLayouts ||\n\t\tdefaultLayouts[ persistedView.type as keyof SupportedLayouts ]\n\t) {\n\t\treturn persistedView;\n\t}\n\tconst { type, ...rest } = persistedView;\n\treturn Object.keys( rest ).length > 0 ? rest : undefined;\n}\n\n/**\n * Resolves the layers below the user's modifications — what a modification has\n * to differ from to count as one. The layout type selects which entry of\n * `defaultLayouts` applies, so the base only makes sense relative to a type.\n *\n * @param layers The layers to resolve.\n * @param effectiveType The layout type to resolve the base at.\n * @return The resolved base view.\n */\nfunction resolveBaseView(\n\tlayers: ViewLayers,\n\teffectiveType: View[ 'type' ] | undefined\n): PlainObject {\n\tconst { defaultView, defaultLayouts, activeViewOverrides } = layers;\n\tconst layoutDefaults =\n\t\tdefaultLayouts?.[ effectiveType as keyof SupportedLayouts ];\n\treturn applyLockedFilters(\n\t\t[ layoutDefaults, activeViewOverrides ].reduce< PlainObject >(\n\t\t\t( lower, upper ) =>\n\t\t\t\tmergeLayer( lower, withoutQueryParams( upper ) ),\n\t\t\twithoutQueryParams( defaultView )\n\t\t),\n\t\tactiveViewOverrides\n\t);\n}\n\n/**\n * Resolves a view out of its layers. Later layers override earlier ones:\n *\n * 1. `defaultView`\n * 2. `defaultLayouts` (for the effective type)\n * 3. `activeViewOverrides`\n * 4. the user's persisted modifications (see `getApplicablePersistedView`)\n * 5. the URL query params (`page` and `search` only)\n *\n * @param args See `ResolveViewArgs`.\n * @return The resolved `view`.\n */\nexport function resolveView( args: ResolveViewArgs ): View {\n\tconst { defaultView, defaultLayouts, activeViewOverrides, page, search } =\n\t\targs;\n\tconst persistedView = getApplicablePersistedView(\n\t\targs.persistedView,\n\t\tdefaultLayouts\n\t);\n\n\t// Resolve the effective layout type first: it selects which entry of\n\t// `defaultLayouts` applies, and the layers above the layouts may change it.\n\tconst effectiveType =\n\t\tpersistedView?.type ?? activeViewOverrides?.type ?? defaultView?.type;\n\tconst baseView = resolveBaseView( args, effectiveType );\n\n\tconst view = {\n\t\t...applyLockedFilters(\n\t\t\tmergeLayer( baseView, withoutQueryParams( persistedView ) ),\n\t\t\tactiveViewOverrides\n\t\t),\n\t\tpage: Number( page ?? 1 ),\n\t\tsearch: search ?? '',\n\t};\n\n\t// The layers are resolved as plain objects: `View` requires a `type`, which\n\t// only the layers taken together are guaranteed to provide.\n\treturn view as unknown as View;\n}\n\n/**\n * The user's modifications: the leaves of the view they interacted with that\n * the layers below do not already resolve to. This is what gets persisted, so\n * that a change to any of those layers keeps showing through the properties the\n * user never touched.\n *\n * Previously persisted leaves that coincide with the base of the current view\n * are carried over rather than dropped: they may still make a difference in\n * another view sharing the preference. See `diffLayer`.\n *\n * The diff basis is resolved at `newView.type`, not at the type the previous\n * resolution used: the type selects the `defaultLayouts` entry, and a layout\n * switch reports the new type together with that entry's values. Diffing them\n * against the previous type's base would persist the difference between the\n * two entries as if the user had picked those values — leaves the layouts\n * already provide, which then linger as phantom modifications after switching\n * back.\n *\n * A persisted `type` the current layouts do not offer never applied (see\n * `getApplicablePersistedView`), so it is not carried over either: the\n * modifications returned replace it with whatever the user picked.\n *\n * @param newView The view the user produced.\n * @param layers The layers the view resolves from. See `ViewLayers`.\n * @return The modified properties, or `undefined` when the user modified none.\n */\nexport function getUserModifications(\n\tnewView: View,\n\tlayers: ViewLayers\n): ViewOverrides | undefined {\n\tconst { defaultLayouts, activeViewOverrides } = layers;\n\tconst persistedView = getApplicablePersistedView(\n\t\tlayers.persistedView,\n\t\tdefaultLayouts\n\t);\n\tconst baseView = resolveBaseView( layers, newView.type );\n\n\tconst modifications = diffLayer(\n\t\twithoutQueryParams( newView ),\n\t\twithoutQueryParams( baseView ),\n\t\twithoutQueryParams( persistedView )\n\t);\n\n\t// Locked filters are pinned on every read, so persisting them would only\n\t// leak them into the views that do not lock them.\n\tconst locked = getLockedFilters( activeViewOverrides );\n\tif ( locked.length > 0 && Array.isArray( modifications.filters ) ) {\n\t\tmodifications.filters = ( modifications.filters as Filter[] ).filter(\n\t\t\t( filter ) => ! locked.some( ( f ) => f.field === filter.field )\n\t\t);\n\t}\n\n\treturn Object.keys( modifications ).length > 0\n\t\t? ( modifications as ViewOverrides )\n\t\t: undefined;\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAuB;AAYvB,IAAM,eAAe,CAAE,QAAQ,QAAS;AAExC,SAAS,cAAe,O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AAoB,aAAc,CAAE;AAAA,MAC1D;AAAA,IACD;AAAA,IACA,MAAM,OAAQ,QAAQ,CAAE;AAAA,IACxB,QAAQ,UAAU;AAAA,EACnB;AAIA,SAAO;AACR;AA4BO,SAAS,qBACf,SACA,QAC4B;AAC5B,QAAM,EAAE,gBAAgB,oBAAoB,IAAI;AAChD,QAAM,gBAAgB;AAAA,IACrB,OAAO;AAAA,IACP;AAAA,EACD;AACA,QAAM,WAAW,gBAAiB,QAAQ,QAAQ,IAAK;AAEvD,QAAM,gBAAgB;AAAA,IACrB,mBAAoB,OAAQ;AAAA,IAC5B,mBAAoB,QAAS;AAAA,IAC7B,mBAAoB,aAAc;AAAA,EACnC;AAIA,QAAM,SAAS,iBAAkB,mBAAoB;AACrD,MAAK,OAAO,SAAS,KAAK,MAAM,QAAS,cAAc,OAAQ,GAAI;AAClE,kBAAc,UAAY,cAAc,QAAsB;AAAA,MAC7D,CAAE,WAAY,CAAE,OAAO,KAAM,CAAE,MAAO,EAAE,UAAU,OAAO,KAAM;AAAA,IAChE;AAAA,EACD;AAEA,SAAO,OAAO,KAAM,aAAc,EAAE,SAAS,IACxC,gBACF;AACJ;",
6
- "names": []
7
- }
1
+ {"version":3,"file":"resolve-view.cjs","names":[],"sources":["../src/resolve-view.ts"],"sourcesContent":["import { dequal } from 'dequal';\nimport type { View, Filter, SupportedLayouts } from '@wordpress/dataviews';\nimport type { ViewOverrides } from './types';\n\ntype PlainObject = Record< string, unknown >;\n\n/**\n * `page` and `search` are URL-managed: the URL is their only source. They are\n * never persisted, and no other layer may configure them — an absent URL param\n * is indistinguishable from the user having cleared the value, so any fallback\n * would resurrect a cleared search on the next read.\n */\nconst QUERY_PARAMS = [ 'page', 'search' ] as const;\n\nfunction isPlainObject( value: unknown ): value is PlainObject {\n\treturn (\n\t\ttypeof value === 'object' && value !== null && ! Array.isArray( value )\n\t);\n}\n\n/**\n * Copies a layer without the URL-managed properties.\n *\n * @param layer The layer to copy.\n * @return A copy of the layer with `page` and `search` removed.\n */\nfunction withoutQueryParams( layer: unknown ): PlainObject {\n\tconst result: PlainObject = isPlainObject( layer ) ? { ...layer } : {};\n\tfor ( const key of QUERY_PARAMS ) {\n\t\tdelete result[ key ];\n\t}\n\treturn result;\n}\n\n/**\n * Merges a layer on top of another, recursing into plain objects so the upper\n * layer only takes over the leaves it declares. Arrays and scalars are leaves:\n * they are replaced wholesale.\n *\n * Nested properties (`layout.styles.<field>.width`, `groupBy.direction`,\n * `sort.direction`) are merged leaf by leaf: a shallow merge would let a layer\n * that only declares one leaf wipe every sibling the layers below provide.\n *\n * @param lower The layer to merge into.\n * @param upper The layer to merge on top.\n * @return The merged layer.\n */\nfunction mergeLayer( lower: PlainObject, upper: PlainObject ): PlainObject {\n\tconst result: PlainObject = { ...lower };\n\tfor ( const key of Object.keys( upper ) ) {\n\t\tconst value = upper[ key ];\n\t\tconst current = result[ key ];\n\t\tresult[ key ] =\n\t\t\tisPlainObject( current ) && isPlainObject( value )\n\t\t\t\t? mergeLayer( current, value )\n\t\t\t\t: value;\n\t}\n\treturn result;\n}\n\n/**\n * The leaves of `value` that differ from `base`. Mirrors `mergeLayer`:\n * `mergeLayer( base, diffLayer( value, base ) )` resolves back to `value`.\n *\n * A persisted leaf that equals both `value` and `base` is kept even though it\n * is not a difference: the preference is shared by every view of the same\n * entity, and such a leaf may only differ from the base of *another* view. In\n * this one, where it coincides with the base, a revert is unobservable — so\n * equality cannot mean the user reverted it. Dropping a persisted leaf\n * requires `value` to actually move away from it, back to `base`.\n *\n * @param value The layer to compare.\n * @param base The layer to compare it against.\n * @param persisted The previously persisted leaves.\n * @return The leaves that differ, as a layer of its own.\n */\nfunction diffLayer(\n\tvalue: PlainObject,\n\tbase: PlainObject,\n\tpersisted: PlainObject = {}\n): PlainObject {\n\tconst result: PlainObject = {};\n\tfor ( const key of Object.keys( value ) ) {\n\t\tconst next = value[ key ];\n\t\tconst current = base[ key ];\n\t\tconst prev = persisted[ key ];\n\t\tif ( next === undefined ) {\n\t\t\tcontinue;\n\t\t}\n\t\tif ( isPlainObject( next ) && isPlainObject( current ) ) {\n\t\t\tconst nested = diffLayer(\n\t\t\t\tnext,\n\t\t\t\tcurrent,\n\t\t\t\tisPlainObject( prev ) ? prev : {}\n\t\t\t);\n\t\t\tif ( Object.keys( nested ).length > 0 ) {\n\t\t\t\tresult[ key ] = nested;\n\t\t\t}\n\t\t} else if ( ! dequal( next, current ) ) {\n\t\t\tresult[ key ] = next;\n\t\t} else if ( prev !== undefined && dequal( next, prev ) ) {\n\t\t\tresult[ key ] = prev;\n\t\t}\n\t}\n\treturn result;\n}\n\nfunction getLockedFilters( overrides?: ViewOverrides ): Filter[] {\n\treturn ( overrides?.filters ?? [] ).filter( ( filter ) => filter.isLocked );\n}\n\n/**\n * Hoists the locked filters an override provides to the front of the view's\n * filters, replacing any same-field filter. Locked filters are not the user's\n * to change, so they are pinned in place instead of taking part in the layer\n * resolution.\n *\n * @param view The view to apply the locked filters to.\n * @param overrides The active view overrides.\n * @return The view with the locked filters applied.\n */\nfunction applyLockedFilters(\n\tview: PlainObject,\n\toverrides?: ViewOverrides\n): PlainObject {\n\tconst locked = getLockedFilters( overrides );\n\tif ( locked.length === 0 ) {\n\t\treturn view;\n\t}\n\tconst rest = ( ( view.filters ?? [] ) as Filter[] ).filter(\n\t\t( filter ) => ! locked.some( ( f ) => f.field === filter.field )\n\t);\n\treturn { ...view, filters: [ ...locked, ...rest ] };\n}\n\ninterface ViewLayers {\n\tdefaultView?: View;\n\tdefaultLayouts?: SupportedLayouts;\n\tactiveViewOverrides?: ViewOverrides;\n\t/**\n\t * The user's persisted modifications: a partial view holding only the\n\t * properties they actually changed.\n\t */\n\tpersistedView?: ViewOverrides;\n}\n\ninterface ResolveViewArgs extends ViewLayers {\n\tpage?: number;\n\tsearch?: string;\n}\n\n/**\n * The user's persisted modifications that still apply.\n *\n * A persisted `type` that `defaultLayouts` does not offer is dropped: DataViews\n * renders nothing for a layout it is not given, and the preference may predate\n * the current set of layouts (a screen that used to offer the list layout, say,\n * and no longer does). The type then resolves out of the layers below, and the\n * other modifications are kept. Without `defaultLayouts` every type is offered.\n *\n * `true` is a valid entry, so the check mirrors DataViews': an entry counts as\n * long as it is truthy.\n *\n * @param persistedView The user's persisted modifications.\n * @param defaultLayouts The layouts on offer, keyed by layout type.\n * @return The modifications that apply, or `undefined` when there are none.\n */\nexport function getApplicablePersistedView(\n\tpersistedView: ViewOverrides | undefined,\n\tdefaultLayouts: SupportedLayouts | undefined\n): ViewOverrides | undefined {\n\tif (\n\t\t! persistedView ||\n\t\tpersistedView.type === undefined ||\n\t\t! defaultLayouts ||\n\t\tdefaultLayouts[ persistedView.type as keyof SupportedLayouts ]\n\t) {\n\t\treturn persistedView;\n\t}\n\tconst { type, ...rest } = persistedView;\n\treturn Object.keys( rest ).length > 0 ? rest : undefined;\n}\n\n/**\n * Resolves the layers below the user's modifications — what a modification has\n * to differ from to count as one. The layout type selects which entry of\n * `defaultLayouts` applies, so the base only makes sense relative to a type.\n *\n * @param layers The layers to resolve.\n * @param effectiveType The layout type to resolve the base at.\n * @return The resolved base view.\n */\nfunction resolveBaseView(\n\tlayers: ViewLayers,\n\teffectiveType: View[ 'type' ] | undefined\n): PlainObject {\n\tconst { defaultView, defaultLayouts, activeViewOverrides } = layers;\n\tconst layoutDefaults =\n\t\tdefaultLayouts?.[ effectiveType as keyof SupportedLayouts ];\n\treturn applyLockedFilters(\n\t\t[ layoutDefaults, activeViewOverrides ].reduce< PlainObject >(\n\t\t\t( lower, upper ) =>\n\t\t\t\tmergeLayer( lower, withoutQueryParams( upper ) ),\n\t\t\twithoutQueryParams( defaultView )\n\t\t),\n\t\tactiveViewOverrides\n\t);\n}\n\n/**\n * Resolves a view out of its layers. Later layers override earlier ones:\n *\n * 1. `defaultView`\n * 2. `defaultLayouts` (for the effective type)\n * 3. `activeViewOverrides`\n * 4. the user's persisted modifications (see `getApplicablePersistedView`)\n * 5. the URL query params (`page` and `search` only)\n *\n * @param args See `ResolveViewArgs`.\n * @return The resolved `view`.\n */\nexport function resolveView( args: ResolveViewArgs ): View {\n\tconst { defaultView, defaultLayouts, activeViewOverrides, page, search } =\n\t\targs;\n\tconst persistedView = getApplicablePersistedView(\n\t\targs.persistedView,\n\t\tdefaultLayouts\n\t);\n\n\t// Resolve the effective layout type first: it selects which entry of\n\t// `defaultLayouts` applies, and the layers above the layouts may change it.\n\tconst effectiveType =\n\t\tpersistedView?.type ?? activeViewOverrides?.type ?? defaultView?.type;\n\tconst baseView = resolveBaseView( args, effectiveType );\n\n\tconst view = {\n\t\t...applyLockedFilters(\n\t\t\tmergeLayer( baseView, withoutQueryParams( persistedView ) ),\n\t\t\tactiveViewOverrides\n\t\t),\n\t\tpage: Number( page ?? 1 ),\n\t\tsearch: search ?? '',\n\t};\n\n\t// The layers are resolved as plain objects: `View` requires a `type`, which\n\t// only the layers taken together are guaranteed to provide.\n\treturn view as unknown as View;\n}\n\n/**\n * The user's modifications: the leaves of the view they interacted with that\n * the layers below do not already resolve to. This is what gets persisted, so\n * that a change to any of those layers keeps showing through the properties the\n * user never touched.\n *\n * Previously persisted leaves that coincide with the base of the current view\n * are carried over rather than dropped: they may still make a difference in\n * another view sharing the preference. See `diffLayer`.\n *\n * The diff basis is resolved at `newView.type`, not at the type the previous\n * resolution used: the type selects the `defaultLayouts` entry, and a layout\n * switch reports the new type together with that entry's values. Diffing them\n * against the previous type's base would persist the difference between the\n * two entries as if the user had picked those values — leaves the layouts\n * already provide, which then linger as phantom modifications after switching\n * back.\n *\n * A persisted `type` the current layouts do not offer never applied (see\n * `getApplicablePersistedView`), so it is not carried over either: the\n * modifications returned replace it with whatever the user picked.\n *\n * @param newView The view the user produced.\n * @param layers The layers the view resolves from. See `ViewLayers`.\n * @return The modified properties, or `undefined` when the user modified none.\n */\nexport function getUserModifications(\n\tnewView: View,\n\tlayers: ViewLayers\n): ViewOverrides | undefined {\n\tconst { defaultLayouts, activeViewOverrides } = layers;\n\tconst persistedView = getApplicablePersistedView(\n\t\tlayers.persistedView,\n\t\tdefaultLayouts\n\t);\n\tconst baseView = resolveBaseView( layers, newView.type );\n\n\tconst modifications = diffLayer(\n\t\twithoutQueryParams( newView ),\n\t\twithoutQueryParams( baseView ),\n\t\twithoutQueryParams( persistedView )\n\t);\n\n\t// Locked filters are pinned on every read, so persisting them would only\n\t// leak them into the views that do not lock them.\n\tconst locked = getLockedFilters( activeViewOverrides );\n\tif ( locked.length > 0 && Array.isArray( modifications.filters ) ) {\n\t\tmodifications.filters = ( modifications.filters as Filter[] ).filter(\n\t\t\t( filter ) => ! locked.some( ( f ) => f.field === filter.field )\n\t\t);\n\t}\n\n\treturn Object.keys( modifications ).length > 0\n\t\t? ( modifications as ViewOverrides )\n\t\t: undefined;\n}\n"],"mappings":";;;;;;;;;;;;;AAYA,MAAM,eAAe,CAAE,QAAQ,QAAS;AAExC,SAAS,cAAe,OAAuC;CAC9D,OACC,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAE,MAAM,QAAS,KAAM;AAExE;;;;;;;AAQA,SAAS,mBAAoB,OAA8B;CAC1D,MAAM,SAAsB,cAAe,KAAM,IAAI,EAAE,GAAG,MAAM,IAAI,CAAC;CACrE,KAAM,MAAM,OAAO,cAClB,OAAO,OAAQ;CAEhB,OAAO;AACR;;;;;;;;;;;;;;AAeA,SAAS,WAAY,OAAoB,OAAkC;CAC1E,MAAM,SAAsB,EAAE,GAAG,MAAM;CACvC,KAAM,MAAM,OAAO,OAAO,KAAM,KAAM,GAAI;EACzC,MAAM,QAAQ,MAAO;EACrB,MAAM,UAAU,OAAQ;EACxB,OAAQ,OACP,cAAe,OAAQ,KAAK,cAAe,KAAM,IAC9C,WAAY,SAAS,KAAM,IAC3B;CACL;CACA,OAAO;AACR;;;;;;;;;;;;;;;;;AAkBA,SAAS,UACR,OACA,MACA,YAAyB,CAAC,GACZ;CACd,MAAM,SAAsB,CAAC;CAC7B,KAAM,MAAM,OAAO,OAAO,KAAM,KAAM,GAAI;EACzC,MAAM,OAA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ACN,CAAE,gBAAgB,mBAAoB,CAAC,CAAC,QACrC,OAAO,UACR,WAAY,OAAO,mBAAoB,KAAM,CAAE,GAChD,mBAAoB,WAAY,CACjC,GACA,mBACD;AACD;;;;;;;;;;;;;AAcA,SAAgB,YAAa,MAA8B;CAC1D,MAAM,EAAE,aAAa,gBAAgB,qBAAqB,MAAM,WAC/D;CACD,MAAM,gBAAgB,2BACrB,KAAK,eACL,cACD;CAmBA,OAAO;EAVN,GAAG,mBACF,WAJe,gBAAiB,MADjC,eAAe,QAAQ,qBAAqB,QAAQ,aAAa,IAK7C,GAAG,mBAAoB,aAAc,CAAE,GAC1D,mBACD;EACA,MAAM,OAAQ,QAAQ,CAAE;EACxB,QAAQ,UAAU;CAKT;AACX;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,SAAgB,qBACf,SACA,QAC4B;CAC5B,MAAM,EAAE,gBAAgB,wBAAwB;CAChD,MAAM,gBAAgB,2BACrB,OAAO,eACP,cACD;CACA,MAAM,WAAW,gBAAiB,QAAQ,QAAQ,IAAK;CAEvD,MAAM,gBAAgB,UACrB,mBAAoB,OAAQ,GAC5B,mBAAoB,QAAS,GAC7B,mBAAoB,aAAc,CACnC;CAIA,MAAM,SAAS,iBAAkB,mBAAoB;CACrD,IAAK,OAAO,SAAS,KAAK,MAAM,QAAS,cAAc,OAAQ,GAC9D,cAAc,UAAY,cAAc,QAAsB,QAC3D,WAAY,CAAE,OAAO,MAAQ,MAAO,EAAE,UAAU,OAAO,KAAM,CAChE;CAGD,OAAO,OAAO,KAAM,aAAc,CAAC,CAAC,SAAS,IACxC,gBACF,KAAA;AACJ"}
package/build/types.cjs CHANGED
@@ -1,19 +1,5 @@
1
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 __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
-
16
- // packages/views/src/types.ts
17
- var types_exports = {};
18
- module.exports = __toCommonJS(types_exports);
19
- //# sourceMappingURL=types.cjs.map
2
+ Object.defineProperties(exports, {
3
+ __esModule: { value: true },
4
+ [Symbol.toStringTag]: { value: "Module" }
5
+ });
@@ -1,53 +1,38 @@
1
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/use-view-config.ts
21
- var use_view_config_exports = {};
22
- __export(use_view_config_exports, {
23
- useViewConfig: () => useViewConfig
2
+ Object.defineProperties(exports, {
3
+ __esModule: { value: true },
4
+ [Symbol.toStringTag]: { value: "Module" }
24
5
  });
25
- module.exports = __toCommonJS(use_view_config_exports);
26
- var import_data = require("@wordpress/data");
27
- var import_core_data = require("@wordpress/core-data");
28
- var import_lock_unlock = require("./lock-unlock.cjs");
29
- function useViewConfig({
30
- kind,
31
- name,
32
- fields
33
- }) {
34
- const fieldList = Array.isArray(fields) ? fields : fields?.split(",");
35
- const fieldsKey = fieldList ? fieldList.toSorted().join(",") : void 0;
36
- return (0, import_data.useSelect)(
37
- (select) => {
38
- return (0, import_lock_unlock.unlock)(select(import_core_data.store)).getViewConfig(
39
- kind,
40
- name,
41
- fieldsKey ? {
42
- fields: fieldsKey
43
- } : void 0
44
- );
45
- },
46
- [kind, name, fieldsKey]
47
- );
6
+ let _wordpress_data = require("@wordpress/data");
7
+ let _wordpress_core_data = require("@wordpress/core-data");
8
+ let __lock_unlock_ts = require("./lock-unlock.cjs");
9
+ // packages/views/src/use-view-config.ts
10
+ /**
11
+ * A hook that retrieves the view configuration for a given entity
12
+ * from the core data store.
13
+ *
14
+ * @param {Object} params
15
+ * @param {string} params.kind The kind of the entity.
16
+ * @param {string} params.name The name of the entity.
17
+ * @param {?(string|string[])} [params.fields] Subset of top-level config
18
+ * properties to request, as an array
19
+ * or a comma-separated string
20
+ * (mapped to the REST API `_fields`
21
+ * parameter). When omitted, the full
22
+ * config is requested.
23
+ * @return {Object} An object containing the `default_view`, `default_layouts`, `view_list`, and `form` configuration for the entity.
24
+ */
25
+ function useViewConfig({ kind, name, fields }) {
26
+ const fieldList = Array.isArray(fields) ? fields : fields?.split(",");
27
+ const fieldsKey = fieldList ? fieldList.toSorted().join(",") : void 0;
28
+ return (0, _wordpress_data.useSelect)((select) => {
29
+ return (0, __lock_unlock_ts.unlock)(select(_wordpress_core_data.store)).getViewConfig(kind, name, fieldsKey ? { fields: fieldsKey } : void 0);
30
+ }, [
31
+ kind,
32
+ name,
33
+ fieldsKey
34
+ ]);
48
35
  }
49
- // Annotate the CommonJS export names for ESM import in node:
50
- 0 && (module.exports = {
51
- useViewConfig
52
- });
53
- //# sourceMappingURL=use-view-config.cjs.map
36
+ exports.useViewConfig = useViewConfig;
37
+
38
+ //# sourceMappingURL=use-view-config.cjs.map
@@ -1,7 +1 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/use-view-config.ts"],
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 ? fieldList.toSorted().join( ',' ) : undefined;\n\treturn useSelect(\n\t\t( select ) => {\n\t\t\treturn unlock( select( coreStore ) ).getViewConfig(\n\t\t\t\tkind,\n\t\t\t\tname,\n\t\t\t\tfieldsKey\n\t\t\t\t\t? {\n\t\t\t\t\t\t\tfields: fieldsKey,\n\t\t\t\t\t\t}\n\t\t\t\t\t: undefined\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,YAAY,UAAU,SAAS,EAAE,KAAM,GAAI,IAAI;AACjE,aAAO;AAAA,IACN,CAAE,WAAY;AACb,iBAAO,2BAAQ,OAAQ,iBAAAA,KAAU,CAAE,EAAE;AAAA,QACpC;AAAA,QACA;AAAA,QACA,YACG;AAAA,UACA,QAAQ;AAAA,QACT,IACC;AAAA,MACJ;AAAA,IACD;AAAA,IACA,CAAE,MAAM,MAAM,SAAU;AAAA,EACzB;AACD;",
6
- "names": ["coreStore"]
7
- }
1
+ {"version":3,"file":"use-view-config.cjs","names":["useSelect","unlock","coreStore"],"sources":["../src/use-view-config.ts"],"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 ? fieldList.toSorted().join( ',' ) : undefined;\n\treturn useSelect(\n\t\t( select ) => {\n\t\t\treturn unlock( select( coreStore ) ).getViewConfig(\n\t\t\t\tkind,\n\t\t\t\tname,\n\t\t\t\tfieldsKey\n\t\t\t\t\t? {\n\t\t\t\t\t\t\tfields: fieldsKey,\n\t\t\t\t\t\t}\n\t\t\t\t\t: undefined\n\t\t\t);\n\t\t},\n\t\t[ kind, name, fieldsKey ]\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,cAAe,EAC9B,MACA,MACA,UAUC;CAGD,MAAM,YAAY,MAAM,QAAS,MAAO,IAAI,SAAS,QAAQ,MAAO,GAAI;CACxE,MAAM,YAAY,YAAY,UAAU,SAAS,CAAC,CAAC,KAAM,GAAI,IAAI,KAAA;CACjE,QAAA,GAAOA,gBAAAA,UAAAA,EACJ,WAAY;EACb,QAAA,GAAOC,iBAAAA,OAAAA,CAAQ,OAAQC,qBAAAA,KAAU,CAAE,CAAC,CAAC,cACpC,MACA,MACA,YACG,EACA,QAAQ,UACT,IACC,KAAA,CACJ;CACD,GACA;EAAE;EAAM;EAAM;CAAU,CACzB;AACD"}
@@ -1,124 +1,87 @@
1
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/use-view.ts
21
- var use_view_exports = {};
22
- __export(use_view_exports, {
23
- useView: () => useView
2
+ Object.defineProperties(exports, {
3
+ __esModule: { value: true },
4
+ [Symbol.toStringTag]: { value: "Module" }
24
5
  });
25
- module.exports = __toCommonJS(use_view_exports);
26
- var import_dequal = require("dequal");
27
- var import_element = require("@wordpress/element");
28
- var import_data = require("@wordpress/data");
29
- var import_preferences = require("@wordpress/preferences");
30
- var import_preference_keys = require("./preference-keys.cjs");
31
- var import_resolve_view = require("./resolve-view.cjs");
6
+ let dequal = require("dequal");
7
+ let _wordpress_element = require("@wordpress/element");
8
+ let _wordpress_data = require("@wordpress/data");
9
+ let _wordpress_preferences = require("@wordpress/preferences");
10
+ let __preference_keys_ts = require("./preference-keys.cjs");
11
+ let __resolve_view_ts = require("./resolve-view.cjs");
12
+ // packages/views/src/use-view.ts
13
+ /**
14
+ * Hook for managing DataViews view state with local persistence.
15
+ *
16
+ * Only the properties the user actually modified are persisted: every other
17
+ * property keeps resolving out of the layers below, so a change to the default
18
+ * view, to the layout defaults or to the active view overrides keeps showing
19
+ * through.
20
+ *
21
+ * @param config Configuration object for loading the view.
22
+ *
23
+ * @return Object with current view, modification state, and update functions.
24
+ */
32
25
  function useView(config) {
33
- const {
34
- kind,
35
- name,
36
- slug,
37
- defaultView,
38
- defaultLayouts,
39
- activeViewOverrides,
40
- queryParams,
41
- onChangeQueryParams
42
- } = config;
43
- const preferenceKey = (0, import_preference_keys.generatePreferenceKey)(kind, name, slug);
44
- const persistedView = (0, import_data.useSelect)(
45
- (select) => {
46
- return select(import_preferences.store).get(
47
- "core/views",
48
- preferenceKey
49
- );
50
- },
51
- [preferenceKey]
52
- );
53
- const { set } = (0, import_data.useDispatch)(import_preferences.store);
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
- ]
73
- );
74
- const applicablePersistedView = (0, import_resolve_view.getApplicablePersistedView)(
75
- persistedView,
76
- defaultLayouts
77
- );
78
- const isModified = !!applicablePersistedView && Object.keys(applicablePersistedView).length > 0;
79
- const updateView = (0, import_element.useCallback)(
80
- (newView) => {
81
- const newQueryParams = {
82
- page: Number(newView?.page ?? 1),
83
- search: newView?.search ?? ""
84
- };
85
- if (onChangeQueryParams && !(0, import_dequal.dequal)(newQueryParams, { page, search })) {
86
- onChangeQueryParams(newQueryParams);
87
- }
88
- const modifications = (0, import_resolve_view.getUserModifications)(newView, {
89
- defaultView,
90
- defaultLayouts,
91
- activeViewOverrides,
92
- persistedView
93
- });
94
- if (!(0, import_dequal.dequal)(modifications, persistedView)) {
95
- set("core/views", preferenceKey, modifications);
96
- }
97
- },
98
- [
99
- onChangeQueryParams,
100
- page,
101
- search,
102
- defaultView,
103
- defaultLayouts,
104
- activeViewOverrides,
105
- persistedView,
106
- set,
107
- preferenceKey
108
- ]
109
- );
110
- const resetToDefault = (0, import_element.useCallback)(() => {
111
- set("core/views", preferenceKey, void 0);
112
- }, [preferenceKey, set]);
113
- return {
114
- view,
115
- isModified,
116
- updateView,
117
- resetToDefault
118
- };
26
+ const { kind, name, slug, defaultView, defaultLayouts, activeViewOverrides, queryParams, onChangeQueryParams } = config;
27
+ const preferenceKey = (0, __preference_keys_ts.generatePreferenceKey)(kind, name, slug);
28
+ const persistedView = (0, _wordpress_data.useSelect)((select) => {
29
+ return select(_wordpress_preferences.store).get("core/views", preferenceKey);
30
+ }, [preferenceKey]);
31
+ const { set } = (0, _wordpress_data.useDispatch)(_wordpress_preferences.store);
32
+ const page = Number(queryParams?.page ?? 1);
33
+ const search = queryParams?.search ?? "";
34
+ const view = (0, _wordpress_element.useMemo)(() => (0, __resolve_view_ts.resolveView)({
35
+ defaultView,
36
+ defaultLayouts,
37
+ activeViewOverrides,
38
+ persistedView,
39
+ page,
40
+ search
41
+ }), [
42
+ defaultView,
43
+ defaultLayouts,
44
+ activeViewOverrides,
45
+ persistedView,
46
+ page,
47
+ search
48
+ ]);
49
+ const applicablePersistedView = (0, __resolve_view_ts.getApplicablePersistedView)(persistedView, defaultLayouts);
50
+ return {
51
+ view,
52
+ isModified: !!applicablePersistedView && Object.keys(applicablePersistedView).length > 0,
53
+ updateView: (0, _wordpress_element.useCallback)((newView) => {
54
+ const newQueryParams = {
55
+ page: Number(newView?.page ?? 1),
56
+ search: newView?.search ?? ""
57
+ };
58
+ if (onChangeQueryParams && !(0, dequal.dequal)(newQueryParams, {
59
+ page,
60
+ search
61
+ })) onChangeQueryParams(newQueryParams);
62
+ const modifications = (0, __resolve_view_ts.getUserModifications)(newView, {
63
+ defaultView,
64
+ defaultLayouts,
65
+ activeViewOverrides,
66
+ persistedView
67
+ });
68
+ if (!(0, dequal.dequal)(modifications, persistedView)) set("core/views", preferenceKey, modifications);
69
+ }, [
70
+ onChangeQueryParams,
71
+ page,
72
+ search,
73
+ defaultView,
74
+ defaultLayouts,
75
+ activeViewOverrides,
76
+ persistedView,
77
+ set,
78
+ preferenceKey
79
+ ]),
80
+ resetToDefault: (0, _wordpress_element.useCallback)(() => {
81
+ set("core/views", preferenceKey, void 0);
82
+ }, [preferenceKey, set])
83
+ };
119
84
  }
120
- // Annotate the CommonJS export names for ESM import in node:
121
- 0 && (module.exports = {
122
- useView
123
- });
124
- //# sourceMappingURL=use-view.cjs.map
85
+ exports.useView = useView;
86
+
87
+ //# sourceMappingURL=use-view.cjs.map
@@ -1,7 +1 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/use-view.ts"],
4
- "sourcesContent": ["import { dequal } from 'dequal';\nimport { useCallback, useMemo } from '@wordpress/element';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport type { View } from '@wordpress/dataviews';\nimport { store as preferencesStore } from '@wordpress/preferences';\nimport { generatePreferenceKey } from './preference-keys';\nimport {\n\tgetApplicablePersistedView,\n\tgetUserModifications,\n\tresolveView,\n} from './resolve-view';\nimport type { ViewConfig, ViewOverrides } from './types';\n\ninterface UseViewReturn {\n\tview: View;\n\tisModified: boolean;\n\tupdateView: ( newView: View ) => void;\n\tresetToDefault: () => void;\n}\n\n/**\n * Hook for managing DataViews view state with local persistence.\n *\n * Only the properties the user actually modified are persisted: every other\n * property keeps resolving out of the layers below, so a change to the default\n * view, to the layout defaults or to the active view overrides keeps showing\n * through.\n *\n * @param config Configuration object for loading the view.\n *\n * @return Object with current view, modification state, and update functions.\n */\nexport function useView( config: ViewConfig ): UseViewReturn {\n\tconst {\n\t\tkind,\n\t\tname,\n\t\tslug,\n\t\tdefaultView,\n\t\tdefaultLayouts,\n\t\tactiveViewOverrides,\n\t\tqueryParams,\n\t\tonChangeQueryParams,\n\t} = config;\n\n\tconst preferenceKey = generatePreferenceKey( kind, name, slug );\n\tconst persistedView: ViewOverrides | undefined = useSelect(\n\t\t( select ) => {\n\t\t\treturn select( preferencesStore ).get(\n\t\t\t\t'core/views',\n\t\t\t\tpreferenceKey\n\t\t\t) as ViewOverrides | undefined;\n\t\t},\n\t\t[ preferenceKey ]\n\t);\n\tconst { set } = useDispatch( preferencesStore );\n\n\tconst page = Number( queryParams?.page ?? 1 );\n\tconst search = queryParams?.search ?? '';\n\n\tconst view = useMemo(\n\t\t() =>\n\t\t\tresolveView( {\n\t\t\t\tdefaultView,\n\t\t\t\tdefaultLayouts,\n\t\t\t\tactiveViewOverrides,\n\t\t\t\tpersistedView,\n\t\t\t\tpage,\n\t\t\t\tsearch,\n\t\t\t} ),\n\t\t[\n\t\t\tdefaultView,\n\t\t\tdefaultLayouts,\n\t\t\tactiveViewOverrides,\n\t\t\tpersistedView,\n\t\t\tpage,\n\t\t\tsearch,\n\t\t]\n\t);\n\n\t// A persisted `type` the layouts do not offer is ignored on read, so it\n\t// does not count as a modification either: there is nothing to reset.\n\tconst applicablePersistedView = getApplicablePersistedView(\n\t\tpersistedView,\n\t\tdefaultLayouts\n\t);\n\tconst isModified =\n\t\t!! applicablePersistedView &&\n\t\tObject.keys( applicablePersistedView ).length > 0;\n\n\tconst updateView = useCallback(\n\t\t( newView: View ) => {\n\t\t\t// `page` and `search` live in the URL, not in the preference: they\n\t\t\t// are reported back to the consumer instead of being persisted.\n\t\t\tconst newQueryParams = {\n\t\t\t\tpage: Number( newView?.page ?? 1 ),\n\t\t\t\tsearch: newView?.search ?? '',\n\t\t\t};\n\t\t\tif (\n\t\t\t\tonChangeQueryParams &&\n\t\t\t\t! dequal( newQueryParams, { page, search } )\n\t\t\t) {\n\t\t\t\tonChangeQueryParams( newQueryParams );\n\t\t\t}\n\n\t\t\tconst modifications = getUserModifications( newView, {\n\t\t\t\tdefaultView,\n\t\t\t\tdefaultLayouts,\n\t\t\t\tactiveViewOverrides,\n\t\t\t\tpersistedView,\n\t\t\t} );\n\t\t\t// Compared against the preference as stored, so a persisted `type`\n\t\t\t// the layouts do not offer is written out on the first update.\n\t\t\tif ( ! dequal( modifications, persistedView ) ) {\n\t\t\t\t// `undefined` clears the preference: the user reverted every\n\t\t\t\t// property they had modified.\n\t\t\t\tset( 'core/views', preferenceKey, modifications );\n\t\t\t}\n\t\t},\n\t\t[\n\t\t\tonChangeQueryParams,\n\t\t\tpage,\n\t\t\tsearch,\n\t\t\tdefaultView,\n\t\t\tdefaultLayouts,\n\t\t\tactiveViewOverrides,\n\t\t\tpersistedView,\n\t\t\tset,\n\t\t\tpreferenceKey,\n\t\t]\n\t);\n\n\tconst resetToDefault = useCallback( () => {\n\t\tset( 'core/views', preferenceKey, undefined );\n\t}, [ preferenceKey, set ] );\n\n\treturn {\n\t\tview,\n\t\tisModified,\n\t\tupdateView,\n\t\tresetToDefault,\n\t};\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAuB;AACvB,qBAAqC;AACrC,kBAAuC;AAEvC,yBAA0C;AAC1C,6BAAsC;AACtC,0BAIO;AAsBA,SAAS,QAAS,QAAoC;AAC5D,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA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,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
- "names": ["preferencesStore"]
7
- }
1
+ {"version":3,"file":"use-view.cjs","names":["generatePreferenceKey","useSelect","preferencesStore","useDispatch","useMemo","resolveView","getApplicablePersistedView","useCallback","getUserModifications"],"sources":["../src/use-view.ts"],"sourcesContent":["import { dequal } from 'dequal';\nimport { useCallback, useMemo } from '@wordpress/element';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport type { View } from '@wordpress/dataviews';\nimport { store as preferencesStore } from '@wordpress/preferences';\nimport { generatePreferenceKey } from './preference-keys';\nimport {\n\tgetApplicablePersistedView,\n\tgetUserModifications,\n\tresolveView,\n} from './resolve-view';\nimport type { ViewConfig, ViewOverrides } from './types';\n\ninterface UseViewReturn {\n\tview: View;\n\tisModified: boolean;\n\tupdateView: ( newView: View ) => void;\n\tresetToDefault: () => void;\n}\n\n/**\n * Hook for managing DataViews view state with local persistence.\n *\n * Only the properties the user actually modified are persisted: every other\n * property keeps resolving out of the layers below, so a change to the default\n * view, to the layout defaults or to the active view overrides keeps showing\n * through.\n *\n * @param config Configuration object for loading the view.\n *\n * @return Object with current view, modification state, and update functions.\n */\nexport function useView( config: ViewConfig ): UseViewReturn {\n\tconst {\n\t\tkind,\n\t\tname,\n\t\tslug,\n\t\tdefaultView,\n\t\tdefaultLayouts,\n\t\tactiveViewOverrides,\n\t\tqueryParams,\n\t\tonChangeQueryParams,\n\t} = config;\n\n\tconst preferenceKey = generatePreferenceKey( kind, name, slug );\n\tconst persistedView: ViewOverrides | undefined = useSelect(\n\t\t( select ) => {\n\t\t\treturn select( preferencesStore ).get(\n\t\t\t\t'core/views',\n\t\t\t\tpreferenceKey\n\t\t\t) as ViewOverrides | undefined;\n\t\t},\n\t\t[ preferenceKey ]\n\t);\n\tconst { set } = useDispatch( preferencesStore );\n\n\tconst page = Number( queryParams?.page ?? 1 );\n\tconst search = queryParams?.search ?? '';\n\n\tconst view = useMemo(\n\t\t() =>\n\t\t\tresolveView( {\n\t\t\t\tdefaultView,\n\t\t\t\tdefaultLayouts,\n\t\t\t\tactiveViewOverrides,\n\t\t\t\tpersistedView,\n\t\t\t\tpage,\n\t\t\t\tsearch,\n\t\t\t} ),\n\t\t[\n\t\t\tdefaultView,\n\t\t\tdefaultLayouts,\n\t\t\tactiveViewOverrides,\n\t\t\tpersistedView,\n\t\t\tpage,\n\t\t\tsearch,\n\t\t]\n\t);\n\n\t// A persisted `type` the layouts do not offer is ignored on read, so it\n\t// does not count as a modification either: there is nothing to reset.\n\tconst applicablePersistedView = getApplicablePersistedView(\n\t\tpersistedView,\n\t\tdefaultLayouts\n\t);\n\tconst isModified =\n\t\t!! applicablePersistedView &&\n\t\tObject.keys( applicablePersistedView ).length > 0;\n\n\tconst updateView = useCallback(\n\t\t( newView: View ) => {\n\t\t\t// `page` and `search` live in the URL, not in the preference: they\n\t\t\t// are reported back to the consumer instead of being persisted.\n\t\t\tconst newQueryParams = {\n\t\t\t\tpage: Number( newView?.page ?? 1 ),\n\t\t\t\tsearch: newView?.search ?? '',\n\t\t\t};\n\t\t\tif (\n\t\t\t\tonChangeQueryParams &&\n\t\t\t\t! dequal( newQueryParams, { page, search } )\n\t\t\t) {\n\t\t\t\tonChangeQueryParams( newQueryParams );\n\t\t\t}\n\n\t\t\tconst modifications = getUserModifications( newView, {\n\t\t\t\tdefaultView,\n\t\t\t\tdefaultLayouts,\n\t\t\t\tactiveViewOverrides,\n\t\t\t\tpersistedView,\n\t\t\t} );\n\t\t\t// Compared against the preference as stored, so a persisted `type`\n\t\t\t// the layouts do not offer is written out on the first update.\n\t\t\tif ( ! dequal( modifications, persistedView ) ) {\n\t\t\t\t// `undefined` clears the preference: the user reverted every\n\t\t\t\t// property they had modified.\n\t\t\t\tset( 'core/views', preferenceKey, modifications );\n\t\t\t}\n\t\t},\n\t\t[\n\t\t\tonChangeQueryParams,\n\t\t\tpage,\n\t\t\tsearch,\n\t\t\tdefaultView,\n\t\t\tdefaultLayouts,\n\t\t\tactiveViewOverrides,\n\t\t\tpersistedView,\n\t\t\tset,\n\t\t\tpreferenceKey,\n\t\t]\n\t);\n\n\tconst resetToDefault = useCallback( () => {\n\t\tset( 'core/views', preferenceKey, undefined );\n\t}, [ preferenceKey, set ] );\n\n\treturn {\n\t\tview,\n\t\tisModified,\n\t\tupdateView,\n\t\tresetToDefault,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAgCA,SAAgB,QAAS,QAAoC;CAC5D,MAAM,EACL,MACA,MACA,MACA,aACA,gBACA,qBACA,aACA,wBACG;CAEJ,MAAM,iBAAA,GAAgBA,qBAAAA,sBAAAA,CAAuB,MAAM,MAAM,IAAK;CAC9D,MAAM,iBAAA,GAA2CC,gBAAAA,UAAAA,EAC9C,WAAY;EACb,OAAO,OAAQC,uBAAAA,KAAiB,CAAC,CAAC,IACjC,cACA,aACD;CACD,GACA,CAAE,aAAc,CACjB;CACA,MAAM,EAAE,SAAA,GAAQC,gBAAAA,YAAAA,CAAaD,uBAAAA,KAAiB;CAE9C,MAAM,OAAO,OAAQ,aAAa,QAAQ,CAAE;CAC5C,MAAM,SAAS,aAAa,UAAU;CAEtC,MAAM,QAAA,GAAOE,mBAAAA,QAAAA,QAAAA,GAEXC,kBAAAA,YAAAA,CAAa;EACZ;EACA;EACA;EACA;EACA;EACA;CACD,CAAE,GACH;EACC;EACA;EACA;EACA;EACA;EACA;CACD,CACD;CAIA,MAAM,2BAAA,GAA0BC,kBAAAA,2BAAAA,CAC/B,eACA,cACD;CAmDA,OAAO;EACN;EACA,YAnDA,CAAC,CAAE,2BACH,OAAO,KAAM,uBAAwB,CAAC,CAAC,SAAS;EAmDhD,aAAA,GAjDkBC,mBAAAA,YAAAA,EAChB,YAAmB;GAGpB,MAAM,iBAAiB;IACtB,MAAM,OAAQ,SAAS,QAAQ,CAAE;IACjC,QAAQ,SAAS,UAAU;GAC5B;GACA,IACC,uBACA,EAAA,GAAE,OAAA,OAAA,CAAQ,gBAAgB;IAAE;IAAM;GAAO,CAAE,GAE3C,oBAAqB,cAAe;GAGrC,MAAM,iBAAA,GAAgBC,kBAAAA,qBAAAA,CAAsB,SAAS;IACpD;IACA;IACA;IACA;GACD,CAAE;GAGF,IAAK,EAAA,GAAE,OAAA,OAAA,CAAQ,eAAe,aAAc,GAG3C,IAAK,cAAc,eAAe,aAAc;EAElD,GACA;GACC;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;EACD,CAUS;EACT,iBAAA,GARsBD,mBAAAA,YAAAA,OAAmB;GACzC,IAAK,cAAc,eAAe,KAAA,CAAU;EAC7C,GAAG,CAAE,eAAe,GAAI,CAMV;CACd;AACD"}
@@ -1,10 +1,4 @@
1
- // packages/views/src/index.ts
2
1
  import { useView } from "./use-view.mjs";
3
2
  import { loadView } from "./load-view.mjs";
4
3
  import { useViewConfig } from "./use-view-config.mjs";
5
- export {
6
- loadView,
7
- useView,
8
- useViewConfig
9
- };
10
- //# sourceMappingURL=index.mjs.map
4
+ export { loadView, useView, useViewConfig };
@@ -1,32 +1,37 @@
1
- // packages/views/src/load-view.ts
2
1
  import { select } from "@wordpress/data";
3
- import { store as preferencesStore } from "@wordpress/preferences";
2
+ import { store } from "@wordpress/preferences";
4
3
  import { generatePreferenceKey } from "./preference-keys.mjs";
5
4
  import { resolveView } from "./resolve-view.mjs";
5
+ // packages/views/src/load-view.ts
6
+ /**
7
+ * Async function for loading view state in route loaders.
8
+ *
9
+ * Resolves the same layers `useView` does, so a route loader and the hook that
10
+ * takes over from it agree on the view.
11
+ *
12
+ * @param config Configuration object for loading the view.
13
+ * @param config.kind Entity kind (e.g., 'postType', 'taxonomy', 'root').
14
+ * @param config.name Specific entity name.
15
+ * @param config.slug View identifier.
16
+ * @param config.defaultView Default view configuration.
17
+ * @param config.defaultLayouts Default layout configurations keyed by layout type.
18
+ * @param config.activeViewOverrides View overrides applied on top but never persisted.
19
+ * @param config.queryParams Object with `page` and/or `search` from URL.
20
+ * @return Promise resolving to the loaded view object.
21
+ */
6
22
  async function loadView(config) {
7
- const {
8
- kind,
9
- name,
10
- slug,
11
- defaultView,
12
- defaultLayouts,
13
- activeViewOverrides,
14
- queryParams
15
- } = config;
16
- const preferenceKey = generatePreferenceKey(kind, name, slug);
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
- });
23
+ const { kind, name, slug, defaultView, defaultLayouts, activeViewOverrides, queryParams } = config;
24
+ const preferenceKey = generatePreferenceKey(kind, name, slug);
25
+ const persistedView = select(store).get("core/views", preferenceKey);
26
+ return resolveView({
27
+ defaultView,
28
+ defaultLayouts,
29
+ activeViewOverrides,
30
+ persistedView,
31
+ page: queryParams?.page,
32
+ search: queryParams?.search
33
+ });
28
34
  }
29
- export {
30
- loadView
31
- };
32
- //# sourceMappingURL=load-view.mjs.map
35
+ export { loadView };
36
+
37
+ //# sourceMappingURL=load-view.mjs.map
@@ -1,7 +1 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/load-view.ts"],
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
- "names": []
7
- }
1
+ {"version":3,"file":"load-view.mjs","names":["preferencesStore"],"sources":["../src/load-view.ts"],"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"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAsBA,eAAsB,SAAU,QAAqB;CACpD,MAAM,EACL,MACA,MACA,MACA,aACA,gBACA,qBACA,gBACG;CACJ,MAAM,gBAAgB,sBAAuB,MAAM,MAAM,IAAK;CAC9D,MAAM,gBAA2C,OAChDA,KACD,CAAC,CAAC,IAAK,cAAc,aAAc;CAEnC,OAAO,YAAa;EACnB;EACA;EACA;EACA;EACA,MAAM,aAAa;EACnB,QAAQ,aAAa;CACtB,CAAE;AACH"}
@@ -1,11 +1,6 @@
1
- // packages/views/src/lock-unlock.ts
2
1
  import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from "@wordpress/private-apis";
3
- var { lock, unlock } = __dangerousOptInToUnstableAPIsOnlyForCoreModules(
4
- "I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.",
5
- "@wordpress/views"
6
- );
7
- export {
8
- lock,
9
- unlock
10
- };
11
- //# sourceMappingURL=lock-unlock.mjs.map
2
+ // packages/views/src/lock-unlock.ts
3
+ const { lock, unlock } = __dangerousOptInToUnstableAPIsOnlyForCoreModules("I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.", "@wordpress/views");
4
+ export { lock, unlock };
5
+
6
+ //# sourceMappingURL=lock-unlock.mjs.map
@@ -1,7 +1 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/lock-unlock.ts"],
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
- "names": []
7
- }
1
+ {"version":3,"file":"lock-unlock.mjs","names":[],"sources":["../src/lock-unlock.ts"],"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"],"mappings":";;AAEA,MAAa,EAAE,MAAM,WACpB,iDACC,iIACA,kBACD"}
@@ -1,8 +1,15 @@
1
1
  // packages/views/src/preference-keys.ts
2
+ /**
3
+ * Generates a unique preference key for a DataViews view.
4
+ *
5
+ * @param kind The entity kind (e.g., 'postType', 'root')
6
+ * @param name The specific entity name (e.g., 'post', 'user', 'site')
7
+ * @param slug The specific entity slug (e.g., 'category', 'post', 'all')
8
+ * @return The preference key string
9
+ */
2
10
  function generatePreferenceKey(kind, name, slug) {
3
- return `dataviews-${kind}-${name}-${slug}`;
11
+ return `dataviews-${kind}-${name}-${slug}`;
4
12
  }
5
- export {
6
- generatePreferenceKey
7
- };
8
- //# sourceMappingURL=preference-keys.mjs.map
13
+ export { generatePreferenceKey };
14
+
15
+ //# sourceMappingURL=preference-keys.mjs.map
@@ -1,7 +1 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/preference-keys.ts"],
4
- "sourcesContent": ["/**\n * Generates a unique preference key for a DataViews view.\n *\n * @param kind The entity kind (e.g., 'postType', 'root')\n * @param name The specific entity name (e.g., 'post', 'user', 'site')\n * @param slug The specific entity slug (e.g., 'category', 'post', 'all')\n * @return The preference key string\n */\nexport function generatePreferenceKey(\n\tkind: string,\n\tname: string,\n\tslug: string\n): string {\n\treturn `dataviews-${ kind }-${ name }-${ slug }`;\n}\n"],
5
- "mappings": ";AAQO,SAAS,sBACf,MACA,MACA,MACS;AACT,SAAO,aAAc,IAAK,IAAK,IAAK,IAAK,IAAK;AAC/C;",
6
- "names": []
7
- }
1
+ {"version":3,"file":"preference-keys.mjs","names":[],"sources":["../src/preference-keys.ts"],"sourcesContent":["/**\n * Generates a unique preference key for a DataViews view.\n *\n * @param kind The entity kind (e.g., 'postType', 'root')\n * @param name The specific entity name (e.g., 'post', 'user', 'site')\n * @param slug The specific entity slug (e.g., 'category', 'post', 'all')\n * @return The preference key string\n */\nexport function generatePreferenceKey(\n\tkind: string,\n\tname: string,\n\tslug: string\n): string {\n\treturn `dataviews-${ kind }-${ name }-${ slug }`;\n}\n"],"mappings":";;;;;;;;;AAQA,SAAgB,sBACf,MACA,MACA,MACS;CACT,OAAO,aAAc,KAAM,GAAI,KAAM,GAAI;AAC1C"}