8
0
Просмотр исходного кода

Add support for preserving text attributes on enter.

Mateusz Samsel 6 лет назад
Родитель
Сommit
9f49b35da9

+ 11 - 0
packages/ckeditor5-enter/src/entercommand.js

@@ -56,7 +56,10 @@ function enterBlock( model, writer, selection, schema ) {
 	}
 
 	if ( isSelectionEmpty ) {
+		// List of text attributes copied to new line/block.
+		const filteredAttr = getAllowedAttributes( writer.model.schema, selection.getAttributes() );
 		splitBlock( writer, range.start );
+		writer.setSelectionAttribute( filteredAttr );
 	} else {
 		const leaveUnmerged = !( range.start.isAtStart && range.end.isAtEnd );
 		const isContainedWithinOneElement = ( startElement == endElement );
@@ -84,3 +87,11 @@ function splitBlock( writer, splitPos ) {
 	writer.split( splitPos );
 	writer.setSelection( splitPos.parent.nextSibling, 0 );
 }
+
+function* getAllowedAttributes( schema, allAttributes ) {
+	for ( const attr of allAttributes ) {
+		if ( attr && schema.getAttributeProperties( attr[ 0 ] ).copyOnEnter ) {
+			yield attr;
+		}
+	}
+}

+ 25 - 0
packages/ckeditor5-enter/tests/entercommand.js

@@ -87,6 +87,31 @@ describe( 'EnterCommand', () => {
 				'<p>x</p><p>[]</p><p>y</p>',
 				'<p>x</p><p></p><p>[]</p><p>y</p>'
 			);
+
+			describe( 'copyOnEnter', () => {
+				beforeEach( () => {
+					schema.extend( '$text', { allowAttributes: 'foo' } );
+					schema.setAttributeProperties( 'foo', { copyOnEnter: true } );
+				} );
+
+				test(
+					'allowed attributes are copied',
+					'<p><$text foo="true">test[]</$text></p>',
+					'<p><$text foo="true">test</$text></p><p selection:foo="true"><$text foo="true">[]</$text></p>'
+				);
+
+				test(
+					'unknown attributes are disabled',
+					'<p><$text bar="true">test[]</$text></p>',
+					'<p><$text bar="true">test</$text></p><p>[]</p>'
+				);
+
+				test(
+					'only allowed attributes are copied from mix set',
+					'<p><$text bar="true" foo="true">test[]</$text></p>',
+					'<p><$text bar="true" foo="true">test</$text></p><p selection:foo="true"><$text foo="true">[]</$text></p>'
+				);
+			} );
 		} );
 
 		describe( 'non-collapsed selection', () => {