@quillforms/block-editor
Version:
152 lines (147 loc) • 4.39 kB
JavaScript
/**
* WordPress Dependencies
*/
import { useDispatch, useSelect } from '@wordpress/data';
import { doAction, applyFilters } from '@wordpress/hooks';
import { DropdownMenu, MenuGroup, MenuItem } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
/**
* External Dependencies
*/
import { confirmAlert } from 'react-confirm-alert';
import classnames from 'classnames';
import { css } from 'emotion';
/**
* Internal Dependencies
*/
import DeleteAlert from '../delete-alert';
import { map, size } from 'lodash';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const BlockActions = ({
id,
parentId,
onAction,
disableDelete
}) => {
const {
deleteBlock,
__experimentalInsertBlock
} = useDispatch('quillForms/block-editor');
const {
blocks
} = useSelect(select => {
return {
blocks: select('quillForms/block-editor').getBlocks(true)
};
});
//console.log("#############")
//console.log(blocks);
//console.log(parentId);
//console.log(id)
let parentIndex;
if (!parentId) {
parentIndex = undefined;
} else {
parentIndex = blocks.findIndex(block => block.id === parentId);
}
//console.log(parentIndex);
//console.log("#############")
let index;
if (!parentId || parentIndex === -1) {
index = blocks.findIndex(block => block.id === id);
} else {
index = blocks[parentIndex].innerBlocks.findIndex(block => block.id === id);
}
const {
block
} = useSelect(select => {
return {
block: select('quillForms/block-editor').getBlockById(id, parentIndex)
};
});
if (!block) {
return null;
}
// Delete Block
const handleDelete = e => {
e.stopPropagation();
let deleteAlerts = [];
deleteAlerts = deleteAlerts.concat(applyFilters('QuillForms.BlockEditor.BlockDeleteAlerts', [], id));
confirmAlert({
customUI: ({
onClose
}) => {
return /*#__PURE__*/_jsx(DeleteAlert, {
messages: deleteAlerts,
approve: () => {
doAction('QuillForms.BlockEditor.BlockDelete', id);
deleteBlock(id, parentId);
onClose();
handleBlockModification();
},
reject: () => {
onClose();
},
closeModal: onClose
});
}
});
};
const handleBlockModification = () => {
// Ensure synchronous execution of block modification and tree recalculation
setTimeout(() => {
onAction();
}, 0);
};
return /*#__PURE__*/_jsx("div", {
className: "block-editor-block-actions__dropdown-wrapper",
onClick: e => e.stopPropagation(),
children: /*#__PURE__*/_jsx(DropdownMenu, {
label: "",
icon: 'ellipsis',
className: classnames('block-editor-block-actions__dropdown', css`
.components-menu-item__item {
min-width: auto;
}
`),
children: ({
onClose
}) => /*#__PURE__*/_jsxs(MenuGroup, {
className: "block-editor-block-actions__menu-group",
children: [block.name !== 'welcome-screen' && block.name !== 'partial-submission-point' && /*#__PURE__*/_jsx(MenuItem, {
onClick: () => {
onClose();
const newBlock = {
...block,
id: Math.random().toString(36).substr(2, 9)
};
if (size(newBlock?.innerBlocks) > 0) {
newBlock.innerBlocks = map(newBlock.innerBlocks, childBlock => ({
...childBlock,
id: Math.random().toString(36).substr(2, 9)
}));
}
__experimentalInsertBlock({
...newBlock
}, index + 1, parentId);
handleBlockModification();
},
children: __('Duplicate', 'quillforms')
}), !disableDelete && /*#__PURE__*/_jsx(MenuItem, {
onClick: e => {
onClose();
handleDelete(e);
},
className: css`
.components-menu-item__item {
color: #b71717 ;
}
`,
children: __('Delete', 'quillforms')
})]
})
})
});
};
export default BlockActions;
//# sourceMappingURL=index.js.map