8
0

contextualtoolbar.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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( 'can be called multiple times', () => {
  230. expect( () => {
  231. contextualToolbar.destroy();
  232. contextualToolbar.destroy();
  233. } ).to.not.throw();
  234. } );
  235. it( 'should not fire `_selectionChangeDebounced` after plugin destroy', done => {
  236. const spy = sandbox.spy();
  237. contextualToolbar.on( '_selectionChangeDebounced', spy );
  238. editor.document.selection.fire( 'change:range', { directChange: true } );
  239. contextualToolbar.destroy();
  240. setTimeout( () => {
  241. sinon.assert.notCalled( spy );
  242. done();
  243. }, 200 );
  244. } );
  245. } );
  246. describe( 'showing and hiding', () => {
  247. let showPanelSpy, hidePanelSpy;
  248. beforeEach( () => {
  249. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  250. // Methods are stubbed because return internal promise which can't be returned in test.
  251. showPanelSpy = sandbox.stub( contextualToolbar, '_showPanel', () => {} );
  252. hidePanelSpy = sandbox.stub( contextualToolbar, '_hidePanel', () => {} );
  253. } );
  254. it( 'should open when selection stops changing', () => {
  255. sinon.assert.notCalled( showPanelSpy );
  256. sinon.assert.notCalled( hidePanelSpy );
  257. contextualToolbar.fire( '_selectionChangeDebounced' );
  258. sinon.assert.calledOnce( showPanelSpy );
  259. sinon.assert.notCalled( hidePanelSpy );
  260. } );
  261. it( 'should close when selection starts changing by a directChange', () => {
  262. contextualToolbar.fire( '_selectionChangeDebounced' );
  263. sinon.assert.calledOnce( showPanelSpy );
  264. sinon.assert.notCalled( hidePanelSpy );
  265. editor.document.selection.fire( 'change:range', { directChange: true } );
  266. sinon.assert.calledOnce( showPanelSpy );
  267. sinon.assert.calledOnce( hidePanelSpy );
  268. } );
  269. it( 'should not close when selection starts changing by not a directChange', () => {
  270. contextualToolbar.fire( '_selectionChangeDebounced' );
  271. sinon.assert.calledOnce( showPanelSpy );
  272. sinon.assert.notCalled( hidePanelSpy );
  273. editor.document.selection.fire( 'change:range', { directChange: false } );
  274. sinon.assert.calledOnce( showPanelSpy );
  275. sinon.assert.notCalled( hidePanelSpy );
  276. } );
  277. it( 'should close when selection starts changing by not a directChange but will become collapsed', () => {
  278. contextualToolbar.fire( '_selectionChangeDebounced' );
  279. sinon.assert.calledOnce( showPanelSpy );
  280. sinon.assert.notCalled( hidePanelSpy );
  281. // Collapse range silently (without firing `change:range` { directChange: true } event).
  282. const range = editor.document.selection._ranges[ 0 ];
  283. range.end = range.start;
  284. editor.document.selection.fire( 'change:range', { directChange: false } );
  285. sinon.assert.calledOnce( showPanelSpy );
  286. sinon.assert.calledOnce( hidePanelSpy );
  287. } );
  288. it( 'should hide if the editor loses focus', () => {
  289. editor.ui.focusTracker.isFocused = true;
  290. contextualToolbar.fire( '_selectionChangeDebounced' );
  291. // Stubbing getters doesn't wor for sandbox.
  292. const stub = sinon.stub( balloon, 'visibleView', { get: () => contextualToolbar.toolbarView } );
  293. sinon.assert.calledOnce( showPanelSpy );
  294. sinon.assert.notCalled( hidePanelSpy );
  295. editor.ui.focusTracker.isFocused = false;
  296. sinon.assert.calledOnce( showPanelSpy );
  297. sinon.assert.calledOnce( hidePanelSpy );
  298. stub.restore();
  299. } );
  300. it( 'should not hide if the editor loses focus and #toolbarView is not visible', () => {
  301. editor.ui.focusTracker.isFocused = true;
  302. contextualToolbar.fire( '_selectionChangeDebounced' );
  303. // Stubbing getters doesn't wor for sandbox.
  304. const stub = sinon.stub( balloon, 'visibleView', { get: () => null } );
  305. sinon.assert.calledOnce( showPanelSpy );
  306. sinon.assert.notCalled( hidePanelSpy );
  307. editor.ui.focusTracker.isFocused = false;
  308. sinon.assert.calledOnce( showPanelSpy );
  309. sinon.assert.notCalled( hidePanelSpy );
  310. stub.restore();
  311. } );
  312. } );
  313. describe( 'beforeShow event', () => {
  314. it( 'should fire `beforeShow` event just before panel shows', () => {
  315. const spy = sinon.spy();
  316. contextualToolbar.on( 'beforeShow', spy );
  317. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  318. const promise = contextualToolbar._showPanel();
  319. sinon.assert.calledOnce( spy );
  320. return promise;
  321. } );
  322. it( 'should not show the panel when `beforeShow` event is stopped', () => {
  323. const balloonAddSpy = sandbox.spy( balloon, 'add' );
  324. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  325. contextualToolbar.on( 'beforeShow', ( evt, stop ) => {
  326. stop();
  327. } );
  328. return contextualToolbar._showPanel().then( () => {
  329. sinon.assert.notCalled( balloonAddSpy );
  330. } );
  331. } );
  332. } );
  333. function stubSelectionRect( forwardSelectionRect, backwardSelectionRect ) {
  334. const editingView = editor.editing.view;
  335. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  336. // Mock selection rect.
  337. sandbox.stub( editingView.domConverter, 'viewRangeToDom', ( ...args ) => {
  338. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  339. sandbox.stub( domRange, 'getClientRects', () => {
  340. return {
  341. length: 2,
  342. item( id ) {
  343. return id === 0 ? forwardSelectionRect : backwardSelectionRect;
  344. }
  345. };
  346. } );
  347. return domRange;
  348. } );
  349. }
  350. } );