| 1 |
import { useEffect, useMemo } from "@wordpress/element"; |
| 2 |
import { getBlockShortName } from "./helpFn"; |
| 3 |
import { select } from "@wordpress/data"; |
| 4 |
|
| 5 |
const getUniqId = (blocks) => |
| 6 |
blocks.reduce( |
| 7 |
(result, block) => { |
| 8 |
if (block?.attributes?.uniqueId && block.name.includes("sp-smart-post-show")) { |
| 9 |
result.blockIds.push(block.attributes.uniqueId); |
| 10 |
result.clientIds.push(block.clientId); |
| 11 |
} |
| 12 |
|
| 13 |
if (block.innerBlocks) { |
| 14 |
const { blockIds, clientIds } = getUniqId(block.innerBlocks); |
| 15 |
result.blockIds = [...result.blockIds, ...blockIds]; |
| 16 |
result.clientIds = [...result.clientIds, ...clientIds]; |
| 17 |
} |
| 18 |
return result; |
| 19 |
}, |
| 20 |
{ blockIds: [], clientIds: [] } |
| 21 |
); |
| 22 |
|
| 23 |
const checkDuplicate = (blockIds, block_id, currentIndex) => { |
| 24 |
const getFiltered = blockIds.filter((el) => el === block_id); |
| 25 |
return getFiltered.length > 1 && currentIndex === blockIds.lastIndexOf(block_id); |
| 26 |
}; |
| 27 |
|
| 28 |
const addInitialAttr = (ChildComponent) => { |
| 29 |
const WrappedComponent = (props) => { |
| 30 |
const { |
| 31 |
name, |
| 32 |
setAttributes, |
| 33 |
clientId, |
| 34 |
attributes: { blockName, uniqueId }, |
| 35 |
} = props; |
| 36 |
|
| 37 |
const blockShortName = useMemo(() => getBlockShortName(name), [name]); |
| 38 |
const updateBlockShortName = blockShortName?.startsWith("smart-") |
| 39 |
? blockShortName.replace(/^smart-/, "") |
| 40 |
: blockShortName; |
| 41 |
|
| 42 |
useEffect(() => { |
| 43 |
const _uniqueId = `sp-smart-${updateBlockShortName}-${clientId?.split("-").pop()}`; |
| 44 |
const attributeObject = { uniqueId: _uniqueId }; |
| 45 |
|
| 46 |
const getStore = select("core/block-editor"); |
| 47 |
const getAllBlocks = getStore?.getBlocks ? getStore.getBlocks() : null; |
| 48 |
const { blockIds, clientIds } = getAllBlocks ? getUniqId(getAllBlocks) : { blockIds: [], clientIds: [] }; |
| 49 |
|
| 50 |
const getGlobalBreakpoint = select("smartpost/global-settings").getCategory("breakpoint"); |
| 51 |
if (typeof getGlobalBreakpoint === "object") { |
| 52 |
attributeObject.globalBreakPointData = getGlobalBreakpoint; |
| 53 |
} |
| 54 |
|
| 55 |
if (!blockName) { |
| 56 |
const updateBlockName = |
| 57 |
blockShortName === "thumbnail-slider" ? "post-thumbnail-slider" : blockShortName; |
| 58 |
|
| 59 |
attributeObject.blockName = updateBlockName; |
| 60 |
} |
| 61 |
|
| 62 |
const filterBlocks = [ |
| 63 |
"post-carousel", |
| 64 |
"post-carousel-two", |
| 65 |
"post-slider", |
| 66 |
"post-slider-two", |
| 67 |
"thumbnail-slider", |
| 68 |
"thumbnail-slider-two", |
| 69 |
"news-ticker", |
| 70 |
"post-grid-one", |
| 71 |
"post-grid-two", |
| 72 |
"post-grid-three", |
| 73 |
"post-grid-four", |
| 74 |
"post-grid-five", |
| 75 |
"post-grid-six", |
| 76 |
"post-list-one", |
| 77 |
"post-list-two", |
| 78 |
"post-list-three", |
| 79 |
"post-timeline-one", |
| 80 |
"post-timeline-two", |
| 81 |
"post-timeline-three", |
| 82 |
"taxonomy", |
| 83 |
"pagination", |
| 84 |
"taxonomy-filter", |
| 85 |
"sort-filter", |
| 86 |
"live-filter", |
| 87 |
"author-filter", |
| 88 |
]; |
| 89 |
// add spBlockId for filter blocks only |
| 90 |
if (filterBlocks.includes(blockShortName)) { |
| 91 |
attributeObject.spBlockId = clientId?.split("-").pop(); |
| 92 |
} |
| 93 |
|
| 94 |
if ("column" === blockShortName) { |
| 95 |
attributeObject.blockClientId = clientId; |
| 96 |
} |
| 97 |
|
| 98 |
if ( |
| 99 |
"not_set" === uniqueId || |
| 100 |
"0" === uniqueId || |
| 101 |
!uniqueId || |
| 102 |
checkDuplicate(blockIds, uniqueId, clientIds.indexOf(clientId)) || |
| 103 |
!blockName |
| 104 |
) { |
| 105 |
setAttributes(attributeObject); |
| 106 |
} |
| 107 |
// eslint-disable-next-line |
| 108 |
}, [clientId, blockShortName]); |
| 109 |
|
| 110 |
const _props = { |
| 111 |
...props, |
| 112 |
attributes: { |
| 113 |
...props.attributes, |
| 114 |
clientId, |
| 115 |
}, |
| 116 |
}; |
| 117 |
return <ChildComponent {..._props} />; |
| 118 |
}; |
| 119 |
|
| 120 |
return WrappedComponent; |
| 121 |
}; |
| 122 |
export default addInitialAttr; |
| 123 |
|