blockautoformatediting.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module autoformat/blockautoformatediting
  7. */
  8. import LiveRange from '@ckeditor/ckeditor5-engine/src/model/liverange';
  9. /**
  10. * The block autoformatting engine. It allows to format various block patterns. For example,
  11. * it can be configured to turn a paragraph starting with `*` and followed by a space into a list item.
  12. *
  13. * The autoformatting operation is integrated with the undo manager,
  14. * so the autoformatting step can be undone if the user's intention was not to format the text.
  15. *
  16. * See the constructors documentation to learn how to create custom inline autoformatters. You can also use
  17. * the {@link module:autoformat/autoformat~Autoformat} feature which enables a set of default autoformatters
  18. * (lists, headings, bold and italic).
  19. */
  20. export default class BlockAutoformatEditing {
  21. /**
  22. * Creates a listener triggered on `change` event in the document.
  23. * Calls the callback when inserted text matches the regular expression or the command name
  24. * if provided instead of the callback.
  25. *
  26. * Examples of usage:
  27. *
  28. * To convert a paragraph to heading 1 when `- ` is typed, using just the command name:
  29. *
  30. * new BlockAutoformatEditing( editor, /^\- $/, 'heading1' );
  31. *
  32. * To convert a paragraph to heading 1 when `- ` is typed, using just the callback:
  33. *
  34. * new BlockAutoformatEditing( editor, /^\- $/, ( context ) => {
  35. * const { match } = context;
  36. * const headingLevel = match[ 1 ].length;
  37. *
  38. * editor.execute( 'heading', {
  39. * formatId: `heading${ headingLevel }`
  40. * } );
  41. * } );
  42. *
  43. * @param {module:core/editor/editor~Editor} editor The editor instance.
  44. * @param {RegExp} pattern The regular expression to execute on just inserted text.
  45. * @param {Function|String} callbackOrCommand The callback to execute or the command to run when the text is matched.
  46. * In case of providing the callback, it receives the following parameter:
  47. * * {Object} match RegExp.exec() result of matching the pattern to inserted text.
  48. */
  49. constructor( editor, pattern, callbackOrCommand ) {
  50. let callback;
  51. let command = null;
  52. if ( typeof callbackOrCommand == 'function' ) {
  53. callback = callbackOrCommand;
  54. } else {
  55. // We assume that the actual command name was provided.
  56. command = editor.commands.get( callbackOrCommand );
  57. callback = () => {
  58. editor.execute( callbackOrCommand );
  59. };
  60. }
  61. editor.model.document.on( 'change', ( evt, batch ) => {
  62. if ( command && !command.isEnabled ) {
  63. return;
  64. }
  65. if ( batch.type == 'transparent' ) {
  66. return;
  67. }
  68. const changes = Array.from( editor.model.document.differ.getChanges() );
  69. const entry = changes[ 0 ];
  70. // Typing is represented by only a single change.
  71. if ( changes.length != 1 || entry.type !== 'insert' || entry.name != '$text' || entry.length != 1 ) {
  72. return;
  73. }
  74. const item = entry.position.textNode || entry.position.nodeAfter;
  75. if ( !item.parent.is( 'paragraph' ) ) {
  76. return;
  77. }
  78. const match = pattern.exec( item.data );
  79. if ( !match ) {
  80. return;
  81. }
  82. // Use enqueueChange to create new batch to separate typing batch from the auto-format changes.
  83. editor.model.enqueueChange( writer => {
  84. // Matched range.
  85. const start = writer.createPositionAt( item.parent, 0 );
  86. const end = writer.createPositionAt( item.parent, match[ 0 ].length );
  87. const range = new LiveRange( start, end );
  88. const wasChanged = callback( { match } );
  89. // Remove matched text.
  90. if ( wasChanged !== false ) {
  91. writer.remove( range );
  92. }
  93. range.detach();
  94. } );
  95. } );
  96. }
  97. }