UNPKG

@wordpress/block-editor

Version:
136 lines (135 loc) 4.5 kB
// packages/block-editor/src/store/get-block-settings.js import { __EXPERIMENTAL_PATHS_WITH_OVERRIDE as PATHS_WITH_OVERRIDE, hasBlockSupport } from "@wordpress/blocks"; import { applyFilters } from "@wordpress/hooks"; import { getValueFromObjectPath } from "../utils/object"; import { getBlockName, getSettings, getBlockAttributes } from "./selectors"; var blockedPaths = [ "color", "border", "dimensions", "typography", "spacing" ]; var deprecatedFlags = { "color.palette": (settings) => settings.colors, "color.gradients": (settings) => settings.gradients, "color.custom": (settings) => settings.disableCustomColors === void 0 ? void 0 : !settings.disableCustomColors, "color.customGradient": (settings) => settings.disableCustomGradients === void 0 ? void 0 : !settings.disableCustomGradients, "typography.fontSizes": (settings) => settings.fontSizes, "typography.customFontSize": (settings) => settings.disableCustomFontSizes === void 0 ? void 0 : !settings.disableCustomFontSizes, "typography.lineHeight": (settings) => settings.enableCustomLineHeight, "spacing.units": (settings) => { if (settings.enableCustomUnits === void 0) { return; } if (settings.enableCustomUnits === true) { return ["px", "em", "rem", "vh", "vw", "%"]; } return settings.enableCustomUnits; }, "spacing.padding": (settings) => settings.enableCustomSpacing }; var prefixedFlags = { /* * These were only available in the plugin * and can be removed when the minimum WordPress version * for the plugin is 5.9. */ "border.customColor": "border.color", "border.customStyle": "border.style", "border.customWidth": "border.width", "typography.customFontStyle": "typography.fontStyle", "typography.customFontWeight": "typography.fontWeight", "typography.customLetterSpacing": "typography.letterSpacing", "typography.customTextDecorations": "typography.textDecoration", "typography.customTextTransforms": "typography.textTransform", /* * These were part of WordPress 5.8 and we need to keep them. */ "border.customRadius": "border.radius", "spacing.customMargin": "spacing.margin", "spacing.customPadding": "spacing.padding", "typography.customLineHeight": "typography.lineHeight" }; var removeCustomPrefixes = (path) => { return prefixedFlags[path] || path; }; function getBlockSettings(state, clientId, ...paths) { const blockName = getBlockName(state, clientId); const candidates = []; if (clientId) { let id = clientId; do { const name = getBlockName(state, id); if (hasBlockSupport(name, "__experimentalSettings", false)) { candidates.push(id); } } while (id = state.blocks.parents.get(id)); } return paths.map((path) => { if (blockedPaths.includes(path)) { console.warn( "Top level useSetting paths are disabled. Please use a subpath to query the information needed." ); return void 0; } let result = applyFilters( "blockEditor.useSetting.before", void 0, path, clientId, blockName ); if (void 0 !== result) { return result; } const normalizedPath = removeCustomPrefixes(path); for (const candidateClientId of candidates) { const candidateAtts = getBlockAttributes( state, candidateClientId ); result = getValueFromObjectPath( candidateAtts.settings?.blocks?.[blockName], normalizedPath ) ?? getValueFromObjectPath( candidateAtts.settings, normalizedPath ); if (result !== void 0) { break; } } const settings = getSettings(state); if (result === void 0 && blockName) { result = getValueFromObjectPath( settings.__experimentalFeatures?.blocks?.[blockName], normalizedPath ); } if (result === void 0) { result = getValueFromObjectPath( settings.__experimentalFeatures, normalizedPath ); } if (result !== void 0) { if (PATHS_WITH_OVERRIDE[normalizedPath]) { return result.custom ?? result.theme ?? result.default; } return result; } const deprecatedSettingsValue = deprecatedFlags[normalizedPath]?.(settings); if (deprecatedSettingsValue !== void 0) { return deprecatedSettingsValue; } return normalizedPath === "typography.dropCap" ? true : void 0; }); } export { getBlockSettings }; //# sourceMappingURL=get-block-settings.js.map