texttransformation.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module typing/texttransformation
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import TextWatcher from '@ckeditor/ckeditor5-utils/src/textwatcher';
  10. // Set of default transformations provided by the feature.
  11. const DEFAULT_TRANSFORMATIONS = [
  12. // Common symbols:
  13. { from: '(c)', to: '©' },
  14. { from: '(r)', to: '®' },
  15. { from: '(tm)', to: '™' },
  16. // Mathematical:
  17. { from: '1/2', to: '½' },
  18. { from: '<=', to: '≤' },
  19. // Typography:
  20. { from: '...', to: '…' },
  21. { from: ' -- ', to: ' – ' },
  22. { from: ' --- ', to: ' — ' },
  23. // Quotations:
  24. // English primary.
  25. { from: buildQuotesRegExp( '"' ), to: '$1“$2”' },
  26. // English secondary.
  27. { from: buildQuotesRegExp( '\'' ), to: '$1‘$2’' }
  28. ];
  29. /**
  30. * The text transformation plugin.
  31. *
  32. * @extends module:core/plugin~Plugin
  33. */
  34. export default class TextTransformation extends Plugin {
  35. /**
  36. * @inheritDoc
  37. */
  38. constructor( editor ) {
  39. super( editor );
  40. editor.config.define( 'textTransformation', {
  41. transformations: DEFAULT_TRANSFORMATIONS
  42. } );
  43. }
  44. /**
  45. * @inheritDoc
  46. */
  47. static get pluginName() {
  48. return 'TextTransformation';
  49. }
  50. /**
  51. * @inheritDoc
  52. */
  53. init() {
  54. const editor = this.editor;
  55. const model = editor.model;
  56. const transformations = editor.config.get( 'textTransformation.transformations' );
  57. for ( const transformation of transformations ) {
  58. const { from, to } = transformation;
  59. let testCallback;
  60. let textReplacer;
  61. if ( from instanceof RegExp ) {
  62. testCallback = text => from.test( text );
  63. textReplacer = message => message.replace( from, to );
  64. } else {
  65. testCallback = text => text.endsWith( from );
  66. textReplacer = message => message.slice( 0, message.length - from.length ) + to;
  67. }
  68. const watcher = new TextWatcher( editor.model, testCallback );
  69. watcher.on( 'matched:data', ( evt, data ) => {
  70. const selection = editor.model.document.selection;
  71. const focus = selection.focus;
  72. const textToReplaceLength = data.text.length;
  73. const textToInsert = textReplacer( data.text );
  74. model.enqueueChange( model.createBatch(), writer => {
  75. const replaceRange = writer.createRange( focus.getShiftedBy( -textToReplaceLength ), focus );
  76. model.insertContent( writer.createText( textToInsert, selection.getAttributes() ), replaceRange );
  77. } );
  78. } );
  79. }
  80. }
  81. }
  82. // Returns a RegExp pattern string that detects a sentence inside a quote.
  83. //
  84. // @param {String} quoteCharacter a character to creat a pattern for.
  85. // @returns {String}
  86. function buildQuotesRegExp( quoteCharacter ) {
  87. return new RegExp( `(^|\\s)${ quoteCharacter }([^${ quoteCharacter }]+)${ quoteCharacter }$` );
  88. }
  89. /**
  90. * Text transformation definition object.
  91. *
  92. * const transformations = [
  93. * // Will replace foo text before caret (ie typed) by user to bar:
  94. * { from: 'foo', to: 'bar' },
  95. *
  96. * // Will remove @ from emails on example.com domain, ie from user@example.com -> user.at.example.com:
  97. * { from: /([a-z-])@(example.com)$/i, to: '$1.at.$2' }
  98. * ]
  99. *
  100. * *Note*: The text watcher always evaluates end of an input - a text before a caret in single node. If passing a RegExp object you must
  101. * include `$` token to match end of string.
  102. *
  103. * @typedef {Object} module:typing/texttransformation~TextTransformationDescription
  104. * @property {String|RegExp} from The string or RegExp to transform.
  105. * @property {String} to The text to transform compatible with `String.replace()`
  106. */
  107. /**
  108. * The configuration of the {@link module:typing/texttransformation~TextTransformation} feature.
  109. *
  110. * Read more in {@link module:typing/texttransformation~TextTransformationConfig}.
  111. *
  112. * @member {module:typing/texttransformation~TextTransformationConfig} module:core/editor/editorconfig~EditorConfig#textTransformation
  113. */
  114. /**
  115. * The configuration of the text transformation feature.
  116. *
  117. * ClassicEditor
  118. * .create( editorElement, {
  119. * textTransformation: ... // Text transformation feature options.
  120. * } )
  121. * .then( ... )
  122. * .catch( ... );
  123. *
  124. * See {@link module:core/editor/editorconfig~EditorConfig all editor options}.
  125. *
  126. * @interface TextTransformationConfig
  127. */
  128. /* eslint-disable max-len */
  129. /**
  130. * The default text transformations supported by the editor.
  131. *
  132. * @member {Array.<module:typing/texttransformation~TextTransformationDescription>} module:typing/texttransformation~TextTransformationConfig#transformations
  133. */
  134. /* eslint-enable max-len */