UNPKG

@wordpress/block-editor

Version:
95 lines (76 loc) 3.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useEditorFeature; var _lodash = require("lodash"); var _data = require("@wordpress/data"); var _blockEdit = require("../block-edit"); var _store = require("../../store"); /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ const deprecatedFlags = { 'color.palette': settings => settings.colors === undefined ? undefined : settings.colors, 'color.gradients': settings => settings.gradients === undefined ? undefined : settings.gradients, 'color.custom': settings => settings.disableCustomColors === undefined ? undefined : !settings.disableCustomColors, 'color.customGradient': settings => settings.disableCustomGradients === undefined ? undefined : !settings.disableCustomGradients, 'typography.fontSizes': settings => settings.fontSizes === undefined ? undefined : settings.fontSizes, 'typography.customFontSize': settings => settings.disableCustomFontSizes === undefined ? undefined : !settings.disableCustomFontSizes, 'typography.customLineHeight': settings => settings.enableCustomLineHeight, 'spacing.units': settings => { if (settings.enableCustomUnits === undefined) { return; } if (settings.enableCustomUnits === true) { return ['px', 'em', 'rem', 'vh', 'vw']; } return settings.enableCustomUnits; }, 'spacing.customPadding': settings => settings.enableCustomSpacing }; /** * Hook that retrieves the setting for the given editor feature. * It works with nested objects using by finding the value at path. * * @param {string} featurePath The path to the feature. * * @return {any} Returns the value defined for the setting. * * @example * ```js * const isEnabled = useEditorFeature( 'typography.dropCap' ); * ``` */ function useEditorFeature(featurePath) { const { name: blockName } = (0, _blockEdit.useBlockEditContext)(); const setting = (0, _data.useSelect)(select => { var _get; const settings = select(_store.store).getSettings(); // 1 - Use __experimental features, if available. // We cascade to the all value if the block one is not available. const defaultsPath = `__experimentalFeatures.${featurePath}`; const blockPath = `__experimentalFeatures.blocks.${blockName}.${featurePath}`; const experimentalFeaturesResult = (_get = (0, _lodash.get)(settings, blockPath)) !== null && _get !== void 0 ? _get : (0, _lodash.get)(settings, defaultsPath); if (experimentalFeaturesResult !== undefined) { return experimentalFeaturesResult; } // 2 - Use deprecated settings, otherwise. const deprecatedSettingsValue = deprecatedFlags[featurePath] ? deprecatedFlags[featurePath](settings) : undefined; if (deprecatedSettingsValue !== undefined) { return deprecatedSettingsValue; } // 3 - Fall back for typography.dropCap: // This is only necessary to support typography.dropCap. // when __experimentalFeatures are not present (core without plugin). // To remove when __experimentalFeatures are ported to core. return featurePath === 'typography.dropCap' ? true : undefined; }, [blockName, featurePath]); return setting; } //# sourceMappingURL=index.js.map