blocktoolbar.js 15 KB

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