balloontoolbar.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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 not add #toolbarView to the #_balloon more than once', () => {
  283. setData( model, '<paragraph>b[a]r</paragraph>' );
  284. balloonToolbar.show();
  285. balloonToolbar.show();
  286. sinon.assert.calledOnce( balloonAddSpy );
  287. } );
  288. it( 'should not add the #toolbarView to the #_balloon when the selection is collapsed', () => {
  289. setData( model, '<paragraph>b[]ar</paragraph>' );
  290. balloonToolbar.show();
  291. sinon.assert.notCalled( balloonAddSpy );
  292. } );
  293. // https://github.com/ckeditor/ckeditor5/issues/6443
  294. it( 'should not add the #toolbarView to the #_balloon when the selection contains more than one fully contained object', () => {
  295. setData( model, '[<horizontalLine></horizontalLine>]<paragraph>foo</paragraph>[<horizontalLine></horizontalLine>]' );
  296. balloonToolbar.show();
  297. sinon.assert.notCalled( balloonAddSpy );
  298. } );
  299. // https://github.com/ckeditor/ckeditor5/issues/6432
  300. it( 'should not add the #toolbarView to the #_balloon when the selection contains more than one fully contained selectable', () => {
  301. // This is for multi cell selection in tables.
  302. setData( model, '<table>' +
  303. '<tableRow>' +
  304. '[<tableCell><paragraph>foo</paragraph></tableCell>]' +
  305. '[<tableCell><paragraph>bar</paragraph></tableCell>]' +
  306. '</tableRow>' +
  307. '</table>' );
  308. balloonToolbar.show();
  309. sinon.assert.notCalled( balloonAddSpy );
  310. } );
  311. it( 'should not add #toolbarView to the #_balloon when all components inside #toolbarView are disabled', () => {
  312. Array.from( balloonToolbar.toolbarView.items ).forEach( item => {
  313. item.isEnabled = false;
  314. } );
  315. setData( model, '<paragraph>b[a]r</paragraph>' );
  316. balloonToolbar.show();
  317. sinon.assert.notCalled( balloonAddSpy );
  318. } );
  319. it( 'should add #toolbarView to the #_balloon when at least one component inside does not have #isEnabled interface', () => {
  320. Array.from( balloonToolbar.toolbarView.items ).forEach( item => {
  321. item.isEnabled = false;
  322. } );
  323. delete balloonToolbar.toolbarView.items.get( 0 ).isEnabled;
  324. setData( model, '<paragraph>b[a]r</paragraph>' );
  325. balloonToolbar.show();
  326. sinon.assert.calledOnce( balloonAddSpy );
  327. } );
  328. it( 'should set the toolbar max-width to 90% of the editable width', () => {
  329. const viewElement = editor.ui.view.editable.element;
  330. setData( model, '<paragraph>b[ar]</paragraph>' );
  331. expect( global.document.body.contains( viewElement ) ).to.be.true;
  332. viewElement.style.width = '400px';
  333. resizeCallback( [ {
  334. target: viewElement,
  335. contentRect: new Rect( viewElement )
  336. } ] );
  337. // The expected width should be 90% of the editor's editable element's width.
  338. const expectedWidth = toPx( new Rect( viewElement ).width * 0.9 );
  339. expect( balloonToolbar.toolbarView.maxWidth ).to.equal( expectedWidth );
  340. } );
  341. } );
  342. describe( 'hide()', () => {
  343. let removeBalloonSpy;
  344. beforeEach( () => {
  345. removeBalloonSpy = sinon.stub( balloon, 'remove' ).returns( {} );
  346. editingView.document.isFocused = true;
  347. } );
  348. it( 'should remove #toolbarView from the #_balloon', () => {
  349. setData( model, '<paragraph>b[a]r</paragraph>' );
  350. balloonToolbar.show();
  351. balloonToolbar.hide();
  352. sinon.assert.calledWithExactly( removeBalloonSpy, balloonToolbar.toolbarView );
  353. } );
  354. it( 'should stop update balloon position on ui#update event', () => {
  355. setData( model, '<paragraph>b[a]r</paragraph>' );
  356. const spy = sinon.spy( balloon, 'updatePosition' );
  357. balloonToolbar.show();
  358. balloonToolbar.hide();
  359. editor.ui.fire( 'update' );
  360. sinon.assert.notCalled( spy );
  361. } );
  362. it( 'should not remove #toolbarView when is not added to the #_balloon', () => {
  363. balloonToolbar.hide();
  364. sinon.assert.notCalled( removeBalloonSpy );
  365. } );
  366. } );
  367. describe( 'destroy()', () => {
  368. it( 'can be called multiple times', () => {
  369. expect( () => {
  370. balloonToolbar.destroy();
  371. balloonToolbar.destroy();
  372. } ).to.not.throw();
  373. } );
  374. it( 'should not fire `_selectionChangeDebounced` after plugin destroy', () => {
  375. const clock = testUtils.sinon.useFakeTimers();
  376. const spy = testUtils.sinon.spy();
  377. balloonToolbar.on( '_selectionChangeDebounced', spy );
  378. selection.fire( 'change:range', { directChange: true } );
  379. balloonToolbar.destroy();
  380. clock.tick( 200 );
  381. sinon.assert.notCalled( spy );
  382. } );
  383. it( 'should destroy #resizeObserver if is available', () => {
  384. const editable = editor.ui.getEditableElement();
  385. const resizeObserver = new ResizeObserver( editable, () => {} );
  386. const destroySpy = sinon.spy( resizeObserver, 'destroy' );
  387. balloonToolbar._resizeObserver = resizeObserver;
  388. balloonToolbar.destroy();
  389. sinon.assert.calledOnce( destroySpy );
  390. } );
  391. } );
  392. describe( 'show and hide triggers', () => {
  393. let showPanelSpy, hidePanelSpy;
  394. beforeEach( () => {
  395. setData( model, '<paragraph>[bar]</paragraph>' );
  396. showPanelSpy = sinon.spy( balloonToolbar, 'show' );
  397. hidePanelSpy = sinon.spy( balloonToolbar, 'hide' );
  398. } );
  399. it( 'should show when selection stops changing', () => {
  400. sinon.assert.notCalled( showPanelSpy );
  401. sinon.assert.notCalled( hidePanelSpy );
  402. balloonToolbar.fire( '_selectionChangeDebounced' );
  403. sinon.assert.calledOnce( showPanelSpy );
  404. sinon.assert.notCalled( hidePanelSpy );
  405. } );
  406. it( 'should not show when the selection stops changing when the editable is blurred', () => {
  407. sinon.assert.notCalled( showPanelSpy );
  408. sinon.assert.notCalled( hidePanelSpy );
  409. editingView.document.isFocused = false;
  410. balloonToolbar.fire( '_selectionChangeDebounced' );
  411. sinon.assert.notCalled( showPanelSpy );
  412. sinon.assert.notCalled( hidePanelSpy );
  413. } );
  414. it( 'should hide when selection starts changing by a direct change', () => {
  415. balloonToolbar.fire( '_selectionChangeDebounced' );
  416. sinon.assert.calledOnce( showPanelSpy );
  417. sinon.assert.notCalled( hidePanelSpy );
  418. selection.fire( 'change:range', { directChange: true } );
  419. sinon.assert.calledOnce( showPanelSpy );
  420. sinon.assert.calledOnce( hidePanelSpy );
  421. } );
  422. it( 'should not hide when selection starts changing by an indirect change', () => {
  423. balloonToolbar.fire( '_selectionChangeDebounced' );
  424. sinon.assert.calledOnce( showPanelSpy );
  425. sinon.assert.notCalled( hidePanelSpy );
  426. selection.fire( 'change:range', { directChange: false } );
  427. sinon.assert.calledOnce( showPanelSpy );
  428. sinon.assert.notCalled( hidePanelSpy );
  429. } );
  430. it( 'should hide when selection starts changing by an indirect change but has changed to collapsed', () => {
  431. balloonToolbar.fire( '_selectionChangeDebounced' );
  432. sinon.assert.calledOnce( showPanelSpy );
  433. sinon.assert.notCalled( hidePanelSpy );
  434. // Collapse range silently (without firing `change:range` { directChange: true } event).
  435. const range = selection._ranges[ 0 ];
  436. range.end = range.start;
  437. selection.fire( 'change:range', { directChange: false } );
  438. sinon.assert.calledOnce( showPanelSpy );
  439. sinon.assert.calledOnce( hidePanelSpy );
  440. } );
  441. it( 'should show on #focusTracker focus', () => {
  442. balloonToolbar.focusTracker.isFocused = false;
  443. sinon.assert.notCalled( showPanelSpy );
  444. sinon.assert.notCalled( hidePanelSpy );
  445. balloonToolbar.focusTracker.isFocused = true;
  446. sinon.assert.calledOnce( showPanelSpy );
  447. sinon.assert.notCalled( hidePanelSpy );
  448. } );
  449. it( 'should hide on #focusTracker blur', () => {
  450. balloonToolbar.focusTracker.isFocused = true;
  451. const stub = sinon.stub( balloon, 'visibleView' ).get( () => balloonToolbar.toolbarView );
  452. sinon.assert.calledOnce( showPanelSpy );
  453. sinon.assert.notCalled( hidePanelSpy );
  454. balloonToolbar.focusTracker.isFocused = false;
  455. sinon.assert.calledOnce( showPanelSpy );
  456. sinon.assert.calledOnce( hidePanelSpy );
  457. stub.restore();
  458. } );
  459. it( 'should not hide on #focusTracker blur when toolbar is not in the balloon stack', () => {
  460. balloonToolbar.focusTracker.isFocused = true;
  461. const stub = sinon.stub( balloon, 'visibleView' ).get( () => null );
  462. sinon.assert.calledOnce( showPanelSpy );
  463. sinon.assert.notCalled( hidePanelSpy );
  464. balloonToolbar.focusTracker.isFocused = false;
  465. sinon.assert.calledOnce( showPanelSpy );
  466. sinon.assert.notCalled( hidePanelSpy );
  467. stub.restore();
  468. } );
  469. } );
  470. describe( 'show event', () => {
  471. it( 'should fire `show` event just before panel shows', () => {
  472. const spy = sinon.spy();
  473. balloonToolbar.on( 'show', spy );
  474. setData( model, '<paragraph>b[a]r</paragraph>' );
  475. balloonToolbar.show();
  476. sinon.assert.calledOnce( spy );
  477. } );
  478. it( 'should not show the panel when `show` event is stopped', () => {
  479. const balloonAddSpy = sinon.spy( balloon, 'add' );
  480. setData( model, '<paragraph>b[a]r</paragraph>' );
  481. balloonToolbar.on( 'show', evt => evt.stop(), { priority: 'high' } );
  482. balloonToolbar.show();
  483. sinon.assert.notCalled( balloonAddSpy );
  484. } );
  485. } );
  486. function stubSelectionRects( rects ) {
  487. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  488. // Mock selection rect.
  489. sinon.stub( editingView.domConverter, 'viewRangeToDom' ).callsFake( ( ...args ) => {
  490. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  491. sinon.stub( domRange, 'getClientRects' )
  492. .returns( rects );
  493. return domRange;
  494. } );
  495. }
  496. } );