|
6 | 6 | * @flow |
7 | 7 | */ |
8 | 8 |
|
9 | | -import React, { Fragment } from "react"; |
| 9 | +import React, { useState } from "react"; |
10 | 10 | import { |
11 | 11 | SafeAreaView, |
12 | 12 | StyleSheet, |
13 | | - ScrollView, |
| 13 | + TouchableOpacity, |
14 | 14 | View, |
15 | | - Text, |
16 | | - StatusBar |
| 15 | + Text |
17 | 16 | } from "react-native"; |
18 | 17 | import KeyboardSpacer from "react-native-keyboard-spacer"; |
19 | 18 | import RNDraftView from "react-native-draftjs"; |
20 | 19 |
|
| 20 | +const ControlButton = ({ text, action, isActive }) => { |
| 21 | + return ( |
| 22 | + <TouchableOpacity |
| 23 | + style={[ |
| 24 | + styles.controlButtonContainer, |
| 25 | + isActive ? { backgroundColor: "gold" } : {} |
| 26 | + ]} |
| 27 | + onPress={action} |
| 28 | + > |
| 29 | + <Text>{text}</Text> |
| 30 | + </TouchableOpacity> |
| 31 | + ); |
| 32 | +}; |
| 33 | + |
| 34 | +const EditorToolBar = ({ |
| 35 | + activeStyles, |
| 36 | + blockType, |
| 37 | + toggleStyle, |
| 38 | + toggleBlockType |
| 39 | +}) => { |
| 40 | + return ( |
| 41 | + <View style={styles.toolbarContainer}> |
| 42 | + <ControlButton |
| 43 | + text={"B"} |
| 44 | + isActive={activeStyles.includes("BOLD")} |
| 45 | + action={() => toggleStyle("BOLD")} |
| 46 | + /> |
| 47 | + <ControlButton |
| 48 | + text={"I"} |
| 49 | + isActive={activeStyles.includes("ITALIC")} |
| 50 | + action={() => toggleStyle("ITALIC")} |
| 51 | + /> |
| 52 | + <ControlButton |
| 53 | + text={"H"} |
| 54 | + isActive={blockType === "header-one"} |
| 55 | + action={() => toggleBlockType("header-one")} |
| 56 | + /> |
| 57 | + <ControlButton |
| 58 | + text={"ul"} |
| 59 | + isActive={blockType === "unordered-list-item"} |
| 60 | + action={() => toggleBlockType("unordered-list-item")} |
| 61 | + /> |
| 62 | + <ControlButton |
| 63 | + text={"ol"} |
| 64 | + isActive={blockType === "ordered-list-item"} |
| 65 | + action={() => toggleBlockType("ordered-list-item")} |
| 66 | + /> |
| 67 | + </View> |
| 68 | + ); |
| 69 | +}; |
| 70 | + |
21 | 71 | const App = () => { |
| 72 | + const _draftRef = React.createRef(); |
| 73 | + const [activeStyles, setActiveStyles] = useState([]); |
| 74 | + const [blockType, setActiveBlockType] = useState("unstyled"); |
| 75 | + |
| 76 | + const editorLoaded = () => { |
| 77 | + _draftRef.current && _draftRef.current.focus(); |
| 78 | + }; |
| 79 | + |
| 80 | + const toggleStyle = style => { |
| 81 | + _draftRef.current && _draftRef.current.setStyle(style); |
| 82 | + }; |
| 83 | + |
| 84 | + const toggleBlockType = blockType => { |
| 85 | + _draftRef.current && _draftRef.current.setBlockType(blockType); |
| 86 | + }; |
| 87 | + |
22 | 88 | return ( |
23 | | - <Fragment> |
24 | | - <View style={styles.containerStyle}> |
25 | | - <RNDraftView /> |
26 | | - <KeyboardSpacer /> |
27 | | - </View> |
28 | | - </Fragment> |
| 89 | + <SafeAreaView style={styles.containerStyle}> |
| 90 | + <RNDraftView |
| 91 | + onEditorReady={editorLoaded} |
| 92 | + style={{ flex: 1 }} |
| 93 | + placeholder={"Some placeholder Text..."} |
| 94 | + ref={_draftRef} |
| 95 | + onStyleChanged={setActiveStyles} |
| 96 | + onBlockTypeChanged={setActiveBlockType} |
| 97 | + /> |
| 98 | + <EditorToolBar |
| 99 | + activeStyles={activeStyles} |
| 100 | + blockType={blockType} |
| 101 | + toggleStyle={toggleStyle} |
| 102 | + toggleBlockType={toggleBlockType} |
| 103 | + /> |
| 104 | + <KeyboardSpacer /> |
| 105 | + </SafeAreaView> |
29 | 106 | ); |
30 | 107 | }; |
31 | 108 |
|
32 | 109 | const styles = StyleSheet.create({ |
33 | 110 | containerStyle: { |
34 | 111 | flex: 1, |
35 | 112 | marginTop: 36 |
| 113 | + }, |
| 114 | + toolbarContainer: { |
| 115 | + height: 56, |
| 116 | + flexDirection: "row", |
| 117 | + backgroundColor: "silver", |
| 118 | + alignItems: "center", |
| 119 | + justifyContent: "space-around" |
| 120 | + }, |
| 121 | + controlButtonContainer: { |
| 122 | + padding: 8, |
| 123 | + borderRadius: 2 |
36 | 124 | } |
37 | 125 | }); |
38 | 126 |
|
|
0 commit comments