UNPKG

2.04 kBJavaScriptView Raw
1/**
2 * Internal dependencies
3 */
4import HeadingToolbar from './heading-toolbar';
5
6/**
7 * External dependencies
8 */
9import { View } from 'react-native';
10
11/**
12 * WordPress dependencies
13 */
14import { __ } from '@wordpress/i18n';
15import { Component } from '@wordpress/element';
16import { RichText, BlockControls } from '@wordpress/block-editor';
17import { createBlock } from '@wordpress/blocks';
18
19/**
20 * Internal dependencies
21 */
22import './editor.scss';
23
24const minHeight = 24;
25
26class HeadingEdit extends Component {
27 constructor( props ) {
28 super( props );
29
30 this.state = {
31 aztecHeight: 0,
32 };
33 }
34
35 render() {
36 const {
37 attributes,
38 setAttributes,
39 mergeBlocks,
40 insertBlocksAfter,
41 } = this.props;
42
43 const {
44 level,
45 placeholder,
46 content,
47 } = attributes;
48
49 const tagName = 'h' + level;
50 return (
51 <View>
52 <BlockControls>
53 <HeadingToolbar minLevel={ 2 } maxLevel={ 5 } selectedLevel={ level } onChange={ ( newLevel ) => setAttributes( { level: newLevel } ) } />
54 </BlockControls>
55 <RichText
56 tagName={ tagName }
57 value={ content }
58 isSelected={ this.props.isSelected }
59 onFocus={ this.props.onFocus } // always assign onFocus as a props
60 onBlur={ this.props.onBlur } // always assign onBlur as a props
61 onCaretVerticalPositionChange={ this.props.onCaretVerticalPositionChange }
62 style={ {
63 minHeight: Math.max( minHeight, this.state.aztecHeight ),
64 } }
65 onChange={ ( value ) => setAttributes( { content: value } ) }
66 onMerge={ mergeBlocks }
67 onSplit={
68 insertBlocksAfter ?
69 ( before, after, ...blocks ) => {
70 setAttributes( { content: before } );
71 insertBlocksAfter( [
72 ...blocks,
73 createBlock( 'core/paragraph', { content: after } ),
74 ] );
75 } :
76 undefined
77 }
78 onContentSizeChange={ ( event ) => {
79 this.setState( { aztecHeight: event.aztecHeight } );
80 } }
81 placeholder={ placeholder || __( 'Write heading…' ) }
82 />
83 </View>
84 );
85 }
86}
87export default HeadingEdit;