balloontoolbar.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 */
  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( 'should fire internal `_selectionChangeDebounced` event 200 ms after last selection change', () => {
  84. const clock = testUtils.sinon.useFakeTimers();
  85. const spy = testUtils.sinon.spy();
  86. setData( model, '<paragraph>[bar]</paragraph>' );
  87. balloonToolbar.on( '_selectionChangeDebounced', spy );
  88. selection.fire( 'change:range', {} );
  89. // Not yet.
  90. sinon.assert.notCalled( spy );
  91. // Lets wait 100 ms.
  92. clock.tick( 100 );
  93. // Still not yet.
  94. sinon.assert.notCalled( spy );
  95. // Fire event one more time.
  96. selection.fire( 'change:range', {} );
  97. // Another 100 ms waiting.
  98. clock.tick( 100 );
  99. // Still not yet.
  100. sinon.assert.notCalled( spy );
  101. // Another waiting.
  102. clock.tick( 110 );
  103. // And here it is.
  104. sinon.assert.calledOnce( spy );
  105. } );
  106. describe( 'pluginName', () => {
  107. it( 'should return plugin by its name', () => {
  108. expect( editor.plugins.get( 'BalloonToolbar' ) ).to.equal( balloonToolbar );
  109. } );
  110. } );
  111. describe( 'focusTracker', () => {
  112. it( 'should be defined', () => {
  113. expect( balloonToolbar.focusTracker ).to.instanceof( FocusTracker );
  114. } );
  115. it( 'it should track the focus of the #editableElement', () => {
  116. expect( balloonToolbar.focusTracker.isFocused ).to.false;
  117. editor.ui.getEditableElement().dispatchEvent( new Event( 'focus' ) );
  118. expect( balloonToolbar.focusTracker.isFocused ).to.true;
  119. } );
  120. it( 'it should track the focus of the toolbarView#element', () => {
  121. expect( balloonToolbar.focusTracker.isFocused ).to.false;
  122. balloonToolbar.toolbarView.element.dispatchEvent( new Event( 'focus' ) );
  123. expect( balloonToolbar.focusTracker.isFocused ).to.true;
  124. } );
  125. } );
  126. describe( 'show()', () => {
  127. let balloonAddSpy, backwardSelectionRect, forwardSelectionRect;
  128. beforeEach( () => {
  129. backwardSelectionRect = {
  130. top: 100,
  131. height: 10,
  132. bottom: 110,
  133. left: 200,
  134. width: 50,
  135. right: 250
  136. };
  137. forwardSelectionRect = {
  138. top: 200,
  139. height: 10,
  140. bottom: 210,
  141. left: 200,
  142. width: 50,
  143. right: 250
  144. };
  145. stubSelectionRects( [
  146. backwardSelectionRect,
  147. forwardSelectionRect
  148. ] );
  149. balloonAddSpy = sinon.spy( balloon, 'add' );
  150. editingView.document.isFocused = true;
  151. } );
  152. it( 'should add #toolbarView to the #_balloon and attach the #_balloon to the selection for the forward selection', () => {
  153. setData( model, '<paragraph>b[a]r</paragraph>' );
  154. const defaultPositions = BalloonPanelView.defaultPositions;
  155. balloonToolbar.show();
  156. sinon.assert.calledWith( balloonAddSpy, {
  157. view: balloonToolbar.toolbarView,
  158. balloonClassName: 'ck-toolbar-container',
  159. position: {
  160. target: sinon.match.func,
  161. positions: [
  162. defaultPositions.southEastArrowNorth,
  163. defaultPositions.southEastArrowNorthEast,
  164. defaultPositions.southEastArrowNorthWest,
  165. defaultPositions.northEastArrowSouth,
  166. defaultPositions.northEastArrowSouthEast,
  167. defaultPositions.northEastArrowSouthWest
  168. ]
  169. }
  170. } );
  171. expect( balloonAddSpy.firstCall.args[ 0 ].position.target() ).to.deep.equal( forwardSelectionRect );
  172. } );
  173. // https://github.com/ckeditor/ckeditor5-ui/issues/385
  174. it( 'should attach the #_balloon to the last range in a case of multi-range forward selection', () => {
  175. setData( model, '<paragraph>b[ar]</paragraph><paragraph>[bi]z</paragraph>' );
  176. balloonToolbar.show();
  177. // Because attaching and pinning BalloonPanelView is stubbed for test
  178. // we need to manually call function that counting rect.
  179. const targetRect = balloonAddSpy.firstCall.args[ 0 ].position.target();
  180. const targetViewRange = editingView.domConverter.viewRangeToDom.lastCall.args[ 0 ];
  181. expect( viewStringify( targetViewRange.root, targetViewRange, { ignoreRoot: true } ) ).to.equal( '<p>bar</p><p>{bi}z</p>' );
  182. expect( targetRect ).to.deep.equal( forwardSelectionRect );
  183. } );
  184. // https://github.com/ckeditor/ckeditor5-ui/issues/308
  185. it( 'should ignore the zero-width orphan rect if there another one preceding it for the forward selection', () => {
  186. // Restore previous stubSelectionRects() call.
  187. editingView.domConverter.viewRangeToDom.restore();
  188. // Simulate an "orphan" rect preceded by a "correct" one.
  189. stubSelectionRects( [
  190. forwardSelectionRect,
  191. { width: 0 }
  192. ] );
  193. setData( model, '<paragraph>b[a]r</paragraph>' );
  194. balloonToolbar.show();
  195. expect( balloonAddSpy.firstCall.args[ 0 ].position.target() ).to.deep.equal( forwardSelectionRect );
  196. } );
  197. it( 'should add #toolbarView to the #_balloon and attach the #_balloon to the selection for the backward selection', () => {
  198. setData( model, '<paragraph>b[a]r</paragraph>', { lastRangeBackward: true } );
  199. const defaultPositions = BalloonPanelView.defaultPositions;
  200. balloonToolbar.show();
  201. sinon.assert.calledWithExactly( balloonAddSpy, {
  202. view: balloonToolbar.toolbarView,
  203. balloonClassName: 'ck-toolbar-container',
  204. position: {
  205. target: sinon.match.func,
  206. positions: [
  207. defaultPositions.northWestArrowSouth,
  208. defaultPositions.northWestArrowSouthWest,
  209. defaultPositions.northWestArrowSouthEast,
  210. defaultPositions.southWestArrowNorth,
  211. defaultPositions.southWestArrowNorthWest,
  212. defaultPositions.southWestArrowNorthEast
  213. ]
  214. }
  215. } );
  216. expect( balloonAddSpy.firstCall.args[ 0 ].position.target() ).to.deep.equal( backwardSelectionRect );
  217. } );
  218. // https://github.com/ckeditor/ckeditor5-ui/issues/385
  219. it( 'should attach the #_balloon to the first range in a case of multi-range backward selection', () => {
  220. setData( model, '<paragraph>b[ar]</paragraph><paragraph>[bi]z</paragraph>', { lastRangeBackward: true } );
  221. balloonToolbar.show();
  222. // Because attaching and pinning BalloonPanelView is stubbed for test
  223. // we need to manually call function that counting rect.
  224. const targetRect = balloonAddSpy.firstCall.args[ 0 ].position.target();
  225. const targetViewRange = editingView.domConverter.viewRangeToDom.lastCall.args[ 0 ];
  226. expect( viewStringify( targetViewRange.root, targetViewRange, { ignoreRoot: true } ) ).to.equal( '<p>b{ar}</p><p>biz</p>' );
  227. expect( targetRect ).to.deep.equal( backwardSelectionRect );
  228. } );
  229. it( 'should update balloon position on ui#update event when #toolbarView is already added to the #_balloon', () => {
  230. setData( model, '<paragraph>b[a]r</paragraph>' );
  231. const spy = sinon.spy( balloon, 'updatePosition' );
  232. editor.ui.fire( 'update' );
  233. balloonToolbar.show();
  234. sinon.assert.notCalled( spy );
  235. editor.ui.fire( 'update' );
  236. sinon.assert.calledOnce( spy );
  237. } );
  238. it( 'should not add #toolbarView to the #_balloon more than once', () => {
  239. setData( model, '<paragraph>b[a]r</paragraph>' );
  240. balloonToolbar.show();
  241. balloonToolbar.show();
  242. sinon.assert.calledOnce( balloonAddSpy );
  243. } );
  244. it( 'should not add the #toolbarView to the #_balloon when the selection is collapsed', () => {
  245. setData( model, '<paragraph>b[]ar</paragraph>' );
  246. balloonToolbar.show();
  247. sinon.assert.notCalled( balloonAddSpy );
  248. } );
  249. it( 'should not add #toolbarView to the #_balloon when all components inside #toolbarView are disabled', () => {
  250. Array.from( balloonToolbar.toolbarView.items ).forEach( item => {
  251. item.isEnabled = false;
  252. } );
  253. setData( model, '<paragraph>b[a]r</paragraph>' );
  254. balloonToolbar.show();
  255. sinon.assert.notCalled( balloonAddSpy );
  256. } );
  257. it( 'should add #toolbarView to the #_balloon when at least one component inside does not have #isEnabled interface', () => {
  258. Array.from( balloonToolbar.toolbarView.items ).forEach( item => {
  259. item.isEnabled = false;
  260. } );
  261. delete balloonToolbar.toolbarView.items.get( 0 ).isEnabled;
  262. setData( model, '<paragraph>b[a]r</paragraph>' );
  263. balloonToolbar.show();
  264. sinon.assert.calledOnce( balloonAddSpy );
  265. } );
  266. it( 'should set balloon toolbar max-width to half of the editable width', () => {
  267. const spy = sinon.spy( balloonToolbar, '_setToolbarMaxWidth' );
  268. testUtils.sinon.stub( editingView.getDomRoot(), 'getBoundingClientRect' ).returns( { width: 100 } );
  269. setData( model, '<paragraph>b[a]r</paragraph>' );
  270. balloonToolbar.show();
  271. sinon.assert.calledOnce( balloonAddSpy );
  272. sinon.assert.calledOnce( spy );
  273. expect( balloonToolbar.toolbarView.element.style.maxWidth ).to.be.equal( '50px' );
  274. } );
  275. } );
  276. describe( 'hide()', () => {
  277. let removeBalloonSpy;
  278. beforeEach( () => {
  279. removeBalloonSpy = sinon.stub( balloon, 'remove' ).returns( {} );
  280. editingView.document.isFocused = true;
  281. } );
  282. it( 'should remove #toolbarView from the #_balloon', () => {
  283. setData( model, '<paragraph>b[a]r</paragraph>' );
  284. balloonToolbar.show();
  285. balloonToolbar.hide();
  286. sinon.assert.calledWithExactly( removeBalloonSpy, balloonToolbar.toolbarView );
  287. } );
  288. it( 'should stop update balloon position on ui#update event', () => {
  289. setData( model, '<paragraph>b[a]r</paragraph>' );
  290. const spy = sinon.spy( balloon, 'updatePosition' );
  291. balloonToolbar.show();
  292. balloonToolbar.hide();
  293. editor.ui.fire( 'update' );
  294. sinon.assert.notCalled( spy );
  295. } );
  296. it( 'should not remove #toolbarView when is not added to the #_balloon', () => {
  297. balloonToolbar.hide();
  298. sinon.assert.notCalled( removeBalloonSpy );
  299. } );
  300. } );
  301. describe( 'destroy()', () => {
  302. it( 'can be called multiple times', () => {
  303. expect( () => {
  304. balloonToolbar.destroy();
  305. balloonToolbar.destroy();
  306. } ).to.not.throw();
  307. } );
  308. it( 'should not fire `_selectionChangeDebounced` after plugin destroy', () => {
  309. const clock = testUtils.sinon.useFakeTimers();
  310. const spy = testUtils.sinon.spy();
  311. balloonToolbar.on( '_selectionChangeDebounced', spy );
  312. selection.fire( 'change:range', { directChange: true } );
  313. balloonToolbar.destroy();
  314. clock.tick( 200 );
  315. sinon.assert.notCalled( spy );
  316. } );
  317. } );
  318. describe( 'show and hide triggers', () => {
  319. let showPanelSpy, hidePanelSpy;
  320. beforeEach( () => {
  321. setData( model, '<paragraph>[bar]</paragraph>' );
  322. showPanelSpy = sinon.spy( balloonToolbar, 'show' );
  323. hidePanelSpy = sinon.spy( balloonToolbar, 'hide' );
  324. } );
  325. it( 'should show when selection stops changing', () => {
  326. sinon.assert.notCalled( showPanelSpy );
  327. sinon.assert.notCalled( hidePanelSpy );
  328. balloonToolbar.fire( '_selectionChangeDebounced' );
  329. sinon.assert.calledOnce( showPanelSpy );
  330. sinon.assert.notCalled( hidePanelSpy );
  331. } );
  332. it( 'should not show when the selection stops changing when the editable is blurred', () => {
  333. sinon.assert.notCalled( showPanelSpy );
  334. sinon.assert.notCalled( hidePanelSpy );
  335. editingView.document.isFocused = false;
  336. balloonToolbar.fire( '_selectionChangeDebounced' );
  337. sinon.assert.notCalled( showPanelSpy );
  338. sinon.assert.notCalled( hidePanelSpy );
  339. } );
  340. it( 'should hide when selection starts changing by a direct change', () => {
  341. balloonToolbar.fire( '_selectionChangeDebounced' );
  342. sinon.assert.calledOnce( showPanelSpy );
  343. sinon.assert.notCalled( hidePanelSpy );
  344. selection.fire( 'change:range', { directChange: true } );
  345. sinon.assert.calledOnce( showPanelSpy );
  346. sinon.assert.calledOnce( hidePanelSpy );
  347. } );
  348. it( 'should not hide when selection starts changing by an indirect change', () => {
  349. balloonToolbar.fire( '_selectionChangeDebounced' );
  350. sinon.assert.calledOnce( showPanelSpy );
  351. sinon.assert.notCalled( hidePanelSpy );
  352. selection.fire( 'change:range', { directChange: false } );
  353. sinon.assert.calledOnce( showPanelSpy );
  354. sinon.assert.notCalled( hidePanelSpy );
  355. } );
  356. it( 'should hide when selection starts changing by an indirect change but has changed to collapsed', () => {
  357. balloonToolbar.fire( '_selectionChangeDebounced' );
  358. sinon.assert.calledOnce( showPanelSpy );
  359. sinon.assert.notCalled( hidePanelSpy );
  360. // Collapse range silently (without firing `change:range` { directChange: true } event).
  361. const range = selection._ranges[ 0 ];
  362. range.end = range.start;
  363. selection.fire( 'change:range', { directChange: false } );
  364. sinon.assert.calledOnce( showPanelSpy );
  365. sinon.assert.calledOnce( hidePanelSpy );
  366. } );
  367. it( 'should show on #focusTracker focus', () => {
  368. balloonToolbar.focusTracker.isFocused = false;
  369. sinon.assert.notCalled( showPanelSpy );
  370. sinon.assert.notCalled( hidePanelSpy );
  371. balloonToolbar.focusTracker.isFocused = true;
  372. sinon.assert.calledOnce( showPanelSpy );
  373. sinon.assert.notCalled( hidePanelSpy );
  374. } );
  375. it( 'should hide on #focusTracker blur', () => {
  376. balloonToolbar.focusTracker.isFocused = true;
  377. const stub = sinon.stub( balloon, 'visibleView' ).get( () => balloonToolbar.toolbarView );
  378. sinon.assert.calledOnce( showPanelSpy );
  379. sinon.assert.notCalled( hidePanelSpy );
  380. balloonToolbar.focusTracker.isFocused = false;
  381. sinon.assert.calledOnce( showPanelSpy );
  382. sinon.assert.calledOnce( hidePanelSpy );
  383. stub.restore();
  384. } );
  385. it( 'should not hide on #focusTracker blur when toolbar is not in the balloon stack', () => {
  386. balloonToolbar.focusTracker.isFocused = true;
  387. const stub = sinon.stub( balloon, 'visibleView' ).get( () => null );
  388. sinon.assert.calledOnce( showPanelSpy );
  389. sinon.assert.notCalled( hidePanelSpy );
  390. balloonToolbar.focusTracker.isFocused = false;
  391. sinon.assert.calledOnce( showPanelSpy );
  392. sinon.assert.notCalled( hidePanelSpy );
  393. stub.restore();
  394. } );
  395. } );
  396. describe( 'show event', () => {
  397. it( 'should fire `show` event just before panel shows', () => {
  398. const spy = sinon.spy();
  399. balloonToolbar.on( 'show', spy );
  400. setData( model, '<paragraph>b[a]r</paragraph>' );
  401. balloonToolbar.show();
  402. sinon.assert.calledOnce( spy );
  403. } );
  404. it( 'should not show the panel when `show` event is stopped', () => {
  405. const balloonAddSpy = sinon.spy( balloon, 'add' );
  406. setData( model, '<paragraph>b[a]r</paragraph>' );
  407. balloonToolbar.on( 'show', evt => evt.stop(), { priority: 'high' } );
  408. balloonToolbar.show();
  409. sinon.assert.notCalled( balloonAddSpy );
  410. } );
  411. } );
  412. function stubSelectionRects( rects ) {
  413. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  414. // Mock selection rect.
  415. sinon.stub( editingView.domConverter, 'viewRangeToDom' ).callsFake( ( ...args ) => {
  416. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  417. sinon.stub( domRange, 'getClientRects' )
  418. .returns( rects );
  419. return domRange;
  420. } );
  421. }
  422. } );