Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed dragging from and into container without other elements. #15126

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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