balloontoolbar.js 20 KB

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