UNPKG

@wordpress/block-editor

Version:
96 lines (93 loc) 3.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _element = require("@wordpress/element"); var _blocks = require("@wordpress/blocks"); var _data = require("@wordpress/data"); var _i18n = require("@wordpress/i18n"); var _notices = require("@wordpress/notices"); var _store = require("../../../store"); var _lockUnlock = require("../../../lock-unlock"); var _utils = require("../block-patterns-tab/utils"); var _utils2 = require("../../../store/utils"); /** * WordPress dependencies */ /** * Internal dependencies */ /** * Retrieves the block patterns inserter state. * * @param {Function} onInsert function called when inserter a list of blocks. * @param {string=} rootClientId Insertion's root client ID. * @param {string} selectedCategory The selected pattern category. * @param {boolean} isQuick For the quick inserter render only allowed patterns. * * @return {Array} Returns the patterns state. (patterns, categories, onSelect handler) */ const usePatternsState = (onInsert, rootClientId, selectedCategory, isQuick) => { const options = (0, _element.useMemo)(() => ({ [_utils2.isFiltered]: !!isQuick }), [isQuick]); const { patternCategories, patterns, userPatternCategories } = (0, _data.useSelect)(select => { const { getSettings, __experimentalGetAllowedPatterns } = (0, _lockUnlock.unlock)(select(_store.store)); const { __experimentalUserPatternCategories, __experimentalBlockPatternCategories } = getSettings(); return { patterns: __experimentalGetAllowedPatterns(rootClientId, options), userPatternCategories: __experimentalUserPatternCategories, patternCategories: __experimentalBlockPatternCategories }; }, [rootClientId, options]); const { getClosestAllowedInsertionPointForPattern } = (0, _lockUnlock.unlock)((0, _data.useSelect)(_store.store)); const allCategories = (0, _element.useMemo)(() => { const categories = [...patternCategories]; userPatternCategories?.forEach(userCategory => { if (!categories.find(existingCategory => existingCategory.name === userCategory.name)) { categories.push(userCategory); } }); return categories; }, [patternCategories, userPatternCategories]); const { createSuccessNotice } = (0, _data.useDispatch)(_notices.store); const onClickPattern = (0, _element.useCallback)((pattern, blocks) => { const destinationRootClientId = isQuick ? rootClientId : getClosestAllowedInsertionPointForPattern(pattern, rootClientId); if (destinationRootClientId === null) { return; } const patternBlocks = pattern.type === _utils.INSERTER_PATTERN_TYPES.user && pattern.syncStatus !== 'unsynced' ? [(0, _blocks.createBlock)('core/block', { ref: pattern.id })] : blocks; onInsert((patternBlocks !== null && patternBlocks !== void 0 ? patternBlocks : []).map(block => { const clonedBlock = (0, _blocks.cloneBlock)(block); if (clonedBlock.attributes.metadata?.categories?.includes(selectedCategory)) { clonedBlock.attributes.metadata.categories = [selectedCategory]; } return clonedBlock; }), pattern.name, false, destinationRootClientId); createSuccessNotice((0, _i18n.sprintf)(/* translators: %s: block pattern title. */ (0, _i18n.__)('Block pattern "%s" inserted.'), pattern.title), { type: 'snackbar', id: 'inserter-notice' }); }, [createSuccessNotice, onInsert, selectedCategory, rootClientId, getClosestAllowedInsertionPointForPattern, isQuick]); return [patterns, allCategories, onClickPattern]; }; var _default = exports.default = usePatternsState; //# sourceMappingURL=use-patterns-state.js.map