PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.9.0
GenerateBlocks v1.9.0
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 / library-layout.js
generateblocks / src / pattern-library / components Last commit date
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
library-layout.js
203 lines
1 import { Button } from '@wordpress/components';
2 import { close, arrowLeft } from '@wordpress/icons';
3 import { __ } from '@wordpress/i18n';
4 import { useState, useEffect, useRef } from '@wordpress/element';
5 import CategoryList from './category-list';
6 import LibrarySelector from './library-selector';
7 import { useLibrary } from './library-provider';
8 import PatternList from './pattern-list';
9 import PatternSearch from './pattern-search';
10 import { SelectedPatterns } from './selected-patterns';
11 import { PatternDetailsHeader } from './pattern-details-header';
12 import LibraryCache from './library-cache';
13 import ManageLibraries from './manage-libraries';
14 import getIcon from '../../utils/get-icon';
15 import { doAction } from '@wordpress/hooks';
16
17 const searchCache = {};
18
19 export default function LibraryLayout( { closeModal } ) {
20 const {
21 activePatternId,
22 setActivePatternId,
23 patterns,
24 setScrollPosition,
25 scrollPosition,
26 activeCategory,
27 search,
28 setSearch,
29 isLocal,
30 activeLibrary,
31 selectedPatterns,
32 } = useLibrary();
33 const [ bulkInsertEnabled, setBulkInsertEnabled ] = useState( false );
34 const [ filteredPatterns, setFilteredPatterns ] = useState( patterns );
35 const [ globalStyleCSS, setGlobalStyleCSS ] = useState( '' );
36 const [ globalStyleData, setGlobalStyleData ] = useState( [] );
37 const [ cacheIsClearing, setCacheIsClearing ] = useState( false );
38 const activePattern = patterns.find( ( pattern ) => activePatternId === pattern.id );
39 const patternContentRef = useRef();
40
41 function filterPatterns( value ) {
42 return patterns.filter( ( pattern ) => {
43 const viewingAll = activeCategory === '';
44 const stringMatch = pattern.label.toLowerCase().includes( value.toLowerCase() );
45 const categoryMatch = pattern.categories.includes( activeCategory );
46
47 return viewingAll ? stringMatch : stringMatch && categoryMatch;
48 } );
49 }
50
51 useEffect( () => {
52 if ( activeCategory === '' ) {
53 setFilteredPatterns( patterns );
54 } else {
55 setFilteredPatterns( filterPatterns( search ) );
56 }
57
58 if ( patternContentRef.current ) {
59 patternContentRef.current.scrollTop = 0;
60 }
61 }, [ patterns, activeCategory ] );
62
63 function maybeGetCachedSearchResult( value ) {
64 const category = activeCategory === '' ? 'all' : activeCategory;
65
66 if ( ! searchCache[ category ] ) {
67 searchCache[ category ] = {};
68 }
69
70 if ( ! searchCache[ category ][ value ] ) {
71 return false;
72 }
73
74 return searchCache[ category ][ value ];
75 }
76
77 const contentStyles = {};
78
79 if ( activePatternId ) {
80 contentStyles.gridColumn = '1 / -1';
81 }
82
83 doAction(
84 'generateblocks.patterns.patternsList',
85 { activeLibrary, setGlobalStyleCSS, setGlobalStyleData, isLocal, cacheIsClearing }
86 );
87
88 return (
89 <div className="gb-pattern-library">
90 <div className="gb-pattern-library__header">
91 <div className="gb-pattern-library__header-title">
92 { ! activePatternId
93 ? <h1>{ getIcon( 'generateblocks' ) } { __( 'Pattern Library', 'generateblocks' ) }</h1>
94 : <h1>{ activePattern.label }</h1>
95 }
96 </div>
97
98 <div className="gb-pattern-library__header-action">
99 { ! activePatternId
100 ? <LibrarySelector />
101 : (
102 <PatternDetailsHeader
103 pattern={ activePattern }
104 bulkInsertEnabled={ bulkInsertEnabled }
105 globalStyleData={ globalStyleData }
106 closeModal={ closeModal }
107 />
108 )
109 }
110 </div>
111
112 <div className="gb-pattern-library__header-close">
113 { ! activePatternId
114 ? (
115 <>
116 <LibraryCache
117 setCacheIsClearing={ setCacheIsClearing }
118 cacheIsClearing={ cacheIsClearing }
119 />
120 <ManageLibraries />
121 <Button
122 variant="tertiary"
123 icon={ close }
124 label={ __( 'Close Pattern Library', 'generateblocks' ) }
125 showTooltip={ true }
126 onClick={ closeModal }
127 />
128 </>
129 ) : (
130 <Button
131 icon={ arrowLeft }
132 onClick={ () => {
133 setActivePatternId( '' );
134 setScrollPosition( scrollPosition );
135 } }
136 >
137 { __( 'Return to library' ) }
138 </Button>
139 )
140 }
141 </div>
142 </div>
143 <div className="gb-pattern-library__sidebar">
144 { ! activePatternId &&
145 <>
146 <PatternSearch onChange={ ( value ) => {
147 setSearch( value );
148
149 const category = activeCategory === '' ? 'all' : activeCategory;
150 // Check if result has been cached already
151 const cachedResult = maybeGetCachedSearchResult( value );
152
153 if ( cachedResult ) {
154 setFilteredPatterns( cachedResult );
155 return;
156 }
157
158 const newPatternList = filterPatterns( value );
159
160 searchCache[ category ][ value ] = newPatternList;
161
162 setFilteredPatterns( newPatternList );
163 } } />
164
165 <CategoryList
166 bulkInsertEnabled={ bulkInsertEnabled }
167 selectedPatterns={ selectedPatterns }
168 />
169
170 { ! bulkInsertEnabled ? (
171 <Button
172 variant="primary"
173 onClick={ () => setBulkInsertEnabled( true ) }
174 >
175 { __( 'Bulk Insert', 'generateblocks' ) }
176 </Button>
177 ) : (
178 <SelectedPatterns
179 closeModal={ closeModal }
180 globalStyleData={ globalStyleData }
181 setBulkInsertEnabled={ setBulkInsertEnabled }
182 />
183 ) }
184 </>
185 }
186 </div>
187 <div
188 className="gb-pattern-library__content"
189 style={ contentStyles }
190 ref={ patternContentRef }
191 >
192 <PatternList
193 patterns={ filteredPatterns }
194 bulkInsertEnabled={ bulkInsertEnabled }
195 closeModal={ closeModal }
196 globalStyleCSS={ globalStyleCSS }
197 globalStyleData={ globalStyleData }
198 />
199 </div>
200 </div>
201 );
202 }
203