contextualtoolbar.js 12 KB

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