|
| 1 | +/* eslint-disable import/no-extraneous-dependencies */ |
| 2 | +/** |
| 3 | + * External dependencies |
| 4 | + */ |
| 5 | +import { getCategories, setCategories } from '@wordpress/blocks'; |
| 6 | +import domReady from '@wordpress/dom-ready'; |
| 7 | + |
| 8 | +const categorySlugs = [ 'jetpack', 'coblocks', 'coblocks-galleries' ]; |
| 9 | + |
| 10 | +// Very fragile checks, we'll replace with proper common bundle splitting in https://github.com/Automattic/wp-calypso/issues/34476 |
| 11 | +const isSimpleSite = !! window.wpcomGutenberg.pluginVersion; |
| 12 | +const isAtomicSite = window._currentSiteType === 'atomic'; |
| 13 | + |
| 14 | +if ( isAtomicSite || isSimpleSite ) { |
| 15 | + domReady( function() { |
| 16 | + //preserve order of other columns, and split matching |
| 17 | + const { core: coreCategories, custom: unsorted } = getCategories().reduce( |
| 18 | + ( { core, custom }, category ) => { |
| 19 | + const isCustomCategory = categorySlugs.includes( category.slug ); |
| 20 | + if ( isCustomCategory ) { |
| 21 | + return { |
| 22 | + core, |
| 23 | + custom: [ ...custom, category ], |
| 24 | + }; |
| 25 | + } |
| 26 | + return { |
| 27 | + core: [ ...core, category ], |
| 28 | + custom, |
| 29 | + }; |
| 30 | + }, |
| 31 | + { custom: [], core: [] } |
| 32 | + ); |
| 33 | + //sort once following order of categorySlugs |
| 34 | + const customCategories = unsorted.sort( ( { slug }, { slug: slugB } ) => { |
| 35 | + const index = categorySlugs.indexOf( slug ); |
| 36 | + const indexB = categorySlugs.indexOf( slugB ); |
| 37 | + if ( index === indexB ) { |
| 38 | + return 0; |
| 39 | + } |
| 40 | + if ( index < indexB ) { |
| 41 | + return -1; |
| 42 | + } |
| 43 | + return 1; |
| 44 | + } ); |
| 45 | + setCategories( [ ...coreCategories, ...customCategories ] ); |
| 46 | + } ); |
| 47 | +} |
0 commit comments