contextualtoolbar.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**
  2. * Copyright (c) 2016, CKSource - Frederico Knabben. All rights reserved.
  3. */
  4. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  5. import ContextualToolbar from '../../src/toolbar/contextualtoolbar';
  6. import ContextualBalloon from '../../src/contextualballoon';
  7. import ToolbarView from '../../src/toolbar/toolbarview';
  8. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  9. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  12. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model.js';
  13. /* global document, window, setTimeout */
  14. describe( 'ContextualToolbar', () => {
  15. let sandbox, editor, contextualToolbar, balloon, editorElement;
  16. beforeEach( () => {
  17. sandbox = sinon.sandbox.create();
  18. editorElement = document.createElement( 'div' );
  19. document.body.appendChild( editorElement );
  20. return ClassicTestEditor.create( editorElement, {
  21. plugins: [ Paragraph, Bold, Italic, ContextualToolbar ],
  22. contextualToolbar: [ 'bold', 'italic' ]
  23. } )
  24. .then( newEditor => {
  25. newEditor.editing.view.attachDomRoot( editorElement );
  26. editor = newEditor;
  27. contextualToolbar = editor.plugins.get( ContextualToolbar );
  28. balloon = editor.plugins.get( ContextualBalloon );
  29. // Focus the engine.
  30. editor.editing.view.isFocused = true;
  31. stubClientRects();
  32. } );
  33. } );
  34. afterEach( () => {
  35. sandbox.restore();
  36. editor.destroy();
  37. } );
  38. it( 'should be loaded', () => {
  39. expect( contextualToolbar ).to.instanceOf( ContextualToolbar );
  40. } );
  41. it( 'should load ContextualBalloon', () => {
  42. expect( balloon ).to.instanceof( ContextualBalloon );
  43. } );
  44. it( 'should create plugin instance with properties', () => {
  45. expect( contextualToolbar.toolbarView ).to.instanceof( ToolbarView );
  46. } );
  47. it( 'should create components from config', () => {
  48. expect( contextualToolbar.toolbarView.items ).to.length( 2 );
  49. } );
  50. it( 'should fire internal `_selectionChangeDone` event 200 ms after last selection change', () => {
  51. // This test uses setTimeout to test lodash#debounce because sinon fake timers
  52. // doesn't work with lodash. Lodash keeps time related stuff in a closure
  53. // and sinon is not able to override it.
  54. const spy = sandbox.spy();
  55. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  56. contextualToolbar.on( '_selectionChangeDone', spy );
  57. editor.document.selection.fire( 'change:range' );
  58. // Not yet.
  59. sinon.assert.notCalled( spy );
  60. // Lets wait 100 ms.
  61. setTimeout( () => {
  62. // Still not yet.
  63. sinon.assert.notCalled( spy );
  64. // Fire event one more time.
  65. editor.document.selection.fire( 'change:range' );
  66. // Another 100 ms waiting.
  67. setTimeout( () => {
  68. // Still not yet.
  69. sinon.assert.notCalled( spy );
  70. // Another 100 ms waiting.
  71. setTimeout( () => {
  72. // And here it is.
  73. sinon.assert.calledOnce( spy );
  74. }, 100 );
  75. }, 100 );
  76. }, 100 );
  77. } );
  78. it( 'should open when selection stops changing', () => {
  79. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  80. expect( balloon.visibleView ).to.null;
  81. contextualToolbar.fire( '_selectionChangeDone' );
  82. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  83. } );
  84. it( 'should close when selection starts changing', () => {
  85. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  86. contextualToolbar.fire( '_selectionChangeDone' );
  87. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  88. editor.document.selection.fire( 'change:range' );
  89. expect( balloon.visibleView ).to.null;
  90. } );
  91. it( 'should open below if the selection is forward', () => {
  92. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  93. contextualToolbar.fire( '_selectionChangeDone' );
  94. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  95. expect( balloon.view.top ).to.be.above( 310 );
  96. } );
  97. it( 'should open above if the selection is forward but panel stick out of the limiter element', () => {
  98. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  99. // Mock limiter rect.
  100. mockBoundingBox( document.body, {
  101. left: 0,
  102. width: 1000,
  103. top: 0,
  104. height: 310
  105. } );
  106. contextualToolbar.fire( '_selectionChangeDone' );
  107. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  108. expect( balloon.view.top ).to.be.below( 310 );
  109. } );
  110. it( 'should open above if the selection is backward', () => {
  111. setData( editor.document, '<paragraph>[bar]</paragraph>', { lastRangeBackward: true } );
  112. contextualToolbar.fire( '_selectionChangeDone' );
  113. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  114. expect( balloon.view.top ).to.be.below( 100 );
  115. } );
  116. it( 'should open below if the selection is backward but panel stick out of the limiter element', () => {
  117. setData( editor.document, '<paragraph>[bar]</paragraph>', { lastRangeBackward: true } );
  118. // Mock limiter rect.
  119. mockBoundingBox( document.body, {
  120. left: 0,
  121. width: 1000,
  122. top: 95,
  123. height: 905
  124. } );
  125. contextualToolbar.fire( '_selectionChangeDone' );
  126. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  127. expect( balloon.view.top ).to.be.above( 100 );
  128. } );
  129. it( 'should not open if the collapsed selection is moving', () => {
  130. setData( editor.document, '<paragraph>ba[]r</paragraph>' );
  131. editor.document.selection.fire( 'change:range' );
  132. contextualToolbar.fire( '_selectionChangeDone' );
  133. setData( editor.document, '<paragraph>b[]ar</paragraph>' );
  134. editor.document.selection.fire( 'change:range' );
  135. contextualToolbar.fire( '_selectionChangeDone' );
  136. expect( balloon.visibleView ).to.null;
  137. } );
  138. it( 'should hide if the editor loses focus', () => {
  139. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  140. editor.ui.focusTracker.isFocused = true;
  141. contextualToolbar.fire( '_selectionChangeDone' );
  142. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  143. editor.ui.focusTracker.isFocused = false;
  144. expect( balloon.visibleView ).to.null;
  145. } );
  146. it( 'should do nothing when panel is being added to balloon stack twice', () => {
  147. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  148. contextualToolbar.fire( '_selectionChangeDone' );
  149. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  150. expect( () => {
  151. contextualToolbar.fire( '_selectionChangeDone' );
  152. } ).to.not.throw();
  153. } );
  154. function stubClientRects() {
  155. const editingView = editor.editing.view;
  156. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  157. // Mock selection rect.
  158. sandbox.stub( editingView.domConverter, 'viewRangeToDom', ( ...args ) => {
  159. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  160. sandbox.stub( domRange, 'getClientRects', () => {
  161. return {
  162. length: 2,
  163. item: id => {
  164. if ( id === 0 ) {
  165. return {
  166. top: 100,
  167. height: 10,
  168. bottom: 110,
  169. left: 200,
  170. width: 50,
  171. right: 250
  172. };
  173. }
  174. return {
  175. top: 300,
  176. height: 10,
  177. bottom: 310,
  178. left: 400,
  179. width: 50,
  180. right: 450
  181. };
  182. }
  183. };
  184. } );
  185. return domRange;
  186. } );
  187. // Mock window rect.
  188. sandbox.stub( global, 'window', {
  189. innerWidth: 1000,
  190. innerHeight: 1000,
  191. scrollX: 0,
  192. scrollY: 0,
  193. getComputedStyle: el => {
  194. return window.getComputedStyle( el );
  195. }
  196. } );
  197. // Mock balloon rect.
  198. mockBoundingBox( balloon.view.element, {
  199. width: 150,
  200. height: 50
  201. } );
  202. }
  203. function mockBoundingBox( element, data ) {
  204. const boundingBox = Object.assign( {}, data );
  205. boundingBox.right = boundingBox.left + boundingBox.width;
  206. boundingBox.bottom = boundingBox.top + boundingBox.height;
  207. sandbox.stub( element, 'getBoundingClientRect' ).returns( boundingBox );
  208. }
  209. } );