-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathindex.native.js
140 lines (125 loc) · 3.99 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
/**
* External dependencies
*/
import { Button, Modal, SafeAreaView } from 'react-native';
import WebView from 'react-native-webview';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
export class UnsupportedBlockModal extends Component {
render() {
const { blockHTML } = this.props;
// Escape new-lines
const escapedBlockHTML = blockHTML.replace( /\n/g, '\\n' );
const scriptToInject = `
window.onSave = () => {
const blocks = window.wp.data.select( 'core/block-editor' ).getBlocks();
const HTML = window.wp.blocks.serialize( blocks );
window.ReactNativeWebView.postMessage( HTML );
};
window.insertBlock = () => {
window.setTimeout( () => {
const blockHTML = '${ escapedBlockHTML }';
const blocks = window.wp.blocks.parse( blockHTML );
window.wp.data.dispatch( 'core/block-editor' ).resetBlocks( blocks );
}, 0 );
};
window.onload = () => {
const wpAdminBar = document.getElementById( 'wpadminbar' );
const wpToolbar = document.getElementById( 'wp-toolbar' );
if ( wpAdminBar ) {
wpAdminBar.style.display = 'none';
}
if ( wpToolbar ) {
wpToolbar.style.display = 'none';
}
const content = document.getElementById( 'wpbody-content' );
if ( content ) {
const callback = function( mutationsList, observer ) {
const header = document.getElementsByClassName(
'edit-post-header'
)[ 0 ];
const postTitle = document.getElementById( 'post-title-0' );
if ( postTitle && header.id === '' ) {
header.id = 'gb-header';
header.style.height = 0;
postTitle.style.display = 'none';
Array.from( header.children ).forEach( ( child ) => {
child.style.display = 'none';
} );
const headerToolbar = header.getElementsByClassName(
'edit-post-header-toolbar'
)[ 0 ];
headerToolbar.style.display = null;
headerToolbar.parentNode.style.display = null;
const inserterToggle = header.getElementsByClassName(
'block-editor-inserter__toggle'
)[ 0 ];
inserterToggle.style.display = 'none';
const blockToolbar = header.getElementsByClassName(
'edit-post-header-toolbar__block-toolbar'
)[ 0 ];
blockToolbar.style.top = '0px';
// const skeleton = document.getElementsByClassName(
// 'block-editor-editor-skeleton'
// )[ 0 ];
// skeleton.style.top = '0px';
const appender = document.getElementsByClassName(
'block-list-appender'
)[ 0 ];
if ( appender && appender.id === '' ) {
appender.id = 'appender';
appender.style.display = 'none';
}
window.insertBlock();
observer.disconnect();
}
};
/* eslint-disable-next-line no-undef */
const observer = new MutationObserver( callback );
const config = { attributes: true, childList: true, subtree: true };
observer.observe( content, config );
}
};
/* eslint-disable-next-line no-unused-expressions */
true; // react-native-webview asks for it
`;
return (
<Modal
animationType="slide"
presentationStyle="fullScreen"
visible={ this.props.visible }
onRequestClose={ () => this.props.closeModal() }
>
<SafeAreaView style={ { flex: 1, backgroundColor: 'black' } }>
<Button
onPress={ () => this.props.closeModal() }
title="Close"
color="red"
/>
<Button
onPress={ () =>
this.webref.injectJavaScript( 'window.onSave()' )
}
title="Save"
color="red"
/>
<WebView
ref={ ( r ) => ( this.webref = r ) }
source={ {
uri:
'https://cruisinglamb.wordpress.com/wp-admin/post-new.php',
} }
injectedJavaScript={ scriptToInject }
onMessage={ ( event ) => {
const html = event.nativeEvent.data;
this.props.closeModal( html );
} }
/>
</SafeAreaView>
</Modal>
);
}
}
export default UnsupportedBlockModal;