-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dataviews: Add preview and grid view in templates list #56382
Changes from all commits
cc74d98
bebe1e8
b6a6f72
53b2456
eeb8fd9
dfd420a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,11 @@ import { __, _x } from '@wordpress/i18n'; | |
import { useState, useMemo, useCallback } from '@wordpress/element'; | ||
import { useEntityRecords } from '@wordpress/core-data'; | ||
import { decodeEntities } from '@wordpress/html-entities'; | ||
import { parse } from '@wordpress/blocks'; | ||
import { | ||
BlockPreview, | ||
privateApis as blockEditorPrivateApis, | ||
} from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -31,17 +36,28 @@ import { | |
deleteTemplateAction, | ||
renameTemplateAction, | ||
} from './template-actions'; | ||
import usePatternSettings from '../page-patterns/use-pattern-settings'; | ||
import { unlock } from '../../lock-unlock'; | ||
|
||
const { ExperimentalBlockEditorProvider } = unlock( blockEditorPrivateApis ); | ||
|
||
const EMPTY_ARRAY = []; | ||
|
||
const defaultConfigPerViewType = { | ||
list: {}, | ||
grid: { | ||
mediaField: 'preview', | ||
}, | ||
}; | ||
|
||
const DEFAULT_VIEW = { | ||
type: 'list', | ||
search: '', | ||
page: 1, | ||
perPage: 20, | ||
// All fields are visible by default, so it's | ||
// better to keep track of the hidden ones. | ||
hiddenFields: [], | ||
hiddenFields: [ 'preview' ], | ||
layout: {}, | ||
}; | ||
|
||
|
@@ -94,6 +110,32 @@ function AuthorField( { item } ) { | |
); | ||
} | ||
|
||
function TemplatePreview( { content, viewType } ) { | ||
const settings = usePatternSettings(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit weird that we use this hook when this page is unrelated to patterns. Looking at the data the hook provides, it sounds like we'd only need the settings from the site editor (everything else is related to patterns). Can we pull those settings directly here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This hook is misnamed I think. It's about providing the block editor with the settings it needs to render anything (with all the patterns, styles...) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually this hook loads the patterns are needed here too, in order to render previews with patterns inside. These patterns are loaded within a Block Editor instance and in our case we don't load one. |
||
const blocks = useMemo( () => { | ||
return parse( content ); | ||
}, [ content ] ); | ||
if ( ! blocks?.length ) { | ||
return null; | ||
} | ||
// Wrap everything in a block editor provider to ensure 'styles' that are needed | ||
// for the previews are synced between the site editor store and the block editor store. | ||
// Additionally we need to have the `__experimentalBlockPatterns` setting in order to | ||
// render patterns inside the previews. | ||
// TODO: Same approach is used in the patterns list and it becomes obvious that some of | ||
// the block editor settings are needed in context where we don't have the block editor. | ||
// Explore how we can solve this in a better way. | ||
return ( | ||
<ExperimentalBlockEditorProvider settings={ settings }> | ||
<div | ||
className={ `page-templates-preview-field is-viewtype-${ viewType }` } | ||
> | ||
<BlockPreview blocks={ blocks } /> | ||
</div> | ||
</ExperimentalBlockEditorProvider> | ||
); | ||
} | ||
|
||
export default function DataviewsTemplates() { | ||
const [ view, setView ] = useState( DEFAULT_VIEW ); | ||
const { records: allTemplates, isResolving: isLoadingData } = | ||
|
@@ -153,6 +195,21 @@ export default function DataviewsTemplates() { | |
}, [ allTemplates, view ] ); | ||
const fields = useMemo( | ||
() => [ | ||
{ | ||
header: __( 'Preview' ), | ||
id: 'preview', | ||
render: ( { item, view: { type: viewType } } ) => { | ||
return ( | ||
<TemplatePreview | ||
content={ item.content.raw } | ||
viewType={ viewType } | ||
/> | ||
); | ||
}, | ||
minWidth: 120, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mentioned elsewhere, but this data is only for one of the layouts (list). We should find a different way to provide this. Either by making it explicit in the field API (e.g.: scope it under Not to fix in this PR, but important to figure out before stabilizing/creating a package for dataviews. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, but let's explore this separately. Probably it should be enough by just adding specific css on each field, per view type. |
||
maxWidth: 120, | ||
enableSorting: false, | ||
}, | ||
{ | ||
header: __( 'Template' ), | ||
id: 'title', | ||
|
@@ -180,7 +237,6 @@ export default function DataviewsTemplates() { | |
{ | ||
header: __( 'Author' ), | ||
id: 'author', | ||
getValue: () => {}, | ||
render: ( { item } ) => <AuthorField item={ item } />, | ||
enableHiding: false, | ||
enableSorting: false, | ||
|
@@ -199,10 +255,19 @@ export default function DataviewsTemplates() { | |
); | ||
const onChangeView = useCallback( | ||
( viewUpdater ) => { | ||
const updatedView = | ||
let updatedView = | ||
typeof viewUpdater === 'function' | ||
? viewUpdater( view ) | ||
: viewUpdater; | ||
if ( updatedView.type !== view.type ) { | ||
updatedView = { | ||
...updatedView, | ||
layout: { | ||
...defaultConfigPerViewType[ updatedView.type ], | ||
}, | ||
}; | ||
} | ||
|
||
setView( updatedView ); | ||
}, | ||
[ view, setView ] | ||
|
@@ -218,7 +283,7 @@ export default function DataviewsTemplates() { | |
isLoading={ isLoadingData } | ||
view={ view } | ||
onChangeView={ onChangeView } | ||
supportedLayouts={ [ 'list' ] } | ||
supportedLayouts={ [ 'list', 'grid' ] } | ||
/> | ||
</Page> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.page-templates-preview-field { | ||
.block-editor-block-preview__container { | ||
border: 1px solid $gray-300; | ||
border-radius: $radius-block-ui; | ||
} | ||
|
||
&.is-viewtype-list { | ||
.block-editor-block-preview__container { | ||
height: 120px; | ||
} | ||
} | ||
|
||
&.is-viewtype-grid { | ||
.block-editor-block-preview__container { | ||
height: 320px; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still consider a bug that the preview field cannot be hidden in the grid layout :/ Not to fix in this PR, though.
Gravacao.do.ecra.2023-11-23.as.11.25.43.mov