8
0

balloontoolbar.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 all components inside #toolbarView are disabled', () => {
  234. Array.from( balloonToolbar.toolbarView.items ).forEach( item => {
  235. item.isEnabled = false;
  236. } );
  237. setData( model, '<paragraph>b[a]r</paragraph>' );
  238. balloonToolbar.show();
  239. sinon.assert.notCalled( balloonAddSpy );
  240. } );
  241. it( 'should add #toolbarView to the #_balloon when at least one component inside does not have #isEnabled interface', () => {
  242. Array.from( balloonToolbar.toolbarView.items ).forEach( item => {
  243. item.isEnabled = false;
  244. } );
  245. delete balloonToolbar.toolbarView.items.get( 0 ).isEnabled;
  246. setData( model, '<paragraph>b[a]r</paragraph>' );
  247. balloonToolbar.show();
  248. sinon.assert.calledOnce( balloonAddSpy );
  249. } );
  250. describe( 'on #_selectionChangeDebounced event', () => {
  251. let showSpy;
  252. beforeEach( () => {
  253. showSpy = sandbox.spy( balloonToolbar, 'show' );
  254. } );
  255. it( 'should not be called when the editor is not focused', () => {
  256. setData( model, '<paragraph>b[a]r</paragraph>' );
  257. editingView.document.isFocused = false;
  258. balloonToolbar.fire( '_selectionChangeDebounced' );
  259. sinon.assert.notCalled( showSpy );
  260. } );
  261. it( 'should not be called when the selection is collapsed', () => {
  262. setData( model, '<paragraph>b[]ar</paragraph>' );
  263. balloonToolbar.fire( '_selectionChangeDebounced' );
  264. sinon.assert.notCalled( showSpy );
  265. } );
  266. it( 'should be called when the selection is not collapsed and editor is focused', () => {
  267. setData( model, '<paragraph>b[a]r</paragraph>' );
  268. editingView.document.isFocused = true;
  269. balloonToolbar.fire( '_selectionChangeDebounced' );
  270. sinon.assert.calledOnce( showSpy );
  271. } );
  272. } );
  273. } );
  274. describe( 'hide()', () => {
  275. let removeBalloonSpy;
  276. beforeEach( () => {
  277. removeBalloonSpy = sandbox.stub( balloon, 'remove' ).returns( {} );
  278. editingView.document.isFocused = true;
  279. } );
  280. it( 'should remove #toolbarView from the #_balloon', () => {
  281. setData( model, '<paragraph>b[a]r</paragraph>' );
  282. balloonToolbar.show();
  283. balloonToolbar.hide();
  284. sinon.assert.calledWithExactly( removeBalloonSpy, balloonToolbar.toolbarView );
  285. } );
  286. it( 'should stop update balloon position on ViewDocument#change event', () => {
  287. setData( model, '<paragraph>b[a]r</paragraph>' );
  288. const spy = sandbox.spy( balloon, 'updatePosition' );
  289. balloonToolbar.show();
  290. balloonToolbar.hide();
  291. editingView.fire( 'render' );
  292. sinon.assert.notCalled( spy );
  293. } );
  294. it( 'should not remove #ttolbarView when is not added to the #_balloon', () => {
  295. balloonToolbar.hide();
  296. sinon.assert.notCalled( removeBalloonSpy );
  297. } );
  298. } );
  299. describe( 'destroy()', () => {
  300. it( 'can be called multiple times', () => {
  301. expect( () => {
  302. balloonToolbar.destroy();
  303. balloonToolbar.destroy();
  304. } ).to.not.throw();
  305. } );
  306. it( 'should not fire `_selectionChangeDebounced` after plugin destroy', done => {
  307. const spy = sandbox.spy();
  308. balloonToolbar.on( '_selectionChangeDebounced', spy );
  309. selection.fire( 'change:range', { directChange: true } );
  310. balloonToolbar.destroy();
  311. setTimeout( () => {
  312. sinon.assert.notCalled( spy );
  313. done();
  314. }, 200 );
  315. } );
  316. } );
  317. describe( 'showing and hiding', () => {
  318. let showPanelSpy, hidePanelSpy;
  319. beforeEach( () => {
  320. setData( model, '<paragraph>[bar]</paragraph>' );
  321. showPanelSpy = sandbox.spy( balloonToolbar, 'show' );
  322. hidePanelSpy = sandbox.spy( balloonToolbar, 'hide' );
  323. } );
  324. it( 'should open when selection stops changing', () => {
  325. sinon.assert.notCalled( showPanelSpy );
  326. sinon.assert.notCalled( hidePanelSpy );
  327. balloonToolbar.fire( '_selectionChangeDebounced' );
  328. sinon.assert.calledOnce( showPanelSpy );
  329. sinon.assert.notCalled( hidePanelSpy );
  330. } );
  331. it( 'should close when selection starts changing by a directChange', () => {
  332. balloonToolbar.fire( '_selectionChangeDebounced' );
  333. sinon.assert.calledOnce( showPanelSpy );
  334. sinon.assert.notCalled( hidePanelSpy );
  335. selection.fire( 'change:range', { directChange: true } );
  336. sinon.assert.calledOnce( showPanelSpy );
  337. sinon.assert.calledOnce( hidePanelSpy );
  338. } );
  339. it( 'should not close when selection starts changing by not a directChange', () => {
  340. balloonToolbar.fire( '_selectionChangeDebounced' );
  341. sinon.assert.calledOnce( showPanelSpy );
  342. sinon.assert.notCalled( hidePanelSpy );
  343. selection.fire( 'change:range', { directChange: false } );
  344. sinon.assert.calledOnce( showPanelSpy );
  345. sinon.assert.notCalled( hidePanelSpy );
  346. } );
  347. it( 'should close when selection starts changing by not a directChange but will become collapsed', () => {
  348. balloonToolbar.fire( '_selectionChangeDebounced' );
  349. sinon.assert.calledOnce( showPanelSpy );
  350. sinon.assert.notCalled( hidePanelSpy );
  351. // Collapse range silently (without firing `change:range` { directChange: true } event).
  352. const range = selection._ranges[ 0 ];
  353. range.end = range.start;
  354. selection.fire( 'change:range', { directChange: false } );
  355. sinon.assert.calledOnce( showPanelSpy );
  356. sinon.assert.calledOnce( hidePanelSpy );
  357. } );
  358. it( 'should hide if the editor loses focus', () => {
  359. editor.ui.focusTracker.isFocused = true;
  360. balloonToolbar.fire( '_selectionChangeDebounced' );
  361. const stub = sandbox.stub( balloon, 'visibleView' ).get( () => balloonToolbar.toolbarView );
  362. sinon.assert.calledOnce( showPanelSpy );
  363. sinon.assert.notCalled( hidePanelSpy );
  364. editor.ui.focusTracker.isFocused = false;
  365. sinon.assert.calledOnce( showPanelSpy );
  366. sinon.assert.calledOnce( hidePanelSpy );
  367. stub.restore();
  368. } );
  369. it( 'should not hide if the editor loses focus and #toolbarView is not visible', () => {
  370. editor.ui.focusTracker.isFocused = true;
  371. balloonToolbar.fire( '_selectionChangeDebounced' );
  372. const stub = sandbox.stub( balloon, 'visibleView' ).get( () => null );
  373. sinon.assert.calledOnce( showPanelSpy );
  374. sinon.assert.notCalled( hidePanelSpy );
  375. editor.ui.focusTracker.isFocused = false;
  376. sinon.assert.calledOnce( showPanelSpy );
  377. sinon.assert.notCalled( hidePanelSpy );
  378. stub.restore();
  379. } );
  380. } );
  381. describe( 'show event', () => {
  382. it( 'should fire `show` event just before panel shows', () => {
  383. const spy = sandbox.spy();
  384. balloonToolbar.on( 'show', spy );
  385. setData( model, '<paragraph>b[a]r</paragraph>' );
  386. balloonToolbar.show();
  387. sinon.assert.calledOnce( spy );
  388. } );
  389. it( 'should not show the panel when `show` event is stopped', () => {
  390. const balloonAddSpy = sandbox.spy( balloon, 'add' );
  391. setData( model, '<paragraph>b[a]r</paragraph>' );
  392. balloonToolbar.on( 'show', evt => evt.stop(), { priority: 'high' } );
  393. balloonToolbar.show();
  394. sinon.assert.notCalled( balloonAddSpy );
  395. } );
  396. } );
  397. function stubSelectionRects( rects ) {
  398. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  399. // Mock selection rect.
  400. sandbox.stub( editingView.domConverter, 'viewRangeToDom' ).callsFake( ( ...args ) => {
  401. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  402. sandbox.stub( domRange, 'getClientRects' )
  403. .returns( rects );
  404. return domRange;
  405. } );
  406. }
  407. } );