-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from wordpress-mobile/feature/html-inputview-…
…component Adding HTMLTextInput component
- Loading branch information
Showing
4 changed files
with
101 additions
and
64 deletions.
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
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
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,78 @@ | ||
/** | ||
* @format | ||
* @flow | ||
*/ | ||
|
||
import React from 'react'; | ||
import { Platform, TextInput, KeyboardAvoidingView } from 'react-native'; | ||
import styles from './html-text-input.scss'; | ||
|
||
// Gutenberg imports | ||
import { serialize } from '@wordpress/blocks'; | ||
|
||
type PropsType = { | ||
blocks: Array<Object>, | ||
parseBlocksAction: string => mixed, | ||
}; | ||
|
||
type StateType = { | ||
html: string, | ||
}; | ||
|
||
export default class HTMLInputView extends React.Component<PropsType, StateType> { | ||
state = { | ||
html: '', | ||
} | ||
|
||
componentDidMount() { | ||
const html = this.serializeBlocksToHtml(); | ||
this.setState( { html } ); | ||
} | ||
|
||
componentWillUnmount() { | ||
//TODO: Blocking main thread | ||
this.props.parseBlocksAction( this.state.html ); | ||
} | ||
|
||
shouldComponentUpdate() { | ||
const isIOS = Platform.OS === 'ios'; | ||
if ( isIOS ) { | ||
// iOS TextInput gets jumpy if it's updated on every key stroke. | ||
// The first render will always be empty, so: | ||
// If the previous state was empty, we let update the component to show the next state. | ||
return this.state.html === ''; | ||
} | ||
return true; | ||
} | ||
|
||
serializeBlocksToHtml() { | ||
return this.props.blocks | ||
.map( this.serializeBlock ) | ||
.join( '' ); | ||
} | ||
|
||
serializeBlock( block: Object ) { | ||
if ( block.name === 'aztec' ) { | ||
return '<aztec>' + block.attributes.content + '</aztec>\n\n'; | ||
} | ||
|
||
return serialize( [ block ] ) + '\n\n'; | ||
} | ||
|
||
render() { | ||
const behavior = Platform.OS === 'ios' ? 'padding' : null; | ||
|
||
return ( | ||
<KeyboardAvoidingView style={ styles.container } behavior={ behavior }> | ||
<TextInput | ||
textAlignVertical="top" | ||
multiline | ||
numberOfLines={ 0 } | ||
style={ styles.htmlView } | ||
value={ this.state.html } | ||
onChangeText={ ( html ) => this.setState( { html } ) } | ||
/> | ||
</KeyboardAvoidingView> | ||
); | ||
} | ||
} |
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,18 @@ | ||
/** @format */ | ||
|
||
@import '../variables.scss'; | ||
|
||
.htmlView { | ||
font-family: $default-monospace-font; | ||
flex: 1; | ||
background-color: #eee; | ||
padding-left: 8; | ||
padding-right: 8; | ||
padding-top: 8; | ||
padding-bottom: 8; | ||
} | ||
|
||
.container { | ||
flex: 1; | ||
} | ||
|