gpt4 book ai didi

reactjs - React contenteditable光标跳转到开头

转载 作者:行者123 更新时间:2023-12-03 13:24:57 61 4
gpt4 key购买 nike

我正在使用模块react-simple-contenteditable启用填充空白工作表的编辑。我必须使用内容可编辑元素而不是输入元素的原因是因为我希望问题的文本换行。例如,如果问题有一个空白,它会将文本分为三部分:空白之前的部分、空白和之后的部分。如果我将外部两个表示为单独的 div s(或输入字段),那么文本就不会像段落一样换行。相反,我必须有一个 contenteditable div其中包含两侧空白和自由文本的输入字段。

文本按照我想要的方式换行,但是当我在可编辑字段中键入文本时,光标跳到开头。我不明白为什么,因为我尝试了 module's github site 上的示例它工作得很好,虽然我的实现有点复杂,但它的工作原理基本上是一样的。

这是我的渲染函数,它使用 <ContentEditable /> :

render() {
const textPieces =
<div className='new-form-text-pieces'>
{
this.props.problem.textPieces.map( (textPiece, idx) => {
if (textPiece.blank) {
return (
<div data-blank={true} className='blank' key={ textPiece.id } style={{display: 'inline'}}>
<input
placeholder="Answer blank"
className='new-form-answer-input'
value={ this.props.problem.textPieces[idx].text }
onChange={ (event) => this.props.handleTextPiecesInput(this.props.problemIdx, idx, event.target.value) }
/>
<button className='modify-blank remove-blank' onClick={ (event) => this.props.removeBlank(this.props.problemIdx, idx) }>-</button>

</div>
);
} else {
let text = this.props.problem.textPieces[idx].text;
const placeholder = idx === 0 ? 'Problem text' : '...continue text';
// text = text === '' ? placeholder : text;
if (text === '') {
text = <span style={{color:'gray'}}>{placeholder}</span>;
} else {

}
return (
this.props.isTextSplit ?
<TextPiece
key={ textPiece.id }
problemIdx={this.props.problemIdx}
textPieceIdx={idx}
dropBlank={this.props.dropBlank}
moveBlank={this.props.moveBlank}
>
<div style={{display: 'inline-block', }}>{text}</div>
</TextPiece>
: text

);
}
})
}
</div>;



return (
this.props.isTextSplit ? textPieces :
<ContentEditable
html={ReactDOMServer.renderToStaticMarkup(textPieces)}
className="my-class"
tagName="div"
onChange={ (event, value) => this.props.handleProblemChange(event, this.props.problemIdx, value) }
contentEditable='plaintext-only'
/>
);

}

这是 onChange功能:

handleProblemChange(event, problemIdx) {
const problems = cloneDeep(this.state.problems);
event.target.children[0].childNodes.forEach( (textPieceNode, idx) => {
if (textPieceNode.constructor === Text) {
problems[problemIdx].textPieces[idx].text = textPieceNode.wholeText;
} else {
problems[problemIdx].textPieces[idx].text = textPieceNode.childNodes[0].value;
}
});
this.setState({ problems });
}

这里是它所指的状态,只是为了澄清这一点:

this.state = {
problems: [
{
id: shortid.generate(),
textPieces: [
{
text : "Three days was simply not a(n)",
blank : false,
id: shortid.generate(),
},
{
text : "acceptable",
blank : true,
id: shortid.generate(),
},
{
text : "amount of time to complete such a lot of work.",
blank : false,
id: shortid.generate(),
}
]
}

非常感谢

最佳答案

长话短说,没有简单的方法可以做到这一点。我自己也尝试过,并花了几天时间尝试。基本上,您必须保存光标位置并在更新后自行重新定位。所有这些都可以通过 window.getSelection() 来实现

https://developer.mozilla.org/en-US/docs/Web/API/Window/getSelection

但这可能会变得非常棘手,具体取决于您的内容更改的程度。

我最终使用了draftJS。这是 facebook 自己对 contenteditable div 的抽象。

https://draftjs.org/docs/overview.html#content

学习时间稍长,但您将能够做更多事情

关于reactjs - React contenteditable光标跳转到开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47257519/

61 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com