8
0

contextualtoolbar.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. // Focus the engine.
  31. editor.editing.view.isFocused = true;
  32. stubClientRects();
  33. } );
  34. } );
  35. afterEach( () => {
  36. sandbox.restore();
  37. return editor.destroy();
  38. } );
  39. it( 'should create a plugin instance', () => {
  40. expect( contextualToolbar ).to.instanceOf( Plugin );
  41. expect( contextualToolbar ).to.instanceOf( ContextualToolbar );
  42. } );
  43. it( 'should load ContextualBalloon', () => {
  44. expect( balloon ).to.instanceof( ContextualBalloon );
  45. } );
  46. it( 'should create plugin instance with properties', () => {
  47. expect( contextualToolbar.toolbarView ).to.instanceof( ToolbarView );
  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 100 ms waiting.
  73. setTimeout( () => {
  74. // And here it is.
  75. sinon.assert.calledOnce( spy );
  76. done();
  77. }, 100 );
  78. }, 100 );
  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. // Mock limiter.
  113. mockBoundingBox( document.body, {
  114. left: 0,
  115. width: 1000,
  116. top: 0,
  117. height: 1000
  118. } );
  119. contextualToolbar.fire( '_selectionChangeDone' );
  120. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  121. expect( balloon.view.top ).to.be.above( 310 );
  122. } );
  123. it( 'should open above if the selection is forward but panel stick out of the limiter element', () => {
  124. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  125. // Mock limiter rect.
  126. mockBoundingBox( document.body, {
  127. left: 0,
  128. width: 1000,
  129. top: 0,
  130. height: 310
  131. } );
  132. contextualToolbar.fire( '_selectionChangeDone' );
  133. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  134. expect( balloon.view.top ).to.be.below( 310 );
  135. } );
  136. it( 'should open above if the selection is backward', () => {
  137. setData( editor.document, '<paragraph>[bar]</paragraph>', { lastRangeBackward: true } );
  138. // Mock limiter.
  139. mockBoundingBox( document.body, {
  140. left: 0,
  141. width: 1000,
  142. top: 0,
  143. height: 1000
  144. } );
  145. contextualToolbar.fire( '_selectionChangeDone' );
  146. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  147. expect( balloon.view.top ).to.be.below( 100 );
  148. } );
  149. it( 'should open below if the selection is backward but panel stick out of the limiter element', () => {
  150. setData( editor.document, '<paragraph>[bar]</paragraph>', { lastRangeBackward: true } );
  151. // Mock limiter rect.
  152. mockBoundingBox( document.body, {
  153. left: 0,
  154. width: 1000,
  155. top: 95,
  156. height: 905
  157. } );
  158. contextualToolbar.fire( '_selectionChangeDone' );
  159. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  160. expect( balloon.view.top ).to.be.above( 100 );
  161. } );
  162. it( 'should not open if the collapsed selection is moving', () => {
  163. setData( editor.document, '<paragraph>ba[]r</paragraph>' );
  164. editor.document.selection.fire( 'change:range', {} );
  165. contextualToolbar.fire( '_selectionChangeDone' );
  166. setData( editor.document, '<paragraph>b[]ar</paragraph>' );
  167. editor.document.selection.fire( 'change:range', {} );
  168. contextualToolbar.fire( '_selectionChangeDone' );
  169. expect( balloon.visibleView ).to.null;
  170. } );
  171. it( 'should hide if the editor loses focus', () => {
  172. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  173. editor.ui.focusTracker.isFocused = true;
  174. contextualToolbar.fire( '_selectionChangeDone' );
  175. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  176. editor.ui.focusTracker.isFocused = false;
  177. expect( balloon.visibleView ).to.null;
  178. } );
  179. it( 'should do nothing when panel is being added to balloon stack twice', () => {
  180. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  181. contextualToolbar.fire( '_selectionChangeDone' );
  182. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  183. expect( () => {
  184. contextualToolbar.fire( '_selectionChangeDone' );
  185. } ).to.not.throw();
  186. } );
  187. it( 'should update balloon position when toolbar is opened and editor content has changed', () => {
  188. const spy = sandbox.spy( balloon, 'updatePosition' );
  189. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  190. contextualToolbar.fire( '_selectionChangeDone' );
  191. sinon.assert.notCalled( spy );
  192. editor.editing.view.fire( 'render' );
  193. sinon.assert.calledOnce( spy );
  194. } );
  195. it( 'should update balloon position when toolbar is closed', () => {
  196. const spy = sandbox.spy( balloon, 'updatePosition' );
  197. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  198. contextualToolbar.fire( '_selectionChangeDone' );
  199. // Hide toolbar.
  200. editor.document.selection.fire( 'change:range', { directChange: true } );
  201. editor.editing.view.fire( 'render' );
  202. sinon.assert.notCalled( spy );
  203. } );
  204. describe( 'pluginName', () => {
  205. it( 'should return plugin by name', () => {
  206. expect( editor.plugins.get( 'contextualballoon' ) ).to.equal( balloon );
  207. } );
  208. } );
  209. describe( 'destroy()', () => {
  210. it( 'should not fire `_selectionChangeDone` after plugin destroy', ( done ) => {
  211. const spy = sandbox.spy();
  212. contextualToolbar.on( '_selectionChangeDone', spy );
  213. editor.document.selection.fire( 'change:range', { directChange: true } );
  214. contextualToolbar.destroy();
  215. setTimeout( () => {
  216. sinon.assert.notCalled( spy );
  217. done();
  218. }, 200 );
  219. } );
  220. } );
  221. function stubClientRects() {
  222. const editingView = editor.editing.view;
  223. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  224. // Mock selection rect.
  225. sandbox.stub( editingView.domConverter, 'viewRangeToDom', ( ...args ) => {
  226. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  227. sandbox.stub( domRange, 'getClientRects', () => {
  228. return {
  229. length: 2,
  230. item: id => {
  231. if ( id === 0 ) {
  232. return {
  233. top: 100,
  234. height: 10,
  235. bottom: 110,
  236. left: 200,
  237. width: 50,
  238. right: 250
  239. };
  240. }
  241. return {
  242. top: 300,
  243. height: 10,
  244. bottom: 310,
  245. left: 400,
  246. width: 50,
  247. right: 450
  248. };
  249. }
  250. };
  251. } );
  252. return domRange;
  253. } );
  254. // Mock window rect.
  255. sandbox.stub( global, 'window', {
  256. innerWidth: 1000,
  257. innerHeight: 1000,
  258. scrollX: 0,
  259. scrollY: 0,
  260. getComputedStyle: el => {
  261. return window.getComputedStyle( el );
  262. }
  263. } );
  264. // Mock balloon rect.
  265. mockBoundingBox( balloon.view.element, {
  266. width: 150,
  267. height: 50
  268. } );
  269. }
  270. function mockBoundingBox( element, data ) {
  271. const boundingBox = Object.assign( {}, data );
  272. boundingBox.right = boundingBox.left + boundingBox.width;
  273. boundingBox.bottom = boundingBox.top + boundingBox.height;
  274. sandbox.stub( element, 'getBoundingClientRect' ).returns( boundingBox );
  275. }
  276. } );