UNPKG

@wordpress/blocks

Version:
255 lines (215 loc) 6.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.blockTypes = blockTypes; exports.blockStyles = blockStyles; exports.blockVariations = blockVariations; exports.createBlockNameSetterReducer = createBlockNameSetterReducer; exports.categories = categories; exports.collections = collections; exports.default = exports.groupingBlockName = exports.unregisteredFallbackBlockName = exports.freeformFallbackBlockName = exports.defaultBlockName = exports.DEFAULT_CATEGORIES = void 0; var _lodash = require("lodash"); var _data = require("@wordpress/data"); var _i18n = require("@wordpress/i18n"); /** * External dependencies */ /** * WordPress dependencies */ /** * @typedef {Object} WPBlockCategory * * @property {string} slug Unique category slug. * @property {string} title Category label, for display in user interface. */ /** * Default set of categories. * * @type {WPBlockCategory[]} */ const DEFAULT_CATEGORIES = [{ slug: 'text', title: (0, _i18n.__)('Text') }, { slug: 'media', title: (0, _i18n.__)('Media') }, { slug: 'design', title: (0, _i18n.__)('Design') }, { slug: 'widgets', title: (0, _i18n.__)('Widgets') }, { slug: 'theme', title: (0, _i18n.__)('Theme') }, { slug: 'embed', title: (0, _i18n.__)('Embeds') }, { slug: 'reusable', title: (0, _i18n.__)('Reusable blocks') }]; /** * Reducer managing the block types * * @param {Object} state Current state. * @param {Object} action Dispatched action. * * @return {Object} Updated state. */ exports.DEFAULT_CATEGORIES = DEFAULT_CATEGORIES; function blockTypes(state = {}, action) { switch (action.type) { case 'ADD_BLOCK_TYPES': return { ...state, ...(0, _lodash.keyBy)((0, _lodash.map)(action.blockTypes, blockType => (0, _lodash.omit)(blockType, 'styles ')), 'name') }; case 'REMOVE_BLOCK_TYPES': return (0, _lodash.omit)(state, action.names); } return state; } /** * Reducer managing the block style variations. * * @param {Object} state Current state. * @param {Object} action Dispatched action. * * @return {Object} Updated state. */ function blockStyles(state = {}, action) { switch (action.type) { case 'ADD_BLOCK_TYPES': return { ...state, ...(0, _lodash.mapValues)((0, _lodash.keyBy)(action.blockTypes, 'name'), blockType => { return (0, _lodash.uniqBy)([...(0, _lodash.get)(blockType, ['styles'], []), ...(0, _lodash.get)(state, [blockType.name], [])], style => style.name); }) }; case 'ADD_BLOCK_STYLES': return { ...state, [action.blockName]: (0, _lodash.uniqBy)([...(0, _lodash.get)(state, [action.blockName], []), ...action.styles], style => style.name) }; case 'REMOVE_BLOCK_STYLES': return { ...state, [action.blockName]: (0, _lodash.filter)((0, _lodash.get)(state, [action.blockName], []), style => action.styleNames.indexOf(style.name) === -1) }; } return state; } /** * Reducer managing the block variations. * * @param {Object} state Current state. * @param {Object} action Dispatched action. * * @return {Object} Updated state. */ function blockVariations(state = {}, action) { switch (action.type) { case 'ADD_BLOCK_TYPES': return { ...state, ...(0, _lodash.mapValues)((0, _lodash.keyBy)(action.blockTypes, 'name'), blockType => { return (0, _lodash.uniqBy)([...(0, _lodash.get)(blockType, ['variations'], []), ...(0, _lodash.get)(state, [blockType.name], [])], variation => variation.name); }) }; case 'ADD_BLOCK_VARIATIONS': return { ...state, [action.blockName]: (0, _lodash.uniqBy)([...(0, _lodash.get)(state, [action.blockName], []), ...action.variations], variation => variation.name) }; case 'REMOVE_BLOCK_VARIATIONS': return { ...state, [action.blockName]: (0, _lodash.filter)((0, _lodash.get)(state, [action.blockName], []), variation => action.variationNames.indexOf(variation.name) === -1) }; } return state; } /** * Higher-order Reducer creating a reducer keeping track of given block name. * * @param {string} setActionType Action type. * * @return {Function} Reducer. */ function createBlockNameSetterReducer(setActionType) { return (state = null, action) => { switch (action.type) { case 'REMOVE_BLOCK_TYPES': if (action.names.indexOf(state) !== -1) { return null; } return state; case setActionType: return action.name || null; } return state; }; } const defaultBlockName = createBlockNameSetterReducer('SET_DEFAULT_BLOCK_NAME'); exports.defaultBlockName = defaultBlockName; const freeformFallbackBlockName = createBlockNameSetterReducer('SET_FREEFORM_FALLBACK_BLOCK_NAME'); exports.freeformFallbackBlockName = freeformFallbackBlockName; const unregisteredFallbackBlockName = createBlockNameSetterReducer('SET_UNREGISTERED_FALLBACK_BLOCK_NAME'); exports.unregisteredFallbackBlockName = unregisteredFallbackBlockName; const groupingBlockName = createBlockNameSetterReducer('SET_GROUPING_BLOCK_NAME'); /** * Reducer managing the categories * * @param {WPBlockCategory[]} state Current state. * @param {Object} action Dispatched action. * * @return {WPBlockCategory[]} Updated state. */ exports.groupingBlockName = groupingBlockName; function categories(state = DEFAULT_CATEGORIES, action) { switch (action.type) { case 'SET_CATEGORIES': return action.categories || []; case 'UPDATE_CATEGORY': { if (!action.category || (0, _lodash.isEmpty)(action.category)) { return state; } const categoryToChange = (0, _lodash.find)(state, ['slug', action.slug]); if (categoryToChange) { return (0, _lodash.map)(state, category => { if (category.slug === action.slug) { return { ...category, ...action.category }; } return category; }); } } } return state; } function collections(state = {}, action) { switch (action.type) { case 'ADD_BLOCK_COLLECTION': return { ...state, [action.namespace]: { title: action.title, icon: action.icon } }; case 'REMOVE_BLOCK_COLLECTION': return (0, _lodash.omit)(state, action.namespace); } return state; } var _default = (0, _data.combineReducers)({ blockTypes, blockStyles, blockVariations, defaultBlockName, freeformFallbackBlockName, unregisteredFallbackBlockName, groupingBlockName, categories, collections }); exports.default = _default; //# sourceMappingURL=reducer.js.map