-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathedit.js
146 lines (139 loc) · 3.72 KB
/
edit.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
/**
* External dependencies
*/
import classNames from 'classnames';
/**
* WordPress dependencies
*/
import {
useEntityProp,
__experimentalUseEntitySaving,
} from '@wordpress/core-data';
import { Button, PanelBody } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import {
AlignmentToolbar,
BlockControls,
ContrastChecker,
FontSizePicker,
InspectorControls,
PanelColorSettings,
PlainText,
withColors,
withFontSizes,
} from '@wordpress/block-editor';
import { compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import { ENTER } from '@wordpress/keycodes';
function SiteDescriptionEdit( {
attributes,
backgroundColor,
className,
fontSize,
insertDefaultBlock,
setAttributes,
setBackgroundColor,
setFontSize,
setTextColor,
textColor,
} ) {
const [ description, setDescription ] = useEntityProp( 'root', 'site', 'description' );
const [ isDirty, isSaving, save ] = __experimentalUseEntitySaving(
'root',
'site',
'description'
);
const { customFontSize, textAlign } = attributes;
const actualFontSize = customFontSize || fontSize.size;
const preventNewlines = ( event ) => {
if ( event.keyCode === ENTER ) {
event.preventDefault();
insertDefaultBlock();
}
};
return (
<>
<BlockControls>
<AlignmentToolbar
value={ textAlign }
onChange={ ( nextAlign ) => setAttributes( { textAlign: nextAlign } ) }
/>
</BlockControls>
<InspectorControls>
<PanelBody className="blocks-font-size" title={ __( 'Text Settings' ) }>
<FontSizePicker onChange={ setFontSize } value={ actualFontSize } />
</PanelBody>
<PanelColorSettings
title={ __( 'Color Settings' ) }
initialOpen={ false }
colorSettings={ [
{
value: backgroundColor.color,
onChange: setBackgroundColor,
label: __( 'Background Color' ),
},
{
value: textColor.color,
onChange: setTextColor,
label: __( 'Text Color' ),
},
] }
>
<ContrastChecker
{ ...{
textColor: textColor.color,
backgroundColor: backgroundColor.color,
} }
fontSize={ actualFontSize }
/>
</PanelColorSettings>
</InspectorControls>
<Button
isPrimary
className="wp-block-site-description__save-button"
disabled={ ! isDirty || ! description }
isBusy={ isSaving }
onClick={ save }
>
{ __( 'Update' ) }
</Button>
<PlainText
className={ classNames( 'site-description', className, {
'has-text-color': textColor.color,
'has-background': backgroundColor.color,
[ `has-text-align-${ textAlign }` ]: textAlign,
[ backgroundColor.class ]: backgroundColor.class,
[ textColor.class ]: textColor.class,
[ fontSize.class ]: ! customFontSize && fontSize.class,
} ) }
isStylable
multiline={ false }
onChange={ setDescription }
onKeyDown={ preventNewlines }
placeholder={ __( 'Site Description' ) }
style={ {
backgroundColor: backgroundColor.color,
color: textColor.color,
fontSize: actualFontSize ? actualFontSize + 'px' : undefined,
} }
value={ description }
/>
</>
);
}
export default compose( [
withColors( 'backgroundColor', { textColor: 'color' } ),
withFontSizes( 'fontSize' ),
withSelect( ( select, { clientId } ) => {
const { getBlockIndex, getBlockRootClientId } = select( 'core/block-editor' );
const rootClientId = getBlockRootClientId( clientId );
return {
blockIndex: getBlockIndex( clientId, rootClientId ),
rootClientId,
};
} ),
withDispatch( ( dispatch, { blockIndex, rootClientId } ) => ( {
insertDefaultBlock: () =>
dispatch( 'core/block-editor' ).insertDefaultBlock( undefined, rootClientId, blockIndex + 1 ),
} ) ),
] )( SiteDescriptionEdit );