-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: Media field changing ui to Dataviews and content preview field t…
…o posts and pages.
- Loading branch information
1 parent
9cba28a
commit 0555cab
Showing
13 changed files
with
395 additions
and
58 deletions.
There are no files selected for viewing
252 changes: 206 additions & 46 deletions
252
packages/dataviews/src/components/dataviews-view-config/index.tsx
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
packages/editor/src/dataviews/fields/content-preview/content-preview-view.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { | ||
BlockPreview, | ||
privateApis as blockEditorPrivateApis, | ||
// @ts-ignore | ||
} from '@wordpress/block-editor'; | ||
import type { BasePost } from '@wordpress/fields'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { useEntityBlockEditor, store as coreStore } from '@wordpress/core-data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { EditorProvider } from '../../../components/provider'; | ||
import { unlock } from '../../../lock-unlock'; | ||
// @ts-ignore | ||
import { store as editorStore } from '../../../store'; | ||
|
||
const { useGlobalStyle } = unlock( blockEditorPrivateApis ); | ||
|
||
function ContentPreviewContainer( { | ||
template, | ||
post, | ||
}: { | ||
template: any; | ||
post: any; | ||
} ) { | ||
const [ backgroundColor = 'white' ] = useGlobalStyle( 'color.background' ); | ||
const [ postBlocks ] = useEntityBlockEditor( 'postType', post.type, { | ||
id: post.id, | ||
} ); | ||
const [ templateBlocks ] = useEntityBlockEditor( | ||
'postType', | ||
template?.type, | ||
{ | ||
id: template?.id, | ||
} | ||
); | ||
const blocks = template && templateBlocks ? templateBlocks : postBlocks; | ||
const isEmpty = ! blocks?.length; | ||
return ( | ||
<div | ||
className="editor-fields-content-preview" | ||
style={ { | ||
backgroundColor, | ||
} } | ||
> | ||
{ isEmpty && ( | ||
<span className="editor-fields-content-preview__empty"> | ||
{ __( 'Empty content' ) } | ||
</span> | ||
) } | ||
{ ! isEmpty && ( | ||
<BlockPreview.Async> | ||
<BlockPreview blocks={ blocks } /> | ||
</BlockPreview.Async> | ||
) } | ||
</div> | ||
); | ||
} | ||
|
||
export default function ContentPreviewView( { item }: { item: BasePost } ) { | ||
const { settings, template } = useSelect( | ||
( select ) => { | ||
const { canUser, getPostType, getTemplateId, getEntityRecord } = | ||
unlock( select( coreStore ) ); | ||
const canViewTemplate = canUser( 'read', { | ||
kind: 'postType', | ||
name: 'wp_template', | ||
} ); | ||
const _settings = select( editorStore ).getEditorSettings(); | ||
// @ts-ignore | ||
const supportsTemplateMode = _settings.supportsTemplateMode; | ||
const isViewable = getPostType( item.type )?.viewable ?? false; | ||
|
||
const templateId = | ||
supportsTemplateMode && isViewable && canViewTemplate | ||
? getTemplateId( item.type, item.id ) | ||
: null; | ||
return { | ||
settings: _settings, | ||
template: templateId | ||
? getEntityRecord( 'postType', 'wp_template', templateId ) | ||
: undefined, | ||
}; | ||
}, | ||
[ item.type, item.id ] | ||
); | ||
// 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 ( | ||
<EditorProvider | ||
post={ item } | ||
settings={ settings } | ||
__unstableTemplate={ template } | ||
> | ||
<ContentPreviewContainer template={ template } post={ item } /> | ||
</EditorProvider> | ||
); | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/editor/src/dataviews/fields/content-preview/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import type { Field } from '@wordpress/dataviews'; | ||
import type { BasePost } from '@wordpress/fields'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ContentPreviewView from './content-preview-view'; | ||
|
||
const contentPreviewField: Field< BasePost > = { | ||
type: 'text', | ||
id: 'content-preview', | ||
label: __( 'Content preview' ), | ||
render: ContentPreviewView, | ||
enableSorting: false, | ||
isMediaField: true, | ||
}; | ||
|
||
export default contentPreviewField; |
21 changes: 21 additions & 0 deletions
21
packages/editor/src/dataviews/fields/content-preview/style.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
.editor-fields-content-preview { | ||
display: flex; | ||
flex-direction: column; | ||
height: 100%; | ||
border-radius: $radius-medium; | ||
|
||
.dataviews-view-table & { | ||
width: 96px; | ||
flex-grow: 0; | ||
} | ||
|
||
.block-editor-block-preview__container, | ||
.editor-fields-content-preview__empty { | ||
margin-top: auto; | ||
margin-bottom: auto; | ||
} | ||
} | ||
|
||
.editor-fields-content-preview__empty { | ||
text-align: center; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from './fields'; | ||
export * from './actions'; | ||
export { default as CreateTemplatePartModal } from './components/create-template-part-modal'; | ||
export type { BasePostWithEmbeddedAuthor, PostType } from './types'; | ||
export type { BasePostWithEmbeddedAuthor, BasePost, PostType } from './types'; |