PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.4.1
Block Animations, Motion & Scroll Effects – Ghost Kit v3.4.1
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 3.4.1, at settings/pages/blocks.js

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