5
5
isAncestorEmpty ,
6
6
unwrapNodes ,
7
7
isFirstChild ,
8
+ isSelectionAtBlockStart ,
8
9
} from '@udecode/plate-common' ;
9
10
import { ELEMENT_BLOCKQUOTE } from '@udecode/plate-block-quote' ;
10
11
@@ -15,6 +16,14 @@ function isWithinBlockquote(editor, entry) {
15
16
return blockAbove ?. [ 0 ] ?. type === ELEMENT_BLOCKQUOTE ;
16
17
}
17
18
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
+
18
27
function unwrap ( editor ) {
19
28
unwrapNodes ( editor , { split : true , match : n => n . type === ELEMENT_BLOCKQUOTE } ) ;
20
29
return true ;
@@ -27,12 +36,11 @@ function onKeyDownBlockquoteExitBreak(editor, { options: { rules } }) {
27
36
const entry = getBlockAbove ( editor ) ;
28
37
if ( ! entry ) return ;
29
38
30
- rules . forEach ( ( { hotkey, isFirstParagraph } ) => {
39
+ rules . forEach ( ( { hotkey, query } ) => {
31
40
if (
32
41
isHotkey ( hotkey , event ) &&
33
- isAncestorEmpty ( editor , entry [ 0 ] ) &&
34
42
isWithinBlockquote ( editor , entry ) &&
35
- ( ! isFirstParagraph || isFirstChild ( entry [ 1 ] ) ) &&
43
+ queryNode ( editor , entry , query ) &&
36
44
unwrap ( editor )
37
45
) {
38
46
event . preventDefault ( ) ;
@@ -48,7 +56,10 @@ const createBlockquoteExtPlugin = createPluginFactory({
48
56
onKeyDown : onKeyDownBlockquoteExitBreak ,
49
57
} ,
50
58
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
+ ] ,
52
63
} ,
53
64
} ) ;
54
65
0 commit comments