8
0

contextualtoolbar.js 16 KB

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