contextualtoolbar.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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/panel/balloon/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, 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. // Init child view.
  33. return contextualToolbar.toolbarView.init();
  34. } );
  35. } );
  36. afterEach( () => {
  37. sandbox.restore();
  38. return editor.destroy();
  39. } );
  40. it( 'should create a plugin instance', () => {
  41. expect( contextualToolbar ).to.instanceOf( Plugin );
  42. expect( contextualToolbar ).to.instanceOf( ContextualToolbar );
  43. expect( contextualToolbar.toolbarView ).to.instanceof( ToolbarView );
  44. } );
  45. it( 'should load ContextualBalloon', () => {
  46. expect( balloon ).to.instanceof( ContextualBalloon );
  47. } );
  48. it( 'should create components from config', () => {
  49. expect( contextualToolbar.toolbarView.items ).to.length( 2 );
  50. } );
  51. it( 'should fire internal `_selectionChangeDebounced` event 200 ms after last selection change', ( done ) => {
  52. // This test uses setTimeout to test lodash#debounce because sinon fake timers
  53. // doesn't work with lodash. Lodash keeps time related stuff in a closure
  54. // and sinon is not able to override it.
  55. const spy = sandbox.spy();
  56. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  57. contextualToolbar.on( '_selectionChangeDebounced', spy );
  58. editor.document.selection.fire( 'change:range', {} );
  59. // Not yet.
  60. sinon.assert.notCalled( spy );
  61. // Lets wait 100 ms.
  62. setTimeout( () => {
  63. // Still not yet.
  64. sinon.assert.notCalled( spy );
  65. // Fire event one more time.
  66. editor.document.selection.fire( 'change:range', {} );
  67. // Another 100 ms waiting.
  68. setTimeout( () => {
  69. // Still not yet.
  70. sinon.assert.notCalled( spy );
  71. // Another 101 ms waiting.
  72. setTimeout( () => {
  73. // And here it is.
  74. sinon.assert.calledOnce( spy );
  75. done();
  76. }, 100 );
  77. }, 101 );
  78. }, 100 );
  79. } );
  80. it( 'should open when selection stops changing', () => {
  81. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  82. expect( balloon.visibleView ).to.null;
  83. contextualToolbar.fire( '_selectionChangeDebounced' );
  84. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  85. } );
  86. it( 'should add additional class to the ContextualBalloon#view', () => {
  87. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  88. contextualToolbar.fire( '_selectionChangeDebounced' );
  89. expect( balloon.view.className ).to.equal( 'ck-toolbar__container' );
  90. } );
  91. it( 'should close when selection starts changing by a directChange', () => {
  92. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  93. contextualToolbar.fire( '_selectionChangeDebounced' );
  94. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  95. editor.document.selection.fire( 'change:range', { directChange: true } );
  96. expect( balloon.visibleView ).to.null;
  97. } );
  98. it( 'should not close when selection starts changing by not a directChange', () => {
  99. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  100. contextualToolbar.fire( '_selectionChangeDebounced' );
  101. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  102. editor.document.selection.fire( 'change:range', { directChange: false } );
  103. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  104. } );
  105. it( 'should close when selection starts changing by not a directChange but will become collapsed', () => {
  106. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  107. contextualToolbar.fire( '_selectionChangeDebounced' );
  108. // Collapse range silently (without firing `change:range` { directChange: true } event).
  109. const range = editor.document.selection._ranges[ 0 ];
  110. range.end = range.start;
  111. editor.document.selection.fire( 'change:range', { directChange: false } );
  112. expect( balloon.visibleView ).to.null;
  113. } );
  114. it( 'should put balloon on the `south` if the selection is forward', () => {
  115. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  116. stubClientRects();
  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. // Balloon is attached after internal promise resolve and we have
  127. // no access to this promise so we need to wait for it.
  128. return wait().then( () => {
  129. expect( balloon.view.position ).to.equal( 'arrow_s' );
  130. } );
  131. } );
  132. it( 'should put balloon on the `north` if the selection is forward `south` is limited', () => {
  133. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  134. stubClientRects();
  135. // Mock limiter rect.
  136. mockBoundingBox( document.body, {
  137. left: 0,
  138. width: 1000,
  139. top: 0,
  140. height: 310
  141. } );
  142. contextualToolbar.fire( '_selectionChangeDebounced' );
  143. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  144. // Balloon is attached after internal promise resolve and we have
  145. // no access to this promise so we need to wait for it.
  146. return wait().then( () => {
  147. expect( balloon.view.position ).to.equal( 'arrow_n' );
  148. } );
  149. } );
  150. it( 'should put balloon on the `north` if the selection is backward', () => {
  151. setData( editor.document, '<paragraph>[bar]</paragraph>', { lastRangeBackward: true } );
  152. stubClientRects();
  153. // Mock limiter rect.
  154. mockBoundingBox( document.body, {
  155. left: 0,
  156. width: 1000,
  157. top: 0,
  158. height: 1000
  159. } );
  160. contextualToolbar.fire( '_selectionChangeDebounced' );
  161. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  162. // Balloon is attached after internal promise resolve and we have
  163. // no access to this promise so we need to wait for it.
  164. return wait().then( () => {
  165. expect( balloon.view.position ).to.equal( 'arrow_n' );
  166. } );
  167. } );
  168. it( 'should put balloon on the `south` if the selection is backward `north` is limited', () => {
  169. setData( editor.document, '<paragraph>[bar]</paragraph>', { lastRangeBackward: true } );
  170. stubClientRects();
  171. // Mock limiter rect.
  172. mockBoundingBox( document.body, {
  173. left: 0,
  174. width: 1000,
  175. top: 95,
  176. height: 905
  177. } );
  178. contextualToolbar.fire( '_selectionChangeDebounced' );
  179. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  180. // Balloon is attached after internal promise resolve and we have
  181. // no access to this promise so we need to wait for it.
  182. return wait().then( () => {
  183. expect( balloon.view.position ).to.equal( 'arrow_s' );
  184. } );
  185. } );
  186. it( 'should not open if the collapsed selection is moving', () => {
  187. setData( editor.document, '<paragraph>ba[]r</paragraph>' );
  188. editor.document.selection.fire( 'change:range', {} );
  189. contextualToolbar.fire( '_selectionChangeDebounced' );
  190. setData( editor.document, '<paragraph>b[]ar</paragraph>' );
  191. editor.document.selection.fire( 'change:range', {} );
  192. contextualToolbar.fire( '_selectionChangeDebounced' );
  193. expect( balloon.visibleView ).to.null;
  194. } );
  195. it( 'should hide if the editor loses focus', () => {
  196. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  197. editor.ui.focusTracker.isFocused = true;
  198. contextualToolbar.fire( '_selectionChangeDebounced' );
  199. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  200. editor.ui.focusTracker.isFocused = false;
  201. expect( balloon.visibleView ).to.null;
  202. } );
  203. it( 'should do nothing when panel is being added to balloon stack twice', () => {
  204. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  205. contextualToolbar.fire( '_selectionChangeDebounced' );
  206. expect( balloon.visibleView ).to.equal( contextualToolbar.toolbarView );
  207. expect( () => {
  208. contextualToolbar.fire( '_selectionChangeDebounced' );
  209. } ).to.not.throw();
  210. } );
  211. it( 'should update balloon position when toolbar is opened and editor content has changed', () => {
  212. const spy = sandbox.stub( balloon, 'updatePosition' );
  213. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  214. contextualToolbar.fire( '_selectionChangeDebounced' );
  215. sinon.assert.notCalled( spy );
  216. editor.editing.view.fire( 'render' );
  217. sinon.assert.calledOnce( spy );
  218. } );
  219. it( 'should update balloon position when toolbar is closed', () => {
  220. const spy = sandbox.spy( balloon, 'updatePosition' );
  221. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  222. contextualToolbar.fire( '_selectionChangeDebounced' );
  223. // Hide toolbar.
  224. editor.document.selection.fire( 'change:range', { directChange: true } );
  225. editor.editing.view.fire( 'render' );
  226. sinon.assert.notCalled( spy );
  227. } );
  228. describe( 'pluginName', () => {
  229. it( 'should return plugin by name', () => {
  230. expect( editor.plugins.get( 'contextualballoon' ) ).to.equal( balloon );
  231. } );
  232. } );
  233. describe( 'destroy()', () => {
  234. it( 'should not fire `_selectionChangeDebounced` after plugin destroy', ( done ) => {
  235. const spy = sandbox.spy();
  236. contextualToolbar.on( '_selectionChangeDebounced', spy );
  237. editor.document.selection.fire( 'change:range', { directChange: true } );
  238. contextualToolbar.destroy();
  239. setTimeout( () => {
  240. sinon.assert.notCalled( spy );
  241. done();
  242. }, 200 );
  243. } );
  244. } );
  245. function stubClientRects() {
  246. const editingView = editor.editing.view;
  247. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  248. // Mock selection rect.
  249. sandbox.stub( editingView.domConverter, 'viewRangeToDom', ( ...args ) => {
  250. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  251. sandbox.stub( domRange, 'getClientRects', () => {
  252. return {
  253. length: 2,
  254. item: id => {
  255. if ( id === 0 ) {
  256. return {
  257. top: 100,
  258. height: 10,
  259. bottom: 110,
  260. left: 200,
  261. width: 50,
  262. right: 250
  263. };
  264. }
  265. return {
  266. top: 300,
  267. height: 10,
  268. bottom: 310,
  269. left: 400,
  270. width: 50,
  271. right: 450
  272. };
  273. }
  274. };
  275. } );
  276. return domRange;
  277. } );
  278. // Mock window rect.
  279. sandbox.stub( global.window, 'innerWidth', 1000 );
  280. sandbox.stub( global.window, 'innerHeight', 1000 );
  281. sandbox.stub( global.window, 'scrollX', 0 );
  282. sandbox.stub( global.window, 'scrollY', 0 );
  283. // Mock balloon rect.
  284. mockBoundingBox( balloon.view.element, {
  285. width: 150,
  286. height: 50
  287. } );
  288. }
  289. function mockBoundingBox( element, data ) {
  290. const boundingBox = Object.assign( {}, data );
  291. boundingBox.right = boundingBox.left + boundingBox.width;
  292. boundingBox.bottom = boundingBox.top + boundingBox.height;
  293. sandbox.stub( element, 'getBoundingClientRect' ).returns( boundingBox );
  294. }
  295. } );
  296. function wait( delay = 1 ) {
  297. return new Promise( ( resolve ) => {
  298. setTimeout( () => resolve(), delay );
  299. } );
  300. }