Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge branch 'i/6114'
Browse files Browse the repository at this point in the history
Internal: Improved the look of multi-cell selection. Prevented the selection from starting in the fake selection container. Closes ckeditor/ckeditor5#6114. Closes ckeditor/ckeditor5#6353.
  • Loading branch information
Reinmar committed Feb 28, 2020
2 parents ffe7df6 + c4a7f0e commit c9326ee
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 15 deletions.
15 changes: 10 additions & 5 deletions src/tableselection/mouseselectionhandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ export default class MouseSelectionHandler {
* A flag indicating that the mouse selection is "active". A selection is "active" if it was started and not yet finished.
* A selection can be "active", for instance, if a user moves a mouse over a table while holding a mouse button down.
*
* @private
* @readonly
* @member {Boolean}
*/
this.isSelecting = false;
this._isSelecting = false;

/**
* Editing mapper.
Expand Down Expand Up @@ -77,12 +78,11 @@ export default class MouseSelectionHandler {

if ( !tableCell ) {
this._tableSelection.clearSelection();
this._tableSelection.stopSelection();

return;
}

this.isSelecting = true;
this._isSelecting = true;
this._tableSelection.startSelectingFrom( tableCell );
}

Expand All @@ -95,12 +95,15 @@ export default class MouseSelectionHandler {
* @private
*/
_handleMouseMove( domEventData ) {
if ( !isButtonPressed( domEventData ) ) {
if ( !this._isSelecting || !isButtonPressed( domEventData ) ) {
this._tableSelection.stopSelection();

return;
}

// https://github.com/ckeditor/ckeditor5/issues/6114
domEventData.preventDefault();

const tableCell = this._getModelTableCellFromDomEvent( domEventData );

if ( !tableCell ) {
Expand All @@ -119,10 +122,12 @@ export default class MouseSelectionHandler {
* @private
*/
_handleMouseUp( domEventData ) {
if ( !this.isSelecting ) {
if ( !this._isSelecting ) {
return;
}

this._isSelecting = false;

const tableCell = this._getModelTableCellFromDomEvent( domEventData );

// Selection can be stopped if table cell is undefined.
Expand Down
5 changes: 5 additions & 0 deletions tests/manual/tableselection.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
.external-source td, .external-source th {
border: solid 1px hsl(0, 0%, 85%);
}

/* It should not create entire viewport scroll. */
pre {
overflow: auto;
}
</style>

<h3>A "content" test editor</h3>
Expand Down
44 changes: 42 additions & 2 deletions tests/tableselection/mouseselectionhandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils'
import TableEditing from '../../src/tableediting';
import TableSelection from '../../src/tableselection';
import { assertSelectedCells, modelTable, viewTable } from '../_utils/utils';
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';

describe( 'table selection', () => {
let editor, model, view, viewDoc;

testUtils.createSinonSandbox();

beforeEach( async () => {
editor = await VirtualTestEditor.create( {
plugins: [ TableEditing, TableSelection, Paragraph ]
Expand Down Expand Up @@ -418,6 +421,41 @@ describe( 'table selection', () => {
[ '10', '11' ]
], { asWidget: true } ) + '<p>{}foo</p>' );
} );

// https://github.com/ckeditor/ckeditor5/issues/6353
it( 'should do nothing on successive "mouseup" events beyond the table after the selection was fininished', () => {
const spy = testUtils.sinon.spy( editor.plugins.get( 'TableSelection' ), 'stopSelection' );

setModelData( model, modelTable( [
[ '[]00', '01' ],
[ '10', '11' ]
] ) );

pressMouseButtonOver( getTableCell( '00' ) );
movePressedMouseOver( getTableCell( '11' ) );
releaseMouseButtonOver( getTableCell( '11' ) );

sinon.assert.calledOnce( spy );

pressMouseButtonOver( viewDoc.getRoot() );
releaseMouseButtonOver( viewDoc.getRoot() );

sinon.assert.calledOnce( spy );
} );

// https://github.com/ckeditor/ckeditor5/issues/6114
it( 'should prevent default the "mousemove" event to prevent the native DOM selection from changing', () => {
setModelData( model, modelTable( [
[ '[]00', '01' ],
[ '10', '11' ]
] ) );

pressMouseButtonOver( getTableCell( '00' ) );
const domEvtData = movePressedMouseOver( getTableCell( '11' ) );
releaseMouseButtonOver( getTableCell( '11' ) );

sinon.assert.calledOnce( domEvtData.preventDefault );
} );
} );

function getTableCell( data ) {
Expand All @@ -437,15 +475,15 @@ describe( 'table selection', () => {
}

function movePressedMouseOver( target ) {
moveMouseOver( target, mouseButtonPressed );
return moveMouseOver( target, mouseButtonPressed );
}

function moveReleasedMouseOver( target ) {
moveMouseOver( target, mouseButtonReleased );
}

function moveMouseOver( target, ...decorators ) {
fireEvent( view, 'mousemove', addTarget( target ), ...decorators );
return fireEvent( view, 'mousemove', addTarget( target ), ...decorators );
}

function releaseMouseButtonOver( target ) {
Expand Down Expand Up @@ -481,5 +519,7 @@ describe( 'table selection', () => {
}

viewDoc.fire( eventName, domEvtDataStub );

return domEvtDataStub;
}
} );
2 changes: 1 addition & 1 deletion theme/table.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
& th {
min-width: 2em;
padding: .4em;
border-color: hsl(0, 0%, 85%);
border-color: hsl(0, 0%, 75%);
}

& th {
Expand Down
7 changes: 0 additions & 7 deletions theme/tableselection.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,3 @@
* it acts as a message to the builder telling that it should look for the corresponding styles
* **in the theme** when compiling the editor.
*/

.ck.ck-editor__editable .table table {
& td.ck-editor__editable_selected,
& th.ck-editor__editable_selected {
box-shadow: inset 0 0 0 1px var(--ck-color-focus-border);
}
}

0 comments on commit c9326ee

Please sign in to comment.