enter.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 enter/enter
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import EnterCommand from './entercommand';
  10. import EnterObserver from './enterobserver';
  11. /**
  12. * This plugin handles the <kbd>Enter</kbd> key (hard line break) in the editor.
  13. *
  14. * See also the {@link module:enter/shiftenter~ShiftEnter} plugin.
  15. *
  16. * For more information about this feature see the {@glink api/enter package page}.
  17. *
  18. * @extends module:core/plugin~Plugin
  19. */
  20. export default class Enter extends Plugin {
  21. /**
  22. * @inheritDoc
  23. */
  24. static get pluginName() {
  25. return 'Enter';
  26. }
  27. init() {
  28. const editor = this.editor;
  29. const view = editor.editing.view;
  30. const viewDocument = view.document;
  31. view.addObserver( EnterObserver );
  32. editor.commands.add( 'enter', new EnterCommand( editor ) );
  33. this.listenTo( viewDocument, 'enter', ( evt, data ) => {
  34. data.preventDefault();
  35. // The soft enter key is handled by the ShiftEnter plugin.
  36. if ( data.isSoft ) {
  37. return;
  38. }
  39. editor.execute( 'enter' );
  40. view.scrollToTheSelection();
  41. }, { priority: 'low' } );
  42. }
  43. }