balloontoolbar.js 21 KB

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