Skip to content

Commit

Permalink
Merge pull request #15126 from ckeditor/ck/15087
Browse files Browse the repository at this point in the history
Fix (clipboard): Fixed dragging from and into a container without other elements. Closes #15087.
  • Loading branch information
arkflpc authored Oct 4, 2023
2 parents 4146775 + ac361bc commit 898e933
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/ckeditor5-clipboard/src/dragdrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,22 @@ export default class DragDrop extends Plugin {

// Delete moved content.
if ( moved && this.isEnabled ) {
model.deleteContent( model.createSelection( this._draggedRange ), { doNotAutoparagraph: true } );
model.change( writer => {
const selection = model.createSelection( this._draggedRange );

model.deleteContent( selection, { doNotAutoparagraph: true } );

// Check result selection if it does not require auto-paragraphing of empty container.
const selectionParent = selection.getFirstPosition()!.parent as Element;

if (
selectionParent.isEmpty &&
!model.schema.checkChild( selectionParent, '$text' ) &&
model.schema.checkChild( selectionParent, 'paragraph' )
) {
writer.insertElement( 'paragraph', selectionParent, 0 );
}
} );
}

this._draggedRange.detach();
Expand Down
4 changes: 4 additions & 0 deletions packages/ckeditor5-clipboard/src/dragdroptarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@ function findDropTargetRange(
let startIndex = 0;
let endIndex = childNodes.length;

if ( endIndex == 0 ) {
return model.createRange( model.createPositionAt( modelElement as Element, 'end' ) );
}

while ( startIndex < endIndex - 1 ) {
const middleIndex = Math.floor( ( startIndex + endIndex ) / 2 );
const side = findElementSide( editor, childNodes[ middleIndex ], clientX, clientY );
Expand Down
27 changes: 27 additions & 0 deletions packages/ckeditor5-clipboard/tests/dragdroptarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,33 @@ describe( 'Drag and Drop target', () => {
) ).to.be.true;
} );

it( 'should find drop position inside empty container element', () => {
model.schema.register( 'htmlDiv', { inheritAllFrom: '$container' } );
editor.conversion.elementToElement( { model: 'htmlDiv', view: 'div' } );

setModelData( model,
'<htmlDiv></htmlDiv>'
);

const modelElement = root.getChild( 0 );
const viewElement = mapper.toViewElement( modelElement );
const domNode = domConverter.mapViewToDom( viewElement );

const { clientX, clientY } = getMockedMousePosition( { domNode } );

dragDropTarget.updateDropMarker(
viewElement,
[ view.createRange( view.createPositionAt( viewElement, 0 ) ) ],
clientX,
clientY,
false
);

expect( model.markers.get( 'drop-target' ).getRange().start.isEqual(
model.createPositionAt( root.getChild( 0 ), 0 )
) ).to.be.true;
} );

it( 'should hide drop target if target is not in editing root', () => {
setModelData( model,
'<horizontalLine></horizontalLine>'
Expand Down

0 comments on commit 898e933

Please sign in to comment.