shiftenter.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 enter/shiftenter
  7. */
  8. import ShiftEnterCommand from './shiftentercommand';
  9. import EnterObserver from './enterobserver';
  10. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  11. /**
  12. * This plugin handles the <kbd>Shift</kbd>+<kbd>Enter</kbd> keystroke (soft line break) in the editor.
  13. *
  14. * See also the {@link module:enter/enter~Enter} 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 ShiftEnter extends Plugin {
  21. /**
  22. * @inheritDoc
  23. */
  24. static get pluginName() {
  25. return 'ShiftEnter';
  26. }
  27. init() {
  28. const editor = this.editor;
  29. const schema = editor.model.schema;
  30. const conversion = editor.conversion;
  31. const view = editor.editing.view;
  32. const viewDocument = view.document;
  33. // Configure the schema.
  34. schema.register( 'softBreak', {
  35. allowWhere: '$text',
  36. isInline: true
  37. } );
  38. // Configure converters.
  39. conversion.for( 'upcast' )
  40. .elementToElement( {
  41. model: 'softBreak',
  42. view: 'br'
  43. } );
  44. conversion.for( 'downcast' )
  45. .elementToElement( {
  46. model: 'softBreak',
  47. view: ( modelElement, viewWriter ) => viewWriter.createEmptyElement( 'br' )
  48. } );
  49. view.addObserver( EnterObserver );
  50. editor.commands.add( 'shiftEnter', new ShiftEnterCommand( editor ) );
  51. this.listenTo( viewDocument, 'enter', ( evt, data ) => {
  52. data.preventDefault();
  53. // The hard enter key is handled by the Enter plugin.
  54. if ( !data.isSoft ) {
  55. return;
  56. }
  57. editor.execute( 'shiftEnter' );
  58. view.scrollToTheSelection();
  59. }, { priority: 'low' } );
  60. }
  61. }