balloontoolbar.js 17 KB

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