balloontoolbar.js 17 KB

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