@quillforms/block-editor
Version:
92 lines (86 loc) • 3.08 kB
JavaScript
/**
* WordPress Dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import BlockControlsHeader from '../block-controls-header';
import { __ } from '@wordpress/i18n';
/**
* Internal Dependencies
*/
import DefaultControls from '../block-default-controls';
import { size } from 'lodash';
import { withErrorBoundary } from '@quillforms/admin-components';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const BlockControls = withErrorBoundary(() => {
// Get the block editor dispatcher
const {
setBlockAttributes
} = useDispatch('quillForms/block-editor');
// Select necessary block editor state
const {
currentBlockId,
currentChildBlockId,
currentChildBlockIndex,
currentFormBlock,
blockTypes
} = useSelect(select => {
const {
getCurrentBlockId,
getCurrentChildBlockId,
getCurrentChildBlockIndex,
getCurrentBlock
} = select('quillForms/block-editor');
return {
currentBlockId: getCurrentBlockId(),
currentChildBlockId: getCurrentChildBlockId(),
currentChildBlockIndex: getCurrentChildBlockIndex(),
currentFormBlock: getCurrentBlock(),
blockTypes: select('quillForms/blocks').getBlockTypes()
};
});
// Return null if no block is selected
if (!currentBlockId || !currentFormBlock) return null;
// Determine if the current block is a child block
const isChildBlock = !!currentChildBlockId && currentChildBlockIndex !== undefined && size(currentFormBlock.innerBlocks) > 0;
// Get the relevant block attributes and name
const parentId = currentFormBlock.id;
const activeBlock = isChildBlock ? currentFormBlock?.innerBlocks[currentChildBlockIndex] : currentFormBlock;
const {
attributes,
name: blockName
} = activeBlock;
const blockType = blockTypes[activeBlock.name];
// Utility function to set attributes
const handleSetAttributes = val => {
if (isChildBlock) {
setBlockAttributes(currentChildBlockId, val, parentId);
} else {
setBlockAttributes(currentBlockId, val);
}
};
return /*#__PURE__*/_jsxs("div", {
className: "block-editor-block-controls",
children: [/*#__PURE__*/_jsx(BlockControlsHeader, {
isChildBlock: isChildBlock,
parentId: parentId,
id: isChildBlock ? currentChildBlockId : currentBlockId,
currentBlockName: blockName
}), /*#__PURE__*/_jsx(DefaultControls, {
blockName: blockName,
isChild: isChildBlock,
attributes: attributes,
setAttributes: handleSetAttributes,
parentBlock: currentFormBlock
}), blockType?.controls && /*#__PURE__*/_jsx(blockType.controls, {
id: isChildBlock ? currentChildBlockId : currentBlockId,
parentId: isChildBlock ? parentId : null,
attributes: attributes,
setAttributes: handleSetAttributes
})]
});
}, {
title: __('Error', 'quillforms'),
'message': __('An error occurred while rendering the block controls. Please contact Support.', 'quillforms')
});
export default BlockControls;
//# sourceMappingURL=index.js.map