@@ -7,18 +7,72 @@ export const BlockNode = Extension.create({
77
88 addCommands ( ) {
99 return {
10- replaceBlockNodeById : ( id , content ) => ( params ) => {
11- const { tr } = params ;
12- return null ;
13- } ,
10+ replaceBlockNodeById :
11+ ( id , contentNode ) =>
12+ ( { dispatch, tr } ) => {
13+ let blockNode = this . editor . helpers . BlockNode . getBlockNodeById ( id ) ;
14+ if ( ! blockNode || blockNode . length > 1 ) {
15+ return true ;
16+ }
1417
15- deleteBlockNodeById : ( id ) => ( params ) => {
16- return null ;
17- } ,
18+ if ( dispatch ) {
19+ let { pos, node } = blockNode [ 0 ] ;
20+ let newPosFrom = tr . mapping . map ( pos ) ; // map the position between transaction steps
21+ let newPosTo = tr . mapping . map ( pos + node . nodeSize ) ;
1822
19- updateBlockNodeAttrs : ( id , attrs ) => ( params ) => {
20- return null ;
21- } ,
23+ let currentNode = tr . doc . nodeAt ( newPosFrom ) ;
24+ if ( node . eq ( currentNode ) ) {
25+ tr . replaceWith ( newPosFrom , newPosTo , contentNode ) ;
26+ }
27+ }
28+
29+ return true ;
30+ } ,
31+
32+ deleteBlockNodeById :
33+ ( id ) =>
34+ ( { dispatch, tr } ) => {
35+ let blockNode = this . editor . helpers . BlockNode . getBlockNodeById ( id ) ;
36+ if ( ! blockNode || blockNode . length > 1 ) {
37+ return true ;
38+ }
39+
40+ if ( dispatch ) {
41+ let { pos, node } = blockNode [ 0 ] ;
42+ let newPosFrom = tr . mapping . map ( pos ) ; // map the position between transaction steps
43+ let newPosTo = tr . mapping . map ( pos + node . nodeSize ) ;
44+
45+ let currentNode = tr . doc . nodeAt ( newPosFrom ) ;
46+ if ( node . eq ( currentNode ) ) {
47+ tr . delete ( newPosFrom , newPosTo ) ;
48+ }
49+ }
50+
51+ return true ;
52+ } ,
53+
54+ updateBlockNodeAttributes :
55+ ( id , attrs = { } ) =>
56+ ( { dispatch, tr } ) => {
57+ if ( ! dispatch ) return true ;
58+
59+ let blockNode = this . editor . helpers . BlockNode . getBlockNodeById ( id ) ;
60+ if ( ! blockNode || blockNode . length > 1 ) {
61+ return true ;
62+ }
63+
64+ let { pos, node } = blockNode [ 0 ] ;
65+ let newPos = tr . mapping . map ( pos ) ;
66+ let currentNode = tr . doc . nodeAt ( newPos ) ;
67+ if ( node . eq ( currentNode ) ) {
68+ tr . setNodeMarkup ( newPos , undefined , {
69+ ...node . attrs ,
70+ ...attrs ,
71+ } ) ;
72+ }
73+
74+ return true ;
75+ } ,
2276 } ;
2377 } ,
2478
0 commit comments