inputcommand-integration.js 6.2 KB

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