-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathedit.native.js
104 lines (90 loc) · 2.25 KB
/
edit.native.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
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { createBlock } from '@wordpress/blocks';
import {
AlignmentControl,
BlockControls,
RichText,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useCallback } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
const name = 'core/paragraph';
const allowedParentBlockAlignments = [ 'left', 'center', 'right' ];
function ParagraphBlock( {
attributes,
mergeBlocks,
onReplace,
setAttributes,
style,
clientId,
parentBlockAlignment,
} ) {
const isRTL = useSelect( ( select ) => {
return !! select( blockEditorStore ).getSettings().isRTL;
}, [] );
const { align, content, placeholder } = attributes;
const styles = {
...( style?.baseColors && {
color: style.baseColors?.color?.text,
placeholderColor: style.color || style.baseColors?.color?.text,
linkColor: style.baseColors?.elements?.link?.color?.text,
} ),
...style,
};
const onAlignmentChange = useCallback( ( nextAlign ) => {
setAttributes( { align: nextAlign } );
}, [] );
const parentTextAlignment = allowedParentBlockAlignments.includes(
parentBlockAlignment
)
? parentBlockAlignment
: undefined;
const textAlignment = align || parentTextAlignment;
return (
<>
<BlockControls group="block">
<AlignmentControl
value={ align }
isRTL={ isRTL }
onChange={ onAlignmentChange }
/>
</BlockControls>
<RichText
identifier="content"
tagName="p"
value={ content }
deleteEnter={ true }
style={ styles }
onChange={ ( nextContent ) => {
setAttributes( {
content: nextContent,
} );
} }
onSplit={ ( value, isOriginal ) => {
let newAttributes;
if ( isOriginal || value ) {
newAttributes = {
...attributes,
content: value,
};
}
const block = createBlock( name, newAttributes );
if ( isOriginal ) {
block.clientId = clientId;
}
return block;
} }
onMerge={ mergeBlocks }
onReplace={ onReplace }
onRemove={ onReplace ? () => onReplace( [] ) : undefined }
placeholder={ placeholder || __( 'Start writing…' ) }
textAlign={ textAlignment }
__unstableEmbedURLOnPaste
/>
</>
);
}
export default ParagraphBlock;