input.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * @license Copyright (c) 2003-2020, 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/input
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import InputCommand from './inputcommand';
  10. import injectUnsafeKeystrokesHandling from './utils/injectunsafekeystrokeshandling';
  11. import injectTypingMutationsHandling from './utils/injecttypingmutationshandling';
  12. /**
  13. * Handles text input coming from the keyboard or other input methods.
  14. *
  15. * @extends module:core/plugin~Plugin
  16. */
  17. export default class Input extends Plugin {
  18. /**
  19. * @inheritDoc
  20. */
  21. static get pluginName() {
  22. return 'Input';
  23. }
  24. /**
  25. * @inheritDoc
  26. */
  27. init() {
  28. const editor = this.editor;
  29. // TODO The above default configuration value should be defined using editor.config.define() once it's fixed.
  30. const inputCommand = new InputCommand( editor, editor.config.get( 'typing.undoStep' ) || 20 );
  31. editor.commands.add( 'input', inputCommand );
  32. injectUnsafeKeystrokesHandling( editor );
  33. injectTypingMutationsHandling( editor );
  34. }
  35. /**
  36. * Checks batch if it is a result of user input - e.g. typing.
  37. *
  38. * const input = editor.plugins.get( 'Input' );
  39. *
  40. * editor.model.document.on( 'change:data', ( evt, batch ) => {
  41. * if ( input.isTyping( batch ) ) {
  42. * console.log( 'The user typed something...' );
  43. * }
  44. * } );
  45. *
  46. * **Note:** This method checks if the batch was created using {@link module:typing/inputcommand~InputCommand 'input'}
  47. * command as typing changes coming from user input are inserted to the document using that command.
  48. *
  49. * @param {module:engine/model/batch~Batch} batch A batch to check.
  50. * @returns {Boolean}
  51. */
  52. isInput( batch ) {
  53. const inputCommand = this.editor.commands.get( 'input' );
  54. return inputCommand._batches.has( batch );
  55. }
  56. }