contextualtoolbar.js 10 KB

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