| 1 |
/** |
| 2 |
* Creates a single draggable block. |
| 3 |
*/ |
| 4 |
import { useState, useRef } from '@wordpress/element'; |
| 5 |
import { Icon, Draggable, Popover } from '@wordpress/components'; |
| 6 |
import { dispatch, useDispatch, select } from '@wordpress/data'; |
| 7 |
import { __, sprintf } from '@wordpress/i18n'; |
| 8 |
import svgIcons from '@Svg/svgs.json'; |
| 9 |
import parse from 'html-react-parser'; |
| 10 |
|
| 11 |
const DraggableBlock = ( props ) => { |
| 12 |
const { |
| 13 |
block, |
| 14 |
id, |
| 15 |
create, |
| 16 |
blockInsertionPoint, |
| 17 |
getSelectedBlockClientId, |
| 18 |
getSelectedBlockAllowedBlocks, |
| 19 |
getBlockRootClientId, |
| 20 |
defaultAllowedQuickSidebarBlocks, |
| 21 |
updateDefaultAllowedQuickSidebarBlocks, |
| 22 |
saveOptionToDatabase, |
| 23 |
} = props; |
| 24 |
const [ hovering, setHovering ] = useState( false ); |
| 25 |
const isDragging = useRef( false ); |
| 26 |
const { createNotice } = useDispatch( 'core/notices' ); |
| 27 |
const [ uniqueId, setUniqueId ] = useState( 0 ); |
| 28 |
const removedNoticeID = `quick-action-sidebar/remove-notices-flow/removed-notice/${ uniqueId }`; |
| 29 |
const remove = parse( svgIcons.remove ); |
| 30 |
const handleMouseOver = () => { |
| 31 |
setHovering( true ); |
| 32 |
}; |
| 33 |
|
| 34 |
const handleMouseOut = () => { |
| 35 |
setHovering( false ); |
| 36 |
}; |
| 37 |
|
| 38 |
const handleOnClick = ( e, selectedBlock ) => { |
| 39 |
let clientId = getBlockRootClientId || ''; |
| 40 |
let insertionPoint = blockInsertionPoint; |
| 41 |
|
| 42 |
if ( |
| 43 |
getSelectedBlockAllowedBlocks && |
| 44 |
getSelectedBlockAllowedBlocks.includes( selectedBlock ) |
| 45 |
) { |
| 46 |
insertionPoint = |
| 47 |
select( 'core/block-editor' ).getSelectedBlock().innerBlocks |
| 48 |
.length; |
| 49 |
clientId = getSelectedBlockClientId; |
| 50 |
} |
| 51 |
if ( e?.target?.classList?.contains( 'block-title-svg' ) ) { |
| 52 |
isDragging.current = false; |
| 53 |
return; |
| 54 |
} |
| 55 |
dispatch( 'core/block-editor' ).insertBlocks( |
| 56 |
create( selectedBlock.name ), |
| 57 |
insertionPoint, |
| 58 |
clientId |
| 59 |
); |
| 60 |
}; |
| 61 |
|
| 62 |
// Removes the specified element from an array. |
| 63 |
const removeElementFromArray = ( |
| 64 |
arrayFromWhichElementNeedToRemove, |
| 65 |
elementToRemove |
| 66 |
) => |
| 67 |
arrayFromWhichElementNeedToRemove.filter( |
| 68 |
( element ) => element !== elementToRemove |
| 69 |
); |
| 70 |
|
| 71 |
const handleRemoveBlock = ( elementToRemove ) => { |
| 72 |
const updatedArray = removeElementFromArray( |
| 73 |
defaultAllowedQuickSidebarBlocks, |
| 74 |
elementToRemove.name |
| 75 |
); |
| 76 |
updateDefaultAllowedQuickSidebarBlocks( updatedArray ); |
| 77 |
saveOptionToDatabase( updatedArray ); |
| 78 |
// Increment uniqueId when removing a block |
| 79 |
setUniqueId( ( prevUniqueId ) => prevUniqueId + 1 ); |
| 80 |
createNotice( |
| 81 |
'success', |
| 82 |
sprintf( |
| 83 |
/* translators: abbreviation for units */ __( |
| 84 |
'%s Removed from Quick Action Bar.', |
| 85 |
'sureforms' |
| 86 |
), |
| 87 |
elementToRemove.title |
| 88 |
), |
| 89 |
{ |
| 90 |
type: 'snackbar', |
| 91 |
id: removedNoticeID, |
| 92 |
isDismissible: true, |
| 93 |
} |
| 94 |
); |
| 95 |
// Set a timeout to remove the notice after a specific duration (e.g., 600 milliseconds) |
| 96 |
setTimeout( () => { |
| 97 |
// Remove the notice by ID |
| 98 |
dispatch( 'core/notices' ).removeNotice( removedNoticeID ); |
| 99 |
}, 1000 ); |
| 100 |
}; |
| 101 |
|
| 102 |
const hoverPopover = ( |
| 103 |
<Popover |
| 104 |
placement="right" |
| 105 |
key={ id } |
| 106 |
className="srfm-ee-quick-access__sidebar--blocks--block--icon--name" |
| 107 |
> |
| 108 |
<div className="block-title"> |
| 109 |
<div |
| 110 |
onClick={ () => { |
| 111 |
handleRemoveBlock( block ); |
| 112 |
} } |
| 113 |
className="srfm-ee-quick-access__sidebar--blocks--block--name" |
| 114 |
> |
| 115 |
{ remove } |
| 116 |
</div> |
| 117 |
{ block.title && block.title } |
| 118 |
</div> |
| 119 |
</Popover> |
| 120 |
); |
| 121 |
|
| 122 |
const separatedArray = block.name.split( '/' ); |
| 123 |
const slug = separatedArray[ 0 ]; |
| 124 |
const blockName = separatedArray[ 1 ]; |
| 125 |
|
| 126 |
return ( |
| 127 |
<div id={ `draggable-box__${ slug }--${ blockName }` }> |
| 128 |
<Draggable |
| 129 |
elementId={ `draggable-box__${ slug }--${ blockName }` } |
| 130 |
__experimentalTransferDataType="wp-blocks" |
| 131 |
transferData={ { |
| 132 |
type: 'inserter', |
| 133 |
blocks: [ create( block.name ) ], |
| 134 |
} } |
| 135 |
> |
| 136 |
{ ( { onDraggableStart, onDraggableEnd } ) => ( |
| 137 |
<div |
| 138 |
className="srfm-ee-quick-access__sidebar--blocks--block" |
| 139 |
key={ id } |
| 140 |
onClick={ ( e ) => { |
| 141 |
handleOnClick( e, block ); |
| 142 |
} } |
| 143 |
draggable |
| 144 |
onDragStart={ ( event ) => { |
| 145 |
isDragging.current = true; |
| 146 |
if ( onDraggableStart ) { |
| 147 |
onDraggableStart( event ); |
| 148 |
} |
| 149 |
} } |
| 150 |
onDragEnd={ ( event ) => { |
| 151 |
isDragging.current = false; |
| 152 |
if ( onDraggableEnd ) { |
| 153 |
onDraggableEnd( event ); |
| 154 |
} |
| 155 |
} } |
| 156 |
onMouseOver={ handleMouseOver } |
| 157 |
onMouseOut={ handleMouseOut } |
| 158 |
onFocus={ handleMouseOver } |
| 159 |
onBlur={ handleMouseOut } |
| 160 |
> |
| 161 |
<div className="srfm-ee-quick-access__sidebar--blocks--block--icon"> |
| 162 |
<Icon |
| 163 |
icon={ |
| 164 |
block.icon?.src |
| 165 |
? block.icon.src |
| 166 |
: block.icon |
| 167 |
} |
| 168 |
/> |
| 169 |
</div> |
| 170 |
{ hovering && hoverPopover } |
| 171 |
</div> |
| 172 |
) } |
| 173 |
</Draggable> |
| 174 |
</div> |
| 175 |
); |
| 176 |
}; |
| 177 |
|
| 178 |
export default DraggableBlock; |
| 179 |
|