add-library.js
2 years ago
category-list.js
1 year ago
insert-pattern.js
2 years ago
library-cache.js
2 years ago
library-layout.js
1 year ago
library-provider.js
2 years ago
library-selector.js
1 year ago
manage-libraries.js
2 years ago
pattern-details-header.js
1 year ago
pattern-details.js
1 year ago
pattern-list.js
1 year ago
pattern-search.js
2 years ago
pattern.js
1 year ago
selected-patterns.js
1 year ago
pattern-details.js
102 lines
| 1 | import { Button } from '@wordpress/components'; |
| 2 | import { seen } from '@wordpress/icons'; |
| 3 | import { __ } from '@wordpress/i18n'; |
| 4 | import { useDispatch, useSelect } from '@wordpress/data'; |
| 5 | import { store as blockEditorStore } from '@wordpress/block-editor'; |
| 6 | import { parse } from '@wordpress/blocks'; |
| 7 | import { decodeEntities } from '@wordpress/html-entities'; |
| 8 | import { useLibrary } from './library-provider'; |
| 9 | import { InsertPattern } from './insert-pattern'; |
| 10 | import { isEmptyContentBlock, updateUniqueIds } from '../utils'; |
| 11 | |
| 12 | export function PatternDetails( { |
| 13 | pattern, |
| 14 | patternRef = null, |
| 15 | children, |
| 16 | showPreview = true, |
| 17 | bulkInsertEnabled, |
| 18 | showTitle = true, |
| 19 | globalStyleData, |
| 20 | closeModal, |
| 21 | readOnly, |
| 22 | } ) { |
| 23 | const { |
| 24 | setActivePatternId, |
| 25 | setScrollPosition, |
| 26 | } = useLibrary(); |
| 27 | const { insertBlocks, replaceBlock } = useDispatch( blockEditorStore ); |
| 28 | const { getBlockInsertionPoint, getSelectedBlock } = useSelect( ( select ) => select( blockEditorStore ), [] ); |
| 29 | const { updateBlockAttributes } = useDispatch( blockEditorStore ); |
| 30 | |
| 31 | return ( |
| 32 | <div className="gb-pattern-details"> |
| 33 | { !! showTitle && ( |
| 34 | <h3>{ decodeEntities( pattern.label ) }</h3> |
| 35 | ) } |
| 36 | |
| 37 | <div className="gb-pattern-details__actions"> |
| 38 | { ! bulkInsertEnabled && ! readOnly && ( |
| 39 | <InsertPattern |
| 40 | label={ __( 'Insert', 'generateblocks' ) } |
| 41 | onClick={ async( e ) => { |
| 42 | e.stopPropagation(); |
| 43 | |
| 44 | const blockInsertionPoint = getBlockInsertionPoint(); |
| 45 | const selectedBlock = getSelectedBlock(); |
| 46 | const renderedPattern = parse( pattern.pattern ); |
| 47 | const updatedBlocks = updateUniqueIds( renderedPattern ); |
| 48 | |
| 49 | updatedBlocks.forEach( ( block ) => { |
| 50 | if ( block.attributes && block.clientId ) { |
| 51 | updateBlockAttributes( block.clientId, block.attributes ); |
| 52 | } |
| 53 | } ); |
| 54 | |
| 55 | const isEmptyContent = isEmptyContentBlock( selectedBlock ); |
| 56 | |
| 57 | if ( isEmptyContent ) { |
| 58 | await replaceBlock( |
| 59 | selectedBlock.clientId, |
| 60 | updatedBlocks |
| 61 | ); |
| 62 | } else { |
| 63 | await insertBlocks( |
| 64 | updatedBlocks, |
| 65 | blockInsertionPoint?.index ?? 0, |
| 66 | blockInsertionPoint.rootClientId ?? '' |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | closeModal(); |
| 71 | } } |
| 72 | patterns={ [ pattern ] } |
| 73 | globalStyleData={ globalStyleData } |
| 74 | /> |
| 75 | ) } |
| 76 | |
| 77 | { ( showPreview && ! bulkInsertEnabled ) && ( |
| 78 | <Button |
| 79 | variant="tertiary" |
| 80 | icon={ seen } |
| 81 | label={ __( 'Preview', 'generateblocks' ) } |
| 82 | showTooltip |
| 83 | onClick={ ( e ) => { |
| 84 | e.stopPropagation(); |
| 85 | setActivePatternId( pattern.id ); |
| 86 | |
| 87 | if ( patternRef ) { |
| 88 | const patternContent = patternRef.current.closest( '.gb-pattern-library__content' ); |
| 89 | |
| 90 | if ( patternContent ) { |
| 91 | setScrollPosition( patternContent.scrollTop ); |
| 92 | } |
| 93 | } |
| 94 | } } |
| 95 | /> |
| 96 | ) } |
| 97 | { children } |
| 98 | </div> |
| 99 | </div> |
| 100 | ); |
| 101 | } |
| 102 |