This repository was archived by the owner on Oct 11, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -159,6 +159,35 @@ const createMarkdownPlugin = (config = {}) => {
159159 }
160160 return "not-handled" ;
161161 } ,
162+ handleKeyCommand ( command , editorState , { setEditorState } ) {
163+ switch ( command ) {
164+ case "backspace" : {
165+ // When a styled block is the first thing in the editor,
166+ // you cannot delete it. Typing backspace only deletes the content
167+ // but never deletes the block styling.
168+ // This piece of code fixes the issue by changing the block type
169+ // to 'unstyled' if we're on the first block of the editor and it's empty
170+ const selection = editorState . getSelection ( ) ;
171+ const currentBlockKey = selection . getStartKey ( ) ;
172+ if ( ! currentBlockKey ) return "not-handled" ;
173+
174+ const content = editorState . getCurrentContent ( ) ;
175+ const currentBlock = content . getBlockForKey ( currentBlockKey ) ;
176+ const firstBlock = content . getFirstBlock ( ) ;
177+ if ( firstBlock !== currentBlock ) return "not-handled" ;
178+
179+ const currentBlockType = currentBlock . getType ( ) ;
180+ const isEmpty = currentBlock . getLength ( ) === 0 ;
181+ if ( ! isEmpty || currentBlockType === "unstyled" ) return "not-handled" ;
182+
183+ setEditorState ( changeCurrentBlockType ( editorState , "unstyled" , "" ) ) ;
184+ return "handled" ;
185+ }
186+ default : {
187+ return "not-handled" ;
188+ }
189+ }
190+ } ,
162191 handlePastedText ( text , html , editorState , { setEditorState } ) {
163192 if ( html ) {
164193 return "not-handled" ;
You can’t perform that action at this time.
0 commit comments