{@snippet features/build-text-transformation-source}
The {@link module:typing/texttransformation~TextTransformation} feature brings support for automatically turning predefined snippets into their improved forms. For instance:
| from | to |
|---|---|
| (tm) | ™ |
| 1/2 | ½ |
| -> | → |
| -- | – |
| "foo" | “foo” |
This feature comes pre-configured with a set of the most popular transformations. You can, however, disable existing ones or add your own ones.
While, most often this feature is used to allow easily inserting characters which are not present on your keyboard, it can also be used to achieve other goals. For instance, you can improve users' productivity by configuring it to expand some abbreviations (e.g. team or company names) into their full forms.
Type snippets such as (tm), 1/2, ->, --, "foo" and see how they get automatically transformed into their nicer typographically forms.
{@snippet features/text-transformation}
In addition to enabling automatic text transformations, you may want to check the following productivity features:
This feature comes pre-configured with a set of transformations. You can find the list of them in {@link module:typing/texttransformation~TextTransformationConfig} documentation.
By using the below defined options you can extend, limit or override this list:
typing.transformations.include} — allows overriding the default configuration. When overriding the default configuration you can reuse the predefined transformations (by using their names, which you can find in the {@link module:typing/texttransformation~TextTransformationConfig} documentation) and write your own transformationstyping.transformations.remove} — allows disabling predefined transformations.typing.transformations.extra} — allows disabling predefined transformations. You can find the names of the predefined transformations in the {@link module:typing/texttransformation~TextTransformationConfig} documentation.transformations.includeFor instance, in order to use only the transformations from the "quotes" and "typography" groups and in order to turn CKE into CKEditor, you can use the transformations.include property like this:
ClassicEditor
.create( editorElement, {
typing: {
transformations: {
include: [
// Use only the 'quotes' and 'typography' groups.
'quotes',
'typography',
// Plus, some custom transformation.
{ from: 'CKE', to: 'CKEditor' }
],
}
}
} )
.then( ... )
.catch( ... );
transformations.remove and extraAnother example, removing a couple of transformations and adding a couple of extra ones:
ClassicEditor
.create( editorElement, {
typing: {
transformations: {
remove: [
// Don't use the transformations from the
// 'symbols' and 'quotes' groups.
'symbols',
'quotes',
// As well as the following transformations.
'arrowLeft',
'arrowRight'
],
extra: [
// Add some custom transformations – e.g. for emojis.
{ from: ':)', to: '🙂' },
{ from: ':+1:', to: '👍' },
{ from: ':tada:', to: '🎉' },
// You can also define patterns using regexp.
// Note: the pattern must end with `$` and all its fragments must be wrapped
// with capturing groups.
// The following rule replaces ` "foo"` with ` «foo»`.
{
from: /(^|\s)(")([^"]*)(")$/,
to: [ null, '«', null, '»' ]
},
// Finally, you can define `to` as a callback.
// This (naive) rule will auto-capitalize first word after a period.
{
from: /(\. )([a-z])$/,
to: matches => [ null, matches[ 1 ].toUpperCase() ]
}
],
}
}
} )
.then( ... )
.catch( ... );
You can read more about the format of transformation rules in {@link module:typing/texttransformation~TextTransformationDescription}.
Test the rules defined above:
{@snippet features/text-transformation-extended}
To add this feature to your rich-text editor, install the @ckeditor/ckeditor5-typing package:
npm install --save @ckeditor/ckeditor5-typing
And add it to your plugin list configuration:
import TextTransformation from '@ckeditor/ckeditor5-typing/src/texttransformation';
ClassicEditor
.create( document.querySelector( '#editor' ), {
plugins: [ TextTransformation, ... ],
} )
.then( ... )
.catch( ... );
Read more about {@link builds/guides/integration/installing-plugins installing plugins}.
The source code of the feature is available on GitHub in https://github.com/ckeditor/ckeditor5-typing.