balloontoolbar.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  11. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  12. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  13. import Underline from '@ckeditor/ckeditor5-basic-styles/src/underline';
  14. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  15. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  16. import { stringify as viewStringify } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  17. /* global document, setTimeout, window */
  18. describe( 'BalloonToolbar', () => {
  19. let sandbox, editor, model, selection, editingView, balloonToolbar, balloon, editorElement;
  20. beforeEach( () => {
  21. sandbox = sinon.sandbox.create();
  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. sandbox.stub( balloon.view, 'attachTo' ).returns( {} );
  39. sandbox.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. sandbox.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 = sandbox.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( 'show()', () => {
  116. let balloonAddSpy, backwardSelectionRect, forwardSelectionRect;
  117. beforeEach( () => {
  118. backwardSelectionRect = {
  119. top: 100,
  120. height: 10,
  121. bottom: 110,
  122. left: 200,
  123. width: 50,
  124. right: 250
  125. };
  126. forwardSelectionRect = {
  127. top: 200,
  128. height: 10,
  129. bottom: 210,
  130. left: 200,
  131. width: 50,
  132. right: 250
  133. };
  134. stubSelectionRects( [
  135. backwardSelectionRect,
  136. forwardSelectionRect
  137. ] );
  138. balloonAddSpy = sandbox.spy( balloon, 'add' );
  139. editingView.document.isFocused = true;
  140. } );
  141. it( 'should add #toolbarView to the #_balloon and attach the #_balloon to the selection for the forward selection', () => {
  142. setData( model, '<paragraph>b[a]r</paragraph>' );
  143. const defaultPositions = BalloonPanelView.defaultPositions;
  144. balloonToolbar.show();
  145. sinon.assert.calledWith( balloonAddSpy, {
  146. view: balloonToolbar.toolbarView,
  147. balloonClassName: 'ck-toolbar-container',
  148. position: {
  149. target: sinon.match.func,
  150. positions: [
  151. defaultPositions.southEastArrowNorth,
  152. defaultPositions.southEastArrowNorthEast,
  153. defaultPositions.southEastArrowNorthWest,
  154. defaultPositions.northEastArrowSouth,
  155. defaultPositions.northEastArrowSouthEast,
  156. defaultPositions.northEastArrowSouthWest
  157. ]
  158. }
  159. } );
  160. expect( balloonAddSpy.firstCall.args[ 0 ].position.target() ).to.deep.equal( forwardSelectionRect );
  161. } );
  162. // https://github.com/ckeditor/ckeditor5-ui/issues/385
  163. it( 'should attach the #_balloon to the last range in a case of multi-range forward selection', () => {
  164. setData( model, '<paragraph>b[ar]</paragraph><paragraph>[bi]z</paragraph>' );
  165. balloonToolbar.show();
  166. // Because attaching and pinning BalloonPanelView is stubbed for test
  167. // we need to manually call function that counting rect.
  168. const targetRect = balloonAddSpy.firstCall.args[ 0 ].position.target();
  169. const targetViewRange = editingView.domConverter.viewRangeToDom.lastCall.args[ 0 ];
  170. expect( viewStringify( targetViewRange.root, targetViewRange ) ).to.equal( '<div><p>bar</p><p>{bi}z</p></div>' );
  171. expect( targetRect ).to.deep.equal( forwardSelectionRect );
  172. } );
  173. // https://github.com/ckeditor/ckeditor5-ui/issues/308
  174. it( 'should ignore the zero-width orphan rect if there another one preceding it for the forward selection', () => {
  175. // Restore previous stubSelectionRects() call.
  176. editingView.domConverter.viewRangeToDom.restore();
  177. // Simulate an "orphan" rect preceded by a "correct" one.
  178. stubSelectionRects( [
  179. forwardSelectionRect,
  180. { width: 0 }
  181. ] );
  182. setData( model, '<paragraph>b[a]r</paragraph>' );
  183. balloonToolbar.show();
  184. expect( balloonAddSpy.firstCall.args[ 0 ].position.target() ).to.deep.equal( forwardSelectionRect );
  185. } );
  186. it( 'should add #toolbarView to the #_balloon and attach the #_balloon to the selection for the backward selection', () => {
  187. setData( model, '<paragraph>b[a]r</paragraph>', { lastRangeBackward: true } );
  188. const defaultPositions = BalloonPanelView.defaultPositions;
  189. balloonToolbar.show();
  190. sinon.assert.calledWithExactly( balloonAddSpy, {
  191. view: balloonToolbar.toolbarView,
  192. balloonClassName: 'ck-toolbar-container',
  193. position: {
  194. target: sinon.match.func,
  195. positions: [
  196. defaultPositions.northWestArrowSouth,
  197. defaultPositions.northWestArrowSouthWest,
  198. defaultPositions.northWestArrowSouthEast,
  199. defaultPositions.southWestArrowNorth,
  200. defaultPositions.southWestArrowNorthWest,
  201. defaultPositions.southWestArrowNorthEast
  202. ]
  203. }
  204. } );
  205. expect( balloonAddSpy.firstCall.args[ 0 ].position.target() ).to.deep.equal( backwardSelectionRect );
  206. } );
  207. // https://github.com/ckeditor/ckeditor5-ui/issues/385
  208. it( 'should attach the #_balloon to the first range in a case of multi-range backward selection', () => {
  209. setData( model, '<paragraph>b[ar]</paragraph><paragraph>[bi]z</paragraph>', { lastRangeBackward: true } );
  210. balloonToolbar.show();
  211. // Because attaching and pinning BalloonPanelView is stubbed for test
  212. // we need to manually call function that counting rect.
  213. const targetRect = balloonAddSpy.firstCall.args[ 0 ].position.target();
  214. const targetViewRange = editingView.domConverter.viewRangeToDom.lastCall.args[ 0 ];
  215. expect( viewStringify( targetViewRange.root, targetViewRange ) ).to.equal( '<div><p>b{ar}</p><p>biz</p></div>' );
  216. expect( targetRect ).to.deep.equal( backwardSelectionRect );
  217. } );
  218. it( 'should update balloon position on view#change event while balloon is added to the #_balloon', () => {
  219. setData( model, '<paragraph>b[a]r</paragraph>' );
  220. const spy = sandbox.spy( balloon, 'updatePosition' );
  221. editingView.fire( 'render' );
  222. balloonToolbar.show();
  223. sinon.assert.notCalled( spy );
  224. editingView.fire( 'render' );
  225. sinon.assert.calledOnce( spy );
  226. } );
  227. it( 'should not add #toolbarView to the #_balloon more than once', () => {
  228. setData( model, '<paragraph>b[a]r</paragraph>' );
  229. balloonToolbar.show();
  230. balloonToolbar.show();
  231. sinon.assert.calledOnce( balloonAddSpy );
  232. } );
  233. it( 'should not add #toolbarView to the #_balloon when the selection is collapsed', () => {
  234. setData( model, '<paragraph>b[]ar</paragraph>' );
  235. balloonToolbar.show();
  236. sinon.assert.notCalled( balloonAddSpy );
  237. } );
  238. it( 'should not add #toolbarView to the #_balloon when all components inside #toolbarView are disabled', () => {
  239. Array.from( balloonToolbar.toolbarView.items ).forEach( item => {
  240. item.isEnabled = false;
  241. } );
  242. setData( model, '<paragraph>b[a]r</paragraph>' );
  243. balloonToolbar.show();
  244. sinon.assert.notCalled( balloonAddSpy );
  245. } );
  246. it( 'should add #toolbarView to the #_balloon when at least one component inside does not have #isEnabled interface', () => {
  247. Array.from( balloonToolbar.toolbarView.items ).forEach( item => {
  248. item.isEnabled = false;
  249. } );
  250. delete balloonToolbar.toolbarView.items.get( 0 ).isEnabled;
  251. setData( model, '<paragraph>b[a]r</paragraph>' );
  252. balloonToolbar.show();
  253. sinon.assert.calledOnce( balloonAddSpy );
  254. } );
  255. describe( 'on #_selectionChangeDebounced event', () => {
  256. let showSpy;
  257. beforeEach( () => {
  258. showSpy = sandbox.spy( balloonToolbar, 'show' );
  259. } );
  260. it( 'should not be called when the editable is not focused', () => {
  261. setData( model, '<paragraph>b[a]r</paragraph>' );
  262. editingView.document.isFocused = false;
  263. balloonToolbar.toolbarView.focusTracker.isFocused = false;
  264. balloonToolbar.fire( '_selectionChangeDebounced' );
  265. sinon.assert.notCalled( showSpy );
  266. } );
  267. it( 'should be called when the editable is focused', () => {
  268. setData( model, '<paragraph>b[a]r</paragraph>' );
  269. editingView.document.isFocused = true;
  270. balloonToolbar.toolbarView.focusTracker.isFocused = false;
  271. balloonToolbar.fire( '_selectionChangeDebounced' );
  272. sinon.assert.calledOnce( showSpy );
  273. } );
  274. } );
  275. } );
  276. describe( 'hide()', () => {
  277. let removeBalloonSpy;
  278. beforeEach( () => {
  279. removeBalloonSpy = sandbox.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 ViewDocument#change event', () => {
  289. setData( model, '<paragraph>b[a]r</paragraph>' );
  290. const spy = sandbox.spy( balloon, 'updatePosition' );
  291. balloonToolbar.show();
  292. balloonToolbar.hide();
  293. editingView.fire( 'render' );
  294. sinon.assert.notCalled( spy );
  295. } );
  296. it( 'should not remove #ttolbarView 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', done => {
  309. const spy = sandbox.spy();
  310. balloonToolbar.on( '_selectionChangeDebounced', spy );
  311. selection.fire( 'change:range', { directChange: true } );
  312. balloonToolbar.destroy();
  313. setTimeout( () => {
  314. sinon.assert.notCalled( spy );
  315. done();
  316. }, 200 );
  317. } );
  318. } );
  319. describe( 'showing and hiding', () => {
  320. let showPanelSpy, hidePanelSpy;
  321. beforeEach( () => {
  322. setData( model, '<paragraph>[bar]</paragraph>' );
  323. showPanelSpy = sandbox.spy( balloonToolbar, 'show' );
  324. hidePanelSpy = sandbox.spy( balloonToolbar, 'hide' );
  325. } );
  326. it( 'should open when selection stops changing', () => {
  327. sinon.assert.notCalled( showPanelSpy );
  328. sinon.assert.notCalled( hidePanelSpy );
  329. balloonToolbar.fire( '_selectionChangeDebounced' );
  330. sinon.assert.calledOnce( showPanelSpy );
  331. sinon.assert.notCalled( hidePanelSpy );
  332. } );
  333. it( 'should close when selection starts changing by a directChange', () => {
  334. balloonToolbar.fire( '_selectionChangeDebounced' );
  335. sinon.assert.calledOnce( showPanelSpy );
  336. sinon.assert.notCalled( hidePanelSpy );
  337. selection.fire( 'change:range', { directChange: true } );
  338. sinon.assert.calledOnce( showPanelSpy );
  339. sinon.assert.calledOnce( hidePanelSpy );
  340. } );
  341. it( 'should not close when selection starts changing by not a directChange', () => {
  342. balloonToolbar.fire( '_selectionChangeDebounced' );
  343. sinon.assert.calledOnce( showPanelSpy );
  344. sinon.assert.notCalled( hidePanelSpy );
  345. selection.fire( 'change:range', { directChange: false } );
  346. sinon.assert.calledOnce( showPanelSpy );
  347. sinon.assert.notCalled( hidePanelSpy );
  348. } );
  349. it( 'should close when selection starts changing by not a directChange but will become collapsed', () => {
  350. balloonToolbar.fire( '_selectionChangeDebounced' );
  351. sinon.assert.calledOnce( showPanelSpy );
  352. sinon.assert.notCalled( hidePanelSpy );
  353. // Collapse range silently (without firing `change:range` { directChange: true } event).
  354. const range = selection._ranges[ 0 ];
  355. range.end = range.start;
  356. selection.fire( 'change:range', { directChange: false } );
  357. sinon.assert.calledOnce( showPanelSpy );
  358. sinon.assert.calledOnce( hidePanelSpy );
  359. } );
  360. it( 'should hide when the editable loses focus', () => {
  361. editor.editing.view.document.isFocused = true;
  362. balloonToolbar.fire( '_selectionChangeDebounced' );
  363. const stub = sandbox.stub( balloon, 'visibleView' ).get( () => balloonToolbar.toolbarView );
  364. sinon.assert.calledOnce( showPanelSpy );
  365. sinon.assert.notCalled( hidePanelSpy );
  366. editor.editing.view.document.isFocused = false;
  367. sinon.assert.calledOnce( showPanelSpy );
  368. sinon.assert.calledOnce( hidePanelSpy );
  369. stub.restore();
  370. } );
  371. it( 'should not hide if the editor loses focus and #toolbarView is not visible', () => {
  372. editor.ui.focusTracker.isFocused = true;
  373. balloonToolbar.fire( '_selectionChangeDebounced' );
  374. const stub = sandbox.stub( balloon, 'visibleView' ).get( () => null );
  375. sinon.assert.calledOnce( showPanelSpy );
  376. sinon.assert.notCalled( hidePanelSpy );
  377. editor.ui.focusTracker.isFocused = false;
  378. sinon.assert.calledOnce( showPanelSpy );
  379. sinon.assert.notCalled( hidePanelSpy );
  380. stub.restore();
  381. } );
  382. } );
  383. describe( 'show event', () => {
  384. it( 'should fire `show` event just before panel shows', () => {
  385. const spy = sandbox.spy();
  386. balloonToolbar.on( 'show', spy );
  387. setData( model, '<paragraph>b[a]r</paragraph>' );
  388. balloonToolbar.show();
  389. sinon.assert.calledOnce( spy );
  390. } );
  391. it( 'should not show the panel when `show` event is stopped', () => {
  392. const balloonAddSpy = sandbox.spy( balloon, 'add' );
  393. setData( model, '<paragraph>b[a]r</paragraph>' );
  394. balloonToolbar.on( 'show', evt => evt.stop(), { priority: 'high' } );
  395. balloonToolbar.show();
  396. sinon.assert.notCalled( balloonAddSpy );
  397. } );
  398. } );
  399. function stubSelectionRects( rects ) {
  400. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  401. // Mock selection rect.
  402. sandbox.stub( editingView.domConverter, 'viewRangeToDom' ).callsFake( ( ...args ) => {
  403. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  404. sandbox.stub( domRange, 'getClientRects' )
  405. .returns( rects );
  406. return domRange;
  407. } );
  408. }
  409. } );