contextualtoolbar.js 14 KB

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