Skip to content

Commit

Permalink
onMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
stulip committed Dec 16, 2020
1 parent ce07a3a commit 89cca50
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@


## 1.4.2
## 1.5.0
- Add `onMessage` props on editor Callback outside postMessage internal type processing
- Added `_.sendEvent(type, data)` event callback to RN, using onMessage to receive callback

## 1.4.0
#### Added
- Add `pasteAsPlainText` props on editor
- Add `removeFormat` props on editor
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ The editor component. Simply place this component in your view hierarchy to rece
* `onHeightChange`
Callback after height change

* `onMessage`
Callback outside postMessage internal type processing
```
<img src="" ontouchstart="_.sendEvent('ImgClick')"/>
```
* `useContainer`
A boolean value that determines if a View container is wrapped around the WebView. The default value is true. If you are using your own View to wrap this library around, set this value to false.
Expand Down
19 changes: 17 additions & 2 deletions examples/src/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import React from 'react';
import {
Alert,
Appearance,
Button,
Keyboard,
Expand All @@ -23,14 +24,14 @@ import {InsertLinkModal} from './insertLink';
import {EmojiView} from './emoji';

const initHTML = `<br/>
<center><b>Rich Editor</b></center>
<center><b ontouchstart="_.sendEvent('TitleClick')">Rich Editor</b></center>
<center>
<a href="https://github.com/wxik/react-native-rich-editor">React Native</a>
<span>And</span>
<a href="https://github.com/wxik/flutter-rich-editor">Flutter</a>
</center>
<br/>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/120px-React-icon.svg.png" />
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/120px-React-icon.svg.png" ontouchstart="_.sendEvent('ImgClick')"/>
<br/><br/><br/><br/>
`;

Expand Down Expand Up @@ -203,6 +204,19 @@ class Example extends React.Component {
console.log('KeyDown:', data);
};

handleMessage = ({type, data}) => {
switch (type) {
case 'ImgClick':
Alert.alert('Click Image');
break;
case 'TitleClick':
Alert.alert('Click Title');
break;
default:
console.log('onMessage', type, data);
}
};

render() {
let that = this;
const {contentStyle, theme, emojiVisible, disabled} = that.state;
Expand Down Expand Up @@ -260,6 +274,7 @@ class Example extends React.Component {
onPaste={that.handlePaste}
onKeyUp={that.handleKeyUp}
onKeyDown={that.handleKeyDown}
onMessage={that.handleMessage}
pasteAsPlainText={true}
/>
</ScrollView>
Expand Down
3 changes: 3 additions & 0 deletions src/RichEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ export default class RichTextEditor extends Component {
case messages.OFFSET_HEIGHT:
this.setWebHeight(data);
break;
default:
this.props.onMessage && this.props.onMessage(message);
break;
}
} catch (e) {
//alert('NON JSON MESSAGE');
Expand Down
16 changes: 13 additions & 3 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function createHTML(options = {}) {
<div class="content"><div id="editor" class="pell"></div></div>
<script>
var __DEV__ = !!${window.__DEV__};
(function (exports) {
var _ = (function (exports) {
var placeholderColor = '${placeholderColor}';
var body = document.body, docEle = document.documentElement;
Expand Down Expand Up @@ -68,8 +68,11 @@ function createHTML(options = {}) {
return document.execCommand(command, false, value);
};
var _postMessage = function (data){
exports.window.postMessage(JSON.stringify(data));
}
var postAction = function(data){
(editor.content.contentEditable === 'true' || data.type === 'OFFSET_HEIGHT') && exports.window.postMessage(JSON.stringify(data));
editor.content.contentEditable === 'true' && _postMessage(data);
};
console.log = function (){
Expand Down Expand Up @@ -196,7 +199,7 @@ function createHTML(options = {}) {
UPDATE_HEIGHT: function() {
var height = Math.max(docEle.scrollHeight, body.scrollHeight);
if (o_height !== height){
postAction({type: 'OFFSET_HEIGHT', data: o_height = height});
_postMessage({type: 'OFFSET_HEIGHT', data: o_height = height});
}
}
};
Expand Down Expand Up @@ -324,6 +327,13 @@ function createHTML(options = {}) {
}, 10);
}
})
return {
sendEvent: function (type, data){
event.preventDefault();
event.stopPropagation();
_postMessage({type, data});
}
}
})({
window: window.ReactNativeWebView || window.parent,
});
Expand Down

0 comments on commit 89cca50

Please sign in to comment.