admin管理员组文章数量:1402146
I have been learning to make a custom markdown editor. However I just ran into the problem of getting the highlighted text before wrapping it with the markdowns. Here's my example
class TextArea extends React.Component {
constructor(props){
super(props);
this.state = {
ment:'He oppose at thrown desire of no. Announcing impression unaffected day his are unreserved indulgence.She oppose at thrown desire of no..'
};
this.onClickMakeBold = this.onClickMakeBold.bind(this);
}
render(){
return <div>
<button onClick={this.onClickMakeBold}>Bold</button>
<div>
<textarea ref="textarea">{this.statement}</textarea>
</div>
</div>
}
onClickMakeBold(){
console.log(getSelectionText())
}
}
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control"){
text = document.selection.createRange().text;
}else{
alert('no')
}
return text;
}
React.render(<TextArea />, document.getElementById('container'));
The result from getSelectionText()
function doesn't return any text at all. How can I get the highlighted text in ReactJS?
I have been learning to make a custom markdown editor. However I just ran into the problem of getting the highlighted text before wrapping it with the markdowns. Here's my example
class TextArea extends React.Component {
constructor(props){
super(props);
this.state = {
ment:'He oppose at thrown desire of no. Announcing impression unaffected day his are unreserved indulgence.She oppose at thrown desire of no..'
};
this.onClickMakeBold = this.onClickMakeBold.bind(this);
}
render(){
return <div>
<button onClick={this.onClickMakeBold}>Bold</button>
<div>
<textarea ref="textarea">{this.state.ment}</textarea>
</div>
</div>
}
onClickMakeBold(){
console.log(getSelectionText())
}
}
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control"){
text = document.selection.createRange().text;
}else{
alert('no')
}
return text;
}
React.render(<TextArea />, document.getElementById('container'));
The result from getSelectionText()
function doesn't return any text at all. How can I get the highlighted text in ReactJS?
- You should try DraftJS, super cool editor from Facebook. – vijayst Commented Aug 7, 2016 at 5:47
2 Answers
Reset to default 4By clicking on the button, you deselect the text before the event is triggered.
Pass the callback to your button as onMouseDown
, as this event happens before the standard click side effects take place.
Also: if you want the text to remain selected after the event has been processed, use event.preventDefault()
in your callback function.
here is solution with react hook pure without any library!
to clone and the small package or any question you can check: https://github./Farbod29/React-text-highlighter-with-hook-
import React, { useState, useEffect } from 'react';
import './highlight.css';
export default function Highlighter() {
const [highlightedText, setHighlightedText] = useState(
'highlighted text will be shown here!'
);
useEffect(() => {
const saveSelection = () => {
setHighlightedText(window.getSelection().toString());
};
document.addEventListener('mouseup', saveSelection);
return () => document.removeEventListener('mouseup', saveSelection);
}, []);
return (
<>
<section className="card">
<div>
<div className="margin-top-zero txt">
Here is react text highlighter with hook and use state/Effect,
understand if you have any question just email or add issue in above git ripo!
</div>
{/* {ghasem} */}
<div className="txt">{highlightedText}</div>
<div // parent of button
>
<button className="btn"> Add Highlighted text </button>
</div>
</div>
</section>
</>
);
}
本文标签: javascriptHow can you get highlighted text (windowgetSelection()) in ReactJSStack Overflow
版权声明:本文标题:javascript - How can you get highlighted text (window.getSelection()) in ReactJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744306663a2599827.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论