contextualtoolbar.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 ViewSelection from '@ckeditor/ckeditor5-engine/src/view/selection';
  7. import ToolbarView from '@ckeditor/ckeditor5-ui/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 */
  14. describe( 'ContextualToolbar', () => {
  15. let sandbox, editor, contextualToolbar, balloon, editorElement;
  16. beforeEach( () => {
  17. editorElement = document.createElement( 'div' );
  18. document.body.appendChild( editorElement );
  19. sandbox = sinon.sandbox.create();
  20. return ClassicTestEditor.create( editorElement, {
  21. plugins: [ Paragraph, Bold, Italic ]
  22. } ).then( newEditor => {
  23. editor = newEditor;
  24. editor.editing.view.attachDomRoot( editorElement );
  25. balloon = editor.ui.balloon;
  26. contextualToolbar = new ContextualToolbar( editor );
  27. editor.editing.view.isFocused = true;
  28. } );
  29. } );
  30. afterEach( () => {
  31. editor.destroy();
  32. sandbox.restore();
  33. } );
  34. it( 'should open below if the selection is forward', () => {
  35. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  36. stubClientRects();
  37. editor.editing.view.fire( 'selectionChangeDone' );
  38. expect( balloon.visible.view ).to.equal( contextualToolbar._toolbarView );
  39. expect( balloon.view.top ).to.be.above( 310 );
  40. } );
  41. it( 'should open above if the selection is forward but panel stick out of the limiter element', () => {
  42. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  43. stubClientRects();
  44. // Mock limiter rect.
  45. mockBoundingBox( document.body, {
  46. left: 0,
  47. width: 1000,
  48. top: 0,
  49. height: 310
  50. } );
  51. editor.editing.view.fire( 'selectionChangeDone' );
  52. expect( balloon.visible.view ).to.equal( contextualToolbar._toolbarView );
  53. expect( balloon.view.top ).to.be.below( 310 );
  54. } );
  55. it( 'should open above if the selection is backward', () => {
  56. setData( editor.document, '<paragraph>[bar]</paragraph>', { lastRangeBackward: true } );
  57. stubClientRects();
  58. editor.editing.view.fire( 'selectionChangeDone' );
  59. expect( balloon.visible.view ).to.equal( contextualToolbar._toolbarView );
  60. expect( balloon.view.top ).to.be.below( 100 );
  61. } );
  62. it( 'should open below if the selection is backward but panel stick out of the limiter element', () => {
  63. setData( editor.document, '<paragraph>[bar]</paragraph>', { lastRangeBackward: true } );
  64. stubClientRects();
  65. // Mock limiter rect.
  66. mockBoundingBox( document.body, {
  67. left: 0,
  68. width: 1000,
  69. top: 95,
  70. height: 905
  71. } );
  72. editor.editing.view.fire( 'selectionChangeDone' );
  73. expect( balloon.visible.view ).to.equal( contextualToolbar._toolbarView );
  74. expect( balloon.view.top ).to.be.above( 100 );
  75. } );
  76. it( 'should not open if selection is collapsed and is moving', () => {
  77. setData( editor.document, '<paragraph>ba[]r</paragraph>' );
  78. const oldSelection = editor.editing.view.selection;
  79. const newSelection = new ViewSelection();
  80. editor.editing.view.fire( 'selectionChange', { oldSelection, newSelection } );
  81. editor.editing.view.fire( 'selectionChangeDone' );
  82. setData( editor.document, '<paragraph>b[]ar</paragraph>' );
  83. editor.editing.view.fire( 'selectionChange', { oldSelection, newSelection } );
  84. editor.editing.view.fire( 'selectionChangeDone' );
  85. expect( balloon.visible ).to.null;
  86. } );
  87. it( 'should hide if editor loses focus', () => {
  88. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  89. editor.ui.focusTracker.isFocused = true;
  90. stubClientRects();
  91. editor.editing.view.fire( 'selectionChangeDone' );
  92. expect( balloon.visible.view ).to.equal( contextualToolbar._toolbarView );
  93. editor.ui.focusTracker.isFocused = false;
  94. expect( balloon.visible ).to.null;
  95. } );
  96. it( 'should hide if selection is changing', () => {
  97. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  98. stubClientRects();
  99. editor.editing.view.fire( 'selectionChangeDone' );
  100. expect( balloon.visible.view ).to.equal( contextualToolbar._toolbarView );
  101. const oldSelection = editor.editing.view.selection;
  102. const newSelection = new ViewSelection();
  103. editor.editing.view.fire( 'selectionChange', { oldSelection, newSelection } );
  104. expect( balloon.visible ).to.null;
  105. } );
  106. it( 'should do nothing when panel is being added to balloon stack twice', () => {
  107. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  108. editor.editing.view.fire( 'selectionChangeDone' );
  109. expect( balloon.visible.view ).to.equal( contextualToolbar._toolbarView );
  110. expect( () => {
  111. editor.editing.view.fire( 'selectionChangeDone' );
  112. } ).to.not.throw();
  113. } );
  114. it( 'should update toolbar position when is visible and editor content has changed', () => {
  115. const spy = sandbox.spy( balloon, 'updatePosition' );
  116. setData( editor.document, '<paragraph>ba[r]</paragraph>' );
  117. editor.editing.view.fire( 'selectionChangeDone' );
  118. sinon.assert.notCalled( spy );
  119. editor.editing.view.fire( 'render' );
  120. sinon.assert.calledOnce( spy );
  121. } );
  122. it( 'should not update toolbar position when is added to the balloon stack but is not visible and editor content has changed', () => {
  123. const spy = sandbox.spy( balloon, 'updatePosition' );
  124. setData( editor.document, '<paragraph>ba[r]</paragraph>' );
  125. editor.editing.view.fire( 'selectionChangeDone' );
  126. const viewMock = {};
  127. balloon.add( { view: viewMock } );
  128. editor.editing.view.fire( 'render' );
  129. sinon.assert.notCalled( spy );
  130. } );
  131. it( 'should not update toolbar position when is added to the balloon stack but is not visible and editor content has changed', () => {
  132. const spy = sandbox.spy( balloon, 'updatePosition' );
  133. setData( editor.document, '<paragraph>ba[r]</paragraph>' );
  134. editor.editing.view.fire( 'selectionChangeDone' );
  135. const viewMock = {};
  136. balloon.add( { view: viewMock } );
  137. editor.editing.view.fire( 'render' );
  138. sinon.assert.notCalled( spy );
  139. } );
  140. describe( 'addComponents()', () => {
  141. it( 'should adds given components as a toolbar content', () => {
  142. contextualToolbar = new ContextualToolbar( editor );
  143. expect( contextualToolbar._toolbarView ).to.instanceof( ToolbarView );
  144. expect( contextualToolbar._toolbarView.items.length ).to.equal( 0 );
  145. contextualToolbar.addComponents( [ 'bold', 'italic' ] );
  146. expect( contextualToolbar._toolbarView.items.length ).to.equal( 2 );
  147. } );
  148. } );
  149. function stubClientRects() {
  150. const editingView = editor.editing.view;
  151. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  152. // Mock selection rect.
  153. sandbox.stub( editingView.domConverter, 'viewRangeToDom', ( ...args ) => {
  154. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  155. sandbox.stub( domRange, 'getClientRects', () => {
  156. return {
  157. length: 2,
  158. item: id => {
  159. if ( id === 0 ) {
  160. return {
  161. top: 100,
  162. height: 10,
  163. bottom: 110,
  164. left: 200,
  165. width: 50,
  166. right: 250
  167. };
  168. }
  169. return {
  170. top: 300,
  171. height: 10,
  172. bottom: 310,
  173. left: 400,
  174. width: 50,
  175. right: 450
  176. };
  177. }
  178. };
  179. } );
  180. return domRange;
  181. } );
  182. // Mock window rect.
  183. sandbox.stub( global, 'window', {
  184. innerWidth: 1000,
  185. innerHeight: 1000,
  186. scrollX: 0,
  187. scrollY: 0,
  188. getComputedStyle: el => {
  189. return window.getComputedStyle( el );
  190. }
  191. } );
  192. // Mock balloon rect.
  193. mockBoundingBox( balloon.view.element, {
  194. width: 150,
  195. height: 50
  196. } );
  197. }
  198. function mockBoundingBox( element, data ) {
  199. const boundingBox = Object.assign( {}, data );
  200. boundingBox.right = boundingBox.left + boundingBox.width;
  201. boundingBox.bottom = boundingBox.top + boundingBox.height;
  202. sandbox.stub( element, 'getBoundingClientRect' ).returns( boundingBox );
  203. }
  204. } );