texttransformation.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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-mention/src/textwatcher';
  10. // Set of default transformations provided by the feature.
  11. const DEFAULT_TRANSFORMATIONS = [
  12. // Common symbols:
  13. { from: '\\(c\\)', to: '©' },
  14. { from: '\\(tm\\)', to: '™' },
  15. // Mathematical:
  16. { from: '1/2', to: '½' },
  17. { from: '<=', to: '≤' },
  18. // Typography:
  19. { from: '\\.\\.\\.', to: '…' },
  20. { from: ' -- ', to: ' – ' },
  21. { from: ' --- ', to: ' — ' },
  22. // Quotations:
  23. // English primary.
  24. { from: buildQuotesPattern( '"' ), to: '“$1”' },
  25. // English secondary.
  26. { from: buildQuotesPattern( '\'' ), to: '‘$1’' }
  27. ];
  28. /**
  29. * The text transformation plugin.
  30. *
  31. * For a detailed overview, check the {@glink features/text-transformation Text transformation feature documentation}.
  32. *
  33. * @extends module:core/plugin~Plugin
  34. */
  35. export default class TextTransformation extends Plugin {
  36. /**
  37. * @inheritDoc
  38. */
  39. constructor( editor ) {
  40. super( editor );
  41. editor.config.define( 'textTransformation', {
  42. transformations: DEFAULT_TRANSFORMATIONS
  43. } );
  44. }
  45. /**
  46. * @inheritDoc
  47. */
  48. static get pluginName() {
  49. return 'TextTransformation';
  50. }
  51. /**
  52. * @inheritDoc
  53. */
  54. init() {
  55. const editor = this.editor;
  56. const model = editor.model;
  57. const transformations = editor.config.get( 'textTransformation.transformations' );
  58. for ( const transformation of transformations ) {
  59. const { from, to } = transformation;
  60. const regExp = from instanceof RegExp ? from : new RegExp( `${ from }$`, 'u' );
  61. // setup text watcher
  62. const watcher = new TextWatcher( editor, text => regExp.test( text ), text => {
  63. const match = text.match( regExp );
  64. return {
  65. text: match[ 0 ],
  66. groups: match
  67. };
  68. } );
  69. watcher.on( 'matched', ( evt, data ) => {
  70. const selection = editor.model.document.selection;
  71. const focus = selection.focus;
  72. const message = data.matched.text;
  73. const textToReplaceLength = message.length;
  74. const textToInsert = message.replace( regExp, to );
  75. model.enqueueChange( model.createBatch(), writer => {
  76. const replaceRange = writer.createRange( focus.getShiftedBy( -textToReplaceLength ), focus );
  77. model.insertContent( writer.createText( textToInsert, selection.getAttributes() ), replaceRange );
  78. } );
  79. } );
  80. }
  81. }
  82. }
  83. // Returns a RegExp pattern string that detects a sentence inside a quote.
  84. //
  85. // @param {String} quoteCharacter a character to creat a pattern for.
  86. // @returns {String}
  87. function buildQuotesPattern( quoteCharacter ) {
  88. return `${ quoteCharacter }([^${ quoteCharacter }]+)${ quoteCharacter }$`;
  89. }
  90. /**
  91. * Text transformation definition object.
  92. *
  93. * const transformations = [
  94. * // Will replace foo text before caret (ie typed) by user to bar:
  95. * { from: 'foo', to: 'bar' },
  96. *
  97. * // Will remove @ from emails on example.com domain, ie from user@example.com -> user.at.example.com:
  98. * { from: /([a-z-])@(example.com)$/i, to: '$1.at.$2' }
  99. * ]
  100. *
  101. * *Note*: When passing a regex pattern as a string the end of string token (`$`) is always inserted.
  102. *
  103. * @typedef {Object} module:typing/texttransformation~TextTransformationDescription
  104. * @property {String|RegExp} from The pattern 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. * Read more about {@glink features/text-transformation#configuration configuring the text transformation feature}.
  118. *
  119. * ClassicEditor
  120. * .create( editorElement, {
  121. * textTransformation: ... // Text transformation feature options.
  122. * } )
  123. * .then( ... )
  124. * .catch( ... );
  125. *
  126. * See {@link module:core/editor/editorconfig~EditorConfig all editor options}.
  127. *
  128. * @interface TextTransformationConfig
  129. */
  130. /* eslint-disable max-len */
  131. /**
  132. * The default text transformations supported by the editor.
  133. *
  134. * @member {Array.<module:typing/texttransformation~TextTransformationDescription>} module:typing/texttransformation~TextTransformationConfig#transformations
  135. */
  136. /* eslint-enable max-len */