UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 6.16 kB
{ "version": 3, "sources": ["../../../src/components/inspector-controls-tabs/use-inspector-controls-tabs.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { __experimentalUseSlotFills as useSlotFills } from '@wordpress/components';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport InspectorControlsGroups from '../inspector-controls/groups';\nimport { InspectorAdvancedControls } from '../inspector-controls';\nimport { TAB_LIST_VIEW, TAB_SETTINGS, TAB_STYLES, TAB_CONTENT } from './utils';\nimport { store as blockEditorStore } from '../../store';\n\nconst EMPTY_ARRAY = [];\n\nfunction getShowTabs( blockName, tabSettings = {} ) {\n\t// Block specific setting takes precedence over generic default.\n\tif ( tabSettings[ blockName ] !== undefined ) {\n\t\treturn tabSettings[ blockName ];\n\t}\n\n\t// Use generic default if set over the Gutenberg experiment option.\n\tif ( tabSettings.default !== undefined ) {\n\t\treturn tabSettings.default;\n\t}\n\n\treturn true;\n}\n\nexport default function useInspectorControlsTabs(\n\tblockName,\n\tcontentClientIds,\n\tisSectionBlock,\n\thasBlockStyles\n) {\n\tconst tabs = [];\n\tconst {\n\t\tbindings: bindingsGroup,\n\t\tborder: borderGroup,\n\t\tcolor: colorGroup,\n\t\tcontent: contentGroup,\n\t\tdefault: defaultGroup,\n\t\tdimensions: dimensionsGroup,\n\t\tlist: listGroup,\n\t\tposition: positionGroup,\n\t\tstyles: stylesGroup,\n\t\ttypography: typographyGroup,\n\t\teffects: effectsGroup,\n\t} = InspectorControlsGroups;\n\n\t// List View Tab: If there are any fills for the list group add that tab.\n\tconst listFills = useSlotFills( listGroup.name );\n\tconst hasListFills = !! listFills && listFills.length;\n\n\t// Content Tab: If there are any fills for the content group add that tab.\n\tconst contentFills = useSlotFills( contentGroup.name );\n\tconst hasContentFills = !! contentFills && contentFills.length;\n\n\t// Styles Tab: Add this tab if there are any fills for block supports\n\t// e.g. border, color, spacing, typography, etc.\n\tconst styleFills = [\n\t\t...( useSlotFills( borderGroup.name ) || [] ),\n\t\t...( useSlotFills( colorGroup.name ) || [] ),\n\t\t...( useSlotFills( dimensionsGroup.name ) || [] ),\n\t\t...( useSlotFills( stylesGroup.name ) || [] ),\n\t\t...( useSlotFills( typographyGroup.name ) || [] ),\n\t\t...( useSlotFills( effectsGroup.name ) || [] ),\n\t];\n\tconst hasStyleFills = styleFills.length;\n\n\t// Settings Tab: If we don't have multiple tabs to display\n\t// (i.e. both list view and styles), check only the default and position\n\t// InspectorControls slots. If we have multiple tabs, we'll need to check\n\t// the advanced controls slot as well to ensure they are rendered.\n\tconst advancedFills = [\n\t\t...( useSlotFills( InspectorAdvancedControls.slotName ) || [] ),\n\t\t...( useSlotFills( bindingsGroup.name ) || [] ),\n\t];\n\n\tconst settingsFills = [\n\t\t...( useSlotFills( defaultGroup.name ) || [] ),\n\t\t...( useSlotFills( positionGroup.name ) || [] ),\n\t\t...( hasListFills && hasStyleFills > 1 ? advancedFills : [] ),\n\t];\n\n\t// When the block fields experiment is active, only rely on `hasContentFills`\n\t// to determine whether the content tab to be shown. The tab purely uses slot\n\t// fills in this situation.\n\tconst shouldShowBlockFields =\n\t\twindow?.__experimentalContentOnlyInspectorFields;\n\tconst hasContentTab =\n\t\thasContentFills ||\n\t\t( ! shouldShowBlockFields && contentClientIds?.length );\n\n\tif ( hasContentTab ) {\n\t\ttabs.push( TAB_CONTENT );\n\t}\n\n\t// Add the tabs in the order that they will default to if available.\n\t// List View > Content > Settings > Styles.\n\tif ( hasListFills ) {\n\t\ttabs.push( TAB_LIST_VIEW );\n\t}\n\n\tif (\n\t\t( settingsFills.length ||\n\t\t\t// Advanded fills who up in settings tab if available or they blend into the default tab, if there's only one tab.\n\t\t\t( advancedFills.length && ( hasContentTab || hasListFills ) ) ) &&\n\t\t! isSectionBlock\n\t) {\n\t\ttabs.push( TAB_SETTINGS );\n\t}\n\n\tif ( hasBlockStyles || hasStyleFills ) {\n\t\ttabs.push( TAB_STYLES );\n\t}\n\n\tconst tabSettings = useSelect( ( select ) => {\n\t\treturn select( blockEditorStore ).getSettings().blockInspectorTabs;\n\t}, [] );\n\n\tconst showTabs = getShowTabs( blockName, tabSettings );\n\treturn showTabs ? tabs : EMPTY_ARRAY;\n}\n"], "mappings": ";AAGA,SAAS,8BAA8B,oBAAoB;AAC3D,SAAS,iBAAiB;AAK1B,OAAO,6BAA6B;AACpC,SAAS,iCAAiC;AAC1C,SAAS,eAAe,cAAc,YAAY,mBAAmB;AACrE,SAAS,SAAS,wBAAwB;AAE1C,IAAM,cAAc,CAAC;AAErB,SAAS,YAAa,WAAW,cAAc,CAAC,GAAI;AAEnD,MAAK,YAAa,SAAU,MAAM,QAAY;AAC7C,WAAO,YAAa,SAAU;AAAA,EAC/B;AAGA,MAAK,YAAY,YAAY,QAAY;AACxC,WAAO,YAAY;AAAA,EACpB;AAEA,SAAO;AACR;AAEe,SAAR,yBACN,WACA,kBACA,gBACA,gBACC;AACD,QAAM,OAAO,CAAC;AACd,QAAM;AAAA,IACL,UAAU;AAAA,IACV,QAAQ;AAAA,IACR,OAAO;AAAA,IACP,SAAS;AAAA,IACT,SAAS;AAAA,IACT,YAAY;AAAA,IACZ,MAAM;AAAA,IACN,UAAU;AAAA,IACV,QAAQ;AAAA,IACR,YAAY;AAAA,IACZ,SAAS;AAAA,EACV,IAAI;AAGJ,QAAM,YAAY,aAAc,UAAU,IAAK;AAC/C,QAAM,eAAe,CAAC,CAAE,aAAa,UAAU;AAG/C,QAAM,eAAe,aAAc,aAAa,IAAK;AACrD,QAAM,kBAAkB,CAAC,CAAE,gBAAgB,aAAa;AAIxD,QAAM,aAAa;AAAA,IAClB,GAAK,aAAc,YAAY,IAAK,KAAK,CAAC;AAAA,IAC1C,GAAK,aAAc,WAAW,IAAK,KAAK,CAAC;AAAA,IACzC,GAAK,aAAc,gBAAgB,IAAK,KAAK,CAAC;AAAA,IAC9C,GAAK,aAAc,YAAY,IAAK,KAAK,CAAC;AAAA,IAC1C,GAAK,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