contextualtoolbar.js 15 KB

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