PluginProbe
GutSlider – All in One Slider and Carousel Blocks for Gutenberg / 2.11.0
GutSlider – All in One Slider and Carousel Blocks for Gutenberg v2.11.0
3.1.0 3.0.0 2.13.2 2.13.1 2.13.0 trunk 1.0.0 2.1.0 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.1 2.5.3 2.5.4 2.5.5 2.6.1 All 56 releases
slider-blocks / src / helper / functions.js

functions.js in GutSlider – All in One Slider and Carousel Blocks for Gutenberg 2.11.0, at src/helper/functions.js

42 lines 1.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { select } from '@wordpress/data';
2
3 export const handleUniqueId = ({ uniqueId, setAttributes, clientId }) => {
4 const uniqueID = 'gutslider' + '-' + Math.random().toString(36).substr(2, 8);
5
6 /**
7 * Define and Generate Unique Block ID
8 */
9 if (!uniqueId) {
10 setAttributes({ uniqueId: uniqueID });
11 return;
12 }
13
14 /**
15 * Assign New Unique ID when duplicate uniqueId found
16 * Mostly happens when User Duplicate a Block
17 */
18
19 const allBlocks = select('core/block-editor').getBlocks();
20
21 let duplicateFound = false;
22 const fixDuplicateUniqueId = blocks => {
23 if (duplicateFound) return;
24 for (const item of blocks) {
25 const { innerBlocks } = item;
26 if (item.attributes.uniqueId === uniqueId) {
27 if (item.clientId !== clientId) {
28 setAttributes({ uniqueId: uniqueID });
29 duplicateFound = true;
30 return;
31 } else if (innerBlocks.length > 0) {
32 fixDuplicateUniqueId(innerBlocks);
33 }
34 } else if (innerBlocks.length > 0) {
35 fixDuplicateUniqueId(innerBlocks);
36 }
37 }
38 };
39
40 fixDuplicateUniqueId(allBlocks);
41 };
42