Skip to content
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

React Native Implement Basic text Toolbar actions #9267

Merged
merged 7 commits into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/components/src/button/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import { TouchableOpacity } from 'react-native';

export default function Button( props ) {
const { children, onClick, 'aria-label': ariaLabel } = props;
const { children, onClick, 'aria-label': ariaLabel, 'aria-pressed': ariaPressed } = props;
return (
<TouchableOpacity
accessible={ true }
accessibilityLabel={ ariaLabel }
onPress={ onClick }
style={ { borderColor: ariaPressed ? 'black' : 'white', borderWidth: 1, borderRadius: 2 } }
>
{ children }
</TouchableOpacity>
Expand Down
32 changes: 29 additions & 3 deletions packages/editor/src/components/rich-text/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ import { children } from '@wordpress/blocks';
import FormatToolbar from './format-toolbar';
import { FORMATTING_CONTROLS } from './formatting-controls';

export function getFormatValue( formatName ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gziolo I wonder why we need to export this function here. Can 't it be moved in RichText component since it's only used there?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is exported only to be used in tests

if ( 'link' === formatName ) {
//TODO: Implement link command
}
return { isActive: true };
}

export class RichText extends Component {
constructor() {
super( ...arguments );
this.onChange = this.onChange.bind( this );
this.onContentSizeChange = this.onContentSizeChange.bind( this );
this.changeFormats = this.changeFormats.bind( this );
this.onActiveFormatsChange = this.onActiveFormatsChange.bind( this );
this.state = {
formats: {},
selectedNodeId: 0,
Expand All @@ -35,6 +43,17 @@ export class RichText extends Component {
this.lastEventCount = 0;
}

onActiveFormatsChange( formats ) {
const newFormats = formats.reduce( ( accFormats, activeFormat ) => {
accFormats[ activeFormat ] = getFormatValue( activeFormat );
return accFormats;
}, {} );

this.setState( {
formats: merge( {}, newFormats ),
selectedNodeId: this.state.selectedNodeId + 1,
} );
}
/**
* Handles any case where the content of the AztecRN instance has changed.
*/
Expand Down Expand Up @@ -91,16 +110,18 @@ export class RichText extends Component {

// eslint-disable-next-line no-unused-vars
removeFormat( format ) {
//TODO: implement Aztec call to remove format
this._editor.applyFormat( format );
}

// eslint-disable-next-line no-unused-vars
applyFormat( format, args, node ) {
//TODO: implement Aztec call to apply format
this._editor.applyFormat( format );
}

changeFormats( formats ) {
const newStateFormats = {};
forEach( formats, ( formatValue, format ) => {
newStateFormats[ format ] = getFormatValue( format );
const isActive = this.isFormatActive( format );
if ( isActive && ! formatValue ) {
this.removeFormat( format );
Expand All @@ -110,7 +131,7 @@ export class RichText extends Component {
} );

this.setState( ( state ) => ( {
formats: merge( {}, state.formats, formats ),
formats: merge( {}, state.formats, newStateFormats ),
} ) );
}

Expand Down Expand Up @@ -139,9 +160,14 @@ export class RichText extends Component {
<View>
{ formatToolbar }
<RCTAztecView
ref={ ( ref ) => {
this._editor = ref;
}
}
text={ { text: html, eventCount: eventCount } }
onChange={ this.onChange }
onContentSizeChange={ this.onContentSizeChange }
onActiveFormatsChange={ this.onActiveFormatsChange }
color={ 'black' }
maxImagesWidth={ 200 }
style={ style }
Expand Down