inputcommand-integration.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  15. describe( 'Typing – InputCommand integration', () => {
  16. let editor, model, doc, viewDocument, boldView, italicView, editorElement;
  17. beforeEach( () => {
  18. editorElement = document.createElement( 'div' );
  19. document.body.appendChild( editorElement );
  20. return ClassicTestEditor
  21. .create( editorElement, {
  22. plugins: [ Typing, Paragraph, Undo, Bold, Italic, Enter ],
  23. typing: { undoStep: 3 }
  24. } )
  25. .then( newEditor => {
  26. editor = newEditor;
  27. model = editor.model;
  28. doc = model.document;
  29. viewDocument = editor.editing.view;
  30. boldView = editor.ui.componentFactory.create( 'bold' );
  31. italicView = editor.ui.componentFactory.create( 'italic' );
  32. } );
  33. } );
  34. afterEach( () => {
  35. editorElement.remove();
  36. return editor.destroy();
  37. } );
  38. function expectOutput( modelOutput, viewOutput ) {
  39. expect( getModelData( model ) ).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. model.change( writer => {
  60. writer.setSelection( writer.createRange(
  61. writer.createPositionFromPath( doc.getRoot(), pathA ),
  62. writer.createPositionFromPath( doc.getRoot(), pathB )
  63. ) );
  64. } );
  65. }
  66. describe( 'InputCommand integration', () => {
  67. it( 'resets the buffer on typing respecting typing.undoStep', () => {
  68. setModelData( model, '<paragraph>0[]</paragraph>' );
  69. simulateTyping( '123456789' );
  70. expectOutput( '<paragraph>0123456789[]</paragraph>', '<p>0123456789{}</p>' );
  71. editor.execute( 'undo' );
  72. expectOutput( '<paragraph>0123456[]</paragraph>', '<p>0123456{}</p>' );
  73. editor.execute( 'undo' );
  74. expectOutput( '<paragraph>0123[]</paragraph>', '<p>0123{}</p>' );
  75. editor.execute( 'redo' );
  76. expectOutput( '<paragraph>0123456[]</paragraph>', '<p>0123456{}</p>' );
  77. } );
  78. it( 'resets the buffer on text insertion respecting typing.undoStep', () => {
  79. setModelData( model, '<paragraph>0[]</paragraph>' );
  80. simulateBatches( [ '1234', '5', '678', '9' ] );
  81. expectOutput( '<paragraph>0123456789[]</paragraph>', '<p>0123456789{}</p>' );
  82. editor.execute( 'undo' );
  83. expectOutput( '<paragraph>012345678[]</paragraph>', '<p>012345678{}</p>' );
  84. editor.execute( 'undo' );
  85. expectOutput( '<paragraph>01234[]</paragraph>', '<p>01234{}</p>' );
  86. editor.execute( 'redo' );
  87. expectOutput( '<paragraph>012345678[]</paragraph>', '<p>012345678{}</p>' );
  88. } );
  89. it( 'resets the buffer when selection changes', () => {
  90. setModelData( model, '<paragraph>Foo[] Bar</paragraph>' );
  91. setSelection( [ 0, 5 ], [ 0, 5 ] );
  92. simulateTyping( '1' );
  93. setSelection( [ 0, 7 ], [ 0, 8 ] );
  94. simulateTyping( '2' );
  95. expectOutput( '<paragraph>Foo B1a2[]</paragraph>', '<p>Foo B1a2{}</p>' );
  96. editor.execute( 'undo' );
  97. expectOutput( '<paragraph>Foo B1a[r]</paragraph>', '<p>Foo B1a{r}</p>' );
  98. editor.execute( 'undo' );
  99. expectOutput( '<paragraph>Foo B[]ar</paragraph>', '<p>Foo B{}ar</p>' );
  100. editor.execute( 'redo' );
  101. expectOutput( '<paragraph>Foo B1[]ar</paragraph>', '<p>Foo B1{}ar</p>' );
  102. } );
  103. it( 'resets the buffer when selection changes (with enter)', () => {
  104. setModelData( model, '<paragraph>Foo[]Bar</paragraph>' );
  105. simulateTyping( '1' );
  106. editor.execute( 'enter' );
  107. setSelection( [ 1, 3 ], [ 1, 3 ] );
  108. simulateTyping( '2' );
  109. editor.execute( 'enter' );
  110. simulateTyping( 'Baz' );
  111. expectOutput( '<paragraph>Foo1</paragraph><paragraph>Bar2</paragraph><paragraph>Baz[]</paragraph>',
  112. '<p>Foo1</p><p>Bar2</p><p>Baz{}</p>' );
  113. editor.execute( 'undo' );
  114. expectOutput( '<paragraph>Foo1</paragraph><paragraph>Bar2</paragraph><paragraph>[]</paragraph>',
  115. '<p>Foo1</p><p>Bar2</p><p>[]</p>' );
  116. editor.execute( 'undo' );
  117. expectOutput( '<paragraph>Foo1</paragraph><paragraph>Bar2[]</paragraph>',
  118. '<p>Foo1</p><p>Bar2{}</p>' );
  119. editor.execute( 'undo' );
  120. expectOutput( '<paragraph>Foo1</paragraph><paragraph>Bar[]</paragraph>',
  121. '<p>Foo1</p><p>Bar{}</p>' );
  122. editor.execute( 'redo' );
  123. expectOutput( '<paragraph>Foo1</paragraph><paragraph>Bar2[]</paragraph>',
  124. '<p>Foo1</p><p>Bar2{}</p>' );
  125. } );
  126. it( 'resets the buffer when attribute changes', () => {
  127. setModelData( model, '<paragraph>Foo[] Bar</paragraph>' );
  128. simulateTyping( ' ' );
  129. boldView.fire( 'execute' );
  130. simulateTyping( 'B' );
  131. italicView.fire( 'execute' );
  132. simulateTyping( 'a' );
  133. boldView.fire( 'execute' );
  134. italicView.fire( 'execute' );
  135. simulateTyping( 'z' );
  136. expectOutput(
  137. '<paragraph>Foo <$text bold="true">B</$text><$text bold="true" italic="true">a</$text>z[] Bar</paragraph>',
  138. '<p>Foo <strong>B</strong><i><strong>a</strong></i>z{} Bar</p>'
  139. );
  140. editor.execute( 'undo' );
  141. expectOutput(
  142. '<paragraph>Foo <$text bold="true">B</$text><$text bold="true" italic="true">a[]</$text> Bar</paragraph>',
  143. '<p>Foo <strong>B</strong><i><strong>a{}</strong></i> Bar</p>'
  144. );
  145. editor.execute( 'undo' );
  146. expectOutput( '<paragraph>Foo <$text bold="true">B[]</$text> Bar</paragraph>',
  147. '<p>Foo <strong>B{}</strong> Bar</p>' );
  148. } );
  149. } );
  150. } );