contextualtoolbar.js 13 KB

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