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
selected-patterns.js
136 lines
| 1 | import { useDispatch, useSelect } from '@wordpress/data'; |
| 2 | import { __ } from '@wordpress/i18n'; |
| 3 | import { Button } from '@wordpress/components'; |
| 4 | import { store as blockEditorStore } from '@wordpress/block-editor'; |
| 5 | import { parse } from '@wordpress/blocks'; |
| 6 | import { lineSolid, seen } from '@wordpress/icons'; |
| 7 | import { useRef } from '@wordpress/element'; |
| 8 | import { SortableList } from '../../components/dnd'; |
| 9 | import { useLibrary } from './library-provider'; |
| 10 | import { InsertPattern } from './insert-pattern'; |
| 11 | import { isEmptyContentBlock, updateUniqueIds } from '../utils'; |
| 12 | |
| 13 | export function SelectedPatterns( { closeModal, globalStyleData, setBulkInsertEnabled } ) { |
| 14 | const { insertBlocks, replaceBlock } = useDispatch( blockEditorStore ); |
| 15 | const { |
| 16 | selectedPatterns = [], |
| 17 | selectedPatternsDispatch, |
| 18 | setActivePatternId, |
| 19 | setScrollPosition, |
| 20 | } = useLibrary(); |
| 21 | const { getBlockInsertionPoint, getSelectedBlock } = useSelect( ( select ) => select( blockEditorStore ), [] ); |
| 22 | const { updateBlockAttributes } = useDispatch( blockEditorStore ); |
| 23 | const ZeroWidthSpace = () => <>​</>; |
| 24 | |
| 25 | function SelectedPattern( { item: pattern } ) { |
| 26 | const ref = useRef( null ); |
| 27 | |
| 28 | return ( |
| 29 | <div |
| 30 | id={ `selected-pattern-${ pattern.id }` } |
| 31 | className="gb-selected-pattern" |
| 32 | ref={ ref } |
| 33 | > |
| 34 | <span className="gb-selected-pattern__label" title={ pattern.label }> |
| 35 | { pattern.label } |
| 36 | </span> |
| 37 | <div className="gb-selected-pattern__actions"> |
| 38 | <ZeroWidthSpace /> |
| 39 | <Button |
| 40 | variant="tertiary" |
| 41 | icon={ lineSolid } |
| 42 | label={ __( 'Remove Pattern', 'generateblocks' ) } |
| 43 | onClick={ () => { |
| 44 | selectedPatternsDispatch( { type: 'REMOVE', pattern } ); |
| 45 | } } |
| 46 | /> |
| 47 | <Button |
| 48 | variant="tertiary" |
| 49 | icon={ seen } |
| 50 | label={ __( 'Preview Pattern', 'generateblocks' ) } |
| 51 | showTooltip |
| 52 | onClick={ () => { |
| 53 | setActivePatternId( pattern.id ); |
| 54 | const patternContent = ref.current.closest( '.gb-pattern-library__content' ); |
| 55 | |
| 56 | if ( patternContent ) { |
| 57 | setScrollPosition( patternContent.scrollTop ); |
| 58 | } |
| 59 | } } |
| 60 | /> |
| 61 | </div> |
| 62 | </div> |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | return ( |
| 67 | <aside className="gb-selected-patterns"> |
| 68 | <h3 className="gb-selected-patterns__headline"> |
| 69 | { __( 'Bulk Insert', 'generateblocks' ) } |
| 70 | </h3> |
| 71 | |
| 72 | { ! selectedPatterns.length && ( |
| 73 | <p>{ __( 'Select patterns to insert.', 'generateblocks' ) }</p> |
| 74 | ) } |
| 75 | |
| 76 | <SortableList |
| 77 | className="gb-selected-patterns__list" |
| 78 | items={ selectedPatterns } |
| 79 | dragHandleLabel={ __( 'Reorder Pattern', 'generateblocks' ) } |
| 80 | setItems={ ( items ) => { |
| 81 | selectedPatternsDispatch( { type: 'SET', patterns: items } ); |
| 82 | } } |
| 83 | itemComponent={ SelectedPattern } |
| 84 | dragHandle={ true } |
| 85 | /> |
| 86 | |
| 87 | <div style={ { display: 'flex', gap: '5px', justifyContent: 'space-between', marginTop: '1em' } }> |
| 88 | <InsertPattern |
| 89 | label={ __( 'Insert All', 'generateblocks' ) } |
| 90 | patterns={ selectedPatterns } |
| 91 | globalStyleData={ globalStyleData } |
| 92 | disabled={ ! selectedPatterns.length } |
| 93 | onClick={ async() => { |
| 94 | const blockReplacements = selectedPatterns.reduce( ( prev, current ) => prev + current.pattern, '' ); |
| 95 | const blockInsertionPoint = getBlockInsertionPoint(); |
| 96 | const renderedPatterns = parse( blockReplacements ); |
| 97 | const updatedBlocks = updateUniqueIds( renderedPatterns ); |
| 98 | const selectedBlock = getSelectedBlock(); |
| 99 | |
| 100 | updatedBlocks.forEach( ( block ) => { |
| 101 | if ( block.attributes && block.clientId ) { |
| 102 | updateBlockAttributes( block.clientId, block.attributes ); |
| 103 | } |
| 104 | } ); |
| 105 | |
| 106 | const isEmptyContent = isEmptyContentBlock( selectedBlock ); |
| 107 | |
| 108 | if ( isEmptyContent ) { |
| 109 | await replaceBlock( |
| 110 | selectedBlock.clientId, |
| 111 | updatedBlocks |
| 112 | ); |
| 113 | } else { |
| 114 | await insertBlocks( |
| 115 | updatedBlocks, |
| 116 | blockInsertionPoint?.index ?? 0, |
| 117 | blockInsertionPoint.rootClientId ?? '' |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | closeModal(); |
| 122 | } } |
| 123 | /> |
| 124 | |
| 125 | <Button |
| 126 | variant="secondary" |
| 127 | onClick={ () => setBulkInsertEnabled( false ) } |
| 128 | isDestructive |
| 129 | > |
| 130 | { __( 'Cancel', 'generateblocks' ) } |
| 131 | </Button> |
| 132 | </div> |
| 133 | </aside> |
| 134 | ); |
| 135 | } |
| 136 |