8
0

autoformat.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import BlockAutoformatEngine from './blockautoformatengine.js';
  6. import InlineAutoformatEngine from './inlineautoformatengine.js';
  7. import Feature from '../core/feature.js';
  8. import HeadingEngine from '../heading/headingengine.js';
  9. import ListEngine from '../list/listengine.js';
  10. import BoldEngine from '../basic-styles/boldengine.js';
  11. import ItalicEngine from '../basic-styles/italicengine.js';
  12. /**
  13. * Includes set of predefined Autoformatting actions:
  14. * * Bulleted list,
  15. * * Numbered list,
  16. * * Headings.
  17. *
  18. * @memberOf autoformat
  19. * @extends core.Feature
  20. */
  21. export default class Autoformat extends Feature {
  22. /**
  23. * @inheritDoc
  24. */
  25. static get requires() {
  26. return [ HeadingEngine, ListEngine, BoldEngine, ItalicEngine ];
  27. }
  28. /**
  29. * @inheritDoc
  30. */
  31. init() {
  32. this._addListAutoformats();
  33. this._addHeadingAutoformats();
  34. this._addInlineAutoformats();
  35. }
  36. /**
  37. * Adds autoformats related to ListEngine commands.
  38. *
  39. * When typed:
  40. *
  41. * `* ` or `- `
  42. * Paragraph will be changed to a bulleted list.
  43. *
  44. * `1. ` or `1) `
  45. * Paragraph will be changed to a numbered list (1 can be any digit or list of digits).
  46. *
  47. * @private
  48. */
  49. _addListAutoformats() {
  50. // Add support for __foo__ and _foo_
  51. new BlockAutoformatEngine( this.editor, /^[\*\-]\s$/, 'bulletedList' );
  52. new BlockAutoformatEngine( this.editor, /^\d+[\.|)]?\s$/, 'numberedList' );
  53. }
  54. /**
  55. * Adds autoformats related to HeadingEngine commands.
  56. *
  57. * When typed:
  58. *
  59. * `#` or `##` or `###`
  60. * Paragraph will be changed to a corresponding heading level.
  61. *
  62. * @private
  63. */
  64. _addHeadingAutoformats() {
  65. new BlockAutoformatEngine( this.editor, /^(#{1,3})\s$/, ( context ) => {
  66. const { batch, match } = context;
  67. const headingLevel = match[ 1 ].length;
  68. this.editor.execute( 'heading', {
  69. batch,
  70. formatId: `heading${ headingLevel }`
  71. } );
  72. } );
  73. }
  74. /**
  75. * Adds inline autoformatting capabilities to the editor.
  76. *
  77. * When typed:
  78. *
  79. * `**foobar**`
  80. * The `**` characters are removed, and `foobar` is set to bold.
  81. * `*foobar*`
  82. * The `*` characters are removed, and `foobar` is set to italic.
  83. *
  84. * @private
  85. */
  86. _addInlineAutoformats() {
  87. // Bold text between `**`, e.g. `**text to bold**`.
  88. new InlineAutoformatEngine( this.editor, /(\*\*)(.+?)(\*\*)$/g, 'bold' );
  89. new InlineAutoformatEngine( this.editor, /(?:[^\*])+(\*)([^\*]+)(\*)$/g, 'italic' );
  90. // Italicize text between `*`, e.g. `*text to italicize*`.
  91. // Slightly more complicated because of the clashing with the Bold autoformat.
  92. // Won't work for text shorter than 3 characters.
  93. // new InlineAutoformatEngine(
  94. // this.editor,
  95. // ( text ) => {
  96. // // For a text: 'Brown *fox* jumps over the lazy dog' the expression below will return following values:
  97. // //
  98. // // [0]: ' *fox* ',
  99. // // [1]: ' ',
  100. // // [2]: '*fox*',
  101. // // [index]: 5
  102. // //
  103. // // Value at index 1 is a "prefix". It can be empty, if the matched word is at the
  104. // // beginning of the line. Length of the prefix is used to calculate `start` index.
  105. // const pattern = /(?:[^\*]|^)(\*[^\*].+?[^\*]\*)(?:[^\*]|$)/g;
  106. //
  107. // let result;
  108. // let remove = [];
  109. // let format = [];
  110. //
  111. // while ( ( result = pattern.exec( text ) ) !== null ) {
  112. // // Add "prefix" length.
  113. // const start = result.index + result[ 1 ].length;
  114. // const fullMatchLen = result[ 2 ].length;
  115. // const delimiterLen = 1; // Length of '*'.
  116. //
  117. // const delStart = [
  118. // start,
  119. // start + delimiterLen
  120. // ];
  121. // const delEnd = [
  122. // start + fullMatchLen - delimiterLen,
  123. // start + fullMatchLen
  124. // ];
  125. //
  126. // remove.push( delStart );
  127. // remove.push( delEnd );
  128. //
  129. // // Calculation of offsets after deletion is not needed.
  130. // format.push( [ start + delimiterLen, start + fullMatchLen - delimiterLen ] );
  131. // }
  132. //
  133. // return {
  134. // remove,
  135. // format
  136. // };
  137. // },
  138. // 'italic'
  139. // );
  140. }
  141. }