8
0

contextualtoolbar.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. 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( '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 add #toolbarView to the #_balloon and attach the #_balloon to the selection for the forward selection', () => {
  114. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  115. const defaultPositions = BalloonPanelView.defaultPositions;
  116. contextualToolbar._showPanel();
  117. sinon.assert.calledWithExactly( balloonAddSpy, {
  118. view: contextualToolbar.toolbarView,
  119. balloonClassName: 'ck-toolbar-container ck-editor-toolbar-container',
  120. position: {
  121. target: sinon.match( value => value() == backwardSelectionRect ),
  122. limiter: editor.ui.view.editable.element,
  123. positions: [
  124. defaultPositions.southEastArrowNorth,
  125. defaultPositions.southEastArrowNorthEast,
  126. defaultPositions.southEastArrowNorthWest,
  127. defaultPositions.northEastArrowSouth,
  128. defaultPositions.northEastArrowSouthEast,
  129. defaultPositions.northEastArrowSouthWest
  130. ]
  131. }
  132. } );
  133. } );
  134. it( 'should add #toolbarView to the #_balloon and attach the #_balloon to the selection for the backward selection', () => {
  135. setData( editor.document, '<paragraph>b[a]r</paragraph>', { lastRangeBackward: true } );
  136. const defaultPositions = BalloonPanelView.defaultPositions;
  137. contextualToolbar._showPanel();
  138. sinon.assert.calledWithExactly( balloonAddSpy, {
  139. view: contextualToolbar.toolbarView,
  140. balloonClassName: 'ck-toolbar-container ck-editor-toolbar-container',
  141. position: {
  142. target: sinon.match( value => value() == forwardSelectionRect ),
  143. limiter: editor.ui.view.editable.element,
  144. positions: [
  145. defaultPositions.northWestArrowSouth,
  146. defaultPositions.northWestArrowSouthWest,
  147. defaultPositions.northWestArrowSouthEast,
  148. defaultPositions.southWestArrowNorth,
  149. defaultPositions.southWestArrowNorthWest,
  150. defaultPositions.southWestArrowNorthEast
  151. ]
  152. }
  153. } );
  154. } );
  155. it( 'should update balloon position on ViewDocument#render event while balloon is added to the #_balloon', () => {
  156. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  157. const spy = sandbox.spy( balloon, 'updatePosition' );
  158. editor.editing.view.fire( 'render' );
  159. contextualToolbar._showPanel();
  160. sinon.assert.notCalled( spy );
  161. editor.editing.view.fire( 'render' );
  162. sinon.assert.calledOnce( spy );
  163. } );
  164. it( 'should not add #toolbarView to the #_balloon more than once', () => {
  165. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  166. contextualToolbar._showPanel();
  167. contextualToolbar._showPanel();
  168. sinon.assert.calledOnce( balloonAddSpy );
  169. } );
  170. it( 'should not add #toolbarView to the #_balloon when editor is not focused', () => {
  171. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  172. editor.editing.view.isFocused = false;
  173. contextualToolbar._showPanel();
  174. sinon.assert.notCalled( balloonAddSpy );
  175. } );
  176. it( 'should not add #toolbarView to the #_balloon when selection is collapsed', () => {
  177. setData( editor.document, '<paragraph>b[]ar</paragraph>' );
  178. contextualToolbar._showPanel();
  179. sinon.assert.notCalled( balloonAddSpy );
  180. } );
  181. } );
  182. describe( '_hidePanel()', () => {
  183. let removeBalloonSpy;
  184. beforeEach( () => {
  185. removeBalloonSpy = sandbox.stub( balloon, 'remove', () => {} );
  186. editor.editing.view.isFocused = true;
  187. } );
  188. it( 'should remove #toolbarView from the #_balloon', () => {
  189. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  190. contextualToolbar._showPanel();
  191. contextualToolbar._hidePanel();
  192. sinon.assert.calledWithExactly( removeBalloonSpy, contextualToolbar.toolbarView );
  193. } );
  194. it( 'should stop update balloon position on ViewDocument#render event', () => {
  195. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  196. const spy = sandbox.spy( balloon, 'updatePosition' );
  197. contextualToolbar._showPanel();
  198. contextualToolbar._hidePanel();
  199. editor.editing.view.fire( 'render' );
  200. sinon.assert.notCalled( spy );
  201. } );
  202. it( 'should not remove #ttolbarView when is not added to the #_balloon', () => {
  203. contextualToolbar._hidePanel();
  204. sinon.assert.notCalled( removeBalloonSpy );
  205. } );
  206. } );
  207. describe( 'destroy()', () => {
  208. it( 'can be called multiple times', () => {
  209. expect( () => {
  210. contextualToolbar.destroy();
  211. contextualToolbar.destroy();
  212. } ).to.not.throw();
  213. } );
  214. it( 'should not fire `_selectionChangeDebounced` after plugin destroy', done => {
  215. const spy = sandbox.spy();
  216. contextualToolbar.on( '_selectionChangeDebounced', spy );
  217. editor.document.selection.fire( 'change:range', { directChange: true } );
  218. contextualToolbar.destroy();
  219. setTimeout( () => {
  220. sinon.assert.notCalled( spy );
  221. done();
  222. }, 200 );
  223. } );
  224. } );
  225. describe( 'showing and hiding', () => {
  226. let showPanelSpy, hidePanelSpy;
  227. beforeEach( () => {
  228. setData( editor.document, '<paragraph>[bar]</paragraph>' );
  229. showPanelSpy = sandbox.spy( contextualToolbar, '_showPanel' );
  230. hidePanelSpy = sandbox.spy( contextualToolbar, '_hidePanel' );
  231. } );
  232. it( 'should open when selection stops changing', () => {
  233. sinon.assert.notCalled( showPanelSpy );
  234. sinon.assert.notCalled( hidePanelSpy );
  235. contextualToolbar.fire( '_selectionChangeDebounced' );
  236. sinon.assert.calledOnce( showPanelSpy );
  237. sinon.assert.notCalled( hidePanelSpy );
  238. } );
  239. it( 'should close when selection starts changing by a directChange', () => {
  240. contextualToolbar.fire( '_selectionChangeDebounced' );
  241. sinon.assert.calledOnce( showPanelSpy );
  242. sinon.assert.notCalled( hidePanelSpy );
  243. editor.document.selection.fire( 'change:range', { directChange: true } );
  244. sinon.assert.calledOnce( showPanelSpy );
  245. sinon.assert.calledOnce( hidePanelSpy );
  246. } );
  247. it( 'should not close when selection starts changing by not a directChange', () => {
  248. contextualToolbar.fire( '_selectionChangeDebounced' );
  249. sinon.assert.calledOnce( showPanelSpy );
  250. sinon.assert.notCalled( hidePanelSpy );
  251. editor.document.selection.fire( 'change:range', { directChange: false } );
  252. sinon.assert.calledOnce( showPanelSpy );
  253. sinon.assert.notCalled( hidePanelSpy );
  254. } );
  255. it( 'should close when selection starts changing by not a directChange but will become collapsed', () => {
  256. contextualToolbar.fire( '_selectionChangeDebounced' );
  257. sinon.assert.calledOnce( showPanelSpy );
  258. sinon.assert.notCalled( hidePanelSpy );
  259. // Collapse range silently (without firing `change:range` { directChange: true } event).
  260. const range = editor.document.selection._ranges[ 0 ];
  261. range.end = range.start;
  262. editor.document.selection.fire( 'change:range', { directChange: false } );
  263. sinon.assert.calledOnce( showPanelSpy );
  264. sinon.assert.calledOnce( hidePanelSpy );
  265. } );
  266. it( 'should hide if the editor loses focus', () => {
  267. editor.ui.focusTracker.isFocused = true;
  268. contextualToolbar.fire( '_selectionChangeDebounced' );
  269. // Stubbing getters doesn't wor for sandbox.
  270. const stub = sinon.stub( balloon, 'visibleView', { get: () => contextualToolbar.toolbarView } );
  271. sinon.assert.calledOnce( showPanelSpy );
  272. sinon.assert.notCalled( hidePanelSpy );
  273. editor.ui.focusTracker.isFocused = false;
  274. sinon.assert.calledOnce( showPanelSpy );
  275. sinon.assert.calledOnce( hidePanelSpy );
  276. stub.restore();
  277. } );
  278. it( 'should not hide if the editor loses focus and #toolbarView is not visible', () => {
  279. editor.ui.focusTracker.isFocused = true;
  280. contextualToolbar.fire( '_selectionChangeDebounced' );
  281. // Stubbing getters doesn't wor for sandbox.
  282. const stub = sinon.stub( balloon, 'visibleView', { get: () => null } );
  283. sinon.assert.calledOnce( showPanelSpy );
  284. sinon.assert.notCalled( hidePanelSpy );
  285. editor.ui.focusTracker.isFocused = false;
  286. sinon.assert.calledOnce( showPanelSpy );
  287. sinon.assert.notCalled( hidePanelSpy );
  288. stub.restore();
  289. } );
  290. } );
  291. describe( 'beforeShow event', () => {
  292. it( 'should fire `beforeShow` event just before panel shows', () => {
  293. const spy = sinon.spy();
  294. contextualToolbar.on( 'beforeShow', spy );
  295. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  296. contextualToolbar._showPanel();
  297. sinon.assert.calledOnce( spy );
  298. } );
  299. it( 'should not show the panel when `beforeShow` event is stopped', () => {
  300. const balloonAddSpy = sandbox.spy( balloon, 'add' );
  301. setData( editor.document, '<paragraph>b[a]r</paragraph>' );
  302. contextualToolbar.on( 'beforeShow', ( evt, stop ) => {
  303. stop();
  304. } );
  305. contextualToolbar._showPanel();
  306. sinon.assert.notCalled( balloonAddSpy );
  307. } );
  308. } );
  309. function stubSelectionRect( forwardSelectionRect, backwardSelectionRect ) {
  310. const editingView = editor.editing.view;
  311. const originalViewRangeToDom = editingView.domConverter.viewRangeToDom;
  312. // Mock selection rect.
  313. sandbox.stub( editingView.domConverter, 'viewRangeToDom', ( ...args ) => {
  314. const domRange = originalViewRangeToDom.apply( editingView.domConverter, args );
  315. sandbox.stub( domRange, 'getClientRects', () => {
  316. return {
  317. length: 2,
  318. item( id ) {
  319. return id === 0 ? forwardSelectionRect : backwardSelectionRect;
  320. }
  321. };
  322. } );
  323. return domRange;
  324. } );
  325. }
  326. } );