-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathedit.js
213 lines (201 loc) · 4.8 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { __, _x } from '@wordpress/i18n';
import { PanelBody, ToggleControl, ToolbarGroup } from '@wordpress/components';
import {
AlignmentToolbar,
BlockControls,
FontSizePicker,
InspectorControls,
RichText,
withFontSizes,
__experimentalUseColors,
} from '@wordpress/block-editor';
import { createBlock } from '@wordpress/blocks';
import { compose } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import { useEffect, useState, useRef } from '@wordpress/element';
/**
* Browser dependencies
*/
const { getComputedStyle } = window;
const querySelector = window.document.querySelector.bind( document );
const name = 'core/paragraph';
const PARAGRAPH_DROP_CAP_SELECTOR = 'p.has-drop-cap';
function ParagraphRTLToolbar( { direction, setDirection } ) {
const isRTL = useSelect( ( select ) => {
return !! select( 'core/block-editor' ).getSettings().isRTL;
}, [] );
return (
isRTL && (
<ToolbarGroup
controls={ [
{
icon: 'editor-ltr',
title: _x( 'Left to right', 'editor button' ),
isActive: direction === 'ltr',
onClick() {
setDirection(
direction === 'ltr' ? undefined : 'ltr'
);
},
},
] }
/>
)
);
}
function useDropCapMinimumHeight( isDropCap, deps ) {
const [ minimumHeight, setMinimumHeight ] = useState();
useEffect( () => {
const element = querySelector( PARAGRAPH_DROP_CAP_SELECTOR );
if ( isDropCap && element ) {
setMinimumHeight(
getComputedStyle( element, 'first-letter' ).lineHeight
);
} else if ( minimumHeight ) {
setMinimumHeight( undefined );
}
}, [ isDropCap, minimumHeight, setMinimumHeight, ...deps ] );
return minimumHeight;
}
function ParagraphBlock( {
attributes,
className,
fontSize,
mergeBlocks,
onReplace,
setAttributes,
setFontSize,
} ) {
const { align, content, dropCap, placeholder, direction } = attributes;
const ref = useRef();
const dropCapMinimumHeight = useDropCapMinimumHeight( dropCap, [
fontSize.size,
] );
const {
TextColor,
BackgroundColor,
InspectorControlsColorPanel,
} = __experimentalUseColors(
[
{ name: 'textColor', property: 'color' },
{ name: 'backgroundColor', className: 'has-background' },
],
{
contrastCheckers: [
{
backgroundColor: true,
textColor: true,
fontSize: fontSize.size,
},
],
colorDetector: { targetRef: ref },
},
[ fontSize.size ]
);
return (
<>
<BlockControls>
<AlignmentToolbar
value={ align }
onChange={ ( newAlign ) =>
setAttributes( { align: newAlign } )
}
/>
<ParagraphRTLToolbar
direction={ direction }
setDirection={ ( newDirection ) =>
setAttributes( { direction: newDirection } )
}
/>
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Text settings' ) }>
<FontSizePicker
value={ fontSize.size }
onChange={ setFontSize }
/>
<ToggleControl
label={ __( 'Drop Cap' ) }
checked={ !! dropCap }
onChange={ () =>
setAttributes( { dropCap: ! dropCap } )
}
help={
dropCap
? __( 'Showing large initial letter.' )
: __( 'Toggle to show a large initial letter.' )
}
/>
</PanelBody>
</InspectorControls>
{ InspectorControlsColorPanel }
<BackgroundColor>
<TextColor>
<RichText
ref={ ref }
identifier="content"
tagName="p"
className={ classnames(
'wp-block-paragraph',
className,
{
'has-drop-cap': dropCap,
[ `has-text-align-${ align }` ]: align,
[ fontSize.class ]: fontSize.class,
}
) }
style={ {
fontSize: fontSize.size
? fontSize.size + 'px'
: undefined,
direction,
minHeight: dropCapMinimumHeight,
} }
value={ content }
onChange={ ( newContent ) =>
setAttributes( { content: newContent } )
}
onSplit={ ( value ) => {
if ( ! value ) {
return createBlock( name );
}
return createBlock( name, {
...attributes,
content: value,
} );
} }
onMerge={ mergeBlocks }
onReplace={ onReplace }
onRemove={
onReplace ? () => onReplace( [] ) : undefined
}
aria-label={
content
? __( 'Paragraph block' )
: __(
'Empty block; start writing or type forward slash to choose a block'
)
}
placeholder={
placeholder ||
__( 'Start writing or type / to choose a block' )
}
__unstableEmbedURLOnPaste
__unstableAllowPrefixTransformations
/>
</TextColor>
</BackgroundColor>
</>
);
}
const ParagraphEdit = compose( [ withFontSizes( 'fontSize' ) ] )(
ParagraphBlock
);
export default ParagraphEdit;