Skip to content

Commit 53cc87b

Browse files
oleqReinmar
authored andcommitted
Added backspace handler – collapsed selection, adjacent blocks.
1 parent 8837211 commit 53cc87b

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

core/editable.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,92 @@
796796
ev.data.preventDefault();
797797
} );
798798
}
799+
800+
if ( CKEDITOR.env.webkit ) {
801+
function isBlock( node ) {
802+
return node.type == CKEDITOR.NODE_ELEMENT && node.isBlockBoundary();
803+
}
804+
805+
this.attachListener( this, 'keydown', function( evt ) {
806+
var key = evt.data.getKey();
807+
808+
if ( !( key in backspaceOrDelete ) )
809+
return;
810+
811+
var backspace = key == 8,
812+
selection = editor.getSelection(),
813+
range = selection.getRanges()[ 0 ],
814+
path = range.startPath();
815+
816+
if ( range.collapsed || !path.block ) {
817+
var walkerRange = editor.createRange();
818+
819+
walkerRange.setStartAt( this, CKEDITOR.POSITION_AFTER_START );
820+
walkerRange.setEndAt( path.block, CKEDITOR.POSITION_AFTER_START );
821+
822+
var walker = new CKEDITOR.dom.walker( walkerRange ),
823+
straightBranch = true,
824+
entered, node, siblingBlock, lastElementLeft;
825+
826+
walker.guard = function( node, leaving ) {
827+
// If leaving for the 2nd time.
828+
if ( entered && leaving )
829+
return false;
830+
831+
entered = !leaving;
832+
833+
// Record last element left.
834+
if ( leaving ) {
835+
console.log( node );
836+
if ( straightBranch )
837+
straightBranch = node.getChildCount() == 1;
838+
839+
lastElementLeft = node;
840+
}
841+
842+
if ( entered && isBlock( node ) )
843+
siblingBlock = node;
844+
845+
return true;
846+
};
847+
848+
while ( ( node = walker.previous() ) ) {}
849+
850+
// console.log( 'siblingBlock', siblingBlock.getOuterHtml() );
851+
// console.log( 'lastElementLeft', lastElementLeft.getOuterHtml() );
852+
853+
// No sibling block to be merged to.
854+
if ( !siblingBlock )
855+
return;
856+
857+
// Not at the first editable position.
858+
if ( !range.checkBoundaryOfElement( lastElementLeft, CKEDITOR.START ) )
859+
return;
860+
861+
// Save selection. It will be restored.
862+
bookmarks = selection.createBookmarks();
863+
864+
// Glue path.block with siblingBlock.
865+
path.block.moveChildren( siblingBlock, false );
866+
867+
if ( !path.lastElement.equals( path.block ) )
868+
path.lastElement.mergeSiblings();
869+
870+
if ( straightBranch )
871+
lastElementLeft.remove();
872+
else
873+
path.block.remove();
874+
875+
// Restore selection.
876+
selection.selectBookmarks( bookmarks );
877+
//console.log( 'end' );
878+
} else {
879+
console.log( 'range not collapsed!' );
880+
}
881+
882+
evt.data.preventDefault();
883+
}, this, null, 100 ); // Later is better – do not override existing listeners.
884+
}
799885
}
800886
},
801887

0 commit comments

Comments
 (0)