contextualtoolbar.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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, backwardSelectionRect, forwardSelectionRect;
  93. beforeEach( () => {
  94. backwardSelectionRect = {
  95. top: 100,
  96. height: 10,
  97. bottom: 110,
  98. left: 200,
  99. width: 50,
  100. right: 250
  101. };
  102. forwardSelectionRect = {
  103. top: 200,
  104. height: 10,
  105. bottom: 210,
  106. left: 200,
  107. width: 50,
  108. right: 250
  109. };
  110. stubSelectionRects( [
  111. backwardSelectionRect,
  112. forwardSelectionRect
  113. ] );
  114. balloonAddSpy = sandbox.spy( balloon, 'add' );
  115. editor.editing.view.isFocused = true;
  116. } );
  117. it( 'should add #toolbarView to the #_balloon and attach the #_balloon to the selection for the forward selection', () => {
  118. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  119. const defaultPositions = BalloonPanelView.defaultPositions;
  120. contextualToolbar.show();
  121. sinon.assert.calledWith( balloonAddSpy, {
  122. view: contextualToolbar.toolbarView,
  123. balloonClassName: 'ck-toolbar-container ck-editor-toolbar-container',
  124. position: {
  125. target: sinon.match.func,
  126. positions: [
  127. defaultPositions.southEastArrowNorth,
  128. defaultPositions.southEastArrowNorthEast,
  129. defaultPositions.southEastArrowNorthWest,
  130. defaultPositions.northEastArrowSouth,
  131. defaultPositions.northEastArrowSouthEast,
  132. defaultPositions.northEastArrowSouthWest
  133. ]
  134. }
  135. } );
  136. expect( balloonAddSpy.firstCall.args[ 0 ].position.target() ).to.deep.equal( forwardSelectionRect );
  137. } );
  138. // https://github.com/ckeditor/ckeditor5-ui/issues/308
  139. it( 'should ignore the zero-width orphan rect if there another one preceding it for the forward selection', () => {
  140. // Restore previous stubSelectionRects() call.
  141. editor.editing.view.domConverter.viewRangeToDom.restore();
  142. // Simulate an "orphan" rect preceded by a "correct" one.
  143. stubSelectionRects( [
  144. forwardSelectionRect,
  145. { width: 0 }
  146. ] );
  147. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  148. contextualToolbar.show();
  149. expect( balloonAddSpy.firstCall.args[ 0 ].position.target() ).to.deep.equal( forwardSelectionRect );
  150. } );
  151. it( 'should add #toolbarView to the #_balloon and attach the #_balloon to the selection for the backward selection', () => {
  152. setData( editor.document, '<paragraph>b[a]r</paragraph>', { lastRangeBackward: true } );
  153. const defaultPositions = BalloonPanelView.defaultPositions;
  154. contextualToolbar.show();
  155. sinon.assert.calledWithExactly( balloonAddSpy, {
  156. view: contextualToolbar.toolbarView,
  157. balloonClassName: 'ck-toolbar-container ck-editor-toolbar-container',
  158. position: {
  159. target: sinon.match.func,
  160. positions: [
  161. defaultPositions.northWestArrowSouth,
  162. defaultPositions.northWestArrowSouthWest,
  163. defaultPositions.northWestArrowSouthEast,
  164. defaultPositions.southWestArrowNorth,
  165. defaultPositions.southWestArrowNorthWest,
  166. defaultPositions.southWestArrowNorthEast
  167. ]
  168. }
  169. } );
  170. expect( balloonAddSpy.firstCall.args[ 0 ].position.target() ).to.deep.equal( backwardSelectionRect );
  171. } );
  172. it( 'should update balloon position on ViewDocument#render event while balloon is added to the #_balloon', () => {
  173. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  174. const spy = sandbox.spy( balloon, 'updatePosition' );
  175. editor.editing.view.fire( 'render' );
  176. contextualToolbar.show();
  177. sinon.assert.notCalled( spy );
  178. editor.editing.view.fire( 'render' );
  179. sinon.assert.calledOnce( spy );
  180. } );
  181. it( 'should not add #toolbarView to the #_balloon more than once', () => {
  182. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  183. contextualToolbar.show();
  184. contextualToolbar.show();
  185. sinon.assert.calledOnce( balloonAddSpy );
  186. } );
  187. it( 'should not add #toolbarView to the #_balloon when all components inside #toolbarView are disabled', () => {
  188. Array.from( contextualToolbar.toolbarView.items ).forEach( item => {
  189. item.isEnabled = false;
  190. } );
  191. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  192. contextualToolbar.show();
  193. sinon.assert.notCalled( balloonAddSpy );
  194. } );
  195. it( 'should add #toolbarView to the #_balloon when at least one component inside does not have #isEnabled interface', () => {
  196. Array.from( contextualToolbar.toolbarView.items ).forEach( item => {
  197. item.isEnabled = false;
  198. } );
  199. delete contextualToolbar.toolbarView.items.get( 0 ).isEnabled;
  200. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  201. contextualToolbar.show();
  202. sinon.assert.calledOnce( balloonAddSpy );
  203. } );
  204. describe( 'on #_selectionChangeDebounced event', () => {
  205. let showSpy;
  206. beforeEach( () => {
  207. showSpy = sinon.spy( contextualToolbar, 'show' );
  208. } );
  209. it( 'should not be called when the editor is not focused', () => {
  210. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  211. editor.editing.view.isFocused = false;
  212. contextualToolbar.fire( '_selectionChangeDebounced' );
  213. sinon.assert.notCalled( showSpy );
  214. } );
  215. it( 'should not be called when the selection is collapsed', () => {
  216. setData( editor.document, '<paragraph>b[]ar</paragraph>' );
  217. contextualToolbar.fire( '_selectionChangeDebounced' );
  218. sinon.assert.notCalled( showSpy );
  219. } );
  220. it( 'should be called when the selection is not collapsed and editor is focused', () => {
  221. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  222. editor.editing.view.isFocused = true;
  223. contextualToolbar.fire( '_selectionChangeDebounced' );
  224. sinon.assert.calledOnce( showSpy );
  225. } );
  226. } );
  227. } );
  228. describe( 'hide()', () => {
  229. let removeBalloonSpy;
  230. beforeEach( () => {
  231. removeBalloonSpy = sandbox.stub( balloon, 'remove' ).returns( {} );
  232. editor.editing.view.isFocused = true;
  233. } );
  234. it( 'should remove #toolbarView from the #_balloon', () => {
  235. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  236. contextualToolbar.show();
  237. contextualToolbar.hide();
  238. sinon.assert.calledWithExactly( removeBalloonSpy, contextualToolbar.toolbarView );
  239. } );
  240. it( 'should stop update balloon position on ViewDocument#render event', () => {
  241. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  242. const spy = sandbox.spy( balloon, 'updatePosition' );
  243. contextualToolbar.show();
  244. contextualToolbar.hide();
  245. editor.editing.view.fire( 'render' );
  246. sinon.assert.notCalled( spy );
  247. } );
  248. it( 'should not remove #ttolbarView when is not added to the #_balloon', () => {
  249. contextualToolbar.hide();
  250. sinon.assert.notCalled( removeBalloonSpy );
  251. } );
  252. } );
  253. describe( 'destroy()', () => {
  254. it( 'can be called multiple times', () => {
  255. expect( () => {
  256. contextualToolbar.destroy();
  257. contextualToolbar.destroy();
  258. } ).to.not.throw();
  259. } );
  260. it( 'should not fire `_selectionChangeDebounced` after plugin destroy', done => {
  261. const spy = sandbox.spy();
  262. contextualToolbar.on( '_selectionChangeDebounced', spy );
  263. editor.document.selection.fire( 'change:range', { directChange: true } );
  264. contextualToolbar.destroy();
  265. setTimeout( () => {
  266. sinon.assert.notCalled( spy );
  267. done();
  268. }, 200 );
  269. } );
  270. } );
  271. describe( 'showing and hiding', () => {
  272. let showPanelSpy, hidePanelSpy;
  273. beforeEach( () => {
  274. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  275. showPanelSpy = sandbox.spy( contextualToolbar, 'show' );
  276. hidePanelSpy = sandbox.spy( contextualToolbar, 'hide' );
  277. } );
  278. it( 'should open when selection stops changing', () => {
  279. sinon.assert.notCalled( showPanelSpy );
  280. sinon.assert.notCalled( hidePanelSpy );
  281. contextualToolbar.fire( '_selectionChangeDebounced' );
  282. sinon.assert.calledOnce( showPanelSpy );
  283. sinon.assert.notCalled( hidePanelSpy );
  284. } );
  285. it( 'should close when selection starts changing by a directChange', () => {
  286. contextualToolbar.fire( '_selectionChangeDebounced' );
  287. sinon.assert.calledOnce( showPanelSpy );
  288. sinon.assert.notCalled( hidePanelSpy );
  289. editor.document.selection.fire( 'change:range', { directChange: true } );
  290. sinon.assert.calledOnce( showPanelSpy );
  291. sinon.assert.calledOnce( hidePanelSpy );
  292. } );
  293. it( 'should not close when selection starts changing by not a directChange', () => {
  294. contextualToolbar.fire( '_selectionChangeDebounced' );
  295. sinon.assert.calledOnce( showPanelSpy );
  296. sinon.assert.notCalled( hidePanelSpy );
  297. editor.document.selection.fire( 'change:range', { directChange: false } );
  298. sinon.assert.calledOnce( showPanelSpy );
  299. sinon.assert.notCalled( hidePanelSpy );
  300. } );
  301. it( 'should close when selection starts changing by not a directChange but will become collapsed', () => {
  302. contextualToolbar.fire( '_selectionChangeDebounced' );
  303. sinon.assert.calledOnce( showPanelSpy );
  304. sinon.assert.notCalled( hidePanelSpy );
  305. // Collapse range silently (without firing `change:range` { directChange: true } event).
  306. const range = editor.document.selection._ranges[ 0 ];
  307. range.end = range.start;
  308. editor.document.selection.fire( 'change:range', { directChange: false } );
  309. sinon.assert.calledOnce( showPanelSpy );
  310. sinon.assert.calledOnce( hidePanelSpy );
  311. } );
  312. it( 'should hide if the editor loses focus', () => {
  313. editor.ui.focusTracker.isFocused = true;
  314. contextualToolbar.fire( '_selectionChangeDebounced' );
  315. const stub = sinon.stub( balloon, 'visibleView' ).get( () => contextualToolbar.toolbarView );
  316. sinon.assert.calledOnce( showPanelSpy );
  317. sinon.assert.notCalled( hidePanelSpy );
  318. editor.ui.focusTracker.isFocused = false;
  319. sinon.assert.calledOnce( showPanelSpy );
  320. sinon.assert.calledOnce( hidePanelSpy );
  321. stub.restore();
  322. } );
  323. it( 'should not hide if the editor loses focus and #toolbarView is not visible', () => {
  324. editor.ui.focusTracker.isFocused = true;
  325. contextualToolbar.fire( '_selectionChangeDebounced' );
  326. const stub = sinon.stub( balloon, 'visibleView' ).get( () => null );
  327. sinon.assert.calledOnce( showPanelSpy );
  328. sinon.assert.notCalled( hidePanelSpy );
  329. editor.ui.focusTracker.isFocused = false;
  330. sinon.assert.calledOnce( showPanelSpy );
  331. sinon.assert.notCalled( hidePanelSpy );
  332. stub.restore();
  333. } );
  334. } );
  335. describe( 'show event', () => {
  336. it( 'should fire `show` event just before panel shows', () => {
  337. const spy = sinon.spy();
  338. contextualToolbar.on( 'show', spy );
  339. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  340. contextualToolbar.show();
  341. sinon.assert.calledOnce( spy );
  342. } );
  343. it( 'should not show the panel when `show` event is stopped', () => {
  344. const balloonAddSpy = sandbox.spy( balloon, 'add' );
  345. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  346. contextualToolbar.on( 'show', evt => evt.stop(), { priority: 'high' } );
  347. contextualToolbar.show();
  348. sinon.assert.notCalled( balloonAddSpy );
  349. } );
  350. } );
  351. function stubSelectionRects( rects ) {
  352. const editingView = editor.editing.view;
  353. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  354. // Mock selection rect.
  355. sandbox.stub( editingView.domConverter, 'viewRangeToDom' ).callsFake( ( ...args ) => {
  356. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  357. sandbox.stub( domRange, 'getClientRects' )
  358. .returns( rects );
  359. return domRange;
  360. } );
  361. }
  362. } );