@wordpress/block-library
Version:
Block library for the WordPress editor.
134 lines (120 loc) • 4.46 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = QueryPlaceholder;
var _element = require("@wordpress/element");
var _data = require("@wordpress/data");
var _blocks = require("@wordpress/blocks");
var _blockEditor = require("@wordpress/block-editor");
var _components = require("@wordpress/components");
var _i18n = require("@wordpress/i18n");
/**
* WordPress dependencies
*/
function QueryPlaceholder(_ref) {
var _matchingVariation$ic, _blockType$icon;
let {
attributes,
clientId,
name,
openPatternSelectionModal,
setAttributes
} = _ref;
const [isStartingBlank, setIsStartingBlank] = (0, _element.useState)(false);
const blockProps = (0, _blockEditor.useBlockProps)();
const {
blockType,
allVariations,
hasPatterns
} = (0, _data.useSelect)(select => {
const {
getBlockVariations,
getBlockType
} = select(_blocks.store);
const {
getBlockRootClientId,
__experimentalGetPatternsByBlockTypes
} = select(_blockEditor.store);
const rootClientId = getBlockRootClientId(clientId);
return {
blockType: getBlockType(name),
allVariations: getBlockVariations(name),
hasPatterns: !!__experimentalGetPatternsByBlockTypes(name, rootClientId).length
};
}, [name, clientId]);
const matchingVariation = (0, _blockEditor.__experimentalGetMatchingVariation)(attributes, allVariations);
const icon = (matchingVariation === null || matchingVariation === void 0 ? void 0 : (_matchingVariation$ic = matchingVariation.icon) === null || _matchingVariation$ic === void 0 ? void 0 : _matchingVariation$ic.src) || (matchingVariation === null || matchingVariation === void 0 ? void 0 : matchingVariation.icon) || (blockType === null || blockType === void 0 ? void 0 : (_blockType$icon = blockType.icon) === null || _blockType$icon === void 0 ? void 0 : _blockType$icon.src);
const label = (matchingVariation === null || matchingVariation === void 0 ? void 0 : matchingVariation.title) || (blockType === null || blockType === void 0 ? void 0 : blockType.title);
if (isStartingBlank) {
return (0, _element.createElement)(QueryVariationPicker, {
clientId: clientId,
name: name,
attributes: attributes,
setAttributes: setAttributes,
icon: icon,
label: label
});
}
return (0, _element.createElement)("div", blockProps, (0, _element.createElement)(_components.Placeholder, {
icon: icon,
label: label,
instructions: (0, _i18n.__)('Choose a pattern for the query loop or start blank.')
}, !!hasPatterns && (0, _element.createElement)(_components.Button, {
variant: "primary",
onClick: openPatternSelectionModal
}, (0, _i18n.__)('Choose')), (0, _element.createElement)(_components.Button, {
variant: "secondary",
onClick: () => {
setIsStartingBlank(true);
}
}, (0, _i18n.__)('Start blank'))));
}
function QueryVariationPicker(_ref2) {
let {
clientId,
name,
attributes,
setAttributes,
icon,
label
} = _ref2;
const {
defaultVariation,
scopeVariations
} = (0, _data.useSelect)(select => {
const {
getBlockVariations,
getBlockType,
getDefaultBlockVariation
} = select(_blocks.store);
return {
blockType: getBlockType(name),
defaultVariation: getDefaultBlockVariation(name, 'block'),
scopeVariations: getBlockVariations(name, 'block')
};
}, [name]);
const {
replaceInnerBlocks
} = (0, _data.useDispatch)(_blockEditor.store);
const blockProps = (0, _blockEditor.useBlockProps)();
return (0, _element.createElement)("div", blockProps, (0, _element.createElement)(_blockEditor.__experimentalBlockVariationPicker, {
icon: icon,
label: label,
variations: scopeVariations,
onSelect: function () {
let nextVariation = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultVariation;
if (nextVariation.attributes) {
setAttributes({ ...nextVariation.attributes,
query: { ...nextVariation.attributes.query,
postType: attributes.query.postType || nextVariation.attributes.query.postType
}
});
}
if (nextVariation.innerBlocks) {
replaceInnerBlocks(clientId, (0, _blocks.createBlocksFromInnerBlocksTemplate)(nextVariation.innerBlocks), false);
}
}
}));
}
//# sourceMappingURL=query-placeholder.js.map
;