8
0

shiftenter.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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. import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
  12. import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  13. /**
  14. * This plugin handles the <kbd>Shift</kbd>+<kbd>Enter</kbd> keystroke (soft line break) in the editor.
  15. *
  16. * See also the {@link module:enter/enter~Enter} plugin.
  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. } );
  37. // Configure converters.
  38. conversion.for( 'upcast' )
  39. .add( upcastElementToElement( {
  40. model: 'softBreak',
  41. view: 'br'
  42. } ) );
  43. conversion.for( 'downcast' )
  44. .add( downcastElementToElement( {
  45. model: 'softBreak',
  46. view: ( modelElement, viewWriter ) => viewWriter.createEmptyElement( 'br' )
  47. } ) );
  48. view.addObserver( EnterObserver );
  49. editor.commands.add( 'shiftEnter', new ShiftEnterCommand( editor ) );
  50. this.listenTo( viewDocument, 'enter', ( evt, data ) => {
  51. data.preventDefault();
  52. // The hard enter key is handled by the Enter plugin.
  53. if ( !data.isSoft ) {
  54. return;
  55. }
  56. editor.execute( 'shiftEnter' );
  57. view.scrollToTheSelection();
  58. }, { priority: 'low' } );
  59. }
  60. }