-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Edit unsupported blocks in a RN web view #21832
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
34d7084
Adding actions to unsupported block sheet.
etoledom 6e3f0e3
Send unsupported fallback message to native
etoledom 73d3763
Edit unsupported blocks in RN web view
guarani a28005a
Fix package-lock file
guarani 939970c
Fixed block replacement
guarani 38ed383
Fixed styles
guarani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
140 changes: 140 additions & 0 deletions
140
packages/block-library/src/unsupported-block-modal/index.native.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have more control over when can we inject JS code?
I have noticed that to avoid some flickering of UI or CSS overrides we will need to inject some snippets as early as possible and others as late as possible.
There's for example this little flash of the WP Admin Bar while the page is loading. So we should inject the "hide WP Admin Bar" CSS earlier. But then the Editor CSS will override the Gutenberg top bar changes since it loads later. And to avoid that, we need to inject the Editor CSS as late as possible (when everything is loaded).