使用DraftJS和Meteor应用程序任务的代码--做一个实时预览,其中来自DraftJS的文本将保存到DB,而从DB则显示在另一个组件上。
但问题是,一旦数据来自DB,而我试图编辑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);任何正确方向的有用的评论都是有用的。
发布于 2017-05-10 15:09:46
当您调用EditorState.createWithContent(c)时,草案将为您返回一个新的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
})发布于 2020-09-01 10:13:44
有一个属性可以将焦点移到末尾:
const newState = EditorState.createEmpty()
this.setState({
editorState:
EditorState.moveFocusToEnd(newState)
})这对我有用。
发布于 2021-11-02 23:24:23
您所需要做的就是在内置于静态EditorState方法的内部传递给定的EditorState.moveSelectionToEnd():
const editorState = EditorState.createEmpty();
const editorStateWithFocusOnTheEnd = EditorState.moveSelectionToEnd(editorState)https://stackoverflow.com/questions/43868815
复制相似问题