UNPKG

@wordpress/blocks

Version:
331 lines (329 loc) 10 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/blocks/src/store/reducer.js var reducer_exports = {}; __export(reducer_exports, { DEFAULT_CATEGORIES: () => DEFAULT_CATEGORIES, blockBindingsSources: () => blockBindingsSources, blockStyles: () => blockStyles, blockTypes: () => blockTypes, blockVariations: () => blockVariations, categories: () => categories, collections: () => collections, createBlockNameSetterReducer: () => createBlockNameSetterReducer, default: () => reducer_default, defaultBlockName: () => defaultBlockName, freeformFallbackBlockName: () => freeformFallbackBlockName, groupingBlockName: () => groupingBlockName, unprocessedBlockTypes: () => unprocessedBlockTypes, unregisteredFallbackBlockName: () => unregisteredFallbackBlockName }); module.exports = __toCommonJS(reducer_exports); var import_change_case = require("change-case"); var import_data = require("@wordpress/data"); var import_i18n = require("@wordpress/i18n"); var import_utils = require("../api/utils"); var DEFAULT_CATEGORIES = [ { slug: "text", title: (0, import_i18n.__)("Text") }, { slug: "media", title: (0, import_i18n.__)("Media") }, { slug: "design", title: (0, import_i18n.__)("Design") }, { slug: "widgets", title: (0, import_i18n.__)("Widgets") }, { slug: "theme", title: (0, import_i18n.__)("Theme") }, { slug: "embed", title: (0, import_i18n.__)("Embeds") }, { slug: "reusable", title: (0, import_i18n.__)("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]) => [(0, import_change_case.camelCase)(key), value]) ); newDefinition.name = name; return { ...state, [name]: newDefinition }; case "REMOVE_BLOCK_TYPES": return (0, import_utils.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 (0, import_utils.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 (0, import_utils.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 (0, import_utils.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 (0, import_utils.omit)(state, action.name); } return state; } var reducer_default = (0, import_data.combineReducers)({ bootstrappedBlockTypes, unprocessedBlockTypes, blockTypes, blockStyles, blockVariations, defaultBlockName, freeformFallbackBlockName, unregisteredFallbackBlockName, groupingBlockName, categories, collections, blockBindingsSources }); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { DEFAULT_CATEGORIES, blockBindingsSources, blockStyles, blockTypes, blockVariations, categories, collections, createBlockNameSetterReducer, defaultBlockName, freeformFallbackBlockName, groupingBlockName, unprocessedBlockTypes, unregisteredFallbackBlockName }); //# sourceMappingURL=reducer.js.map