From 688776bc10d43a1676921217126daa0e988c55c4 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 17 Jul 2018 13:29:40 +0100 Subject: [PATCH] allow enter to remove current block if its empty useful for lists Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .../views/rooms/MessageComposerInput.js | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 2fae59a1fe..841fcccf06 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -992,24 +992,25 @@ export default class MessageComposerInput extends React.Component { const editorState = this.state.editorState; - let inBlock = false; - let inEmptyBlock = false; - - for (const block of editorState.blocks) { - if (['code', 'block-quote', 'list-item'].includes(block.type)) { - inBlock = true; - if (block.text === '') { - inEmptyBlock = true; - } - break; + const lastBlock = editorState.blocks.last(); + if (['code', 'block-quote', 'list-item'].includes(lastBlock.type)) { + const text = lastBlock.text; + if (text === '') { + // allow the user to cancel empty block by hitting return, useful in conjunction with below `inBlock` + return change + .setBlocks(DEFAULT_NODE) + .unwrapBlock('bulleted-list') + .unwrapBlock('numbered-list'); } - } - if (inEmptyBlock) { - // allow the user to cancel empty block by hitting return, useful in conjunction with below `inBlock` - return change.setBlocks(DEFAULT_NODE); - } else if (inBlock) { - // allow the user to terminate blocks by hitting return rather than sending a msg + // TODO strip trailing lines from blockquotes/list entries + // the below code seemingly works but doesn't account for edge cases like return with caret not at end + /* const trailingNewlines = text.match(/\n*$/); + if (trailingNewlines && trailingNewlines[0]) { + remove trailing newlines at the end of this block before making a new one + return change.deleteBackward(trailingNewlines[0].length); + }*/ + return; }