8
0

contextualtoolbar.js 15 KB

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