From 26be20407e9a27e5de32b906bebb4f632bdde1b2 Mon Sep 17 00:00:00 2001 From: iseulde Date: Mon, 28 Jan 2019 17:57:27 +0100 Subject: [PATCH] WIP: make shift+enter work in lists --- .../editor/src/components/rich-text/index.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/editor/src/components/rich-text/index.js b/packages/editor/src/components/rich-text/index.js index 72d86b84415566..e7c3259bdbaca9 100644 --- a/packages/editor/src/components/rich-text/index.js +++ b/packages/editor/src/components/rich-text/index.js @@ -567,7 +567,24 @@ export class RichText extends Component { } if ( this.multilineTag ) { - if ( this.onSplit && isEmptyLine( record ) ) { + if ( event.shiftKey ) { + const text = getTextContent( record ); + const end = getSelectionEnd( record ); + const length = text.length; + let toInsert = '\n'; + + // If the caret is at the end of the text, and there is no + // trailing line break or no text at all, we have to insert two + // line breaks in order to create a new line visually and place + // the caret there. + if ( ( end === length || charAt( record, end ) === LINE_SEPARATOR ) && ( + text.charAt( end - 1 ) !== '\n' || length === 0 + ) ) { + toInsert = '\n\n'; + } + + this.onChange( insert( record, toInsert ) ); + } else if ( this.onSplit && isEmptyLine( record ) ) { this.onSplit( ...split( record ).map( this.valueToFormat ) ); } else { this.onChange( insertLineSeparator( record ) );