首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何阻止DraftJS光标跳到文本的开头?

如何阻止DraftJS光标跳到文本的开头?
EN

Stack Overflow用户
提问于 2017-05-09 11:38:50
回答 3查看 5.4K关注 0票数 4

使用DraftJS和Meteor应用程序任务的代码--做一个实时预览,其中来自DraftJS的文本将保存到DB,而从DB则显示在另一个组件上。

但问题是,一旦数据来自DB,而我试图编辑DraftJS游标,移到开头。

代码是

代码语言:javascript
复制
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);

任何正确方向的有用的评论都是有用的。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-05-10 15:09:46

当您调用EditorState.createWithContent(c)时,草案将为您返回一个新的EditorState,但它不知道您当前的SelectionState。相反,它只会在新ContentState的第一个块中创建一个新的空选择。

要克服这个问题,您必须自己设置SelectionState,使用当前状态的SelectionState,例如:

代码语言:javascript
复制
const stateWithContent = EditorState.createWithContent(c)
const currentSelection = this.state.editorState.getSelection()
const stateWithContentAndSelection = EditorState.forceSelection(stateWithContent, currentSelection)

this.setState({
  editorState: stateWithContentAndSelection
})
票数 8
EN

Stack Overflow用户

发布于 2020-09-01 10:13:44

有一个属性可以将焦点移到末尾:

代码语言:javascript
复制
const newState = EditorState.createEmpty()
this.setState({
 editorState:
  EditorState.moveFocusToEnd(newState)
 })

这对我有用。

票数 0
EN

Stack Overflow用户

发布于 2021-11-02 23:24:23

您所需要做的就是在内置于静态EditorState方法的内部传递给定的EditorState.moveSelectionToEnd()

代码语言:javascript
复制
const editorState = EditorState.createEmpty();
const editorStateWithFocusOnTheEnd = EditorState.moveSelectionToEnd(editorState)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43868815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档