Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.
Closed
27 changes: 24 additions & 3 deletions draftjs-html-source/draftjs-source.html

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions draftjs-web/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ function App() {
_draftEditorRef.current && _draftEditorRef.current.blur();
};

const getSelection = () => {
_draftEditorRef.current;
return _draftEditorRef.current.getSelection();
};

const setEditorBlockRenderMap = renderMapString => {
try {
setBlockRenderMap(Map(JSON.parse(renderMapString)));
Expand All @@ -125,6 +130,8 @@ function App() {
window.focusTextEditor = focusTextEditor;
window.blurTextEditor = blurTextEditor;
window.setEditorBlockRenderMap = setEditorBlockRenderMap;
window.toggleLink = toggleLink;
window.getSelection = getSelection;

if (window.ReactNativeWebView) {
window.ReactNativeWebView.postMessage(
Expand Down
26 changes: 24 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class RNDraftView extends Component {
styleSheet: PropTypes.string,
styleMap: PropTypes.object,
blockRenderMap: PropTypes.object,
onEditorReady: PropTypes.func
onEditorReady: PropTypes.func,
targetSelection: PropTypes.string,
entitykey: PropTypes.string
};

_webViewRef = React.createRef();
Expand All @@ -31,6 +33,15 @@ class RNDraftView extends Component {
);
};

executeScriptTwo = (functionName, parameter, parameterTwo) => {
this._webViewRef.current &&
this._webViewRef.current.injectJavaScript(
`window.${functionName}(${
parameter && parameterTwo ? `'${parameter}', '${parameterTwo}'` : ""
});true;`
);
};

setBlockType = blockType => {
this.executeScript("toggleBlockType", blockType);
};
Expand All @@ -43,6 +54,10 @@ class RNDraftView extends Component {
return this.state.editorState;
};

setLink = () => {
this.executeScriptTwo("toggleLink", targetSelection, entityKey);
};

_onMessage = event => {
const {
onStyleChanged = () => null,
Expand Down Expand Up @@ -103,8 +118,13 @@ class RNDraftView extends Component {
this.executeScript("blurTextEditor");
};

getSelection = () => {
return this.executeScript("getSelection");
};

render() {
const { style = { flex: 1 } } = this.props;

return (
<WebView
ref={this._webViewRef}
Expand All @@ -115,12 +135,14 @@ class RNDraftView extends Component {
: { uri: "file:///android_asset/draftjs-source.html" }
}
useWebKit={true}
keyboardDisplayRequiresUserAction={false}
keyboardDisplayRequiresUserAction={true}
originWhitelist={["*"]}
onMessage={this._onMessage}
hideKeyboardAccessoryView={true}
/>
);
}
git;
}

export default RNDraftView;