balloontoolbar.js 17 KB

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