contextualtoolbar.js 15 KB

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