Browse Source

Add support for copyOnEnter for shiftenter command.

Mateusz Samsel 6 years ago
parent
commit
df19f62956

+ 1 - 8
packages/ckeditor5-enter/src/entercommand.js

@@ -8,6 +8,7 @@
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
+import { getCopyOnEnterAttributes } from './utils';
 
 /**
  * Enter command. It is used by the {@link module:enter/enter~Enter Enter feature} to handle the <kbd>Enter</kbd> key.
@@ -86,11 +87,3 @@ function splitBlock( writer, splitPos ) {
 	writer.split( splitPos );
 	writer.setSelection( splitPos.parent.nextSibling, 0 );
 }
-
-function* getCopyOnEnterAttributes( schema, allAttributes ) {
-	for ( const attribute of allAttributes ) {
-		if ( attribute && schema.getAttributeProperties( attribute[ 0 ] ).copyOnEnter ) {
-			yield attribute;
-		}
-	}
-}

+ 3 - 0
packages/ckeditor5-enter/src/shiftentercommand.js

@@ -8,6 +8,7 @@
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
+import { getCopyOnEnterAttributes } from './utils';
 
 /**
  * ShiftEnter command. It is used by the {@link module:enter/shiftenter~ShiftEnter ShiftEnter feature} to handle
@@ -81,7 +82,9 @@ function softBreakAction( model, writer, selection ) {
 	const isContainedWithinOneElement = ( startElement == endElement );
 
 	if ( isSelectionEmpty ) {
+		const attributesToCopy = getCopyOnEnterAttributes( model.schema, selection.getAttributes() );
 		insertBreak( writer, range.end );
+		writer.setSelectionAttribute( attributesToCopy );
 	} else {
 		const leaveUnmerged = !( range.start.isAtStart && range.end.isAtEnd );
 		model.deleteContent( selection, { leaveUnmerged } );

+ 25 - 0
packages/ckeditor5-enter/src/utils.js

@@ -0,0 +1,25 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/**
+ * @module enter/utils
+ */
+
+/**
+ * Returns iterator which has filtered attributes read from {@link module:engine/model/selection~Selection#getAttributes selection}.
+ * Filtering is realized based on `copyOnEnter` attribute property. Read more about attribute properties
+ * {@link module:engine/model/schema~Schema#setAttributeProperties here}.
+ *
+ * @param {module:engine/model/schema~Schema} schema
+ * @param {Iterable.<*>} allAttributes iterator with attributes of current selection.
+ * @returns {Iterable.<*>} filtered attributes which has `copyOnEnter` property.
+ */
+export function* getCopyOnEnterAttributes( schema, allAttributes ) {
+	for ( const attribute of allAttributes ) {
+		if ( attribute && schema.getAttributeProperties( attribute[ 0 ] ).copyOnEnter ) {
+			yield attribute;
+		}
+	}
+}