-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathshuffle.js
111 lines (103 loc) · 3.36 KB
/
shuffle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* WordPress dependencies
*/
import { shuffle } from '@wordpress/icons';
import { ToolbarButton, ToolbarGroup } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useMemo } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
const EMPTY_ARRAY = [];
function Container( props ) {
return (
<ToolbarGroup>
<ToolbarButton { ...props } />
</ToolbarGroup>
);
}
export default function Shuffle( { clientId, as = Container } ) {
const { categories, patterns, patternName } = useSelect(
( select ) => {
const {
getBlockAttributes,
getBlockRootClientId,
__experimentalGetAllowedPatterns,
} = select( blockEditorStore );
const attributes = getBlockAttributes( clientId );
const _categories = attributes?.metadata?.categories || EMPTY_ARRAY;
const _patternName = attributes?.metadata?.patternName;
const rootBlock = getBlockRootClientId( clientId );
// Calling `__experimentalGetAllowedPatterns` is expensive.
// Checking if the block can be shuffled prevents unnecessary selector calls.
// See: https://github.com/WordPress/gutenberg/pull/64736.
const _patterns =
_categories.length > 0
? __experimentalGetAllowedPatterns( rootBlock )
: EMPTY_ARRAY;
return {
categories: _categories,
patterns: _patterns,
patternName: _patternName,
};
},
[ clientId ]
);
const { replaceBlocks } = useDispatch( blockEditorStore );
const sameCategoryPatternsWithSingleWrapper = useMemo( () => {
if ( categories.length === 0 || ! patterns || patterns.length === 0 ) {
return EMPTY_ARRAY;
}
return patterns.filter( ( pattern ) => {
const isCorePattern =
pattern.source === 'core' ||
( pattern.source?.startsWith( 'pattern-directory' ) &&
pattern.source !== 'pattern-directory/theme' );
return (
// Check if the pattern has only one top level block,
// otherwise we may shuffle to pattern that will not allow to continue shuffling.
pattern.blocks.length === 1 &&
// We exclude the core patterns and pattern directory patterns that are not theme patterns.
! isCorePattern &&
pattern.categories?.some( ( category ) => {
return categories.includes( category );
} ) &&
// Check if the pattern is not a synced pattern.
( pattern.syncStatus === 'unsynced' || ! pattern.id )
);
} );
}, [ categories, patterns ] );
if ( sameCategoryPatternsWithSingleWrapper.length < 2 ) {
return null;
}
function getNextPattern() {
const numberOfPatterns = sameCategoryPatternsWithSingleWrapper.length;
const patternIndex = sameCategoryPatternsWithSingleWrapper.findIndex(
( { name } ) => name === patternName
);
const nextPatternIndex =
patternIndex + 1 < numberOfPatterns ? patternIndex + 1 : 0;
return sameCategoryPatternsWithSingleWrapper[ nextPatternIndex ];
}
const ComponentToUse = as;
return (
<ComponentToUse
label={ __( 'Shuffle' ) }
icon={ shuffle }
className="block-editor-block-toolbar-shuffle"
onClick={ () => {
const nextPattern = getNextPattern();
nextPattern.blocks[ 0 ].attributes = {
...nextPattern.blocks[ 0 ].attributes,
metadata: {
...nextPattern.blocks[ 0 ].attributes.metadata,
categories,
},
};
replaceBlocks( clientId, nextPattern.blocks );
} }
/>
);
}