Skip to content
Open
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
57 changes: 27 additions & 30 deletions src/RichTextToolbar.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {ListView, View, TouchableOpacity, Image, StyleSheet} from 'react-native';
import {actions} from './const';
import { FlatList, View, TouchableOpacity, Image, StyleSheet } from 'react-native';
import { actions } from './const';

const defaultActions = [
actions.insertImage,
Expand Down Expand Up @@ -46,20 +46,20 @@ export default class RichTextToolbar extends Component {
editor: undefined,
selectedItems: [],
actions,
ds: new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}).cloneWithRows(this.getRows(actions, []))
dataSet: this.getRows(actions, [])
};
}

componentDidReceiveProps(newProps) {
const actions = newProps.actions ? newProps.actions : defaultActions;
this.setState({
actions,
ds: this.state.ds.cloneWithRows(this.getRows(actions, this.state.selectedItems))
dataSet: this.getRows(actions, this.state.selectedItems)
});
}

getRows(actions, selectedItems) {
return actions.map((action) => {return {action, selected: selectedItems.includes(action)};});
return actions.map((action) => { return { action, selected: selectedItems.includes(action) }; });
}

componentDidMount() {
Expand All @@ -68,15 +68,15 @@ export default class RichTextToolbar extends Component {
throw new Error('Toolbar has no editor!');
} else {
editor.registerToolbar((selectedItems) => this.setSelectedItems(selectedItems));
this.setState({editor});
this.setState({ editor });
}
}

setSelectedItems(selectedItems) {
if (selectedItems !== this.state.selectedItems) {
this.setState({
selectedItems,
ds: this.state.ds.cloneWithRows(this.getRows(this.state.actions, selectedItems))
dataSet: this.getRows(this.state.actions, selectedItems)
});
}
}
Expand All @@ -92,7 +92,7 @@ export default class RichTextToolbar extends Component {
_getButtonIcon(action) {
if (this.props.iconMap && this.props.iconMap[action]) {
return this.props.iconMap[action];
} else if (getDefaultIcon()[action]){
} else if (getDefaultIcon()[action]) {
return getDefaultIcon()[action];
} else {
return undefined;
Expand All @@ -103,41 +103,38 @@ export default class RichTextToolbar extends Component {
const icon = this._getButtonIcon(action);
return (
<TouchableOpacity
key={action}
style={[
{height: 50, width: 50, justifyContent: 'center'},
selected ? this._getButtonSelectedStyle() : this._getButtonUnselectedStyle()
]}
onPress={() => this._onPress(action)}
key={action}
style={[
{ height: 50, width: 50, justifyContent: 'center' },
selected ? this._getButtonSelectedStyle() : this._getButtonUnselectedStyle()
]}
onPress={() => this._onPress(action)}
>
{icon ? <Image source={icon} style={{tintColor: selected ? this.props.selectedIconTint : this.props.iconTint}}/> : null}
{icon ? <Image source={icon} style={{ tintColor: selected ? this.props.selectedIconTint : this.props.iconTint }} /> : null}
</TouchableOpacity>
);
}

_renderAction(action, selected) {
return this.props.renderAction ?
this.props.renderAction(action, selected) :
this._defaultRenderAction(action, selected);
this.props.renderAction(action, selected) :
this._defaultRenderAction(action, selected);
}

render() {
return (
<View
style={[{height: 50, backgroundColor: '#D3D3D3', alignItems: 'center'}, this.props.style]}
>
<ListView
horizontal
contentContainerStyle={{flexDirection: 'row'}}
dataSource={this.state.ds}
renderRow= {(row) => this._renderAction(row.action, row.selected)}
<View style={[{ height: 50, backgroundColor: '#D3D3D3', alignItems: 'center' }, this.props.style]}>
<FlatList
data={this.state.dataSet}
numColumns={this.state.actions.length}
renderItem={(item) => this._renderAction(item.item.action, item.item.selected)}
/>
</View>
);
)
}

_onPress(action) {
switch(action) {
switch (action) {
case actions.setBold:
case actions.setItalic:
case actions.insertBulletsList:
Expand Down Expand Up @@ -165,7 +162,7 @@ export default class RichTextToolbar extends Component {
break;
case actions.insertLink:
this.state.editor.prepareInsert();
if(this.props.onPressAddLink) {
if (this.props.onPressAddLink) {
this.props.onPressAddLink();
} else {
this.state.editor.getSelectedText().then(selectedText => {
Expand All @@ -175,7 +172,7 @@ export default class RichTextToolbar extends Component {
break;
case actions.insertImage:
this.state.editor.prepareInsert();
if(this.props.onPressAddImage) {
if (this.props.onPressAddImage) {
this.props.onPressAddImage();
}
break;
Expand All @@ -189,4 +186,4 @@ const styles = StyleSheet.create({
backgroundColor: 'red'
},
defaultUnselectedButton: {}
});
});