浏览代码

Add filtering of blocks that are nested in other selected blocks.

Maciej Gołaszewski 7 年之前
父节点
当前提交
a0c939d921

+ 21 - 1
packages/ckeditor5-block-quote/src/blockquotecommand.js

@@ -44,7 +44,15 @@ export default class BlockQuoteCommand extends Command {
 		const model = this.editor.model;
 		const doc = model.document;
 		const schema = model.schema;
-		const blocks = Array.from( doc.selection.getSelectedBlocks() );
+
+		const selectedBlocks = Array.from( doc.selection.getSelectedBlocks() );
+
+		const blocks = selectedBlocks.filter( block => {
+			const parentBlock = findAncestorBlock( model.createPositionBefore( block ), schema );
+
+			// Filter out blocks that are nested in other selected blocks (like paragraphs in tables).
+			return !parentBlock || !selectedBlocks.includes( parentBlock );
+		} );
 
 		model.change( writer => {
 			if ( this.value ) {
@@ -223,3 +231,15 @@ function checkCanBeQuoted( schema, block ) {
 
 	return isBQAllowed && isBlockAllowedInBQ;
 }
+
+export function findAncestorBlock( position, schema ) {
+	let parent = position.parent;
+
+	while ( parent ) {
+		if ( schema.isBlock( parent ) ) {
+			return parent;
+		}
+
+		parent = parent.parent;
+	}
+}

+ 16 - 1
packages/ckeditor5-block-quote/tests/integration.js

@@ -20,6 +20,7 @@ import {
 	getData as getModelData,
 	setData as setModelData
 } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+import Table from '@ckeditor/ckeditor5-table/src/table';
 
 describe( 'BlockQuote integration', () => {
 	let editor, model, element, viewDocument;
@@ -30,7 +31,7 @@ describe( 'BlockQuote integration', () => {
 
 		return ClassicTestEditor
 			.create( element, {
-				plugins: [ BlockQuote, Paragraph, Image, ImageCaption, List, Enter, Delete, Heading ]
+				plugins: [ BlockQuote, Paragraph, Image, ImageCaption, List, Enter, Delete, Heading, Table ]
 			} )
 			.then( newEditor => {
 				editor = newEditor;
@@ -418,4 +419,18 @@ describe( 'BlockQuote integration', () => {
 			);
 		} );
 	} );
+
+	describe( 'compatibility with tables', () => {
+		it( 'wraps whole table in paragraph', () => {
+			setModelData( model, '[<table><tableRow><tableCell><paragraph>foo</paragraph></tableCell></tableRow></table>]' );
+
+			editor.execute( 'blockQuote' );
+
+			expect( getModelData( model ) ).to.equal(
+				'<blockQuote>' +
+				'[<table><tableRow><tableCell><paragraph>foo</paragraph></tableCell></tableRow></table>]' +
+				'</blockQuote>'
+			);
+		} );
+	} );
 } );