content.js
1 year ago
filters.js
1 year ago
header.js
1 year ago
import-modal.js
1 year ago
list-item.js
1 year ago
notices.js
5 years ago
pagination.js
5 years ago
preview.js
1 year ago
publish-button.js
2 years ago
template-predefine.js
2 years ago
templates-content.js
1 year ago
tpc-templates-button.js
2 years ago
preview.js
104 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { parse } from '@wordpress/blocks'; |
| 3 | import { Button, Placeholder, Spinner } from '@wordpress/components'; |
| 4 | import { useViewportMatch } from '@wordpress/compose'; |
| 5 | import { useDispatch, useSelect } from '@wordpress/data'; |
| 6 | import { useEffect, useState } from '@wordpress/element'; |
| 7 | import { BlockPreview } from '@wordpress/block-editor'; |
| 8 | |
| 9 | import { importTemplate } from './../data/templates-cloud/index'; |
| 10 | |
| 11 | const Preview = ( { isFetching, importBlocks } ) => { |
| 12 | const isLarger = useViewportMatch( 'large', '>=' ); |
| 13 | const isLarge = useViewportMatch( 'large', '<=' ); |
| 14 | const isSmall = useViewportMatch( 'small', '>=' ); |
| 15 | const isSmaller = useViewportMatch( 'small', '<=' ); |
| 16 | |
| 17 | let viewportWidth = 1400; |
| 18 | |
| 19 | const isTablet = ! isLarger && ! isLarge && isSmall && ! isSmaller; |
| 20 | const isMobile = ! isLarger && ! isLarge && ! isSmall && ! isSmaller; |
| 21 | |
| 22 | if ( isTablet ) { |
| 23 | viewportWidth = 960; |
| 24 | } else if ( isMobile ) { |
| 25 | viewportWidth = 600; |
| 26 | } |
| 27 | |
| 28 | const { setFetching, togglePreview } = useDispatch( 'tpc/block-editor' ); |
| 29 | |
| 30 | const { item } = useSelect( ( select ) => |
| 31 | select( 'tpc/block-editor' ).getPreview() |
| 32 | ); |
| 33 | |
| 34 | useEffect( () => { |
| 35 | init(); |
| 36 | }, [] ); |
| 37 | |
| 38 | const [ content, setContent ] = useState( '' ); |
| 39 | |
| 40 | const init = async () => { |
| 41 | setFetching( true ); |
| 42 | await importContent(); |
| 43 | setFetching( false ); |
| 44 | }; |
| 45 | |
| 46 | const importContent = async () => { |
| 47 | setFetching( true ); |
| 48 | const data = await importTemplate( item.template_id ); |
| 49 | |
| 50 | if ( data.__file && data.content && 'wp_export' === data.__file ) { |
| 51 | setContent( data.content ); |
| 52 | } |
| 53 | setFetching( false ); |
| 54 | }; |
| 55 | |
| 56 | const importItem = () => { |
| 57 | togglePreview(); |
| 58 | importBlocks( content, item.meta || [] ); |
| 59 | }; |
| 60 | |
| 61 | return ( |
| 62 | <div className="tpc-modal-content"> |
| 63 | <div className="preview-header"> |
| 64 | <div className="left"> |
| 65 | { item.template_name || __( 'Template','templates-patterns-collection' ) } |
| 66 | </div> |
| 67 | |
| 68 | <div className="right"> |
| 69 | <Button isSecondary onClick={ togglePreview }> |
| 70 | { __( |
| 71 | 'Close Preview', |
| 72 | 'templates-patterns-collection' |
| 73 | ) } |
| 74 | </Button> |
| 75 | |
| 76 | <Button |
| 77 | isPrimary |
| 78 | isBusy={ isFetching } |
| 79 | disabled={ isFetching } |
| 80 | onClick={ importItem } |
| 81 | > |
| 82 | { __( 'Import', 'templates-patterns-collection' ) } |
| 83 | </Button> |
| 84 | </div> |
| 85 | </div> |
| 86 | |
| 87 | { isFetching ? ( |
| 88 | <Placeholder> |
| 89 | <Spinner /> |
| 90 | </Placeholder> |
| 91 | ) : ( |
| 92 | <div className="preview-content"> |
| 93 | <BlockPreview |
| 94 | blocks={ parse( content ) } |
| 95 | viewportWidth={ viewportWidth } |
| 96 | /> |
| 97 | </div> |
| 98 | ) } |
| 99 | </div> |
| 100 | ); |
| 101 | }; |
| 102 | |
| 103 | export default Preview; |
| 104 |