Bladeren bron

First batch of changes updating schema use to match the new API.

Piotrek Koszuliński 8 jaren geleden
bovenliggende
commit
e0dcacaec8

+ 16 - 20
packages/ckeditor5-block-quote/src/blockquoteengine.js

@@ -31,9 +31,22 @@ export default class BlockQuoteEngine extends Plugin {
 
 		editor.commands.add( 'blockQuote', new BlockQuoteCommand( editor ) );
 
-		schema.registerItem( 'blockQuote' );
-		schema.allow( { name: 'blockQuote', inside: '$root' } );
-		schema.allow( { name: '$block', inside: 'blockQuote' } );
+		schema.register( 'blockQuote', {
+			allowWhere: '$block',
+			allowContentOf: '$root'
+		} );
+
+		// Disallow blockQuote in blockQuote.
+		schema.on( 'checkChild', ( evt, args ) => {
+			const ctx = args[ 0 ];
+			const child = args[ 1 ];
+			const childRule = schema.getRule( child );
+
+			if ( childRule.name == 'blockQuote' && ctx.matchEnd( 'blockQuote' ) ) {
+				evt.stop();
+				evt.return = false;
+			}
+		}, { priority: 'high' } );
 
 		buildViewConverter().for( editor.data.viewToModel )
 			.fromElement( 'blockquote' )
@@ -43,21 +56,4 @@ export default class BlockQuoteEngine extends Plugin {
 			.fromElement( 'blockQuote' )
 			.toElement( 'blockquote' );
 	}
-
-	/**
-	 * @inheritDoc
-	 */
-	afterInit() {
-		const schema = this.editor.model.schema;
-
-		// TODO
-		// Workaround for https://github.com/ckeditor/ckeditor5-engine/issues/532#issuecomment-280924650.
-		if ( schema.hasItem( 'listItem' ) ) {
-			schema.allow( {
-				name: 'listItem',
-				inside: 'blockQuote',
-				attributes: [ 'type', 'indent' ]
-			} );
-		}
-	}
 }

+ 16 - 15
packages/ckeditor5-block-quote/tests/blockquotecommand.js

@@ -27,14 +27,15 @@ describe( 'BlockQuoteCommand', () => {
 
 				model = editor.model;
 
-				model.schema.registerItem( 'paragraph', '$block' );
-				model.schema.registerItem( 'heading', '$block' );
-				model.schema.registerItem( 'widget' );
+				model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
+				model.schema.register( 'heading', { inheritAllFrom: '$block' } );
+				model.schema.register( 'widget' );
 
-				model.schema.allow( { name: 'widget', inside: '$root' } );
-				model.schema.allow( { name: '$text', inside: 'widget' } );
-
-				model.schema.limits.add( 'widget' );
+				model.schema.extend( 'widget', {
+					allowIn: '$root',
+					isLimit: true
+				} );
+				model.schema.extend( '$text', { allowIn: 'widget' } );
 
 				buildModelConverter().for( editor.editing.modelToView )
 					.fromElement( 'paragraph' )
@@ -75,7 +76,7 @@ describe( 'BlockQuoteCommand', () => {
 		} );
 
 		it( 'is false when selection starts in a blockless space', () => {
-			model.schema.allow( { name: '$text', inside: '$root' } );
+			model.schema.extend( '$text', { allowIn: '$root' } );
 
 			setModelData( model, 'x[]x' );
 
@@ -124,7 +125,7 @@ describe( 'BlockQuoteCommand', () => {
 			'is false when selection is in an element which cannot be wrapped with blockQuote' +
 			'(because mQ is not allowed in its parent)',
 			() => {
-				model.schema.disallow( { name: 'blockQuote', inside: '$root' } );
+				model.schema.dis.extend( 'blockQuote', { allowIn: '$root' } );
 
 				setModelData( model, '<paragraph>x[]x</paragraph>' );
 
@@ -371,8 +372,8 @@ describe( 'BlockQuoteCommand', () => {
 
 			it( 'should not wrap a block which can not be in a quote', () => {
 				// blockQuote is allowed in root, but fooBlock can not be inside blockQuote.
-				model.schema.registerItem( 'fooBlock', '$block' );
-				model.schema.disallow( { name: 'fooBlock', inside: 'blockQuote' } );
+				model.schema.register( 'fooBlock', { inheritAllFrom: '$block' } );
+				model.schema.dis.extend( 'fooBlock', { allowIn: 'blockQuote' } );
 				buildModelConverter().for( editor.editing.modelToView )
 					.fromElement( 'fooBlock' )
 					.toElement( 'fooblock' );
@@ -399,11 +400,11 @@ describe( 'BlockQuoteCommand', () => {
 
 			it( 'should not wrap a block which parent does not allow quote inside itself', () => {
 				// blockQuote is not be allowed in fooWrapper, but fooBlock can be inside blockQuote.
-				model.schema.registerItem( 'fooWrapper' );
-				model.schema.registerItem( 'fooBlock', '$block' );
+				model.schema.register( 'fooWrapper' );
+				model.schema.register( 'fooBlock', { inheritAllFrom: '$block' } );
 
-				model.schema.allow( { name: 'fooWrapper', inside: '$root' } );
-				model.schema.allow( { name: 'fooBlock', inside: 'fooWrapper' } );
+				model.schema.extend( 'fooWrapper', { allowIn: '$root' } );
+				model.schema.extend( 'fooBlock', { allowIn: 'fooWrapper' } );
 
 				buildModelConverter().for( editor.editing.modelToView )
 					.fromElement( 'fooWrapper' )