jpuri / draftjs-utils

An collection of useful utility functions for DraftJS.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How do you use the handleNewLine utility

martinbroos opened this issue · comments

I have the following code

 _handleReturn(event) {
        // Find out if user did a hard or soft newline
        var editorState = handleNewLine(this.state.editorState, event);
        if (editorState) {
            this._onChange(editorState);
            return true;
        }
        return false;
    }

It does log a soft newline when using shift enter, but it still generates a new unstyled block instead of a /n

Any examples how i should handle this? I'm using DrafJS 0.10.1

@martinbroos your code snippet looks exactly like mine (https://github.com/springload/draftail/blob/master/lib/components/DraftailEditor.js#L238), which works as expected. Are you sure there is no other code overriding the change before the export?

NB: can be tested here - https://www.draftail.org/

Thnx for your response, It must have something to do with my onChange event. For now I did my own implementation without draftJS-util
Maybe someone can use it:

    _handleReturn({ shiftKey }) {
        var newEditorState = null,
            currentEditorState = this.state.editorState;

        if (shiftKey) {
            newEditorState = RichUtils.insertSoftNewline(currentEditorState);
        } else {
            var newContent = Modifier.splitBlock(
                currentEditorState.getCurrentContent(),
                currentEditorState.getSelection()
            );

            newEditorState = EditorState.push(currentEditorState, newContent, 'split-block');
        }

        // updates the editorState
        this._setNewEditorState(newEditorState);

        return 'handled';
    }

Issue can be closed as it's properly due to the way i handled the change event. And the above solution works for me.