/
index.js
170 lines (155 loc) · 4.35 KB
/
index.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* External dependencies
*/
import { find, noop } from 'lodash';
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import TokenList from '@wordpress/token-list';
import { ENTER, SPACE } from '@wordpress/keycodes';
import { _x } from '@wordpress/i18n';
import { getBlockType, cloneBlock, getBlockFromExample } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import BlockPreview from '../block-preview';
/**
* Returns the active style from the given className.
*
* @param {Array} styles Block style variations.
* @param {string} className Class name
*
* @return {Object?} The active style.
*/
export function getActiveStyle( styles, className ) {
for ( const style of new TokenList( className ).values() ) {
if ( style.indexOf( 'is-style-' ) === -1 ) {
continue;
}
const potentialStyleName = style.substring( 9 );
const activeStyle = find( styles, { name: potentialStyleName } );
if ( activeStyle ) {
return activeStyle;
}
}
return find( styles, 'isDefault' );
}
/**
* Replaces the active style in the block's className.
*
* @param {string} className Class name.
* @param {Object?} activeStyle The replaced style.
* @param {Object} newStyle The replacing style.
*
* @return {string} The updated className.
*/
export function replaceActiveStyle( className, activeStyle, newStyle ) {
const list = new TokenList( className );
if ( activeStyle ) {
list.remove( 'is-style-' + activeStyle.name );
}
list.add( 'is-style-' + newStyle.name );
return list.value;
}
function BlockStyles( {
styles,
className,
onChangeClassName,
type,
block,
onSwitch = noop,
onHoverClassName = noop,
} ) {
if ( ! styles || styles.length === 0 ) {
return null;
}
if ( ! type.styles && ! find( styles, 'isDefault' ) ) {
styles = [
{
name: 'default',
label: _x( 'Default', 'block style' ),
isDefault: true,
},
...styles,
];
}
const activeStyle = getActiveStyle( styles, className );
function updateClassName( style ) {
const updatedClassName = replaceActiveStyle( className, activeStyle, style );
onChangeClassName( updatedClassName );
onHoverClassName( null );
onSwitch();
}
return (
<div className="editor-block-styles block-editor-block-styles">
{ styles.map( ( style ) => {
const styleClassName = replaceActiveStyle( className, activeStyle, style );
return (
<div
key={ style.name }
className={ classnames(
'editor-block-styles__item block-editor-block-styles__item', {
'is-active': activeStyle === style,
}
) }
onClick={ () => updateClassName( style ) }
onKeyDown={ ( event ) => {
if ( ENTER === event.keyCode || SPACE === event.keyCode ) {
event.preventDefault();
updateClassName( style );
}
} }
onMouseEnter={ () => onHoverClassName( styleClassName ) }
onMouseLeave={ () => onHoverClassName( null ) }
role="button"
tabIndex="0"
aria-label={ style.label || style.name }
>
<div className="editor-block-styles__item-preview block-editor-block-styles__item-preview">
<BlockPreview
viewportWidth={ 500 }
blocks={
type.example ?
getBlockFromExample( block.name, {
attributes: { ...type.example.attributes, className: styleClassName },
innerBlocks: type.example.innerBlocks,
} ) :
cloneBlock( block, { className: styleClassName } )
}
/>
</div>
<div className="editor-block-styles__item-label block-editor-block-styles__item-label">
{ style.label || style.name }
</div>
</div>
);
} ) }
</div>
);
}
export default compose( [
withSelect( ( select, { clientId } ) => {
const { getBlock } = select( 'core/block-editor' );
const { getBlockStyles } = select( 'core/blocks' );
const block = getBlock( clientId );
const blockType = getBlockType( block.name );
return {
block,
className: block.attributes.className || '',
styles: getBlockStyles( block.name ),
type: blockType,
};
} ),
withDispatch( ( dispatch, { clientId } ) => {
return {
onChangeClassName( newClassName ) {
dispatch( 'core/block-editor' ).updateBlockAttributes( clientId, {
className: newClassName,
} );
},
};
} ),
] )( BlockStyles );