Keine Beschreibung

RichTextToolbar.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import React, {Component, PropTypes} from 'react';
  2. import {View, TouchableOpacity, Text} from 'react-native';
  3. import {actions} from './const';
  4. const defaultActions = [
  5. actions.insertImage,
  6. actions.setBold,
  7. actions.setItalic,
  8. actions.insertBulletsList,
  9. actions.insertOrderedList,
  10. actions.insertLink
  11. ];
  12. function getDefaultIconText() {
  13. const texts = {};
  14. texts[actions.insertImage] = 'IMG';
  15. texts[actions.setBold] = 'B';
  16. texts[actions.setItalic] = 'I';
  17. texts[actions.insertBulletsList] = '0';
  18. texts[actions.insertOrderedList] = '1';
  19. texts[actions.insertLink] = '<a>';
  20. return texts;
  21. }
  22. export default class RichTextToolbar extends Component {
  23. static propTypes = {
  24. getEditor: PropTypes.func.isRequired
  25. };
  26. constructor(props) {
  27. super(props);
  28. this.state = {
  29. editor: undefined,
  30. selectedItems: []
  31. };
  32. }
  33. componentDidMount() {
  34. const editor = this.props.getEditor();
  35. if (!editor) {
  36. throw new Error('Toolbar has no editor!');
  37. } else {
  38. editor.registerToolbar((selectedItems) => this.setSelectedItems(selectedItems));
  39. this.setState({editor});
  40. }
  41. }
  42. setSelectedItems(selectedItems) {
  43. this.setState({
  44. selectedItems
  45. });
  46. }
  47. _getButton(action, selected) {
  48. return (
  49. <TouchableOpacity
  50. key={action}
  51. style={{flex: 1, backgroundColor: selected? 'red' : '#D3D3D3', justifyContent: 'center'}}
  52. onPress={() => this._onPress(action)}
  53. >
  54. <Text style={{textAlign: 'center'}}>
  55. {getDefaultIconText()[action]}
  56. </Text>
  57. </TouchableOpacity>
  58. );
  59. }
  60. render() {
  61. return (
  62. <View style={{flexDirection: 'row', height: 50}}>
  63. {defaultActions.map((action) => this._getButton(action, this.state.selectedItems.includes(action)))}
  64. </View>
  65. );
  66. }
  67. _onPress(action) {
  68. switch(action) {
  69. case actions.setBold:
  70. case actions.setItalic:
  71. case actions.insertBulletsList:
  72. case actions.insertOrderedList:
  73. case actions.setUnderline:
  74. case actions.heading1:
  75. case actions.heading2:
  76. case actions.heading3:
  77. case actions.heading4:
  78. case actions.heading5:
  79. case actions.heading6:
  80. case actions.setParagraph:
  81. case actions.removeFormat:
  82. case actions.alignLeft:
  83. case actions.alignCenter:
  84. case actions.alignRight:
  85. case actions.alignFull:
  86. case actions.setSubscript:
  87. case actions.setSuperscript:
  88. case actions.setStrikethrough:
  89. case actions.setHR:
  90. case actions.setIndent:
  91. case actions.setOutdent:
  92. this.state.editor._sendAction(action);
  93. break;
  94. case actions.insertLink:
  95. case actions.insertImage:
  96. break;
  97. }
  98. }
  99. }