UNPKG

@wordpress/blocks

Version:
295 lines (294 loc) 8.15 kB
// packages/blocks/src/store/reducer.js import { camelCase } from "change-case"; import { combineReducers } from "@wordpress/data"; import { __ } from "@wordpress/i18n"; import { omit } from "../api/utils"; var DEFAULT_CATEGORIES = [ { slug: "text", title: __("Text") }, { slug: "media", title: __("Media") }, { slug: "design", title: __("Design") }, { slug: "widgets", title: __("Widgets") }, { slug: "theme", title: __("Theme") }, { slug: "embed", title: __("Embeds") }, { slug: "reusable", title: __("Reusable blocks") } ]; function keyBlockTypesByName(types) { return types.reduce( (newBlockTypes, block) => ({ ...newBlockTypes, [block.name]: block }), {} ); } function getUniqueItemsByName(items) { return items.reduce((acc, currentItem) => { if (!acc.some((item) => item.name === currentItem.name)) { acc.push(currentItem); } return acc; }, []); } function bootstrappedBlockTypes(state = {}, action) { switch (action.type) { case "ADD_BOOTSTRAPPED_BLOCK_TYPE": const { name, blockType } = action; const serverDefinition = state[name]; if (serverDefinition) { return state; } const newDefinition = Object.fromEntries( Object.entries(blockType).filter( ([, value]) => value !== null && value !== void 0 ).map(([key, value]) => [camelCase(key), value]) ); newDefinition.name = name; return { ...state, [name]: newDefinition }; case "REMOVE_BLOCK_TYPES": return omit(state, action.names); } return state; } function unprocessedBlockTypes(state = {}, action) { switch (action.type) { case "ADD_UNPROCESSED_BLOCK_TYPE": return { ...state, [action.name]: action.blockType }; case "REMOVE_BLOCK_TYPES": return omit(state, action.names); } return state; } function blockTypes(state = {}, action) { switch (action.type) { case "ADD_BLOCK_TYPES": return { ...state, ...keyBlockTypesByName(action.blockTypes) }; case "REMOVE_BLOCK_TYPES": return omit(state, action.names); } return state; } function blockStyles(state = {}, action) { switch (action.type) { case "ADD_BLOCK_TYPES": return { ...state, ...Object.fromEntries( Object.entries( keyBlockTypesByName(action.blockTypes) ).map(([name, blockType]) => [ name, getUniqueItemsByName([ ...(blockType.styles ?? []).map((style) => ({ ...style, source: "block" })), ...(state[blockType.name] ?? []).filter( ({ source }) => "block" !== source ) ]) ]) ) }; case "ADD_BLOCK_STYLES": const updatedStyles = {}; action.blockNames.forEach((blockName) => { updatedStyles[blockName] = getUniqueItemsByName([ ...state[blockName] ?? [], ...action.styles ]); }); return { ...state, ...updatedStyles }; case "REMOVE_BLOCK_STYLES": return { ...state, [action.blockName]: (state[action.blockName] ?? []).filter( (style) => action.styleNames.indexOf(style.name) === -1 ) }; } return state; } function blockVariations(state = {}, action) { switch (action.type) { case "ADD_BLOCK_TYPES": return { ...state, ...Object.fromEntries( Object.entries( keyBlockTypesByName(action.blockTypes) ).map(([name, blockType]) => { return [ name, getUniqueItemsByName([ ...(blockType.variations ?? []).map( (variation) => ({ ...variation, source: "block" }) ), ...(state[blockType.name] ?? []).filter( ({ source }) => "block" !== source ) ]) ]; }) ) }; case "ADD_BLOCK_VARIATIONS": return { ...state, [action.blockName]: getUniqueItemsByName([ ...state[action.blockName] ?? [], ...action.variations ]) }; case "REMOVE_BLOCK_VARIATIONS": return { ...state, [action.blockName]: (state[action.blockName] ?? []).filter( (variation) => action.variationNames.indexOf(variation.name) === -1 ) }; } return state; } 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; }; } var defaultBlockName = createBlockNameSetterReducer( "SET_DEFAULT_BLOCK_NAME" ); var freeformFallbackBlockName = createBlockNameSetterReducer( "SET_FREEFORM_FALLBACK_BLOCK_NAME" ); var unregisteredFallbackBlockName = createBlockNameSetterReducer( "SET_UNREGISTERED_FALLBACK_BLOCK_NAME" ); var groupingBlockName = createBlockNameSetterReducer( "SET_GROUPING_BLOCK_NAME" ); function categories(state = DEFAULT_CATEGORIES, action) { switch (action.type) { case "SET_CATEGORIES": const uniqueCategories = /* @__PURE__ */ new Map(); (action.categories || []).forEach((category) => { uniqueCategories.set(category.slug, category); }); return [...uniqueCategories.values()]; case "UPDATE_CATEGORY": { if (!action.category || !Object.keys(action.category).length) { return state; } const categoryToChange = state.find( ({ slug }) => slug === action.slug ); if (categoryToChange) { return state.map((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 omit(state, action.namespace); } return state; } function getMergedUsesContext(existingUsesContext = [], newUsesContext = []) { const mergedArrays = Array.from( new Set(existingUsesContext.concat(newUsesContext)) ); return mergedArrays.length > 0 ? mergedArrays : void 0; } function blockBindingsSources(state = {}, action) { switch (action.type) { case "ADD_BLOCK_BINDINGS_SOURCE": return { ...state, [action.name]: { label: action.label || state[action.name]?.label, usesContext: getMergedUsesContext( state[action.name]?.usesContext, action.usesContext ), getValues: action.getValues, setValues: action.setValues, // Only set `canUserEditValue` if `setValues` is also defined. canUserEditValue: action.setValues && action.canUserEditValue, getFieldsList: action.getFieldsList } }; case "REMOVE_BLOCK_BINDINGS_SOURCE": return omit(state, action.name); } return state; } var reducer_default = combineReducers({ bootstrappedBlockTypes, unprocessedBlockTypes, blockTypes, blockStyles, blockVariations, defaultBlockName, freeformFallbackBlockName, unregisteredFallbackBlockName, groupingBlockName, categories, collections, blockBindingsSources }); export { DEFAULT_CATEGORIES, blockBindingsSources, blockStyles, blockTypes, blockVariations, categories, collections, createBlockNameSetterReducer, reducer_default as default, defaultBlockName, freeformFallbackBlockName, groupingBlockName, unprocessedBlockTypes, unregisteredFallbackBlockName }; //# sourceMappingURL=reducer.js.map