@@ -11,19 +11,62 @@ const { subscribeOwnedListener, ownsSelection } = unlock( richTextPrivateApis );
1111const { subscribeDelegatedListener } = unlock ( composePrivateApis ) ;
1212
1313export default ( props ) => ( element ) => {
14- function onKeyDownDeprecated ( event ) {
14+ function onKeyDown ( event ) {
1515 if ( event . keyCode !== ENTER ) {
1616 return ;
1717 }
1818
19- const { onReplace, onSplit } = props . current ;
19+ const {
20+ onReplace,
21+ onSplit,
22+ supportsSplitting,
23+ disableLineBreaks,
24+ onChange,
25+ value,
26+ onSplitAtDoubleLineEnd,
27+ registry,
28+ onSplitAtEnd,
29+ } = props . current ;
30+ const { text, start, end } = value ;
2031
21- if ( onReplace && onSplit ) {
32+ if ( event . shiftKey ) {
33+ if ( ! disableLineBreaks ) {
34+ event . preventDefault ( ) ;
35+ onChange ( insert ( value , '\n' ) ) ;
36+ }
37+ } else if ( onSplitAtEnd && start === end && end === text . length ) {
38+ event . preventDefault ( ) ;
39+ onSplitAtEnd ( ) ;
40+ } else if ( onReplace && onSplit ) {
2241 event . __deprecatedOnSplit = true ;
42+ } else if (
43+ ! supportsSplitting &&
44+ ! disableLineBreaks &&
45+ ! event . defaultPrevented
46+ ) {
47+ event . preventDefault ( ) ;
48+ if (
49+ // For some blocks it's desirable to split at the end of the
50+ // block when there are two line breaks at the end of the
51+ // block, so triple Enter exits the block.
52+ onSplitAtDoubleLineEnd &&
53+ start === end &&
54+ end === text . length &&
55+ text . slice ( - 2 ) === '\n\n'
56+ ) {
57+ registry . batch ( ( ) => {
58+ const _value = { ...value } ;
59+ _value . start = _value . end - 2 ;
60+ onChange ( remove ( _value ) ) ;
61+ onSplitAtDoubleLineEnd ( ) ;
62+ } ) ;
63+ } else {
64+ onChange ( insert ( value , '\n' ) ) ;
65+ }
2366 }
2467 }
2568
26- function onKeyDown ( event ) {
69+ function onDefaultKeyDown ( event ) {
2770 if ( event . defaultPrevented ) {
2871 return ;
2972 }
@@ -39,64 +82,30 @@ export default ( props ) => ( element ) => {
3982 return ;
4083 }
4184
42- const {
43- value,
44- onChange,
45- disableLineBreaks,
46- onSplitAtEnd,
47- onSplitAtDoubleLineEnd,
48- registry,
49- } = props . current ;
50-
85+ // On ENTER, we ALWAYS want to prevent the default browser behaviour
86+ // at this last interception point.
5187 event . preventDefault ( ) ;
52-
53- const { text, start, end } = value ;
54-
55- if ( event . shiftKey ) {
56- if ( ! disableLineBreaks ) {
57- onChange ( insert ( value , '\n' ) ) ;
58- }
59- } else if ( onSplitAtEnd && start === end && end === text . length ) {
60- onSplitAtEnd ( ) ;
61- } else if (
62- // For some blocks it's desirable to split at the end of the
63- // block when there are two line breaks at the end of the
64- // block, so triple Enter exits the block.
65- onSplitAtDoubleLineEnd &&
66- start === end &&
67- end === text . length &&
68- text . slice ( - 2 ) === '\n\n'
69- ) {
70- registry . batch ( ( ) => {
71- const _value = { ...value } ;
72- _value . start = _value . end - 2 ;
73- onChange ( remove ( _value ) ) ;
74- onSplitAtDoubleLineEnd ( ) ;
75- } ) ;
76- } else if ( ! disableLineBreaks ) {
77- onChange ( insert ( value , '\n' ) ) ;
78- }
7988 }
8089
8190 const { defaultView } = element . ownerDocument ;
8291
8392 // Attach the listener to the window so parent elements have the chance to
8493 // prevent the default behavior.
85- const unsubscribeKeyDown = subscribeDelegatedListener (
94+ const unsubscribeDefaultKeyDown = subscribeDelegatedListener (
8695 defaultView ,
8796 'keydown' ,
88- onKeyDown
97+ onDefaultKeyDown
8998 ) ;
9099 // Capture phase so this runs before ancestor (writing flow) bubble
91100 // handlers, matching the timing of the previous raw element listener.
92- const unsubscribeKeyDownDeprecated = subscribeOwnedListener (
101+ const unsubscribeKeyDown = subscribeOwnedListener (
93102 element ,
94103 'keydown' ,
95- onKeyDownDeprecated ,
104+ onKeyDown ,
96105 true
97106 ) ;
98107 return ( ) => {
108+ unsubscribeDefaultKeyDown ( ) ;
99109 unsubscribeKeyDown ( ) ;
100- unsubscribeKeyDownDeprecated ( ) ;
101110 } ;
102111} ;
0 commit comments