@quillforms/block-editor
Version:
160 lines (153 loc) • 4.91 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _data = require("@wordpress/data");
var _hooks = require("@wordpress/hooks");
var _components = require("@wordpress/components");
var _i18n = require("@wordpress/i18n");
var _reactConfirmAlert = require("react-confirm-alert");
var _classnames = _interopRequireDefault(require("classnames"));
var _emotion = require("emotion");
var _deleteAlert = _interopRequireDefault(require("../delete-alert"));
var _lodash = require("lodash");
var _jsxRuntime = require("react/jsx-runtime");
/**
* WordPress Dependencies
*/
/**
* External Dependencies
*/
/**
* Internal Dependencies
*/
const BlockActions = ({
id,
parentId,
onAction,
disableDelete
}) => {
const {
deleteBlock,
__experimentalInsertBlock
} = (0, _data.useDispatch)('quillForms/block-editor');
const {
blocks
} = (0, _data.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
} = (0, _data.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((0, _hooks.applyFilters)('QuillForms.BlockEditor.BlockDeleteAlerts', [], id));
(0, _reactConfirmAlert.confirmAlert)({
customUI: ({
onClose
}) => {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_deleteAlert.default, {
messages: deleteAlerts,
approve: () => {
(0, _hooks.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__*/(0, _jsxRuntime.jsx)("div", {
className: "block-editor-block-actions__dropdown-wrapper",
onClick: e => e.stopPropagation(),
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.DropdownMenu, {
label: "",
icon: 'ellipsis',
className: (0, _classnames.default)('block-editor-block-actions__dropdown', (0, _emotion.css)`
.components-menu-item__item {
min-width: auto;
}
`),
children: ({
onClose
}) => /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.MenuGroup, {
className: "block-editor-block-actions__menu-group",
children: [block.name !== 'welcome-screen' && block.name !== 'partial-submission-point' && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItem, {
onClick: () => {
onClose();
const newBlock = {
...block,
id: Math.random().toString(36).substr(2, 9)
};
if ((0, _lodash.size)(newBlock?.innerBlocks) > 0) {
newBlock.innerBlocks = (0, _lodash.map)(newBlock.innerBlocks, childBlock => ({
...childBlock,
id: Math.random().toString(36).substr(2, 9)
}));
}
__experimentalInsertBlock({
...newBlock
}, index + 1, parentId);
handleBlockModification();
},
children: (0, _i18n.__)('Duplicate', 'quillforms')
}), !disableDelete && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItem, {
onClick: e => {
onClose();
handleDelete(e);
},
className: (0, _emotion.css)`
.components-menu-item__item {
color: #b71717 !important;
}
`,
children: (0, _i18n.__)('Delete', 'quillforms')
})]
})
})
});
};
var _default = exports.default = BlockActions;
//# sourceMappingURL=index.js.map