8
0

contextualtoolbar.js 10 KB

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