PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 2.25.0
Block Animations, Motion & Scroll Effects – Ghost Kit v2.25.0
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / settings / pages / blocks.js

blocks.js in Block Animations, Motion & Scroll Effects – Ghost Kit 2.25.0, at settings/pages/blocks.js

340 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * External dependencies
3 */
4 import classnames from 'classnames/dedupe';
5 import { debounce } from 'throttle-debounce';
6
7 /**
8 * Internal dependencies
9 */
10 import Info from '../components/info';
11
12 /**
13 * WordPress dependencies
14 */
15 const { Component } = wp.element;
16
17 const { merge } = window.lodash;
18
19 const { apiFetch } = wp;
20
21 const { __, sprintf } = wp.i18n;
22
23 const { ToggleControl, Tooltip } = wp.components;
24
25 const { GHOSTKIT } = window;
26
27 // register core Gutenberg blocks.
28 if (wp.blockLibrary && wp.blockLibrary.registerCoreBlocks) {
29 wp.blockLibrary.registerCoreBlocks();
30 }
31
32 export default class Blocks extends Component {
33 constructor(props) {
34 super(props);
35
36 this.state = {
37 activeCategory: false,
38 disabledBlocks: GHOSTKIT.disabledBlocks || {},
39 };
40
41 this.updateDisabledBlocks = this.updateDisabledBlocks.bind(this);
42 this.updateDisabledBlocksDebounce = debounce(
43 1000,
44 this.updateDisabledBlocksDebounce.bind(this)
45 );
46 this.getBlocksCategories = this.getBlocksCategories.bind(this);
47 this.getBlocksFromCategory = this.getBlocksFromCategory.bind(this);
48 this.getDisabledBlock = this.getDisabledBlock.bind(this);
49 this.setDisabledBlock = this.setDisabledBlock.bind(this);
50 this.setDisabledAllBlocks = this.setDisabledAllBlocks.bind(this);
51 this.getDisabledCount = this.getDisabledCount.bind(this);
52 }
53
54 componentDidMount() {
55 const categories = this.getBlocksCategories();
56
57 this.setState({
58 activeCategory: categories[0].slug,
59 });
60 }
61
62 getDisabledBlock(data) {
63 let result = false;
64
65 if (typeof this.state.disabledBlocks[data.name] !== 'undefined') {
66 result = this.state.disabledBlocks[data.name];
67 }
68
69 return result;
70 }
71
72 setDisabledAllBlocks(disabled) {
73 const { activeCategory } = this.state;
74
75 const disabledBlocks = {};
76
77 const blocks = this.getBlocksFromCategory(activeCategory);
78 Object.keys(blocks).forEach((name) => {
79 const block = blocks[name];
80 disabledBlocks[block.name] = !disabled;
81 });
82
83 this.updateDisabledBlocks(disabledBlocks);
84 }
85
86 setDisabledBlock(data) {
87 this.updateDisabledBlocks({
88 [data.name]: !this.getDisabledBlock(data),
89 });
90 }
91
92 getDisabledCount(blocks) {
93 let result = 0;
94
95 Object.keys(blocks).forEach((name) => {
96 if (this.getDisabledBlock(blocks[name])) {
97 result += 1;
98 }
99 });
100
101 return result;
102 }
103
104 // eslint-disable-next-line class-methods-use-this
105 getBlocksCategories() {
106 const categories = wp.blocks.getCategories();
107 const result = [];
108
109 categories.forEach((cat) => {
110 const blocks = this.getBlocksFromCategory(cat.slug);
111
112 if (Object.keys(blocks).length) {
113 result.push(cat);
114 }
115 });
116
117 return result;
118 }
119
120 // eslint-disable-next-line class-methods-use-this
121 getBlocksFromCategory(category) {
122 const result = {};
123
124 if (category) {
125 const blocks = wp.blocks.getBlockTypes();
126 blocks.forEach((block) => {
127 if (
128 // blocks from needed category only
129 block.category === category &&
130 // prevent adding blocks with parent option (fe Grid Column).
131 !(block.parent && block.parent.length) &&
132 // prevent showing blocks with disabled inserter.
133 !(
134 block.supports &&
135 typeof block.supports.inserter !== 'undefined' &&
136 !block.supports.inserter
137 )
138 ) {
139 let icon = block.icon.src ? block.icon.src : block.icon;
140
141 // Prepare icon.
142 if (typeof icon === 'function') {
143 icon = wp.element.renderToString(icon());
144 } else if (typeof icon === 'object') {
145 icon = wp.element.renderToString(icon);
146 } else if (typeof icon === 'string') {
147 icon = wp.element.createElement(wp.components.Dashicon, { icon });
148 icon = wp.element.renderToString(icon);
149 }
150
151 result[block.name] = {
152 ...block,
153 ...{ icon },
154 };
155 }
156 });
157 }
158
159 return result;
160 }
161
162 updateDisabledBlocksDebounce() {
163 apiFetch({
164 path: '/ghostkit/v1/update_disabled_blocks',
165 method: 'POST',
166 data: {
167 blocks: this.state.disabledBlocks,
168 },
169 }).then((result) => {
170 if (!result.success || !result.response) {
171 // eslint-disable-next-line no-console
172 console.log(result);
173 }
174 });
175 }
176
177 updateDisabledBlocks(newBlocks) {
178 this.setState(
179 (prevState) => ({
180 disabledBlocks: merge({}, prevState.disabledBlocks, newBlocks),
181 }),
182 () => {
183 this.updateDisabledBlocksDebounce();
184 }
185 );
186 }
187
188 render() {
189 const { activeCategory, disabledBlocks } = this.state;
190
191 const blocks = this.getBlocksFromCategory(activeCategory);
192 const categories = this.getBlocksCategories();
193 const resultTabs = [];
194 const resultBlocks = [];
195
196 let count = 0;
197 const disabledCount = this.getDisabledCount(blocks);
198
199 // category content.
200 Object.keys(blocks).forEach((name) => {
201 const block = blocks[name];
202
203 count += 1;
204
205 resultBlocks.push(
206 <li
207 className={classnames(
208 'ghostkit-settings-blocks-item',
209 disabledBlocks[block.name] ? 'ghostkit-settings-blocks-item-disabled' : ''
210 )}
211 key={block.name}
212 >
213 <h3>
214 <span
215 className="ghostkit-settings-blocks-item-icon"
216 // eslint-disable-next-line react/no-danger
217 dangerouslySetInnerHTML={{ __html: block.icon }}
218 />
219 {block.title}
220 </h3>
221 {block.description ? (
222 <div className="ghostkit-settings-blocks-item-description">{block.description}</div>
223 ) : (
224 ''
225 )}
226 {block.ghostkit && block.ghostkit.previewUrl ? (
227 <div className="ghostkit-settings-blocks-item-preview-url">
228 <a href={block.ghostkit.previewUrl}>{__('Preview', 'ghostkit')}</a>
229 </div>
230 ) : (
231 ''
232 )}
233 <Tooltip
234 text={
235 this.getDisabledBlock(block)
236 ? __('Enable Block', 'ghostkit')
237 : __('Disable Block', 'ghostkit')
238 }
239 >
240 <div className="ghostkit-settings-blocks-item-check">
241 <ToggleControl
242 checked={!this.getDisabledBlock(block)}
243 onChange={() => {
244 this.setDisabledBlock(block);
245 }}
246 />
247 </div>
248 </Tooltip>
249 </li>
250 );
251 });
252
253 // categories tabs.
254 categories.forEach((cat) => {
255 const disabledCurrentCount = this.getDisabledCount(this.getBlocksFromCategory(cat.slug));
256 let categoryButton = (
257 /* eslint-disable-next-line react/button-has-type */
258 <button
259 className={classnames(
260 'ghostkit-settings-blocks-categories-button',
261 activeCategory === cat.slug ? 'ghostkit-settings-blocks-categories-button-active' : ''
262 )}
263 onClick={() => {
264 this.setState({
265 activeCategory: cat.slug,
266 });
267 }}
268 >
269 {cat.title}
270 {disabledCurrentCount ? (
271 <span className="ghostkit-settings-blocks-categories-button-indicator" />
272 ) : (
273 ''
274 )}
275 </button>
276 );
277
278 if (disabledCurrentCount) {
279 categoryButton = (
280 <Tooltip
281 text={sprintf(__('Disabled Blocks: %s', 'ghostkit'), disabledCurrentCount)}
282 key="tab-disabled-blocks"
283 >
284 {categoryButton}
285 </Tooltip>
286 );
287 }
288
289 resultTabs.push(<li key={`tab-${cat.slug}`}>{categoryButton}</li>);
290 });
291
292 if (!count) {
293 resultBlocks.push(
294 <Info key="no-blocks">{__('No blocks in selected category.', 'ghostkit')}</Info>
295 );
296 }
297
298 return (
299 <div className="ghostkit-settings-content-wrapper ghostkit-settings-blocks">
300 <div className="ghostkit-settings-blocks-left">
301 <ul className="ghostkit-settings-blocks-categories">{resultTabs}</ul>
302 </div>
303 <div className="ghostkit-settings-blocks-right">
304 {count ? (
305 <div className="ghostkit-settings-blocks-items-head">
306 <span className="ghostkit-settings-blocks-items-head-count">
307 {sprintf(__('Blocks: %s', 'ghostkit'), count)}
308 </span>
309 <Tooltip
310 text={
311 disabledCount !== count
312 ? __('Disable All Blocks', 'ghostkit')
313 : __('Enable All Blocks', 'ghostkit')
314 }
315 >
316 <div
317 className={classnames(
318 'ghostkit-settings-blocks-all-check',
319 disabledCount !== 0 && disabledCount !== count
320 ? 'ghostkit-settings-blocks-check-gray'
321 : ''
322 )}
323 >
324 <ToggleControl
325 checked={disabledCount !== count}
326 onChange={() => {
327 this.setDisabledAllBlocks(!(disabledCount !== count));
328 }}
329 />
330 </div>
331 </Tooltip>
332 </div>
333 ) : null}
334 <ul className="ghostkit-settings-blocks-items">{resultBlocks}</ul>
335 </div>
336 </div>
337 );
338 }
339 }
340