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 text in read-only mode. Fixed dragging nested editable. #9374

Merged
merged 2 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions packages/ckeditor5-clipboard/src/dragdrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export default class DragDrop extends Plugin {
const selection = modelDocument.selection;

// Don't drag the editable element itself.
if ( data.target && data.target.is( 'rootElement' ) ) {
if ( data.target && data.target.is( 'editableElement' ) ) {
data.preventDefault();

return;
Expand Down Expand Up @@ -470,7 +470,7 @@ export default class DragDrop extends Plugin {
// If this was not a widget then we should check if we need to drag some text content.
// In Chrome set a 'draggable' attribute on closest editable to allow immediate dragging of the selected text range.
// In Firefox this is not needed. In Safari it makes the whole editable draggable (not just textual content).
if ( env.isBlink && !draggableElement && !viewDocument.selection.isCollapsed ) {
if ( env.isBlink && !editor.isReadOnly && !draggableElement && !viewDocument.selection.isCollapsed ) {
const selectedElement = viewDocument.selection.getSelectedElement();

if ( !selectedElement || !isWidget( selectedElement ) ) {
Expand Down
83 changes: 82 additions & 1 deletion packages/ckeditor5-clipboard/tests/dragdrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,38 @@ describe( 'Drag and Drop', () => {
expect( spyClipboardOutput.notCalled ).to.be.true;
} );

it( 'should not start dragging if the editable would be dragged itself', () => {
setModelData( model, '<table><tableRow><tableCell><paragraph>[foo]bar</paragraph></tableCell></tableRow></table>' );

const dataTransferMock = createDataTransfer();
const spyClipboardOutput = sinon.spy();
const spyPreventDefault = sinon.spy();

viewDocument.on( 'clipboardOutput', ( event, data ) => spyClipboardOutput( data ) );

const modelElement = root.getNodeByPath( [ 0, 0, 0 ] );
const eventData = prepareEventData( model.createPositionAt( modelElement.getChild( 0 ), 3 ) );
eventData.target = mapper.toViewElement( modelElement );
eventData.domTarget = domConverter.mapViewToDom( eventData.target );

expect( eventData.target.is( 'editableElement', 'td' ) ).to.be.true;

viewDocument.fire( 'mousedown', {
...eventData
} );

viewDocument.fire( 'dragstart', {
...eventData,
dataTransfer: dataTransferMock,
preventDefault: spyPreventDefault,
stopPropagation: () => {
}
} );

expect( spyPreventDefault.called ).to.be.true;
expect( spyClipboardOutput.notCalled ).to.be.true;
} );

it( 'should mark allowed effect as "copy" if the editor is read-only', () => {
setModelData( model, '<paragraph>[foo]bar</paragraph>' );

Expand All @@ -781,7 +813,7 @@ describe( 'Drag and Drop', () => {

fireDragStart( dataTransferMock );

expect( viewDocument.getRoot().hasAttribute( 'draggable' ) ).to.be.true;
expect( viewDocument.getRoot().hasAttribute( 'draggable' ) ).to.be.false;
expect( dataTransferMock.effectAllowed ).to.equal( 'copy' );
} );

Expand Down Expand Up @@ -851,6 +883,55 @@ describe( 'Drag and Drop', () => {
);
} );

it( 'should start dragging by grabbing the widget selection handle (in read only mode)', () => {
setModelData( model,
'<paragraph>[]foobar</paragraph>' +
'<table><tableRow><tableCell><paragraph>abc</paragraph></tableCell></tableRow></table>'
);

editor.isReadOnly = true;

const dataTransferMock = createDataTransfer();
const spyClipboardOutput = sinon.spy();

viewDocument.on( 'clipboardOutput', ( event, data ) => spyClipboardOutput( data ) );

const domNode = view.getDomRoot().querySelector( '.ck-widget__selection-handle' );
const widgetViewElement = viewDocument.getRoot().getChild( 1 );
const selectionHandleElement = widgetViewElement.getChild( 0 );

expect( selectionHandleElement.hasClass( 'ck-widget__selection-handle' ) ).to.be.true;

const eventData = {
domTarget: domNode,
target: selectionHandleElement,
domEvent: {}
};

viewDocument.fire( 'mousedown', {
...eventData
} );

viewDocument.fire( 'dragstart', {
...eventData,
dataTransfer: dataTransferMock,
stopPropagation: () => {}
} );

expect( dataTransferMock.getData( 'text/html' ) ).to.equal(
'<figure class="table"><table><tbody><tr><td>abc</td></tr></tbody></table></figure>'
);

expect( widgetViewElement.getAttribute( 'draggable' ) ).to.equal( 'true' );

expect( spyClipboardOutput.called ).to.be.true;
expect( spyClipboardOutput.firstCall.firstArg.method ).to.equal( 'dragstart' );
expect( spyClipboardOutput.firstCall.firstArg.dataTransfer ).to.equal( dataTransferMock );
expect( stringifyView( spyClipboardOutput.firstCall.firstArg.content ) ).to.equal(
'<figure class="table"><table><tbody><tr><td>abc</td></tr></tbody></table></figure>'
);
} );

it( 'should start dragging by grabbing the widget element directly', () => {
setModelData( model,
'<paragraph>[]foobar</paragraph>' +
Expand Down