Skip to content

Commit

Permalink
Manipulate the DOM in the editor
Browse files Browse the repository at this point in the history
  • Loading branch information
stulip committed Dec 16, 2020
1 parent 89cca50 commit 70fd09d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## 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
- Add `_.sendEvent(type, data)` event callback to RN, using onMessage to receive callback
- Add `commandDOM` method can manipulate DOM

## 1.4.0
#### Added
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ The editor component. Simply place this component in your view hierarchy to rece
<img src="" ontouchstart="_.sendEvent('ImgClick')"/>
```
* `commandDOM`
Manipulate the DOM in the editor
```
// $ = document.querySelector
this.richText.current?.commandDOM(`$('#title').style.color='${color}'`);
```
* `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
9 changes: 7 additions & 2 deletions examples/src/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {InsertLinkModal} from './insertLink';
import {EmojiView} from './emoji';

const initHTML = `<br/>
<center><b ontouchstart="_.sendEvent('TitleClick')">Rich Editor</b></center>
<center><b ontouchstart="_.sendEvent('TitleClick')" id="title">Rich Editor</b></center>
<center>
<a href="https://github.com/wxik/react-native-rich-editor">React Native</a>
<span>And</span>
Expand Down Expand Up @@ -207,7 +207,12 @@ class Example extends React.Component {
handleMessage = ({type, data}) => {
switch (type) {
case 'ImgClick':
Alert.alert('Click Image');
const index = this._tempIndex || 0;
const color = ['red', 'blue', 'gray', 'yellow', 'coral'][index];
this._tempIndex = index + 1 >= color.length ? 0 : index + 1;

// command: $ = document.querySelector
this.richText.current?.commandDOM(`$('#title').style.color='${color}'`);
break;
case 'TitleClick':
Alert.alert('Click Title');
Expand Down
6 changes: 6 additions & 0 deletions src/RichEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ export default class RichTextEditor extends Component {
}
}

commandDOM(command) {
if (command) {
this._sendAction(actions.content, 'commandDOM', command);
}
}

init() {
let that = this;
const {initialFocus, initialContentHTML, placeholder, editorInitializedCallback, disabled} = that.props;
Expand Down
5 changes: 5 additions & 0 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ function createHTML(options = {}) {
console.log("set placeholderColor error!")
}
}
},
commandDOM: function (command){
try {new Function("$", command)(exports.document.querySelector.bind(exports.document))} catch(e){console.error(e)};
}
},
Expand Down Expand Up @@ -336,6 +340,7 @@ function createHTML(options = {}) {
}
})({
window: window.ReactNativeWebView || window.parent,
document: document
});
</script>
</body>
Expand Down

0 comments on commit 70fd09d

Please sign in to comment.