8
0

blocktoolbar.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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.view.editableElement
  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. it( 'should display the button when the selection is inside the object', () => {
  170. setData( editor.model, '<image src="foo.jpg"><caption>f[]oo</caption></image>' );
  171. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  172. } );
  173. it( 'should not display the button when the selection is placed in the root element', () => {
  174. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  175. setData( editor.model, '<paragraph>foo</paragraph>[]<paragraph>bar</paragraph>' );
  176. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  177. } );
  178. it( 'should not display the button when all toolbar items are disabled for the selected element', () => {
  179. const element = document.createElement( 'div' );
  180. document.body.appendChild( element );
  181. return ClassicTestEditor.create( element, {
  182. plugins: [ BlockToolbar, Heading, HeadingButtonsUI, Paragraph, ParagraphButtonUI, Image ],
  183. blockToolbar: [ 'paragraph', 'heading1', 'heading2' ]
  184. } ).then( editor => {
  185. const blockToolbar = editor.plugins.get( BlockToolbar );
  186. setData( editor.model, '[<image src="foo.jpg"></image>]' );
  187. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  188. element.remove();
  189. return editor.destroy();
  190. } );
  191. } );
  192. } );
  193. describe( 'attaching the button to the content', () => {
  194. beforeEach( () => {
  195. // Be sure that window is not scrolled.
  196. testUtils.sinon.stub( window, 'scrollX' ).get( () => 0 );
  197. testUtils.sinon.stub( window, 'scrollY' ).get( () => 0 );
  198. } );
  199. it( 'should attach the right side of the button to the left side of the editable and center with the first line ' +
  200. 'of the selected block #1', () => {
  201. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  202. const target = editor.ui.view.editableElement.querySelector( 'p' );
  203. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  204. styleMock.withArgs( target ).returns( {
  205. lineHeight: '20px',
  206. paddingTop: '10px'
  207. } );
  208. styleMock.callThrough();
  209. testUtils.sinon.stub( editor.ui.view.editableElement, 'getBoundingClientRect' ).returns( {
  210. left: 200
  211. } );
  212. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  213. top: 500,
  214. left: 300
  215. } );
  216. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  217. width: 100,
  218. height: 100
  219. } );
  220. editor.ui.fire( 'update' );
  221. expect( blockToolbar.buttonView.top ).to.equal( 470 );
  222. expect( blockToolbar.buttonView.left ).to.equal( 100 );
  223. } );
  224. it( 'should attach the right side of the button to the left side of the editable and center with the first line ' +
  225. 'of the selected block #2', () => {
  226. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  227. const target = editor.ui.view.editableElement.querySelector( 'p' );
  228. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  229. styleMock.withArgs( target ).returns( {
  230. lineHeight: 'normal',
  231. fontSize: '20px',
  232. paddingTop: '10px'
  233. } );
  234. styleMock.callThrough();
  235. testUtils.sinon.stub( editor.ui.view.editableElement, 'getBoundingClientRect' ).returns( {
  236. left: 200
  237. } );
  238. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  239. top: 500,
  240. left: 300
  241. } );
  242. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  243. width: 100,
  244. height: 100
  245. } );
  246. editor.ui.fire( 'update' );
  247. expect( blockToolbar.buttonView.top ).to.equal( 472 );
  248. expect( blockToolbar.buttonView.left ).to.equal( 100 );
  249. } );
  250. it( 'should reposition the #panelView when open on ui#update', () => {
  251. blockToolbar.panelView.isVisible = false;
  252. const spy = testUtils.sinon.spy( blockToolbar.panelView, 'pin' );
  253. editor.ui.fire( 'update' );
  254. sinon.assert.notCalled( spy );
  255. blockToolbar.panelView.isVisible = true;
  256. editor.ui.fire( 'update' );
  257. sinon.assert.calledWith( spy, {
  258. target: blockToolbar.buttonView.element,
  259. limiter: editor.ui.view.editableElement
  260. } );
  261. } );
  262. it( 'should not reset the toolbar focus on ui#update', () => {
  263. blockToolbar.panelView.isVisible = true;
  264. const spy = testUtils.sinon.spy( blockToolbar.toolbarView, 'focus' );
  265. editor.ui.fire( 'update' );
  266. sinon.assert.notCalled( spy );
  267. } );
  268. it( 'should hide the open panel on a direct selection change', () => {
  269. blockToolbar.panelView.isVisible = true;
  270. editor.model.document.selection.fire( 'change:range', { directChange: true } );
  271. expect( blockToolbar.panelView.isVisible ).to.be.false;
  272. } );
  273. it( 'should not hide the open panel on an indirect selection change', () => {
  274. blockToolbar.panelView.isVisible = true;
  275. editor.model.document.selection.fire( 'change:range', { directChange: false } );
  276. expect( blockToolbar.panelView.isVisible ).to.be.true;
  277. } );
  278. it( 'should hide the UI when editor switched to readonly', () => {
  279. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  280. blockToolbar.buttonView.isVisible = true;
  281. blockToolbar.panelView.isVisible = true;
  282. editor.isReadOnly = true;
  283. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  284. expect( blockToolbar.panelView.isVisible ).to.be.false;
  285. } );
  286. it( 'should show the button when the editor switched back from readonly', () => {
  287. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  288. expect( blockToolbar.buttonView.isVisible ).to.true;
  289. editor.isReadOnly = true;
  290. expect( blockToolbar.buttonView.isVisible ).to.false;
  291. editor.isReadOnly = false;
  292. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  293. } );
  294. it( 'should show/hide the button on the editor focus/blur', () => {
  295. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  296. editor.ui.focusTracker.isFocused = true;
  297. expect( blockToolbar.buttonView.isVisible ).to.true;
  298. editor.ui.focusTracker.isFocused = false;
  299. expect( blockToolbar.buttonView.isVisible ).to.false;
  300. editor.ui.focusTracker.isFocused = true;
  301. expect( blockToolbar.buttonView.isVisible ).to.true;
  302. } );
  303. it( 'should hide the UI when the editor switched to the readonly when the panel is not visible', () => {
  304. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  305. blockToolbar.buttonView.isVisible = true;
  306. blockToolbar.panelView.isVisible = false;
  307. editor.isReadOnly = true;
  308. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  309. expect( blockToolbar.panelView.isVisible ).to.be.false;
  310. } );
  311. it( 'should update the button position on browser resize only when the button is visible', () => {
  312. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  313. const spy = testUtils.sinon.spy( blockToolbar, '_attachButtonToElement' );
  314. // Place the selection outside of any block because the toolbar will not be shown in this case.
  315. setData( editor.model, '[]<paragraph>bar</paragraph>' );
  316. window.dispatchEvent( new Event( 'resize' ) );
  317. sinon.assert.notCalled( spy );
  318. setData( editor.model, '<paragraph>ba[]r</paragraph>' );
  319. spy.resetHistory();
  320. window.dispatchEvent( new Event( 'resize' ) );
  321. sinon.assert.called( spy );
  322. setData( editor.model, '[]<paragraph>bar</paragraph>' );
  323. spy.resetHistory();
  324. window.dispatchEvent( new Event( 'resize' ) );
  325. sinon.assert.notCalled( spy );
  326. } );
  327. } );
  328. } );