8
0

blocktoolbar.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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 global from '@ckeditor/ckeditor5-utils/src/dom/global';
  19. import ResizeObserver from '@ckeditor/ckeditor5-utils/src/dom/resizeobserver';
  20. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  21. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  22. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  23. import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
  24. describe( 'BlockToolbar', () => {
  25. let editor, element, blockToolbar;
  26. let resizeCallback;
  27. testUtils.createSinonSandbox();
  28. beforeEach( () => {
  29. element = document.createElement( 'div' );
  30. document.body.appendChild( element );
  31. // Make sure other tests of the editor do not affect tests that follow.
  32. // Without it, if an instance of ResizeObserver already exists somewhere undestroyed
  33. // in DOM, the following DOM mock will have no effect.
  34. ResizeObserver._observerInstance = null;
  35. testUtils.sinon.stub( global.window, 'ResizeObserver' ).callsFake( callback => {
  36. resizeCallback = callback;
  37. return {
  38. observe: sinon.spy(),
  39. unobserve: sinon.spy()
  40. };
  41. } );
  42. return ClassicTestEditor.create( element, {
  43. plugins: [ BlockToolbar, Heading, HeadingButtonsUI, Paragraph, ParagraphButtonUI, BlockQuote, Image, ImageCaption ],
  44. blockToolbar: [ 'paragraph', 'heading1', 'heading2', 'blockQuote' ]
  45. } ).then( newEditor => {
  46. editor = newEditor;
  47. blockToolbar = editor.plugins.get( BlockToolbar );
  48. editor.ui.focusTracker.isFocused = true;
  49. } );
  50. } );
  51. afterEach( () => {
  52. // Blur editor so `blockToolbar.buttonView` `window.resize` listener is detached.
  53. editor.ui.focusTracker.isFocused = false;
  54. element.remove();
  55. return editor.destroy();
  56. } );
  57. it( 'should have pluginName property', () => {
  58. expect( BlockToolbar.pluginName ).to.equal( 'BlockToolbar' );
  59. } );
  60. it( 'should not throw when empty config is provided', async () => {
  61. // Remove default editor instance.
  62. await editor.destroy();
  63. editor = await ClassicTestEditor.create( element, {
  64. plugins: [ BlockToolbar ]
  65. } );
  66. } );
  67. it( 'should accept the extended format of the toolbar config', () => {
  68. return ClassicTestEditor
  69. .create( element, {
  70. plugins: [ BlockToolbar, Heading, HeadingButtonsUI, Paragraph, ParagraphButtonUI, BlockQuote ],
  71. blockToolbar: {
  72. items: [ 'paragraph', 'heading1', 'heading2', 'blockQuote' ]
  73. }
  74. } )
  75. .then( editor => {
  76. blockToolbar = editor.plugins.get( BlockToolbar );
  77. expect( blockToolbar.toolbarView.items ).to.length( 4 );
  78. element.remove();
  79. return editor.destroy();
  80. } );
  81. } );
  82. it( 'should not group items when the config.shouldNotGroupWhenFull option is enabled', () => {
  83. return ClassicTestEditor.create( element, {
  84. plugins: [ BlockToolbar, Heading, HeadingButtonsUI, Paragraph, ParagraphButtonUI, BlockQuote ],
  85. blockToolbar: {
  86. items: [ 'paragraph', 'heading1', 'heading2', 'blockQuote' ],
  87. shouldNotGroupWhenFull: true
  88. }
  89. } ).then( editor => {
  90. const blockToolbar = editor.plugins.get( BlockToolbar );
  91. expect( blockToolbar.toolbarView.options.shouldGroupWhenFull ).to.be.false;
  92. element.remove();
  93. return editor.destroy();
  94. } );
  95. } );
  96. describe( 'child views', () => {
  97. describe( 'panelView', () => {
  98. it( 'should create a view instance', () => {
  99. expect( blockToolbar.panelView ).to.instanceof( BalloonPanelView );
  100. } );
  101. it( 'should have an additional class name', () => {
  102. expect( blockToolbar.panelView.class ).to.equal( 'ck-toolbar-container' );
  103. } );
  104. it( 'should be added to the ui.view.body collection', () => {
  105. expect( Array.from( editor.ui.view.body ) ).to.include( blockToolbar.panelView );
  106. } );
  107. it( 'should add the #panelView to ui.focusTracker', () => {
  108. editor.ui.focusTracker.isFocused = false;
  109. blockToolbar.panelView.element.dispatchEvent( new Event( 'focus' ) );
  110. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  111. } );
  112. it( 'should close the #panelView after `Esc` is pressed and focus view document', () => {
  113. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  114. blockToolbar.panelView.isVisible = true;
  115. blockToolbar.toolbarView.keystrokes.press( {
  116. keyCode: keyCodes.esc,
  117. preventDefault: () => {},
  118. stopPropagation: () => {}
  119. } );
  120. expect( blockToolbar.panelView.isVisible ).to.be.false;
  121. sinon.assert.calledOnce( spy );
  122. } );
  123. it( 'should close the #panelView upon click outside the panel and not focus view document', () => {
  124. const spy = testUtils.sinon.spy();
  125. editor.editing.view.on( 'focus', spy );
  126. blockToolbar.panelView.isVisible = true;
  127. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  128. expect( blockToolbar.panelView.isVisible ).to.be.false;
  129. sinon.assert.notCalled( spy );
  130. } );
  131. it( 'should not close the #panelView upon click on panel element', () => {
  132. blockToolbar.panelView.isVisible = true;
  133. blockToolbar.panelView.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  134. expect( blockToolbar.panelView.isVisible ).to.be.true;
  135. } );
  136. } );
  137. describe( 'toolbarView', () => {
  138. it( 'should create the view instance', () => {
  139. expect( blockToolbar.toolbarView ).to.instanceof( ToolbarView );
  140. } );
  141. it( 'should add an additional class to toolbar element', () => {
  142. expect( blockToolbar.toolbarView.element.classList.contains( 'ck-toolbar_floating' ) ).to.be.true;
  143. } );
  144. it( 'should be added to the panelView#content collection', () => {
  145. expect( Array.from( blockToolbar.panelView.content ) ).to.include( blockToolbar.toolbarView );
  146. } );
  147. it( 'should initialize the toolbar items based on Editor#blockToolbar config', () => {
  148. expect( Array.from( blockToolbar.toolbarView.items ) ).to.length( 4 );
  149. } );
  150. it( 'should hide the panel after clicking on the button from toolbar', () => {
  151. blockToolbar.buttonView.fire( 'execute' );
  152. expect( blockToolbar.panelView.isVisible ).to.be.true;
  153. blockToolbar.toolbarView.items.get( 0 ).fire( 'execute' );
  154. expect( blockToolbar.panelView.isVisible ).to.be.false;
  155. } );
  156. it( 'should hide the panel on toolbar blur', () => {
  157. blockToolbar.toolbarView.focusTracker.isFocused = true;
  158. blockToolbar.buttonView.fire( 'execute' );
  159. expect( blockToolbar.panelView.isVisible ).to.be.true;
  160. blockToolbar.toolbarView.focusTracker.isFocused = false;
  161. expect( blockToolbar.panelView.isVisible ).to.be.false;
  162. } );
  163. it( 'should set a proper toolbar max-width', () => {
  164. const viewElement = editor.ui.view.editable.element;
  165. viewElement.style.width = '400px';
  166. resizeCallback( [ {
  167. target: viewElement,
  168. contentRect: new Rect( viewElement )
  169. } ] );
  170. // The expected width should be a sum of the editable width and distance between
  171. // block toolbar button and editable.
  172. // ---------------------------
  173. // | |
  174. // ___ | |
  175. // |__| | EDITABLE |
  176. // button | |
  177. // | |
  178. // <--------------max-width------------>
  179. const expectedWidth = blockToolbar._getToolbarMaxWidth();
  180. expect( blockToolbar.toolbarView.maxWidth ).to.be.equal( expectedWidth );
  181. } );
  182. } );
  183. describe( 'buttonView', () => {
  184. it( 'should create a view instance', () => {
  185. expect( blockToolbar.buttonView ).to.instanceof( BlockButtonView );
  186. } );
  187. it( 'should be added to the editor ui.view.body collection', () => {
  188. expect( Array.from( editor.ui.view.body ) ).to.include( blockToolbar.buttonView );
  189. } );
  190. it( 'should add the #buttonView to the ui.focusTracker', () => {
  191. editor.ui.focusTracker.isFocused = false;
  192. blockToolbar.buttonView.element.dispatchEvent( new Event( 'focus' ) );
  193. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  194. } );
  195. it( 'should pin the #panelView to the button and focus first item in toolbar on #execute event', () => {
  196. expect( blockToolbar.panelView.isVisible ).to.be.false;
  197. const pinSpy = testUtils.sinon.spy( blockToolbar.panelView, 'pin' );
  198. const focusSpy = testUtils.sinon.spy( blockToolbar.toolbarView.items.get( 0 ), 'focus' );
  199. blockToolbar.buttonView.fire( 'execute' );
  200. expect( blockToolbar.panelView.isVisible ).to.be.true;
  201. sinon.assert.calledWith( pinSpy, {
  202. target: blockToolbar.buttonView.element,
  203. limiter: editor.ui.getEditableElement()
  204. } );
  205. sinon.assert.calledOnce( focusSpy );
  206. } );
  207. it( 'should hide the #panelView and focus the editable on #execute event when panel was visible', () => {
  208. blockToolbar.panelView.isVisible = true;
  209. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  210. blockToolbar.buttonView.fire( 'execute' );
  211. expect( blockToolbar.panelView.isVisible ).to.be.false;
  212. sinon.assert.calledOnce( spy );
  213. } );
  214. it( 'should bind #isOn to panelView#isVisible', () => {
  215. blockToolbar.panelView.isVisible = false;
  216. expect( blockToolbar.buttonView.isOn ).to.be.false;
  217. blockToolbar.panelView.isVisible = true;
  218. expect( blockToolbar.buttonView.isOn ).to.be.true;
  219. } );
  220. it( 'should hide the #button tooltip when the #panelView is open', () => {
  221. blockToolbar.panelView.isVisible = false;
  222. expect( blockToolbar.buttonView.tooltip ).to.be.true;
  223. blockToolbar.panelView.isVisible = true;
  224. expect( blockToolbar.buttonView.tooltip ).to.be.false;
  225. } );
  226. it( 'should hide the #button if empty config was passed', async () => {
  227. // Remove default editor instance.
  228. await editor.destroy();
  229. editor = await ClassicTestEditor.create( element, {
  230. plugins: [ BlockToolbar ]
  231. } );
  232. const blockToolbar = editor.plugins.get( BlockToolbar );
  233. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  234. } );
  235. } );
  236. } );
  237. describe( 'allowed elements', () => {
  238. it( 'should display the button when the first selected block is a block element', () => {
  239. editor.model.schema.register( 'foo', { inheritAllFrom: '$block' } );
  240. editor.conversion.elementToElement( { model: 'foo', view: 'foo' } );
  241. setData( editor.model, '<foo>foo[]bar</foo>' );
  242. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  243. } );
  244. it( 'should display the button when the first selected block is an object', () => {
  245. setData( editor.model, '[<image src="/assets/sample.png"><caption>foo</caption></image>]' );
  246. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  247. } );
  248. // This test makes no sense now, but so do all other tests here (see https://github.com/ckeditor/ckeditor5/issues/1522).
  249. it( 'should not display the button when the selection is inside a limit element', () => {
  250. setData( editor.model, '<image src="/assets/sample.png"><caption>f[]oo</caption></image>' );
  251. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  252. } );
  253. it( 'should not display the button when the selection is placed in the root element', () => {
  254. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  255. setData( editor.model, '<paragraph>foo</paragraph>[]<paragraph>bar</paragraph>' );
  256. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  257. } );
  258. it( 'should not display the button when all toolbar items are disabled for the selected element', () => {
  259. const element = document.createElement( 'div' );
  260. document.body.appendChild( element );
  261. return ClassicTestEditor.create( element, {
  262. plugins: [ BlockToolbar, Heading, HeadingButtonsUI, Paragraph, ParagraphButtonUI, Image ],
  263. blockToolbar: [ 'paragraph', 'heading1', 'heading2' ]
  264. } ).then( editor => {
  265. const blockToolbar = editor.plugins.get( BlockToolbar );
  266. setData( editor.model, '[<image src="/assets/sample.png"></image>]' );
  267. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  268. element.remove();
  269. return editor.destroy();
  270. } );
  271. } );
  272. } );
  273. describe( 'attaching the button to the content', () => {
  274. beforeEach( () => {
  275. // Be sure that window is not scrolled.
  276. testUtils.sinon.stub( window, 'scrollX' ).get( () => 0 );
  277. testUtils.sinon.stub( window, 'scrollY' ).get( () => 0 );
  278. } );
  279. it( 'should attach the right side of the button to the left side of the editable and center with the first line ' +
  280. 'of the selected block #1', () => {
  281. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  282. const target = editor.ui.getEditableElement().querySelector( 'p' );
  283. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  284. styleMock.withArgs( target ).returns( {
  285. lineHeight: '20px',
  286. paddingTop: '10px'
  287. } );
  288. styleMock.callThrough();
  289. testUtils.sinon.stub( editor.ui.getEditableElement(), 'getBoundingClientRect' ).returns( {
  290. left: 200
  291. } );
  292. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  293. top: 500,
  294. left: 300
  295. } );
  296. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  297. width: 100,
  298. height: 100
  299. } );
  300. editor.ui.fire( 'update' );
  301. expect( blockToolbar.buttonView.top ).to.equal( 470 );
  302. expect( blockToolbar.buttonView.left ).to.equal( 100 );
  303. } );
  304. it( 'should attach the right side of the button to the left side of the editable and center with the first line ' +
  305. 'of the selected block #2', () => {
  306. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  307. const target = editor.ui.getEditableElement().querySelector( 'p' );
  308. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  309. styleMock.withArgs( target ).returns( {
  310. lineHeight: 'normal',
  311. fontSize: '20px',
  312. paddingTop: '10px'
  313. } );
  314. styleMock.callThrough();
  315. testUtils.sinon.stub( editor.ui.getEditableElement(), 'getBoundingClientRect' ).returns( {
  316. left: 200
  317. } );
  318. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  319. top: 500,
  320. left: 300
  321. } );
  322. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  323. width: 100,
  324. height: 100
  325. } );
  326. editor.ui.fire( 'update' );
  327. expect( blockToolbar.buttonView.top ).to.equal( 472 );
  328. expect( blockToolbar.buttonView.left ).to.equal( 100 );
  329. } );
  330. it( 'should attach the left side of the button to the right side of the editable when language direction is RTL', () => {
  331. editor.locale.uiLanguageDirection = 'rtl';
  332. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  333. const target = editor.ui.getEditableElement().querySelector( 'p' );
  334. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  335. styleMock.withArgs( target ).returns( {
  336. lineHeight: 'normal',
  337. fontSize: '20px',
  338. paddingTop: '10px'
  339. } );
  340. styleMock.callThrough();
  341. testUtils.sinon.stub( editor.ui.getEditableElement(), 'getBoundingClientRect' ).returns( {
  342. left: 200,
  343. right: 600
  344. } );
  345. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  346. top: 500,
  347. left: 300
  348. } );
  349. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  350. width: 100,
  351. height: 100
  352. } );
  353. editor.ui.fire( 'update' );
  354. expect( blockToolbar.buttonView.top ).to.equal( 472 );
  355. expect( blockToolbar.buttonView.left ).to.equal( 600 );
  356. } );
  357. it( 'should reposition the #panelView when open on ui#update', () => {
  358. blockToolbar.panelView.isVisible = false;
  359. const spy = testUtils.sinon.spy( blockToolbar.panelView, 'pin' );
  360. editor.ui.fire( 'update' );
  361. sinon.assert.notCalled( spy );
  362. blockToolbar.panelView.isVisible = true;
  363. editor.ui.fire( 'update' );
  364. sinon.assert.calledWith( spy, {
  365. target: blockToolbar.buttonView.element,
  366. limiter: editor.ui.getEditableElement()
  367. } );
  368. } );
  369. it( 'should not reset the toolbar focus on ui#update', () => {
  370. blockToolbar.panelView.isVisible = true;
  371. const spy = testUtils.sinon.spy( blockToolbar.toolbarView, 'focus' );
  372. editor.ui.fire( 'update' );
  373. sinon.assert.notCalled( spy );
  374. } );
  375. it( 'should hide the open panel on a direct selection change', () => {
  376. blockToolbar.panelView.isVisible = true;
  377. editor.model.document.selection.fire( 'change:range', { directChange: true } );
  378. expect( blockToolbar.panelView.isVisible ).to.be.false;
  379. } );
  380. it( 'should not hide the open panel on an indirect selection change', () => {
  381. blockToolbar.panelView.isVisible = true;
  382. editor.model.document.selection.fire( 'change:range', { directChange: false } );
  383. expect( blockToolbar.panelView.isVisible ).to.be.true;
  384. } );
  385. it( 'should hide the UI when editor switched to readonly', () => {
  386. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  387. blockToolbar.buttonView.isVisible = true;
  388. blockToolbar.panelView.isVisible = true;
  389. editor.isReadOnly = true;
  390. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  391. expect( blockToolbar.panelView.isVisible ).to.be.false;
  392. } );
  393. it( 'should show the button when the editor switched back from readonly', () => {
  394. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  395. expect( blockToolbar.buttonView.isVisible ).to.true;
  396. editor.isReadOnly = true;
  397. expect( blockToolbar.buttonView.isVisible ).to.false;
  398. editor.isReadOnly = false;
  399. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  400. } );
  401. it( 'should show/hide the button on the editor focus/blur', () => {
  402. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  403. editor.ui.focusTracker.isFocused = true;
  404. expect( blockToolbar.buttonView.isVisible ).to.true;
  405. editor.ui.focusTracker.isFocused = false;
  406. expect( blockToolbar.buttonView.isVisible ).to.false;
  407. editor.ui.focusTracker.isFocused = true;
  408. expect( blockToolbar.buttonView.isVisible ).to.true;
  409. } );
  410. it( 'should hide the UI when the editor switched to the readonly when the panel is not visible', () => {
  411. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  412. blockToolbar.buttonView.isVisible = true;
  413. blockToolbar.panelView.isVisible = false;
  414. editor.isReadOnly = true;
  415. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  416. expect( blockToolbar.panelView.isVisible ).to.be.false;
  417. } );
  418. it( 'should update the button position on browser resize only when the button is visible', () => {
  419. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  420. const spy = testUtils.sinon.spy( blockToolbar, '_attachButtonToElement' );
  421. // Place the selection outside of any block because the toolbar will not be shown in this case.
  422. setData( editor.model, '[]<paragraph>bar</paragraph>' );
  423. window.dispatchEvent( new Event( 'resize' ) );
  424. sinon.assert.notCalled( spy );
  425. setData( editor.model, '<paragraph>ba[]r</paragraph>' );
  426. spy.resetHistory();
  427. window.dispatchEvent( new Event( 'resize' ) );
  428. sinon.assert.called( spy );
  429. setData( editor.model, '[]<paragraph>bar</paragraph>' );
  430. spy.resetHistory();
  431. window.dispatchEvent( new Event( 'resize' ) );
  432. sinon.assert.notCalled( spy );
  433. } );
  434. } );
  435. } );