This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.js
126 lines (112 loc) · 3.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import React, { Component } from "react";
import { ViewPropTypes, Platform } from "react-native";
import WebView from "react-native-webview";
import PropTypes from "prop-types";
const draftJsHtml = require("./draftjs-html-source/draftjs-source.html");
class RNDraftView extends Component {
static propTypes = {
style: ViewPropTypes.style,
onStyleChanged: PropTypes.func,
onBlockTypeChanged: PropTypes.func,
defaultValue: PropTypes.string,
placeholder: PropTypes.string,
styleSheet: PropTypes.string,
styleMap: PropTypes.object,
blockRenderMap: PropTypes.object,
onEditorReady: PropTypes.func
};
_webViewRef = React.createRef();
state = {
editorState: ""
};
executeScript = (functionName, parameter) => {
this._webViewRef.current &&
this._webViewRef.current.injectJavaScript(
`window.${functionName}(${parameter ? `'${parameter}'` : ""});true;`
);
};
setBlockType = blockType => {
this.executeScript("toggleBlockType", blockType);
};
setStyle = style => {
this.executeScript("toggleInlineStyle", style);
};
getEditorState = () => {
return this.state.editorState;
};
_onMessage = event => {
const {
onStyleChanged = () => null,
onBlockTypeChanged = () => null
} = this.props;
const { data } = event.nativeEvent;
const { blockType, styles, editorState, isMounted } = JSON.parse(data);
onStyleChanged(styles ? styles.split(",") : []);
if (blockType) onBlockTypeChanged(blockType);
if (editorState)
this.setState({ editorState: editorState.replace(/(\r\n|\n|\r)/gm, "") });
if (isMounted) this.widgetMounted();
};
widgetMounted = () => {
const {
placeholder,
defaultValue,
styleSheet,
styleMap,
blockRenderMap,
onEditorReady = () => null
} = this.props;
if (defaultValue) {
this.executeScript("setDefaultValue", defaultValue);
}
if (placeholder) {
this.executeScript("setEditorPlaceholder", placeholder);
}
if (styleSheet) {
this.executeScript("setEditorStyleSheet", styleSheet);
}
if (styleMap) {
try {
this.executeScript("setEditorStyleMap", JSON.stringify(styleMap));
} catch (e) {
console.error(e);
}
}
if (blockRenderMap) {
try {
this.executeScript(
"setEditorBlockRenderMap",
JSON.stringify(blockRenderMap)
);
} catch (e) {
console.error(e);
}
}
onEditorReady();
};
focus = () => {
this.executeScript("focusTextEditor");
};
blur = () => {
this.executeScript("blurTextEditor");
};
render() {
const { style = { flex: 1 } } = this.props;
return (
<WebView
ref={this._webViewRef}
style={style}
source={
Platform.OS === "ios"
? draftJsHtml
: { uri: "file:///android_asset/draftjs-source.html" }
}
useWebKit={true}
keyboardDisplayRequiresUserAction={false}
originWhitelist={["*"]}
onMessage={this._onMessage}
/>
);
}
}
export default RNDraftView;