Unable to set cursor in Draft.js editor












3














I am trying to integrate the Draft.js editor in a project.
The way I am thinking of using it, is to create a new EditorState out of my own state on every render call (the reason for this approach are related to my specific context I am not going to detail here).



What I have not succeeded is to set the cursor position in the Editor.



I have created an example on Codepen:
http://codepen.io/nutrina/pen/JKaaOo?editors=0011



In this example any character I type is prepended to the beginning of the text, instead of being inserted at the cursor position.
I have tried setting the cursor by using:



  state = EditorState.acceptSelection(state, this.state.selectionState);
state = EditorState.forceSelection(state, this.state.selectionState);


but without much success.
Any help would be appreciated.



Thanks,
Gerald










share|improve this question
























  • I happened answer the same question:stackoverflow.com/questions/38749474/…
    – Jiang YD
    Aug 4 '16 at 7:09










  • @JiangYD: Thanks for the hint, I am a step further now. In my case however the keys remain unchanged. I am basically restoring the selection state of the editor on each render, but now the cursor advances 2 positions? I have updated the Codepen sample. Any ideas?
    – nutrina
    Aug 4 '16 at 14:52










  • I suppose that some key press triggers multiple 'onChange' events, but your code break the events by generating a new EditorState, then something happens weirdly. 'create a new EditorState every render call' will not work I think. Maybe you could set a EditorState in component state for selection change rendering, and use your 'new state' for content change rendering.
    – Jiang YD
    Aug 5 '16 at 2:57
















3














I am trying to integrate the Draft.js editor in a project.
The way I am thinking of using it, is to create a new EditorState out of my own state on every render call (the reason for this approach are related to my specific context I am not going to detail here).



What I have not succeeded is to set the cursor position in the Editor.



I have created an example on Codepen:
http://codepen.io/nutrina/pen/JKaaOo?editors=0011



In this example any character I type is prepended to the beginning of the text, instead of being inserted at the cursor position.
I have tried setting the cursor by using:



  state = EditorState.acceptSelection(state, this.state.selectionState);
state = EditorState.forceSelection(state, this.state.selectionState);


but without much success.
Any help would be appreciated.



Thanks,
Gerald










share|improve this question
























  • I happened answer the same question:stackoverflow.com/questions/38749474/…
    – Jiang YD
    Aug 4 '16 at 7:09










  • @JiangYD: Thanks for the hint, I am a step further now. In my case however the keys remain unchanged. I am basically restoring the selection state of the editor on each render, but now the cursor advances 2 positions? I have updated the Codepen sample. Any ideas?
    – nutrina
    Aug 4 '16 at 14:52










  • I suppose that some key press triggers multiple 'onChange' events, but your code break the events by generating a new EditorState, then something happens weirdly. 'create a new EditorState every render call' will not work I think. Maybe you could set a EditorState in component state for selection change rendering, and use your 'new state' for content change rendering.
    – Jiang YD
    Aug 5 '16 at 2:57














3












3








3







I am trying to integrate the Draft.js editor in a project.
The way I am thinking of using it, is to create a new EditorState out of my own state on every render call (the reason for this approach are related to my specific context I am not going to detail here).



What I have not succeeded is to set the cursor position in the Editor.



I have created an example on Codepen:
http://codepen.io/nutrina/pen/JKaaOo?editors=0011



In this example any character I type is prepended to the beginning of the text, instead of being inserted at the cursor position.
I have tried setting the cursor by using:



  state = EditorState.acceptSelection(state, this.state.selectionState);
state = EditorState.forceSelection(state, this.state.selectionState);


but without much success.
Any help would be appreciated.



Thanks,
Gerald










share|improve this question















I am trying to integrate the Draft.js editor in a project.
The way I am thinking of using it, is to create a new EditorState out of my own state on every render call (the reason for this approach are related to my specific context I am not going to detail here).



What I have not succeeded is to set the cursor position in the Editor.



I have created an example on Codepen:
http://codepen.io/nutrina/pen/JKaaOo?editors=0011



In this example any character I type is prepended to the beginning of the text, instead of being inserted at the cursor position.
I have tried setting the cursor by using:



  state = EditorState.acceptSelection(state, this.state.selectionState);
state = EditorState.forceSelection(state, this.state.selectionState);


but without much success.
Any help would be appreciated.



Thanks,
Gerald







draftjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 4 '16 at 5:33







nutrina

















asked Aug 3 '16 at 15:37









nutrinanutrina

6171622




6171622












  • I happened answer the same question:stackoverflow.com/questions/38749474/…
    – Jiang YD
    Aug 4 '16 at 7:09










  • @JiangYD: Thanks for the hint, I am a step further now. In my case however the keys remain unchanged. I am basically restoring the selection state of the editor on each render, but now the cursor advances 2 positions? I have updated the Codepen sample. Any ideas?
    – nutrina
    Aug 4 '16 at 14:52










  • I suppose that some key press triggers multiple 'onChange' events, but your code break the events by generating a new EditorState, then something happens weirdly. 'create a new EditorState every render call' will not work I think. Maybe you could set a EditorState in component state for selection change rendering, and use your 'new state' for content change rendering.
    – Jiang YD
    Aug 5 '16 at 2:57


















  • I happened answer the same question:stackoverflow.com/questions/38749474/…
    – Jiang YD
    Aug 4 '16 at 7:09










  • @JiangYD: Thanks for the hint, I am a step further now. In my case however the keys remain unchanged. I am basically restoring the selection state of the editor on each render, but now the cursor advances 2 positions? I have updated the Codepen sample. Any ideas?
    – nutrina
    Aug 4 '16 at 14:52










  • I suppose that some key press triggers multiple 'onChange' events, but your code break the events by generating a new EditorState, then something happens weirdly. 'create a new EditorState every render call' will not work I think. Maybe you could set a EditorState in component state for selection change rendering, and use your 'new state' for content change rendering.
    – Jiang YD
    Aug 5 '16 at 2:57
















I happened answer the same question:stackoverflow.com/questions/38749474/…
– Jiang YD
Aug 4 '16 at 7:09




I happened answer the same question:stackoverflow.com/questions/38749474/…
– Jiang YD
Aug 4 '16 at 7:09












@JiangYD: Thanks for the hint, I am a step further now. In my case however the keys remain unchanged. I am basically restoring the selection state of the editor on each render, but now the cursor advances 2 positions? I have updated the Codepen sample. Any ideas?
– nutrina
Aug 4 '16 at 14:52




@JiangYD: Thanks for the hint, I am a step further now. In my case however the keys remain unchanged. I am basically restoring the selection state of the editor on each render, but now the cursor advances 2 positions? I have updated the Codepen sample. Any ideas?
– nutrina
Aug 4 '16 at 14:52












I suppose that some key press triggers multiple 'onChange' events, but your code break the events by generating a new EditorState, then something happens weirdly. 'create a new EditorState every render call' will not work I think. Maybe you could set a EditorState in component state for selection change rendering, and use your 'new state' for content change rendering.
– Jiang YD
Aug 5 '16 at 2:57




I suppose that some key press triggers multiple 'onChange' events, but your code break the events by generating a new EditorState, then something happens weirdly. 'create a new EditorState every render call' will not work I think. Maybe you could set a EditorState in component state for selection change rendering, and use your 'new state' for content change rendering.
– Jiang YD
Aug 5 '16 at 2:57












1 Answer
1






active

oldest

votes


















0














A easy way to move the cursor around is to use Editor.forceSelection and a key binding function!



This is what your render function would look like once you have it set up



render() {
return (
<Editor
editorState={this.state.editorState}
onChange={this.onChange}
handleKeyCommand={this.handleKeyCommand}
keyBindingFn={this.myKeyBindingFn}
/>
);
}


Once you have your keybinding function, you can do something along the lines of



  myKeyBindingFn = (e) => {
// on spacebar
if (e.keyCode == 32) {
const newSelection = selectionState.merge({
anchorOffset: selectionState.getAnchorOffset() + 1,
focusOffset: selectionState.getAnchorOffset() + 1,
});

const newEditorState = EditorState.forceSelection(
editorState,
newSelection,
);

this.setState({ editorState: newEditorState });

return 'space-press';
}
};


Feel free to replace anchorOffset and focusOffset with the position you would like the cursor to be in. Using a keybinding function allows better control over events



Your handleKeyCommand function would look something like this



handleKeyCommand = (command: string): DraftHandleValue => {
if (command === 'space-press') {
return 'handled';
}
return 'not-handled';
};





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38748053%2funable-to-set-cursor-in-draft-js-editor%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    A easy way to move the cursor around is to use Editor.forceSelection and a key binding function!



    This is what your render function would look like once you have it set up



    render() {
    return (
    <Editor
    editorState={this.state.editorState}
    onChange={this.onChange}
    handleKeyCommand={this.handleKeyCommand}
    keyBindingFn={this.myKeyBindingFn}
    />
    );
    }


    Once you have your keybinding function, you can do something along the lines of



      myKeyBindingFn = (e) => {
    // on spacebar
    if (e.keyCode == 32) {
    const newSelection = selectionState.merge({
    anchorOffset: selectionState.getAnchorOffset() + 1,
    focusOffset: selectionState.getAnchorOffset() + 1,
    });

    const newEditorState = EditorState.forceSelection(
    editorState,
    newSelection,
    );

    this.setState({ editorState: newEditorState });

    return 'space-press';
    }
    };


    Feel free to replace anchorOffset and focusOffset with the position you would like the cursor to be in. Using a keybinding function allows better control over events



    Your handleKeyCommand function would look something like this



    handleKeyCommand = (command: string): DraftHandleValue => {
    if (command === 'space-press') {
    return 'handled';
    }
    return 'not-handled';
    };





    share|improve this answer




























      0














      A easy way to move the cursor around is to use Editor.forceSelection and a key binding function!



      This is what your render function would look like once you have it set up



      render() {
      return (
      <Editor
      editorState={this.state.editorState}
      onChange={this.onChange}
      handleKeyCommand={this.handleKeyCommand}
      keyBindingFn={this.myKeyBindingFn}
      />
      );
      }


      Once you have your keybinding function, you can do something along the lines of



        myKeyBindingFn = (e) => {
      // on spacebar
      if (e.keyCode == 32) {
      const newSelection = selectionState.merge({
      anchorOffset: selectionState.getAnchorOffset() + 1,
      focusOffset: selectionState.getAnchorOffset() + 1,
      });

      const newEditorState = EditorState.forceSelection(
      editorState,
      newSelection,
      );

      this.setState({ editorState: newEditorState });

      return 'space-press';
      }
      };


      Feel free to replace anchorOffset and focusOffset with the position you would like the cursor to be in. Using a keybinding function allows better control over events



      Your handleKeyCommand function would look something like this



      handleKeyCommand = (command: string): DraftHandleValue => {
      if (command === 'space-press') {
      return 'handled';
      }
      return 'not-handled';
      };





      share|improve this answer


























        0












        0








        0






        A easy way to move the cursor around is to use Editor.forceSelection and a key binding function!



        This is what your render function would look like once you have it set up



        render() {
        return (
        <Editor
        editorState={this.state.editorState}
        onChange={this.onChange}
        handleKeyCommand={this.handleKeyCommand}
        keyBindingFn={this.myKeyBindingFn}
        />
        );
        }


        Once you have your keybinding function, you can do something along the lines of



          myKeyBindingFn = (e) => {
        // on spacebar
        if (e.keyCode == 32) {
        const newSelection = selectionState.merge({
        anchorOffset: selectionState.getAnchorOffset() + 1,
        focusOffset: selectionState.getAnchorOffset() + 1,
        });

        const newEditorState = EditorState.forceSelection(
        editorState,
        newSelection,
        );

        this.setState({ editorState: newEditorState });

        return 'space-press';
        }
        };


        Feel free to replace anchorOffset and focusOffset with the position you would like the cursor to be in. Using a keybinding function allows better control over events



        Your handleKeyCommand function would look something like this



        handleKeyCommand = (command: string): DraftHandleValue => {
        if (command === 'space-press') {
        return 'handled';
        }
        return 'not-handled';
        };





        share|improve this answer














        A easy way to move the cursor around is to use Editor.forceSelection and a key binding function!



        This is what your render function would look like once you have it set up



        render() {
        return (
        <Editor
        editorState={this.state.editorState}
        onChange={this.onChange}
        handleKeyCommand={this.handleKeyCommand}
        keyBindingFn={this.myKeyBindingFn}
        />
        );
        }


        Once you have your keybinding function, you can do something along the lines of



          myKeyBindingFn = (e) => {
        // on spacebar
        if (e.keyCode == 32) {
        const newSelection = selectionState.merge({
        anchorOffset: selectionState.getAnchorOffset() + 1,
        focusOffset: selectionState.getAnchorOffset() + 1,
        });

        const newEditorState = EditorState.forceSelection(
        editorState,
        newSelection,
        );

        this.setState({ editorState: newEditorState });

        return 'space-press';
        }
        };


        Feel free to replace anchorOffset and focusOffset with the position you would like the cursor to be in. Using a keybinding function allows better control over events



        Your handleKeyCommand function would look something like this



        handleKeyCommand = (command: string): DraftHandleValue => {
        if (command === 'space-press') {
        return 'handled';
        }
        return 'not-handled';
        };






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 '18 at 0:06

























        answered Nov 19 '18 at 18:29









        Dummy accountDummy account

        12




        12






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38748053%2funable-to-set-cursor-in-draft-js-editor%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith