contextualtoolbar.js 11 KB

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