balloontoolbar.js 17 KB

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