Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 98ca937

Browse files
author
Luke Barnard
committed
Interpret backspace at start of style block as block style toggle
Part of fixing element-hq/element-web#4580
1 parent ee51175 commit 98ca937

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/components/views/rooms/MessageComposerInput.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -528,16 +528,19 @@ export default class MessageComposerInput extends React.Component {
528528
if (this.state.isRichtextEnabled) {
529529
// These are block types, not handled by RichUtils by default.
530530
const blockCommands = ['code-block', 'blockquote', 'unordered-list-item', 'ordered-list-item'];
531+
const currentBlockType = RichUtils.getCurrentBlockType(this.state.editorState);
531532

532533
if (blockCommands.includes(command)) {
533-
this.setState({
534-
editorState: RichUtils.toggleBlockType(this.state.editorState, command),
535-
});
534+
newState = RichUtils.toggleBlockType(this.state.editorState, command);
536535
} else if (command === 'strike') {
537536
// this is the only inline style not handled by Draft by default
538-
this.setState({
539-
editorState: RichUtils.toggleInlineStyle(this.state.editorState, 'STRIKETHROUGH'),
540-
});
537+
newState = RichUtils.toggleInlineStyle(this.state.editorState, 'STRIKETHROUGH');
538+
} else if (command === 'backspace' && currentBlockType !== 'unstyled') {
539+
const currentStartOffset = this.state.editorState.getSelection().getStartOffset();
540+
if (currentStartOffset === 0) {
541+
// Toggle current block type (setting it to 'unstyled')
542+
newState = RichUtils.toggleBlockType(this.state.editorState, currentBlockType);
543+
}
541544
}
542545
} else {
543546
let contentState = this.state.editorState.getCurrentContent();
@@ -644,6 +647,7 @@ export default class MessageComposerInput extends React.Component {
644647
// By returning false, we allow the default draft-js key binding to occur,
645648
// which in this case invokes "split-block". This creates a new block of the
646649
// same type, allowing the user to delete it with backspace.
650+
// See handleKeyCommand (when command === 'backspace')
647651
return false;
648652
}
649653

0 commit comments

Comments
 (0)