@wordpress/dataviews 13.1.1-next.v.202603161435.0 → 14.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -6
- package/README.md +17 -2
- package/build/components/dataform-controls/datetime.cjs +8 -4
- package/build/components/dataform-controls/datetime.cjs.map +2 -2
- package/build/components/dataform-layouts/card/index.cjs +132 -128
- package/build/components/dataform-layouts/card/index.cjs.map +3 -3
- package/build/components/dataviews-bulk-actions/index.cjs +28 -5
- package/build/components/dataviews-bulk-actions/index.cjs.map +2 -2
- package/build/components/dataviews-context/index.cjs +2 -2
- package/build/components/dataviews-context/index.cjs.map +2 -2
- package/build/components/dataviews-footer/index.cjs +2 -3
- package/build/components/dataviews-footer/index.cjs.map +2 -2
- package/build/components/dataviews-layouts/grid/composite-grid.cjs +378 -249
- package/build/components/dataviews-layouts/grid/composite-grid.cjs.map +2 -2
- package/build/components/dataviews-layouts/picker-grid/index.cjs +63 -30
- package/build/components/dataviews-layouts/picker-grid/index.cjs.map +2 -2
- package/build/components/dataviews-layouts/picker-table/index.cjs +34 -22
- package/build/components/dataviews-layouts/picker-table/index.cjs.map +2 -2
- package/build/components/dataviews-layouts/utils/use-infinite-scroll.cjs +62 -0
- package/build/components/dataviews-layouts/utils/use-infinite-scroll.cjs.map +7 -0
- package/build/components/dataviews-picker-footer/index.cjs +23 -4
- package/build/components/dataviews-picker-footer/index.cjs.map +2 -2
- package/build/components/dataviews-search/index.cjs +2 -1
- package/build/components/dataviews-search/index.cjs.map +2 -2
- package/build/components/dataviews-selection-checkbox/index.cjs +3 -2
- package/build/components/dataviews-selection-checkbox/index.cjs.map +2 -2
- package/build/components/dataviews-view-config/index.cjs +0 -2
- package/build/components/dataviews-view-config/index.cjs.map +3 -3
- package/build/components/dataviews-view-config/infinite-scroll-toggle.cjs +0 -3
- package/build/components/dataviews-view-config/infinite-scroll-toggle.cjs.map +2 -2
- package/build/dataviews/index.cjs +37 -37
- package/build/dataviews/index.cjs.map +3 -3
- package/build/dataviews-picker/index.cjs +25 -24
- package/build/dataviews-picker/index.cjs.map +3 -3
- package/build/hooks/index.cjs +11 -2
- package/build/hooks/index.cjs.map +2 -2
- package/build/hooks/use-data.cjs +146 -9
- package/build/hooks/use-data.cjs.map +2 -2
- package/build/hooks/use-infinite-scroll.cjs +208 -0
- package/build/hooks/use-infinite-scroll.cjs.map +7 -0
- package/build/hooks/use-selected-items.cjs +57 -0
- package/build/hooks/use-selected-items.cjs.map +7 -0
- package/build/types/dataviews.cjs.map +1 -1
- package/build/types/field-api.cjs.map +1 -1
- package/build/utils/filter-sort-and-paginate.cjs +5 -1
- package/build/utils/filter-sort-and-paginate.cjs.map +2 -2
- package/build/utils/get-footer-message.cjs +8 -8
- package/build/utils/get-footer-message.cjs.map +2 -2
- package/build-module/components/dataform-controls/datetime.mjs +8 -4
- package/build-module/components/dataform-controls/datetime.mjs.map +2 -2
- package/build-module/components/dataform-layouts/card/index.mjs +132 -133
- package/build-module/components/dataform-layouts/card/index.mjs.map +2 -2
- package/build-module/components/dataviews-bulk-actions/index.mjs +28 -5
- package/build-module/components/dataviews-bulk-actions/index.mjs.map +2 -2
- package/build-module/components/dataviews-context/index.mjs +2 -2
- package/build-module/components/dataviews-context/index.mjs.map +2 -2
- package/build-module/components/dataviews-footer/index.mjs +2 -3
- package/build-module/components/dataviews-footer/index.mjs.map +2 -2
- package/build-module/components/dataviews-layouts/grid/composite-grid.mjs +387 -250
- package/build-module/components/dataviews-layouts/grid/composite-grid.mjs.map +2 -2
- package/build-module/components/dataviews-layouts/picker-grid/index.mjs +67 -31
- package/build-module/components/dataviews-layouts/picker-grid/index.mjs.map +2 -2
- package/build-module/components/dataviews-layouts/picker-table/index.mjs +34 -22
- package/build-module/components/dataviews-layouts/picker-table/index.mjs.map +2 -2
- package/build-module/components/dataviews-layouts/utils/use-infinite-scroll.mjs +26 -0
- package/build-module/components/dataviews-layouts/utils/use-infinite-scroll.mjs.map +7 -0
- package/build-module/components/dataviews-picker-footer/index.mjs +23 -4
- package/build-module/components/dataviews-picker-footer/index.mjs.map +2 -2
- package/build-module/components/dataviews-search/index.mjs +2 -1
- package/build-module/components/dataviews-search/index.mjs.map +2 -2
- package/build-module/components/dataviews-selection-checkbox/index.mjs +3 -2
- package/build-module/components/dataviews-selection-checkbox/index.mjs.map +2 -2
- package/build-module/components/dataviews-view-config/index.mjs +0 -2
- package/build-module/components/dataviews-view-config/index.mjs.map +2 -2
- package/build-module/components/dataviews-view-config/infinite-scroll-toggle.mjs +0 -3
- package/build-module/components/dataviews-view-config/infinite-scroll-toggle.mjs.map +2 -2
- package/build-module/dataviews/index.mjs +45 -39
- package/build-module/dataviews/index.mjs.map +2 -2
- package/build-module/dataviews-picker/index.mjs +33 -26
- package/build-module/dataviews-picker/index.mjs.map +2 -2
- package/build-module/hooks/index.mjs +7 -1
- package/build-module/hooks/index.mjs.map +2 -2
- package/build-module/hooks/use-data.mjs +147 -10
- package/build-module/hooks/use-data.mjs.map +2 -2
- package/build-module/hooks/use-infinite-scroll.mjs +188 -0
- package/build-module/hooks/use-infinite-scroll.mjs.map +7 -0
- package/build-module/hooks/use-selected-items.mjs +36 -0
- package/build-module/hooks/use-selected-items.mjs.map +7 -0
- package/build-module/utils/filter-sort-and-paginate.mjs +5 -1
- package/build-module/utils/filter-sort-and-paginate.mjs.map +2 -2
- package/build-module/utils/get-footer-message.mjs +8 -8
- package/build-module/utils/get-footer-message.mjs.map +2 -2
- package/build-style/style-rtl.css +61 -37
- package/build-style/style.css +61 -37
- package/build-types/components/dataform-controls/datetime.d.ts +1 -1
- package/build-types/components/dataform-controls/datetime.d.ts.map +1 -1
- package/build-types/components/dataform-layouts/card/index.d.ts.map +1 -1
- package/build-types/components/dataviews-bulk-actions/index.d.ts +2 -1
- package/build-types/components/dataviews-bulk-actions/index.d.ts.map +1 -1
- package/build-types/components/dataviews-context/index.d.ts +1 -1
- package/build-types/components/dataviews-context/index.d.ts.map +1 -1
- package/build-types/components/dataviews-footer/index.d.ts.map +1 -1
- package/build-types/components/dataviews-layouts/grid/composite-grid.d.ts.map +1 -1
- package/build-types/components/dataviews-layouts/picker-grid/index.d.ts.map +1 -1
- package/build-types/components/dataviews-layouts/picker-table/index.d.ts.map +1 -1
- package/build-types/components/dataviews-layouts/utils/use-infinite-scroll.d.ts +22 -0
- package/build-types/components/dataviews-layouts/utils/use-infinite-scroll.d.ts.map +1 -0
- package/build-types/components/dataviews-picker-footer/index.d.ts.map +1 -1
- package/build-types/components/dataviews-search/index.d.ts.map +1 -1
- package/build-types/components/dataviews-selection-checkbox/index.d.ts.map +1 -1
- package/build-types/components/dataviews-view-config/index.d.ts.map +1 -1
- package/build-types/components/dataviews-view-config/infinite-scroll-toggle.d.ts +1 -1
- package/build-types/components/dataviews-view-config/infinite-scroll-toggle.d.ts.map +1 -1
- package/build-types/dataviews/index.d.ts +0 -1
- package/build-types/dataviews/index.d.ts.map +1 -1
- package/build-types/dataviews/stories/fixtures.d.ts.map +1 -1
- package/build-types/dataviews/stories/free-composition.d.ts.map +1 -1
- package/build-types/dataviews/stories/index.story.d.ts +11 -0
- package/build-types/dataviews/stories/index.story.d.ts.map +1 -1
- package/build-types/dataviews/stories/infinite-scroll.d.ts.map +1 -1
- package/build-types/dataviews/stories/with-card.d.ts.map +1 -1
- package/build-types/dataviews-picker/index.d.ts +0 -1
- package/build-types/dataviews-picker/index.d.ts.map +1 -1
- package/build-types/dataviews-picker/stories/fixtures.d.ts.map +1 -1
- package/build-types/dataviews-picker/stories/index.story.d.ts.map +1 -1
- package/build-types/field-types/stories/index.story.d.ts.map +1 -1
- package/build-types/hooks/index.d.ts +3 -0
- package/build-types/hooks/index.d.ts.map +1 -1
- package/build-types/hooks/test/use-data.d.ts +2 -0
- package/build-types/hooks/test/use-data.d.ts.map +1 -0
- package/build-types/hooks/use-data.d.ts +41 -3
- package/build-types/hooks/use-data.d.ts.map +1 -1
- package/build-types/hooks/use-infinite-scroll.d.ts +21 -0
- package/build-types/hooks/use-infinite-scroll.d.ts.map +1 -0
- package/build-types/hooks/use-selected-items.d.ts +19 -0
- package/build-types/hooks/use-selected-items.d.ts.map +1 -0
- package/build-types/types/dataviews.d.ts +7 -1
- package/build-types/types/dataviews.d.ts.map +1 -1
- package/build-types/types/field-api.d.ts +15 -4
- package/build-types/types/field-api.d.ts.map +1 -1
- package/build-types/utils/filter-sort-and-paginate.d.ts.map +1 -1
- package/build-types/utils/get-footer-message.d.ts +3 -2
- package/build-types/utils/get-footer-message.d.ts.map +1 -1
- package/build-wp/index.js +3013 -2613
- package/package.json +19 -19
- package/src/components/dataform-controls/datetime.tsx +19 -11
- package/src/components/dataform-layouts/card/index.tsx +171 -146
- package/src/components/dataform-layouts/card/style.scss +8 -5
- package/src/components/dataviews-bulk-actions/index.tsx +28 -1
- package/src/components/dataviews-context/index.ts +2 -2
- package/src/components/dataviews-footer/index.tsx +1 -6
- package/src/components/dataviews-layouts/grid/composite-grid.tsx +433 -284
- package/src/components/dataviews-layouts/grid/style.scss +4 -0
- package/src/components/dataviews-layouts/picker-grid/index.tsx +53 -15
- package/src/components/dataviews-layouts/picker-table/index.tsx +42 -22
- package/src/components/dataviews-layouts/utils/use-infinite-scroll.ts +64 -0
- package/src/components/dataviews-picker-footer/index.tsx +21 -1
- package/src/components/dataviews-search/index.tsx +2 -1
- package/src/components/dataviews-selection-checkbox/index.tsx +4 -2
- package/src/components/dataviews-view-config/index.tsx +0 -2
- package/src/components/dataviews-view-config/infinite-scroll-toggle.tsx +0 -5
- package/src/dataviews/index.tsx +57 -52
- package/src/dataviews/stories/fixtures.tsx +288 -0
- package/src/dataviews/stories/free-composition.tsx +12 -11
- package/src/dataviews/stories/index.story.tsx +19 -2
- package/src/dataviews/stories/infinite-scroll.tsx +12 -92
- package/src/dataviews/stories/with-card.tsx +30 -23
- package/src/dataviews/style.scss +5 -7
- package/src/dataviews/test/dataviews.tsx +21 -9
- package/src/dataviews-picker/index.tsx +40 -34
- package/src/dataviews-picker/stories/fixtures.tsx +270 -0
- package/src/dataviews-picker/stories/index.story.tsx +62 -129
- package/src/field-types/stories/index.story.tsx +12 -0
- package/src/hooks/index.ts +3 -0
- package/src/hooks/test/use-data.ts +791 -0
- package/src/hooks/use-data.ts +288 -21
- package/src/hooks/use-infinite-scroll.ts +304 -0
- package/src/hooks/use-selected-items.ts +72 -0
- package/src/types/dataviews.ts +8 -1
- package/src/types/field-api.ts +16 -3
- package/src/utils/filter-sort-and-paginate.ts +13 -1
- package/src/utils/get-footer-message.ts +12 -9
- package/src/utils/test/filter-sort-and-paginate.js +78 -54
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hooks/use-data.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useEffect, useRef, useState } from '@wordpress/element';\n\ntype PaginationInfo = {\n\ttotalItems: number;\n\ttotalPages: number;\n\tinfiniteScrollHandler?: () => void;\n};\n\nexport default function useData< Item >(\n\tdata: Item[],\n\tisLoading: boolean | undefined,\n\tpaginationInfo: PaginationInfo\n): {\n\tdata: Item[];\n\tpaginationInfo: PaginationInfo;\n\thasInitiallyLoaded: boolean;\n} {\n\tconst previousDataRef = useRef< Item[] >( data );\n\tconst previousPaginationInfoRef =\n\t\tuseRef< PaginationInfo >( paginationInfo );\n\tconst [ hasInitiallyLoaded, setHasInitiallyLoaded ] = useState(\n\t\t! isLoading\n\t);\n\tuseEffect( () => {\n\t\tif ( ! isLoading ) {\n\t\t\tpreviousDataRef.current = data;\n\t\t\tpreviousPaginationInfoRef.current = paginationInfo;\n\t\t\tsetHasInitiallyLoaded( true );\n\t\t}\n\t}, [ data, isLoading, paginationInfo ] );\n\treturn {\n\t\tdata:\n\t\t\tisLoading && previousDataRef.current?.length\n\t\t\t\t? previousDataRef.current\n\t\t\t\t: data,\n\t\tpaginationInfo:\n\t\t\tisLoading && previousDataRef.current?.length\n\t\t\t\t? previousPaginationInfoRef.current\n\t\t\t\t: paginationInfo,\n\t\thasInitiallyLoaded,\n\t};\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useState, useEffect, useMemo, useRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport type { View } from '../types';\n\ntype PaginationInfo = {\n\ttotalItems: number;\n\ttotalPages: number;\n};\n\ninterface UseDataParams< Item > {\n\tview: View;\n\tdata: Item[];\n\tgetItemId: ( item: Item ) => string;\n\tisLoading?: boolean;\n\tpaginationInfo: PaginationInfo;\n\tselection?: string[];\n}\n\ninterface UseDataResult< Item > {\n\tdata: ( Item & { position?: number } )[];\n\tpaginationInfo: PaginationInfo;\n\thasInitiallyLoaded: boolean;\n\tsetVisibleEntries?: React.Dispatch< React.SetStateAction< number[] > >;\n}\n\n/**\n * Hook to manage data for DataViews.\n *\n * When infinite scroll is enabled, this hook handles:\n * - Loading more data when scrolling up or down\n * - Maintaining stable positions for items\n * - Unloading items that are no longer visible (with a buffer)\n *\n * When infinite scroll is disabled, it preserves the previous data and\n * pagination info while loading, so the UI doesn't flash empty.\n *\n * In both cases, it tracks whether data has initially loaded.\n *\n * @param params - Configuration parameters\n * @param params.view - Current view configuration\n * @param params.data - Current page of data\n * @param params.getItemId - Function to extract item ID\n * @param params.isLoading - Whether data is currently loading\n * @param params.paginationInfo - Pagination info (totalItems, totalPages)\n * @param params.selection - Currently selected item IDs\n * @return Object containing data, paginationInfo, hasInitiallyLoaded,\n * and optional setVisibleEntries callback\n */\nexport default function useData< Item >( {\n\tview,\n\tdata: shownData,\n\tgetItemId,\n\tisLoading,\n\tpaginationInfo,\n\tselection,\n}: UseDataParams< Item > ): UseDataResult< Item > {\n\tconst isInfiniteScrollEnabled = view.infiniteScrollEnabled;\n\n\tconst [ hasInitiallyLoaded, setHasInitiallyLoaded ] = useState(\n\t\t! isLoading\n\t);\n\tuseEffect( () => {\n\t\tif ( ! isLoading ) {\n\t\t\tsetHasInitiallyLoaded( true );\n\t\t}\n\t}, [ isLoading ] );\n\n\tconst previousDataRef = useRef< Item[] >( shownData );\n\tconst previousPaginationInfoRef =\n\t\tuseRef< PaginationInfo >( paginationInfo );\n\tuseEffect( () => {\n\t\tif ( ! isLoading ) {\n\t\t\tpreviousDataRef.current = shownData;\n\t\t\tpreviousPaginationInfoRef.current = paginationInfo;\n\t\t}\n\t}, [ shownData, isLoading, paginationInfo ] );\n\n\t// Infinite scroll state.\n\tconst [ visibleEntries, setVisibleEntries ] = useState< number[] >( [] );\n\n\t// Track the mapping of item IDs to their positions in the full dataset\n\tconst positionMapRef = useRef< Map< string, number > >( new Map() );\n\n\t// Store accumulated records in a ref for persistence across renders\n\tconst allLoadedRecordsRef = useRef< Item[] >( [] );\n\n\t// Track previous view parameters to detect when we need to reset\n\tconst prevViewParamsRef = useRef< {\n\t\tsearch: string | undefined;\n\t\tfilters: string | undefined;\n\t\tperPage: number | undefined;\n\t} >( {\n\t\tsearch: undefined,\n\t\tfilters: undefined,\n\t\tperPage: undefined,\n\t} );\n\n\t// Determine scroll direction based on position changes\n\tconst scrollDirectionRef = useRef< 'up' | 'down' | undefined >( undefined );\n\tconst prevStartPositionRef = useRef< number | undefined >( undefined );\n\n\t// Track whether we've done initial load\n\tconst hasInitializedRef = useRef( false );\n\n\t// Compute data synchronously during render using useMemo\n\t// This ensures the returned data is always in sync with shownData\n\tconst allLoadedRecords = useMemo( () => {\n\t\t// Update scroll direction based on position changes\n\t\tif (\n\t\t\tview.startPosition !== undefined &&\n\t\t\tprevStartPositionRef.current !== undefined\n\t\t) {\n\t\t\tif ( view.startPosition < prevStartPositionRef.current ) {\n\t\t\t\tscrollDirectionRef.current = 'up';\n\t\t\t} else if ( view.startPosition > prevStartPositionRef.current ) {\n\t\t\t\tscrollDirectionRef.current = 'down';\n\t\t\t}\n\t\t}\n\t\tprevStartPositionRef.current = view.startPosition;\n\n\t\t// Serialize filters for comparison\n\t\tconst currentFiltersKey = JSON.stringify( view.filters ?? [] );\n\t\tconst prevFiltersKey = prevViewParamsRef.current.filters;\n\n\t\t// Check if view parameters that require a reset have changed\n\t\tconst shouldReset =\n\t\t\t! hasInitializedRef.current ||\n\t\t\t! view.infiniteScrollEnabled ||\n\t\t\tview.search !== prevViewParamsRef.current.search ||\n\t\t\tcurrentFiltersKey !== prevFiltersKey ||\n\t\t\tview.perPage !== prevViewParamsRef.current.perPage;\n\t\thasInitializedRef.current = true;\n\t\t// Update tracked view parameters\n\t\tprevViewParamsRef.current = {\n\t\t\tsearch: view.search,\n\t\t\tfilters: currentFiltersKey,\n\t\t\tperPage: view.perPage,\n\t\t};\n\n\t\tif ( shouldReset ) {\n\t\t\t// Reset - clear position map and replace all data\n\t\t\tpositionMapRef.current.clear();\n\t\t\t// Reset scroll direction to prevent stale directional filtering\n\t\t\tscrollDirectionRef.current = undefined;\n\t\t\t// Use the view's startPosition if defined, otherwise default to 1\n\t\t\tconst startPosition = view.search ? 1 : view.startPosition ?? 1;\n\t\t\tconst records = shownData.map( ( record, index ) => {\n\t\t\t\tconst position = startPosition + index;\n\t\t\t\tpositionMapRef.current.set( getItemId( record ), position );\n\t\t\t\treturn {\n\t\t\t\t\t...record,\n\t\t\t\t\tposition,\n\t\t\t\t};\n\t\t\t} );\n\t\t\tallLoadedRecordsRef.current = records;\n\t\t\treturn records;\n\t\t}\n\n\t\t// Subsequent pages - merge with existing data\n\t\tconst prev = allLoadedRecordsRef.current;\n\t\tconst shownDataIds = new Set( shownData.map( getItemId ) );\n\t\tconst scrollDirection = scrollDirectionRef.current;\n\n\t\t// The position for each item in shownData should be based on the\n\t\t// current startPosition from the view, which reflects the actual\n\t\t// offset in the dataset. This ensures aria-posinset values are\n\t\t// semantically correct - if startPosition is 40, there are exactly\n\t\t// 39 items before the first item in shownData.\n\t\t// When there's an active search, always start from position 1 since\n\t\t// search results are a filtered subset, not a paginated view.\n\t\tconst basePosition = view.search ? 1 : view.startPosition ?? 1;\n\t\tconst newRecords = shownData.map( ( record, index ) => {\n\t\t\tconst itemId = getItemId( record );\n\t\t\tconst position = view.infiniteScrollEnabled\n\t\t\t\t? basePosition + index\n\t\t\t\t: undefined;\n\n\t\t\t// Always update the position map with the correct position\n\t\t\t// based on the current query's startPosition\n\t\t\tif ( position !== undefined ) {\n\t\t\t\tpositionMapRef.current.set( itemId, position );\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\t...record,\n\t\t\t\tposition,\n\t\t\t};\n\t\t} );\n\n\t\tif ( newRecords.length === 0 ) {\n\t\t\treturn prev;\n\t\t}\n\n\t\t// Remove duplicates from prev, keeping only records not in shownData\n\t\tconst prevWithoutDuplicates = prev.filter(\n\t\t\t( record ) => ! shownDataIds.has( getItemId( record ) )\n\t\t);\n\n\t\t// Update the loaded range\n\t\tconst allRecords =\n\t\t\tscrollDirection === 'up'\n\t\t\t\t? [ ...newRecords, ...prevWithoutDuplicates ]\n\t\t\t\t: [ ...prevWithoutDuplicates, ...newRecords ];\n\n\t\t// Sort all records by position to ensure correct order\n\t\t// This is crucial when items are reloaded after scrolling in different directions\n\t\tallRecords.sort( ( a, b ) => {\n\t\t\tconst posA = ( a as Item & { position: number } ).position;\n\t\t\tconst posB = ( b as Item & { position: number } ).position;\n\t\t\treturn posA - posB;\n\t\t} );\n\n\t\tlet result = allRecords;\n\n\t\tif ( visibleEntries.length > 0 ) {\n\t\t\tconst visibleMin = Math.min( ...visibleEntries );\n\t\t\tconst visibleMax = Math.max( ...visibleEntries );\n\t\t\t// Buffer size balances allowing new items to render (when prepended\n\t\t\t// during scroll up) while unloading items no longer on screen.\n\t\t\t// Use a larger buffer to prevent scrollbar from jumping when items\n\t\t\t// are unloaded, which could trigger unwanted scroll events.\n\t\t\tconst buffer = 20;\n\n\t\t\tconst recordPositions = allRecords.map(\n\t\t\t\t( r ) => ( r as Item & { position: number } ).position\n\t\t\t);\n\t\t\tconst minRecordPos = Math.min( ...recordPositions );\n\t\t\tconst maxRecordPos = Math.max( ...recordPositions );\n\n\t\t\t// Check if there's any overlap between visible range and actual record positions\n\t\t\t// to avoid filtering when visibleEntries are stale (e.g., after search/filter reset)\n\t\t\tconst hasOverlap = ! (\n\t\t\t\tmaxRecordPos < visibleMin - buffer ||\n\t\t\t\tminRecordPos > visibleMax + buffer\n\t\t\t);\n\n\t\t\tif ( hasOverlap ) {\n\t\t\t\tresult = allRecords.filter( ( record ) => {\n\t\t\t\t\tconst itemId = getItemId( record );\n\t\t\t\t\tconst isSelected = selection?.includes( itemId );\n\t\t\t\t\t// Never unload selected items, even if outside visible range\n\t\t\t\t\tif ( isSelected ) {\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t}\n\n\t\t\t\t\tconst itemPosition = (\n\t\t\t\t\t\trecord as Item & { position: number }\n\t\t\t\t\t ).position;\n\t\t\t\t\t// When scrolling, only trim items from the end we're scrolling away from\n\t\t\t\t\tif ( scrollDirection === 'up' ) {\n\t\t\t\t\t\t// When scrolling up, only trim items below the visible range\n\t\t\t\t\t\treturn itemPosition <= visibleMax + buffer;\n\t\t\t\t\t} else if ( scrollDirection === 'down' ) {\n\t\t\t\t\t\t// When scrolling down, only trim items above the visible range\n\t\t\t\t\t\treturn itemPosition >= visibleMin - buffer;\n\t\t\t\t\t}\n\t\t\t\t\t// When not scrolling or first load, keep items within buffer range\n\t\t\t\t\treturn (\n\t\t\t\t\t\titemPosition >= visibleMin - buffer &&\n\t\t\t\t\t\titemPosition <= visibleMax + buffer\n\t\t\t\t\t);\n\t\t\t\t} );\n\t\t\t}\n\t\t}\n\n\t\tallLoadedRecordsRef.current = result;\n\t\treturn result;\n\t}, [\n\t\tshownData,\n\t\tview.search,\n\t\tview.filters,\n\t\tview.perPage,\n\t\tview.startPosition,\n\t\tview.infiniteScrollEnabled,\n\t\tvisibleEntries,\n\t\tselection,\n\t\tgetItemId,\n\t] );\n\n\t// When infinite scroll is disabled, preserve previous data while loading\n\tif ( ! isInfiniteScrollEnabled ) {\n\t\tconst dataToReturn =\n\t\t\tisLoading && previousDataRef.current?.length\n\t\t\t\t? previousDataRef.current\n\t\t\t\t: shownData;\n\t\treturn {\n\t\t\tdata: dataToReturn.map( ( item ) => ( {\n\t\t\t\t...item,\n\t\t\t\tposition: undefined,\n\t\t\t} ) ) as ( Item & { position?: number } )[],\n\t\t\tpaginationInfo:\n\t\t\t\tisLoading && previousDataRef.current?.length\n\t\t\t\t\t? previousPaginationInfoRef.current\n\t\t\t\t\t: paginationInfo,\n\t\t\thasInitiallyLoaded,\n\t\t\tsetVisibleEntries: undefined,\n\t\t};\n\t}\n\n\treturn {\n\t\tdata: allLoadedRecords as ( Item & { position?: number } )[],\n\t\tpaginationInfo,\n\t\thasInitiallyLoaded,\n\t\tsetVisibleEntries,\n\t};\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAAqD;AAmDtC,SAAR,QAAkC;AAAA,EACxC;AAAA,EACA,MAAM;AAAA,EACN;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,GAAkD;AACjD,QAAM,0BAA0B,KAAK;AAErC,QAAM,CAAE,oBAAoB,qBAAsB,QAAI;AAAA,IACrD,CAAE;AAAA,EACH;AACA,gCAAW,MAAM;AAChB,QAAK,CAAE,WAAY;AAClB,4BAAuB,IAAK;AAAA,IAC7B;AAAA,EACD,GAAG,CAAE,SAAU,CAAE;AAEjB,QAAM,sBAAkB,uBAAkB,SAAU;AACpD,QAAM,gCACL,uBAA0B,cAAe;AAC1C,gCAAW,MAAM;AAChB,QAAK,CAAE,WAAY;AAClB,sBAAgB,UAAU;AAC1B,gCAA0B,UAAU;AAAA,IACrC;AAAA,EACD,GAAG,CAAE,WAAW,WAAW,cAAe,CAAE;AAG5C,QAAM,CAAE,gBAAgB,iBAAkB,QAAI,yBAAsB,CAAC,CAAE;AAGvE,QAAM,qBAAiB,uBAAiC,oBAAI,IA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AAY;AACzC,gBAAM,SAAS,UAAW,MAAO;AACjC,gBAAM,aAAa,WAAW,SAAU,MAAO;AAE/C,cAAK,YAAa;AACjB,mBAAO;AAAA,UACR;AAEA,gBAAM,eACL,OACE;AAEH,cAAK,oBAAoB,MAAO;AAE/B,mBAAO,gBAAgB,aAAa;AAAA,UACrC,WAAY,oBAAoB,QAAS;AAExC,mBAAO,gBAAgB,aAAa;AAAA,UACrC;AAEA,iBACC,gBAAgB,aAAa,UAC7B,gBAAgB,aAAa;AAAA,QAE/B,CAAE;AAAA,MACH;AAAA,IACD;AAEA,wBAAoB,UAAU;AAC9B,WAAO;AAAA,EACR,GAAG;AAAA,IACF;AAAA,IACA,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,EACD,CAAE;AAGF,MAAK,CAAE,yBAA0B;AAChC,UAAM,eACL,aAAa,gBAAgB,SAAS,SACnC,gBAAgB,UAChB;AACJ,WAAO;AAAA,MACN,MAAM,aAAa,IAAK,CAAE,UAAY;AAAA,QACrC,GAAG;AAAA,QACH,UAAU;AAAA,MACX,EAAI;AAAA,MACJ,gBACC,aAAa,gBAAgB,SAAS,SACnC,0BAA0B,UAC1B;AAAA,MACJ;AAAA,MACA,mBAAmB;AAAA,IACpB;AAAA,EACD;AAEA,SAAO;AAAA,IACN,MAAM;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1,208 @@
|
|
|
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/dataviews/src/hooks/use-infinite-scroll.ts
|
|
21
|
+
var use_infinite_scroll_exports = {};
|
|
22
|
+
__export(use_infinite_scroll_exports, {
|
|
23
|
+
useInfiniteScroll: () => useInfiniteScroll
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(use_infinite_scroll_exports);
|
|
26
|
+
var import_element = require("@wordpress/element");
|
|
27
|
+
var import_compose = require("@wordpress/compose");
|
|
28
|
+
function captureAnchorElement(container, anchorElementRef, direction) {
|
|
29
|
+
const containerRect = container.getBoundingClientRect();
|
|
30
|
+
const centerY = containerRect.top + containerRect.height / 2;
|
|
31
|
+
const items = Array.from(container.querySelectorAll("[aria-posinset]"));
|
|
32
|
+
if (items.length === 0) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
const bestAnchor = items.reduce((best, item) => {
|
|
36
|
+
const itemRect = item.getBoundingClientRect();
|
|
37
|
+
const itemCenterY = itemRect.top + itemRect.height / 2;
|
|
38
|
+
const distance = Math.abs(itemCenterY - centerY);
|
|
39
|
+
const bestRect = best.getBoundingClientRect();
|
|
40
|
+
const bestCenterY = bestRect.top + bestRect.height / 2;
|
|
41
|
+
const bestDistance = Math.abs(bestCenterY - centerY);
|
|
42
|
+
return distance < bestDistance ? item : best;
|
|
43
|
+
});
|
|
44
|
+
const posinset = Number(bestAnchor.getAttribute("aria-posinset"));
|
|
45
|
+
const anchorRect = bestAnchor.getBoundingClientRect();
|
|
46
|
+
anchorElementRef.current = {
|
|
47
|
+
posinset,
|
|
48
|
+
viewportOffset: anchorRect.top - containerRect.top,
|
|
49
|
+
direction
|
|
50
|
+
};
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
function useInfiniteScroll({
|
|
54
|
+
view,
|
|
55
|
+
onChangeView,
|
|
56
|
+
isLoading,
|
|
57
|
+
paginationInfo,
|
|
58
|
+
containerRef,
|
|
59
|
+
setVisibleEntries
|
|
60
|
+
}) {
|
|
61
|
+
const anchorElementRef = (0, import_element.useRef)(null);
|
|
62
|
+
const viewRef = (0, import_element.useRef)(view);
|
|
63
|
+
const isLoadingRef = (0, import_element.useRef)(isLoading);
|
|
64
|
+
const onChangeViewRef = (0, import_element.useRef)(onChangeView);
|
|
65
|
+
const totalItemsRef = (0, import_element.useRef)(paginationInfo.totalItems);
|
|
66
|
+
(0, import_element.useLayoutEffect)(() => {
|
|
67
|
+
viewRef.current = view;
|
|
68
|
+
isLoadingRef.current = isLoading;
|
|
69
|
+
onChangeViewRef.current = onChangeView;
|
|
70
|
+
totalItemsRef.current = paginationInfo.totalItems;
|
|
71
|
+
}, [view, isLoading, onChangeView, paginationInfo.totalItems]);
|
|
72
|
+
const intersectionObserverCallback = (0, import_element.useCallback)(
|
|
73
|
+
(entries) => {
|
|
74
|
+
if (!setVisibleEntries) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
setVisibleEntries((prev) => {
|
|
78
|
+
const newVisibleEntries = new Set(prev);
|
|
79
|
+
let hasChanged = false;
|
|
80
|
+
entries.forEach((entry) => {
|
|
81
|
+
const posInSet = Number(
|
|
82
|
+
entry.target?.attributes?.getNamedItem(
|
|
83
|
+
"aria-posinset"
|
|
84
|
+
)?.value
|
|
85
|
+
);
|
|
86
|
+
if (isNaN(posInSet)) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (entry.isIntersecting) {
|
|
90
|
+
if (!newVisibleEntries.has(posInSet)) {
|
|
91
|
+
newVisibleEntries.add(posInSet);
|
|
92
|
+
hasChanged = true;
|
|
93
|
+
}
|
|
94
|
+
} else if (newVisibleEntries.has(posInSet)) {
|
|
95
|
+
newVisibleEntries.delete(posInSet);
|
|
96
|
+
hasChanged = true;
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
return hasChanged ? Array.from(newVisibleEntries).sort() : prev;
|
|
100
|
+
});
|
|
101
|
+
},
|
|
102
|
+
[setVisibleEntries]
|
|
103
|
+
);
|
|
104
|
+
(0, import_element.useLayoutEffect)(() => {
|
|
105
|
+
const container = containerRef.current;
|
|
106
|
+
const anchor = anchorElementRef.current;
|
|
107
|
+
if (!container || !view.infiniteScrollEnabled || !anchor || isLoading) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const anchorElement = container.querySelector(
|
|
111
|
+
`[aria-posinset="${anchor.posinset}"]`
|
|
112
|
+
);
|
|
113
|
+
if (anchorElement) {
|
|
114
|
+
const containerRect = container.getBoundingClientRect();
|
|
115
|
+
const anchorRect = anchorElement.getBoundingClientRect();
|
|
116
|
+
const currentOffset = anchorRect.top - containerRect.top;
|
|
117
|
+
const scrollAdjustment = currentOffset - anchor.viewportOffset;
|
|
118
|
+
if (Math.abs(scrollAdjustment) > 1) {
|
|
119
|
+
container.scrollTop += scrollAdjustment;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
anchorElementRef.current = null;
|
|
123
|
+
}, [containerRef, isLoading, view.infiniteScrollEnabled]);
|
|
124
|
+
const intersectionObserverRef = (0, import_element.useRef)(
|
|
125
|
+
null
|
|
126
|
+
);
|
|
127
|
+
(0, import_element.useEffect)(() => {
|
|
128
|
+
if (!view.infiniteScrollEnabled || !intersectionObserverCallback) {
|
|
129
|
+
if (intersectionObserverRef.current) {
|
|
130
|
+
intersectionObserverRef.current.disconnect();
|
|
131
|
+
intersectionObserverRef.current = null;
|
|
132
|
+
}
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
intersectionObserverRef.current = new IntersectionObserver(
|
|
136
|
+
intersectionObserverCallback,
|
|
137
|
+
{ root: null, rootMargin: "0px", threshold: 0.1 }
|
|
138
|
+
);
|
|
139
|
+
return () => {
|
|
140
|
+
if (intersectionObserverRef.current) {
|
|
141
|
+
intersectionObserverRef.current.disconnect();
|
|
142
|
+
intersectionObserverRef.current = null;
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}, [view.infiniteScrollEnabled, intersectionObserverCallback]);
|
|
146
|
+
(0, import_element.useEffect)(() => {
|
|
147
|
+
if (!view.infiniteScrollEnabled || !containerRef.current) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
let lastScrollTop = 0;
|
|
151
|
+
const BOTTOM_THRESHOLD = 600;
|
|
152
|
+
const TOP_THRESHOLD = 800;
|
|
153
|
+
const handleScroll = (0, import_compose.throttle)((event) => {
|
|
154
|
+
const currentView = viewRef.current;
|
|
155
|
+
const totalItems = totalItemsRef.current;
|
|
156
|
+
const target = event.target;
|
|
157
|
+
const scrollTop = target.scrollTop;
|
|
158
|
+
const scrollHeight = target.scrollHeight;
|
|
159
|
+
const clientHeight = target.clientHeight;
|
|
160
|
+
const scrollDirection = scrollTop > lastScrollTop ? "down" : "up";
|
|
161
|
+
lastScrollTop = scrollTop;
|
|
162
|
+
if (isLoadingRef.current) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const currentStartPosition = currentView.startPosition || 1;
|
|
166
|
+
const batchSize = currentView.perPage || 10;
|
|
167
|
+
const currentEndPosition = Math.min(
|
|
168
|
+
currentStartPosition + batchSize,
|
|
169
|
+
totalItems
|
|
170
|
+
);
|
|
171
|
+
if (scrollDirection === "down" && scrollTop + clientHeight >= scrollHeight - BOTTOM_THRESHOLD) {
|
|
172
|
+
if (currentEndPosition < totalItems) {
|
|
173
|
+
const newStartPosition = currentEndPosition;
|
|
174
|
+
captureAnchorElement(target, anchorElementRef, "down");
|
|
175
|
+
onChangeViewRef.current({
|
|
176
|
+
...currentView,
|
|
177
|
+
startPosition: newStartPosition
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (scrollDirection === "up" && scrollTop <= TOP_THRESHOLD) {
|
|
182
|
+
if (currentStartPosition > 1) {
|
|
183
|
+
const calculatedStartPosition = currentStartPosition - batchSize;
|
|
184
|
+
const newStartPosition = calculatedStartPosition < 6 ? 1 : calculatedStartPosition;
|
|
185
|
+
captureAnchorElement(target, anchorElementRef, "up");
|
|
186
|
+
onChangeViewRef.current({
|
|
187
|
+
...currentView,
|
|
188
|
+
startPosition: newStartPosition
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}, 50);
|
|
193
|
+
const container = containerRef.current;
|
|
194
|
+
container.addEventListener("scroll", handleScroll);
|
|
195
|
+
return () => {
|
|
196
|
+
container.removeEventListener("scroll", handleScroll);
|
|
197
|
+
handleScroll.cancel();
|
|
198
|
+
};
|
|
199
|
+
}, [containerRef, view.infiniteScrollEnabled]);
|
|
200
|
+
return {
|
|
201
|
+
intersectionObserver: intersectionObserverRef.current
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
205
|
+
0 && (module.exports = {
|
|
206
|
+
useInfiniteScroll
|
|
207
|
+
});
|
|
208
|
+
//# sourceMappingURL=use-infinite-scroll.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/hooks/use-infinite-scroll.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {\n\tuseCallback,\n\tuseEffect,\n\tuseLayoutEffect,\n\tuseRef,\n} from '@wordpress/element';\nimport { throttle } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport type { View } from '../types';\n\n/**\n * Captures an anchor element for scroll position preservation.\n * Finds the element closest to the center of the viewport and stores its position.\n *\n * @param container The scrollable container element.\n * @param anchorElementRef Ref to store the anchor element data.\n * @param direction The scroll direction ('up' or 'down').\n * @return Whether an anchor element was successfully captured.\n */\nfunction captureAnchorElement(\n\tcontainer: HTMLElement,\n\tanchorElementRef: React.MutableRefObject< {\n\t\tposinset: number;\n\t\tviewportOffset: number;\n\t\tdirection: 'up' | 'down' | null;\n\t} | null >,\n\tdirection: 'up' | 'down'\n): boolean {\n\t// Find a visible element to use as anchor - prefer one in the middle of the viewport\n\tconst containerRect = container.getBoundingClientRect();\n\tconst centerY = containerRect.top + containerRect.height / 2;\n\n\t// Query all items with aria-posinset and find the one closest to center\n\tconst items = Array.from( container.querySelectorAll( '[aria-posinset]' ) );\n\n\tif ( items.length === 0 ) {\n\t\treturn false;\n\t}\n\n\t// Find the item closest to the center of the viewport\n\tconst bestAnchor = items.reduce( ( best, item ) => {\n\t\tconst itemRect = item.getBoundingClientRect();\n\t\tconst itemCenterY = itemRect.top + itemRect.height / 2;\n\t\tconst distance = Math.abs( itemCenterY - centerY );\n\n\t\tconst bestRect = best.getBoundingClientRect();\n\t\tconst bestCenterY = bestRect.top + bestRect.height / 2;\n\t\tconst bestDistance = Math.abs( bestCenterY - centerY );\n\n\t\treturn distance < bestDistance ? item : best;\n\t} );\n\n\tconst posinset = Number( bestAnchor.getAttribute( 'aria-posinset' ) );\n\tconst anchorRect = bestAnchor.getBoundingClientRect();\n\tanchorElementRef.current = {\n\t\tposinset,\n\t\tviewportOffset: anchorRect.top - containerRect.top,\n\t\tdirection,\n\t};\n\treturn true;\n}\n\ntype UseInfiniteScrollProps = {\n\tview: View;\n\tonChangeView: ( view: View ) => void;\n\tisLoading: boolean;\n\tpaginationInfo: {\n\t\ttotalItems: number;\n\t\ttotalPages: number;\n\t};\n\tcontainerRef: React.MutableRefObject< HTMLDivElement | null >;\n\tsetVisibleEntries?: React.Dispatch< React.SetStateAction< number[] > >;\n};\n\ntype UseInfiniteScrollResult = {\n\tintersectionObserver?: IntersectionObserver | null;\n};\n\nexport function useInfiniteScroll( {\n\tview,\n\tonChangeView,\n\tisLoading,\n\tpaginationInfo,\n\tcontainerRef,\n\tsetVisibleEntries,\n}: UseInfiniteScrollProps ): UseInfiniteScrollResult {\n\t// Track an anchor element for scroll position preservation\n\t// This approach is robust even when items are added/removed from both ends simultaneously\n\tconst anchorElementRef = useRef< {\n\t\tposinset: number;\n\t\tviewportOffset: number;\n\t\tdirection: 'up' | 'down' | null;\n\t} | null >( null );\n\tconst viewRef = useRef( view );\n\tconst isLoadingRef = useRef( isLoading );\n\tconst onChangeViewRef = useRef( onChangeView );\n\tconst totalItemsRef = useRef( paginationInfo.totalItems );\n\n\tuseLayoutEffect( () => {\n\t\tviewRef.current = view;\n\t\tisLoadingRef.current = isLoading;\n\t\tonChangeViewRef.current = onChangeView;\n\t\ttotalItemsRef.current = paginationInfo.totalItems;\n\t}, [ view, isLoading, onChangeView, paginationInfo.totalItems ] );\n\n\tconst intersectionObserverCallback: IntersectionObserverCallback =\n\t\tuseCallback(\n\t\t\t( entries: IntersectionObserverEntry[] ) => {\n\t\t\t\t// Calculate new visible entries outside of setState\n\t\t\t\tif ( ! setVisibleEntries ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tsetVisibleEntries( ( prev: number[] ) => {\n\t\t\t\t\tconst newVisibleEntries = new Set( prev );\n\t\t\t\t\tlet hasChanged = false;\n\n\t\t\t\t\tentries.forEach( ( entry ) => {\n\t\t\t\t\t\tconst posInSet = Number(\n\t\t\t\t\t\t\tentry.target?.attributes?.getNamedItem(\n\t\t\t\t\t\t\t\t'aria-posinset'\n\t\t\t\t\t\t\t)?.value\n\t\t\t\t\t\t);\n\t\t\t\t\t\tif ( isNaN( posInSet ) ) {\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif ( entry.isIntersecting ) {\n\t\t\t\t\t\t\tif ( ! newVisibleEntries.has( posInSet ) ) {\n\t\t\t\t\t\t\t\tnewVisibleEntries.add( posInSet );\n\t\t\t\t\t\t\t\thasChanged = true;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else if ( newVisibleEntries.has( posInSet ) ) {\n\t\t\t\t\t\t\tnewVisibleEntries.delete( posInSet );\n\t\t\t\t\t\t\thasChanged = true;\n\t\t\t\t\t\t}\n\t\t\t\t\t} );\n\n\t\t\t\t\t// Only return new array if something actually changed\n\t\t\t\t\treturn hasChanged\n\t\t\t\t\t\t? Array.from( newVisibleEntries ).sort()\n\t\t\t\t\t\t: prev;\n\t\t\t\t} );\n\t\t\t},\n\t\t\t[ setVisibleEntries ]\n\t\t);\n\n\t// Preserve scroll position when items are added or removed during infinite scroll\n\t// Uses anchor element approach: find the same element after render and restore its viewport position\n\tuseLayoutEffect( () => {\n\t\tconst container = containerRef.current;\n\t\tconst anchor = anchorElementRef.current;\n\n\t\tif (\n\t\t\t! container ||\n\t\t\t! view.infiniteScrollEnabled ||\n\t\t\t! anchor ||\n\t\t\tisLoading\n\t\t) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Find the anchor element by its posinset\n\t\tconst anchorElement = container.querySelector(\n\t\t\t`[aria-posinset=\"${ anchor.posinset }\"]`\n\t\t);\n\n\t\tif ( anchorElement ) {\n\t\t\tconst containerRect = container.getBoundingClientRect();\n\t\t\tconst anchorRect = anchorElement.getBoundingClientRect();\n\t\t\tconst currentOffset = anchorRect.top - containerRect.top;\n\n\t\t\t// Calculate how much the anchor has moved and adjust scroll to compensate\n\t\t\tconst scrollAdjustment = currentOffset - anchor.viewportOffset;\n\n\t\t\tif ( Math.abs( scrollAdjustment ) > 1 ) {\n\t\t\t\tcontainer.scrollTop += scrollAdjustment;\n\t\t\t}\n\t\t}\n\n\t\t// Reset the anchor state now that we've adjusted\n\t\tanchorElementRef.current = null;\n\t}, [ containerRef, isLoading, view.infiniteScrollEnabled ] );\n\n\t// Create and expose a shared IntersectionObserver for provider-level reuse.\n\tconst intersectionObserverRef = useRef< IntersectionObserver | null >(\n\t\tnull\n\t);\n\tuseEffect( () => {\n\t\tif ( ! view.infiniteScrollEnabled || ! intersectionObserverCallback ) {\n\t\t\tif ( intersectionObserverRef.current ) {\n\t\t\t\tintersectionObserverRef.current.disconnect();\n\t\t\t\tintersectionObserverRef.current = null;\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tintersectionObserverRef.current = new IntersectionObserver(\n\t\t\tintersectionObserverCallback,\n\t\t\t{ root: null, rootMargin: '0px', threshold: 0.1 }\n\t\t);\n\n\t\treturn () => {\n\t\t\tif ( intersectionObserverRef.current ) {\n\t\t\t\tintersectionObserverRef.current.disconnect();\n\t\t\t\tintersectionObserverRef.current = null;\n\t\t\t}\n\t\t};\n\t}, [ view.infiniteScrollEnabled, intersectionObserverCallback ] );\n\n\t// Attach scroll event listener for infinite scroll\n\tuseEffect( () => {\n\t\tif ( ! view.infiniteScrollEnabled || ! containerRef.current ) {\n\t\t\treturn;\n\t\t}\n\n\t\tlet lastScrollTop = 0;\n\t\t// Use larger thresholds to trigger loading earlier during fast scrolling\n\t\tconst BOTTOM_THRESHOLD = 600; // px from bottom to trigger load\n\t\tconst TOP_THRESHOLD = 800; // px from top to trigger load\n\n\t\tconst handleScroll = throttle( ( event: unknown ) => {\n\t\t\tconst currentView = viewRef.current;\n\t\t\tconst totalItems = totalItemsRef.current;\n\t\t\tconst target = ( event as Event ).target as HTMLElement;\n\t\t\tconst scrollTop = target.scrollTop;\n\t\t\tconst scrollHeight = target.scrollHeight;\n\t\t\tconst clientHeight = target.clientHeight;\n\n\t\t\t// Determine scroll direction\n\t\t\tconst scrollDirection = scrollTop > lastScrollTop ? 'down' : 'up';\n\t\t\tlastScrollTop = scrollTop;\n\n\t\t\t// Don't trigger if already loading\n\t\t\tif ( isLoadingRef.current ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst currentStartPosition = currentView.startPosition || 1;\n\t\t\tconst batchSize = currentView.perPage || 10;\n\t\t\tconst currentEndPosition = Math.min(\n\t\t\t\tcurrentStartPosition + batchSize,\n\t\t\t\ttotalItems\n\t\t\t);\n\n\t\t\t// Check if user has scrolled near the bottom\n\t\t\tif (\n\t\t\t\tscrollDirection === 'down' &&\n\t\t\t\tscrollTop + clientHeight >= scrollHeight - BOTTOM_THRESHOLD\n\t\t\t) {\n\t\t\t\t// Check if there's more data to load\n\t\t\t\tif ( currentEndPosition < totalItems ) {\n\t\t\t\t\tconst newStartPosition = currentEndPosition;\n\n\t\t\t\t\t// Capture anchor element for scroll position preservation\n\t\t\t\t\tcaptureAnchorElement( target, anchorElementRef, 'down' );\n\n\t\t\t\t\tonChangeViewRef.current( {\n\t\t\t\t\t\t...currentView,\n\t\t\t\t\t\tstartPosition: newStartPosition,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Check if user has scrolled near the top\n\t\t\tif ( scrollDirection === 'up' && scrollTop <= TOP_THRESHOLD ) {\n\t\t\t\t// Check if there's more data to load\n\t\t\t\tif ( currentStartPosition > 1 ) {\n\t\t\t\t\t// Round to 1 if we're close to the beginning to avoid tiny batches\n\t\t\t\t\tconst calculatedStartPosition =\n\t\t\t\t\t\tcurrentStartPosition - batchSize;\n\t\t\t\t\tconst newStartPosition =\n\t\t\t\t\t\tcalculatedStartPosition < 6\n\t\t\t\t\t\t\t? 1\n\t\t\t\t\t\t\t: calculatedStartPosition;\n\n\t\t\t\t\t// Capture anchor element for scroll position preservation\n\t\t\t\t\tcaptureAnchorElement( target, anchorElementRef, 'up' );\n\n\t\t\t\t\tonChangeViewRef.current( {\n\t\t\t\t\t\t...currentView,\n\t\t\t\t\t\tstartPosition: newStartPosition,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t}\n\t\t}, 50 ); // Faster throttle (50ms) for better response to fast scrolling\n\n\t\tconst container = containerRef.current;\n\t\tcontainer.addEventListener( 'scroll', handleScroll );\n\n\t\treturn () => {\n\t\t\tcontainer.removeEventListener( 'scroll', handleScroll );\n\t\t\thandleScroll.cancel(); // Cancel any pending throttled calls\n\t\t};\n\t}, [ containerRef, view.infiniteScrollEnabled ] );\n\n\treturn {\n\t\tintersectionObserver: intersectionObserverRef.current,\n\t};\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAKO;AACP,qBAAyB;AAgBzB,SAAS,qBACR,WACA,kBAKA,WACU;AAEV,QAAM,gBAAgB,UAAU,sBAAsB;AACtD,QAAM,UAAU,cAAc,MAAM,cAAc,SAAS;AAG3D,QAAM,QAAQ,MAAM,KAAM,UAAU,iBAAkB,iBAAkB,CAAE;AAE1E,MAAK,MAAM,WAAW,GAAI;AACzB,WAAO;AAAA,EACR;AAGA,QAAM,aAAa,MAAM,OAAQ,CAAE,MAAM,SAAU;AAClD,UAAM,WAAW,KAAK,sBAAsB;AAC5C,UAAM,cAAc,SAAS,MAAM,SAAS,SAAS;AACrD,UAAM,WAAW,KAAK,IAAK,cAAc,OAAQ;AAEjD,UAAM,WAAW,KAAK,sBAAsB;AAC5C,UAAM,cAAc,SAAS,MAAM,SAAS,SAAS;AACrD,UAAM,eAAe,KAAK,IAAK,cAAc,OAAQ;AAErD,WAAO,WAAW,eAAe,OAAO;AAAA,EACzC,CAAE;AAEF,QAAM,WAAW,OAAQ,WAAW,aAAc,eAAgB,CAAE;AACpE,QAAM,aAAa,WAAW,sBAAsB;AACpD,mBAAiB,UAAU;AAAA,IAC1B;AAAA,IACA,gBAAgB,WAAW,MAAM,cAAc;AAAA,IAC/C;AAAA,EACD;AACA,SAAO;AACR;AAkBO,SAAS,kBAAmB;AAAA,EAClC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,GAAqD;AAGpD,QAAM,uBAAmB,uBAIb,IAAK;AACjB,QAAM,cAAU,uBAAQ,IAAK;AAC7B,QAAM,mBAAe,uBAAQ,SAAU;AACvC,QAA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AAY,WAAW;AACzC,YAAM,qBAAqB,KAAK;AAAA,QAC/B,uBAAuB;AAAA,QACvB;AAAA,MACD;AAGA,UACC,oBAAoB,UACpB,YAAY,gBAAgB,eAAe,kBAC1C;AAED,YAAK,qBAAqB,YAAa;AACtC,gBAAM,mBAAmB;AAGzB,+BAAsB,QAAQ,kBAAkB,MAAO;AAEvD,0BAAgB,QAAS;AAAA,YACxB,GAAG;AAAA,YACH,eAAe;AAAA,UAChB,CAAE;AAAA,QACH;AAAA,MACD;AAGA,UAAK,oBAAoB,QAAQ,aAAa,eAAgB;AAE7D,YAAK,uBAAuB,GAAI;AAE/B,gBAAM,0BACL,uBAAuB;AACxB,gBAAM,mBACL,0BAA0B,IACvB,IACA;AAGJ,+BAAsB,QAAQ,kBAAkB,IAAK;AAErD,0BAAgB,QAAS;AAAA,YACxB,GAAG;AAAA,YACH,eAAe;AAAA,UAChB,CAAE;AAAA,QACH;AAAA,MACD;AAAA,IACD,GAAG,EAAG;AAEN,UAAM,YAAY,aAAa;AAC/B,cAAU,iBAAkB,UAAU,YAAa;AAEnD,WAAO,MAAM;AACZ,gBAAU,oBAAqB,UAAU,YAAa;AACtD,mBAAa,OAAO;AAAA,IACrB;AAAA,EACD,GAAG,CAAE,cAAc,KAAK,qBAAsB,CAAE;AAEhD,SAAO;AAAA,IACN,sBAAsB,wBAAwB;AAAA,EAC/C;AACD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
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/dataviews/src/hooks/use-selected-items.ts
|
|
21
|
+
var use_selected_items_exports = {};
|
|
22
|
+
__export(use_selected_items_exports, {
|
|
23
|
+
default: () => useSelectedItems
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(use_selected_items_exports);
|
|
26
|
+
var import_element = require("@wordpress/element");
|
|
27
|
+
function useSelectedItems(view, data, selection, getItemId, filterFn) {
|
|
28
|
+
const selectedItemsCacheRef = (0, import_element.useRef)(/* @__PURE__ */ new Map());
|
|
29
|
+
return (0, import_element.useMemo)(() => {
|
|
30
|
+
const selectionSet = new Set(selection);
|
|
31
|
+
if (view.infiniteScrollEnabled) {
|
|
32
|
+
data.forEach((item) => {
|
|
33
|
+
const id = getItemId(item);
|
|
34
|
+
if (selectionSet.has(id)) {
|
|
35
|
+
const passesFilter = filterFn ? filterFn(item) : true;
|
|
36
|
+
if (passesFilter) {
|
|
37
|
+
selectedItemsCacheRef.current.set(id, item);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
selectedItemsCacheRef.current.forEach((_, id) => {
|
|
42
|
+
if (!selectionSet.has(id)) {
|
|
43
|
+
selectedItemsCacheRef.current.delete(id);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
return Array.from(selectedItemsCacheRef.current.values());
|
|
47
|
+
}
|
|
48
|
+
return data.filter((item) => {
|
|
49
|
+
const id = getItemId(item);
|
|
50
|
+
if (!selectionSet.has(id)) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return filterFn ? filterFn(item) : true;
|
|
54
|
+
});
|
|
55
|
+
}, [view.infiniteScrollEnabled, selection, getItemId, data, filterFn]);
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=use-selected-items.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/hooks/use-selected-items.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useMemo, useRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport type { View } from '../types';\n\n/**\n * Hook to get selected items, with support for infinite scroll.\n *\n * When infinite scroll is enabled, items that scroll out of view are cached\n * so they remain available for bulk actions even when not in the current data set.\n *\n * @param view The current view configuration.\n * @param data The current page of data items.\n * @param selection Array of selected item IDs.\n * @param getItemId Function to get the ID of an item.\n * @param filterFn Optional filter function to apply to selected items (e.g., for selectability).\n * @return Array of selected items.\n */\nexport default function useSelectedItems< Item >(\n\tview: View,\n\tdata: Item[],\n\tselection: string[],\n\tgetItemId: ( item: Item ) => string,\n\tfilterFn?: ( item: Item ) => boolean\n): Item[] {\n\t// With infinite scroll enabled items scroll out of view, but we want to keep the selection unaltered,\n\t// unlike page-based navigation where we might clear selection upon navigating to a different page.\n\tconst selectedItemsCacheRef = useRef< Map< string, Item > >( new Map() );\n\n\treturn useMemo( () => {\n\t\tconst selectionSet = new Set( selection );\n\n\t\tif ( view.infiniteScrollEnabled ) {\n\t\t\t// Selection array contains selected item IDs\n\t\t\t// Cache selected items so they remain available when scrolled out of view\n\t\t\tdata.forEach( ( item ) => {\n\t\t\t\tconst id = getItemId( item );\n\t\t\t\tif ( selectionSet.has( id ) ) {\n\t\t\t\t\tconst passesFilter = filterFn ? filterFn( item ) : true;\n\t\t\t\t\tif ( passesFilter ) {\n\t\t\t\t\t\tselectedItemsCacheRef.current.set( id, item );\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} );\n\n\t\t\t// Remove items from cache that are no longer selected\n\t\t\tselectedItemsCacheRef.current.forEach( ( _, id ) => {\n\t\t\t\tif ( ! selectionSet.has( id ) ) {\n\t\t\t\t\tselectedItemsCacheRef.current.delete( id );\n\t\t\t\t}\n\t\t\t} );\n\n\t\t\t// Return all cached selected items\n\t\t\treturn Array.from( selectedItemsCacheRef.current.values() );\n\t\t}\n\n\t\t// Non-infinite scroll mode\n\t\treturn data.filter( ( item ) => {\n\t\t\tconst id = getItemId( item );\n\t\t\tif ( ! selectionSet.has( id ) ) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\t// Apply optional filter (e.g., selectability check for bulk actions)\n\t\t\treturn filterFn ? filterFn( item ) : true;\n\t\t} );\n\t}, [ view.infiniteScrollEnabled, selection, getItemId, data, filterFn ] );\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAAgC;AAoBjB,SAAR,iBACN,MACA,MACA,WACA,WACA,UACS;AAGT,QAAM,4BAAwB,uBAA+B,oBAAI,IAAI,CAAE;AAEvE,aAAO,wBAAS,MAAM;AACrB,UAAM,eAAe,IAAI,IAAK,SAAU;AAExC,QAAK,KAAK,uBAAwB;AAGjC,WAAK,QAAS,CAAE,SAAU;AACzB,cAAM,KAAK,UAAW,IAAK;AAC3B,YAAK,aAAa,IAAK,EAAG,GAAI;AAC7B,gBAAM,eAAe,WAAW,SAAU,IAAK,IAAI;AACnD,cAAK,cAAe;AACnB,kCAAsB,QAAQ,IAAK,IAAI,IAAK;AAAA,UAC7C;AAAA,QACD;AAAA,MACD,CAAE;AAGF,4BAAsB,QAAQ,QAAS,CAAE,GAAG,OAAQ;AACnD,YAAK,CAAE,aAAa,IAAK,EAAG,GAAI;AAC/B,gCAAsB,QAAQ,OAAQ,EAAG;AAAA,QAC1C;AAAA,MACD,CAAE;AAGF,aAAO,MAAM,KAAM,sBAAsB,QAAQ,OAAO,CAAE;AAAA,IAC3D;AAGA,WAAO,KAAK,OAAQ,CAAE,SAAU;AAC/B,YAAM,KAAK,UAAW,IAAK;AAC3B,UAAK,CAAE,aAAa,IAAK,EAAG,GAAI;AAC/B,eAAO;AAAA,MACR;AAEA,aAAO,WAAW,SAAU,IAAK,IAAI;AAAA,IACtC,CAAE;AAAA,EACH,GAAG,CAAE,KAAK,uBAAuB,WAAW,WAAW,MAAM,QAAS,CAAE;AACzE;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/types/dataviews.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { ReactElement, ReactNode, ComponentProps } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport type { useFocusOnMount } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport type {\n\tNormalizedField,\n\tOperator,\n\tOption,\n\tSortDirection,\n} from './field-api';\nimport type { SetSelection } from './private';\n\n/**\n * The filters applied to the dataset.\n */\nexport interface Filter {\n\t/**\n\t * The field to filter by.\n\t */\n\tfield: string;\n\n\t/**\n\t * The operator to use.\n\t */\n\toperator: Operator;\n\n\t/**\n\t * The value to filter by.\n\t */\n\tvalue: any;\n\n\t/**\n\t * Whether the filter can be edited by the user.\n\t */\n\tisLocked?: boolean;\n}\n\nexport interface NormalizedFilter {\n\t/**\n\t * The field to filter by.\n\t */\n\tfield: string;\n\n\t/**\n\t * The field name.\n\t */\n\tname: string;\n\n\t/**\n\t * The list of options to pick from when using the field as a filter.\n\t */\n\telements?: Option[];\n\n\t/**\n\t * Retrieval function to get the elements.\n\t */\n\tgetElements?: () => Promise< Option[] >;\n\n\t/**\n\t * Whether the filter has elements.\n\t */\n\thasElements: boolean;\n\n\t/**\n\t * Is a single selection filter.\n\t */\n\tsingleSelection: boolean;\n\n\t/**\n\t * The list of operators supported by the field.\n\t */\n\toperators: Operator[];\n\n\t/**\n\t * Whether the filter is visible.\n\t */\n\tisVisible: boolean;\n\n\t/**\n\t * Whether it is a primary filter.\n\t */\n\tisPrimary: boolean;\n\n\t/**\n\t * Whether the filter can be edited by the user.\n\t */\n\tisLocked: boolean;\n}\n\ninterface ViewBase {\n\t/**\n\t * The layout of the view.\n\t */\n\ttype: string;\n\n\t/**\n\t * The global search term.\n\t */\n\tsearch?: string;\n\n\t/**\n\t * The filters to apply.\n\t */\n\tfilters?: Filter[];\n\n\t/**\n\t * The sorting configuration.\n\t */\n\tsort?: {\n\t\t/**\n\t\t * The field to sort by.\n\t\t */\n\t\tfield: string;\n\n\t\t/**\n\t\t * The direction to sort by.\n\t\t */\n\t\tdirection: SortDirection;\n\t};\n\n\t/**\n\t * The active page\n\t */\n\tpage?: number;\n\n\t/**\n\t * The number of items per page\n\t */\n\tperPage?: number;\n\n\t/**\n\t * The fields to render\n\t */\n\tfields?: string[];\n\n\t/**\n\t * Title field\n\t */\n\ttitleField?: string;\n\n\t/**\n\t * Media field\n\t */\n\tmediaField?: string;\n\n\t/**\n\t * Description field\n\t */\n\tdescriptionField?: string;\n\n\t/**\n\t * Whether to show the title\n\t */\n\tshowTitle?: boolean;\n\n\t/**\n\t * Whether to show the media\n\t */\n\tshowMedia?: boolean;\n\n\t/**\n\t * Whether to show the description\n\t */\n\tshowDescription?: boolean;\n\n\t/**\n\t * Whether to show the hierarchical levels.\n\t */\n\tshowLevels?: boolean;\n\n\t/**\n\t * The grouping configuration.\n\t */\n\tgroupBy?: {\n\t\t/**\n\t\t * The field to group by.\n\t\t */\n\t\tfield: string;\n\n\t\t/**\n\t\t * The direction to sort by.\n\t\t */\n\t\tdirection: SortDirection;\n\n\t\t/**\n\t\t * Whether to show the field label in the group header.\n\t\t *\n\t\t * @default true\n\t\t */\n\t\tshowLabel?: boolean;\n\t};\n\n\t/**\n\t * Whether infinite scroll is enabled.\n\t */\n\tinfiniteScrollEnabled?: boolean;\n}\n\nexport interface ColumnStyle {\n\t/**\n\t * The width of the field column.\n\t */\n\twidth?: string | number;\n\n\t/**\n\t * The minimum width of the field column.\n\t */\n\tmaxWidth?: string | number;\n\n\t/**\n\t * The maximum width of the field column.\n\t */\n\tminWidth?: string | number;\n\n\t/**\n\t * The alignment of the field column, defaults to left.\n\t */\n\talign?: 'start' | 'center' | 'end';\n}\n\nexport type Density = 'compact' | 'balanced' | 'comfortable';\n\nexport interface ViewTable extends ViewBase {\n\ttype: 'table';\n\n\tlayout?: {\n\t\t/**\n\t\t * The styles for the columns.\n\t\t */\n\t\tstyles?: Record< string, ColumnStyle >;\n\n\t\t/**\n\t\t * The density of the view.\n\t\t */\n\t\tdensity?: Density;\n\n\t\t/**\n\t\t * Whether the view allows column moving.\n\t\t */\n\t\tenableMoving?: boolean;\n\t};\n}\n\nexport interface ViewList extends ViewBase {\n\ttype: 'list';\n\n\tlayout?: {\n\t\t/**\n\t\t * The density of the view.\n\t\t */\n\t\tdensity?: Density;\n\t};\n}\n\nexport interface ViewActivity extends ViewBase {\n\ttype: 'activity';\n\n\tlayout?: {\n\t\t/**\n\t\t * The density of the view.\n\t\t */\n\t\tdensity?: Density;\n\t};\n}\n\nexport interface ViewGrid extends ViewBase {\n\ttype: 'grid';\n\n\tlayout?: {\n\t\t/**\n\t\t * The fields to use as badge fields.\n\t\t */\n\t\tbadgeFields?: string[];\n\n\t\t/**\n\t\t * The preview size of the grid.\n\t\t */\n\t\tpreviewSize?: number;\n\n\t\t/**\n\t\t * The density of the grid layout.\n\t\t */\n\t\tdensity?: Density;\n\t};\n}\n\nexport interface ViewPickerGrid extends ViewBase {\n\ttype: 'pickerGrid';\n\n\tlayout?: {\n\t\t/**\n\t\t * The fields to use as badge fields.\n\t\t */\n\t\tbadgeFields?: string[];\n\n\t\t/**\n\t\t * The preview size of the grid.\n\t\t */\n\t\tpreviewSize?: number;\n\n\t\t/**\n\t\t * The density of the grid layout.\n\t\t */\n\t\tdensity?: Density;\n\t};\n}\n\nexport interface ViewPickerTable extends ViewBase {\n\ttype: 'pickerTable';\n\n\tlayout?: {\n\t\t/**\n\t\t * The styles for the columns.\n\t\t */\n\t\tstyles?: Record< string, ColumnStyle >;\n\n\t\t/**\n\t\t * The density of the view.\n\t\t */\n\t\tdensity?: Density;\n\n\t\t/**\n\t\t * Whether the view allows column moving.\n\t\t */\n\t\tenableMoving?: boolean;\n\t};\n}\n\nexport type View =\n\t| ViewList\n\t| ViewGrid\n\t| ViewTable\n\t| ViewPickerGrid\n\t| ViewPickerTable\n\t| ViewActivity;\n\ninterface ActionBase< Item > {\n\t/**\n\t * The unique identifier of the action.\n\t */\n\tid: string;\n\n\t/**\n\t * The label of the action.\n\t * In case we want to adjust the label based on the selected items,\n\t * a function can be provided.\n\t */\n\tlabel: string | ( ( items: Item[] ) => string );\n\n\t/**\n\t * The icon of the action. (Either a string or an SVG element)\n\t * This should be IconType from the components package\n\t * but that import is breaking typescript build for the moment.\n\t */\n\ticon?: any;\n\n\t/**\n\t * Whether the action is disabled.\n\t */\n\tdisabled?: boolean;\n\n\t/**\n\t * Whether the action is a primary action.\n\t */\n\tisPrimary?: boolean;\n\n\t/**\n\t * Whether the item passed as an argument supports the current action.\n\t */\n\tisEligible?: ( item: Item ) => boolean;\n\n\t/**\n\t * Whether the action can be used as a bulk action.\n\t */\n\tsupportsBulk?: boolean;\n\n\t/**\n\t * The context in which the action is visible.\n\t * This is only a \"meta\" information for now.\n\t */\n\tcontext?: 'list' | 'single';\n}\n\nexport interface RenderModalProps< Item > {\n\titems: Item[];\n\tcloseModal?: () => void;\n\tonActionPerformed?: ( items: Item[] ) => void;\n}\n\nexport interface ActionModal< Item > extends ActionBase< Item > {\n\t/**\n\t * Modal to render when the action is triggered.\n\t */\n\tRenderModal: ( {\n\t\titems,\n\t\tcloseModal,\n\t\tonActionPerformed,\n\t}: RenderModalProps< Item > ) => ReactElement;\n\n\t/**\n\t * Whether to hide the modal header.\n\t */\n\thideModalHeader?: boolean;\n\n\t/**\n\t * The header of the modal.\n\t */\n\tmodalHeader?: string | ( ( items: Item[] ) => string );\n\n\t/**\n\t * The size of the modal.\n\t *\n\t * @default 'medium'\n\t */\n\tmodalSize?: 'small' | 'medium' | 'large' | 'fill';\n\n\t/**\n\t * The focus on mount property of the modal.\n\t */\n\tmodalFocusOnMount?:\n\t\t| Parameters< typeof useFocusOnMount >[ 0 ]\n\t\t| 'firstContentElement';\n}\n\nexport interface ActionButton< Item > extends ActionBase< Item > {\n\t/**\n\t * The callback to execute when the action is triggered.\n\t */\n\tcallback: (\n\t\titems: Item[],\n\t\tcontext: {\n\t\t\tregistry: any;\n\t\t\tonActionPerformed?: ( items: Item[] ) => void;\n\t\t}\n\t) => void;\n}\n\nexport type Action< Item > = ActionModal< Item > | ActionButton< Item >;\n\nexport interface ViewBaseProps< Item > {\n\tclassName?: string;\n\tactions: Action< Item >[];\n\tdata: Item[];\n\tfields: NormalizedField< Item >[];\n\tgetItemId: ( item: Item ) => string;\n\tgetItemLevel?: ( item: Item ) => number;\n\tisLoading?: boolean;\n\tonChangeView: ( view: View ) => void;\n\tonChangeSelection: SetSelection;\n\tselection: string[];\n\tsetOpenedFilter: ( fieldId: string ) => void;\n\tonClickItem?: ( item: Item ) => void;\n\trenderItemLink?: (\n\t\tprops: {\n\t\t\titem: Item;\n\t\t} & ComponentProps< 'a' >\n\t) => ReactElement;\n\tisItemClickable: ( item: Item ) => boolean;\n\tview: View;\n\tempty: ReactNode;\n}\n\nexport type ViewPickerBaseProps< Item > = Omit<\n\tViewBaseProps< Item >,\n\t| 'view'\n\t| 'onChangeView'\n\t// The following props are not supported for pickers.\n\t| 'isItemClickable'\n\t| 'onClickItem'\n\t| 'renderItemLink'\n\t| 'getItemLevel'\n> & {\n\tview: View;\n\tonChangeView: ( view: View ) => void;\n};\n\nexport interface ViewTableProps< Item > extends ViewBaseProps< Item > {\n\tview: ViewTable;\n}\n\nexport interface ViewListProps< Item > extends ViewBaseProps< Item > {\n\tview: ViewList;\n}\n\nexport interface ViewActivityProps< Item > extends ViewBaseProps< Item > {\n\tview: ViewActivity;\n}\n\nexport interface ViewGridProps< Item > extends ViewBaseProps< Item > {\n\tview: ViewGrid;\n}\n\nexport interface ViewPickerGridProps< Item >\n\textends Omit< ViewPickerBaseProps< Item >, 'view' > {\n\tview: ViewPickerGrid;\n}\n\nexport interface ViewPickerTableProps< Item >\n\textends Omit< ViewPickerBaseProps< Item >, 'view' > {\n\tview: ViewPickerTable;\n}\n\nexport type ViewProps< Item > =\n\t| ViewTableProps< Item >\n\t| ViewGridProps< Item >\n\t| ViewListProps< Item >\n\t| ViewActivityProps< Item >;\n\nexport type ViewPickerProps< Item > =\n\t| ViewPickerGridProps< Item >\n\t| ViewPickerTableProps< Item >;\n\nexport interface SupportedLayouts {\n\tlist?: Omit< ViewList, 'type' >;\n\tgrid?: Omit< ViewGrid, 'type' >;\n\ttable?: Omit< ViewTable, 'type' >;\n\tactivity?: Omit< ViewActivity, 'type' >;\n\tpickerGrid?: Omit< ViewPickerGrid, 'type' >;\n\tpickerTable?: Omit< ViewPickerTable, 'type' >;\n}\n"],
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { ReactElement, ReactNode, ComponentProps } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport type { useFocusOnMount } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport type {\n\tNormalizedField,\n\tOperator,\n\tOption,\n\tSortDirection,\n} from './field-api';\nimport type { SetSelection } from './private';\n\n/**\n * The filters applied to the dataset.\n */\nexport interface Filter {\n\t/**\n\t * The field to filter by.\n\t */\n\tfield: string;\n\n\t/**\n\t * The operator to use.\n\t */\n\toperator: Operator;\n\n\t/**\n\t * The value to filter by.\n\t */\n\tvalue: any;\n\n\t/**\n\t * Whether the filter can be edited by the user.\n\t */\n\tisLocked?: boolean;\n}\n\nexport interface NormalizedFilter {\n\t/**\n\t * The field to filter by.\n\t */\n\tfield: string;\n\n\t/**\n\t * The field name.\n\t */\n\tname: string;\n\n\t/**\n\t * The list of options to pick from when using the field as a filter.\n\t */\n\telements?: Option[];\n\n\t/**\n\t * Retrieval function to get the elements.\n\t */\n\tgetElements?: () => Promise< Option[] >;\n\n\t/**\n\t * Whether the filter has elements.\n\t */\n\thasElements: boolean;\n\n\t/**\n\t * Is a single selection filter.\n\t */\n\tsingleSelection: boolean;\n\n\t/**\n\t * The list of operators supported by the field.\n\t */\n\toperators: Operator[];\n\n\t/**\n\t * Whether the filter is visible.\n\t */\n\tisVisible: boolean;\n\n\t/**\n\t * Whether it is a primary filter.\n\t */\n\tisPrimary: boolean;\n\n\t/**\n\t * Whether the filter can be edited by the user.\n\t */\n\tisLocked: boolean;\n}\n\ninterface ViewBase {\n\t/**\n\t * The layout of the view.\n\t */\n\ttype: string;\n\n\t/**\n\t * The global search term.\n\t */\n\tsearch?: string;\n\n\t/**\n\t * The filters to apply.\n\t */\n\tfilters?: Filter[];\n\n\t/**\n\t * The sorting configuration.\n\t */\n\tsort?: {\n\t\t/**\n\t\t * The field to sort by.\n\t\t */\n\t\tfield: string;\n\n\t\t/**\n\t\t * The direction to sort by.\n\t\t */\n\t\tdirection: SortDirection;\n\t};\n\n\t/**\n\t * The active page\n\t */\n\tpage?: number;\n\n\t/**\n\t * The number of items per page.\n\t * Also used as the batch size when infinite scroll is enabled.\n\t */\n\tperPage?: number;\n\n\t/**\n\t * The fields to render\n\t */\n\tfields?: string[];\n\n\t/**\n\t * Title field\n\t */\n\ttitleField?: string;\n\n\t/**\n\t * Media field\n\t */\n\tmediaField?: string;\n\n\t/**\n\t * Description field\n\t */\n\tdescriptionField?: string;\n\n\t/**\n\t * Whether to show the title\n\t */\n\tshowTitle?: boolean;\n\n\t/**\n\t * Whether to show the media\n\t */\n\tshowMedia?: boolean;\n\n\t/**\n\t * Whether to show the description\n\t */\n\tshowDescription?: boolean;\n\n\t/**\n\t * Whether to show the hierarchical levels.\n\t */\n\tshowLevels?: boolean;\n\n\t/**\n\t * The grouping configuration.\n\t */\n\tgroupBy?: {\n\t\t/**\n\t\t * The field to group by.\n\t\t */\n\t\tfield: string;\n\n\t\t/**\n\t\t * The direction to sort by.\n\t\t */\n\t\tdirection: SortDirection;\n\n\t\t/**\n\t\t * Whether to show the field label in the group header.\n\t\t *\n\t\t * @default true\n\t\t */\n\t\tshowLabel?: boolean;\n\t};\n\n\t/**\n\t * Whether infinite scroll is enabled.\n\t */\n\tinfiniteScrollEnabled?: boolean;\n\n\t/**\n\t * The start position for infinite scroll (1-indexed).\n\t * Used when infiniteScrollEnabled is true.\n\t */\n\tstartPosition?: number;\n}\n\nexport interface ColumnStyle {\n\t/**\n\t * The width of the field column.\n\t */\n\twidth?: string | number;\n\n\t/**\n\t * The minimum width of the field column.\n\t */\n\tmaxWidth?: string | number;\n\n\t/**\n\t * The maximum width of the field column.\n\t */\n\tminWidth?: string | number;\n\n\t/**\n\t * The alignment of the field column, defaults to left.\n\t */\n\talign?: 'start' | 'center' | 'end';\n}\n\nexport type Density = 'compact' | 'balanced' | 'comfortable';\n\nexport interface ViewTable extends ViewBase {\n\ttype: 'table';\n\n\tlayout?: {\n\t\t/**\n\t\t * The styles for the columns.\n\t\t */\n\t\tstyles?: Record< string, ColumnStyle >;\n\n\t\t/**\n\t\t * The density of the view.\n\t\t */\n\t\tdensity?: Density;\n\n\t\t/**\n\t\t * Whether the view allows column moving.\n\t\t */\n\t\tenableMoving?: boolean;\n\t};\n}\n\nexport interface ViewList extends ViewBase {\n\ttype: 'list';\n\n\tlayout?: {\n\t\t/**\n\t\t * The density of the view.\n\t\t */\n\t\tdensity?: Density;\n\t};\n}\n\nexport interface ViewActivity extends ViewBase {\n\ttype: 'activity';\n\n\tlayout?: {\n\t\t/**\n\t\t * The density of the view.\n\t\t */\n\t\tdensity?: Density;\n\t};\n}\n\nexport interface ViewGrid extends ViewBase {\n\ttype: 'grid';\n\n\tlayout?: {\n\t\t/**\n\t\t * The fields to use as badge fields.\n\t\t */\n\t\tbadgeFields?: string[];\n\n\t\t/**\n\t\t * The preview size of the grid.\n\t\t */\n\t\tpreviewSize?: number;\n\n\t\t/**\n\t\t * The density of the grid layout.\n\t\t */\n\t\tdensity?: Density;\n\t};\n}\n\nexport interface ViewPickerGrid extends ViewBase {\n\ttype: 'pickerGrid';\n\n\tlayout?: {\n\t\t/**\n\t\t * The fields to use as badge fields.\n\t\t */\n\t\tbadgeFields?: string[];\n\n\t\t/**\n\t\t * The preview size of the grid.\n\t\t */\n\t\tpreviewSize?: number;\n\n\t\t/**\n\t\t * The density of the grid layout.\n\t\t */\n\t\tdensity?: Density;\n\t};\n}\n\nexport interface ViewPickerTable extends ViewBase {\n\ttype: 'pickerTable';\n\n\tlayout?: {\n\t\t/**\n\t\t * The styles for the columns.\n\t\t */\n\t\tstyles?: Record< string, ColumnStyle >;\n\n\t\t/**\n\t\t * The density of the view.\n\t\t */\n\t\tdensity?: Density;\n\n\t\t/**\n\t\t * Whether the view allows column moving.\n\t\t */\n\t\tenableMoving?: boolean;\n\t};\n}\n\nexport type View =\n\t| ViewList\n\t| ViewGrid\n\t| ViewTable\n\t| ViewPickerGrid\n\t| ViewPickerTable\n\t| ViewActivity;\n\ninterface ActionBase< Item > {\n\t/**\n\t * The unique identifier of the action.\n\t */\n\tid: string;\n\n\t/**\n\t * The label of the action.\n\t * In case we want to adjust the label based on the selected items,\n\t * a function can be provided.\n\t */\n\tlabel: string | ( ( items: Item[] ) => string );\n\n\t/**\n\t * The icon of the action. (Either a string or an SVG element)\n\t * This should be IconType from the components package\n\t * but that import is breaking typescript build for the moment.\n\t */\n\ticon?: any;\n\n\t/**\n\t * Whether the action is disabled.\n\t */\n\tdisabled?: boolean;\n\n\t/**\n\t * Whether the action is a primary action.\n\t */\n\tisPrimary?: boolean;\n\n\t/**\n\t * Whether the item passed as an argument supports the current action.\n\t */\n\tisEligible?: ( item: Item ) => boolean;\n\n\t/**\n\t * Whether the action can be used as a bulk action.\n\t */\n\tsupportsBulk?: boolean;\n\n\t/**\n\t * The context in which the action is visible.\n\t * This is only a \"meta\" information for now.\n\t */\n\tcontext?: 'list' | 'single';\n}\n\nexport interface RenderModalProps< Item > {\n\titems: Item[];\n\tcloseModal?: () => void;\n\tonActionPerformed?: ( items: Item[] ) => void;\n}\n\nexport interface ActionModal< Item > extends ActionBase< Item > {\n\t/**\n\t * Modal to render when the action is triggered.\n\t */\n\tRenderModal: ( {\n\t\titems,\n\t\tcloseModal,\n\t\tonActionPerformed,\n\t}: RenderModalProps< Item > ) => ReactElement;\n\n\t/**\n\t * Whether to hide the modal header.\n\t */\n\thideModalHeader?: boolean;\n\n\t/**\n\t * The header of the modal.\n\t */\n\tmodalHeader?: string | ( ( items: Item[] ) => string );\n\n\t/**\n\t * The size of the modal.\n\t *\n\t * @default 'medium'\n\t */\n\tmodalSize?: 'small' | 'medium' | 'large' | 'fill';\n\n\t/**\n\t * The focus on mount property of the modal.\n\t */\n\tmodalFocusOnMount?:\n\t\t| Parameters< typeof useFocusOnMount >[ 0 ]\n\t\t| 'firstContentElement';\n}\n\nexport interface ActionButton< Item > extends ActionBase< Item > {\n\t/**\n\t * The callback to execute when the action is triggered.\n\t */\n\tcallback: (\n\t\titems: Item[],\n\t\tcontext: {\n\t\t\tregistry: any;\n\t\t\tonActionPerformed?: ( items: Item[] ) => void;\n\t\t}\n\t) => void;\n}\n\nexport type Action< Item > = ActionModal< Item > | ActionButton< Item >;\n\nexport interface ViewBaseProps< Item > {\n\tclassName?: string;\n\tactions: Action< Item >[];\n\tdata: Item[];\n\tfields: NormalizedField< Item >[];\n\tgetItemId: ( item: Item ) => string;\n\tgetItemLevel?: ( item: Item ) => number;\n\tisLoading?: boolean;\n\tonChangeView: ( view: View ) => void;\n\tonChangeSelection: SetSelection;\n\tselection: string[];\n\tsetOpenedFilter: ( fieldId: string ) => void;\n\tonClickItem?: ( item: Item ) => void;\n\trenderItemLink?: (\n\t\tprops: {\n\t\t\titem: Item;\n\t\t} & ComponentProps< 'a' >\n\t) => ReactElement;\n\tisItemClickable: ( item: Item ) => boolean;\n\tview: View;\n\tempty: ReactNode;\n}\n\nexport type ViewPickerBaseProps< Item > = Omit<\n\tViewBaseProps< Item >,\n\t| 'view'\n\t| 'onChangeView'\n\t// The following props are not supported for pickers.\n\t| 'isItemClickable'\n\t| 'onClickItem'\n\t| 'renderItemLink'\n\t| 'getItemLevel'\n> & {\n\tview: View;\n\tonChangeView: ( view: View ) => void;\n};\n\nexport interface ViewTableProps< Item > extends ViewBaseProps< Item > {\n\tview: ViewTable;\n}\n\nexport interface ViewListProps< Item > extends ViewBaseProps< Item > {\n\tview: ViewList;\n}\n\nexport interface ViewActivityProps< Item > extends ViewBaseProps< Item > {\n\tview: ViewActivity;\n}\n\nexport interface ViewGridProps< Item > extends ViewBaseProps< Item > {\n\tview: ViewGrid;\n}\n\nexport interface ViewPickerGridProps< Item >\n\textends Omit< ViewPickerBaseProps< Item >, 'view' > {\n\tview: ViewPickerGrid;\n}\n\nexport interface ViewPickerTableProps< Item >\n\textends Omit< ViewPickerBaseProps< Item >, 'view' > {\n\tview: ViewPickerTable;\n}\n\nexport type ViewProps< Item > =\n\t| ViewTableProps< Item >\n\t| ViewGridProps< Item >\n\t| ViewListProps< Item >\n\t| ViewActivityProps< Item >;\n\nexport type ViewPickerProps< Item > =\n\t| ViewPickerGridProps< Item >\n\t| ViewPickerTableProps< Item >;\n\nexport interface SupportedLayouts {\n\tlist?: Omit< ViewList, 'type' >;\n\tgrid?: Omit< ViewGrid, 'type' >;\n\ttable?: Omit< ViewTable, 'type' >;\n\tactivity?: Omit< ViewActivity, 'type' >;\n\tpickerGrid?: Omit< ViewPickerGrid, 'type' >;\n\tpickerTable?: Omit< ViewPickerTable, 'type' >;\n}\n"],
|
|
5
5
|
"mappings": ";;;;;;;;;;;;;;;;AAAA;AAAA;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/types/field-api.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { ReactElement, ComponentType } from 'react';\n\n/**\n * Utility type that makes all properties of T optional recursively.\n * Used by field setValue functions to allow partial item updates.\n */\nexport type DeepPartial< T > = {\n\t[ P in keyof T ]?: T[ P ] extends object ? DeepPartial< T[ P ] > : T[ P ];\n};\n\nexport type SortDirection = 'asc' | 'desc';\n\n/**\n * Generic option type.\n */\nexport interface Option< Value extends any = any > {\n\tvalue: Value;\n\tlabel: string;\n\tdescription?: string;\n}\n\nexport interface FilterByConfig {\n\t/**\n\t * The list of operators supported by the field.\n\t */\n\toperators?: Operator[];\n\n\t/**\n\t * Whether it is a primary filter.\n\t *\n\t * A primary filter is always visible and is not listed in the \"Add filter\" component,\n\t * except for the list layout where it behaves like a secondary filter.\n\t */\n\tisPrimary?: boolean;\n}\n\nexport type Operator =\n\t| 'is'\n\t| 'isNot'\n\t| 'isAny'\n\t| 'isNone'\n\t| 'isAll'\n\t| 'isNotAll'\n\t| 'lessThan'\n\t| 'greaterThan'\n\t| 'lessThanOrEqual'\n\t| 'greaterThanOrEqual'\n\t| 'before'\n\t| 'after'\n\t| 'beforeInc'\n\t| 'afterInc'\n\t| 'contains'\n\t| 'notContains'\n\t| 'startsWith'\n\t| 'between'\n\t| 'on'\n\t| 'notOn'\n\t| 'inThePast'\n\t| 'over';\n\nexport type FieldTypeName =\n\t| 'text'\n\t| 'integer'\n\t| 'number'\n\t| 'datetime'\n\t| 'date'\n\t| 'media'\n\t| 'boolean'\n\t| 'email'\n\t| 'password'\n\t| 'telephone'\n\t| 'color'\n\t| 'url'\n\t| 'array';\n\nexport type Rules< Item > = {\n\trequired?: boolean;\n\telements?: boolean;\n\tpattern?: string;\n\tminLength?: number;\n\tmaxLength?: number;\n\tmin?: number;\n\tmax?: number;\n\tcustom?:\n\t\t| ( ( item: Item, field: NormalizedField< Item > ) => null | string )\n\t\t| ( (\n\t\t\t\titem: Item,\n\t\t\t\tfield: NormalizedField< Item >\n\t\t ) => Promise< null | string > );\n};\n\nexport type Validator< Item > = (\n\titem: Item,\n\tfield: NormalizedField< Item >\n) => boolean;\n\nexport type CustomValidator< Item > =\n\t| ( ( item: Item, field: NormalizedField< Item > ) => null | string )\n\t| ( (\n\t\t\titem: Item,\n\t\t\tfield: NormalizedField< Item >\n\t ) => Promise< null | string > );\n\nexport type FilterOperator< Item > = (\n\titem: Item,\n\tfield: NormalizedField< Item >,\n\tfilterValue: any\n) => boolean;\n\nexport type FilterOperatorMap< Item > = Partial<\n\tRecord< Operator, FilterOperator< Item > >\n>;\n\ntype NormalizedRule< Item, ConstraintType > = {\n\tconstraint: ConstraintType;\n\tvalidate: Validator< Item >;\n};\n\nexport type NormalizedRules< Item > = {\n\trequired?: NormalizedRule< Item, boolean >;\n\telements?: NormalizedRule< Item, boolean >;\n\tpattern?: NormalizedRule< Item, string >;\n\tminLength?: NormalizedRule< Item, number >;\n\tmaxLength?: NormalizedRule< Item, number >;\n\tmin?: NormalizedRule< Item, number >;\n\tmax?: NormalizedRule< Item, number >;\n\tcustom?: CustomValidator< Item >;\n};\n\n/**\n * Edit configuration for textarea controls.\n */\nexport type EditConfigTextarea = {\n\tcontrol: 'textarea';\n\t/**\n\t * Number of rows for the textarea.\n\t */\n\trows?: number;\n};\n\n/**\n * Edit configuration for text controls.\n */\nexport type EditConfigText = {\n\tcontrol: 'text';\n\t/**\n\t * Prefix component to display before the input.\n\t */\n\tprefix?: React.ComponentType;\n\t/**\n\t * Suffix component to display after the input.\n\t */\n\tsuffix?: React.ComponentType;\n};\n\n/**\n * Edit configuration for other control types (excluding 'text' and 'textarea').\n */\nexport type EditConfigGeneric = {\n\tcontrol: Exclude< FieldTypeName, 'text' | 'textarea' >;\n};\n\n/**\n * Edit configuration object with type-safe control options.\n * Each control type has its own specific configuration properties.\n */\nexport type EditConfig =\n\t| EditConfigTextarea\n\t| EditConfigText\n\t| EditConfigGeneric;\n\n/**\n * A dataview field for a specific property of a data type.\n */\nexport type Field< Item > = {\n\t/**\n\t * Type of the fields.\n\t */\n\ttype?: FieldTypeName;\n\n\t/**\n\t * The unique identifier of the field.\n\t */\n\tid: string;\n\n\t/**\n\t * The label of the field. Defaults to the id.\n\t */\n\tlabel?: string;\n\n\t/**\n\t * The header of the field. Defaults to the label.\n\t * It allows the usage of a React Element to render the field labels.\n\t */\n\theader?: string | ReactElement;\n\n\t/**\n\t * A description of the field.\n\t */\n\tdescription?: string;\n\n\t/**\n\t * Placeholder for the field.\n\t */\n\tplaceholder?: string;\n\n\t/**\n\t * Callback used to render the field. Defaults to `field.getValue`.\n\t */\n\trender?: ComponentType< DataViewRenderFieldProps< Item > >;\n\n\t/**\n\t * Callback used to render an edit control for the field.\n\t */\n\tEdit?: ComponentType< DataFormControlProps< Item > > | string | EditConfig;\n\n\t/**\n\t * Callback used to sort the field.\n\t */\n\tsort?: ( a: Item, b: Item, direction: SortDirection ) => number;\n\n\t/**\n\t * Callback used to validate the field.\n\t */\n\tisValid?: Rules< Item >;\n\n\t/**\n\t * Callback used to decide if a field should be displayed.\n\t */\n\tisVisible?: ( item: Item ) => boolean;\n\n\t/**\n\t * Whether the field is sortable.\n\t */\n\tenableSorting?: boolean;\n\n\t/**\n\t * Whether the field is searchable.\n\t */\n\tenableGlobalSearch?: boolean;\n\n\t/**\n\t * Whether the field can be hidden in the UI.\n\t */\n\tenableHiding?: boolean;\n\n\t/**\n\t * The list of options to pick from when using the field as a filter.\n\t */\n\telements?: Option[];\n\n\t/**\n\t * Retrieval function for elements.\n\t */\n\tgetElements?: () => Promise< Option[] >;\n\n\t/**\n\t * Filter config for the field.\n\t */\n\tfilterBy?: FilterByConfig | false;\n\n\t/**\n\t * Whether the field is readOnly.\n\t * If `true`, the value will be rendered using the `render` callback.\n\t */\n\treadOnly?: boolean;\n\n\t/**\n\t * Callback used to retrieve the value of the field from the item.\n\t * Defaults to `item[ field.id ]`.\n\t */\n\tgetValue?: ( args: { item: Item } ) => any;\n\n\t/**\n\t * Callback used to set the value of the field on the item.\n\t * Used for editing operations to update field values.\n\t */\n\tsetValue?: ( args: { item: Item; value: any } ) => DeepPartial< Item >;\n\n\t/**\n\t * Display format configuration for fields.\n\t */\n\tformat?: FormatDatetime | FormatDate | FormatNumber | FormatInteger;\n\n\t/**\n\t * Callback used to format the value of the field for display.\n\t */\n\tgetValueFormatted?: ( {\n\t\titem,\n\t\tfield,\n\t}: {\n\t\titem: Item;\n\t\tfield: NormalizedField< Item >;\n\t} ) => string;\n};\n\n/**\n * Format for datetime fields:\n *\n * - datetime: the format string (e.g., \"M j, Y g:i a\" for \"Jan 1, 2021 2:30 pm\").\n * - weekStartsOn: to specify the first day of the week (0 for 'sunday', 1 for 'monday', etc.).\n *\n * If not provided, defaults to WordPress date format settings.\n */\nexport type FormatDatetime = {\n\tdatetime?: string;\n\tweekStartsOn?: DayNumber;\n};\n\n/**\n * Format for date fields:\n *\n * - date: the format string (e.g., 'F j, Y' for 'March 10, 2023')\n * - weekStartsOn: to specify the first day of the week (0 for 'sunday', 1 for 'monday', etc.).\n *\n * If not provided, defaults to WordPress date format settings.\n */\nexport type FormatDate = {\n\tdate?: string;\n\tweekStartsOn?: DayNumber;\n};\nexport type DayNumber = 0 | 1 | 2 | 3 | 4 | 5 | 6;\n\n/**\n * Format for number fields:\n *\n * - separatorThousand: character to use for thousand separators (e.g., ',')\n * - separatorDecimal: character to use for decimal point (e.g., '.')\n * - decimals: number of decimal places to display (e.g., 2)\n *\n * If not provided, defaults to ',' for thousands, '.' for decimal, 2 decimals.\n */\nexport type FormatNumber = {\n\tseparatorThousand?: string;\n\tseparatorDecimal?: string;\n\tdecimals?: number;\n};\n\n/**\n * Format for integer fields:\n *\n * - separatorThousand: character to use for thousand separators (e.g., ',')\n *\n * If not provided, defaults to ',' for thousands.\n */\nexport type FormatInteger = {\n\tseparatorThousand?: string;\n};\n\nexport type NormalizedField< Item > = Omit<\n\tField< Item >,\n\t'Edit' | 'isValid'\n> & {\n\tlabel: string;\n\theader: string | ReactElement;\n\tgetValue: ( args: { item: Item } ) => any;\n\tsetValue: ( args: { item: Item; value: any } ) => DeepPartial< Item >;\n\trender: ComponentType< DataViewRenderFieldProps< Item > >;\n\tEdit: ComponentType< DataFormControlProps< Item > > | null;\n\thasElements: boolean;\n\tsort: ( a: Item, b: Item, direction: SortDirection ) => number;\n\tisValid: NormalizedRules< Item >;\n\tenableHiding: boolean;\n\tenableSorting: boolean;\n\tfilterBy: Required< FilterByConfig > | false;\n\tfilter: FilterOperatorMap< Item >;\n\treadOnly: boolean;\n\tformat:\n\t\t| {}\n\t\t| Required< FormatDate >\n\t\t| Required< FormatInteger >\n\t\t| Required< FormatNumber >;\n\tgetValueFormatted: ( {\n\t\titem,\n\t\tfield,\n\t}: {\n\t\titem: Item;\n\t\tfield: NormalizedField< Item >;\n\t} ) => string;\n};\n\n/**\n * A collection of dataview fields for a data type.\n */\nexport type Fields< Item > = Field< Item >[];\n\nexport type FieldValidity = {\n\trequired?: {\n\t\ttype: 'valid' | 'invalid' | 'validating';\n\t\tmessage?: string;\n\t};\n\tpattern?: {\n\t\ttype: 'valid' | 'invalid' | 'validating';\n\t\tmessage: string;\n\t};\n\tmin?: {\n\t\ttype: 'valid' | 'invalid' | 'validating';\n\t\tmessage: string;\n\t};\n\tmax?: {\n\t\ttype: 'valid' | 'invalid' | 'validating';\n\t\tmessage: string;\n\t};\n\tminLength?: {\n\t\ttype: 'valid' | 'invalid' | 'validating';\n\t\tmessage: string;\n\t};\n\tmaxLength?: {\n\t\ttype: 'valid' | 'invalid' | 'validating';\n\t\tmessage: string;\n\t};\n\telements?: {\n\t\ttype: 'valid' | 'invalid' | 'validating';\n\t\tmessage: string;\n\t};\n\tcustom?: {\n\t\ttype: 'valid' | 'invalid' | 'validating';\n\t\tmessage: string;\n\t};\n\tchildren?: Record< string, FieldValidity >;\n};\n\nexport type DataFormControlProps< Item > = {\n\tdata: Item;\n\tfield: NormalizedField< Item >;\n\tonChange: ( value: DeepPartial< Item > ) => void;\n\thideLabelFromVision?: boolean;\n\t/**\n\t * Label the control as \"optional\" when _not_ required, instead of showing \"required\".\n\t */\n\tmarkWhenOptional?: boolean;\n\t/**\n\t * The currently selected filter operator for this field.\n\t *\n\t * Used by DataViews filters to determine which control to render based on the operator type.\n\t */\n\toperator?: Operator;\n\t/**\n\t * Validity information for the field, if any.\n\t */\n\tvalidity?: FieldValidity;\n\t/**\n\t * Configuration object for the control.\n\t */\n\tconfig?: {\n\t\tprefix?: React.ComponentType;\n\t\tsuffix?: React.ComponentType;\n\t\trows?: number;\n\t};\n};\n\nexport type DataViewRenderFieldProps< Item > = {\n\titem: Item;\n\tfield: NormalizedField< Item >;\n\tconfig?: {\n\t\tsizes: string;\n\t};\n};\n"],
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { ReactElement, ComponentType } from 'react';\n\n/**\n * Utility type that makes all properties of T optional recursively.\n * Used by field setValue functions to allow partial item updates.\n */\nexport type DeepPartial< T > = {\n\t[ P in keyof T ]?: T[ P ] extends object ? DeepPartial< T[ P ] > : T[ P ];\n};\n\nexport type SortDirection = 'asc' | 'desc';\n\n/**\n * Generic option type.\n */\nexport interface Option< Value extends any = any > {\n\tvalue: Value;\n\tlabel: string;\n\tdescription?: string;\n}\n\nexport interface FilterByConfig {\n\t/**\n\t * The list of operators supported by the field.\n\t */\n\toperators?: Operator[];\n\n\t/**\n\t * Whether it is a primary filter.\n\t *\n\t * A primary filter is always visible and is not listed in the \"Add filter\" component,\n\t * except for the list layout where it behaves like a secondary filter.\n\t */\n\tisPrimary?: boolean;\n}\n\nexport type Operator =\n\t| 'is'\n\t| 'isNot'\n\t| 'isAny'\n\t| 'isNone'\n\t| 'isAll'\n\t| 'isNotAll'\n\t| 'lessThan'\n\t| 'greaterThan'\n\t| 'lessThanOrEqual'\n\t| 'greaterThanOrEqual'\n\t| 'before'\n\t| 'after'\n\t| 'beforeInc'\n\t| 'afterInc'\n\t| 'contains'\n\t| 'notContains'\n\t| 'startsWith'\n\t| 'between'\n\t| 'on'\n\t| 'notOn'\n\t| 'inThePast'\n\t| 'over';\n\nexport type FieldTypeName =\n\t| 'text'\n\t| 'integer'\n\t| 'number'\n\t| 'datetime'\n\t| 'date'\n\t| 'media'\n\t| 'boolean'\n\t| 'email'\n\t| 'password'\n\t| 'telephone'\n\t| 'color'\n\t| 'url'\n\t| 'array';\n\nexport type Rules< Item > = {\n\trequired?: boolean;\n\telements?: boolean;\n\tpattern?: string;\n\tminLength?: number;\n\tmaxLength?: number;\n\tmin?: number;\n\tmax?: number;\n\tcustom?:\n\t\t| ( ( item: Item, field: NormalizedField< Item > ) => null | string )\n\t\t| ( (\n\t\t\t\titem: Item,\n\t\t\t\tfield: NormalizedField< Item >\n\t\t ) => Promise< null | string > );\n};\n\nexport type Validator< Item > = (\n\titem: Item,\n\tfield: NormalizedField< Item >\n) => boolean;\n\nexport type CustomValidator< Item > =\n\t| ( ( item: Item, field: NormalizedField< Item > ) => null | string )\n\t| ( (\n\t\t\titem: Item,\n\t\t\tfield: NormalizedField< Item >\n\t ) => Promise< null | string > );\n\nexport type FilterOperator< Item > = (\n\titem: Item,\n\tfield: NormalizedField< Item >,\n\tfilterValue: any\n) => boolean;\n\nexport type FilterOperatorMap< Item > = Partial<\n\tRecord< Operator, FilterOperator< Item > >\n>;\n\ntype NormalizedRule< Item, ConstraintType > = {\n\tconstraint: ConstraintType;\n\tvalidate: Validator< Item >;\n};\n\nexport type NormalizedRules< Item > = {\n\trequired?: NormalizedRule< Item, boolean >;\n\telements?: NormalizedRule< Item, boolean >;\n\tpattern?: NormalizedRule< Item, string >;\n\tminLength?: NormalizedRule< Item, number >;\n\tmaxLength?: NormalizedRule< Item, number >;\n\tmin?: NormalizedRule< Item, number >;\n\tmax?: NormalizedRule< Item, number >;\n\tcustom?: CustomValidator< Item >;\n};\n\n/**\n * Edit configuration for textarea controls.\n */\nexport type EditConfigTextarea = {\n\tcontrol: 'textarea';\n\t/**\n\t * Number of rows for the textarea.\n\t */\n\trows?: number;\n};\n\n/**\n * Edit configuration for text controls.\n */\nexport type EditConfigText = {\n\tcontrol: 'text';\n\t/**\n\t * Prefix component to display before the input.\n\t */\n\tprefix?: React.ComponentType;\n\t/**\n\t * Suffix component to display after the input.\n\t */\n\tsuffix?: React.ComponentType;\n};\n\n/**\n * Edit configuration for datetime controls.\n */\nexport type EditConfigDatetime = {\n\tcontrol: 'datetime';\n\t/**\n\t * Whether to render a compact version without the calendar widget.\n\t */\n\tcompact?: boolean;\n};\n\n/**\n * Edit configuration for other control types (excluding 'text', 'textarea', and 'datetime').\n */\nexport type EditConfigGeneric = {\n\tcontrol: Exclude< FieldTypeName, 'text' | 'textarea' | 'datetime' >;\n};\n\n/**\n * Edit configuration object with type-safe control options.\n * Each control type has its own specific configuration properties.\n */\nexport type EditConfig =\n\t| EditConfigTextarea\n\t| EditConfigText\n\t| EditConfigDatetime\n\t| EditConfigGeneric;\n\n/**\n * A dataview field for a specific property of a data type.\n */\nexport type Field< Item > = {\n\t/**\n\t * Type of the fields.\n\t */\n\ttype?: FieldTypeName;\n\n\t/**\n\t * The unique identifier of the field.\n\t */\n\tid: string;\n\n\t/**\n\t * The label of the field. Defaults to the id.\n\t */\n\tlabel?: string;\n\n\t/**\n\t * The header of the field. Defaults to the label.\n\t * It allows the usage of a React Element to render the field labels.\n\t */\n\theader?: string | ReactElement;\n\n\t/**\n\t * A description of the field.\n\t */\n\tdescription?: string | ReactElement;\n\n\t/**\n\t * Placeholder for the field.\n\t */\n\tplaceholder?: string;\n\n\t/**\n\t * Callback used to render the field. Defaults to `field.getValue`.\n\t */\n\trender?: ComponentType< DataViewRenderFieldProps< Item > >;\n\n\t/**\n\t * Callback used to render an edit control for the field.\n\t */\n\tEdit?: ComponentType< DataFormControlProps< Item > > | string | EditConfig;\n\n\t/**\n\t * Callback used to sort the field.\n\t */\n\tsort?: ( a: Item, b: Item, direction: SortDirection ) => number;\n\n\t/**\n\t * Callback used to validate the field.\n\t */\n\tisValid?: Rules< Item >;\n\n\t/**\n\t * Callback used to decide if a field should be displayed.\n\t */\n\tisVisible?: ( item: Item ) => boolean;\n\n\t/**\n\t * Whether the field is sortable.\n\t */\n\tenableSorting?: boolean;\n\n\t/**\n\t * Whether the field is searchable.\n\t */\n\tenableGlobalSearch?: boolean;\n\n\t/**\n\t * Whether the field can be hidden in the UI.\n\t */\n\tenableHiding?: boolean;\n\n\t/**\n\t * The list of options to pick from when using the field as a filter.\n\t */\n\telements?: Option[];\n\n\t/**\n\t * Retrieval function for elements.\n\t */\n\tgetElements?: () => Promise< Option[] >;\n\n\t/**\n\t * Filter config for the field.\n\t */\n\tfilterBy?: FilterByConfig | false;\n\n\t/**\n\t * Whether the field is readOnly.\n\t * If `true`, the value will be rendered using the `render` callback.\n\t */\n\treadOnly?: boolean;\n\n\t/**\n\t * Callback used to retrieve the value of the field from the item.\n\t * Defaults to `item[ field.id ]`.\n\t */\n\tgetValue?: ( args: { item: Item } ) => any;\n\n\t/**\n\t * Callback used to set the value of the field on the item.\n\t * Used for editing operations to update field values.\n\t */\n\tsetValue?: ( args: { item: Item; value: any } ) => DeepPartial< Item >;\n\n\t/**\n\t * Display format configuration for fields.\n\t */\n\tformat?: FormatDatetime | FormatDate | FormatNumber | FormatInteger;\n\n\t/**\n\t * Callback used to format the value of the field for display.\n\t */\n\tgetValueFormatted?: ( {\n\t\titem,\n\t\tfield,\n\t}: {\n\t\titem: Item;\n\t\tfield: NormalizedField< Item >;\n\t} ) => string;\n};\n\n/**\n * Format for datetime fields:\n *\n * - datetime: the format string (e.g., \"M j, Y g:i a\" for \"Jan 1, 2021 2:30 pm\").\n * - weekStartsOn: to specify the first day of the week (0 for 'sunday', 1 for 'monday', etc.).\n *\n * If not provided, defaults to WordPress date format settings.\n */\nexport type FormatDatetime = {\n\tdatetime?: string;\n\tweekStartsOn?: DayNumber;\n};\n\n/**\n * Format for date fields:\n *\n * - date: the format string (e.g., 'F j, Y' for 'March 10, 2023')\n * - weekStartsOn: to specify the first day of the week (0 for 'sunday', 1 for 'monday', etc.).\n *\n * If not provided, defaults to WordPress date format settings.\n */\nexport type FormatDate = {\n\tdate?: string;\n\tweekStartsOn?: DayNumber;\n};\nexport type DayNumber = 0 | 1 | 2 | 3 | 4 | 5 | 6;\n\n/**\n * Format for number fields:\n *\n * - separatorThousand: character to use for thousand separators (e.g., ',')\n * - separatorDecimal: character to use for decimal point (e.g., '.')\n * - decimals: number of decimal places to display (e.g., 2)\n *\n * If not provided, defaults to ',' for thousands, '.' for decimal, 2 decimals.\n */\nexport type FormatNumber = {\n\tseparatorThousand?: string;\n\tseparatorDecimal?: string;\n\tdecimals?: number;\n};\n\n/**\n * Format for integer fields:\n *\n * - separatorThousand: character to use for thousand separators (e.g., ',')\n *\n * If not provided, defaults to ',' for thousands.\n */\nexport type FormatInteger = {\n\tseparatorThousand?: string;\n};\n\nexport type NormalizedField< Item > = Omit<\n\tField< Item >,\n\t'Edit' | 'isValid'\n> & {\n\tlabel: string;\n\theader: string | ReactElement;\n\tgetValue: ( args: { item: Item } ) => any;\n\tsetValue: ( args: { item: Item; value: any } ) => DeepPartial< Item >;\n\trender: ComponentType< DataViewRenderFieldProps< Item > >;\n\tEdit: ComponentType< DataFormControlProps< Item > > | null;\n\thasElements: boolean;\n\tsort: ( a: Item, b: Item, direction: SortDirection ) => number;\n\tisValid: NormalizedRules< Item >;\n\tenableHiding: boolean;\n\tenableSorting: boolean;\n\tfilterBy: Required< FilterByConfig > | false;\n\tfilter: FilterOperatorMap< Item >;\n\treadOnly: boolean;\n\tformat:\n\t\t| {}\n\t\t| Required< FormatDate >\n\t\t| Required< FormatInteger >\n\t\t| Required< FormatNumber >;\n\tgetValueFormatted: ( {\n\t\titem,\n\t\tfield,\n\t}: {\n\t\titem: Item;\n\t\tfield: NormalizedField< Item >;\n\t} ) => string;\n};\n\n/**\n * A collection of dataview fields for a data type.\n */\nexport type Fields< Item > = Field< Item >[];\n\nexport type FieldValidity = {\n\trequired?: {\n\t\ttype: 'valid' | 'invalid' | 'validating';\n\t\tmessage?: string;\n\t};\n\tpattern?: {\n\t\ttype: 'valid' | 'invalid' | 'validating';\n\t\tmessage: string;\n\t};\n\tmin?: {\n\t\ttype: 'valid' | 'invalid' | 'validating';\n\t\tmessage: string;\n\t};\n\tmax?: {\n\t\ttype: 'valid' | 'invalid' | 'validating';\n\t\tmessage: string;\n\t};\n\tminLength?: {\n\t\ttype: 'valid' | 'invalid' | 'validating';\n\t\tmessage: string;\n\t};\n\tmaxLength?: {\n\t\ttype: 'valid' | 'invalid' | 'validating';\n\t\tmessage: string;\n\t};\n\telements?: {\n\t\ttype: 'valid' | 'invalid' | 'validating';\n\t\tmessage: string;\n\t};\n\tcustom?: {\n\t\ttype: 'valid' | 'invalid' | 'validating';\n\t\tmessage: string;\n\t};\n\tchildren?: Record< string, FieldValidity >;\n};\n\nexport type DataFormControlProps< Item > = {\n\tdata: Item;\n\tfield: NormalizedField< Item >;\n\tonChange: ( value: DeepPartial< Item > ) => void;\n\thideLabelFromVision?: boolean;\n\t/**\n\t * Label the control as \"optional\" when _not_ required, instead of showing \"required\".\n\t */\n\tmarkWhenOptional?: boolean;\n\t/**\n\t * The currently selected filter operator for this field.\n\t *\n\t * Used by DataViews filters to determine which control to render based on the operator type.\n\t */\n\toperator?: Operator;\n\t/**\n\t * Validity information for the field, if any.\n\t */\n\tvalidity?: FieldValidity;\n\t/**\n\t * Configuration object for the control.\n\t */\n\tconfig?: {\n\t\tprefix?: React.ComponentType;\n\t\tsuffix?: React.ComponentType;\n\t\trows?: number;\n\t\tcompact?: boolean;\n\t};\n};\n\nexport type DataViewRenderFieldProps< Item > = {\n\titem: Item;\n\tfield: NormalizedField< Item >;\n\tconfig?: {\n\t\tsizes: string;\n\t};\n};\n"],
|
|
5
5
|
"mappings": ";;;;;;;;;;;;;;;;AAAA;AAAA;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -111,7 +111,11 @@ function filterSortAndPaginate(data, view, fields) {
|
|
|
111
111
|
}
|
|
112
112
|
let totalItems = filteredData.length;
|
|
113
113
|
let totalPages = 1;
|
|
114
|
-
if (view.
|
|
114
|
+
if (view.infiniteScrollEnabled && view.startPosition !== void 0 && view.perPage !== void 0) {
|
|
115
|
+
const start = view.startPosition - 1;
|
|
116
|
+
const end = Math.min(start + view.perPage, totalItems);
|
|
117
|
+
filteredData = filteredData?.slice(start, end);
|
|
118
|
+
} else if (view.page !== void 0 && view.perPage !== void 0) {
|
|
115
119
|
const start = (view.page - 1) * view.perPage;
|
|
116
120
|
totalItems = filteredData?.length || 0;
|
|
117
121
|
totalPages = Math.ceil(totalItems / view.perPage);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/utils/filter-sort-and-paginate.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport removeAccents from 'remove-accents';\n\n/**\n * WordPress dependencies\n */\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport { OPERATOR_IS_NOT_ALL } from '../constants';\nimport normalizeFields from '../field-types';\nimport type { Field, Operator, View } from '../types';\n\nfunction normalizeSearchInput( input = '' ) {\n\treturn removeAccents( input.trim().toLowerCase() );\n}\n\nconst EMPTY_ARRAY: [] = [];\n\n/**\n * Applies the filtering, sorting and pagination to the raw data based on the view configuration.\n *\n * @param data Raw data.\n * @param view View config.\n * @param fields Fields config.\n *\n * @return Filtered, sorted and paginated data.\n */\nexport default function filterSortAndPaginate< Item >(\n\tdata: Item[],\n\tview: View,\n\tfields: Field< Item >[]\n): {\n\tdata: Item[];\n\tpaginationInfo: { totalItems: number; totalPages: number };\n} {\n\tif ( ! data ) {\n\t\treturn {\n\t\t\tdata: EMPTY_ARRAY,\n\t\t\tpaginationInfo: { totalItems: 0, totalPages: 0 },\n\t\t};\n\t}\n\tconst _fields = normalizeFields( fields );\n\tlet filteredData = [ ...data ];\n\t// Handle global search.\n\tif ( view.search ) {\n\t\tconst normalizedSearch = normalizeSearchInput( view.search );\n\t\tfilteredData = filteredData.filter( ( item ) => {\n\t\t\treturn _fields\n\t\t\t\t.filter( ( field ) => field.enableGlobalSearch )\n\t\t\t\t.some( ( field ) => {\n\t\t\t\t\tconst fieldValue = field.getValue( { item } );\n\t\t\t\t\tconst values = Array.isArray( fieldValue )\n\t\t\t\t\t\t? fieldValue\n\t\t\t\t\t\t: [ fieldValue ];\n\t\t\t\t\treturn values.some( ( value ) =>\n\t\t\t\t\t\tnormalizeSearchInput( String( value ) ).includes(\n\t\t\t\t\t\t\tnormalizedSearch\n\t\t\t\t\t\t)\n\t\t\t\t\t);\n\t\t\t\t} );\n\t\t} );\n\t}\n\n\tif ( view.filters && view.filters?.length > 0 ) {\n\t\tview.filters.forEach( ( filter ) => {\n\t\t\tconst field = _fields.find(\n\t\t\t\t( _field ) => _field.id === filter.field\n\t\t\t);\n\t\t\tif ( field ) {\n\t\t\t\t// Show deprecation warning for `isNotAll` operator.\n\t\t\t\t// We still handle this by mapping it to `isNone` in `getFilter`.\n\t\t\t\tif ( filter.operator === OPERATOR_IS_NOT_ALL ) {\n\t\t\t\t\tdeprecated( \"The 'isNotAll' filter operator\", {\n\t\t\t\t\t\tsince: '7.0',\n\t\t\t\t\t\talternative: \"'isNone'\",\n\t\t\t\t\t} );\n\t\t\t\t}\n\n\t\t\t\tconst handler = field.filter[ filter.operator as Operator ];\n\t\t\t\tif ( handler ) {\n\t\t\t\t\tfilteredData = filteredData.filter( ( item ) =>\n\t\t\t\t\t\thandler( item, field, filter.value )\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t} );\n\t}\n\n\t// Handle sorting.\n\tconst sortByField = view.sort?.field\n\t\t? _fields.find( ( field ) => {\n\t\t\t\treturn (\n\t\t\t\t\tfield.enableSorting !== false &&\n\t\t\t\t\tfield.id === view.sort?.field\n\t\t\t\t);\n\t\t } )\n\t\t: null;\n\tconst groupByField = view.groupBy?.field\n\t\t? _fields.find( ( field ) => {\n\t\t\t\treturn (\n\t\t\t\t\tfield.enableSorting !== false &&\n\t\t\t\t\tfield.id === view.groupBy?.field\n\t\t\t\t);\n\t\t } )\n\t\t: null;\n\tif ( sortByField || groupByField ) {\n\t\tfilteredData.sort( ( a, b ) => {\n\t\t\tif ( groupByField ) {\n\t\t\t\tconst groupCompare = groupByField.sort(\n\t\t\t\t\ta,\n\t\t\t\t\tb,\n\t\t\t\t\tview.groupBy?.direction ?? 'asc'\n\t\t\t\t);\n\n\t\t\t\t// If items are in different groups, return the group comparison result.\n\t\t\t\t// Otherwise, fall back to sorting by the sort field.\n\t\t\t\tif ( groupCompare !== 0 ) {\n\t\t\t\t\treturn groupCompare;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif ( sortByField ) {\n\t\t\t\treturn sortByField.sort( a, b, view.sort?.direction ?? 'desc' );\n\t\t\t}\n\n\t\t\treturn 0;\n\t\t} );\n\t}\n\n\t// Handle pagination.\n\tlet totalItems = filteredData.length;\n\tlet totalPages = 1;\n\tif ( view.page !== undefined && view.perPage !== undefined ) {\n\t\tconst start = ( view.page - 1 ) * view.perPage;\n\t\ttotalItems = filteredData?.length || 0;\n\t\ttotalPages = Math.ceil( totalItems / view.perPage );\n\t\tfilteredData = filteredData?.slice( start, start + view.perPage );\n\t}\n\n\treturn {\n\t\tdata: filteredData,\n\t\tpaginationInfo: {\n\t\t\ttotalItems,\n\t\t\ttotalPages,\n\t\t},\n\t};\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,4BAA0B;AAK1B,wBAAuB;AAKvB,uBAAoC;AACpC,yBAA4B;AAG5B,SAAS,qBAAsB,QAAQ,IAAK;AAC3C,aAAO,sBAAAA,SAAe,MAAM,KAAK,EAAE,YAAY,CAAE;AAClD;AAEA,IAAM,cAAkB,CAAC;AAWV,SAAR,sBACN,MACA,MACA,QAIC;AACD,MAAK,CAAE,MAAO;AACb,WAAO;AAAA,MACN,MAA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EAEzB,CAAE,IACF;AACH,QAAM,eAAe,KAAK,SAAS,QAChC,QAAQ,KAAM,CAAE,UAAW;AAC3B,WACC,MAAM,kBAAkB,SACxB,MAAM,OAAO,KAAK,SAAS;AAAA,EAE5B,CAAE,IACF;AACH,MAAK,eAAe,cAAe;AAClC,iBAAa,KAAM,CAAE,GAAG,MAAO;AAC9B,UAAK,cAAe;AACnB,cAAM,eAAe,aAAa;AAAA,UACjC;AAAA,UACA;AAAA,UACA,KAAK,SAAS,aAAa;AAAA,QAC5B;AAIA,YAAK,iBAAiB,GAAI;AACzB,iBAAO;AAAA,QACR;AAAA,MACD;AAEA,UAAK,aAAc;AAClB,eAAO,YAAY,KAAM,GAAG,GAAG,KAAK,MAAM,aAAa,MAAO;AAAA,MAC/D;AAEA,aAAO;AAAA,IACR,CAAE;AAAA,EACH;AAGA,MAAI,aAAa,aAAa;AAC9B,MAAI,aAAa;
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport removeAccents from 'remove-accents';\n\n/**\n * WordPress dependencies\n */\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport { OPERATOR_IS_NOT_ALL } from '../constants';\nimport normalizeFields from '../field-types';\nimport type { Field, Operator, View } from '../types';\n\nfunction normalizeSearchInput( input = '' ) {\n\treturn removeAccents( input.trim().toLowerCase() );\n}\n\nconst EMPTY_ARRAY: [] = [];\n\n/**\n * Applies the filtering, sorting and pagination to the raw data based on the view configuration.\n *\n * @param data Raw data.\n * @param view View config.\n * @param fields Fields config.\n *\n * @return Filtered, sorted and paginated data.\n */\nexport default function filterSortAndPaginate< Item >(\n\tdata: Item[],\n\tview: View,\n\tfields: Field< Item >[]\n): {\n\tdata: Item[];\n\tpaginationInfo: { totalItems: number; totalPages: number };\n} {\n\tif ( ! data ) {\n\t\treturn {\n\t\t\tdata: EMPTY_ARRAY,\n\t\t\tpaginationInfo: { totalItems: 0, totalPages: 0 },\n\t\t};\n\t}\n\tconst _fields = normalizeFields( fields );\n\tlet filteredData = [ ...data ];\n\t// Handle global search.\n\tif ( view.search ) {\n\t\tconst normalizedSearch = normalizeSearchInput( view.search );\n\t\tfilteredData = filteredData.filter( ( item ) => {\n\t\t\treturn _fields\n\t\t\t\t.filter( ( field ) => field.enableGlobalSearch )\n\t\t\t\t.some( ( field ) => {\n\t\t\t\t\tconst fieldValue = field.getValue( { item } );\n\t\t\t\t\tconst values = Array.isArray( fieldValue )\n\t\t\t\t\t\t? fieldValue\n\t\t\t\t\t\t: [ fieldValue ];\n\t\t\t\t\treturn values.some( ( value ) =>\n\t\t\t\t\t\tnormalizeSearchInput( String( value ) ).includes(\n\t\t\t\t\t\t\tnormalizedSearch\n\t\t\t\t\t\t)\n\t\t\t\t\t);\n\t\t\t\t} );\n\t\t} );\n\t}\n\n\tif ( view.filters && view.filters?.length > 0 ) {\n\t\tview.filters.forEach( ( filter ) => {\n\t\t\tconst field = _fields.find(\n\t\t\t\t( _field ) => _field.id === filter.field\n\t\t\t);\n\t\t\tif ( field ) {\n\t\t\t\t// Show deprecation warning for `isNotAll` operator.\n\t\t\t\t// We still handle this by mapping it to `isNone` in `getFilter`.\n\t\t\t\tif ( filter.operator === OPERATOR_IS_NOT_ALL ) {\n\t\t\t\t\tdeprecated( \"The 'isNotAll' filter operator\", {\n\t\t\t\t\t\tsince: '7.0',\n\t\t\t\t\t\talternative: \"'isNone'\",\n\t\t\t\t\t} );\n\t\t\t\t}\n\n\t\t\t\tconst handler = field.filter[ filter.operator as Operator ];\n\t\t\t\tif ( handler ) {\n\t\t\t\t\tfilteredData = filteredData.filter( ( item ) =>\n\t\t\t\t\t\thandler( item, field, filter.value )\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t} );\n\t}\n\n\t// Handle sorting.\n\tconst sortByField = view.sort?.field\n\t\t? _fields.find( ( field ) => {\n\t\t\t\treturn (\n\t\t\t\t\tfield.enableSorting !== false &&\n\t\t\t\t\tfield.id === view.sort?.field\n\t\t\t\t);\n\t\t } )\n\t\t: null;\n\tconst groupByField = view.groupBy?.field\n\t\t? _fields.find( ( field ) => {\n\t\t\t\treturn (\n\t\t\t\t\tfield.enableSorting !== false &&\n\t\t\t\t\tfield.id === view.groupBy?.field\n\t\t\t\t);\n\t\t } )\n\t\t: null;\n\tif ( sortByField || groupByField ) {\n\t\tfilteredData.sort( ( a, b ) => {\n\t\t\tif ( groupByField ) {\n\t\t\t\tconst groupCompare = groupByField.sort(\n\t\t\t\t\ta,\n\t\t\t\t\tb,\n\t\t\t\t\tview.groupBy?.direction ?? 'asc'\n\t\t\t\t);\n\n\t\t\t\t// If items are in different groups, return the group comparison result.\n\t\t\t\t// Otherwise, fall back to sorting by the sort field.\n\t\t\t\tif ( groupCompare !== 0 ) {\n\t\t\t\t\treturn groupCompare;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif ( sortByField ) {\n\t\t\t\treturn sortByField.sort( a, b, view.sort?.direction ?? 'desc' );\n\t\t\t}\n\n\t\t\treturn 0;\n\t\t} );\n\t}\n\n\t// Handle pagination.\n\tlet totalItems = filteredData.length;\n\tlet totalPages = 1;\n\n\t// Use position-based pagination for infinite scroll\n\tif (\n\t\tview.infiniteScrollEnabled &&\n\t\tview.startPosition !== undefined &&\n\t\tview.perPage !== undefined\n\t) {\n\t\t// Convert 1-indexed positions to 0-indexed array indices\n\t\tconst start = view.startPosition - 1;\n\t\tconst end = Math.min( start + view.perPage, totalItems );\n\t\tfilteredData = filteredData?.slice( start, end );\n\t} else if ( view.page !== undefined && view.perPage !== undefined ) {\n\t\t// Use traditional page-based pagination\n\t\tconst start = ( view.page - 1 ) * view.perPage;\n\t\ttotalItems = filteredData?.length || 0;\n\t\ttotalPages = Math.ceil( totalItems / view.perPage );\n\t\tfilteredData = filteredData?.slice( start, start + view.perPage );\n\t}\n\n\treturn {\n\t\tdata: filteredData,\n\t\tpaginationInfo: {\n\t\t\ttotalItems,\n\t\t\ttotalPages,\n\t\t},\n\t};\n}\n"],
|
|
5
|
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K,KAAM,aAAa,KAAK,OAAQ;AAClD,mBAAe,cAAc,MAAO,OAAO,QAAQ,KAAK,OAAQ;AAAA,EACjE;AAEA,SAAO;AAAA,IACN,MAAM;AAAA,IACN,gBAAgB;AAAA,MACf;AAAA,MACA;AAAA,IACD;AAAA,EACD;AACD;",
|
|
6
6
|
"names": ["removeAccents", "normalizeFields", "deprecated"]
|
|
7
7
|
}
|
|
@@ -24,7 +24,7 @@ __export(get_footer_message_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(get_footer_message_exports);
|
|
26
26
|
var import_i18n = require("@wordpress/i18n");
|
|
27
|
-
function getFooterMessage(selectionCount, itemsCount, totalItems) {
|
|
27
|
+
function getFooterMessage(selectionCount, itemsCount, totalItems, onlyTotalCount = false) {
|
|
28
28
|
if (selectionCount > 0) {
|
|
29
29
|
return (0, import_i18n.sprintf)(
|
|
30
30
|
/* translators: %d: number of items. */
|
|
@@ -32,18 +32,18 @@ function getFooterMessage(selectionCount, itemsCount, totalItems) {
|
|
|
32
32
|
selectionCount
|
|
33
33
|
);
|
|
34
34
|
}
|
|
35
|
-
if (totalItems
|
|
35
|
+
if (onlyTotalCount || totalItems <= itemsCount) {
|
|
36
36
|
return (0, import_i18n.sprintf)(
|
|
37
|
-
/* translators: %
|
|
38
|
-
(0, import_i18n._n)("%
|
|
39
|
-
itemsCount,
|
|
37
|
+
/* translators: %d: number of items. */
|
|
38
|
+
(0, import_i18n._n)("%d Item", "%d Items", totalItems),
|
|
40
39
|
totalItems
|
|
41
40
|
);
|
|
42
41
|
}
|
|
43
42
|
return (0, import_i18n.sprintf)(
|
|
44
|
-
/* translators: %d: number of items. */
|
|
45
|
-
(0, import_i18n._n)("%d Item", "%d Items",
|
|
46
|
-
itemsCount
|
|
43
|
+
/* translators: %1$d: number of items. %2$d: total number of items. */
|
|
44
|
+
(0, import_i18n._n)("%1$d of %2$d Item", "%1$d of %2$d Items", totalItems),
|
|
45
|
+
itemsCount,
|
|
46
|
+
totalItems
|
|
47
47
|
);
|
|
48
48
|
}
|
|
49
49
|
//# sourceMappingURL=get-footer-message.cjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/utils/get-footer-message.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { _n, sprintf } from '@wordpress/i18n';\n\n/**\n * Get the footer message for the DataViews footer.\n *\n * @param selectionCount - The number of items
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAA4B;
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { _n, sprintf } from '@wordpress/i18n';\n\n/**\n * Get the footer message for the DataViews footer.\n *\n * @param selectionCount - The number of selected items.\n * @param itemsCount - The number of items in the current page.\n * @param totalItems - The total number of items.\n * @param onlyTotalCount - Whether to only show the total count (used with infinite scroll).\n * @return - The footer message.\n */\nexport default function getFooterMessage(\n\tselectionCount: number,\n\titemsCount: number,\n\ttotalItems: number,\n\tonlyTotalCount = false\n): string {\n\tif ( selectionCount > 0 ) {\n\t\treturn sprintf(\n\t\t\t/* translators: %d: number of items. */\n\t\t\t_n( '%d Item selected', '%d Items selected', selectionCount ),\n\t\t\tselectionCount\n\t\t);\n\t}\n\n\t// No selection - show item count\n\tif ( onlyTotalCount || totalItems <= itemsCount ) {\n\t\treturn sprintf(\n\t\t\t/* translators: %d: number of items. */\n\t\t\t_n( '%d Item', '%d Items', totalItems ),\n\t\t\ttotalItems\n\t\t);\n\t}\n\n\treturn sprintf(\n\t\t/* translators: %1$d: number of items. %2$d: total number of items. */\n\t\t_n( '%1$d of %2$d Item', '%1$d of %2$d Items', totalItems ),\n\t\titemsCount,\n\t\ttotalItems\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAA4B;AAWb,SAAR,iBACN,gBACA,YACA,YACA,iBAAiB,OACR;AACT,MAAK,iBAAiB,GAAI;AACzB,eAAO;AAAA;AAAA,UAEN,gBAAI,oBAAoB,qBAAqB,cAAe;AAAA,MAC5D;AAAA,IACD;AAAA,EACD;AAGA,MAAK,kBAAkB,cAAc,YAAa;AACjD,eAAO;AAAA;AAAA,UAEN,gBAAI,WAAW,YAAY,UAAW;AAAA,MACtC;AAAA,IACD;AAAA,EACD;AAEA,aAAO;AAAA;AAAA,QAEN,gBAAI,qBAAqB,sBAAsB,UAAW;AAAA,IAC1D;AAAA,IACA;AAAA,EACD;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|