55 isAncestorEmpty ,
66 unwrapNodes ,
77 isFirstChild ,
8+ isSelectionAtBlockStart ,
89} from '@udecode/plate-common' ;
910import { ELEMENT_BLOCKQUOTE } from '@udecode/plate-block-quote' ;
1011
@@ -15,6 +16,14 @@ function isWithinBlockquote(editor, entry) {
1516 return blockAbove ?. [ 0 ] ?. type === ELEMENT_BLOCKQUOTE ;
1617}
1718
19+ function queryNode ( editor , entry , { empty, first, start } ) {
20+ return (
21+ ( ! empty || isAncestorEmpty ( editor , entry [ 0 ] ) ) &&
22+ ( ! first || isFirstChild ( entry [ 1 ] ) ) &&
23+ ( ! start || isSelectionAtBlockStart ( editor ) )
24+ ) ;
25+ }
26+
1827function unwrap ( editor ) {
1928 unwrapNodes ( editor , { split : true , match : n => n . type === ELEMENT_BLOCKQUOTE } ) ;
2029 return true ;
@@ -27,12 +36,11 @@ function onKeyDownBlockquoteExitBreak(editor, { options: { rules } }) {
2736 const entry = getBlockAbove ( editor ) ;
2837 if ( ! entry ) return ;
2938
30- rules . forEach ( ( { hotkey, isFirstParagraph } ) => {
39+ rules . forEach ( ( { hotkey, query } ) => {
3140 if (
3241 isHotkey ( hotkey , event ) &&
33- isAncestorEmpty ( editor , entry [ 0 ] ) &&
3442 isWithinBlockquote ( editor , entry ) &&
35- ( ! isFirstParagraph || isFirstChild ( entry [ 1 ] ) ) &&
43+ queryNode ( editor , entry , query ) &&
3644 unwrap ( editor )
3745 ) {
3846 event . preventDefault ( ) ;
@@ -48,7 +56,10 @@ const createBlockquoteExtPlugin = createPluginFactory({
4856 onKeyDown : onKeyDownBlockquoteExitBreak ,
4957 } ,
5058 options : {
51- rules : [ { hotkey : 'enter' } , { hotkey : 'backspace' , isFirstParagraph : true } ] ,
59+ rules : [
60+ { hotkey : 'enter' , query : { empty : true } } ,
61+ { hotkey : 'backspace' , query : { first : true , start : true } } ,
62+ ] ,
5263 } ,
5364} ) ;
5465
0 commit comments