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

339 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 />
233 </div>
234 </Tooltip>
235 </li>
236 );
237 });
238
239 // categories tabs.
240 categories.forEach((cat) => {
241 const disabledCurrentCount = this.getDisabledCount(
242 this.getBlocksFromCategory(cat.slug)
243 );
244 let categoryButton = (
245 <button
246 className={classnames(
247 'ghostkit-settings-blocks-categories-button',
248 activeCategory === cat.slug
249 ? 'ghostkit-settings-blocks-categories-button-active'
250 : ''
251 )}
252 onClick={() => {
253 this.setState({
254 activeCategory: cat.slug,
255 });
256 }}
257 >
258 {cat.title}
259 {disabledCurrentCount ? (
260 <span className="ghostkit-settings-blocks-categories-button-indicator" />
261 ) : null}
262 </button>
263 );
264
265 if (disabledCurrentCount) {
266 categoryButton = (
267 <Tooltip
268 text={sprintf(
269 __('Disabled Blocks: %s', 'ghostkit'),
270 disabledCurrentCount
271 )}
272 key="tab-disabled-blocks"
273 >
274 {categoryButton}
275 </Tooltip>
276 );
277 }
278
279 resultTabs.push(<li key={`tab-${cat.slug}`}>{categoryButton}</li>);
280 });
281
282 if (!count) {
283 resultBlocks.push(
284 <Info key="no-blocks">
285 {__('No blocks in selected category.', 'ghostkit')}
286 </Info>
287 );
288 }
289
290 return (
291 <div className="ghostkit-settings-content-wrapper ghostkit-settings-blocks">
292 <div className="ghostkit-settings-blocks-left">
293 <ul className="ghostkit-settings-blocks-categories">
294 {resultTabs}
295 </ul>
296 </div>
297 <div className="ghostkit-settings-blocks-right">
298 {count ? (
299 <div className="ghostkit-settings-blocks-items-head">
300 <span className="ghostkit-settings-blocks-items-head-count">
301 {sprintf(__('Blocks: %s', 'ghostkit'), count)}
302 </span>
303 <Tooltip
304 text={
305 disabledCount !== count
306 ? __('Disable All Blocks', 'ghostkit')
307 : __('Enable All Blocks', 'ghostkit')
308 }
309 >
310 <div
311 className={classnames(
312 'ghostkit-settings-blocks-all-check',
313 disabledCount !== 0 &&
314 disabledCount !== count
315 ? 'ghostkit-settings-blocks-check-gray'
316 : ''
317 )}
318 >
319 <ToggleControl
320 checked={disabledCount !== count}
321 onChange={() => {
322 this.setDisabledAllBlocks(
323 !(disabledCount !== count)
324 );
325 }}
326 />
327 </div>
328 </Tooltip>
329 </div>
330 ) : null}
331 <ul className="ghostkit-settings-blocks-items">
332 {resultBlocks}
333 </ul>
334 </div>
335 </div>
336 );
337 }
338 }
339