8
0

blocktoolbar.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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.className ).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.buttonView.fire( 'execute' );
  104. expect( blockToolbar.panelView.isVisible ).to.be.true;
  105. blockToolbar.toolbarView.focusTracker.isFocused = false;
  106. expect( blockToolbar.panelView.isVisible ).to.be.false;
  107. } );
  108. } );
  109. describe( 'buttonView', () => {
  110. it( 'should create a view instance', () => {
  111. expect( blockToolbar.buttonView ).to.instanceof( BlockButtonView );
  112. } );
  113. it( 'should be added to the editor ui.view.body collection', () => {
  114. expect( Array.from( editor.ui.view.body ) ).to.include( blockToolbar.buttonView );
  115. } );
  116. it( 'should add the #buttonView to the ui.focusTracker', () => {
  117. editor.ui.focusTracker.isFocused = false;
  118. blockToolbar.buttonView.element.dispatchEvent( new Event( 'focus' ) );
  119. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  120. } );
  121. it( 'should pin the #panelView to the button and focus first item in toolbar on #execute event', () => {
  122. expect( blockToolbar.panelView.isVisible ).to.be.false;
  123. const pinSpy = testUtils.sinon.spy( blockToolbar.panelView, 'pin' );
  124. const focusSpy = testUtils.sinon.spy( blockToolbar.toolbarView.items.get( 0 ), 'focus' );
  125. blockToolbar.buttonView.fire( 'execute' );
  126. expect( blockToolbar.panelView.isVisible ).to.be.true;
  127. sinon.assert.calledWith( pinSpy, {
  128. target: blockToolbar.buttonView.element,
  129. limiter: editor.ui.view.editableElement
  130. } );
  131. sinon.assert.calledOnce( focusSpy );
  132. } );
  133. it( 'should hide the #panelView and focus the editable on #execute event when panel was visible', () => {
  134. blockToolbar.panelView.isVisible = true;
  135. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  136. blockToolbar.buttonView.fire( 'execute' );
  137. expect( blockToolbar.panelView.isVisible ).to.be.false;
  138. sinon.assert.calledOnce( spy );
  139. } );
  140. it( 'should bind #isOn to panelView#isVisible', () => {
  141. blockToolbar.panelView.isVisible = false;
  142. expect( blockToolbar.buttonView.isOn ).to.be.false;
  143. blockToolbar.panelView.isVisible = true;
  144. expect( blockToolbar.buttonView.isOn ).to.be.true;
  145. } );
  146. it( 'should hide the #button tooltip when the #panelView is open', () => {
  147. blockToolbar.panelView.isVisible = false;
  148. expect( blockToolbar.buttonView.tooltip ).to.be.true;
  149. blockToolbar.panelView.isVisible = true;
  150. expect( blockToolbar.buttonView.tooltip ).to.be.false;
  151. } );
  152. } );
  153. } );
  154. describe( 'allowed elements', () => {
  155. it( 'should display the button when the first selected block is a block element', () => {
  156. editor.model.schema.register( 'foo', { inheritAllFrom: '$block' } );
  157. editor.conversion.elementToElement( { model: 'foo', view: 'foo' } );
  158. setData( editor.model, '<foo>foo[]bar</foo>' );
  159. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  160. } );
  161. it( 'should display the button when the first selected block is an object', () => {
  162. setData( editor.model, '[<image src="foo.jpg"><caption>foo</caption></image>]' );
  163. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  164. } );
  165. it( 'should display the button when the selection is inside the object', () => {
  166. setData( editor.model, '<image src="foo.jpg"><caption>f[]oo</caption></image>' );
  167. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  168. } );
  169. it( 'should not display the button when the selection is placed in the root element', () => {
  170. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  171. setData( editor.model, '<paragraph>foo</paragraph>[]<paragraph>bar</paragraph>' );
  172. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  173. } );
  174. it( 'should not display the button when all toolbar items are disabled for the selected element', () => {
  175. const element = document.createElement( 'div' );
  176. document.body.appendChild( element );
  177. return ClassicTestEditor.create( element, {
  178. plugins: [ BlockToolbar, Heading, HeadingButtonsUI, Paragraph, ParagraphButtonUI, Image ],
  179. blockToolbar: [ 'paragraph', 'heading1', 'heading2' ]
  180. } ).then( editor => {
  181. const blockToolbar = editor.plugins.get( BlockToolbar );
  182. setData( editor.model, '[<image src="foo.jpg"></image>]' );
  183. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  184. element.remove();
  185. return editor.destroy();
  186. } );
  187. } );
  188. } );
  189. describe( 'attaching the button to the content', () => {
  190. beforeEach( () => {
  191. // Be sure that window is not scrolled.
  192. testUtils.sinon.stub( window, 'scrollX' ).get( () => 0 );
  193. testUtils.sinon.stub( window, 'scrollY' ).get( () => 0 );
  194. } );
  195. it( 'should attach the right side of the button to the left side of the editable and center with the first line ' +
  196. 'of the selected block #1', () => {
  197. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  198. const target = editor.ui.view.editableElement.querySelector( 'p' );
  199. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  200. styleMock.withArgs( target ).returns( {
  201. lineHeight: '20px',
  202. paddingTop: '10px'
  203. } );
  204. styleMock.callThrough();
  205. testUtils.sinon.stub( editor.ui.view.editableElement, 'getBoundingClientRect' ).returns( {
  206. left: 200
  207. } );
  208. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  209. top: 500,
  210. left: 300
  211. } );
  212. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  213. width: 100,
  214. height: 100
  215. } );
  216. editor.ui.fire( 'update' );
  217. expect( blockToolbar.buttonView.top ).to.equal( 470 );
  218. expect( blockToolbar.buttonView.left ).to.equal( 100 );
  219. } );
  220. it( 'should attach the right side of the button to the left side of the editable and center with the first line ' +
  221. 'of the selected block #2', () => {
  222. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  223. const target = editor.ui.view.editableElement.querySelector( 'p' );
  224. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  225. styleMock.withArgs( target ).returns( {
  226. lineHeight: 'normal',
  227. fontSize: '20px',
  228. paddingTop: '10px'
  229. } );
  230. styleMock.callThrough();
  231. testUtils.sinon.stub( editor.ui.view.editableElement, 'getBoundingClientRect' ).returns( {
  232. left: 200
  233. } );
  234. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  235. top: 500,
  236. left: 300
  237. } );
  238. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  239. width: 100,
  240. height: 100
  241. } );
  242. editor.ui.fire( 'update' );
  243. expect( blockToolbar.buttonView.top ).to.equal( 472 );
  244. expect( blockToolbar.buttonView.left ).to.equal( 100 );
  245. } );
  246. it( 'should reposition the #panelView when open on ui#update', () => {
  247. blockToolbar.panelView.isVisible = false;
  248. const spy = testUtils.sinon.spy( blockToolbar.panelView, 'pin' );
  249. editor.ui.fire( 'update' );
  250. sinon.assert.notCalled( spy );
  251. blockToolbar.panelView.isVisible = true;
  252. editor.ui.fire( 'update' );
  253. sinon.assert.calledWith( spy, {
  254. target: blockToolbar.buttonView.element,
  255. limiter: editor.ui.view.editableElement
  256. } );
  257. } );
  258. it( 'should not reset the toolbar focus on ui#update', () => {
  259. blockToolbar.panelView.isVisible = true;
  260. const spy = testUtils.sinon.spy( blockToolbar.toolbarView, 'focus' );
  261. editor.ui.fire( 'update' );
  262. sinon.assert.notCalled( spy );
  263. } );
  264. it( 'should hide the open panel on a direct selection change', () => {
  265. blockToolbar.panelView.isVisible = true;
  266. editor.model.document.selection.fire( 'change:range', { directChange: true } );
  267. expect( blockToolbar.panelView.isVisible ).to.be.false;
  268. } );
  269. it( 'should not hide the open panel on an indirect selection change', () => {
  270. blockToolbar.panelView.isVisible = true;
  271. editor.model.document.selection.fire( 'change:range', { directChange: false } );
  272. expect( blockToolbar.panelView.isVisible ).to.be.true;
  273. } );
  274. it( 'should hide the UI when editor switched to readonly', () => {
  275. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  276. blockToolbar.buttonView.isVisible = true;
  277. blockToolbar.panelView.isVisible = true;
  278. editor.isReadOnly = true;
  279. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  280. expect( blockToolbar.panelView.isVisible ).to.be.false;
  281. } );
  282. it( 'should show the button when the editor switched back from readonly', () => {
  283. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  284. expect( blockToolbar.buttonView.isVisible ).to.true;
  285. editor.isReadOnly = true;
  286. expect( blockToolbar.buttonView.isVisible ).to.false;
  287. editor.isReadOnly = false;
  288. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  289. } );
  290. it( 'should show/hide the button on the editor focus/blur', () => {
  291. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  292. editor.ui.focusTracker.isFocused = true;
  293. expect( blockToolbar.buttonView.isVisible ).to.true;
  294. editor.ui.focusTracker.isFocused = false;
  295. expect( blockToolbar.buttonView.isVisible ).to.false;
  296. editor.ui.focusTracker.isFocused = true;
  297. expect( blockToolbar.buttonView.isVisible ).to.true;
  298. } );
  299. it( 'should hide the UI when the editor switched to the readonly when the panel is not visible', () => {
  300. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  301. blockToolbar.buttonView.isVisible = true;
  302. blockToolbar.panelView.isVisible = false;
  303. editor.isReadOnly = true;
  304. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  305. expect( blockToolbar.panelView.isVisible ).to.be.false;
  306. } );
  307. it( 'should update the button position on browser resize only when the button is visible', () => {
  308. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  309. const spy = testUtils.sinon.spy( blockToolbar, '_attachButtonToElement' );
  310. // Place the selection outside of any block because the toolbar will not be shown in this case.
  311. setData( editor.model, '[]<paragraph>bar</paragraph>' );
  312. window.dispatchEvent( new Event( 'resize' ) );
  313. sinon.assert.notCalled( spy );
  314. setData( editor.model, '<paragraph>ba[]r</paragraph>' );
  315. spy.resetHistory();
  316. window.dispatchEvent( new Event( 'resize' ) );
  317. sinon.assert.called( spy );
  318. setData( editor.model, '[]<paragraph>bar</paragraph>' );
  319. spy.resetHistory();
  320. window.dispatchEvent( new Event( 'resize' ) );
  321. sinon.assert.notCalled( spy );
  322. } );
  323. } );
  324. } );