-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathcustom-class-name.js
174 lines (160 loc) · 4.35 KB
/
custom-class-name.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
171
172
173
174
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { TextControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { hasBlockSupport } from '@wordpress/blocks';
import { createHigherOrderComponent } from '@wordpress/compose';
/**
* Internal dependencies
*/
import { InspectorControls } from '../components';
/**
* Filters registered block settings, extending attributes with anchor using ID
* of the first node.
*
* @param {Object} settings Original block settings.
*
* @return {Object} Filtered block settings.
*/
export function addAttribute( settings ) {
if ( hasBlockSupport( settings, 'customClassName', true ) ) {
// Gracefully handle if settings.attributes is undefined.
settings.attributes = {
...settings.attributes,
className: {
type: 'string',
},
};
}
return settings;
}
/**
* Override the default edit UI to include a new block inspector control for
* assigning the custom class name, if block supports custom class name.
*
* @param {WPComponent} BlockEdit Original component.
*
* @return {WPComponent} Wrapped component.
*/
export const withInspectorControl = createHigherOrderComponent(
( BlockEdit ) => {
return ( props ) => {
const hasCustomClassName = hasBlockSupport(
props.name,
'customClassName',
true
);
if ( hasCustomClassName && props.isSelected ) {
return (
<>
<BlockEdit { ...props } />
<InspectorControls __experimentalGroup="advanced">
<TextControl
autoComplete="off"
label={ __( 'Additional CSS class(es)' ) }
value={ props.attributes.className || '' }
onChange={ ( nextValue ) => {
props.setAttributes( {
className:
nextValue !== ''
? nextValue
: undefined,
} );
} }
help={ __(
'Separate multiple classes with spaces.'
) }
/>
</InspectorControls>
</>
);
}
return <BlockEdit { ...props } />;
};
},
'withInspectorControl'
);
/**
* Override props assigned to save component to inject anchor ID, if block
* supports anchor. This is only applied if the block's save result is an
* element and not a markup string.
*
* @param {Object} extraProps Additional props applied to save element.
* @param {Object} blockType Block type.
* @param {Object} attributes Current block attributes.
*
* @return {Object} Filtered props applied to save element.
*/
export function addSaveProps( extraProps, blockType, attributes ) {
if (
hasBlockSupport( blockType, 'customClassName', true ) &&
attributes.className
) {
extraProps.className = classnames(
extraProps.className,
attributes.className
);
}
return extraProps;
}
export function addTransforms( result, source, index, results ) {
if ( ! hasBlockSupport( result.name, 'customClassName', true ) ) {
return result;
}
// If the condition verifies we are probably in the presence of a wrapping transform
// e.g: nesting paragraphs in a group or columns and in that case the class should not be kept.
if ( results.length === 1 && result.innerBlocks.length === source.length ) {
return result;
}
// If we are transforming one block to multiple blocks or multiple blocks to one block,
// we ignore the class during the transform.
if (
( results.length === 1 && source.length > 1 ) ||
( results.length > 1 && source.length === 1 )
) {
return result;
}
// If we are in presence of transform between one or more block in the source
// that have one or more blocks in the result
// we apply the class on source N to the result N,
// if source N does not exists we do nothing.
if ( source[ index ] ) {
const originClassName = source[ index ]?.attributes.className;
if ( originClassName ) {
return {
...result,
attributes: {
...result.attributes,
className: originClassName,
},
};
}
}
return result;
}
addFilter(
'blocks.registerBlockType',
'core/custom-class-name/attribute',
addAttribute
);
addFilter(
'editor.BlockEdit',
'core/editor/custom-class-name/with-inspector-control',
withInspectorControl
);
addFilter(
'blocks.getSaveContent.extraProps',
'core/custom-class-name/save-props',
addSaveProps
);
addFilter(
'blocks.switchToBlockType.transformedBlock',
'core/color/addTransforms',
addTransforms
);