balloontoolbar.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import BalloonToolbar from '../../../src/toolbar/balloon/balloontoolbar';
  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 FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  11. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  12. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  13. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  14. import Underline from '@ckeditor/ckeditor5-basic-styles/src/underline';
  15. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  16. import HorizontalLine from '@ckeditor/ckeditor5-horizontal-line/src/horizontalline';
  17. import TableEditing from '@ckeditor/ckeditor5-table/src/tableediting';
  18. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  19. import ResizeObserver from '@ckeditor/ckeditor5-utils/src/dom/resizeobserver';
  20. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  21. import { stringify as viewStringify } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  22. import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
  23. import toUnit from '@ckeditor/ckeditor5-utils/src/dom/tounit';
  24. const toPx = toUnit( 'px' );
  25. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  26. /* global document, window, Event */
  27. describe( 'BalloonToolbar', () => {
  28. let editor, model, selection, editingView, balloonToolbar, balloon, editorElement;
  29. let resizeCallback;
  30. testUtils.createSinonSandbox();
  31. beforeEach( () => {
  32. editorElement = document.createElement( 'div' );
  33. document.body.appendChild( editorElement );
  34. // Make sure other tests of the editor do not affect tests that follow.
  35. // Without it, if an instance of ResizeObserver already exists somewhere undestroyed
  36. // in DOM, the following DOM mock will have no effect.
  37. ResizeObserver._observerInstance = null;
  38. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( callback => {
  39. resizeCallback = callback;
  40. return {
  41. observe: sinon.spy(),
  42. unobserve: sinon.spy()
  43. };
  44. } );
  45. return ClassicTestEditor
  46. .create( editorElement, {
  47. plugins: [ Paragraph, Bold, Italic, BalloonToolbar, HorizontalLine, TableEditing ],
  48. balloonToolbar: [ 'bold', 'italic' ]
  49. } )
  50. .then( newEditor => {
  51. editor = newEditor;
  52. model = editor.model;
  53. editingView = editor.editing.view;
  54. selection = model.document.selection;
  55. balloonToolbar = editor.plugins.get( BalloonToolbar );
  56. balloon = editor.plugins.get( ContextualBalloon );
  57. editingView.attachDomRoot( editorElement );
  58. // There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
  59. sinon.stub( balloon.view, 'attachTo' ).returns( {} );
  60. sinon.stub( balloon.view, 'pin' ).returns( {} );
  61. // Focus the engine.
  62. editingView.document.isFocused = true;
  63. editingView.getDomRoot().focus();
  64. // Remove all selection ranges from DOM before testing.
  65. window.getSelection().removeAllRanges();
  66. } );
  67. } );
  68. afterEach( () => {
  69. sinon.restore();
  70. editorElement.remove();
  71. return editor.destroy();
  72. } );
  73. it( 'should create a plugin instance', () => {
  74. expect( balloonToolbar ).to.instanceOf( Plugin );
  75. expect( balloonToolbar ).to.instanceOf( BalloonToolbar );
  76. expect( balloonToolbar.toolbarView ).to.instanceof( ToolbarView );
  77. expect( balloonToolbar.toolbarView.element.classList.contains( 'ck-toolbar_floating' ) ).to.be.true;
  78. } );
  79. it( 'should load ContextualBalloon', () => {
  80. expect( balloon ).to.instanceof( ContextualBalloon );
  81. } );
  82. it( 'should create components from config', () => {
  83. expect( balloonToolbar.toolbarView.items ).to.length( 2 );
  84. } );
  85. it( 'should accept the extended format of the toolbar config', () => {
  86. const editorElement = document.createElement( 'div' );
  87. document.body.appendChild( editorElement );
  88. return ClassicTestEditor
  89. .create( editorElement, {
  90. plugins: [ Paragraph, Bold, Italic, Underline, BalloonToolbar ],
  91. balloonToolbar: {
  92. items: [ 'bold', 'italic', 'underline' ]
  93. }
  94. } )
  95. .then( editor => {
  96. const balloonToolbar = editor.plugins.get( BalloonToolbar );
  97. expect( balloonToolbar.toolbarView.items ).to.length( 3 );
  98. editorElement.remove();
  99. return editor.destroy();
  100. } );
  101. } );
  102. it( 'should not group items when the config.shouldNotGroupWhenFull option is enabled', () => {
  103. const editorElement = document.createElement( 'div' );
  104. document.body.appendChild( editorElement );
  105. return ClassicTestEditor.create( editorElement, {
  106. plugins: [ Paragraph, Bold, Italic, Underline, BalloonToolbar ],
  107. balloonToolbar: {
  108. items: [ 'bold', 'italic', 'underline' ],
  109. shouldNotGroupWhenFull: true
  110. }
  111. } ).then( editor => {
  112. const balloonToolbar = editor.plugins.get( BalloonToolbar );
  113. expect( balloonToolbar.toolbarView.options.shouldGroupWhenFull ).to.be.false;
  114. return editor.destroy();
  115. } ).then( () => {
  116. editorElement.remove();
  117. } );
  118. } );
  119. it( 'should fire internal `_selectionChangeDebounced` event 200 ms after last selection change', () => {
  120. const clock = testUtils.sinon.useFakeTimers();
  121. const spy = testUtils.sinon.spy();
  122. setData( model, '<paragraph>[bar]</paragraph>' );
  123. balloonToolbar.on( '_selectionChangeDebounced', spy );
  124. selection.fire( 'change:range', {} );
  125. // Not yet.
  126. sinon.assert.notCalled( spy );
  127. // Lets wait 100 ms.
  128. clock.tick( 100 );
  129. // Still not yet.
  130. sinon.assert.notCalled( spy );
  131. // Fire event one more time.
  132. selection.fire( 'change:range', {} );
  133. // Another 100 ms waiting.
  134. clock.tick( 100 );
  135. // Still not yet.
  136. sinon.assert.notCalled( spy );
  137. // Another waiting.
  138. clock.tick( 110 );
  139. // And here it is.
  140. sinon.assert.calledOnce( spy );
  141. } );
  142. describe( 'pluginName', () => {
  143. it( 'should return plugin by its name', () => {
  144. expect( editor.plugins.get( 'BalloonToolbar' ) ).to.equal( balloonToolbar );
  145. } );
  146. } );
  147. describe( 'focusTracker', () => {
  148. it( 'should be defined', () => {
  149. expect( balloonToolbar.focusTracker ).to.instanceof( FocusTracker );
  150. } );
  151. it( 'it should track the focus of the #editableElement', () => {
  152. expect( balloonToolbar.focusTracker.isFocused ).to.false;
  153. editor.ui.getEditableElement().dispatchEvent( new Event( 'focus' ) );
  154. expect( balloonToolbar.focusTracker.isFocused ).to.true;
  155. } );
  156. it( 'it should track the focus of the toolbarView#element', () => {
  157. expect( balloonToolbar.focusTracker.isFocused ).to.false;
  158. balloonToolbar.toolbarView.element.dispatchEvent( new Event( 'focus' ) );
  159. expect( balloonToolbar.focusTracker.isFocused ).to.true;
  160. } );
  161. } );
  162. describe( 'show()', () => {
  163. let balloonAddSpy, backwardSelectionRect, forwardSelectionRect;
  164. beforeEach( () => {
  165. backwardSelectionRect = {
  166. top: 100,
  167. height: 10,
  168. bottom: 110,
  169. left: 200,
  170. width: 50,
  171. right: 250
  172. };
  173. forwardSelectionRect = {
  174. top: 200,
  175. height: 10,
  176. bottom: 210,
  177. left: 200,
  178. width: 50,
  179. right: 250
  180. };
  181. stubSelectionRects( [
  182. backwardSelectionRect,
  183. forwardSelectionRect
  184. ] );
  185. balloonAddSpy = sinon.spy( balloon, 'add' );
  186. editingView.document.isFocused = true;
  187. } );
  188. it( 'should add #toolbarView to the #_balloon and attach the #_balloon to the selection for the forward selection', () => {
  189. setData( model, '<paragraph>b[a]r</paragraph>' );
  190. const defaultPositions = BalloonPanelView.defaultPositions;
  191. balloonToolbar.show();
  192. sinon.assert.calledWith( balloonAddSpy, {
  193. view: balloonToolbar.toolbarView,
  194. balloonClassName: 'ck-toolbar-container',
  195. position: {
  196. target: sinon.match.func,
  197. positions: [
  198. defaultPositions.southEastArrowNorth,
  199. defaultPositions.southEastArrowNorthEast,
  200. defaultPositions.southEastArrowNorthWest,
  201. defaultPositions.southEastArrowNorthMiddleEast,
  202. defaultPositions.southEastArrowNorthMiddleWest,
  203. defaultPositions.northEastArrowSouth,
  204. defaultPositions.northEastArrowSouthEast,
  205. defaultPositions.northEastArrowSouthWest,
  206. defaultPositions.northEastArrowSouthMiddleEast,
  207. defaultPositions.northEastArrowSouthMiddleWest
  208. ]
  209. }
  210. } );
  211. expect( balloonAddSpy.firstCall.args[ 0 ].position.target() ).to.deep.equal( forwardSelectionRect );
  212. } );
  213. // https://github.com/ckeditor/ckeditor5-ui/issues/385
  214. it( 'should attach the #_balloon to the last range in a case of multi-range forward selection', () => {
  215. setData( model, '<paragraph>b[ar]</paragraph><paragraph>[bi]z</paragraph>' );
  216. balloonToolbar.show();
  217. // Because attaching and pinning BalloonPanelView is stubbed for test
  218. // we need to manually call function that counting rect.
  219. const targetRect = balloonAddSpy.firstCall.args[ 0 ].position.target();
  220. const targetViewRange = editingView.domConverter.viewRangeToDom.lastCall.args[ 0 ];
  221. expect( viewStringify( targetViewRange.root, targetViewRange, { ignoreRoot: true } ) ).to.equal( '<p>bar</p><p>{bi}z</p>' );
  222. expect( targetRect ).to.deep.equal( forwardSelectionRect );
  223. } );
  224. // https://github.com/ckeditor/ckeditor5-ui/issues/308
  225. it( 'should ignore the zero-width orphan rect if there another one preceding it for the forward selection', () => {
  226. // Restore previous stubSelectionRects() call.
  227. editingView.domConverter.viewRangeToDom.restore();
  228. // Simulate an "orphan" rect preceded by a "correct" one.
  229. stubSelectionRects( [
  230. forwardSelectionRect,
  231. { width: 0 }
  232. ] );
  233. setData( model, '<paragraph>b[a]r</paragraph>' );
  234. balloonToolbar.show();
  235. expect( balloonAddSpy.firstCall.args[ 0 ].position.target() ).to.deep.equal( forwardSelectionRect );
  236. } );
  237. it( 'should add #toolbarView to the #_balloon and attach the #_balloon to the selection for the backward selection', () => {
  238. setData( model, '<paragraph>b[a]r</paragraph>', { lastRangeBackward: true } );
  239. const defaultPositions = BalloonPanelView.defaultPositions;
  240. balloonToolbar.show();
  241. sinon.assert.calledWithExactly( balloonAddSpy, {
  242. view: balloonToolbar.toolbarView,
  243. balloonClassName: 'ck-toolbar-container',
  244. position: {
  245. target: sinon.match.func,
  246. positions: [
  247. defaultPositions.northWestArrowSouth,
  248. defaultPositions.northWestArrowSouthWest,
  249. defaultPositions.northWestArrowSouthEast,
  250. defaultPositions.northWestArrowSouthMiddleEast,
  251. defaultPositions.northWestArrowSouthMiddleWest,
  252. defaultPositions.southWestArrowNorth,
  253. defaultPositions.southWestArrowNorthWest,
  254. defaultPositions.southWestArrowNorthEast,
  255. defaultPositions.southWestArrowNorthMiddleWest,
  256. defaultPositions.southWestArrowNorthMiddleEast
  257. ]
  258. }
  259. } );
  260. expect( balloonAddSpy.firstCall.args[ 0 ].position.target() ).to.deep.equal( backwardSelectionRect );
  261. } );
  262. // https://github.com/ckeditor/ckeditor5-ui/issues/385
  263. it( 'should attach the #_balloon to the first range in a case of multi-range backward selection', () => {
  264. setData( model, '<paragraph>b[ar]</paragraph><paragraph>[bi]z</paragraph>', { lastRangeBackward: true } );
  265. balloonToolbar.show();
  266. // Because attaching and pinning BalloonPanelView is stubbed for test
  267. // we need to manually call function that counting rect.
  268. const targetRect = balloonAddSpy.firstCall.args[ 0 ].position.target();
  269. const targetViewRange = editingView.domConverter.viewRangeToDom.lastCall.args[ 0 ];
  270. expect( viewStringify( targetViewRange.root, targetViewRange, { ignoreRoot: true } ) ).to.equal( '<p>b{ar}</p><p>biz</p>' );
  271. expect( targetRect ).to.deep.equal( backwardSelectionRect );
  272. } );
  273. it( 'should update balloon position on ui#update event when #toolbarView is already added to the #_balloon', () => {
  274. setData( model, '<paragraph>b[a]r</paragraph>' );
  275. const spy = sinon.spy( balloon, 'updatePosition' );
  276. editor.ui.fire( 'update' );
  277. balloonToolbar.show();
  278. sinon.assert.notCalled( spy );
  279. editor.ui.fire( 'update' );
  280. sinon.assert.calledOnce( spy );
  281. } );
  282. it( 'should update the balloon position whenever #toolbarView fires the #groupedItemsUpdate (it changed its geometry)', () => {
  283. setData( model, '<paragraph>b[a]r</paragraph>' );
  284. const spy = sinon.spy( balloon, 'updatePosition' );
  285. balloonToolbar.show();
  286. sinon.assert.notCalled( spy );
  287. balloonToolbar.toolbarView.fire( 'groupedItemsUpdate' );
  288. sinon.assert.calledOnce( spy );
  289. } );
  290. it( 'should not add #toolbarView to the #_balloon more than once', () => {
  291. setData( model, '<paragraph>b[a]r</paragraph>' );
  292. balloonToolbar.show();
  293. balloonToolbar.show();
  294. sinon.assert.calledOnce( balloonAddSpy );
  295. } );
  296. it( 'should not add the #toolbarView to the #_balloon when the selection is collapsed', () => {
  297. setData( model, '<paragraph>b[]ar</paragraph>' );
  298. balloonToolbar.show();
  299. sinon.assert.notCalled( balloonAddSpy );
  300. } );
  301. // https://github.com/ckeditor/ckeditor5/issues/6443
  302. it( 'should not add the #toolbarView to the #_balloon when the selection contains more than one fully contained object', () => {
  303. setData( model, '[<horizontalLine></horizontalLine>]<paragraph>foo</paragraph>[<horizontalLine></horizontalLine>]' );
  304. balloonToolbar.show();
  305. sinon.assert.notCalled( balloonAddSpy );
  306. } );
  307. // https://github.com/ckeditor/ckeditor5/issues/6432
  308. it( 'should not add the #toolbarView to the #_balloon when the selection contains more than one fully contained selectable', () => {
  309. // This is for multi cell selection in tables.
  310. setData( model, '<table>' +
  311. '<tableRow>' +
  312. '[<tableCell><paragraph>foo</paragraph></tableCell>]' +
  313. '[<tableCell><paragraph>bar</paragraph></tableCell>]' +
  314. '</tableRow>' +
  315. '</table>' );
  316. balloonToolbar.show();
  317. sinon.assert.notCalled( balloonAddSpy );
  318. } );
  319. it( 'should not add #toolbarView to the #_balloon when all components inside #toolbarView are disabled', () => {
  320. Array.from( balloonToolbar.toolbarView.items ).forEach( item => {
  321. item.isEnabled = false;
  322. } );
  323. setData( model, '<paragraph>b[a]r</paragraph>' );
  324. balloonToolbar.show();
  325. sinon.assert.notCalled( balloonAddSpy );
  326. } );
  327. it( 'should add #toolbarView to the #_balloon when at least one component inside does not have #isEnabled interface', () => {
  328. Array.from( balloonToolbar.toolbarView.items ).forEach( item => {
  329. item.isEnabled = false;
  330. } );
  331. delete balloonToolbar.toolbarView.items.get( 0 ).isEnabled;
  332. setData( model, '<paragraph>b[a]r</paragraph>' );
  333. balloonToolbar.show();
  334. sinon.assert.calledOnce( balloonAddSpy );
  335. } );
  336. it( 'should set the toolbar max-width to 90% of the editable width', () => {
  337. const viewElement = editor.ui.view.editable.element;
  338. setData( model, '<paragraph>b[ar]</paragraph>' );
  339. expect( global.document.body.contains( viewElement ) ).to.be.true;
  340. viewElement.style.width = '400px';
  341. resizeCallback( [ {
  342. target: viewElement,
  343. contentRect: new Rect( viewElement )
  344. } ] );
  345. // The expected width should be 90% of the editor's editable element's width.
  346. const expectedWidth = toPx( new Rect( viewElement ).width * 0.9 );
  347. expect( balloonToolbar.toolbarView.maxWidth ).to.equal( expectedWidth );
  348. } );
  349. } );
  350. describe( 'hide()', () => {
  351. let removeBalloonSpy;
  352. beforeEach( () => {
  353. removeBalloonSpy = sinon.stub( balloon, 'remove' ).returns( {} );
  354. editingView.document.isFocused = true;
  355. } );
  356. it( 'should remove #toolbarView from the #_balloon', () => {
  357. setData( model, '<paragraph>b[a]r</paragraph>' );
  358. balloonToolbar.show();
  359. balloonToolbar.hide();
  360. sinon.assert.calledWithExactly( removeBalloonSpy, balloonToolbar.toolbarView );
  361. } );
  362. it( 'should stop update balloon position on ui#update event', () => {
  363. setData( model, '<paragraph>b[a]r</paragraph>' );
  364. const spy = sinon.spy( balloon, 'updatePosition' );
  365. balloonToolbar.show();
  366. balloonToolbar.hide();
  367. editor.ui.fire( 'update' );
  368. sinon.assert.notCalled( spy );
  369. } );
  370. it( 'should not remove #toolbarView when is not added to the #_balloon', () => {
  371. balloonToolbar.hide();
  372. sinon.assert.notCalled( removeBalloonSpy );
  373. } );
  374. } );
  375. describe( 'destroy()', () => {
  376. it( 'can be called multiple times', () => {
  377. expect( () => {
  378. balloonToolbar.destroy();
  379. balloonToolbar.destroy();
  380. } ).to.not.throw();
  381. } );
  382. it( 'should not fire `_selectionChangeDebounced` after plugin destroy', () => {
  383. const clock = testUtils.sinon.useFakeTimers();
  384. const spy = testUtils.sinon.spy();
  385. balloonToolbar.on( '_selectionChangeDebounced', spy );
  386. selection.fire( 'change:range', { directChange: true } );
  387. balloonToolbar.destroy();
  388. clock.tick( 200 );
  389. sinon.assert.notCalled( spy );
  390. } );
  391. it( 'should destroy #resizeObserver if is available', () => {
  392. const editable = editor.ui.getEditableElement();
  393. const resizeObserver = new ResizeObserver( editable, () => {} );
  394. const destroySpy = sinon.spy( resizeObserver, 'destroy' );
  395. balloonToolbar._resizeObserver = resizeObserver;
  396. balloonToolbar.destroy();
  397. sinon.assert.calledOnce( destroySpy );
  398. } );
  399. } );
  400. describe( 'show and hide triggers', () => {
  401. let showPanelSpy, hidePanelSpy;
  402. beforeEach( () => {
  403. setData( model, '<paragraph>[bar]</paragraph>' );
  404. showPanelSpy = sinon.spy( balloonToolbar, 'show' );
  405. hidePanelSpy = sinon.spy( balloonToolbar, 'hide' );
  406. } );
  407. it( 'should show when selection stops changing', () => {
  408. sinon.assert.notCalled( showPanelSpy );
  409. sinon.assert.notCalled( hidePanelSpy );
  410. balloonToolbar.fire( '_selectionChangeDebounced' );
  411. sinon.assert.calledOnce( showPanelSpy );
  412. sinon.assert.notCalled( hidePanelSpy );
  413. } );
  414. it( 'should not show when the selection stops changing when the editable is blurred', () => {
  415. sinon.assert.notCalled( showPanelSpy );
  416. sinon.assert.notCalled( hidePanelSpy );
  417. editingView.document.isFocused = false;
  418. balloonToolbar.fire( '_selectionChangeDebounced' );
  419. sinon.assert.notCalled( showPanelSpy );
  420. sinon.assert.notCalled( hidePanelSpy );
  421. } );
  422. it( 'should hide when selection starts changing by a direct change', () => {
  423. balloonToolbar.fire( '_selectionChangeDebounced' );
  424. sinon.assert.calledOnce( showPanelSpy );
  425. sinon.assert.notCalled( hidePanelSpy );
  426. selection.fire( 'change:range', { directChange: true } );
  427. sinon.assert.calledOnce( showPanelSpy );
  428. sinon.assert.calledOnce( hidePanelSpy );
  429. } );
  430. it( 'should not hide when selection starts changing by an indirect change', () => {
  431. balloonToolbar.fire( '_selectionChangeDebounced' );
  432. sinon.assert.calledOnce( showPanelSpy );
  433. sinon.assert.notCalled( hidePanelSpy );
  434. selection.fire( 'change:range', { directChange: false } );
  435. sinon.assert.calledOnce( showPanelSpy );
  436. sinon.assert.notCalled( hidePanelSpy );
  437. } );
  438. it( 'should hide when selection starts changing by an indirect change but has changed to collapsed', () => {
  439. balloonToolbar.fire( '_selectionChangeDebounced' );
  440. sinon.assert.calledOnce( showPanelSpy );
  441. sinon.assert.notCalled( hidePanelSpy );
  442. // Collapse range silently (without firing `change:range` { directChange: true } event).
  443. const range = selection._ranges[ 0 ];
  444. range.end = range.start;
  445. selection.fire( 'change:range', { directChange: false } );
  446. sinon.assert.calledOnce( showPanelSpy );
  447. sinon.assert.calledOnce( hidePanelSpy );
  448. } );
  449. it( 'should show on #focusTracker focus', () => {
  450. balloonToolbar.focusTracker.isFocused = false;
  451. sinon.assert.notCalled( showPanelSpy );
  452. sinon.assert.notCalled( hidePanelSpy );
  453. balloonToolbar.focusTracker.isFocused = true;
  454. sinon.assert.calledOnce( showPanelSpy );
  455. sinon.assert.notCalled( hidePanelSpy );
  456. } );
  457. it( 'should hide on #focusTracker blur', () => {
  458. balloonToolbar.focusTracker.isFocused = true;
  459. const stub = sinon.stub( balloon, 'visibleView' ).get( () => balloonToolbar.toolbarView );
  460. sinon.assert.calledOnce( showPanelSpy );
  461. sinon.assert.notCalled( hidePanelSpy );
  462. balloonToolbar.focusTracker.isFocused = false;
  463. sinon.assert.calledOnce( showPanelSpy );
  464. sinon.assert.calledOnce( hidePanelSpy );
  465. stub.restore();
  466. } );
  467. it( 'should not hide on #focusTracker blur when toolbar is not in the balloon stack', () => {
  468. balloonToolbar.focusTracker.isFocused = true;
  469. const stub = sinon.stub( balloon, 'visibleView' ).get( () => null );
  470. sinon.assert.calledOnce( showPanelSpy );
  471. sinon.assert.notCalled( hidePanelSpy );
  472. balloonToolbar.focusTracker.isFocused = false;
  473. sinon.assert.calledOnce( showPanelSpy );
  474. sinon.assert.notCalled( hidePanelSpy );
  475. stub.restore();
  476. } );
  477. } );
  478. describe( 'show event', () => {
  479. it( 'should fire `show` event just before panel shows', () => {
  480. const spy = sinon.spy();
  481. balloonToolbar.on( 'show', spy );
  482. setData( model, '<paragraph>b[a]r</paragraph>' );
  483. balloonToolbar.show();
  484. sinon.assert.calledOnce( spy );
  485. } );
  486. it( 'should not show the panel when `show` event is stopped', () => {
  487. const balloonAddSpy = sinon.spy( balloon, 'add' );
  488. setData( model, '<paragraph>b[a]r</paragraph>' );
  489. balloonToolbar.on( 'show', evt => evt.stop(), { priority: 'high' } );
  490. balloonToolbar.show();
  491. sinon.assert.notCalled( balloonAddSpy );
  492. } );
  493. } );
  494. function stubSelectionRects( rects ) {
  495. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  496. // Mock selection rect.
  497. sinon.stub( editingView.domConverter, 'viewRangeToDom' ).callsFake( ( ...args ) => {
  498. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  499. sinon.stub( domRange, 'getClientRects' )
  500. .returns( rects );
  501. return domRange;
  502. } );
  503. }
  504. } );