inputcommand-integration.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  10. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  11. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  12. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  13. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  14. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  15. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  16. import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  17. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  18. describe( 'InputCommand integration', () => {
  19. let editor, doc, viewDocument, boldView, italicView;
  20. beforeEach( () => {
  21. const editorElement = document.createElement( 'div' );
  22. document.body.appendChild( editorElement );
  23. return ClassicTestEditor.create( editorElement, {
  24. plugins: [ Typing, Paragraph, Undo, Bold, Italic, Enter ],
  25. typing: { undoStep: 3 }
  26. } )
  27. .then( newEditor => {
  28. editor = newEditor;
  29. doc = editor.document;
  30. viewDocument = editor.editing.view;
  31. boldView = editor.ui.componentFactory.create( 'bold' );
  32. italicView = editor.ui.componentFactory.create( 'italic' );
  33. } );
  34. } );
  35. afterEach( () => {
  36. return editor.destroy();
  37. } );
  38. function expectOutput( modelOutput, viewOutput ) {
  39. expect( getModelData( editor.document ) ).to.equal( modelOutput );
  40. expect( getViewData( viewDocument ) ).to.equal( viewOutput );
  41. }
  42. function simulateTyping( text ) {
  43. // While typing, every character is an atomic change.
  44. text.split( '' ).forEach( ( character ) => {
  45. editor.execute( 'input', {
  46. text: character
  47. } );
  48. } );
  49. }
  50. function simulateBatches( batches ) {
  51. // Use longer text at once in input command.
  52. batches.forEach( ( batch ) => {
  53. editor.execute( 'input', {
  54. text: batch
  55. } );
  56. } );
  57. }
  58. function setSelection( pathA, pathB ) {
  59. doc.selection.setRanges( [ new Range( new Position( doc.getRoot(), pathA ), new Position( doc.getRoot(), pathB ) ) ] );
  60. }
  61. describe( 'InputCommand integration', () => {
  62. it( 'resets the buffer on typing respecting typing.undoStep', () => {
  63. setModelData( doc, '<paragraph>0[]</paragraph>' );
  64. simulateTyping( '123456789' );
  65. expectOutput( '<paragraph>0123456789[]</paragraph>', '<p>0123456789{}</p>' );
  66. editor.execute( 'undo' );
  67. expectOutput( '<paragraph>0123456[]</paragraph>', '<p>0123456{}</p>' );
  68. editor.execute( 'undo' );
  69. expectOutput( '<paragraph>0123[]</paragraph>', '<p>0123{}</p>' );
  70. editor.execute( 'redo' );
  71. expectOutput( '<paragraph>0123456[]</paragraph>', '<p>0123456{}</p>' );
  72. } );
  73. it( 'resets the buffer on text insertion respecting typing.undoStep', () => {
  74. setModelData( doc, '<paragraph>0[]</paragraph>' );
  75. simulateBatches( [ '1234', '5', '678', '9' ] );
  76. expectOutput( '<paragraph>0123456789[]</paragraph>', '<p>0123456789{}</p>' );
  77. editor.execute( 'undo' );
  78. expectOutput( '<paragraph>012345678[]</paragraph>', '<p>012345678{}</p>' );
  79. editor.execute( 'undo' );
  80. expectOutput( '<paragraph>01234[]</paragraph>', '<p>01234{}</p>' );
  81. editor.execute( 'redo' );
  82. expectOutput( '<paragraph>012345678[]</paragraph>', '<p>012345678{}</p>' );
  83. } );
  84. it( 'resets the buffer when selection changes', () => {
  85. setModelData( doc, '<paragraph>Foo[] Bar</paragraph>' );
  86. setSelection( [ 0, 5 ], [ 0, 5 ] );
  87. simulateTyping( '1' );
  88. setSelection( [ 0, 7 ], [ 0, 8 ] );
  89. simulateTyping( '2' );
  90. expectOutput( '<paragraph>Foo B1a2[]</paragraph>', '<p>Foo B1a2{}</p>' );
  91. editor.execute( 'undo' );
  92. expectOutput( '<paragraph>Foo B1a[r]</paragraph>', '<p>Foo B1a{r}</p>' );
  93. editor.execute( 'undo' );
  94. expectOutput( '<paragraph>Foo B[]ar</paragraph>', '<p>Foo B{}ar</p>' );
  95. editor.execute( 'redo' );
  96. expectOutput( '<paragraph>Foo B1[]ar</paragraph>', '<p>Foo B1{}ar</p>' );
  97. } );
  98. it( 'resets the buffer when selection changes (with enter)', () => {
  99. setModelData( doc, '<paragraph>Foo[]Bar</paragraph>' );
  100. simulateTyping( '1' );
  101. editor.execute( 'enter' );
  102. setSelection( [ 1, 3 ], [ 1, 3 ] );
  103. simulateTyping( '2' );
  104. editor.execute( 'enter' );
  105. simulateTyping( 'Baz' );
  106. expectOutput( '<paragraph>Foo1</paragraph><paragraph>Bar2</paragraph><paragraph>Baz[]</paragraph>',
  107. '<p>Foo1</p><p>Bar2</p><p>Baz{}</p>' );
  108. editor.execute( 'undo' );
  109. expectOutput( '<paragraph>Foo1</paragraph><paragraph>Bar2</paragraph><paragraph>[]</paragraph>',
  110. '<p>Foo1</p><p>Bar2</p><p>[]</p>' );
  111. editor.execute( 'undo' );
  112. expectOutput( '<paragraph>Foo1</paragraph><paragraph>Bar2[]</paragraph>',
  113. '<p>Foo1</p><p>Bar2{}</p>' );
  114. editor.execute( 'undo' );
  115. expectOutput( '<paragraph>Foo1</paragraph><paragraph>Bar[]</paragraph>',
  116. '<p>Foo1</p><p>Bar{}</p>' );
  117. editor.execute( 'redo' );
  118. expectOutput( '<paragraph>Foo1</paragraph><paragraph>Bar2[]</paragraph>',
  119. '<p>Foo1</p><p>Bar2{}</p>' );
  120. } );
  121. it( 'resets the buffer when attribute changes', () => {
  122. setModelData( doc, '<paragraph>Foo[] Bar</paragraph>' );
  123. simulateTyping( ' ' );
  124. boldView.fire( 'execute' );
  125. simulateTyping( 'B' );
  126. italicView.fire( 'execute' );
  127. simulateTyping( 'a' );
  128. boldView.fire( 'execute' );
  129. italicView.fire( 'execute' );
  130. simulateTyping( 'z' );
  131. expectOutput( '<paragraph>Foo <$text bold="true">B</$text><$text italic="true"><$text bold="true">a</$text></$text>z[] Bar</paragraph>',
  132. '<p>Foo <strong>B</strong><em><strong>a</strong></em>z{} Bar</p>' );
  133. editor.execute( 'undo' );
  134. expectOutput( '<paragraph>Foo <$text bold="true">B</$text><$text italic="true"><$text bold="true">a</$text></$text><$text bold="true" italic="true">[]</$text> Bar</paragraph>',
  135. '<p>Foo <strong>B</strong><em><strong>a{}</strong></em> Bar</p>' );
  136. editor.execute( 'undo' );
  137. expectOutput( '<paragraph>Foo <$text bold="true">B[]</$text> Bar</paragraph>',
  138. '<p>Foo <strong>B{}</strong> Bar</p>' );
  139. } );
  140. } );
  141. } );