UNPKG

@wordpress/editor

Version:
8 lines (7 loc) 4.95 kB
{ "version": 3, "sources": ["../../../../src/dataviews/fields/content-preview/content-preview-view.tsx"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\nimport {\n\t// @ts-ignore\n\tBlockPreview,\n\t// @ts-ignore\n} from '@wordpress/block-editor';\nimport type { BasePost } from '@wordpress/fields';\nimport { useSelect } from '@wordpress/data';\nimport { useEntityBlockEditor, store as coreStore } from '@wordpress/core-data';\n\n/**\n * Internal dependencies\n */\nimport { EditorProvider } from '../../../components/provider';\nimport { useStyle } from '../../../components/global-styles';\nimport { unlock } from '../../../lock-unlock';\n// @ts-ignore\nimport { store as editorStore } from '../../../store';\n\nfunction PostPreviewContainer( {\n\ttemplate,\n\tpost,\n}: {\n\ttemplate: any;\n\tpost: any;\n} ) {\n\tconst [ backgroundColor = 'white' ] = useStyle( 'color.background' );\n\tconst [ postBlocks ] = useEntityBlockEditor( 'postType', post.type, {\n\t\tid: post.id,\n\t} );\n\tconst [ templateBlocks ] = useEntityBlockEditor(\n\t\t'postType',\n\t\ttemplate?.type,\n\t\t{\n\t\t\tid: template?.id,\n\t\t}\n\t);\n\tconst blocks = template && templateBlocks ? templateBlocks : postBlocks;\n\tconst isEmpty = ! blocks?.length;\n\treturn (\n\t\t<div\n\t\t\tclassName=\"editor-fields-content-preview\"\n\t\t\tstyle={ {\n\t\t\t\tbackgroundColor,\n\t\t\t} }\n\t\t>\n\t\t\t{ isEmpty && (\n\t\t\t\t<span className=\"editor-fields-content-preview__empty\">\n\t\t\t\t\t{ __( 'Empty content' ) }\n\t\t\t\t</span>\n\t\t\t) }\n\t\t\t{ ! isEmpty && (\n\t\t\t\t<BlockPreview.Async>\n\t\t\t\t\t<BlockPreview blocks={ blocks } />\n\t\t\t\t</BlockPreview.Async>\n\t\t\t) }\n\t\t</div>\n\t);\n}\n\nexport default function PostPreviewView( { item }: { item: BasePost } ) {\n\tconst { settings, template } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { canUser, getPostType, getTemplateId, getEntityRecord } =\n\t\t\t\tunlock( select( coreStore ) );\n\t\t\tconst canViewTemplate = canUser( 'read', {\n\t\t\t\tkind: 'postType',\n\t\t\t\tname: 'wp_template',\n\t\t\t} );\n\t\t\tconst _settings = select( editorStore ).getEditorSettings();\n\t\t\t// @ts-ignore\n\t\t\tconst supportsTemplateMode = _settings.supportsTemplateMode;\n\t\t\tconst isViewable = getPostType( item.type )?.viewable ?? false;\n\n\t\t\tconst templateId =\n\t\t\t\tsupportsTemplateMode && isViewable && canViewTemplate\n\t\t\t\t\t? getTemplateId( item.type, item.id )\n\t\t\t\t\t: null;\n\t\t\treturn {\n\t\t\t\tsettings: _settings,\n\t\t\t\ttemplate: templateId\n\t\t\t\t\t? getEntityRecord( 'postType', 'wp_template', templateId )\n\t\t\t\t\t: undefined,\n\t\t\t};\n\t\t},\n\t\t[ item.type, item.id ]\n\t);\n\t// Wrap everything in a block editor provider to ensure 'styles' that are needed\n\t// for the previews are synced between the site editor store and the block editor store.\n\t// Additionally we need to have the `__experimentalBlockPatterns` setting in order to\n\t// render patterns inside the previews.\n\t// TODO: Same approach is used in the patterns list and it becomes obvious that some of\n\t// the block editor settings are needed in context where we don't have the block editor.\n\t// Explore how we can solve this in a better way.\n\treturn (\n\t\t<EditorProvider\n\t\t\tpost={ item }\n\t\t\tsettings={ settings }\n\t\t\t__unstableTemplate={ template }\n\t\t>\n\t\t\t<PostPreviewContainer template={ template } post={ item } />\n\t\t</EditorProvider>\n\t);\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAAmB;AACnB,0BAIO;AAEP,kBAA0B;AAC1B,uBAAyD;AAKzD,sBAA+B;AAC/B,2BAAyB;AACzB,yBAAuB;AAEvB,mBAAqC;AAuBnC;AArBF,SAAS,qBAAsB;AAAA,EAC9B;AAAA,EACA;AACD,GAGI;AACH,QAAM,CAAE,kBAAkB,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