Преглед изворни кода

Define text transformation configuration.

Maciej Gołaszewski пре 6 година
родитељ
комит
1a545c1a0d

+ 32 - 13
packages/ckeditor5-typing/src/texttransformation.js

@@ -4,7 +4,7 @@
  */
  */
 
 
 /**
 /**
- * @module text-transformation/texttransformation
+ * @module typing/texttransformation
  */
  */
 
 
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
@@ -27,21 +27,21 @@ export default class TextTransformation extends Plugin {
 		editor.config.define( 'textTransformation', {
 		editor.config.define( 'textTransformation', {
 			transformations: [
 			transformations: [
 				// TODO: not nice definition - needs RegExp escaping.
 				// TODO: not nice definition - needs RegExp escaping.
-				{ in: '\\(c\\)$', out: '©' },
-				{ in: '\\(tm\\)$', out: '™' },
-				{ in: '\\.\\.\\.$', out: '…' },
-				{ in: '1/2', out: '½' },
-				{ in: '<=', out: '≤' },
+				{ from: '\\(c\\)$', to: '©' },
+				{ from: '\\(tm\\)$', to: '™' },
+				{ from: '\\.\\.\\.$', to: '…' },
+				{ from: '1/2$', to: '½' },
+				{ from: '<=$', to: '≤' },
 
 
 				// TODO: not nice - needs special order.
 				// TODO: not nice - needs special order.
 				// TODO: not nice - needs spaces.
 				// TODO: not nice - needs spaces.
-				{ in: ' --- $', out: ' — ' },
-				{ in: ' -- $', out: ' – ' },
+				{ from: ' --- $', to: ' — ' },
+				{ from: ' -- $', to: ' – ' },
 
 
 				// English quotation - primary.
 				// English quotation - primary.
-				{ in: buildQuotesPattern( '"' ), out: '“$1”' },
+				{ from: buildQuotesPattern( '"' ), to: '“$1”' },
 				// English quotation - secondary.
 				// English quotation - secondary.
-				{ in: buildQuotesPattern( '\'' ), out: '‘$1’' }
+				{ from: buildQuotesPattern( '\'' ), to: '‘$1’' }
 			]
 			]
 		} );
 		} );
 	}
 	}
@@ -60,7 +60,8 @@ export default class TextTransformation extends Plugin {
 		const transformations = editor.config.get( 'textTransformation.transformations' );
 		const transformations = editor.config.get( 'textTransformation.transformations' );
 
 
 		for ( const transformation of transformations ) {
 		for ( const transformation of transformations ) {
-			const regExp = new RegExp( transformation.in, 'u' );
+			// TODO: always add $ to the string regexp!
+			const regExp = new RegExp( transformation.from, 'u' );
 
 
 			// setup text watcher
 			// setup text watcher
 			const watcher = new TextWatcher( editor, text => regExp.test( text ), text => {
 			const watcher = new TextWatcher( editor, text => regExp.test( text ), text => {
@@ -78,7 +79,7 @@ export default class TextTransformation extends Plugin {
 				const message = data.matched.text;
 				const message = data.matched.text;
 				const textToReplaceLength = message.length;
 				const textToReplaceLength = message.length;
 
 
-				const textToInsert = message.replace( regExp, transformation.out );
+				const textToInsert = message.replace( regExp, transformation.to );
 
 
 				// TODO: use model.insertContent()
 				// TODO: use model.insertContent()
 				model.enqueueChange( model.createBatch(), writer => {
 				model.enqueueChange( model.createBatch(), writer => {
@@ -100,6 +101,22 @@ function buildQuotesPattern( quoteCharacter ) {
 	return `${ quoteCharacter }([^${ quoteCharacter }]+)${ quoteCharacter }$`;
 	return `${ quoteCharacter }([^${ quoteCharacter }]+)${ quoteCharacter }$`;
 }
 }
 
 
+/**
+ * Text transformation definition object.
+ *
+ *		const transformations = [
+ *			// Will replace foo text before caret (ie typed) by user to bar:
+ *			{ from: 'foo', to: 'bar' },
+ *
+ *			// Will remove @ from emails on example.com domain, ie from user@example.com -> user.at.example.com
+ *			{ from: /([a-z-])@(example.com)$/, to: '$1.at.$2' }
+ *		]
+ *
+ * @typedef {Object} module:typing/texttransformation~TextTransformationDescription
+ * @property {String|RegExp} from The pattern to transform.
+ * @property {String} to The text to transform compatible with `String.replace()`
+ */
+
 /**
 /**
  * The configuration of the {@link module:typing/texttransformation~TextTransformation} feature.
  * The configuration of the {@link module:typing/texttransformation~TextTransformation} feature.
  *
  *
@@ -125,8 +142,10 @@ function buildQuotesPattern( quoteCharacter ) {
  * @interface TextTransformationConfig
  * @interface TextTransformationConfig
  */
  */
 
 
+/* eslint-disable max-len */
 /**
 /**
  * The default text transformations supported by the editor.
  * The default text transformations supported by the editor.
  *
  *
- * @member {*} module:typing/texttransformation~TextTransformationConfig#transformations
+ * @member {Array.<module:typing/texttransformation~TextTransformationDescription>} module:typing/texttransformation~TextTransformationConfig#transformations
  */
  */
+/* eslint-enable max-len */

+ 6 - 0
packages/ckeditor5-typing/tests/texttransformation.js

@@ -80,5 +80,11 @@ describe( 'Text transformation feature', () => {
 			} );
 			} );
 		}
 		}
 	} );
 	} );
+
+	describe( 'configuration', () => {
+		it( 'should allow adding own rules with string pattern' );
+
+		it( 'should allow adding own rules with RegExp object' );
+	} );
 } );
 } );