balloontoolbar.js 19 KB

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