8
0

blocktoolbar.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';
  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/view/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. testUtils.createSinonSandbox();
  22. describe( 'BlockToolbar', () => {
  23. let editor, element, blockToolbar;
  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. } );
  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. it( 'should register click observer', () => {
  43. expect( editor.editing.view.getObserver( ClickObserver ) ).to.be.instanceOf( ClickObserver );
  44. } );
  45. describe( 'child views', () => {
  46. describe( 'panelView', () => {
  47. it( 'should create view instance', () => {
  48. expect( blockToolbar.panelView ).to.instanceof( BalloonPanelView );
  49. } );
  50. it( 'should have additional class name', () => {
  51. expect( blockToolbar.panelView.className ).to.equal( 'ck-balloon-panel-block-toolbar' );
  52. } );
  53. it( 'should be added to ui.view.body collection', () => {
  54. expect( Array.from( editor.ui.view.body ) ).to.include( blockToolbar.panelView );
  55. } );
  56. it( 'should add panelView to ui.focusTracker', () => {
  57. expect( editor.ui.focusTracker.isFocused ).to.false;
  58. blockToolbar.panelView.element.dispatchEvent( new Event( 'focus' ) );
  59. expect( editor.ui.focusTracker.isFocused ).to.true;
  60. } );
  61. it( 'should close panelView after `Esc` press 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.false;
  70. sinon.assert.calledOnce( spy );
  71. } );
  72. it( 'should close panelView on 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.false;
  78. sinon.assert.notCalled( spy );
  79. } );
  80. it( 'should not close panelView on 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.true;
  84. } );
  85. } );
  86. describe( 'toolbarView', () => {
  87. it( 'should create view instance', () => {
  88. expect( blockToolbar.toolbarView ).to.instanceof( ToolbarView );
  89. } );
  90. it( 'should add additional class to toolbar element', () => {
  91. expect( blockToolbar.toolbarView.element.classList.contains( 'ck-toolbar_floating' ) ).to.true;
  92. } );
  93. it( 'should be added to panelView#content collection', () => {
  94. expect( Array.from( blockToolbar.panelView.content ) ).to.include( blockToolbar.toolbarView );
  95. } );
  96. it( 'should initialize toolbar items based on Editor#blockToolbar config', () => {
  97. expect( Array.from( blockToolbar.toolbarView.items ) ).to.length( 4 );
  98. } );
  99. it( 'should hide panel after clicking on the button from toolbar', () => {
  100. blockToolbar.buttonView.fire( 'execute' );
  101. expect( blockToolbar.panelView.isVisible ).to.true;
  102. blockToolbar.toolbarView.items.get( 0 ).fire( 'execute' );
  103. expect( blockToolbar.panelView.isVisible ).to.false;
  104. } );
  105. } );
  106. describe( 'buttonView', () => {
  107. it( 'should create view instance', () => {
  108. expect( blockToolbar.buttonView ).to.instanceof( BlockButtonView );
  109. } );
  110. it( 'should be added to editor ui.view.body collection', () => {
  111. expect( Array.from( editor.ui.view.body ) ).to.include( blockToolbar.buttonView );
  112. } );
  113. it( 'should add buttonView to ui.focusTracker', () => {
  114. expect( editor.ui.focusTracker.isFocused ).to.false;
  115. blockToolbar.buttonView.element.dispatchEvent( new Event( 'focus' ) );
  116. expect( editor.ui.focusTracker.isFocused ).to.true;
  117. } );
  118. it( 'should pin panelView to the button and focus first item in toolbar on #execute event', () => {
  119. expect( blockToolbar.panelView.isVisible ).to.false;
  120. const pinSpy = testUtils.sinon.spy( blockToolbar.panelView, 'pin' );
  121. const focusSpy = testUtils.sinon.spy( blockToolbar.toolbarView.items.get( 0 ), 'focus' );
  122. blockToolbar.buttonView.fire( 'execute' );
  123. expect( blockToolbar.panelView.isVisible ).to.true;
  124. sinon.assert.calledWith( pinSpy, {
  125. target: blockToolbar.buttonView.element,
  126. limiter: editor.ui.view.editableElement
  127. } );
  128. sinon.assert.calledOnce( focusSpy );
  129. } );
  130. it( 'should hide panelView and focus editable on #execute event when panel was visible', () => {
  131. blockToolbar.panelView.isVisible = true;
  132. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  133. blockToolbar.buttonView.fire( 'execute' );
  134. expect( blockToolbar.panelView.isVisible ).to.false;
  135. sinon.assert.calledOnce( spy );
  136. } );
  137. it( 'should bind #isOn to panelView#isVisible', () => {
  138. blockToolbar.panelView.isVisible = false;
  139. expect( blockToolbar.buttonView.isOn ).to.false;
  140. blockToolbar.panelView.isVisible = true;
  141. expect( blockToolbar.buttonView.isOn ).to.true;
  142. } );
  143. it( 'should hide button tooltip when panelView is opened', () => {
  144. blockToolbar.panelView.isVisible = false;
  145. expect( blockToolbar.buttonView.tooltip ).to.true;
  146. blockToolbar.panelView.isVisible = true;
  147. expect( blockToolbar.buttonView.tooltip ).to.false;
  148. } );
  149. } );
  150. } );
  151. describe( 'allowed elements', () => {
  152. it( 'should display button when the first selected block is a block element', () => {
  153. editor.model.schema.register( 'foo', { inheritAllFrom: '$block' } );
  154. editor.conversion.elementToElement( { model: 'foo', view: 'foo' } );
  155. setData( editor.model, '<foo>foo[]bar</foo>' );
  156. expect( blockToolbar.buttonView.isVisible ).to.true;
  157. } );
  158. it( 'should display button when the first selected block is an object', () => {
  159. setData( editor.model, '[<image src="foo.jpg"><caption>foo</caption></image>]' );
  160. expect( blockToolbar.buttonView.isVisible ).to.true;
  161. } );
  162. it( 'should display button when the selection is inside the object', () => {
  163. setData( editor.model, '<image src="foo.jpg"><caption>f[]oo</caption></image>' );
  164. expect( blockToolbar.buttonView.isVisible ).to.true;
  165. } );
  166. it( 'should not display button when the selection is placed in a root element', () => {
  167. setData( editor.model, '<paragraph>foo</paragraph>[]<paragraph>bar</paragraph>' );
  168. expect( blockToolbar.buttonView.isVisible ).to.false;
  169. } );
  170. it( 'should not display button when all toolbar items are disabled for the selected element', () => {
  171. const element = document.createElement( 'div' );
  172. document.body.appendChild( element );
  173. return ClassicTestEditor.create( element, {
  174. plugins: [ BlockToolbar, Heading, HeadingButtonsUI, Paragraph, ParagraphButtonUI, Image ],
  175. blockToolbar: [ 'paragraph', 'heading1', 'heading2' ]
  176. } ).then( editor => {
  177. const blockToolbar = editor.plugins.get( BlockToolbar );
  178. setData( editor.model, '[<image src="foo.jpg"></image>]' );
  179. expect( blockToolbar.buttonView.isVisible ).to.false;
  180. element.remove();
  181. return editor.destroy();
  182. } );
  183. } );
  184. } );
  185. describe( 'attaching button to the content', () => {
  186. it( 'should attach right side of the button to the left side of the editable and center with the first line ' +
  187. 'of selected block #1', () => {
  188. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  189. const target = editor.ui.view.editableElement.querySelector( 'p' );
  190. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  191. styleMock.withArgs( target ).returns( {
  192. lineHeight: '20px',
  193. paddingTop: '10px'
  194. } );
  195. styleMock.callThrough();
  196. testUtils.sinon.stub( editor.ui.view.editableElement, 'getBoundingClientRect' ).returns( {
  197. left: 200
  198. } );
  199. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  200. top: 500,
  201. left: 300
  202. } );
  203. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  204. width: 100,
  205. height: 100
  206. } );
  207. editor.editing.view.fire( 'render' );
  208. expect( blockToolbar.buttonView.top ).to.equal( 470 );
  209. expect( blockToolbar.buttonView.left ).to.equal( 100 );
  210. } );
  211. it( 'should attach right side of the button to the left side of the editable and center with the first line ' +
  212. 'of selected block #2', () => {
  213. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  214. const target = editor.ui.view.editableElement.querySelector( 'p' );
  215. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  216. styleMock.withArgs( target ).returns( {
  217. lineHeight: 'normal',
  218. fontSize: '20px',
  219. paddingTop: '10px'
  220. } );
  221. styleMock.callThrough();
  222. testUtils.sinon.stub( editor.ui.view.editableElement, 'getBoundingClientRect' ).returns( {
  223. left: 200
  224. } );
  225. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  226. top: 500,
  227. left: 300
  228. } );
  229. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  230. width: 100,
  231. height: 100
  232. } );
  233. editor.editing.view.fire( 'render' );
  234. expect( blockToolbar.buttonView.top ).to.equal( 472 );
  235. expect( blockToolbar.buttonView.left ).to.equal( 100 );
  236. } );
  237. it( 'should reposition panelView when is opened on view#render', () => {
  238. blockToolbar.panelView.isVisible = false;
  239. const spy = testUtils.sinon.spy( blockToolbar.panelView, 'pin' );
  240. editor.editing.view.fire( 'render' );
  241. sinon.assert.notCalled( spy );
  242. blockToolbar.panelView.isVisible = true;
  243. editor.editing.view.fire( 'render' );
  244. sinon.assert.calledWith( spy, {
  245. target: blockToolbar.buttonView.element,
  246. limiter: editor.ui.view.editableElement
  247. } );
  248. } );
  249. it( 'should not reset toolbar focus on view#render', () => {
  250. blockToolbar.panelView.isVisible = true;
  251. const spy = testUtils.sinon.spy( blockToolbar.toolbarView, 'focus' );
  252. editor.editing.view.fire( 'render' );
  253. sinon.assert.notCalled( spy );
  254. } );
  255. it( 'should hide opened panel on a selection direct change', () => {
  256. blockToolbar.panelView.isVisible = true;
  257. editor.model.document.selection.fire( 'change:range', { directChange: true } );
  258. expect( blockToolbar.panelView.isVisible ).to.false;
  259. } );
  260. it( 'should not hide opened panel on a selection not direct change', () => {
  261. blockToolbar.panelView.isVisible = true;
  262. editor.model.document.selection.fire( 'change:range', { directChange: false } );
  263. expect( blockToolbar.panelView.isVisible ).to.true;
  264. } );
  265. it( 'should hide UI when editor switch to readonly when panel is not visible', () => {
  266. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  267. blockToolbar.buttonView.isVisible = true;
  268. blockToolbar.panelView.isVisible = false;
  269. editor.isReadOnly = true;
  270. expect( blockToolbar.buttonView.isVisible ).to.false;
  271. expect( blockToolbar.panelView.isVisible ).to.false;
  272. } );
  273. it( 'should not hide button when editor switch to readonly when panel is visible', () => {
  274. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  275. blockToolbar.buttonView.isVisible = true;
  276. blockToolbar.panelView.isVisible = true;
  277. editor.isReadOnly = true;
  278. expect( blockToolbar.buttonView.isVisible ).to.true;
  279. expect( blockToolbar.panelView.isVisible ).to.true;
  280. } );
  281. it( 'should update button position on browser resize only when button is visible', () => {
  282. const spy = testUtils.sinon.spy( blockToolbar, '_attachButtonToElement' );
  283. setData( editor.model, '[]<paragraph>bar</paragraph>' );
  284. window.dispatchEvent( new Event( 'resize' ) );
  285. sinon.assert.notCalled( spy );
  286. setData( editor.model, '<paragraph>ba[]r</paragraph>' );
  287. spy.resetHistory();
  288. window.dispatchEvent( new Event( 'resize' ) );
  289. sinon.assert.called( spy );
  290. setData( editor.model, '[]<paragraph>bar</paragraph>' );
  291. spy.resetHistory();
  292. window.dispatchEvent( new Event( 'resize' ) );
  293. sinon.assert.notCalled( spy );
  294. } );
  295. } );
  296. } );