-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.native.js
146 lines (124 loc) · 3.49 KB
/
index.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
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 { Platform, SafeAreaView, View } from 'react-native';
import SafeArea from 'react-native-safe-area';
import { sendNativeEditorDidLayout } from 'react-native-gutenberg-bridge';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { HTMLTextInput, KeyboardAvoidingView, ReadableContentView } from '@wordpress/components';
/**
* Internal dependencies
*/
import styles from './style.scss';
import headerToolbarStyles from '../header/header-toolbar/style.scss';
import Header from '../header';
import VisualEditor from '../visual-editor';
class Layout extends Component {
constructor() {
super( ...arguments );
this.onSafeAreaInsetsUpdate = this.onSafeAreaInsetsUpdate.bind( this );
this.onRootViewLayout = this.onRootViewLayout.bind( this );
this.state = {
rootViewHeight: 0,
safeAreaInsets: { top: 0, bottom: 0, right: 0, left: 0 },
isFullyBordered: true,
};
SafeArea.getSafeAreaInsetsForRootView().then( this.onSafeAreaInsetsUpdate );
}
componentDidMount() {
this._isMounted = true;
SafeArea.addEventListener( 'safeAreaInsetsForRootViewDidChange', this.onSafeAreaInsetsUpdate );
}
componentWillUnmount() {
SafeArea.removeEventListener( 'safeAreaInsetsForRootViewDidChange', this.onSafeAreaInsetsUpdate );
this._isMounted = false;
}
onSafeAreaInsetsUpdate( result ) {
const { safeAreaInsets } = result;
if ( this._isMounted ) {
this.setState( { safeAreaInsets } );
}
}
onRootViewLayout( event ) {
if ( this._isMounted ) {
this.setHeightState( event );
this.setBorderStyleState();
}
}
setHeightState( event ) {
const { height } = event.nativeEvent.layout;
this.setState( { rootViewHeight: height }, sendNativeEditorDidLayout );
}
setBorderStyleState() {
const isFullyBordered = ReadableContentView.isContentMaxWidth();
if ( isFullyBordered !== this.state.isFullyBordered ) {
this.setState( { isFullyBordered } );
}
}
renderHTML() {
return (
<HTMLTextInput />
);
}
renderVisual() {
const {
isReady,
} = this.props;
if ( ! isReady ) {
return null;
}
return (
<VisualEditor
isFullyBordered={ this.state.isFullyBordered }
setTitleRef={ this.props.setTitleRef }
/>
);
}
render() {
const {
mode,
} = this.props;
// add a margin view at the bottom for the header
const marginBottom = Platform.OS === 'android' ? headerToolbarStyles.container.height : 0;
const toolbarKeyboardAvoidingViewStyle = {
...styles.toolbarKeyboardAvoidingView,
left: this.state.safeAreaInsets.left,
right: this.state.safeAreaInsets.right,
bottom: this.state.safeAreaInsets.bottom,
};
return (
<SafeAreaView style={ styles.container } onLayout={ this.onRootViewLayout }>
<View style={ { flex: 1 } }>
{ mode === 'text' ? this.renderHTML() : this.renderVisual() }
</View>
<View style={ { flex: 0, flexBasis: marginBottom, height: marginBottom } }>
</View>
<KeyboardAvoidingView
parentHeight={ this.state.rootViewHeight }
style={ toolbarKeyboardAvoidingViewStyle }
>
<Header />
</KeyboardAvoidingView>
</SafeAreaView>
);
}
}
export default compose( [
withSelect( ( select ) => {
const {
__unstableIsEditorReady: isEditorReady,
} = select( 'core/editor' );
const {
getEditorMode,
} = select( 'core/edit-post' );
return {
isReady: isEditorReady(),
mode: getEditorMode(),
};
} ),
] )( Layout );