blocktoolbar.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 List from '@ckeditor/ckeditor5-list/src/list';
  19. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  20. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  21. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  22. testUtils.createSinonSandbox();
  23. describe( 'BlockToolbar', () => {
  24. let editor, element, blockToolbar;
  25. beforeEach( () => {
  26. element = document.createElement( 'div' );
  27. document.body.appendChild( element );
  28. return ClassicTestEditor.create( element, {
  29. plugins: [ BlockToolbar, Heading, HeadingButtonsUI, Paragraph, ParagraphButtonUI, BlockQuote, Image, ImageCaption ],
  30. blockToolbar: [ 'paragraph', 'heading1', 'heading2', 'blockQuote' ]
  31. } ).then( newEditor => {
  32. editor = newEditor;
  33. blockToolbar = editor.plugins.get( BlockToolbar );
  34. } );
  35. } );
  36. afterEach( () => {
  37. element.remove();
  38. return editor.destroy();
  39. } );
  40. it( 'should have pluginName property', () => {
  41. expect( BlockToolbar.pluginName ).to.equal( 'BlockToolbar' );
  42. } );
  43. it( 'should register click observer', () => {
  44. expect( editor.editing.view.getObserver( ClickObserver ) ).to.be.instanceOf( ClickObserver );
  45. } );
  46. it( 'should initialize properly without Heading plugin', () => {
  47. const element = document.createElement( 'div' );
  48. document.body.appendChild( element );
  49. return ClassicTestEditor.create( element, {
  50. plugins: [ BlockToolbar, Paragraph, ParagraphButtonUI, BlockQuote, List ],
  51. blockToolbar: [ 'paragraph', 'blockQuote' ]
  52. } ).then( editor => {
  53. element.remove();
  54. return editor.destroy();
  55. } );
  56. } );
  57. describe( 'child views', () => {
  58. describe( 'panelView', () => {
  59. it( 'should create view instance', () => {
  60. expect( blockToolbar.panelView ).to.instanceof( BalloonPanelView );
  61. } );
  62. it( 'should have additional class name', () => {
  63. expect( blockToolbar.panelView.className ).to.equal( 'ck-balloon-panel-block-toolbar' );
  64. } );
  65. it( 'should be added to ui.view.body collection', () => {
  66. expect( Array.from( editor.ui.view.body ) ).to.include( blockToolbar.panelView );
  67. } );
  68. it( 'should add panelView to ui.focusTracker', () => {
  69. expect( editor.ui.focusTracker.isFocused ).to.false;
  70. blockToolbar.panelView.element.dispatchEvent( new Event( 'focus' ) );
  71. expect( editor.ui.focusTracker.isFocused ).to.true;
  72. } );
  73. it( 'should close panelView after `Esc` press and focus view document', () => {
  74. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  75. blockToolbar.panelView.isVisible = true;
  76. blockToolbar.toolbarView.keystrokes.press( {
  77. keyCode: keyCodes.esc,
  78. preventDefault: () => {},
  79. stopPropagation: () => {}
  80. } );
  81. expect( blockToolbar.panelView.isVisible ).to.false;
  82. sinon.assert.calledOnce( spy );
  83. } );
  84. it( 'should close panelView on click outside the panel and not focus view document', () => {
  85. const spy = testUtils.sinon.spy();
  86. editor.editing.view.on( 'focus', spy );
  87. blockToolbar.panelView.isVisible = true;
  88. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  89. expect( blockToolbar.panelView.isVisible ).to.false;
  90. sinon.assert.notCalled( spy );
  91. } );
  92. it( 'should not close panelView on click on panel element', () => {
  93. blockToolbar.panelView.isVisible = true;
  94. blockToolbar.panelView.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  95. expect( blockToolbar.panelView.isVisible ).to.true;
  96. } );
  97. } );
  98. describe( 'toolbarView', () => {
  99. it( 'should create view instance', () => {
  100. expect( blockToolbar.toolbarView ).to.instanceof( ToolbarView );
  101. } );
  102. it( 'should be added to panelView#content collection', () => {
  103. expect( Array.from( blockToolbar.panelView.content ) ).to.include( blockToolbar.toolbarView );
  104. } );
  105. it( 'should initialize toolbar items based on Editor#blockToolbar config', () => {
  106. expect( Array.from( blockToolbar.toolbarView.items ) ).to.length( 4 );
  107. } );
  108. it( 'should hide panel after clicking on the button from toolbar', () => {
  109. blockToolbar.buttonView.fire( 'execute' );
  110. expect( blockToolbar.panelView.isVisible ).to.true;
  111. blockToolbar.toolbarView.items.get( 0 ).fire( 'execute' );
  112. expect( blockToolbar.panelView.isVisible ).to.false;
  113. } );
  114. } );
  115. describe( 'buttonView', () => {
  116. it( 'should create view instance', () => {
  117. expect( blockToolbar.buttonView ).to.instanceof( BlockButtonView );
  118. } );
  119. it( 'should be added to editor ui.view.body collection', () => {
  120. expect( Array.from( editor.ui.view.body ) ).to.include( blockToolbar.buttonView );
  121. } );
  122. it( 'should add buttonView to ui.focusTracker', () => {
  123. expect( editor.ui.focusTracker.isFocused ).to.false;
  124. blockToolbar.buttonView.element.dispatchEvent( new Event( 'focus' ) );
  125. expect( editor.ui.focusTracker.isFocused ).to.true;
  126. } );
  127. it( 'should pin panelView to the button and focus first item in toolbar on #execute event', () => {
  128. expect( blockToolbar.panelView.isVisible ).to.false;
  129. const pinSpy = testUtils.sinon.spy( blockToolbar.panelView, 'pin' );
  130. const focusSpy = testUtils.sinon.spy( blockToolbar.toolbarView.items.get( 0 ), 'focus' );
  131. blockToolbar.buttonView.fire( 'execute' );
  132. expect( blockToolbar.panelView.isVisible ).to.true;
  133. sinon.assert.calledWith( pinSpy, {
  134. target: blockToolbar.buttonView.element,
  135. limiter: editor.ui.view.editableElement
  136. } );
  137. sinon.assert.calledOnce( focusSpy );
  138. } );
  139. it( 'should hide panelView and focus editable on #execute event when panel was visible', () => {
  140. blockToolbar.panelView.isVisible = true;
  141. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  142. blockToolbar.buttonView.fire( 'execute' );
  143. expect( blockToolbar.panelView.isVisible ).to.false;
  144. sinon.assert.calledOnce( spy );
  145. } );
  146. it( 'should bind #isOn to panelView#isVisible', () => {
  147. blockToolbar.panelView.isVisible = false;
  148. expect( blockToolbar.buttonView.isOn ).to.false;
  149. blockToolbar.panelView.isVisible = true;
  150. expect( blockToolbar.buttonView.isOn ).to.true;
  151. } );
  152. it( 'should hide button tooltip when panelView is opened', () => {
  153. blockToolbar.panelView.isVisible = false;
  154. expect( blockToolbar.buttonView.tooltip ).to.true;
  155. blockToolbar.panelView.isVisible = true;
  156. expect( blockToolbar.buttonView.tooltip ).to.false;
  157. } );
  158. } );
  159. } );
  160. describe( 'allowed elements', () => {
  161. it( 'should display button when selection is placed in a block element', () => {
  162. editor.model.schema.register( 'foo', { inheritAllFrom: '$block' } );
  163. editor.conversion.elementToElement( { model: 'foo', view: 'foo' } );
  164. setData( editor.model, '<foo>foo[]bar</foo>' );
  165. expect( blockToolbar.buttonView.isVisible ).to.true;
  166. } );
  167. it( 'should not display button when selection is placed in an object', () => {
  168. setData( editor.model, '<image src="foo.jpg"><caption>fo[]o</caption></image>' );
  169. expect( blockToolbar.buttonView.isVisible ).to.false;
  170. } );
  171. it( 'should not display button when selection is placed in a root element', () => {
  172. setData( editor.model, '[<image src="foo.jpg"></image>]' );
  173. expect( blockToolbar.buttonView.isVisible ).to.false;
  174. } );
  175. it( 'should make it possible to provide custom validation', () => {
  176. blockToolbar.on( 'checkAllowed', ( evt, args ) => {
  177. const modelElement = args[ 0 ];
  178. if ( modelElement.name === 'heading1' ) {
  179. evt.return = false;
  180. }
  181. } );
  182. setData( editor.model, '<heading1>foo[]bar</heading1>' );
  183. expect( blockToolbar.buttonView.isVisible ).to.false;
  184. } );
  185. } );
  186. describe( 'attaching button to the content', () => {
  187. it( 'should attach button to the left side of selected content and center with the first line on view#render #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: 100
  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 button to the left side of selected content and center with the first line on view#render #2', () => {
  212. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  213. const target = editor.ui.view.editableElement.querySelector( 'p' );
  214. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  215. styleMock.withArgs( target ).returns( {
  216. fontSize: '20px',
  217. paddingTop: '10px'
  218. } );
  219. styleMock.callThrough();
  220. testUtils.sinon.stub( editor.ui.view.editableElement, 'getBoundingClientRect' ).returns( {
  221. left: 100
  222. } );
  223. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  224. top: 500,
  225. left: 300
  226. } );
  227. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  228. width: 100,
  229. height: 100
  230. } );
  231. editor.editing.view.fire( 'render' );
  232. expect( blockToolbar.buttonView.top ).to.equal( 470 );
  233. expect( blockToolbar.buttonView.left ).to.equal( 100 );
  234. } );
  235. it( 'should reposition panelView when is opened on view#render', () => {
  236. blockToolbar.panelView.isVisible = false;
  237. const spy = testUtils.sinon.spy( blockToolbar.panelView, 'pin' );
  238. editor.editing.view.fire( 'render' );
  239. sinon.assert.notCalled( spy );
  240. blockToolbar.panelView.isVisible = true;
  241. editor.editing.view.fire( 'render' );
  242. sinon.assert.calledWith( spy, {
  243. target: blockToolbar.buttonView.element,
  244. limiter: editor.ui.view.editableElement
  245. } );
  246. } );
  247. it( 'should not reset toolbar focus on view#render', () => {
  248. blockToolbar.panelView.isVisible = true;
  249. const spy = testUtils.sinon.spy( blockToolbar.toolbarView, 'focus' );
  250. editor.editing.view.fire( 'render' );
  251. sinon.assert.notCalled( spy );
  252. } );
  253. it( 'should hide opened panel on a selection direct change', () => {
  254. blockToolbar.panelView.isVisible = true;
  255. editor.model.document.selection.fire( 'change:range', { directChange: true } );
  256. expect( blockToolbar.panelView.isVisible ).to.false;
  257. } );
  258. it( 'should not hide opened panel on a selection not direct change', () => {
  259. blockToolbar.panelView.isVisible = true;
  260. editor.model.document.selection.fire( 'change:range', { directChange: false } );
  261. expect( blockToolbar.panelView.isVisible ).to.true;
  262. } );
  263. it( 'should hide button and stop attaching it when editor switch to readonly', () => {
  264. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  265. blockToolbar.panelView.isVisible = true;
  266. expect( blockToolbar.buttonView.isVisible ).to.true;
  267. expect( blockToolbar.panelView.isVisible ).to.true;
  268. editor.isReadOnly = true;
  269. expect( blockToolbar.buttonView.isVisible ).to.false;
  270. expect( blockToolbar.panelView.isVisible ).to.false;
  271. editor.editing.view.fire( 'render' );
  272. expect( blockToolbar.buttonView.isVisible ).to.false;
  273. expect( blockToolbar.panelView.isVisible ).to.false;
  274. editor.isReadOnly = false;
  275. editor.editing.view.fire( 'render' );
  276. expect( blockToolbar.buttonView.isVisible ).to.true;
  277. expect( blockToolbar.panelView.isVisible ).to.false;
  278. } );
  279. it( 'should update button position on browser resize only when button is visible', () => {
  280. const spy = testUtils.sinon.spy( blockToolbar, '_attachButtonToElement' );
  281. setData( editor.model, '[<image src="foo.jpg"></image>]<paragraph>bar</paragraph>' );
  282. window.dispatchEvent( new Event( 'resize' ) );
  283. sinon.assert.notCalled( spy );
  284. setData( editor.model, '<image src="foo.jpg"></image><paragraph>ba[]r</paragraph>' );
  285. spy.resetHistory();
  286. window.dispatchEvent( new Event( 'resize' ) );
  287. sinon.assert.called( spy );
  288. setData( editor.model, '[<image src="foo.jpg"></image>]<paragraph>bar</paragraph>' );
  289. spy.resetHistory();
  290. window.dispatchEvent( new Event( 'resize' ) );
  291. sinon.assert.notCalled( spy );
  292. } );
  293. } );
  294. } );