PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.1.1
GenerateBlocks v2.1.1
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / pattern-library / components / selected-patterns.js
generateblocks / src / pattern-library / components Last commit date
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
selected-patterns.js
139 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
9 import { SortableList } from '@edge22/components';
10
11 import { useLibrary } from './library-provider';
12 import { InsertPattern } from './insert-pattern';
13 import { isEmptyContentBlock, updateUniqueIds } from '../utils';
14
15 export function SelectedPatterns( { closeModal, globalStyleData, setBulkInsertEnabled, filteredPatterns } ) {
16 const { insertBlocks, replaceBlock } = useDispatch( blockEditorStore );
17 const {
18 selectedPatterns = [],
19 selectedPatternsDispatch,
20 setActivePatternId,
21 setScrollPosition,
22 } = useLibrary();
23 const { getBlockInsertionPoint, getSelectedBlock } = useSelect( ( select ) => select( blockEditorStore ), [] );
24 const { updateBlockAttributes } = useDispatch( blockEditorStore );
25 const ZeroWidthSpace = () => <>&#8203;</>;
26
27 function SelectedPattern( { item: pattern } ) {
28 const ref = useRef( null );
29
30 return (
31 <div
32 id={ `selected-pattern-${ pattern.id }` }
33 className="gb-selected-pattern"
34 ref={ ref }
35 >
36 <span className="gb-selected-pattern__label" title={ pattern.label }>
37 { pattern.label }
38 </span>
39 <div className="gb-selected-pattern__actions">
40 <ZeroWidthSpace />
41 <Button
42 variant="tertiary"
43 icon={ lineSolid }
44 label={ __( 'Remove Pattern', 'generateblocks' ) }
45 onClick={ () => {
46 selectedPatternsDispatch( { type: 'REMOVE', pattern } );
47 } }
48 />
49 <Button
50 variant="tertiary"
51 icon={ seen }
52 label={ __( 'Preview Pattern', 'generateblocks' ) }
53 showTooltip
54 disabled={ ! filteredPatterns.find( ( { id } ) => id === pattern.id ) }
55 onClick={ () => {
56 setActivePatternId( pattern.id );
57 const patternContent = ref.current.closest( '.gb-pattern-library__content' );
58
59 if ( patternContent ) {
60 setScrollPosition( patternContent.scrollTop );
61 }
62 } }
63 />
64 </div>
65 </div>
66 );
67 }
68
69 return (
70 <aside className="gb-selected-patterns">
71 <h3 className="gb-selected-patterns__headline">
72 { __( 'Bulk Insert', 'generateblocks' ) }
73 </h3>
74
75 { ! selectedPatterns.length && (
76 <p>{ __( 'Select patterns to insert.', 'generateblocks' ) }</p>
77 ) }
78
79 <SortableList
80 className="gb-selected-patterns__list"
81 items={ selectedPatterns }
82 dragHandleLabel={ __( 'Reorder Pattern', 'generateblocks' ) }
83 setItems={ ( items ) => {
84 selectedPatternsDispatch( { type: 'SET', patterns: items } );
85 } }
86 itemComponent={ SelectedPattern }
87 dragHandle={ true }
88 />
89
90 <div style={ { display: 'flex', gap: '5px', justifyContent: 'space-between', marginTop: '1em' } }>
91 <InsertPattern
92 label={ __( 'Insert All', 'generateblocks' ) }
93 patterns={ selectedPatterns }
94 globalStyleData={ globalStyleData }
95 disabled={ ! selectedPatterns.length }
96 onClick={ async() => {
97 const blockReplacements = selectedPatterns.reduce( ( prev, current ) => prev + current.pattern, '' );
98 const blockInsertionPoint = getBlockInsertionPoint();
99 const renderedPatterns = parse( blockReplacements );
100 const updatedBlocks = updateUniqueIds( renderedPatterns );
101 const selectedBlock = getSelectedBlock();
102
103 updatedBlocks.forEach( ( block ) => {
104 if ( block.attributes && block.clientId ) {
105 updateBlockAttributes( block.clientId, block.attributes );
106 }
107 } );
108
109 const isEmptyContent = isEmptyContentBlock( selectedBlock );
110
111 if ( isEmptyContent ) {
112 await replaceBlock(
113 selectedBlock.clientId,
114 updatedBlocks
115 );
116 } else {
117 await insertBlocks(
118 updatedBlocks,
119 blockInsertionPoint?.index ?? 0,
120 blockInsertionPoint.rootClientId ?? ''
121 );
122 }
123
124 closeModal();
125 } }
126 />
127
128 <Button
129 variant="secondary"
130 onClick={ () => setBulkInsertEnabled( false ) }
131 isDestructive
132 >
133 { __( 'Cancel', 'generateblocks' ) }
134 </Button>
135 </div>
136 </aside>
137 );
138 }
139