contextualtoolbar.js 13 KB

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