contextualtoolbar.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 BalloonPanelView from '../../../src/panel/balloon/balloonpanelview';
  8. import ToolbarView from '../../../src/toolbar/toolbarview';
  9. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  10. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  11. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  12. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  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. // There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
  31. sandbox.stub( balloon.view, 'attachTo', () => {} );
  32. sandbox.stub( balloon.view, 'pin', () => {} );
  33. // Focus the engine.
  34. editor.editing.view.isFocused = true;
  35. contextualToolbar.toolbarView.init();
  36. } );
  37. } );
  38. afterEach( () => {
  39. sandbox.restore();
  40. editorElement.remove();
  41. return editor.destroy();
  42. } );
  43. it( 'should create a plugin instance', () => {
  44. expect( contextualToolbar ).to.instanceOf( Plugin );
  45. expect( contextualToolbar ).to.instanceOf( ContextualToolbar );
  46. expect( contextualToolbar.toolbarView ).to.instanceof( ToolbarView );
  47. expect( contextualToolbar.toolbarView.element.classList.contains( 'ck-editor-toolbar' ) ).to.be.true;
  48. expect( contextualToolbar.toolbarView.element.classList.contains( 'ck-toolbar_floating' ) ).to.be.true;
  49. } );
  50. it( 'should load ContextualBalloon', () => {
  51. expect( balloon ).to.instanceof( ContextualBalloon );
  52. } );
  53. it( 'should create components from config', () => {
  54. expect( contextualToolbar.toolbarView.items ).to.length( 2 );
  55. } );
  56. it( 'should fire internal `_selectionChangeDebounced` event 200 ms after last selection change', done => {
  57. // This test uses setTimeout to test lodash#debounce because sinon fake timers
  58. // doesn't work with lodash. Lodash keeps time related stuff in a closure
  59. // and sinon is not able to override it.
  60. const spy = sandbox.spy();
  61. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  62. contextualToolbar.on( '_selectionChangeDebounced', spy );
  63. editor.document.selection.fire( 'change:range', {} );
  64. // Not yet.
  65. sinon.assert.notCalled( spy );
  66. // Lets wait 100 ms.
  67. setTimeout( () => {
  68. // Still not yet.
  69. sinon.assert.notCalled( spy );
  70. // Fire event one more time.
  71. editor.document.selection.fire( 'change:range', {} );
  72. // Another 100 ms waiting.
  73. setTimeout( () => {
  74. // Still not yet.
  75. sinon.assert.notCalled( spy );
  76. // Another 101 ms waiting.
  77. setTimeout( () => {
  78. // And here it is.
  79. sinon.assert.calledOnce( spy );
  80. done();
  81. }, 100 );
  82. }, 101 );
  83. }, 100 );
  84. } );
  85. describe( 'pluginName', () => {
  86. it( 'should return plugin by its name', () => {
  87. expect( editor.plugins.get( 'ContextualToolbar' ) ).to.equal( contextualToolbar );
  88. } );
  89. } );
  90. describe( 'show()', () => {
  91. let balloonAddSpy, forwardSelectionRect, backwardSelectionRect;
  92. beforeEach( () => {
  93. forwardSelectionRect = {
  94. top: 100,
  95. height: 10,
  96. bottom: 110,
  97. left: 200,
  98. width: 50,
  99. right: 250
  100. };
  101. backwardSelectionRect = {
  102. top: 200,
  103. height: 10,
  104. bottom: 210,
  105. left: 200,
  106. width: 50,
  107. right: 250
  108. };
  109. stubSelectionRect( forwardSelectionRect, backwardSelectionRect );
  110. balloonAddSpy = sandbox.spy( balloon, 'add' );
  111. editor.editing.view.isFocused = true;
  112. } );
  113. it( 'should add #toolbarView to the #_balloon and attach the #_balloon to the selection for the forward selection', () => {
  114. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  115. const defaultPositions = BalloonPanelView.defaultPositions;
  116. contextualToolbar.show();
  117. sinon.assert.calledWith( balloonAddSpy, {
  118. view: contextualToolbar.toolbarView,
  119. balloonClassName: 'ck-toolbar-container ck-editor-toolbar-container',
  120. position: {
  121. target: sinon.match.func,
  122. limiter: editor.ui.view.editable.element,
  123. positions: [
  124. defaultPositions.southEastArrowNorth,
  125. defaultPositions.southEastArrowNorthEast,
  126. defaultPositions.southEastArrowNorthWest,
  127. defaultPositions.northEastArrowSouth,
  128. defaultPositions.northEastArrowSouthEast,
  129. defaultPositions.northEastArrowSouthWest
  130. ]
  131. }
  132. } );
  133. expect( balloonAddSpy.firstCall.args[ 0 ].position.target() ).to.deep.equal( backwardSelectionRect );
  134. } );
  135. it( 'should add #toolbarView to the #_balloon and attach the #_balloon to the selection for the backward selection', () => {
  136. setData( editor.document, '<paragraph>b[a]r</paragraph>', { lastRangeBackward: true } );
  137. const defaultPositions = BalloonPanelView.defaultPositions;
  138. contextualToolbar.show();
  139. sinon.assert.calledWithExactly( balloonAddSpy, {
  140. view: contextualToolbar.toolbarView,
  141. balloonClassName: 'ck-toolbar-container ck-editor-toolbar-container',
  142. position: {
  143. target: sinon.match.func,
  144. limiter: editor.ui.view.editable.element,
  145. positions: [
  146. defaultPositions.northWestArrowSouth,
  147. defaultPositions.northWestArrowSouthWest,
  148. defaultPositions.northWestArrowSouthEast,
  149. defaultPositions.southWestArrowNorth,
  150. defaultPositions.southWestArrowNorthWest,
  151. defaultPositions.southWestArrowNorthEast
  152. ]
  153. }
  154. } );
  155. expect( balloonAddSpy.firstCall.args[ 0 ].position.target() ).to.deep.equal( forwardSelectionRect );
  156. } );
  157. it( 'should update balloon position on ViewDocument#render event while balloon is added to the #_balloon', () => {
  158. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  159. const spy = sandbox.spy( balloon, 'updatePosition' );
  160. editor.editing.view.fire( 'render' );
  161. contextualToolbar.show();
  162. sinon.assert.notCalled( spy );
  163. editor.editing.view.fire( 'render' );
  164. sinon.assert.calledOnce( spy );
  165. } );
  166. it( 'should not add #toolbarView to the #_balloon more than once', () => {
  167. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  168. contextualToolbar.show();
  169. contextualToolbar.show();
  170. sinon.assert.calledOnce( balloonAddSpy );
  171. } );
  172. describe( 'on #_selectionChangeDebounced event', () => {
  173. let showSpy;
  174. beforeEach( () => {
  175. showSpy = sinon.spy( contextualToolbar, 'show' );
  176. } );
  177. it( 'should not be called when the editor is not focused', () => {
  178. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  179. editor.editing.view.isFocused = false;
  180. contextualToolbar.fire( '_selectionChangeDebounced' );
  181. sinon.assert.notCalled( showSpy );
  182. } );
  183. it( 'should not be called when the selection is collapsed', () => {
  184. setData( editor.document, '<paragraph>b[]ar</paragraph>' );
  185. contextualToolbar.fire( '_selectionChangeDebounced' );
  186. sinon.assert.notCalled( showSpy );
  187. } );
  188. it( 'should be called when the selection is not collapsed and editor is focused', () => {
  189. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  190. editor.editing.view.isFocused = true;
  191. contextualToolbar.fire( '_selectionChangeDebounced' );
  192. sinon.assert.calledOnce( showSpy );
  193. } );
  194. } );
  195. } );
  196. describe( 'hide()', () => {
  197. let removeBalloonSpy;
  198. beforeEach( () => {
  199. removeBalloonSpy = sandbox.stub( balloon, 'remove', () => {} );
  200. editor.editing.view.isFocused = true;
  201. } );
  202. it( 'should remove #toolbarView from the #_balloon', () => {
  203. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  204. contextualToolbar.show();
  205. contextualToolbar.hide();
  206. sinon.assert.calledWithExactly( removeBalloonSpy, contextualToolbar.toolbarView );
  207. } );
  208. it( 'should stop update balloon position on ViewDocument#render event', () => {
  209. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  210. const spy = sandbox.spy( balloon, 'updatePosition' );
  211. contextualToolbar.show();
  212. contextualToolbar.hide();
  213. editor.editing.view.fire( 'render' );
  214. sinon.assert.notCalled( spy );
  215. } );
  216. it( 'should not remove #ttolbarView when is not added to the #_balloon', () => {
  217. contextualToolbar.hide();
  218. sinon.assert.notCalled( removeBalloonSpy );
  219. } );
  220. } );
  221. describe( 'destroy()', () => {
  222. it( 'can be called multiple times', () => {
  223. expect( () => {
  224. contextualToolbar.destroy();
  225. contextualToolbar.destroy();
  226. } ).to.not.throw();
  227. } );
  228. it( 'should not fire `_selectionChangeDebounced` after plugin destroy', done => {
  229. const spy = sandbox.spy();
  230. contextualToolbar.on( '_selectionChangeDebounced', spy );
  231. editor.document.selection.fire( 'change:range', { directChange: true } );
  232. contextualToolbar.destroy();
  233. setTimeout( () => {
  234. sinon.assert.notCalled( spy );
  235. done();
  236. }, 200 );
  237. } );
  238. } );
  239. describe( 'showing and hiding', () => {
  240. let showPanelSpy, hidePanelSpy;
  241. beforeEach( () => {
  242. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  243. showPanelSpy = sandbox.spy( contextualToolbar, 'show' );
  244. hidePanelSpy = sandbox.spy( contextualToolbar, 'hide' );
  245. } );
  246. it( 'should open when selection stops changing', () => {
  247. sinon.assert.notCalled( showPanelSpy );
  248. sinon.assert.notCalled( hidePanelSpy );
  249. contextualToolbar.fire( '_selectionChangeDebounced' );
  250. sinon.assert.calledOnce( showPanelSpy );
  251. sinon.assert.notCalled( hidePanelSpy );
  252. } );
  253. it( 'should close when selection starts changing by a directChange', () => {
  254. contextualToolbar.fire( '_selectionChangeDebounced' );
  255. sinon.assert.calledOnce( showPanelSpy );
  256. sinon.assert.notCalled( hidePanelSpy );
  257. editor.document.selection.fire( 'change:range', { directChange: true } );
  258. sinon.assert.calledOnce( showPanelSpy );
  259. sinon.assert.calledOnce( hidePanelSpy );
  260. } );
  261. it( 'should not close when selection starts changing by not a directChange', () => {
  262. contextualToolbar.fire( '_selectionChangeDebounced' );
  263. sinon.assert.calledOnce( showPanelSpy );
  264. sinon.assert.notCalled( hidePanelSpy );
  265. editor.document.selection.fire( 'change:range', { directChange: false } );
  266. sinon.assert.calledOnce( showPanelSpy );
  267. sinon.assert.notCalled( hidePanelSpy );
  268. } );
  269. it( 'should close when selection starts changing by not a directChange but will become collapsed', () => {
  270. contextualToolbar.fire( '_selectionChangeDebounced' );
  271. sinon.assert.calledOnce( showPanelSpy );
  272. sinon.assert.notCalled( hidePanelSpy );
  273. // Collapse range silently (without firing `change:range` { directChange: true } event).
  274. const range = editor.document.selection._ranges[ 0 ];
  275. range.end = range.start;
  276. editor.document.selection.fire( 'change:range', { directChange: false } );
  277. sinon.assert.calledOnce( showPanelSpy );
  278. sinon.assert.calledOnce( hidePanelSpy );
  279. } );
  280. it( 'should hide if the editor loses focus', () => {
  281. editor.ui.focusTracker.isFocused = true;
  282. contextualToolbar.fire( '_selectionChangeDebounced' );
  283. // Stubbing getters doesn't wor for sandbox.
  284. const stub = sinon.stub( balloon, 'visibleView', { get: () => contextualToolbar.toolbarView } );
  285. sinon.assert.calledOnce( showPanelSpy );
  286. sinon.assert.notCalled( hidePanelSpy );
  287. editor.ui.focusTracker.isFocused = false;
  288. sinon.assert.calledOnce( showPanelSpy );
  289. sinon.assert.calledOnce( hidePanelSpy );
  290. stub.restore();
  291. } );
  292. it( 'should not hide if the editor loses focus and #toolbarView is not visible', () => {
  293. editor.ui.focusTracker.isFocused = true;
  294. contextualToolbar.fire( '_selectionChangeDebounced' );
  295. // Stubbing getters doesn't wor for sandbox.
  296. const stub = sinon.stub( balloon, 'visibleView', { get: () => null } );
  297. sinon.assert.calledOnce( showPanelSpy );
  298. sinon.assert.notCalled( hidePanelSpy );
  299. editor.ui.focusTracker.isFocused = false;
  300. sinon.assert.calledOnce( showPanelSpy );
  301. sinon.assert.notCalled( hidePanelSpy );
  302. stub.restore();
  303. } );
  304. } );
  305. describe( 'beforeShow event', () => {
  306. it( 'should fire `show` event just before panel shows', () => {
  307. const spy = sinon.spy();
  308. contextualToolbar.on( 'show', spy );
  309. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  310. contextualToolbar.show();
  311. sinon.assert.calledOnce( spy );
  312. } );
  313. it( 'should not show the panel when `show` event is stopped', () => {
  314. const balloonAddSpy = sandbox.spy( balloon, 'add' );
  315. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  316. contextualToolbar.on( 'show', evt => evt.stop() );
  317. contextualToolbar.show();
  318. sinon.assert.notCalled( balloonAddSpy );
  319. } );
  320. } );
  321. function stubSelectionRect( forwardSelectionRect, backwardSelectionRect ) {
  322. const editingView = editor.editing.view;
  323. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  324. // Mock selection rect.
  325. sandbox.stub( editingView.domConverter, 'viewRangeToDom', ( ...args ) => {
  326. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  327. sandbox.stub( domRange, 'getClientRects' )
  328. .returns( [ forwardSelectionRect, backwardSelectionRect ] );
  329. return domRange;
  330. } );
  331. }
  332. } );