blocktoolbar.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /**
  2. * Copyright (c) 2016 - 2017, CKSource - Frederico Knabben. All rights reserved.
  3. */
  4. /* global document, window, Event */
  5. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import BlockToolbar from '../../../src/toolbar/block/blocktoolbar';
  7. import ToolbarView from '../../../src/toolbar/toolbarview';
  8. import BalloonPanelView from '../../../src/panel/balloon/balloonpanelview';
  9. import BlockButtonView from '../../../src/toolbar/block/blockbuttonview';
  10. import Heading from '@ckeditor/ckeditor5-heading/src/heading';
  11. import HeadingButtonsUI from '@ckeditor/ckeditor5-heading/src/headingbuttonsui';
  12. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  13. import ParagraphButtonUI from '@ckeditor/ckeditor5-paragraph/src/paragraphbuttonui';
  14. import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
  15. import Image from '@ckeditor/ckeditor5-image/src/image';
  16. import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';
  17. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  18. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  19. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  20. describe( 'BlockToolbar', () => {
  21. let editor, element, blockToolbar;
  22. testUtils.createSinonSandbox();
  23. beforeEach( () => {
  24. element = document.createElement( 'div' );
  25. document.body.appendChild( element );
  26. return ClassicTestEditor.create( element, {
  27. plugins: [ BlockToolbar, Heading, HeadingButtonsUI, Paragraph, ParagraphButtonUI, BlockQuote, Image, ImageCaption ],
  28. blockToolbar: [ 'paragraph', 'heading1', 'heading2', 'blockQuote' ]
  29. } ).then( newEditor => {
  30. editor = newEditor;
  31. blockToolbar = editor.plugins.get( BlockToolbar );
  32. editor.ui.focusTracker.isFocused = true;
  33. } );
  34. } );
  35. afterEach( () => {
  36. element.remove();
  37. return editor.destroy();
  38. } );
  39. it( 'should have pluginName property', () => {
  40. expect( BlockToolbar.pluginName ).to.equal( 'BlockToolbar' );
  41. } );
  42. describe( 'child views', () => {
  43. describe( 'panelView', () => {
  44. it( 'should create a view instance', () => {
  45. expect( blockToolbar.panelView ).to.instanceof( BalloonPanelView );
  46. } );
  47. it( 'should have an additional class name', () => {
  48. expect( blockToolbar.panelView.class ).to.equal( 'ck-toolbar-container' );
  49. } );
  50. it( 'should be added to the ui.view.body collection', () => {
  51. expect( Array.from( editor.ui.view.body ) ).to.include( blockToolbar.panelView );
  52. } );
  53. it( 'should add the #panelView to ui.focusTracker', () => {
  54. editor.ui.focusTracker.isFocused = false;
  55. blockToolbar.panelView.element.dispatchEvent( new Event( 'focus' ) );
  56. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  57. } );
  58. it( 'should close the #panelView after `Esc` is pressed and focus view document', () => {
  59. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  60. blockToolbar.panelView.isVisible = true;
  61. blockToolbar.toolbarView.keystrokes.press( {
  62. keyCode: keyCodes.esc,
  63. preventDefault: () => {},
  64. stopPropagation: () => {}
  65. } );
  66. expect( blockToolbar.panelView.isVisible ).to.be.false;
  67. sinon.assert.calledOnce( spy );
  68. } );
  69. it( 'should close the #panelView upon click outside the panel and not focus view document', () => {
  70. const spy = testUtils.sinon.spy();
  71. editor.editing.view.on( 'focus', spy );
  72. blockToolbar.panelView.isVisible = true;
  73. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  74. expect( blockToolbar.panelView.isVisible ).to.be.false;
  75. sinon.assert.notCalled( spy );
  76. } );
  77. it( 'should not close the #panelView upon click on panel element', () => {
  78. blockToolbar.panelView.isVisible = true;
  79. blockToolbar.panelView.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  80. expect( blockToolbar.panelView.isVisible ).to.be.true;
  81. } );
  82. } );
  83. describe( 'toolbarView', () => {
  84. it( 'should create the view instance', () => {
  85. expect( blockToolbar.toolbarView ).to.instanceof( ToolbarView );
  86. } );
  87. it( 'should add an additional class to toolbar element', () => {
  88. expect( blockToolbar.toolbarView.element.classList.contains( 'ck-toolbar_floating' ) ).to.be.true;
  89. } );
  90. it( 'should be added to the panelView#content collection', () => {
  91. expect( Array.from( blockToolbar.panelView.content ) ).to.include( blockToolbar.toolbarView );
  92. } );
  93. it( 'should initialize the toolbar items based on Editor#blockToolbar config', () => {
  94. expect( Array.from( blockToolbar.toolbarView.items ) ).to.length( 4 );
  95. } );
  96. it( 'should hide the panel after clicking on the button from toolbar', () => {
  97. blockToolbar.buttonView.fire( 'execute' );
  98. expect( blockToolbar.panelView.isVisible ).to.be.true;
  99. blockToolbar.toolbarView.items.get( 0 ).fire( 'execute' );
  100. expect( blockToolbar.panelView.isVisible ).to.be.false;
  101. } );
  102. it( 'should hide the panel on toolbar blur', () => {
  103. blockToolbar.toolbarView.focusTracker.isFocused = true;
  104. blockToolbar.buttonView.fire( 'execute' );
  105. expect( blockToolbar.panelView.isVisible ).to.be.true;
  106. blockToolbar.toolbarView.focusTracker.isFocused = false;
  107. expect( blockToolbar.panelView.isVisible ).to.be.false;
  108. } );
  109. } );
  110. describe( 'buttonView', () => {
  111. it( 'should create a view instance', () => {
  112. expect( blockToolbar.buttonView ).to.instanceof( BlockButtonView );
  113. } );
  114. it( 'should be added to the editor ui.view.body collection', () => {
  115. expect( Array.from( editor.ui.view.body ) ).to.include( blockToolbar.buttonView );
  116. } );
  117. it( 'should add the #buttonView to the ui.focusTracker', () => {
  118. editor.ui.focusTracker.isFocused = false;
  119. blockToolbar.buttonView.element.dispatchEvent( new Event( 'focus' ) );
  120. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  121. } );
  122. it( 'should pin the #panelView to the button and focus first item in toolbar on #execute event', () => {
  123. expect( blockToolbar.panelView.isVisible ).to.be.false;
  124. const pinSpy = testUtils.sinon.spy( blockToolbar.panelView, 'pin' );
  125. const focusSpy = testUtils.sinon.spy( blockToolbar.toolbarView.items.get( 0 ), 'focus' );
  126. blockToolbar.buttonView.fire( 'execute' );
  127. expect( blockToolbar.panelView.isVisible ).to.be.true;
  128. sinon.assert.calledWith( pinSpy, {
  129. target: blockToolbar.buttonView.element,
  130. limiter: editor.ui.view.editableElement
  131. } );
  132. sinon.assert.calledOnce( focusSpy );
  133. } );
  134. it( 'should hide the #panelView and focus the editable on #execute event when panel was visible', () => {
  135. blockToolbar.panelView.isVisible = true;
  136. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  137. blockToolbar.buttonView.fire( 'execute' );
  138. expect( blockToolbar.panelView.isVisible ).to.be.false;
  139. sinon.assert.calledOnce( spy );
  140. } );
  141. it( 'should bind #isOn to panelView#isVisible', () => {
  142. blockToolbar.panelView.isVisible = false;
  143. expect( blockToolbar.buttonView.isOn ).to.be.false;
  144. blockToolbar.panelView.isVisible = true;
  145. expect( blockToolbar.buttonView.isOn ).to.be.true;
  146. } );
  147. it( 'should hide the #button tooltip when the #panelView is open', () => {
  148. blockToolbar.panelView.isVisible = false;
  149. expect( blockToolbar.buttonView.tooltip ).to.be.true;
  150. blockToolbar.panelView.isVisible = true;
  151. expect( blockToolbar.buttonView.tooltip ).to.be.false;
  152. } );
  153. } );
  154. } );
  155. describe( 'allowed elements', () => {
  156. it( 'should display the button when the first selected block is a block element', () => {
  157. editor.model.schema.register( 'foo', { inheritAllFrom: '$block' } );
  158. editor.conversion.elementToElement( { model: 'foo', view: 'foo' } );
  159. setData( editor.model, '<foo>foo[]bar</foo>' );
  160. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  161. } );
  162. it( 'should display the button when the first selected block is an object', () => {
  163. setData( editor.model, '[<image src="foo.jpg"><caption>foo</caption></image>]' );
  164. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  165. } );
  166. it( 'should display the button when the selection is inside the object', () => {
  167. setData( editor.model, '<image src="foo.jpg"><caption>f[]oo</caption></image>' );
  168. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  169. } );
  170. it( 'should not display the button when the selection is placed in the root element', () => {
  171. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  172. setData( editor.model, '<paragraph>foo</paragraph>[]<paragraph>bar</paragraph>' );
  173. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  174. } );
  175. it( 'should not display the button when all toolbar items are disabled for the selected element', () => {
  176. const element = document.createElement( 'div' );
  177. document.body.appendChild( element );
  178. return ClassicTestEditor.create( element, {
  179. plugins: [ BlockToolbar, Heading, HeadingButtonsUI, Paragraph, ParagraphButtonUI, Image ],
  180. blockToolbar: [ 'paragraph', 'heading1', 'heading2' ]
  181. } ).then( editor => {
  182. const blockToolbar = editor.plugins.get( BlockToolbar );
  183. setData( editor.model, '[<image src="foo.jpg"></image>]' );
  184. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  185. element.remove();
  186. return editor.destroy();
  187. } );
  188. } );
  189. } );
  190. describe( 'attaching the button to the content', () => {
  191. beforeEach( () => {
  192. // Be sure that window is not scrolled.
  193. testUtils.sinon.stub( window, 'scrollX' ).get( () => 0 );
  194. testUtils.sinon.stub( window, 'scrollY' ).get( () => 0 );
  195. } );
  196. it( 'should attach the right side of the button to the left side of the editable and center with the first line ' +
  197. 'of the selected block #1', () => {
  198. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  199. const target = editor.ui.view.editableElement.querySelector( 'p' );
  200. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  201. styleMock.withArgs( target ).returns( {
  202. lineHeight: '20px',
  203. paddingTop: '10px'
  204. } );
  205. styleMock.callThrough();
  206. testUtils.sinon.stub( editor.ui.view.editableElement, 'getBoundingClientRect' ).returns( {
  207. left: 200
  208. } );
  209. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  210. top: 500,
  211. left: 300
  212. } );
  213. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  214. width: 100,
  215. height: 100
  216. } );
  217. editor.ui.fire( 'update' );
  218. expect( blockToolbar.buttonView.top ).to.equal( 470 );
  219. expect( blockToolbar.buttonView.left ).to.equal( 100 );
  220. } );
  221. it( 'should attach the right side of the button to the left side of the editable and center with the first line ' +
  222. 'of the selected block #2', () => {
  223. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  224. const target = editor.ui.view.editableElement.querySelector( 'p' );
  225. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  226. styleMock.withArgs( target ).returns( {
  227. lineHeight: 'normal',
  228. fontSize: '20px',
  229. paddingTop: '10px'
  230. } );
  231. styleMock.callThrough();
  232. testUtils.sinon.stub( editor.ui.view.editableElement, 'getBoundingClientRect' ).returns( {
  233. left: 200
  234. } );
  235. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  236. top: 500,
  237. left: 300
  238. } );
  239. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  240. width: 100,
  241. height: 100
  242. } );
  243. editor.ui.fire( 'update' );
  244. expect( blockToolbar.buttonView.top ).to.equal( 472 );
  245. expect( blockToolbar.buttonView.left ).to.equal( 100 );
  246. } );
  247. it( 'should reposition the #panelView when open on ui#update', () => {
  248. blockToolbar.panelView.isVisible = false;
  249. const spy = testUtils.sinon.spy( blockToolbar.panelView, 'pin' );
  250. editor.ui.fire( 'update' );
  251. sinon.assert.notCalled( spy );
  252. blockToolbar.panelView.isVisible = true;
  253. editor.ui.fire( 'update' );
  254. sinon.assert.calledWith( spy, {
  255. target: blockToolbar.buttonView.element,
  256. limiter: editor.ui.view.editableElement
  257. } );
  258. } );
  259. it( 'should not reset the toolbar focus on ui#update', () => {
  260. blockToolbar.panelView.isVisible = true;
  261. const spy = testUtils.sinon.spy( blockToolbar.toolbarView, 'focus' );
  262. editor.ui.fire( 'update' );
  263. sinon.assert.notCalled( spy );
  264. } );
  265. it( 'should hide the open panel on a direct selection change', () => {
  266. blockToolbar.panelView.isVisible = true;
  267. editor.model.document.selection.fire( 'change:range', { directChange: true } );
  268. expect( blockToolbar.panelView.isVisible ).to.be.false;
  269. } );
  270. it( 'should not hide the open panel on an indirect selection change', () => {
  271. blockToolbar.panelView.isVisible = true;
  272. editor.model.document.selection.fire( 'change:range', { directChange: false } );
  273. expect( blockToolbar.panelView.isVisible ).to.be.true;
  274. } );
  275. it( 'should hide the UI when editor switched to readonly', () => {
  276. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  277. blockToolbar.buttonView.isVisible = true;
  278. blockToolbar.panelView.isVisible = true;
  279. editor.isReadOnly = true;
  280. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  281. expect( blockToolbar.panelView.isVisible ).to.be.false;
  282. } );
  283. it( 'should show the button when the editor switched back from readonly', () => {
  284. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  285. expect( blockToolbar.buttonView.isVisible ).to.true;
  286. editor.isReadOnly = true;
  287. expect( blockToolbar.buttonView.isVisible ).to.false;
  288. editor.isReadOnly = false;
  289. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  290. } );
  291. it( 'should show/hide the button on the editor focus/blur', () => {
  292. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  293. editor.ui.focusTracker.isFocused = true;
  294. expect( blockToolbar.buttonView.isVisible ).to.true;
  295. editor.ui.focusTracker.isFocused = false;
  296. expect( blockToolbar.buttonView.isVisible ).to.false;
  297. editor.ui.focusTracker.isFocused = true;
  298. expect( blockToolbar.buttonView.isVisible ).to.true;
  299. } );
  300. it( 'should hide the UI when the editor switched to the readonly when the panel is not visible', () => {
  301. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  302. blockToolbar.buttonView.isVisible = true;
  303. blockToolbar.panelView.isVisible = false;
  304. editor.isReadOnly = true;
  305. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  306. expect( blockToolbar.panelView.isVisible ).to.be.false;
  307. } );
  308. it( 'should update the button position on browser resize only when the button is visible', () => {
  309. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  310. const spy = testUtils.sinon.spy( blockToolbar, '_attachButtonToElement' );
  311. // Place the selection outside of any block because the toolbar will not be shown in this case.
  312. setData( editor.model, '[]<paragraph>bar</paragraph>' );
  313. window.dispatchEvent( new Event( 'resize' ) );
  314. sinon.assert.notCalled( spy );
  315. setData( editor.model, '<paragraph>ba[]r</paragraph>' );
  316. spy.resetHistory();
  317. window.dispatchEvent( new Event( 'resize' ) );
  318. sinon.assert.called( spy );
  319. setData( editor.model, '[]<paragraph>bar</paragraph>' );
  320. spy.resetHistory();
  321. window.dispatchEvent( new Event( 'resize' ) );
  322. sinon.assert.notCalled( spy );
  323. } );
  324. } );
  325. } );