-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.native.js
207 lines (183 loc) · 4.77 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
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
/**
* External dependencies
*/
import { Platform, SafeAreaView, View } from 'react-native';
import SafeArea from 'react-native-safe-area';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withSelect } from '@wordpress/data';
import {
BottomSheetSettings,
FloatingToolbar,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { compose, withPreferredColorScheme } from '@wordpress/compose';
import {
HTMLTextInput,
KeyboardAvoidingView,
NoticeList,
Tooltip,
__unstableAutocompletionItemsSlot as AutocompletionItemsSlot,
} from '@wordpress/components';
import {
AutosaveMonitor,
OfflineStatus,
store as editorStore,
} from '@wordpress/editor';
import { sendNativeEditorDidLayout } from '@wordpress/react-native-bridge';
/**
* 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 },
};
SafeArea.getSafeAreaInsetsForRootView().then(
this.onSafeAreaInsetsUpdate
);
}
componentDidMount() {
this._isMounted = true;
this.safeAreaSubscription = SafeArea.addEventListener(
'safeAreaInsetsForRootViewDidChange',
this.onSafeAreaInsetsUpdate
);
}
componentWillUnmount() {
this.safeAreaSubscription?.remove();
this._isMounted = false;
}
onSafeAreaInsetsUpdate( result ) {
const { safeAreaInsets } = result;
if ( this._isMounted ) {
this.setState( { safeAreaInsets } );
}
}
onRootViewLayout( event ) {
if ( this._isMounted ) {
this.setHeightState( event );
}
}
setHeightState( event ) {
const { height } = event.nativeEvent.layout;
if ( height !== this.state.rootViewHeight ) {
this.setState(
{ rootViewHeight: height },
sendNativeEditorDidLayout
);
}
}
renderHTML() {
const { globalStyles } = this.props;
return (
<HTMLTextInput
parentHeight={ this.state.rootViewHeight }
style={ globalStyles }
/>
);
}
renderVisual() {
const { isReady } = this.props;
if ( ! isReady ) {
return null;
}
return <VisualEditor setTitleRef={ this.props.setTitleRef } />;
}
render() {
const { getStylesFromColorScheme, mode, globalStyles } = this.props;
const isHtmlView = mode === 'text';
// Add a margin view at the bottom for the header.
const marginBottom =
Platform.OS === 'android' && ! isHtmlView
? headerToolbarStyles[ 'header-toolbar__container' ].height
: 0;
const containerStyles = getStylesFromColorScheme(
styles.container,
styles.containerDark
);
const toolbarKeyboardAvoidingViewStyle = {
...styles.toolbarKeyboardAvoidingView,
left: this.state.safeAreaInsets.left,
right: this.state.safeAreaInsets.right,
bottom: this.state.safeAreaInsets.bottom,
backgroundColor: containerStyles.backgroundColor,
};
const editorStyles = [
getStylesFromColorScheme(
styles.background,
styles.backgroundDark
),
globalStyles?.background && {
backgroundColor: globalStyles.background,
},
];
return (
<Tooltip.Slot>
<SafeAreaView
style={ containerStyles }
onLayout={ this.onRootViewLayout }
>
<AutosaveMonitor disableIntervalChecks />
<OfflineStatus />
<View style={ editorStyles }>
{ isHtmlView ? this.renderHTML() : this.renderVisual() }
{ ! isHtmlView && Platform.OS === 'android' && (
<FloatingToolbar />
) }
<NoticeList />
</View>
<View
style={ {
flex: 0,
flexBasis: marginBottom,
height: marginBottom,
} }
/>
{ ! isHtmlView && (
<KeyboardAvoidingView
parentHeight={ this.state.rootViewHeight }
style={ toolbarKeyboardAvoidingViewStyle }
withAnimatedHeight
>
{ Platform.OS === 'ios' && (
<>
<AutocompletionItemsSlot />
<FloatingToolbar />
</>
) }
<Header />
<BottomSheetSettings />
</KeyboardAvoidingView>
) }
{ Platform.OS === 'android' && <AutocompletionItemsSlot /> }
</SafeAreaView>
</Tooltip.Slot>
);
}
}
export default compose( [
withSelect( ( select ) => {
const { __unstableIsEditorReady: isEditorReady, getEditorMode } =
select( editorStore );
const { getSettings } = select( blockEditorStore );
const globalStyles =
getSettings()?.__experimentalGlobalStylesBaseStyles?.color;
return {
isReady: isEditorReady(),
mode: getEditorMode(),
globalStyles,
};
} ),
withPreferredColorScheme,
] )( Layout );