balloontoolbar.js 21 KB

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