blocktoolbar.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. } );
  164. describe( 'buttonView', () => {
  165. it( 'should create a view instance', () => {
  166. expect( blockToolbar.buttonView ).to.instanceof( BlockButtonView );
  167. } );
  168. it( 'should be added to the editor ui.view.body collection', () => {
  169. expect( Array.from( editor.ui.view.body ) ).to.include( blockToolbar.buttonView );
  170. } );
  171. it( 'should add the #buttonView to the ui.focusTracker', () => {
  172. editor.ui.focusTracker.isFocused = false;
  173. blockToolbar.buttonView.element.dispatchEvent( new Event( 'focus' ) );
  174. expect( editor.ui.focusTracker.isFocused ).to.be.true;
  175. } );
  176. it( 'should pin the #panelView to the button and focus first item in toolbar on #execute event', () => {
  177. expect( blockToolbar.panelView.isVisible ).to.be.false;
  178. const pinSpy = testUtils.sinon.spy( blockToolbar.panelView, 'pin' );
  179. const focusSpy = testUtils.sinon.spy( blockToolbar.toolbarView.items.get( 0 ), 'focus' );
  180. blockToolbar.buttonView.fire( 'execute' );
  181. expect( blockToolbar.panelView.isVisible ).to.be.true;
  182. sinon.assert.calledWith( pinSpy, {
  183. target: blockToolbar.buttonView.element,
  184. limiter: editor.ui.getEditableElement()
  185. } );
  186. sinon.assert.calledOnce( focusSpy );
  187. } );
  188. it( 'should hide the #panelView and focus the editable on #execute event when panel was visible', () => {
  189. blockToolbar.panelView.isVisible = true;
  190. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  191. blockToolbar.buttonView.fire( 'execute' );
  192. expect( blockToolbar.panelView.isVisible ).to.be.false;
  193. sinon.assert.calledOnce( spy );
  194. } );
  195. it( 'should bind #isOn to panelView#isVisible', () => {
  196. blockToolbar.panelView.isVisible = false;
  197. expect( blockToolbar.buttonView.isOn ).to.be.false;
  198. blockToolbar.panelView.isVisible = true;
  199. expect( blockToolbar.buttonView.isOn ).to.be.true;
  200. } );
  201. it( 'should hide the #button tooltip when the #panelView is open', () => {
  202. blockToolbar.panelView.isVisible = false;
  203. expect( blockToolbar.buttonView.tooltip ).to.be.true;
  204. blockToolbar.panelView.isVisible = true;
  205. expect( blockToolbar.buttonView.tooltip ).to.be.false;
  206. } );
  207. it( 'should hide the #button if empty config was passed', async () => {
  208. // Remove default editor instance.
  209. await editor.destroy();
  210. editor = await ClassicTestEditor.create( element, {
  211. plugins: [ BlockToolbar ]
  212. } );
  213. const blockToolbar = editor.plugins.get( BlockToolbar );
  214. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  215. } );
  216. } );
  217. } );
  218. describe( 'allowed elements', () => {
  219. it( 'should display the button when the first selected block is a block element', () => {
  220. editor.model.schema.register( 'foo', { inheritAllFrom: '$block' } );
  221. editor.conversion.elementToElement( { model: 'foo', view: 'foo' } );
  222. setData( editor.model, '<foo>foo[]bar</foo>' );
  223. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  224. } );
  225. it( 'should display the button when the first selected block is an object', () => {
  226. setData( editor.model, '[<image src="/assets/sample.png"><caption>foo</caption></image>]' );
  227. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  228. } );
  229. // This test makes no sense now, but so do all other tests here (see https://github.com/ckeditor/ckeditor5/issues/1522).
  230. it( 'should not display the button when the selection is inside a limit element', () => {
  231. setData( editor.model, '<image src="/assets/sample.png"><caption>f[]oo</caption></image>' );
  232. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  233. } );
  234. it( 'should not display the button when the selection is placed in the root element', () => {
  235. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  236. setData( editor.model, '<paragraph>foo</paragraph>[]<paragraph>bar</paragraph>' );
  237. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  238. } );
  239. it( 'should not display the button when all toolbar items are disabled for the selected element', () => {
  240. const element = document.createElement( 'div' );
  241. document.body.appendChild( element );
  242. return ClassicTestEditor.create( element, {
  243. plugins: [ BlockToolbar, Heading, HeadingButtonsUI, Paragraph, ParagraphButtonUI, Image ],
  244. blockToolbar: [ 'paragraph', 'heading1', 'heading2' ]
  245. } ).then( editor => {
  246. const blockToolbar = editor.plugins.get( BlockToolbar );
  247. setData( editor.model, '[<image src="/assets/sample.png"></image>]' );
  248. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  249. element.remove();
  250. return editor.destroy();
  251. } );
  252. } );
  253. } );
  254. describe( 'attaching the button to the content', () => {
  255. beforeEach( () => {
  256. // Be sure that window is not scrolled.
  257. testUtils.sinon.stub( window, 'scrollX' ).get( () => 0 );
  258. testUtils.sinon.stub( window, 'scrollY' ).get( () => 0 );
  259. } );
  260. it( 'should attach the right side of the button to the left side of the editable and center with the first line ' +
  261. 'of the selected block #1', () => {
  262. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  263. const target = editor.ui.getEditableElement().querySelector( 'p' );
  264. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  265. styleMock.withArgs( target ).returns( {
  266. lineHeight: '20px',
  267. paddingTop: '10px'
  268. } );
  269. styleMock.callThrough();
  270. testUtils.sinon.stub( editor.ui.getEditableElement(), 'getBoundingClientRect' ).returns( {
  271. left: 200
  272. } );
  273. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  274. top: 500,
  275. left: 300
  276. } );
  277. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  278. width: 100,
  279. height: 100
  280. } );
  281. editor.ui.fire( 'update' );
  282. expect( blockToolbar.buttonView.top ).to.equal( 470 );
  283. expect( blockToolbar.buttonView.left ).to.equal( 100 );
  284. } );
  285. it( 'should attach the right side of the button to the left side of the editable and center with the first line ' +
  286. 'of the selected block #2', () => {
  287. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  288. const target = editor.ui.getEditableElement().querySelector( 'p' );
  289. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  290. styleMock.withArgs( target ).returns( {
  291. lineHeight: 'normal',
  292. fontSize: '20px',
  293. paddingTop: '10px'
  294. } );
  295. styleMock.callThrough();
  296. testUtils.sinon.stub( editor.ui.getEditableElement(), 'getBoundingClientRect' ).returns( {
  297. left: 200
  298. } );
  299. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  300. top: 500,
  301. left: 300
  302. } );
  303. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  304. width: 100,
  305. height: 100
  306. } );
  307. editor.ui.fire( 'update' );
  308. expect( blockToolbar.buttonView.top ).to.equal( 472 );
  309. expect( blockToolbar.buttonView.left ).to.equal( 100 );
  310. } );
  311. it( 'should attach the left side of the button to the right side of the editable when language direction is RTL', () => {
  312. editor.locale.uiLanguageDirection = 'rtl';
  313. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  314. const target = editor.ui.getEditableElement().querySelector( 'p' );
  315. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  316. styleMock.withArgs( target ).returns( {
  317. lineHeight: 'normal',
  318. fontSize: '20px',
  319. paddingTop: '10px'
  320. } );
  321. styleMock.callThrough();
  322. testUtils.sinon.stub( editor.ui.getEditableElement(), 'getBoundingClientRect' ).returns( {
  323. left: 200,
  324. right: 600
  325. } );
  326. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  327. top: 500,
  328. left: 300
  329. } );
  330. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  331. width: 100,
  332. height: 100
  333. } );
  334. editor.ui.fire( 'update' );
  335. expect( blockToolbar.buttonView.top ).to.equal( 472 );
  336. expect( blockToolbar.buttonView.left ).to.equal( 600 );
  337. } );
  338. describe( 'toolbarView#maxWidth', () => {
  339. it( 'should be set when the panel shows up', () => {
  340. expect( blockToolbar.toolbarView.maxWidth ).to.equal( 'auto' );
  341. blockToolbar.buttonView.fire( 'execute' );
  342. expect( blockToolbar.panelView.isVisible ).to.be.true;
  343. expect( blockToolbar.toolbarView.maxWidth ).to.match( /.+px/ );
  344. } );
  345. it( 'should be set after the toolbar was shown (but before panel#pin()) to let the items grouping do its job', () => {
  346. const maxWidthSetSpy = sinon.spy( blockToolbar.toolbarView, 'maxWidth', [ 'set' ] );
  347. const panelViewShowSpy = sinon.spy( blockToolbar.panelView, 'show' );
  348. const panelViewPinSpy = sinon.spy( blockToolbar.panelView, 'pin' );
  349. blockToolbar.buttonView.fire( 'execute' );
  350. sinon.assert.callOrder( panelViewShowSpy, maxWidthSetSpy.set, panelViewPinSpy );
  351. } );
  352. it( 'should set a proper toolbar max-width', () => {
  353. const viewElement = editor.ui.getEditableElement();
  354. testUtils.sinon.stub( viewElement, 'getBoundingClientRect' ).returns( {
  355. left: 100,
  356. width: 400
  357. } );
  358. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  359. left: 60,
  360. width: 40
  361. } );
  362. resizeCallback( [ {
  363. target: viewElement,
  364. contentRect: new Rect( viewElement )
  365. } ] );
  366. blockToolbar.buttonView.fire( 'execute' );
  367. // The expected width should be equal the distance between
  368. // left edge of the block toolbar button and right edge of the editable.
  369. // ---------------------------
  370. // | |
  371. // ____ | |
  372. // |__| | EDITABLE |
  373. // button | |
  374. // | |
  375. // <--------------max-width------------>
  376. expect( blockToolbar.toolbarView.maxWidth ).to.equal( '440px' );
  377. } );
  378. it( 'should set a proper toolbar max-width in RTL', () => {
  379. const viewElement = editor.ui.getEditableElement();
  380. editor.locale.uiLanguageDirection = 'rtl';
  381. testUtils.sinon.stub( viewElement, 'getBoundingClientRect' ).returns( {
  382. right: 450,
  383. width: 400
  384. } );
  385. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  386. left: 450,
  387. width: 40
  388. } );
  389. resizeCallback( [ {
  390. target: viewElement,
  391. contentRect: new Rect( viewElement )
  392. } ] );
  393. blockToolbar.buttonView.fire( 'execute' );
  394. // The expected width should be equal the distance between
  395. // left edge of the editable and right edge of the block toolbar button.
  396. // ---------------------------
  397. // | |
  398. // | | ____
  399. // | EDITABLE | |__|
  400. // | | button
  401. // | |
  402. // <--------------max-width------------>
  403. expect( blockToolbar.toolbarView.maxWidth ).to.equal( '440px' );
  404. } );
  405. } );
  406. it( 'should reposition the #panelView when open on ui#update', () => {
  407. blockToolbar.panelView.isVisible = false;
  408. const spy = testUtils.sinon.spy( blockToolbar.panelView, 'pin' );
  409. editor.ui.fire( 'update' );
  410. sinon.assert.notCalled( spy );
  411. blockToolbar.panelView.isVisible = true;
  412. editor.ui.fire( 'update' );
  413. sinon.assert.calledWith( spy, {
  414. target: blockToolbar.buttonView.element,
  415. limiter: editor.ui.getEditableElement()
  416. } );
  417. } );
  418. it( 'should not reset the toolbar focus on ui#update', () => {
  419. blockToolbar.panelView.isVisible = true;
  420. const spy = testUtils.sinon.spy( blockToolbar.toolbarView, 'focus' );
  421. editor.ui.fire( 'update' );
  422. sinon.assert.notCalled( spy );
  423. } );
  424. it( 'should hide the open panel on a direct selection change', () => {
  425. blockToolbar.panelView.isVisible = true;
  426. editor.model.document.selection.fire( 'change:range', { directChange: true } );
  427. expect( blockToolbar.panelView.isVisible ).to.be.false;
  428. } );
  429. it( 'should not hide the open panel on an indirect selection change', () => {
  430. blockToolbar.panelView.isVisible = true;
  431. editor.model.document.selection.fire( 'change:range', { directChange: false } );
  432. expect( blockToolbar.panelView.isVisible ).to.be.true;
  433. } );
  434. it( 'should hide the UI when editor switched to readonly', () => {
  435. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  436. blockToolbar.buttonView.isVisible = true;
  437. blockToolbar.panelView.isVisible = true;
  438. editor.isReadOnly = true;
  439. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  440. expect( blockToolbar.panelView.isVisible ).to.be.false;
  441. } );
  442. it( 'should show the button when the editor switched back from readonly', () => {
  443. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  444. expect( blockToolbar.buttonView.isVisible ).to.true;
  445. editor.isReadOnly = true;
  446. expect( blockToolbar.buttonView.isVisible ).to.false;
  447. editor.isReadOnly = false;
  448. expect( blockToolbar.buttonView.isVisible ).to.be.true;
  449. } );
  450. it( 'should show/hide the button on the editor focus/blur', () => {
  451. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  452. editor.ui.focusTracker.isFocused = true;
  453. expect( blockToolbar.buttonView.isVisible ).to.true;
  454. editor.ui.focusTracker.isFocused = false;
  455. expect( blockToolbar.buttonView.isVisible ).to.false;
  456. editor.ui.focusTracker.isFocused = true;
  457. expect( blockToolbar.buttonView.isVisible ).to.true;
  458. } );
  459. it( 'should hide the UI when the editor switched to the readonly when the panel is not visible', () => {
  460. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  461. blockToolbar.buttonView.isVisible = true;
  462. blockToolbar.panelView.isVisible = false;
  463. editor.isReadOnly = true;
  464. expect( blockToolbar.buttonView.isVisible ).to.be.false;
  465. expect( blockToolbar.panelView.isVisible ).to.be.false;
  466. } );
  467. it( 'should update the button position on browser resize only when the button is visible', () => {
  468. editor.model.schema.extend( '$text', { allowIn: '$root' } );
  469. const spy = testUtils.sinon.spy( blockToolbar, '_attachButtonToElement' );
  470. // Place the selection outside of any block because the toolbar will not be shown in this case.
  471. setData( editor.model, '[]<paragraph>bar</paragraph>' );
  472. window.dispatchEvent( new Event( 'resize' ) );
  473. sinon.assert.notCalled( spy );
  474. setData( editor.model, '<paragraph>ba[]r</paragraph>' );
  475. spy.resetHistory();
  476. window.dispatchEvent( new Event( 'resize' ) );
  477. sinon.assert.called( spy );
  478. setData( editor.model, '[]<paragraph>bar</paragraph>' );
  479. spy.resetHistory();
  480. window.dispatchEvent( new Event( 'resize' ) );
  481. sinon.assert.notCalled( spy );
  482. } );
  483. } );
  484. describe( 'destroy()', () => {
  485. it( 'should destroy #resizeObserver if is available', () => {
  486. const editable = editor.ui.getEditableElement();
  487. const resizeObserver = new ResizeObserver( editable, () => {} );
  488. const destroySpy = sinon.spy( resizeObserver, 'destroy' );
  489. blockToolbar._resizeObserver = resizeObserver;
  490. blockToolbar.destroy();
  491. sinon.assert.calledOnce( destroySpy );
  492. } );
  493. } );
  494. } );