如何阻止 DraftJS 光标跳到文本开头?
How to stop DraftJS cursor jumping to beginning of text?
涉及使用DraftJS和Meteor Js应用程序的代码
任务 - 进行实时预览,其中来自 DraftJS 的文本将保存到数据库,然后从数据库显示在另一个组件上。
但问题是一旦数据来自数据库,我尝试编辑 DraftJS 光标移动到开头。
密码是
import {Editor, EditorState, ContentState} from 'draft-js';
import React, { Component } from 'react';
import { TestDB } from '../api/yaml-component.js';
import { createContainer } from 'meteor/react-meteor-data';
import PropTypes from 'prop-types';
class EditorComponent extends Component {
constructor(props) {
super(props);
this.state = {
editorState : EditorState.createEmpty(),
};
}
componentWillReceiveProps(nextProps) {
console.log('Receiving Props');
if (!nextProps) return;
console.log(nextProps);
let j = nextProps.testDB[0];
let c = ContentState.createFromText(j.text);
this.setState({
editorState: EditorState.createWithContent(c),
})
}
insertToDB(finalComponentStructure) {
if (!finalComponentStructure) return;
finalComponentStructure.author = 'Sandeep3005';
Meteor.call('testDB.insert', finalComponentStructure);
}
_handleChange(editorState) {
console.log('Inside handle change');
let contentState = editorState.getCurrentContent();
this.insertToDB({text: contentState.getPlainText()});
this.setState({editorState});
}
render() {
return (
<div>
<Editor
placeholder="Insert YAML Here"
editorState={this.state.editorState}
onChange={this._handleChange.bind(this)}
/>
</div>
);
}
}
EditorComponent.propTypes = {
staff: PropTypes.array.isRequired,
};
export default createContainer(() => {
return {
staff: Staff.find({}).fetch(),
};
}, EditorComponent);
任何方向正确的有用评论都会有用
当您调用 EditorState.createWithContent(c)
时,草稿将为您 return 一个新的 EditorState
,但它不知道您当前的 SelectionState
。相反,它只会在新 ContentState
的第一个块中创建一个新的空选择。
要克服这个问题,您必须自己设置 SelectionState
,使用当前状态下的 SelectionState
,例如:
const stateWithContent = EditorState.createWithContent(c)
const currentSelection = this.state.editorState.getSelection()
const stateWithContentAndSelection = EditorState.forceSelection(stateWithContent, currentSelection)
this.setState({
editorState: stateWithContentAndSelection
})
有个属性把焦点移到结尾:
const newState = EditorState.createEmpty()
this.setState({
editorState:
EditorState.moveFocusToEnd(newState)
})
这对我有用。
您需要做的就是将给定的 EditorState
传递到内置静态 EditorState.moveSelectionToEnd()
方法中:
const editorState = EditorState.createEmpty();
const editorStateWithFocusOnTheEnd = EditorState.moveSelectionToEnd(editorState)
涉及使用DraftJS和Meteor Js应用程序的代码 任务 - 进行实时预览,其中来自 DraftJS 的文本将保存到数据库,然后从数据库显示在另一个组件上。
但问题是一旦数据来自数据库,我尝试编辑 DraftJS 光标移动到开头。
密码是
import {Editor, EditorState, ContentState} from 'draft-js';
import React, { Component } from 'react';
import { TestDB } from '../api/yaml-component.js';
import { createContainer } from 'meteor/react-meteor-data';
import PropTypes from 'prop-types';
class EditorComponent extends Component {
constructor(props) {
super(props);
this.state = {
editorState : EditorState.createEmpty(),
};
}
componentWillReceiveProps(nextProps) {
console.log('Receiving Props');
if (!nextProps) return;
console.log(nextProps);
let j = nextProps.testDB[0];
let c = ContentState.createFromText(j.text);
this.setState({
editorState: EditorState.createWithContent(c),
})
}
insertToDB(finalComponentStructure) {
if (!finalComponentStructure) return;
finalComponentStructure.author = 'Sandeep3005';
Meteor.call('testDB.insert', finalComponentStructure);
}
_handleChange(editorState) {
console.log('Inside handle change');
let contentState = editorState.getCurrentContent();
this.insertToDB({text: contentState.getPlainText()});
this.setState({editorState});
}
render() {
return (
<div>
<Editor
placeholder="Insert YAML Here"
editorState={this.state.editorState}
onChange={this._handleChange.bind(this)}
/>
</div>
);
}
}
EditorComponent.propTypes = {
staff: PropTypes.array.isRequired,
};
export default createContainer(() => {
return {
staff: Staff.find({}).fetch(),
};
}, EditorComponent);
任何方向正确的有用评论都会有用
当您调用 EditorState.createWithContent(c)
时,草稿将为您 return 一个新的 EditorState
,但它不知道您当前的 SelectionState
。相反,它只会在新 ContentState
的第一个块中创建一个新的空选择。
要克服这个问题,您必须自己设置 SelectionState
,使用当前状态下的 SelectionState
,例如:
const stateWithContent = EditorState.createWithContent(c)
const currentSelection = this.state.editorState.getSelection()
const stateWithContentAndSelection = EditorState.forceSelection(stateWithContent, currentSelection)
this.setState({
editorState: stateWithContentAndSelection
})
有个属性把焦点移到结尾:
const newState = EditorState.createEmpty()
this.setState({
editorState:
EditorState.moveFocusToEnd(newState)
})
这对我有用。
您需要做的就是将给定的 EditorState
传递到内置静态 EditorState.moveSelectionToEnd()
方法中:
const editorState = EditorState.createEmpty();
const editorStateWithFocusOnTheEnd = EditorState.moveSelectionToEnd(editorState)