blocktoolbar.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /**
  2. * @license Copyright (c) 2003-2020, 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. it( 'should not throw when empty config is provided', async () => {
  46. // Remove default editor instance.
  47. await editor.destroy();
  48. editor = await ClassicTestEditor.create( element, {
  49. plugins: [ BlockToolbar ]
  50. } );
  51. } );
  52. describe( 'child views', () => {
  53. describe( 'panelView', () => {
  54. it( 'should create a view instance', () => {
  55. expect( blockToolbar.panelView ).to.instanceof( BalloonPanelView );
  56. } );
  57. it( 'should have an additional class name', () => {
  58. expect( blockToolbar.panelView.class ).to.equal( 'ck-toolbar-container' );
  59. } );
  60. it( 'should be added to the ui.view.body collection', () => {
  61. expect( Array.from( editor.ui.view.body ) ).to.include( blockToolbar.panelView );
  62. } );
  63. it( 'should add the #panelView to ui.focusTracker', () => {
  64. editor.ui.focusTracker.isFocused = false;
  65. blockToolbar.panelView.element.dispatchEvent( new Event( 'focus' ) );
  66. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  67. } );
  68. it( 'should close the #panelView after `Esc` is pressed and focus view document', () => {
  69. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  70. blockToolbar.panelView.isVisible = true;
  71. blockToolbar.toolbarView.keystrokes.press( {
  72. keyCode: keyCodes.esc,
  73. preventDefault: () => {},
  74. stopPropagation: () => {}
  75. } );
  76. expect( blockToolbar.panelView.isVisible ).to.be.false;
  77. sinon.assert.calledOnce( spy );
  78. } );
  79. it( 'should close the #panelView upon click outside the panel and not focus view document', () => {
  80. const spy = testUtils.sinon.spy();
  81. editor.editing.view.on( 'focus', spy );
  82. blockToolbar.panelView.isVisible = true;
  83. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  84. expect( blockToolbar.panelView.isVisible ).to.be.false;
  85. sinon.assert.notCalled( spy );
  86. } );
  87. it( 'should not close the #panelView upon click on panel element', () => {
  88. blockToolbar.panelView.isVisible = true;
  89. blockToolbar.panelView.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  90. expect( blockToolbar.panelView.isVisible ).to.be.true;
  91. } );
  92. } );
  93. describe( 'toolbarView', () => {
  94. it( 'should create the view instance', () => {
  95. expect( blockToolbar.toolbarView ).to.instanceof( ToolbarView );
  96. } );
  97. it( 'should add an additional class to toolbar element', () => {
  98. expect( blockToolbar.toolbarView.element.classList.contains( 'ck-toolbar_floating' ) ).to.be.true;
  99. } );
  100. it( 'should be added to the panelView#content collection', () => {
  101. expect( Array.from( blockToolbar.panelView.content ) ).to.include( blockToolbar.toolbarView );
  102. } );
  103. it( 'should initialize the toolbar items based on Editor#blockToolbar config', () => {
  104. expect( Array.from( blockToolbar.toolbarView.items ) ).to.length( 4 );
  105. } );
  106. it( 'should hide the panel after clicking on the button from toolbar', () => {
  107. blockToolbar.buttonView.fire( 'execute' );
  108. expect( blockToolbar.panelView.isVisible ).to.be.true;
  109. blockToolbar.toolbarView.items.get( 0 ).fire( 'execute' );
  110. expect( blockToolbar.panelView.isVisible ).to.be.false;
  111. } );
  112. it( 'should hide the panel on toolbar blur', () => {
  113. blockToolbar.toolbarView.focusTracker.isFocused = true;
  114. blockToolbar.buttonView.fire( 'execute' );
  115. expect( blockToolbar.panelView.isVisible ).to.be.true;
  116. blockToolbar.toolbarView.focusTracker.isFocused = false;
  117. expect( blockToolbar.panelView.isVisible ).to.be.false;
  118. } );
  119. } );
  120. describe( 'buttonView', () => {
  121. it( 'should create a view instance', () => {
  122. expect( blockToolbar.buttonView ).to.instanceof( BlockButtonView );
  123. } );
  124. it( 'should be added to the editor ui.view.body collection', () => {
  125. expect( Array.from( editor.ui.view.body ) ).to.include( blockToolbar.buttonView );
  126. } );
  127. it( 'should add the #buttonView to the ui.focusTracker', () => {
  128. editor.ui.focusTracker.isFocused = false;
  129. blockToolbar.buttonView.element.dispatchEvent( new Event( 'focus' ) );
  130. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  131. } );
  132. it( 'should pin the #panelView to the button and focus first item in toolbar on #execute event', () => {
  133. expect( blockToolbar.panelView.isVisible ).to.be.false;
  134. const pinSpy = testUtils.sinon.spy( blockToolbar.panelView, 'pin' );
  135. const focusSpy = testUtils.sinon.spy( blockToolbar.toolbarView.items.get( 0 ), 'focus' );
  136. blockToolbar.buttonView.fire( 'execute' );
  137. expect( blockToolbar.panelView.isVisible ).to.be.true;
  138. sinon.assert.calledWith( pinSpy, {
  139. target: blockToolbar.buttonView.element,
  140. limiter: editor.ui.getEditableElement()
  141. } );
  142. sinon.assert.calledOnce( focusSpy );
  143. } );
  144. it( 'should hide the #panelView and focus the editable on #execute event when panel was visible', () => {
  145. blockToolbar.panelView.isVisible = true;
  146. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  147. blockToolbar.buttonView.fire( 'execute' );
  148. expect( blockToolbar.panelView.isVisible ).to.be.false;
  149. sinon.assert.calledOnce( spy );
  150. } );
  151. it( 'should bind #isOn to panelView#isVisible', () => {
  152. blockToolbar.panelView.isVisible = false;
  153. expect( blockToolbar.buttonView.isOn ).to.be.false;
  154. blockToolbar.panelView.isVisible = true;
  155. expect( blockToolbar.buttonView.isOn ).to.be.true;
  156. } );
  157. it( 'should hide the #button tooltip when the #panelView is open', () => {
  158. blockToolbar.panelView.isVisible = false;
  159. expect( blockToolbar.buttonView.tooltip ).to.be.true;
  160. blockToolbar.panelView.isVisible = true;
  161. expect( blockToolbar.buttonView.tooltip ).to.be.false;
  162. } );
  163. it( 'should hide the #button if empty config was passed', async () => {
  164. // Remove default editor instance.
  165. await editor.destroy();
  166. editor = await ClassicTestEditor.create( element, {
  167. plugins: [ BlockToolbar ]
  168. } );
  169. const blockToolbar = editor.plugins.get( BlockToolbar );
  170. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  171. } );
  172. } );
  173. } );
  174. describe( 'allowed elements', () => {
  175. it( 'should display the button when the first selected block is a block element', () => {
  176. editor.model.schema.register( 'foo', { inheritAllFrom: '$block' } );
  177. editor.conversion.elementToElement( { model: 'foo', view: 'foo' } );
  178. setData( editor.model, '<foo>foo[]bar</foo>' );
  179. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  180. } );
  181. it( 'should display the button when the first selected block is an object', () => {
  182. setData( editor.model, '[<image src="/assets/sample.png"><caption>foo</caption></image>]' );
  183. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  184. } );
  185. // This test makes no sense now, but so do all other tests here (see https://github.com/ckeditor/ckeditor5/issues/1522).
  186. it( 'should not display the button when the selection is inside a limit element', () => {
  187. setData( editor.model, '<image src="/assets/sample.png"><caption>f[]oo</caption></image>' );
  188. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  189. } );
  190. it( 'should not display the button when the selection is placed in the root element', () => {
  191. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  192. setData( editor.model, '<paragraph>foo</paragraph>[]<paragraph>bar</paragraph>' );
  193. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  194. } );
  195. it( 'should not display the button when all toolbar items are disabled for the selected element', () => {
  196. const element = document.createElement( 'div' );
  197. document.body.appendChild( element );
  198. return ClassicTestEditor.create( element, {
  199. plugins: [ BlockToolbar, Heading, HeadingButtonsUI, Paragraph, ParagraphButtonUI, Image ],
  200. blockToolbar: [ 'paragraph', 'heading1', 'heading2' ]
  201. } ).then( editor => {
  202. const blockToolbar = editor.plugins.get( BlockToolbar );
  203. setData( editor.model, '[<image src="/assets/sample.png"></image>]' );
  204. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  205. element.remove();
  206. return editor.destroy();
  207. } );
  208. } );
  209. } );
  210. describe( 'attaching the button to the content', () => {
  211. beforeEach( () => {
  212. // Be sure that window is not scrolled.
  213. testUtils.sinon.stub( window, 'scrollX' ).get( () => 0 );
  214. testUtils.sinon.stub( window, 'scrollY' ).get( () => 0 );
  215. } );
  216. it( 'should attach the right side of the button to the left side of the editable and center with the first line ' +
  217. 'of the selected block #1', () => {
  218. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  219. const target = editor.ui.getEditableElement().querySelector( 'p' );
  220. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  221. styleMock.withArgs( target ).returns( {
  222. lineHeight: '20px',
  223. paddingTop: '10px'
  224. } );
  225. styleMock.callThrough();
  226. testUtils.sinon.stub( editor.ui.getEditableElement(), 'getBoundingClientRect' ).returns( {
  227. left: 200
  228. } );
  229. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  230. top: 500,
  231. left: 300
  232. } );
  233. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  234. width: 100,
  235. height: 100
  236. } );
  237. editor.ui.fire( 'update' );
  238. expect( blockToolbar.buttonView.top ).to.equal( 470 );
  239. expect( blockToolbar.buttonView.left ).to.equal( 100 );
  240. } );
  241. it( 'should attach the right side of the button to the left side of the editable and center with the first line ' +
  242. 'of the selected block #2', () => {
  243. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  244. const target = editor.ui.getEditableElement().querySelector( 'p' );
  245. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  246. styleMock.withArgs( target ).returns( {
  247. lineHeight: 'normal',
  248. fontSize: '20px',
  249. paddingTop: '10px'
  250. } );
  251. styleMock.callThrough();
  252. testUtils.sinon.stub( editor.ui.getEditableElement(), 'getBoundingClientRect' ).returns( {
  253. left: 200
  254. } );
  255. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  256. top: 500,
  257. left: 300
  258. } );
  259. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  260. width: 100,
  261. height: 100
  262. } );
  263. editor.ui.fire( 'update' );
  264. expect( blockToolbar.buttonView.top ).to.equal( 472 );
  265. expect( blockToolbar.buttonView.left ).to.equal( 100 );
  266. } );
  267. it( 'should attach the left side of the button to the right side of the editable when language direction is RTL', () => {
  268. editor.locale.uiLanguageDirection = 'rtl';
  269. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  270. const target = editor.ui.getEditableElement().querySelector( 'p' );
  271. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  272. styleMock.withArgs( target ).returns( {
  273. lineHeight: 'normal',
  274. fontSize: '20px',
  275. paddingTop: '10px'
  276. } );
  277. styleMock.callThrough();
  278. testUtils.sinon.stub( editor.ui.getEditableElement(), 'getBoundingClientRect' ).returns( {
  279. left: 200,
  280. right: 600
  281. } );
  282. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  283. top: 500,
  284. left: 300
  285. } );
  286. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  287. width: 100,
  288. height: 100
  289. } );
  290. editor.ui.fire( 'update' );
  291. expect( blockToolbar.buttonView.top ).to.equal( 472 );
  292. expect( blockToolbar.buttonView.left ).to.equal( 600 );
  293. } );
  294. it( 'should reposition the #panelView when open on ui#update', () => {
  295. blockToolbar.panelView.isVisible = false;
  296. const spy = testUtils.sinon.spy( blockToolbar.panelView, 'pin' );
  297. editor.ui.fire( 'update' );
  298. sinon.assert.notCalled( spy );
  299. blockToolbar.panelView.isVisible = true;
  300. editor.ui.fire( 'update' );
  301. sinon.assert.calledWith( spy, {
  302. target: blockToolbar.buttonView.element,
  303. limiter: editor.ui.getEditableElement()
  304. } );
  305. } );
  306. it( 'should not reset the toolbar focus on ui#update', () => {
  307. blockToolbar.panelView.isVisible = true;
  308. const spy = testUtils.sinon.spy( blockToolbar.toolbarView, 'focus' );
  309. editor.ui.fire( 'update' );
  310. sinon.assert.notCalled( spy );
  311. } );
  312. it( 'should hide the open panel on a direct selection change', () => {
  313. blockToolbar.panelView.isVisible = true;
  314. editor.model.document.selection.fire( 'change:range', { directChange: true } );
  315. expect( blockToolbar.panelView.isVisible ).to.be.false;
  316. } );
  317. it( 'should not hide the open panel on an indirect selection change', () => {
  318. blockToolbar.panelView.isVisible = true;
  319. editor.model.document.selection.fire( 'change:range', { directChange: false } );
  320. expect( blockToolbar.panelView.isVisible ).to.be.true;
  321. } );
  322. it( 'should hide the UI when editor switched to readonly', () => {
  323. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  324. blockToolbar.buttonView.isVisible = true;
  325. blockToolbar.panelView.isVisible = true;
  326. editor.isReadOnly = true;
  327. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  328. expect( blockToolbar.panelView.isVisible ).to.be.false;
  329. } );
  330. it( 'should show the button when the editor switched back from readonly', () => {
  331. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  332. expect( blockToolbar.buttonView.isVisible ).to.true;
  333. editor.isReadOnly = true;
  334. expect( blockToolbar.buttonView.isVisible ).to.false;
  335. editor.isReadOnly = false;
  336. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  337. } );
  338. it( 'should show/hide the button on the editor focus/blur', () => {
  339. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  340. editor.ui.focusTracker.isFocused = true;
  341. expect( blockToolbar.buttonView.isVisible ).to.true;
  342. editor.ui.focusTracker.isFocused = false;
  343. expect( blockToolbar.buttonView.isVisible ).to.false;
  344. editor.ui.focusTracker.isFocused = true;
  345. expect( blockToolbar.buttonView.isVisible ).to.true;
  346. } );
  347. it( 'should hide the UI when the editor switched to the readonly when the panel is not visible', () => {
  348. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  349. blockToolbar.buttonView.isVisible = true;
  350. blockToolbar.panelView.isVisible = false;
  351. editor.isReadOnly = true;
  352. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  353. expect( blockToolbar.panelView.isVisible ).to.be.false;
  354. } );
  355. it( 'should update the button position on browser resize only when the button is visible', () => {
  356. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  357. const spy = testUtils.sinon.spy( blockToolbar, '_attachButtonToElement' );
  358. // Place the selection outside of any block because the toolbar will not be shown in this case.
  359. setData( editor.model, '[]<paragraph>bar</paragraph>' );
  360. window.dispatchEvent( new Event( 'resize' ) );
  361. sinon.assert.notCalled( spy );
  362. setData( editor.model, '<paragraph>ba[]r</paragraph>' );
  363. spy.resetHistory();
  364. window.dispatchEvent( new Event( 'resize' ) );
  365. sinon.assert.called( spy );
  366. setData( editor.model, '[]<paragraph>bar</paragraph>' );
  367. spy.resetHistory();
  368. window.dispatchEvent( new Event( 'resize' ) );
  369. sinon.assert.notCalled( spy );
  370. } );
  371. } );
  372. } );