contextualtoolbar.js 14 KB

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