Przeglądaj źródła

Add extra & include text transformation configuration options.

Maciej Gołaszewski 6 lat temu
rodzic
commit
e9eee92669

+ 19 - 7
packages/ckeditor5-typing/src/texttransformation.js

@@ -45,8 +45,11 @@ export default class TextTransformation extends Plugin {
 	constructor( editor ) {
 		super( editor );
 
-		editor.config.define( 'textTransformation', {
-			transformations: DEFAULT_TRANSFORMATIONS
+		editor.config.define( 'typing', {
+			transformation: {
+				include: DEFAULT_TRANSFORMATIONS,
+				extra: []
+			}
 		} );
 	}
 
@@ -64,9 +67,10 @@ export default class TextTransformation extends Plugin {
 		const editor = this.editor;
 		const model = editor.model;
 
-		const transformations = editor.config.get( 'textTransformation.transformations' );
+		const transformations = editor.config.get( 'typing.transformation.include' );
+		const extraTransformations = editor.config.get( 'typing.transformation.extra' );
 
-		for ( const transformation of transformations ) {
+		for ( const transformation of [ ...transformations, ...extraTransformations ] ) {
 			const { from, to } = transformation;
 
 			let testCallback;
@@ -130,7 +134,7 @@ function buildQuotesRegExp( quoteCharacter ) {
  *
  * Read more in {@link module:typing/texttransformation~TextTransformationConfig}.
  *
- * @member {module:typing/texttransformation~TextTransformationConfig} module:core/editor/editorconfig~EditorConfig#textTransformation
+ * @member {module:typing/texttransformation~TextTransformationConfig} module:typing/typing~TypingConfig#transformation
  */
 
 /**
@@ -138,7 +142,9 @@ function buildQuotesRegExp( quoteCharacter ) {
  *
  *		ClassicEditor
  *			.create( editorElement, {
- *				textTransformation: ... // Text transformation feature options.
+ *				typing: {
+ *					transformation:  ... // Text transformation feature options.
+ *				}
  *			} )
  *			.then( ... )
  *			.catch( ... );
@@ -152,6 +158,12 @@ function buildQuotesRegExp( quoteCharacter ) {
 /**
  * The default text transformations supported by the editor.
  *
- * @member {Array.<module:typing/texttransformation~TextTransformationDescription>} module:typing/texttransformation~TextTransformationConfig#transformations
+ * @member {Array.<module:typing/texttransformation~TextTransformationDescription>} module:typing/texttransformation~TextTransformationConfig#include
+ */
+
+/**
+ * The extra text transformations that are added to the transformations defined in {@link module:typing/texttransformation~TextTransformationConfig#inlcude}.
+ *
+ * @member {Array.<module:typing/texttransformation~TextTransformationDescription>} module:typing/texttransformation~TextTransformationConfig#extra
  */
 /* eslint-enable max-len */

+ 66 - 16
packages/ckeditor5-typing/tests/texttransformation.js

@@ -119,34 +119,32 @@ describe( 'Text transformation feature', () => {
 	describe( 'configuration', () => {
 		it( 'should allow adding own rules with string pattern', () => {
 			return createEditorInstance( {
-				textTransformation: {
-					transformations: [
-						{
-							from: 'CKS',
-							to: 'CKSource'
-						}
-					]
+				typing: {
+					transformation: {
+						extra: [
+							{ from: 'CKE', to: 'CKEditor' }
+						]
+					}
 				}
 			} ).then( () => {
 				setData( model, '<paragraph>[]</paragraph>' );
 
 				model.enqueueChange( model.createBatch(), writer => {
-					writer.insertText( 'CKS', doc.selection.focus );
+					writer.insertText( 'CKE', doc.selection.focus );
 				} );
 
-				expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKSource</paragraph>' );
+				expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor</paragraph>' );
 			} );
 		} );
 
 		it( 'should allow adding own rules with RegExp object', () => {
 			return createEditorInstance( {
-				textTransformation: {
-					transformations: [
-						{
-							from: /([a-z]+)@(example.com)$/,
-							to: '$1.at.$2'
-						}
-					]
+				typing: {
+					transformation: {
+						extra: [
+							{ from: /([a-z]+)@(example.com)$/, to: '$1.at.$2' }
+						]
+					}
 				}
 			} ).then( () => {
 				setData( model, '<paragraph>[]</paragraph>' );
@@ -158,6 +156,58 @@ describe( 'Text transformation feature', () => {
 				expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>user.at.example.com</paragraph>' );
 			} );
 		} );
+
+		it( 'should not alter include rules adding own rules as extra', () => {
+			return createEditorInstance( {
+				typing: {
+					transformation: {
+						extra: [
+							{ from: 'CKE', to: 'CKEditor' }
+						]
+					}
+				}
+			} ).then( () => {
+				setData( model, '<paragraph>[]</paragraph>' );
+
+				model.change( writer => {
+					writer.insertText( 'CKE', doc.selection.focus );
+				} );
+
+				expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor</paragraph>' );
+
+				model.change( writer => {
+					writer.insertText( '(tm)', doc.selection.focus );
+				} );
+
+				expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor™</paragraph>' );
+			} );
+		} );
+
+		it( 'should overwrite all rules when defining include rules', () => {
+			return createEditorInstance( {
+				typing: {
+					transformation: {
+						include: [
+							{ from: 'CKE', to: 'CKEditor' }
+						]
+					}
+				}
+			} ).then( () => {
+				setData( model, '<paragraph>[]</paragraph>' );
+
+				model.change( writer => {
+					writer.insertText( 'CKE', doc.selection.focus );
+				} );
+
+				expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor</paragraph>' );
+
+				model.change( writer => {
+					writer.insertText( '(tm)', doc.selection.focus );
+				} );
+
+				expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>CKEditor(tm)</paragraph>' );
+			} );
+		} );
 	} );
 
 	function createEditorInstance( additionalConfig = {} ) {