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

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