contextualtoolbar.js 12 KB

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