8
0
فهرست منبع

Select all in parent limit elements for successive select-all commands. Closes #6621.

Tomek Wytrębowicz 5 سال پیش
والد
کامیت
0c1d2e67c1
2فایلهای تغییر یافته به همراه78 افزوده شده و 3 حذف شده
  1. 15 2
      packages/ckeditor5-select-all/src/selectallcommand.js
  2. 63 1
      packages/ckeditor5-select-all/tests/selectallcommand.js

+ 15 - 2
packages/ckeditor5-select-all/src/selectallcommand.js

@@ -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
  */
@@ -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 );
 		} );
 	}
 }

+ 63 - 1
packages/ckeditor5-select-all/tests/selectallcommand.js

@@ -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>' +
@@ -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>]'
+			);
+		} );
 	} );
 } );