balloontoolbar.js 21 KB

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