balloontoolbar.js 19 KB

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