8
0

contextualtoolbar.js 15 KB

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