Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/dataviews/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- DataForm: support `isDisabled` field property. [#77090](https://github.com/WordPress/gutenberg/pull/77090)
- DataViews/DataViewsPicker: simplify `defaultLayouts` property. [#77232](https://github.com/WordPress/gutenberg/pull/77232)
- DataForm: Show tooltip in edit button in `panel` layout. [#77024](https://github.com/WordPress/gutenberg/pull/77024)
- DataViewsPicker: Add `onReset` prop to support view reset functionality. [#77288](https://github.com/WordPress/gutenberg/pull/77288)

### Bug Fixes

Expand Down
3 changes: 3 additions & 0 deletions packages/dataviews/src/dataviews-picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type DataViewsPickerProps< Item > = {
};
itemListLabel?: string;
empty?: ReactNode;
onReset?: ( () => void ) | false;
} & ( Item extends ItemWithId
? { getItemId?: ( item: Item ) => string }
: { getItemId: ( item: Item ) => string } );
Expand Down Expand Up @@ -143,6 +144,7 @@ function DataViewsPicker< Item >( {
config = { perPageSizes: [ 10, 20, 50, 100 ] },
itemListLabel,
empty,
onReset,
}: DataViewsPickerProps< Item > ) {
// useData ensures data loading is correct whether infinite scroll is enabled or pagination is used.
const { data: displayData, setVisibleEntries } = useData( {
Expand Down Expand Up @@ -250,6 +252,7 @@ function DataViewsPicker< Item >( {
config,
itemListLabel,
empty,
onReset,
hasInitiallyLoaded: true,
intersectionObserver,
} }
Expand Down
1 change: 1 addition & 0 deletions packages/media-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@wordpress/notices": "file:../notices",
"@wordpress/private-apis": "file:../private-apis",
"@wordpress/ui": "file:../ui",
"@wordpress/views": "file:../views",
"clsx": "^2.1.1"
},
"peerDependencies": {
Expand Down
90 changes: 48 additions & 42 deletions packages/media-utils/src/components/media-upload-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ import { Modal, DropZone, FormFileUpload, Button } from '@wordpress/components';
import { upload as uploadIcon } from '@wordpress/icons';
import { DataViewsPicker } from '@wordpress/dataviews';
import type {
View,
Field,
ActionButton,
SupportedLayouts,
View,
} from '@wordpress/dataviews';
import { useView } from '@wordpress/views';
import { Stack } from '@wordpress/ui';
import {
altTextField,
Expand Down Expand Up @@ -69,6 +70,43 @@ const NOTICES_CONTEXT = 'media-modal';
// Notice ID - reused for all upload-related notices to prevent flooding
const NOTICE_ID_UPLOAD_PROGRESS = 'media-modal-upload-progress';

const defaultView: View = {
type: LAYOUT_PICKER_GRID,
fields: [],
showTitle: false,
titleField: 'title',
mediaField: 'media_thumbnail',
search: '',
page: 1,
perPage: 50,
filters: [],
layout: {
previewSize: 170,
density: 'compact',
},
};

const defaultLayouts: SupportedLayouts = {
[ LAYOUT_PICKER_GRID ]: {
fields: [],
showTitle: false,
layout: {
previewSize: 170,
density: 'compact',
},
},
[ LAYOUT_PICKER_TABLE ]: {
fields: [
'filename',
'filesize',
'media_dimensions',
'author',
'date',
],
showTitle: true,
},
};

interface MediaUploadModalProps {
/**
* Array of allowed media types.
Expand Down Expand Up @@ -193,22 +231,13 @@ export function MediaUploadModal( {
const invalidateAttachmentResolutions =
useInvalidateAttachmentResolutions();

// DataViews configuration - allow view updates
const [ view, setView ] = useState< View >( () => ( {
type: LAYOUT_PICKER_GRID,
fields: [],
showTitle: false,
titleField: 'title',
mediaField: 'media_thumbnail',
search: '',
page: 1,
perPage: 50,
filters: [],
layout: {
previewSize: 170,
density: 'compact',
},
} ) );
// Persist view configuration across sessions via the preferences store.
const { view, updateView, isModified, resetToDefault } = useView( {
kind: 'postType',
name: 'attachment',
slug: 'media-modal',
defaultView,
} );

// Build query args based on view properties, similar to PostList
const queryArgs = useMemo( () => {
Expand Down Expand Up @@ -456,30 +485,6 @@ export function MediaUploadModal( {
[ totalItems, totalPages ]
);

const defaultLayouts: SupportedLayouts = useMemo(
() => ( {
[ LAYOUT_PICKER_GRID ]: {
fields: [],
showTitle: false,
layout: {
previewSize: 170,
density: 'compact',
},
},
[ LAYOUT_PICKER_TABLE ]: {
fields: [
'filename',
'filesize',
'media_dimensions',
'author',
'date',
],
showTitle: true,
},
} ),
[]
);

// Build accept attribute from allowedTypes
const acceptTypes = useMemo( () => {
if ( allowedTypes?.includes( '*' ) ) {
Expand Down Expand Up @@ -553,7 +558,7 @@ export function MediaUploadModal( {
data={ mediaRecords || [] }
fields={ fields }
view={ view }
onChangeView={ setView }
onChangeView={ updateView }
actions={ actions }
selection={ selection }
onChangeSelection={ setSelection }
Expand All @@ -562,6 +567,7 @@ export function MediaUploadModal( {
defaultLayouts={ defaultLayouts }
getItemId={ ( item: RestAttachment ) => String( item.id ) }
itemListLabel={ __( 'Media items' ) }
onReset={ isModified ? resetToDefault : false }
>
<Stack
direction="row"
Expand Down
3 changes: 2 additions & 1 deletion packages/media-utils/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
{ "path": "../media-fields" },
{ "path": "../notices" },
{ "path": "../private-apis" },
{ "path": "../ui" }
{ "path": "../ui" },
{ "path": "../views" }
]
}
Loading