@quillforms/block-editor
Version:
100 lines (92 loc) • 3.48 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 _blockControlsHeader = _interopRequireDefault(require("../block-controls-header"));
var _i18n = require("@wordpress/i18n");
var _blockDefaultControls = _interopRequireDefault(require("../block-default-controls"));
var _lodash = require("lodash");
var _adminComponents = require("@quillforms/admin-components");
var _jsxRuntime = require("react/jsx-runtime");
/**
* WordPress Dependencies
*/
/**
* Internal Dependencies
*/
const BlockControls = (0, _adminComponents.withErrorBoundary)(() => {
// Get the block editor dispatcher
const {
setBlockAttributes
} = (0, _data.useDispatch)('quillForms/block-editor');
// Select necessary block editor state
const {
currentBlockId,
currentChildBlockId,
currentChildBlockIndex,
currentFormBlock,
blockTypes
} = (0, _data.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 && (0, _lodash.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__*/(0, _jsxRuntime.jsxs)("div", {
className: "block-editor-block-controls",
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_blockControlsHeader.default, {
isChildBlock: isChildBlock,
parentId: parentId,
id: isChildBlock ? currentChildBlockId : currentBlockId,
currentBlockName: blockName
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockDefaultControls.default, {
blockName: blockName,
isChild: isChildBlock,
attributes: attributes,
setAttributes: handleSetAttributes,
parentBlock: currentFormBlock
}), blockType?.controls && /*#__PURE__*/(0, _jsxRuntime.jsx)(blockType.controls, {
id: isChildBlock ? currentChildBlockId : currentBlockId,
parentId: isChildBlock ? parentId : null,
attributes: attributes,
setAttributes: handleSetAttributes
})]
});
}, {
title: (0, _i18n.__)('Error', 'quillforms'),
'message': (0, _i18n.__)('An error occurred while rendering the block controls. Please contact Support.', 'quillforms')
});
var _default = exports.default = BlockControls;
//# sourceMappingURL=index.js.map