Skip to content

Commit

Permalink
Merge pull request #240 from kosbog/onlink-handler
Browse files Browse the repository at this point in the history
Link click handler
  • Loading branch information
stulip authored Apr 20, 2023
2 parents 142777d + 55fde20 commit 400b30b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ The editor component. Simply place this component in your view hierarchy to rece
* `onInput`
Callback input value

* `onLink`
Callback link click

* `onFocus`
Callback editor focus

Expand Down
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ export interface RichEditorProps extends WebViewProps {
*/
onInput?: ({data: string, inputType: string}) => void;

/**
* Callback when the link clicked
*/
onLink?: (url: string) => void;

/**
* Callback when the editor focus some content
*/
Expand Down
17 changes: 14 additions & 3 deletions src/RichEditor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {Component} from 'react';
import {WebView} from 'react-native-webview';
import {actions, messages} from './const';
import {Keyboard, Platform, StyleSheet, TextInput, View} from 'react-native';
import {Keyboard, Platform, StyleSheet, TextInput, View, Linking} from 'react-native';
import {createHTML} from './editor';

const PlatformIOS = Platform.OS === 'ios';
Expand Down Expand Up @@ -140,7 +140,7 @@ export default class RichTextEditor extends Component {

onMessage(event) {
const that = this;
const {onFocus, onBlur, onChange, onPaste, onKeyUp, onKeyDown, onInput, onMessage, onCursorPosition} = that.props;
const {onFocus, onBlur, onChange, onPaste, onKeyUp, onKeyDown, onInput, onMessage, onCursorPosition, onLink} = that.props;
try {
const message = JSON.parse(event.nativeEvent.data);
const data = message.data;
Expand All @@ -156,6 +156,9 @@ export default class RichTextEditor extends Component {
}
}
break;
case messages.LINK_TOUCHED:
onLink?.(data);
break;
case messages.LOG:
console.log('FROM EDIT:', ...data);
break;
Expand Down Expand Up @@ -249,7 +252,7 @@ export default class RichTextEditor extends Component {

renderWebView() {
let that = this;
const {html, editorStyle, useContainer, style, ...rest} = that.props;
const {html, editorStyle, useContainer, style, onLink, ...rest} = that.props;
const {html: viewHTML} = that.state;
return (
<>
Expand All @@ -270,6 +273,14 @@ export default class RichTextEditor extends Component {
javaScriptEnabled={true}
source={viewHTML}
onLoad={that.init}
onShouldStartLoadWithRequest={event => {
if (event.url !== 'about:blank') {
this.webviewBridge?.stopLoading();
Linking?.openURL(event.url);
return false;
}
return true;
}}
/>
{Platform.OS === 'android' && <TextInput ref={ref => (that._input = ref)} style={styles._input} />}
</>
Expand Down
3 changes: 3 additions & 0 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,9 @@ function createHTML(options = {}) {
if (ele.checked) ele.setAttribute('checked', '');
else ele.removeAttribute('checked');
}
if (ele.nodeName === 'A' && ele.getAttribute('href')) {
postAction({type: 'LINK_TOUCHED', data: ele.getAttribute('href')});
}
}
addEventListener(content, 'touchcancel', handleSelecting);
addEventListener(content, 'mouseup', handleSelecting);
Expand Down

0 comments on commit 400b30b

Please sign in to comment.