@@ -2,66 +2,76 @@ import {keymap} from 'prosemirror-keymap';
22import { Fragment } from 'prosemirror-model' ;
33import { Command , TextSelection } from 'prosemirror-state' ;
44import { findParentNodeOfType } from 'prosemirror-utils' ;
5+ import { isWholeSelection } from '../../../utils/selection' ;
56import { pType } from '../../base/BaseSchema' ;
67import { checkboxInputType , checkboxLabelType , checkboxType } from './utils' ;
78
8- export const splitCheckbox : Command = ( state , dispatch ) => {
9- const { $from, $to} = state . selection ;
10- const { schema} = state ;
11- const label = findParentNodeOfType ( checkboxLabelType ( schema ) ) ( state . selection ) ;
9+ export const splitCheckbox : ( replaceWithParagraph ?: boolean ) => Command =
10+ ( replaceWithParagraph ) => ( state , dispatch ) => {
11+ const { $from, $to} = state . selection ;
12+ const { schema} = state ;
13+ const label = findParentNodeOfType ( checkboxLabelType ( schema ) ) ( state . selection ) ;
1214
13- if ( ! label ) return false ;
15+ if ( ! label ) return false ;
1416
15- const checkbox = findParentNodeOfType ( checkboxType ( schema ) ) ( state . selection ) ;
17+ const checkbox = findParentNodeOfType ( checkboxType ( schema ) ) ( state . selection ) ;
1618
17- if ( label . node . content . size === 0 && checkbox ) {
18- dispatch ?.(
19- state . tr
20- . replaceWith (
21- checkbox . pos ,
22- checkbox . pos + checkbox . node . nodeSize ,
23- pType ( schema ) . create ( ) ,
24- )
25- . scrollIntoView ( ) ,
26- ) ;
19+ if ( label . node . content . size === 0 && checkbox ) {
20+ dispatch ?.(
21+ state . tr
22+ . replaceWith (
23+ checkbox . pos ,
24+ checkbox . pos + checkbox . node . nodeSize ,
25+ pType ( schema ) . create ( ) ,
26+ )
27+ . scrollIntoView ( ) ,
28+ ) ;
2729
28- return true ;
29- }
30+ return true ;
31+ }
3032
31- if ( $from . pos === $to . pos ) {
32- const { tr} = state ;
33+ if ( $from . pos === $to . pos ) {
34+ const { tr} = state ;
3335
34- const node = checkboxType ( schema ) . create ( { } , [
35- checkboxInputType ( schema ) . create ( ) ,
36- checkboxLabelType ( schema ) . create (
37- { } ,
38- Fragment . from ( $from . parent . cut ( $from . parentOffset ) . content ) ,
39- ) ,
40- ] ) ;
36+ const content = Fragment . from ( $from . parent . cut ( $from . parentOffset ) . content ) ;
4137
42- tr . insert ( $from . after ( ) , [ node ] ) ;
38+ const node = replaceWithParagraph
39+ ? pType ( state . schema ) . create ( { } , content )
40+ : checkboxType ( schema ) . create ( { } , [
41+ checkboxInputType ( schema ) . create ( ) ,
42+ checkboxLabelType ( schema ) . create ( { } , content ) ,
43+ ] ) ;
4344
44- tr . replace ( label . start + $from . parentOffset , label . pos + label . node . nodeSize ) ;
45- tr . setSelection ( new TextSelection ( tr . doc . resolve ( tr . selection . $from . after ( ) + 4 ) ) ) ;
46- dispatch ?.( tr ) ;
45+ tr . insert ( $from . after ( ) , [ node ] ) ;
4746
48- return true ;
49- }
47+ tr . replace ( label . start + $from . parentOffset , label . pos + label . node . nodeSize ) ;
48+ tr . setSelection (
49+ new TextSelection (
50+ tr . doc . resolve ( tr . selection . $from . after ( ) + ( replaceWithParagraph ? 2 : 4 ) ) ,
51+ ) ,
52+ ) ;
53+ dispatch ?.( tr ) ;
5054
51- return false ;
52- } ;
55+ return true ;
56+ }
57+
58+ return false ;
59+ } ;
5360
5461const removeCheckbox : Command = ( state , dispatch ) => {
5562 const label = findParentNodeOfType ( checkboxLabelType ( state . schema ) ) ( state . selection ) ;
5663 const checkbox = findParentNodeOfType ( checkboxType ( state . schema ) ) ( state . selection ) ;
5764
65+ const { selection} = state ;
66+ const { from, to} = selection ;
67+
5868 if ( ! label || ! checkbox ) {
5969 return false ;
6070 }
6171
62- const idx = state . selection . from - label . pos - 2 ;
72+ const idx = from - label . pos - 2 ;
6373
64- if ( idx < 0 ) {
74+ if ( idx < 0 && from === to && ! isWholeSelection ( selection ) ) {
6575 const { tr} = state ;
6676 dispatch ?.(
6777 tr
@@ -80,6 +90,7 @@ const removeCheckbox: Command = (state, dispatch) => {
8090
8191export const keymapPlugin = ( ) =>
8292 keymap ( {
83- Enter : splitCheckbox ,
93+ Enter : splitCheckbox ( ) ,
8494 Backspace : removeCheckbox ,
95+ 'Shift-Enter' : splitCheckbox ( true ) ,
8596 } ) ;
0 commit comments