Skip to content

Commit

Permalink
Select all in parent limit elements for successive select-all commands.
Browse files Browse the repository at this point in the history
Closes #6621.
  • Loading branch information
tomalec committed May 13, 2020
1 parent fb5fe8b commit 0c1d2e6
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
17 changes: 15 additions & 2 deletions packages/ckeditor5-select-all/src/selectallcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Command from '@ckeditor/ckeditor5-core/src/command';
*
* If the selection was anchored in a {@glink framework/guides/tutorials/implementing-a-block-widget nested editable}
* (e.g. a caption of an image), the new selection will contain its entire content.
* Successive execution - selecting all if entire content is selected - expands selection to the parent editable.
*
* @extends module:core/command~Command
*/
Expand All @@ -30,10 +31,22 @@ export default class SelectAllCommand extends Command {
*/
execute() {
const model = this.editor.model;
const limitElement = model.schema.getLimitElement( model.document.selection );
const selection = model.document.selection;
let limitElement = model.schema.getLimitElement( selection );

let place = 'in';
// If entire element was already selected, try selecting all in a parent limit element (if any).
if ( selection.containsEntireContent( limitElement ) && limitElement.root !== limitElement ) {
do {
if ( limitElement.parent ) {
limitElement = limitElement.parent;
}
} while ( !model.schema.isLimit( limitElement ) );
place = 'on';
}

model.change( writer => {
writer.setSelection( limitElement, 'in' );
writer.setSelection( limitElement, place );
} );
}
}
64 changes: 63 additions & 1 deletion packages/ckeditor5-select-all/tests/selectallcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,21 @@ describe( 'SelectAllCommand', () => {
} );

it( 'should select all (selection in a nested editable)', () => {
setData( model, '<paragraph>foo</paragraph><image src="foo.png"><caption>[bar]</caption></image>' );
setData( model, '<paragraph>foo</paragraph><image src="foo.png"><caption>b[ar]</caption></image>' );

editor.execute( 'selectAll' );

expect( getData( model ) ).to.equal( '<paragraph>foo</paragraph><image src="foo.png"><caption>[bar]</caption></image>' );
} );

it( 'when entire editable is selected, should select all in parent limit element', () => {
setData( model, '<paragraph>foo</paragraph><image src="foo.png"><caption>[bar]</caption></image>' );

editor.execute( 'selectAll' );

expect( getData( model ) ).to.equal( '<paragraph>foo</paragraph>[<image src="foo.png"><caption>bar</caption></image>]' );
} );

it( 'should select all in the closest nested editable (nested editable inside another nested editable)', () => {
setData( model,
'<paragraph>foo</paragraph>' +
Expand All @@ -103,5 +111,59 @@ describe( 'SelectAllCommand', () => {
'</table>'
);
} );

it( 'consecutive execute() on nested editable, should select all in the parent limit element', () => {
setData( model,
'<paragraph>foo</paragraph>' +
'<table>' +
'<tableRow>' +
'<tableCell>' +
'<paragraph>foo</paragraph>' +
'<image src="foo.png"><caption>b[]ar</caption></image>' +
'</tableCell>' +
'</tableRow>' +
'</table>'
);

editor.execute( 'selectAll' );
editor.execute( 'selectAll' );

expect( getData( model ) ).to.equal( '<paragraph>foo</paragraph>' +
'<table>' +
'<tableRow>' +
'<tableCell>' +
'<paragraph>foo</paragraph>' +
'[<image src="foo.png"><caption>bar</caption></image>]' +
'</tableCell>' +
'</tableRow>' +
'</table>'
);

editor.execute( 'selectAll' );

expect( getData( model ) ).to.equal( '<paragraph>foo</paragraph>' +
'<table>' +
'<tableRow>' +
'<tableCell>' +
'<paragraph>[foo</paragraph>' +
'<image src="foo.png"><caption>bar</caption></image>]' +
'</tableCell>' +
'</tableRow>' +
'</table>'
);

editor.execute( 'selectAll' );

expect( getData( model ) ).to.equal( '<paragraph>foo</paragraph>' +
'[<table>' +
'<tableRow>' +
'<tableCell>' +
'<paragraph>foo</paragraph>' +
'<image src="foo.png"><caption>bar</caption></image>' +
'</tableCell>' +
'</tableRow>' +
'</table>]'
);
} );
} );
} );

0 comments on commit 0c1d2e6

Please sign in to comment.