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