contextualtoolbar.js 14 KB

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