8
0

contextualtoolbar.js 10 KB

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