inputcommand-integration.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 '../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( 'InputCommand integration', () => {
  18. let editor, 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. 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. editorElement.remove();
  37. return editor.destroy();
  38. } );
  39. function expectOutput( modelOutput, viewOutput ) {
  40. expect( getModelData( editor.document ) ).to.equal( modelOutput );
  41. expect( getViewData( viewDocument ) ).to.equal( viewOutput );
  42. }
  43. function simulateTyping( text ) {
  44. // While typing, every character is an atomic change.
  45. text.split( '' ).forEach( character => {
  46. editor.execute( 'input', {
  47. text: character
  48. } );
  49. } );
  50. }
  51. function simulateBatches( batches ) {
  52. // Use longer text at once in input command.
  53. batches.forEach( batch => {
  54. editor.execute( 'input', {
  55. text: batch
  56. } );
  57. } );
  58. }
  59. function setSelection( pathA, pathB ) {
  60. doc.selection.setRanges( [ new Range( new Position( doc.getRoot(), pathA ), new Position( doc.getRoot(), pathB ) ) ] );
  61. }
  62. describe( 'InputCommand integration', () => {
  63. it( 'resets the buffer on typing respecting typing.undoStep', () => {
  64. setModelData( doc, '<paragraph>0[]</paragraph>' );
  65. simulateTyping( '123456789' );
  66. expectOutput( '<paragraph>0123456789[]</paragraph>', '<p>0123456789{}</p>' );
  67. editor.execute( 'undo' );
  68. expectOutput( '<paragraph>0123456[]</paragraph>', '<p>0123456{}</p>' );
  69. editor.execute( 'undo' );
  70. expectOutput( '<paragraph>0123[]</paragraph>', '<p>0123{}</p>' );
  71. editor.execute( 'redo' );
  72. expectOutput( '<paragraph>0123456[]</paragraph>', '<p>0123456{}</p>' );
  73. } );
  74. it( 'resets the buffer on text insertion respecting typing.undoStep', () => {
  75. setModelData( doc, '<paragraph>0[]</paragraph>' );
  76. simulateBatches( [ '1234', '5', '678', '9' ] );
  77. expectOutput( '<paragraph>0123456789[]</paragraph>', '<p>0123456789{}</p>' );
  78. editor.execute( 'undo' );
  79. expectOutput( '<paragraph>012345678[]</paragraph>', '<p>012345678{}</p>' );
  80. editor.execute( 'undo' );
  81. expectOutput( '<paragraph>01234[]</paragraph>', '<p>01234{}</p>' );
  82. editor.execute( 'redo' );
  83. expectOutput( '<paragraph>012345678[]</paragraph>', '<p>012345678{}</p>' );
  84. } );
  85. it( 'resets the buffer when selection changes', () => {
  86. setModelData( doc, '<paragraph>Foo[] Bar</paragraph>' );
  87. setSelection( [ 0, 5 ], [ 0, 5 ] );
  88. simulateTyping( '1' );
  89. setSelection( [ 0, 7 ], [ 0, 8 ] );
  90. simulateTyping( '2' );
  91. expectOutput( '<paragraph>Foo B1a2[]</paragraph>', '<p>Foo B1a2{}</p>' );
  92. editor.execute( 'undo' );
  93. expectOutput( '<paragraph>Foo B1a[r]</paragraph>', '<p>Foo B1a{r}</p>' );
  94. editor.execute( 'undo' );
  95. expectOutput( '<paragraph>Foo B[]ar</paragraph>', '<p>Foo B{}ar</p>' );
  96. editor.execute( 'redo' );
  97. expectOutput( '<paragraph>Foo B1[]ar</paragraph>', '<p>Foo B1{}ar</p>' );
  98. } );
  99. it( 'resets the buffer when selection changes (with enter)', () => {
  100. setModelData( doc, '<paragraph>Foo[]Bar</paragraph>' );
  101. simulateTyping( '1' );
  102. editor.execute( 'enter' );
  103. setSelection( [ 1, 3 ], [ 1, 3 ] );
  104. simulateTyping( '2' );
  105. editor.execute( 'enter' );
  106. simulateTyping( 'Baz' );
  107. expectOutput( '<paragraph>Foo1</paragraph><paragraph>Bar2</paragraph><paragraph>Baz[]</paragraph>',
  108. '<p>Foo1</p><p>Bar2</p><p>Baz{}</p>' );
  109. editor.execute( 'undo' );
  110. expectOutput( '<paragraph>Foo1</paragraph><paragraph>Bar2</paragraph><paragraph>[]</paragraph>',
  111. '<p>Foo1</p><p>Bar2</p><p>[]</p>' );
  112. editor.execute( 'undo' );
  113. expectOutput( '<paragraph>Foo1</paragraph><paragraph>Bar2[]</paragraph>',
  114. '<p>Foo1</p><p>Bar2{}</p>' );
  115. editor.execute( 'undo' );
  116. expectOutput( '<paragraph>Foo1</paragraph><paragraph>Bar[]</paragraph>',
  117. '<p>Foo1</p><p>Bar{}</p>' );
  118. editor.execute( 'redo' );
  119. expectOutput( '<paragraph>Foo1</paragraph><paragraph>Bar2[]</paragraph>',
  120. '<p>Foo1</p><p>Bar2{}</p>' );
  121. } );
  122. it( 'resets the buffer when attribute changes', () => {
  123. setModelData( doc, '<paragraph>Foo[] Bar</paragraph>' );
  124. simulateTyping( ' ' );
  125. boldView.fire( 'execute' );
  126. simulateTyping( 'B' );
  127. italicView.fire( 'execute' );
  128. simulateTyping( 'a' );
  129. boldView.fire( 'execute' );
  130. italicView.fire( 'execute' );
  131. simulateTyping( 'z' );
  132. expectOutput(
  133. '<paragraph>Foo <$text bold="true">B<$text italic="true">a</$text></$text>z[] Bar</paragraph>',
  134. '<p>Foo <strong>B</strong><i><strong>a</strong></i>z{} Bar</p>'
  135. );
  136. editor.execute( 'undo' );
  137. expectOutput(
  138. '<paragraph>' +
  139. 'Foo <$text bold="true">B<$text italic="true">a[]</$text></$text> Bar' +
  140. '</paragraph>',
  141. '<p>Foo <strong>B</strong><i><strong>a{}</strong></i> Bar</p>'
  142. );
  143. editor.execute( 'undo' );
  144. expectOutput( '<paragraph>Foo <$text bold="true">B[]</$text> Bar</paragraph>',
  145. '<p>Foo <strong>B{}</strong> Bar</p>' );
  146. } );
  147. } );
  148. } );